fix: preserve existing template when adding parameters
This commit is contained in:
159
src/test/ConfigUpdate.test.ts
Normal file
159
src/test/ConfigUpdate.test.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import { Config } from '../models/Config';
|
||||
import { Env } from '../models/Env';
|
||||
import { EnvParam } from '../models/EnvParam';
|
||||
|
||||
describe('Config Template Update', () => {
|
||||
it('should preserve existing template when adding new parameter', () => {
|
||||
// Arrange: Create config with existing template
|
||||
const config = new Config();
|
||||
const existingTemplate = `{
|
||||
"existingKey": "existingValue",
|
||||
"anotherKey": 123
|
||||
}`;
|
||||
config.setTemplate(existingTemplate);
|
||||
|
||||
const env = new Env(0, 'DEFAULT', [
|
||||
new EnvParam(1, 'host', 'localhost'),
|
||||
new EnvParam(2, 'port', '8080')
|
||||
]);
|
||||
config.setEnvs([env]);
|
||||
|
||||
// Act: Update template from env (simulating adding parameters)
|
||||
config.updateTemplateFromEnv(env);
|
||||
|
||||
// Assert: Template should have both existing content and new placeholders
|
||||
const templateContent = config.template.content;
|
||||
const templateObj = JSON.parse(templateContent);
|
||||
|
||||
expect(templateObj.existingKey).toBe('existingValue');
|
||||
expect(templateObj.anotherKey).toBe(123);
|
||||
expect(templateObj['!!! host']).toBe('@host@');
|
||||
expect(templateObj['!!! port']).toBe('@port@');
|
||||
});
|
||||
|
||||
it('should not duplicate placeholders that already exist', () => {
|
||||
// Arrange: Create config with template that already has placeholder
|
||||
const config = new Config();
|
||||
const existingTemplate = `{
|
||||
"host": "@host@",
|
||||
"port": 8080
|
||||
}`;
|
||||
config.setTemplate(existingTemplate);
|
||||
|
||||
const env = new Env(0, 'DEFAULT', [
|
||||
new EnvParam(1, 'host', 'localhost'), // Already in template
|
||||
new EnvParam(2, 'port', '8080') // Already in template
|
||||
]);
|
||||
config.setEnvs([env]);
|
||||
|
||||
// Act: Update template
|
||||
config.updateTemplateFromEnv(env);
|
||||
|
||||
// Assert: Should not add !!! entries for existing placeholders
|
||||
const templateContent = config.template.content;
|
||||
// Count occurrences of @host@
|
||||
const hostCount = (templateContent.match(/@host@/g) || []).length;
|
||||
expect(hostCount).toBe(1); // Only original, no duplicate
|
||||
|
||||
// Should preserve original structure
|
||||
expect(templateContent).toContain('"host": "@host@"');
|
||||
});
|
||||
|
||||
it('should initialize empty template', () => {
|
||||
// Arrange: Create config with empty template
|
||||
const config = new Config();
|
||||
config.setTemplate('');
|
||||
|
||||
const env = new Env(0, 'DEFAULT', [
|
||||
new EnvParam(1, 'host', 'localhost')
|
||||
]);
|
||||
config.setEnvs([env]);
|
||||
|
||||
// Act: Update template
|
||||
config.updateTemplateFromEnv(env);
|
||||
|
||||
// Assert: Should create template with placeholder
|
||||
const templateContent = config.template.content;
|
||||
const templateObj = JSON.parse(templateContent);
|
||||
expect(templateObj['!!! host']).toBe('@host@');
|
||||
});
|
||||
|
||||
it('should not modify invalid JSON template', () => {
|
||||
// Arrange: Create config with invalid JSON template
|
||||
const config = new Config();
|
||||
const invalidTemplate = `{
|
||||
"key": "value" // missing comma
|
||||
"another": "key"
|
||||
}`;
|
||||
config.setTemplate(invalidTemplate);
|
||||
const originalTemplate = config.template.content;
|
||||
|
||||
const env = new Env(0, 'DEFAULT', [
|
||||
new EnvParam(1, 'host', 'localhost')
|
||||
]);
|
||||
config.setEnvs([env]);
|
||||
|
||||
// Act: Update template (should not modify invalid JSON)
|
||||
config.updateTemplateFromEnv(env);
|
||||
|
||||
// Assert: Template should remain unchanged
|
||||
expect(config.template.content).toBe(originalTemplate);
|
||||
});
|
||||
|
||||
it('should add multiple new placeholders to existing template', () => {
|
||||
// Arrange
|
||||
const config = new Config();
|
||||
const existingTemplate = `{
|
||||
"name": "myapp"
|
||||
}`;
|
||||
config.setTemplate(existingTemplate);
|
||||
|
||||
const env = new Env(0, 'DEFAULT', [
|
||||
new EnvParam(1, 'host', 'localhost'),
|
||||
new EnvParam(2, 'port', '8080'),
|
||||
new EnvParam(3, 'user', 'admin')
|
||||
]);
|
||||
config.setEnvs([env]);
|
||||
|
||||
// Act
|
||||
config.updateTemplateFromEnv(env);
|
||||
|
||||
// Assert
|
||||
const templateObj = JSON.parse(config.template.content);
|
||||
expect(templateObj.name).toBe('myapp');
|
||||
expect(templateObj['!!! host']).toBe('@host@');
|
||||
expect(templateObj['!!! port']).toBe('@port@');
|
||||
expect(templateObj['!!! user']).toBe('@user@');
|
||||
});
|
||||
|
||||
it('should only add placeholders for params not in template', () => {
|
||||
// Arrange: Template has @host@ but not @port@
|
||||
const config = new Config();
|
||||
config.setTemplate(`{
|
||||
"host": "@host@",
|
||||
"name": "test"
|
||||
}`);
|
||||
|
||||
const env = new Env(0, 'DEFAULT', [
|
||||
new EnvParam(1, 'host', 'localhost'), // In template
|
||||
new EnvParam(2, 'port', '8080') // Not in template
|
||||
]);
|
||||
config.setEnvs([env]);
|
||||
|
||||
// Act
|
||||
config.updateTemplateFromEnv(env);
|
||||
|
||||
// Assert
|
||||
const templateContent = config.template.content;
|
||||
const templateObj = JSON.parse(templateContent);
|
||||
|
||||
// Should preserve original
|
||||
expect(templateObj.host).toBe('@host@');
|
||||
expect(templateObj.name).toBe('test');
|
||||
|
||||
// Should add only missing
|
||||
expect(templateObj['!!! port']).toBe('@port@');
|
||||
expect(templateObj['!!! host']).toBeUndefined(); // Not added, already exists
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user