From a58ff0dda6d143259fd249dc220369d3912b8047 Mon Sep 17 00:00:00 2001 From: ravindra-bruno Date: Thu, 9 Jul 2026 22:09:59 +0530 Subject: [PATCH 1/3] fix(migration): migrated collection to yml in-place to preserve open tabs and uids --- .../ReduxStore/slices/collections/actions.js | 52 ++---------- .../ReduxStore/slices/collections/index.js | 34 ++++++++ .../src/providers/ReduxStore/slices/tabs.js | 9 ++ packages/bruno-electron/src/ipc/collection.js | 8 ++ .../collections/other-collection/bruno.json | 9 ++ .../collections/other-collection/keep-req.bru | 11 +++ .../source-collection/api/users.bru | 11 +++ .../source-collection/api/v2/deep.bru | 11 +++ .../collections/source-collection/bruno.json | 9 ++ .../source-collection/root-req.bru | 11 +++ .../init-user-data/preferences.json | 12 +++ .../migrate-to-yml-nested-multi.spec.ts | 85 +++++++++++++++++++ .../keep-request-tab-open.spec.ts | 67 +++++++++++++++ .../migrate-to-yml/migrate-to-yml.spec.ts | 74 ---------------- 14 files changed, 285 insertions(+), 118 deletions(-) create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/bruno.json create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/keep-req.bru create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/users.bru create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/deep.bru create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/bruno.json create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/root-req.bru create mode 100644 tests/collection/migrate-to-yml-nested-multi/init-user-data/preferences.json create mode 100644 tests/collection/migrate-to-yml-nested-multi/migrate-to-yml-nested-multi.spec.ts create mode 100644 tests/collection/migrate-to-yml/keep-request-tab-open.spec.ts diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js index 468098f2eb7..d092f3ad999 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/actions.js @@ -67,11 +67,11 @@ import { addTransientDirectory, addSaveTransientRequestModal, updatePathParam, - toggleCollection + migrateCollectionToYmlInPlace } from './index'; import { each } from 'lodash'; -import { closeAllCollectionTabs, closeTabs as _closeTabs, focusTab, restoreTabs, reopenLastClosedTab } from 'providers/ReduxStore/slices/tabs'; +import { closeAllCollectionTabs, closeTabs as _closeTabs, focusTab, restoreTabs, reopenLastClosedTab, migrateCollectionTabsToYml } from 'providers/ReduxStore/slices/tabs'; import { clearOpenApiSyncTabState } from 'providers/ReduxStore/slices/openapi-sync'; import { removeCollectionFromWorkspace } from 'providers/ReduxStore/slices/workspaces'; import { resolveRequestFilename } from 'utils/common/platform'; @@ -3557,54 +3557,18 @@ export const migrateCollectionToYml = (collectionUid) => (dispatch, getState) => } const collectionPathname = collection.pathname; - const uid = collection.uid; ipcRenderer .invoke('renderer:migrate-collection-to-yml', collectionPathname, collectionUid) - .then(async (updatedBrunoConfig) => { - // Remove old collection state so we can recreate it with the new yml config. - // openCollectionEvent requires no existing collection with the same pathname. - dispatch(_removeCollection({ collectionUid })); - dispatch(closeAllCollectionTabs({ collectionUid })); - - try { - // Reopen the collection with updated config (now yml format) - await dispatch(openCollectionEvent(uid, collectionPathname, updatedBrunoConfig)); - - // Mount the collection (starts the watcher and loads items) - await dispatch(mountCollection({ - collectionUid: uid, - collectionPathname: collectionPathname, - brunoConfig: updatedBrunoConfig - })); - } catch (reopenError) { - // Files on disk are already yml; best-effort recovery so the - // collection doesn't disappear from the UI. openCollectionEvent is - // a no-op if it already succeeded, and mountCollection is what we - // retry when it was the failing step. - try { - await dispatch(openCollectionEvent(uid, collectionPathname, updatedBrunoConfig)); - await dispatch(mountCollection({ - collectionUid: uid, - collectionPathname: collectionPathname, - brunoConfig: updatedBrunoConfig - })); - } catch (_) { } - throw reopenError; - } - - // Expand the collection in the sidebar (only if collapsed) - const reopenedCollection = findCollectionByUid(getState().collections.collections, uid); - if (reopenedCollection?.collapsed) { - dispatch(toggleCollection(uid)); - } + .then((updatedBrunoConfig) => { + dispatch(migrateCollectionToYmlInPlace({ collectionUid, brunoConfig: updatedBrunoConfig })); + dispatch(migrateCollectionTabsToYml({ collectionUid })); - // Reopen collection settings on the overview tab dispatch(addTab({ - uid: uid, - collectionUid: uid, + uid: collectionUid, + collectionUid: collectionUid, type: 'collection-settings' })); - dispatch(updateSettingsSelectedTab({ collectionUid: uid, tab: 'overview' })); + dispatch(updateSettingsSelectedTab({ collectionUid: collectionUid, tab: 'overview' })); toast.success('Collection migrated to YML format successfully'); resolve(); diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 2f9d33f2dd8..405fd76dfb4 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -313,6 +313,39 @@ export const collectionsSlice = createSlice({ collection.brunoConfig = brunoConfig; } }, + migrateCollectionToYmlInPlace: (state, action) => { + const { collectionUid, brunoConfig } = action.payload; + const collection = findCollectionByUid(state.collections, collectionUid); + if (!collection) { + return; + } + + if (brunoConfig) { + collection.brunoConfig = brunoConfig; + } + collection.format = 'yml'; + + const rewriteItemPaths = (items) => { + (items || []).forEach((item) => { + if (typeof item.pathname === 'string') { + item.pathname = item.pathname.replace(/\.bru$/, '.yml'); + } + if (typeof item.filename === 'string') { + item.filename = item.filename.replace(/\.bru$/, '.yml'); + } + if (item.items && item.items.length) { + rewriteItemPaths(item.items); + } + }); + }; + rewriteItemPaths(collection.items); + + (collection.environments || []).forEach((environment) => { + if (typeof environment.pathname === 'string') { + environment.pathname = environment.pathname.replace(/\.bru$/, '.yml'); + } + }); + }, renameCollection: (state, action) => { const collection = findCollectionByUid(state.collections, action.payload.collectionUid); @@ -3900,6 +3933,7 @@ export const { setCollectionSecurityConfig, updateCollectionVersion, brunoConfigUpdateEvent, + migrateCollectionToYmlInPlace, renameCollection, removeCollection, sortCollections, diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/tabs.js b/packages/bruno-app/src/providers/ReduxStore/slices/tabs.js index 405bff3cf58..356501d93cb 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/tabs.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/tabs.js @@ -397,6 +397,14 @@ export const tabsSlice = createSlice({ console.error('Tab not found!'); } }, + migrateCollectionTabsToYml: (state, action) => { + const { collectionUid } = action.payload; + state.tabs.forEach((tab) => { + if (tab.collectionUid === collectionUid && typeof tab.pathname === 'string') { + tab.pathname = tab.pathname.replace(/\.bru$/, '.yml'); + } + }); + }, collapseRequestPane: (state, action) => { const tab = find(state.tabs, (t) => t.uid === action.payload.uid); if (tab) { @@ -557,6 +565,7 @@ export const { closeTabs, closeAllCollectionTabs, makeTabPermanent, + migrateCollectionTabsToYml, collapseRequestPane, collapseResponsePane, expandRequestPane, diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 6f27f171294..bac046a5c1a 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -2779,6 +2779,14 @@ const registerRendererEventHandlers = (mainWindow, watcher) => { ymlBrunoConfig.size = size; ymlBrunoConfig.filesCount = filesCount; + if (watcher) { + try { + watcher.addWatcher(mainWindow, collectionPathname, collectionUid, ymlBrunoConfig, false, undefined, { ignoreInitial: true }); + } catch (watcherError) { + console.error('Failed to re-attach watcher after migration:', watcherError); + } + } + return ymlBrunoConfig; } catch (error) { for (const ymlFile of writtenYmlFiles) { diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/bruno.json b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/bruno.json new file mode 100644 index 00000000000..a1015fa1b31 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "keep-open", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/keep-req.bru b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/keep-req.bru new file mode 100644 index 00000000000..d954fdf8993 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/other-collection/keep-req.bru @@ -0,0 +1,11 @@ +meta { + name: Keep Me + type: http + seq: 1 +} + +get { + url: https://example.com/keep + body: none + auth: none +} diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/users.bru b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/users.bru new file mode 100644 index 00000000000..9c65de04c6f --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/users.bru @@ -0,0 +1,11 @@ +meta { + name: users + type: http + seq: 1 +} + +get { + url: https://example.com/api/users + body: none + auth: none +} diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/deep.bru b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/deep.bru new file mode 100644 index 00000000000..900c05be998 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/deep.bru @@ -0,0 +1,11 @@ +meta { + name: deep + type: http + seq: 1 +} + +get { + url: https://example.com/api/v2/deep + body: none + auth: none +} diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/bruno.json b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/bruno.json new file mode 100644 index 00000000000..7a5824483ba --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "migrate-source", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/root-req.bru b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/root-req.bru new file mode 100644 index 00000000000..276223793f0 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/root-req.bru @@ -0,0 +1,11 @@ +meta { + name: root-req + type: http + seq: 1 +} + +get { + url: https://example.com/root + body: none + auth: none +} diff --git a/tests/collection/migrate-to-yml-nested-multi/init-user-data/preferences.json b/tests/collection/migrate-to-yml-nested-multi/init-user-data/preferences.json new file mode 100644 index 00000000000..b03f1fd5987 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/init-user-data/preferences.json @@ -0,0 +1,12 @@ +{ + "lastOpenedCollections": [ + "{{collectionPath}}/source-collection", + "{{collectionPath}}/other-collection" + ], + "preferences": { + "onboarding": { + "hasLaunchedBefore": true, + "hasSeenWelcomeModal": true + } + } +} diff --git a/tests/collection/migrate-to-yml-nested-multi/migrate-to-yml-nested-multi.spec.ts b/tests/collection/migrate-to-yml-nested-multi/migrate-to-yml-nested-multi.spec.ts new file mode 100644 index 00000000000..7dfbe4dad65 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/migrate-to-yml-nested-multi.spec.ts @@ -0,0 +1,85 @@ +import fs from 'fs'; +import path from 'path'; +import { test, expect } from '../../../playwright'; +import { closeAllCollections, openCollection, buildCommonLocators } from '../../utils/page'; + +test.describe('Migrating one collection to yml preserves nested tabs & expanded folders and leaves other collections untouched', () => { + test.afterAll(async ({ page }) => { + await closeAllCollections(page); + }); + + test('nested tabs stay open, nested folders stay expanded, other collection is untouched', async ({ pageWithUserData: page, collectionFixturePath }) => { + const collectionPath = collectionFixturePath!; + const loc = buildCommonLocators(page); + + const pageErrors: Error[] = []; + page.on('pageerror', (error) => pageErrors.push(error)); + + await test.step('Both collections are open', async () => { + await expect(loc.sidebar.collection('migrate-source')).toBeVisible({ timeout: 15000 }); + await expect(loc.sidebar.collection('keep-open')).toBeVisible(); + }); + + await test.step('Expand nested folders and open nested requests as permanent tabs in the source collection', async () => { + await openCollection(page, 'migrate-source'); + await loc.folder.chevron('api').click(); + await loc.folder.chevron('v2').click(); + + await loc.sidebar.request('root-req').dblclick(); + await loc.sidebar.request('users').dblclick(); + await loc.sidebar.request('deep').dblclick(); + + await expect(page.locator('#request-url').locator('.CodeMirror')).toContainText('/api/v2/deep'); + }); + + await test.step('Open a request tab in the other collection', async () => { + await openCollection(page, 'keep-open'); + await loc.sidebar.request('Keep Me').dblclick(); + await expect(page.locator('#request-url').locator('.CodeMirror')).toContainText('/keep'); + }); + + await test.step('Migrate the source collection to yml', async () => { + await loc.sidebar.collection('migrate-source').click(); + await page.getByTestId('collection-settings-tab-overview').click(); + await page.getByRole('button', { name: 'Convert to YML' }).click(); + + const modal = page.locator('.bruno-modal').filter({ hasText: 'Migrate to YML format' }); + await modal.waitFor({ state: 'visible', timeout: 5000 }); + await modal.getByRole('button', { name: 'Migrate' }).click(); + + await expect(page.getByText('Collection migrated to YML format successfully')).toBeVisible({ timeout: 30000 }); + }); + + await test.step('Files migrated on disk, including the deeply nested request', async () => { + expect(fs.existsSync(path.join(collectionPath, 'source-collection', 'api', 'v2', 'deep.yml'))).toBe(true); + expect(fs.existsSync(path.join(collectionPath, 'source-collection', 'api', 'v2', 'deep.bru'))).toBe(false); + expect(fs.existsSync(path.join(collectionPath, 'source-collection', 'api', 'users.yml'))).toBe(true); + }); + + await test.step('All nested request tabs stay open (no "Request no longer exists")', async () => { + await expect(page.getByText('Request no longer exists')).not.toBeVisible(); + + for (const name of ['root-req', 'users', 'deep']) { + await expect(page.locator('.request-tab .tab-label').filter({ hasText: name })).toBeVisible({ timeout: 15000 }); + } + }); + + await test.step('A migrated tab resolves to a real request (not a broken not-found tab)', async () => { + await page.locator('.request-tab').filter({ hasText: 'deep' }).click({ force: true }); + await expect(page.getByText('Request no longer exists')).not.toBeVisible(); + await expect(page.locator('#request-url').locator('.CodeMirror')).toContainText('/api/v2/deep'); + }); + + await test.step('Nested folders remain expanded (nested request rows still visible in the tree)', async () => { + await expect(loc.sidebar.request('users')).toBeVisible(); + await expect(loc.sidebar.request('deep')).toBeVisible(); + }); + + await test.step('The other collection tab is untouched by the migration', async () => { + await loc.sidebar.collection('keep-open').click(); + await expect(page.locator('.request-tab .tab-label').filter({ hasText: 'Keep Me' })).toBeVisible(); + }); + + expect(pageErrors).toHaveLength(0); + }); +}); diff --git a/tests/collection/migrate-to-yml/keep-request-tab-open.spec.ts b/tests/collection/migrate-to-yml/keep-request-tab-open.spec.ts new file mode 100644 index 00000000000..701d77a0ccd --- /dev/null +++ b/tests/collection/migrate-to-yml/keep-request-tab-open.spec.ts @@ -0,0 +1,67 @@ +import fs from 'fs'; +import path from 'path'; +import { test, expect } from '../../../playwright'; +import { closeAllCollections, openCollection, selectEnvironment, sendRequestAndWaitForResponse } from '../../utils/page'; + +test.describe('Migrate collection from bru to yml format — keep open request tab', () => { + test.afterAll(async ({ page }) => { + await closeAllCollections(page); + }); + + test('should keep a request tab open during migration functional (no "Request no longer exists")', async ({ pageWithUserData: page, collectionFixturePath }) => { + const collectionPath = collectionFixturePath!; + + const pageErrors: Error[] = []; + page.on('pageerror', (error) => pageErrors.push(error)); + + await test.step('Open the ping request as a permanent tab', async () => { + await openCollection(page, 'migration-test'); + await page.locator('.item-name').filter({ hasText: 'ping' }).dblclick(); + + const urlContainer = page.locator('#request-url'); + await expect(urlContainer).toBeVisible(); + await expect(urlContainer.locator('.CodeMirror')).toContainText('/ping'); + }); + + await test.step('Migrate the collection to yml', async () => { + await page.locator('#sidebar-collection-name').filter({ hasText: 'migration-test' }).click(); + + const overviewTab = page.getByTestId('collection-settings-tab-overview'); + await expect(overviewTab).toBeVisible(); + await overviewTab.click(); + + const convertButton = page.getByRole('button', { name: 'Convert to YML' }); + await expect(convertButton).toBeVisible(); + await convertButton.click(); + + const modal = page.locator('.bruno-modal').filter({ hasText: 'Migrate to YML format' }); + await modal.waitFor({ state: 'visible', timeout: 5000 }); + await modal.getByRole('button', { name: 'Migrate' }).click(); + + await expect(page.getByText('Collection migrated to YML format successfully')).toBeVisible({ timeout: 30000 }); + }); + + await test.step('The migrated files exist on disk', async () => { + expect(fs.existsSync(path.join(collectionPath, 'ping.yml'))).toBe(true); + expect(fs.existsSync(path.join(collectionPath, 'ping.bru'))).toBe(false); + }); + + await test.step('The ping tab stays open and resolves to the yml request (no "Request no longer exists")', async () => { + await expect(page.getByText('Request no longer exists')).not.toBeVisible(); + + const pingTab = page.locator('.request-tab .tab-label').filter({ hasText: 'ping' }); + await expect(pingTab).toBeVisible({ timeout: 15000 }); + await page.locator('.request-tab').filter({ hasText: 'ping' }).click({ force: true }); + + await expect(page.getByText('Request no longer exists')).not.toBeVisible(); + const urlContainer = page.locator('#request-url'); + await expect(urlContainer).toBeVisible(); + await expect(urlContainer.locator('.CodeMirror')).toContainText('/ping'); + + await selectEnvironment(page, 'Local'); + await sendRequestAndWaitForResponse(page, 200); + }); + + expect(pageErrors).toHaveLength(0); + }); +}); diff --git a/tests/collection/migrate-to-yml/migrate-to-yml.spec.ts b/tests/collection/migrate-to-yml/migrate-to-yml.spec.ts index 12828eb340c..4c4a54715d1 100644 --- a/tests/collection/migrate-to-yml/migrate-to-yml.spec.ts +++ b/tests/collection/migrate-to-yml/migrate-to-yml.spec.ts @@ -136,78 +136,4 @@ test.describe('Migrate collection from bru to yml format', () => { // Verify no uncaught JS errors occurred during migration expect(pageErrors).toHaveLength(0); }); - - test('should keep a request tab open during migration functional (no "Request no longer exists")', async ({ pageWithUserData: page, collectionFixturePath }) => { - const collectionPath = collectionFixturePath!; - - const pageErrors: Error[] = []; - page.on('pageerror', (error) => pageErrors.push(error)); - - const readSnapshotTabPathnames = async (): Promise => { - const tabsSnapshot = await page.evaluate( - (path) => (window as any).ipcRenderer.invoke('renderer:snapshot:get-tabs', path, null), - collectionPath - ); - return ((tabsSnapshot?.tabs || []) as Array<{ pathname?: string }>) - .map((tab) => tab.pathname) - .filter((pathname): pathname is string => typeof pathname === 'string'); - }; - - await test.step('Open the ping request as a permanent tab', async () => { - await openCollection(page, 'migration-test'); - await page.locator('.item-name').filter({ hasText: 'ping' }).dblclick(); - - const urlContainer = page.locator('#request-url'); - await expect(urlContainer).toBeVisible(); - await expect(urlContainer.locator('.CodeMirror')).toContainText('/ping'); - }); - - await test.step('The open tab is persisted to the UI-state snapshot as a .bru tab', async () => { - await expect - .poll(async () => (await readSnapshotTabPathnames()).some((p) => p.endsWith('ping.bru')), { timeout: 10000 }) - .toBe(true); - }); - - await test.step('Migrate the collection to yml', async () => { - await page.locator('#sidebar-collection-name').filter({ hasText: 'migration-test' }).click(); - await page.getByTestId('collection-settings-tab-overview').click(); - await page.getByRole('button', { name: 'Convert to YML' }).click(); - - const modal = page.locator('.bruno-modal').filter({ hasText: 'Migrate to YML format' }); - await modal.waitFor({ state: 'visible', timeout: 5000 }); - await modal.getByRole('button', { name: 'Migrate' }).click(); - - await expect(page.getByText('Collection migrated to YML format successfully')).toBeVisible({ timeout: 30000 }); - }); - - await test.step('The migrated files exist on disk', async () => { - expect(fs.existsSync(path.join(collectionPath, 'ping.yml'))).toBe(true); - expect(fs.existsSync(path.join(collectionPath, 'ping.bru'))).toBe(false); - }); - - await test.step('The persisted snapshot no longer references any .bru request tab', async () => { - await expect - .poll(async () => (await readSnapshotTabPathnames()).some((p) => p.endsWith('.bru')), { timeout: 10000 }) - .toBe(false); - }); - - await test.step('Migration does not leave a broken "Request no longer exists" tab', async () => { - await expect(page.getByText('Request no longer exists')).not.toBeVisible(); - }); - - await test.step('The migrated request opens against the yml file and is runnable', async () => { - await page.locator('.item-name').filter({ hasText: 'ping' }).click(); - - await expect(page.getByText('Request no longer exists')).not.toBeVisible(); - - const urlContainer = page.locator('#request-url'); - await expect(urlContainer).toBeVisible(); - await expect(urlContainer.locator('.CodeMirror')).toContainText('/ping'); - - await selectEnvironment(page, 'Local'); - await sendRequestAndWaitForResponse(page, 200); - }); - - expect(pageErrors).toHaveLength(0); - }); }); From a9ae22d7f8ab6d03334d21d87ffcf98143026401 Mon Sep 17 00:00:00 2001 From: ravindra-bruno Date: Thu, 9 Jul 2026 22:37:42 +0530 Subject: [PATCH 2/3] Added the unit test and e2e for nesting folders --- .../collections/migrate-in-place.spec.js | 142 ++++++++++++++++++ .../ReduxStore/slices/tabs-migrate.spec.js | 40 +++++ .../source-collection/api/folder.bru | 3 + .../source-collection/api/v2/folder.bru | 3 + 4 files changed, 188 insertions(+) create mode 100644 packages/bruno-app/src/providers/ReduxStore/slices/collections/migrate-in-place.spec.js create mode 100644 packages/bruno-app/src/providers/ReduxStore/slices/tabs-migrate.spec.js create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/folder.bru create mode 100644 tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/folder.bru diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/migrate-in-place.spec.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/migrate-in-place.spec.js new file mode 100644 index 00000000000..8d7a83a8db0 --- /dev/null +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/migrate-in-place.spec.js @@ -0,0 +1,142 @@ +import { collectionsSlice } from './index'; + +const { migrateCollectionToYmlInPlace } = collectionsSlice.actions; +const reducer = collectionsSlice.reducer; + +const makeState = () => ({ + collections: [ + { + uid: 'col1', + format: 'bru', + brunoConfig: { version: '1', name: 'demo' }, + environments: [ + { uid: 'e1', name: 'Local', pathname: '/c/environments/Local.bru' } + ], + items: [ + { + uid: 'r1', + type: 'http-request', + name: 'ping', + pathname: '/c/ping.bru', + filename: 'ping.bru', + seq: 1, + draft: { request: { url: 'https://example.com/ping' } } + }, + { + uid: 'f1', + type: 'folder', + name: 'api', + pathname: '/c/api', + filename: 'api', + collapsed: false, + items: [ + { + uid: 'r2', + type: 'http-request', + name: 'users', + pathname: '/c/api/users.bru', + filename: 'users.bru', + seq: 1 + }, + { + uid: 'f2', + type: 'folder', + name: 'v2', + pathname: '/c/api/v2', + filename: 'v2', + collapsed: false, + items: [ + { + uid: 'r3', + type: 'http-request', + name: 'deep', + pathname: '/c/api/v2/deep.bru', + filename: 'deep.bru', + seq: 1 + } + ] + } + ] + } + ] + } + ] +}); + +describe('migrateCollectionToYmlInPlace', () => { + it('rewrites request pathnames/filenames .bru -> .yml at every nesting depth', () => { + const next = reducer( + makeState(), + migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: { opencollection: '1.0.0', version: 'v1' } }) + ); + const col = next.collections[0]; + const r1 = col.items[0]; + const f1 = col.items[1]; + const r2 = f1.items[0]; + const f2 = f1.items[1]; + const r3 = f2.items[0]; + + expect(r1.pathname).toBe('/c/ping.yml'); + expect(r1.filename).toBe('ping.yml'); + expect(r2.pathname).toBe('/c/api/users.yml'); + expect(r2.filename).toBe('users.yml'); + expect(r3.pathname).toBe('/c/api/v2/deep.yml'); + expect(r3.filename).toBe('deep.yml'); + }); + + it('flips collection.format to yml and updates brunoConfig', () => { + const next = reducer( + makeState(), + migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: { opencollection: '1.0.0', version: 'v1' } }) + ); + expect(next.collections[0].format).toBe('yml'); + expect(next.collections[0].brunoConfig).toEqual({ opencollection: '1.0.0', version: 'v1' }); + }); + + it('preserves item uids, seq, draft, and folder collapsed state', () => { + const next = reducer( + makeState(), + migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: {} }) + ); + const col = next.collections[0]; + const r1 = col.items[0]; + const f1 = col.items[1]; + const f2 = f1.items[1]; + + expect(r1.uid).toBe('r1'); + expect(r1.seq).toBe(1); + expect(r1.draft).toEqual({ request: { url: 'https://example.com/ping' } }); + expect(f1.uid).toBe('f1'); + expect(f1.collapsed).toBe(false); + expect(f2.collapsed).toBe(false); + }); + + it('leaves folder pathnames/filenames unchanged (only files migrate)', () => { + const next = reducer( + makeState(), + migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: {} }) + ); + const f1 = next.collections[0].items[1]; + const f2 = f1.items[1]; + expect(f1.pathname).toBe('/c/api'); + expect(f1.filename).toBe('api'); + expect(f2.pathname).toBe('/c/api/v2'); + expect(f2.filename).toBe('v2'); + }); + + it('rewrites environment file pathnames .bru -> .yml', () => { + const next = reducer( + makeState(), + migrateCollectionToYmlInPlace({ collectionUid: 'col1', brunoConfig: {} }) + ); + expect(next.collections[0].environments[0].pathname).toBe('/c/environments/Local.yml'); + expect(next.collections[0].environments[0].uid).toBe('e1'); + }); + + it('is a no-op for an unknown collection', () => { + const state = makeState(); + const next = reducer(state, migrateCollectionToYmlInPlace({ collectionUid: 'nope', brunoConfig: {} })); + expect(next.collections[0].items[0].pathname).toBe('/c/ping.bru'); + expect(next.collections[0].format).toBe('bru'); + }); +}); diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/tabs-migrate.spec.js b/packages/bruno-app/src/providers/ReduxStore/slices/tabs-migrate.spec.js new file mode 100644 index 00000000000..048c4cb57a4 --- /dev/null +++ b/packages/bruno-app/src/providers/ReduxStore/slices/tabs-migrate.spec.js @@ -0,0 +1,40 @@ +import { tabsSlice } from './tabs'; + +const { migrateCollectionTabsToYml } = tabsSlice.actions; +const reducer = tabsSlice.reducer; + +const makeState = () => ({ + tabs: [ + { uid: 'r1', collectionUid: 'col1', type: 'http-request', pathname: '/c/ping.bru' }, + { uid: 'r2', collectionUid: 'col1', type: 'http-request', pathname: '/c/api/users.bru' }, + { uid: 'fs1', collectionUid: 'col1', type: 'folder-settings', pathname: '/c/api' }, + { uid: 'cs1', collectionUid: 'col1', type: 'collection-settings' }, + { uid: 'other', collectionUid: 'col2', type: 'http-request', pathname: '/c2/req.bru' } + ], + activeTabUid: 'r1' +}); + +describe('migrateCollectionTabsToYml', () => { + it('rewrites .bru -> .yml only for tabs of the given collection', () => { + const next = reducer(makeState(), migrateCollectionTabsToYml({ collectionUid: 'col1' })); + const byUid = (uid) => next.tabs.find((t) => t.uid === uid); + + expect(byUid('r1').pathname).toBe('/c/ping.yml'); + expect(byUid('r2').pathname).toBe('/c/api/users.yml'); + expect(byUid('other').pathname).toBe('/c2/req.bru'); + }); + + it('leaves folder-settings (directory) paths and tabs without a pathname untouched', () => { + const next = reducer(makeState(), migrateCollectionTabsToYml({ collectionUid: 'col1' })); + const byUid = (uid) => next.tabs.find((t) => t.uid === uid); + + expect(byUid('fs1').pathname).toBe('/c/api'); + expect(byUid('cs1').pathname).toBeUndefined(); + }); + + it('does not change tab uids or the active tab', () => { + const next = reducer(makeState(), migrateCollectionTabsToYml({ collectionUid: 'col1' })); + expect(next.tabs.map((t) => t.uid)).toEqual(['r1', 'r2', 'fs1', 'cs1', 'other']); + expect(next.activeTabUid).toBe('r1'); + }); +}); diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/folder.bru b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/folder.bru new file mode 100644 index 00000000000..464a5dd2a0a --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/folder.bru @@ -0,0 +1,3 @@ +meta { + name: api +} diff --git a/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/folder.bru b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/folder.bru new file mode 100644 index 00000000000..4a655f92c01 --- /dev/null +++ b/tests/collection/migrate-to-yml-nested-multi/fixtures/collections/source-collection/api/v2/folder.bru @@ -0,0 +1,3 @@ +meta { + name: v2 +} From ec8b548ed06a2d5a3ecb6d84f6030d2031f60ee5 Mon Sep 17 00:00:00 2001 From: ravindra-bruno Date: Fri, 10 Jul 2026 13:13:56 +0530 Subject: [PATCH 3/3] fix(migration): surface watcher reattach failure and sync draft config on yml migration --- .../src/providers/ReduxStore/slices/collections/index.js | 6 ++++++ packages/bruno-electron/src/ipc/collection.js | 3 +++ 2 files changed, 9 insertions(+) diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 405fd76dfb4..8f45ef96450 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -322,11 +322,17 @@ export const collectionsSlice = createSlice({ if (brunoConfig) { collection.brunoConfig = brunoConfig; + if (collection.draft?.brunoConfig) { + collection.draft.brunoConfig = brunoConfig; + } } collection.format = 'yml'; const rewriteItemPaths = (items) => { (items || []).forEach((item) => { + if (item.isTransient) { + return; + } if (typeof item.pathname === 'string') { item.pathname = item.pathname.replace(/\.bru$/, '.yml'); } diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index bac046a5c1a..f562207f46b 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -2784,6 +2784,9 @@ const registerRendererEventHandlers = (mainWindow, watcher) => { watcher.addWatcher(mainWindow, collectionPathname, collectionUid, ymlBrunoConfig, false, undefined, { ignoreInitial: true }); } catch (watcherError) { console.error('Failed to re-attach watcher after migration:', watcherError); + mainWindow.webContents.send('main:display-error', { + message: `Collection migrated to yml, but live sync could not be re-enabled: ${watcherError.message}. Please reopen the collection.` + }); } }