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']); }); });