Files
bruno/packages/bruno-lang/v2/tests/jsonToEnv.spec.js
2026-06-19 19:36:59 +05:30

355 lines
7.6 KiB
JavaScript

const parser = require('../src/jsonToEnv');
describe('jsonToEnv', () => {
it('should stringify empty vars', () => {
const input = {
variables: []
};
const output = parser(input);
const expected = `vars {
}
`;
expect(output).toEqual(expected);
});
it('should stringify single var line', () => {
const input = {
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
}
]
};
const output = parser(input);
const expected = `vars {
url: http://localhost:3000
}
`;
expect(output).toEqual(expected);
});
it('should stringify multiple var lines', () => {
const input = {
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
},
{
name: 'port',
value: '3000',
enabled: false
}
]
};
const expected = `vars {
url: http://localhost:3000
~port: 3000
}
`;
const output = parser(input);
expect(output).toEqual(expected);
});
it('should stringify secret vars', () => {
const input = {
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
},
{
name: 'token',
value: 'abracadabra',
enabled: true,
secret: true
}
]
};
const output = parser(input);
const expected = `vars {
url: http://localhost:3000
}
vars:secret [
token
]
`;
expect(output).toEqual(expected);
});
it('should stringify multiple secret vars', () => {
const input = {
variables: [
{
name: 'url',
value: 'http://localhost:3000',
enabled: true
},
{
name: 'access_token',
value: 'abracadabra',
enabled: true,
secret: true
},
{
name: 'access_secret',
value: 'abracadabra',
enabled: false,
secret: true
}
]
};
const output = parser(input);
const expected = `vars {
url: http://localhost:3000
}
vars:secret [
access_token,
~access_secret
]
`;
expect(output).toEqual(expected);
});
it('should stringify even if the only secret vars are present', () => {
const input = {
variables: [
{
name: 'token',
value: 'abracadabra',
enabled: true,
secret: true
}
]
};
const output = parser(input);
const expected = `vars:secret [
token
]
`;
expect(output).toEqual(expected);
});
it('should stringify multiline variables', () => {
const input = {
variables: [
{
name: 'json_data',
value: '{\n "name": "test",\n "value": 123\n}',
enabled: true
}
]
};
const output = parser(input);
const expected = `vars {
json_data: '''
{
"name": "test",
"value": 123
}
'''
}
`;
expect(output).toEqual(expected);
});
it('should stringify multiline variables containing indentation', () => {
const input = {
variables: [
{
name: 'script',
value: 'function test() {\n console.log("hello");\n return true;\n}',
enabled: true
}
]
};
const output = parser(input);
const expected = `vars {
script: '''
function test() {
console.log("hello");
return true;
}
'''
}
`;
expect(output).toEqual(expected);
});
it('should stringify disabled multiline variable', () => {
const input = {
variables: [
{
name: 'disabled_multiline',
value: 'line 1\nline 2\nline 3',
enabled: false
}
]
};
const output = parser(input);
const expected = `vars {
~disabled_multiline: '''
line 1
line 2
line 3
'''
}
`;
expect(output).toEqual(expected);
});
it('should stringify multiple multiline variables', () => {
const input = {
variables: [
{
name: 'config',
value: 'debug=true\nport=3000',
enabled: true
},
{
name: 'template',
value: '<html>\n <body>Hello World</body>\n</html>',
enabled: true
}
]
};
const output = parser(input);
const expected = `vars {
config: '''
debug=true
port=3000
'''
template: '''
<html>
<body>Hello World</body>
</html>
'''
}
`;
expect(output).toEqual(expected);
});
describe('typed environment variables', () => {
it('should serialize @number dataType as a decorator', () => {
const input = {
variables: [
{ name: 'port', value: 3000, enabled: true, dataType: 'number' }
]
};
const output = parser(input);
expect(output).toEqual(`vars {
@number
port: 3000
}
`);
});
it('should serialize @boolean dataType as a decorator', () => {
const input = {
variables: [
{ name: 'isEnabled', value: true, enabled: true, dataType: 'boolean' }
]
};
const output = parser(input);
expect(output).toEqual(`vars {
@boolean
isEnabled: true
}
`);
});
it('should serialize @object dataType with JSON-stringified multiline value', () => {
const input = {
variables: [
{ name: 'config', value: { a: 1, b: 'x' }, enabled: true, dataType: 'object' }
]
};
const output = parser(input);
expect(output).toContain('@object');
expect(output).toContain('"a": 1');
expect(output).toContain('"b": "x"');
});
it('should not emit a decorator for string dataType', () => {
const input = {
variables: [
{ name: 'apiKey', value: 'abc123', enabled: true, dataType: 'string' }
]
};
const output = parser(input);
expect(output).toEqual(`vars {
apiKey: abc123
}
`);
});
it('should drop dataType annotations from existing list and rebuild from dataType field', () => {
const input = {
variables: [
{
name: 'port',
value: 3000,
enabled: true,
annotations: [{ name: 'string' }, { name: 'description', value: 'service port' }],
dataType: 'number'
}
]
};
const output = parser(input);
// @string from old annotations should be dropped, @number should be set from dataType
expect(output).toContain('@number');
expect(output).not.toContain('@string');
expect(output).toContain('@description(\'service port\')');
});
it('should emit dataType but not the value for secret vars', () => {
const input = {
variables: [
{ name: 'api_key', value: 'redacted', enabled: true, secret: true, dataType: 'number' }
]
};
const output = parser(input);
// secret vars use the vars:secret block: the dataType is emitted as a
// decorator, but the value is never written to disk.
expect(output).toContain('vars:secret');
expect(output).toContain('api_key');
expect(output).toContain('@number');
expect(output).not.toContain('redacted');
});
it('should round-trip non-string values through getValueString', () => {
const input = {
variables: [
{ name: 'port', value: 8080, enabled: true, dataType: 'number' },
{ name: 'flag', value: false, enabled: true, dataType: 'boolean' }
]
};
const output = parser(input);
expect(output).toContain('port: 8080');
expect(output).toContain('flag: false');
});
});
});