Files
bruno/packages/bruno-electron/tests/utils/workspace-config.spec.js
Sanjai Kumar 71227224dd fix: collection reorder not persisting after restart (#7093)
* feat: implement workspace collection reordering functionality

feat: add tests and improve collection reordering functionality

fix: handle IPC errors during collection reordering and update tests for persistence validation

refactor: enhance collection reordering logic and update related tests for path consistency

fix: prevent unnecessary promise resolution in collection reordering when no collections are present

* fix: ensure consistent path normalization for workspace collections during reordering
2026-02-20 14:00:44 +05:30

77 lines
2.5 KiB
JavaScript

const path = require('path');
const fs = require('fs');
const os = require('os');
const yaml = require('js-yaml');
const { reorderWorkspaceCollections } = require('../../src/utils/workspace-config');
const collection = (name, pathSegment) => ({ name, path: pathSegment });
describe('reorderWorkspaceCollections', () => {
let workspacePath;
/** Writes workspace.yml with the given collections (relative paths). */
const writeWorkspaceYml = (collections) => {
const content = [
'opencollection: 1.0.0',
'info:',
' name: Test',
' type: workspace',
'collections:',
...collections.flatMap((c) => [` - name: ${c.name}`, ` path: ${c.path}`]),
'specs: []',
'docs: \'\''
].join('\n');
fs.writeFileSync(path.join(workspacePath, 'workspace.yml'), content);
};
/** Returns collection paths (relative) in order as stored in workspace.yml. */
const getCollectionPathsFromYml = () => {
const raw = fs.readFileSync(path.join(workspacePath, 'workspace.yml'), 'utf8');
const config = yaml.load(raw);
return (config.collections || []).map((c) => c.path);
};
/** Resolves a relative collection path segment to an absolute path under the current workspace. */
const absPath = (relativePath) => path.resolve(workspacePath, relativePath);
beforeEach(() => {
workspacePath = fs.mkdtempSync(path.join(os.tmpdir(), 'bruno-ws-'));
});
afterEach(() => {
fs.rmSync(workspacePath, { recursive: true, force: true });
});
test('reorders collections to match given path list', async () => {
writeWorkspaceYml([
collection('API', 'collections/api'),
collection('Backend', 'collections/backend'),
collection('Frontend', 'collections/frontend')
]);
await reorderWorkspaceCollections(workspacePath, [
absPath('collections/frontend'),
absPath('collections/api'),
absPath('collections/backend')
]);
expect(getCollectionPathsFromYml()).toEqual(['collections/frontend', 'collections/api', 'collections/backend']);
});
test('deduplicates when reorder list contains duplicate paths', async () => {
writeWorkspaceYml([
collection('API', 'collections/api'),
collection('Backend', 'collections/backend')
]);
await reorderWorkspaceCollections(workspacePath, [
absPath('collections/api'),
absPath('collections/backend'),
absPath('collections/api'),
absPath('collections/api')
]);
expect(getCollectionPathsFromYml()).toEqual(['collections/api', 'collections/backend']);
});
});