Files
bruno/packages/bruno-lang/v2/tests/jsonToBru.spec.js
Sid 0bba66a590 feat: variable descriptions (#8269)
* feat: add description column to various tables and enhance UI interactions
- Introduced a description column in Headers, VarsTable, and other components to allow multi-line descriptions.
- Added toggle buttons to show/hide the description column in relevant tables.
- Updated EditableTable component to support dynamic row addition with customizable labels.
- Enhanced UI for better user experience with consistent button styles and spacing adjustments.

* chore: resize fix handles

* fix: restrict column check for last index

* refactor: remove unwanted style and fix bottom padding of the last child in the table row

* tests: improve testability and fix impacted locators

* tests: fix locators for value fields

* fix: improve newline handling in description prefix

* fix: ts changes

* chore: remove redundant code and fix description roundtrip

* chore: remove redundant check

* tests(bru-lang): tests for description and annotation

* test: improve locators and tests

* tests: descriptions

* chore: re-add description setup

* fix: re-add description cols

* chore: fix the double click reset

* fix: account for hidden sidebar in request pane width calculation

* refactor: ui/ux fixes for data type toggle

* chore: inline common deps into bruno-lang and bruno-schema

* chore: tests

* chore: fix ux for descriptions

* fix: layout for environment vars table

* fix: ensure correct row selection for multipart file upload in tests

* feat: enhance UID assignment for request headers and variables in collections

* chore: simplify

* chore: update pkg

* chore: abstract the e2e utils

* chore: fix exports

* tests: fix locators for datatype vars

* fix: ux issue with secrets in environment table

* tests: update locators for collection headers and vars descriptions

* tests: update locators to use data attributes for datatype selectors

* chore: update default css width for actions column

* chore: remove tooltip for string type warnings

* fix: reduce name widths

* refactor: update annotation serialization to use single-quote delimiters for descriptions

* refactor(tests): simplify description formatting in jsonToEnv tests

* feat: add multiline description escaping and unescaping functions with tests

* refactor: clean up imports and improve multiline text block handling

* refactor: update locators to use new test IDs for environment variable editors

* refactor: streamline header input handling and improve environment variable row access

* refactor: update e2e-test job to support self-hosted runners

* refactor: update E2E test runner configuration to include e2e and macOS environments

---------

Co-authored-by: Pragadesh-45 <temporaryg7904@gmail.com>
2026-07-07 21:43:39 +05:30

442 lines
13 KiB
JavaScript

const stringify = require('../src/jsonToBru');
describe('jsonToBru stringify', () => {
describe('body:ws', () => {
it('stringifies a valid bruno request | smoke', () => {
const input = {
ws: {
url: 'ws://localhost:3000',
body: 'ws'
},
body: {
mode: 'ws',
ws: [
{
content: '{"foo":"bar"}',
name: 'message 1',
type: 'json'
}
]
},
settings: {
keepAliveInterval: 30,
timeout: 250
}
};
const output = stringify(input);
// generic structure snapshot
expect(output).toMatchInlineSnapshot(`
"ws {
url: ws://localhost:3000
body: ws
}
body:ws {
name: message 1
type: json
content: '''
{"foo":"bar"}
'''
}
settings {
keepAliveInterval: 30
timeout: 250
}
"
`);
// Hard check if the input settings were stored as is
expect(output).toMatch(new RegExp(`keepAliveInterval: ${input.settings.keepAliveInterval}`));
expect(output).toMatch(new RegExp(`timeout: ${input.settings.timeout}`));
});
});
describe('body:multipart-form file values', () => {
it('stringifies an empty file value without a leading pipe', () => {
const input = {
body: {
multipartForm: [
{
name: 'file',
value: [],
enabled: true,
type: 'file',
contentType: ''
}
]
}
};
const output = stringify(input);
expect(output).toContain('file: @file()');
expect(output).not.toContain('@file(|');
});
it('drops empty entries when stringifying multiple file paths', () => {
const input = {
body: {
multipartForm: [
{
name: 'file',
value: ['', '/path/to/file.csv'],
enabled: true,
type: 'file',
contentType: ''
}
]
}
};
const output = stringify(input);
expect(output).toContain('file: @file(/path/to/file.csv)');
expect(output).not.toContain('@file(|');
});
});
describe('multi-line values', () => {
it('handles multi-line values in URL, headers, params, and vars', () => {
const input = {
meta: {
name: 'new-line',
type: 'http',
seq: 1
},
http: {
method: 'get',
url: 'https://httpbin.io/anything?foo=hello\nworld',
body: 'none',
auth: 'oauth2'
},
params: [
{
name: 'foo',
value: 'hello\nworld',
enabled: true,
type: 'query'
}
],
headers: [
{
name: 'test header',
value: 't1\nt2',
enabled: true
}
],
vars: {
req: [
{
name: 'test-var',
value: 't1\nt2',
enabled: true
}
]
}
};
const output = stringify(input);
expect(output).toMatchInlineSnapshot(`
"meta {
name: new-line
type: http
seq: 1
}
get {
url: '''
https://httpbin.io/anything?foo=hello
world
'''
body: none
auth: oauth2
}
params:query {
foo: '''
hello
world
'''
}
headers {
"test header": '''
t1
t2
'''
}
vars:pre-request {
test-var: '''
t1
t2
'''
}
"
`);
});
});
describe('description annotation', () => {
it('emits @description with single quotes for headers, params, vars, and assertions when present in JSON', () => {
const input = {
meta: { name: 'desc-test', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Custom', value: 'val', enabled: true, description: 'Custom header note' }
],
params: [
{ name: 'q', value: 'search', enabled: true, type: 'query', description: 'Query param hint' }
],
vars: {
req: [
{ name: 'apiKey', value: 'key123', enabled: true, description: 'Pre-request API key' }
]
},
assertions: [
{ name: 'res.status', value: 'eq 200', enabled: true, description: 'Expect OK' }
]
};
const output = stringify(input);
expect(output).toMatch(/@description\('Custom header note'\)\n X-Custom: val/);
expect(output).toMatch(/@description\('Query param hint'\)\n q: search/);
expect(output).toMatch(/@description\('Pre-request API key'\)\n apiKey: key123/);
expect(output).toMatch(/@description\('Expect OK'\)\n res\.status: eq 200/);
});
it('emits triple-quoted description with literal newlines when description is multiline', () => {
const input = {
meta: { name: 'ml', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Note', value: 'v', enabled: true, description: 'Line one\nLine two' }
]
};
const output = stringify(input);
expect(output).toContain('@description(\'\'\'\n Line one\n Line two\n \'\'\')\n X-Note: v');
});
it('emits double-quoted description when description contains triple quote', () => {
const input = {
meta: { name: 'tq', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Desc', value: 'v', enabled: true, description: 'Say \'\'\'triple\'\'\'' }
]
};
const output = stringify(input);
expect(output).toMatch(/@description\("Say '''triple'''"\)/);
});
it('emits single-quoted description when value contains double quotes', () => {
const input = {
meta: { name: 'esc', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Desc', value: 'v', enabled: true, description: 'Say "hello"' }
]
};
const output = stringify(input);
expect(output).toMatch(/@description\('Say "hello"'\)/);
});
it('emits triple-quoted description with emoji', () => {
const input = {
meta: { name: 'emoji', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'Authorization', value: 'Bearer xxx', enabled: true, description: 'Auth token 🔑' },
{ name: 'X-Region', value: 'us-east', enabled: true, description: 'Region 🌍 selector' }
]
};
const output = stringify(input);
expect(output).toMatch(/@description\('Auth token 🔑'\)/);
expect(output).toMatch(/@description\('Region 🌍 selector'\)/);
});
it('emits multiline triple-quoted description with emoji', () => {
const input = {
meta: { name: 'emoji-ml', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Launch', value: 'val', enabled: true, description: 'Launch 🚀\nSecond line' }
]
};
const output = stringify(input);
expect(output).toContain('@description(\'\'\'\n Launch 🚀\n Second line\n \'\'\')\n X-Launch: val');
});
it('emits multiline triple-quoted description with \\n (LF)', () => {
const input = {
meta: { name: 'lf', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Note', value: 'v', enabled: true, description: 'First\nSecond\nThird' }
]
};
const output = stringify(input);
expect(output).toContain('@description(\'\'\'\n First\n Second\n Third\n \'\'\')\n X-Note: v');
});
it('emits multiline triple-quoted description with \\r\\n (CRLF) normalized to LF', () => {
const input = {
meta: { name: 'crlf', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Note', value: 'v', enabled: true, description: 'Line one\r\nLine two' }
]
};
const output = stringify(input);
// indentString splits on \r\n|\r|\n and rejoins with \n, normalizing Windows CRLF to LF
expect(output).toContain('@description(\'\'\'\n Line one\n Line two\n \'\'\')\n X-Note: v');
});
it('emits multiline triple-quoted description with multiple \\r\\n lines normalized to LF', () => {
const input = {
meta: { name: 'crlf-multi', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Note', value: 'v', enabled: true, description: 'Line one\r\nLine two\r\nLine three' }
]
};
const output = stringify(input);
expect(output).toContain('@description(\'\'\'\n Line one\n Line two\n Line three\n \'\'\')\n X-Note: v');
});
it('emits single line single-quoted description when single lined', () => {
const input = {
meta: { name: 'single-line-single-quoted', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Note', value: 'v', enabled: true, description: 'Line one' }
]
};
const output = stringify(input);
expect(output).toContain('@description(\'Line one\'');
});
it('emits multiline triple-quoted description when multi line description', () => {
const input = {
meta: { name: 'multi-line-triple-quoted', type: 'http', seq: 1 },
http: { method: 'get', url: 'https://example.com', body: 'none' },
headers: [
{ name: 'X-Note', value: 'v', enabled: true, description: 'Line one\nLine two\nLine three' }
]
};
const output = stringify(input);
expect(output).toContain('@description(\'\'\'\n Line one\n Line two\n Line three\n \'\'\')\n X-Note: v');
});
});
describe('vars:pre-request dataType decorators', () => {
const baseMeta = { name: 'test', type: 'http', seq: 1 };
const baseHttp = { method: 'get', url: 'http://localhost' };
it('emits dataType decorators for typed variables', () => {
const output = stringify({
meta: baseMeta,
http: baseHttp,
vars: {
req: [
{ name: 'apiKey', value: 'abc', enabled: true, local: false },
{ name: 'port', value: 3000, enabled: true, local: false, dataType: 'number' },
{ name: 'flag', value: true, enabled: true, local: false, dataType: 'boolean' }
]
}
});
expect(output).toContain('apiKey: abc');
expect(output).toContain('@number\n port: 3000');
expect(output).toContain('@boolean\n flag: true');
});
it('serializes @object values as multiline JSON', () => {
const output = stringify({
meta: baseMeta,
http: baseHttp,
vars: {
req: [
{ name: 'config', value: { a: 1, b: 'x' }, enabled: true, local: false, dataType: 'object' }
]
}
});
expect(output).toContain('@object');
expect(output).toContain('"a": 1');
expect(output).toContain('"b": "x"');
});
it('preserves local, disabled and disabled+local prefixes alongside dataType', () => {
const output = stringify({
meta: baseMeta,
http: baseHttp,
vars: {
req: [
{ name: 'a', value: 1, enabled: true, local: true, dataType: 'number' },
{ name: 'b', value: 2, enabled: false, local: false, dataType: 'number' },
{ name: 'c', value: 3, enabled: false, local: true, dataType: 'number' }
]
}
});
expect(output).toContain('@number\n @a: 1');
expect(output).toContain('@number\n ~b: 2');
expect(output).toContain('@number\n ~@c: 3');
});
it('does not emit a dataType decorator for the string default', () => {
const output = stringify({
meta: baseMeta,
http: baseHttp,
vars: {
req: [
{ name: 'apiKey', value: 'abc', enabled: true, local: false, dataType: 'string' }
]
}
});
expect(output).not.toContain('@string');
expect(output).toContain('apiKey: abc');
});
it('drops a stale dataType annotation in favour of the dataType field', () => {
const output = stringify({
meta: baseMeta,
http: baseHttp,
vars: {
req: [
{
name: 'port',
value: 3000,
enabled: true,
local: false,
annotations: [{ name: 'string' }, { name: 'description', value: 'service port' }],
dataType: 'number'
}
]
}
});
expect(output).toContain('@number');
expect(output).not.toContain('@string');
expect(output).toContain('@description(\'service port\')');
});
});
});