fix(cli): preserve request item type during import and fail on unsupported types (#7207)

* fix(cli): preserve request type when importing collections

* fix(cli): fail fast on unsupported imported item types

* fix(cli): force BRU format when writing imported files

* chore: apply code rabbit fixes

* chore: adress review comment - keep changes minimal

* add additional test to test bru folder format

* agree with coderabbit, error handling is required

---------

Co-authored-by: Ramesh Sunkara <rs@rsunkara.com>
This commit is contained in:
Ram
2026-02-19 10:13:44 -05:00
committed by GitHub
parent d060544da6
commit 09b8e8a32a
2 changed files with 151 additions and 6 deletions

View File

@@ -0,0 +1,143 @@
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { describe, it, expect, afterEach } = require('@jest/globals');
const { parseRequest, parseFolder } = require('@usebruno/filestore');
const { createCollectionFromBrunoObject } = require('../../../src/utils/collection');
describe('createCollectionFromBrunoObject', () => {
let outputDir;
const createOutputDir = () => {
outputDir = fs.mkdtempSync(path.join(os.tmpdir(), 'bruno-cli-import-'));
return outputDir;
};
const parseBruRequestFromPath = (filePath) => parseRequest(fs.readFileSync(filePath, 'utf8'), { format: 'bru' });
const parseBruFolderFromPath = (filePath) => parseFolder(fs.readFileSync(filePath, 'utf8'), { format: 'bru' });
afterEach(() => {
if (outputDir && fs.existsSync(outputDir)) {
fs.rmSync(outputDir, { recursive: true, force: true });
}
});
it('writes http and graphql requests from imported collection items', async () => {
createOutputDir();
await createCollectionFromBrunoObject(
{
name: 'imported-collection',
items: [
{
type: 'http-request',
name: 'Get Users',
filename: 'get-users.bru',
seq: 1,
request: {
method: 'GET',
url: 'https://api.example.com/users'
}
},
{
type: 'graphql-request',
name: 'Get Viewer',
filename: 'get-viewer.bru',
seq: 2,
request: {
method: 'POST',
url: 'https://api.example.com/graphql',
body: {
mode: 'graphql',
graphql: {
query: 'query { viewer { id } }',
variables: '{}'
}
}
}
}
]
},
outputDir
);
const httpPath = path.join(outputDir, 'get-users.bru');
const graphqlPath = path.join(outputDir, 'get-viewer.bru');
expect(fs.existsSync(httpPath)).toBe(true);
expect(fs.existsSync(graphqlPath)).toBe(true);
const httpRequest = parseBruRequestFromPath(httpPath);
const graphqlRequest = parseBruRequestFromPath(graphqlPath);
expect(httpRequest).toHaveProperty('type', 'http-request');
expect(httpRequest).toHaveProperty('request.method', 'GET');
expect(graphqlRequest).toHaveProperty('type', 'graphql-request');
expect(graphqlRequest).toHaveProperty('request.method', 'POST');
});
it('writes folder.bru in bru format', async () => {
createOutputDir();
await createCollectionFromBrunoObject(
{
name: 'folder-collection',
items: [
{
type: 'folder',
name: 'Users',
seq: 3,
root: {
meta: { name: 'Users' }
},
items: [
{
type: 'http-request',
name: 'List Users',
filename: 'list-users.bru',
seq: 1,
request: {
method: 'GET',
url: 'https://api.example.com/users'
}
}
]
}
]
},
outputDir
);
const folderPath = path.join(outputDir, 'Users');
const folderBruPath = path.join(folderPath, 'folder.bru');
const nestedRequestPath = path.join(folderPath, 'list-users.bru');
expect(fs.existsSync(folderBruPath)).toBe(true);
expect(fs.existsSync(nestedRequestPath)).toBe(true);
const folder = parseBruFolderFromPath(folderBruPath);
const nestedRequest = parseBruRequestFromPath(nestedRequestPath);
expect(folder).toHaveProperty('meta.name', 'Users');
expect(folder).toHaveProperty('meta.seq', 3);
expect(nestedRequest).toHaveProperty('type', 'http-request');
expect(nestedRequest).toHaveProperty('request.method', 'GET');
});
it('throws for unsupported item types', async () => {
createOutputDir();
await expect(
createCollectionFromBrunoObject(
{
name: 'invalid-item-type-collection',
items: [
{
type: 'unsupported-type',
name: 'Unsupported'
}
]
},
outputDir
)
).rejects.toThrow('Unsupported item type: unsupported-type');
});
});