Files
bruno/packages/bruno-cli/tests/utils/parse-environment-json.spec.js
Sanjai Kumar 8d2f087206 feat: enhance json environment file support in bruno-cli (#5660)
* feat: enhance json environment file support in bruno-cli

feat: add parseEnvironmentJson function to normalize environment JSON structure

lint fixes

feat: added tests for invalid JSON environment files in CLI and added missing constant defenition.

feat: improve JSON environment file handling and update tests

Trigger test

fix: update CLI command syntax for non-existent JSON environment file test

fix: correct CLI command syntax in test for non-existent JSON environment file

fix: update CLI command syntax in test for non-existent JSON environment file

fix: update test to use temporary path for non-existent JSON environment file

trying to fix the tests

fix tests

refactor: rename ERROR_INVALID_JSON to ERROR_INVALID_FILE and update related error handling in CLI commands and tests

fix: update parseEnvironmentJson to preserve secret flag

test: improved tests

* refactor: move parseEnvironmentJson function to utils/ environment.js file and update imports

* test: update tests
2025-10-07 12:49:22 +05:30

92 lines
2.5 KiB
JavaScript

const { describe, it, expect } = require('@jest/globals');
const { getEnvVars } = require('../../src/utils/bru');
const { parseEnvironmentJson } = require('../../src/utils/environment');
describe('parseEnvironmentJson', () => {
it('normalizes single environment object', () => {
const input = {
name: 'My Env',
variables: [
{ name: 'host', value: 'https://www.httpfaker.org' },
{ name: 'token', value: 'abc', enabled: false, secret: true }
]
};
const env = parseEnvironmentJson(input);
expect(Array.isArray(env.variables)).toBe(true);
expect(env.variables[0]).toEqual({
name: 'host',
value: 'https://www.httpfaker.org',
type: 'text',
enabled: true,
secret: false
});
expect(env.variables[1].enabled).toBe(false);
expect(env.variables[1].secret).toBe(true);
const vars = getEnvVars(env);
expect(vars).toEqual({ host: 'https://www.httpfaker.org' });
});
it('throws on invalid shape', () => {
expect(() => parseEnvironmentJson({ name: 'x' })).toThrow(/Invalid environment JSON/i);
});
it('respects explicit fields and preserves secret flag', () => {
const input = {
name: 'My Env',
variables: [
{ name: 'one', value: '1', type: 'text', enabled: true, secret: true },
{ name: 'two', value: '2', type: 'file', enabled: false, secret: false }
]
};
const env = parseEnvironmentJson(input);
expect(env.variables[0]).toEqual({
name: 'one',
value: '1',
type: 'text',
enabled: true,
secret: true
});
expect(env.variables[1]).toEqual({
name: 'two',
value: '2',
type: 'file',
enabled: false,
secret: false
});
const vars = getEnvVars(env);
expect(vars).toEqual({ one: '1' });
});
it('defaults secret to false for undefined and null', () => {
const input = {
name: 'My Env',
variables: [
{ name: 'three', value: '3', enabled: true },
{ name: 'four', value: '4', enabled: true, secret: null }
]
};
const env = parseEnvironmentJson(input);
expect(env.variables[0]).toEqual({
name: 'three',
value: '3',
type: 'text',
enabled: true,
secret: false
});
expect(env.variables[1]).toEqual({
name: 'four',
value: '4',
type: 'text',
enabled: true,
secret: false
});
const vars = getEnvVars(env);
expect(vars).toEqual({ three: '3', four: '4' });
});
});