remove activeEnvironmentUid and migration (#7545)

* remove activeEnvironmentUid and migration

* fix: no environment handling

* fix: standardize workspace path handling
This commit is contained in:
naman-bruno
2026-03-23 21:02:53 +05:30
committed by GitHub
parent 32b9f527ea
commit f1d7f007fe
16 changed files with 401 additions and 136 deletions

View File

@@ -0,0 +1,5 @@
{
"version": "1",
"name": "Test Collection",
"type": "collection"
}

View File

@@ -0,0 +1,4 @@
name: Alpha
variables:
- name: mode
value: alpha

View File

@@ -0,0 +1,4 @@
name: Beta
variables:
- name: mode
value: beta

View File

@@ -0,0 +1,12 @@
opencollection: 1.0.0
info:
name: "My Workspace"
type: workspace
collections:
- name: "Test Collection"
path: "collections/test-collection"
specs:
docs: ''

View File

@@ -0,0 +1,92 @@
import path from 'path';
import fs from 'fs';
import { test, expect, closeElectronApp } from '../../../playwright';
import { openCollection } from '../../utils/page';
const initUserDataPath = path.join(__dirname, 'init-user-data');
const workspaceFixturePath = path.join(__dirname, 'fixtures', 'workspace');
/**
* Replicate the uid generation from bruno-electron/src/utils/common.js
* so we can compute environment uids at test time.
*/
function simpleHash(str: string): string {
let hash = 0;
for (let i = 0; i < str.length; i++) {
const char = str.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash &= hash;
}
return new Uint32Array([hash])[0].toString(36);
}
function generateUidBasedOnHash(str: string): string {
const hash = simpleHash(str);
return `${hash}`.padEnd(21, '0');
}
/**
* Copy the workspace fixture to a temp location and return the path.
*/
async function copyWorkspaceFixture(destDir: string): Promise<string> {
const workspacePath = path.join(destDir, 'workspace');
await fs.promises.cp(workspaceFixturePath, workspacePath, { recursive: true });
return workspacePath;
}
test.describe('Global Environment Migration from workspace.yml', () => {
test('should migrate activeEnvironmentUid from workspace.yml to electron store and remove from file', async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('env-migrate-from-file');
const fixtureDir = await createTmpDir('ws-fixture');
// Copy workspace fixture to temp location
const workspacePath = await copyWorkspaceFixture(fixtureDir);
// Compute uid for the Alpha environment file at its actual path
const alphaFilePath = path.join(workspacePath, 'environments', 'Alpha.yml');
const alphaUid = generateUidBasedOnHash(alphaFilePath);
// Inject activeEnvironmentUid into workspace.yml (simulating pre-migration state)
const workspaceYmlPath = path.join(workspacePath, 'workspace.yml');
let workspaceYml = fs.readFileSync(workspaceYmlPath, 'utf8');
workspaceYml = workspaceYml.replace(
'collections:',
`activeEnvironmentUid: "${alphaUid}"\n\ncollections:`
);
fs.writeFileSync(workspaceYmlPath, workspaceYml);
// Launch with init-user-data pointing to the workspace
const app1 = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
const page1 = await app1.firstWindow();
await page1.locator('[data-app-state="loaded"]').waitFor({ timeout: 30000 });
// Open the collection so the env selector toolbar is visible
await openCollection(page1, 'Test Collection');
// Verify "Alpha" environment is selected (migrated from workspace.yml)
await expect(page1.locator('.current-environment')).toContainText('Alpha');
// Verify workspace.yml no longer contains activeEnvironmentUid
const updatedYml = fs.readFileSync(workspaceYmlPath, 'utf8');
expect(updatedYml).not.toContain('activeEnvironmentUid');
await closeElectronApp(app1);
// Restart — should still have Alpha selected (now from electron store)
const app2 = await launchElectronApp({ userDataPath });
const page2 = await app2.firstWindow();
await page2.locator('[data-app-state="loaded"]').waitFor({ timeout: 30000 });
await openCollection(page2, 'Test Collection');
await expect(page2.locator('.current-environment')).toContainText('Alpha');
await closeElectronApp(app2);
});
});

View File

@@ -0,0 +1,11 @@
{
"preferences": {
"onboarding": {
"hasLaunchedBefore": true,
"hasSeenWelcomeModal": true
},
"general": {
"defaultWorkspacePath": "{{workspacePath}}"
}
}
}

View File

@@ -0,0 +1,114 @@
import path from 'path';
import { test, expect, closeElectronApp } from '../../../playwright';
import {
createWorkspace,
switchWorkspace,
createCollection,
createEnvironment,
openCollection
} from '../../utils/page';
const initUserDataPath = path.join(__dirname, 'init-user-data');
test.describe('Global Environment Per-Workspace Persistence', () => {
test('should persist selected global environment across app restart', async ({ launchElectronApp, createTmpDir }) => {
const userDataPath = await createTmpDir('global-env-persist');
const wsLocation = await createTmpDir('ws-location');
const collectionDir = await createTmpDir('collection-persist');
// First launch
const app1 = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { wsLocation }
});
const page1 = await app1.firstWindow();
await page1.locator('[data-app-state="loaded"]').waitFor({ timeout: 30000 });
// Create a collection so the environment selector is visible
await createCollection(page1, 'Test Collection', collectionDir);
// Create a global environment (createEnvironment also selects it)
await createEnvironment(page1, 'Persist Test Env', 'global');
await expect(page1.locator('.current-environment')).toContainText('Persist Test Env');
await closeElectronApp(app1);
// Second launch - same userDataPath to preserve electron store
const app2 = await launchElectronApp({ userDataPath });
const page2 = await app2.firstWindow();
await page2.locator('[data-app-state="loaded"]').waitFor({ timeout: 30000 });
// Open the collection so the env selector is visible
await openCollection(page2, 'Test Collection');
// Verify the global environment is still selected after restart
await expect(page2.locator('.current-environment')).toContainText('Persist Test Env');
await closeElectronApp(app2);
});
test('should maintain independent global env selections per workspace', async ({ launchElectronApp, createTmpDir }) => {
const userDataPath = await createTmpDir('global-env-per-ws');
const wsLocation = await createTmpDir('ws-location-multi');
const collectionDir1 = await createTmpDir('collection-ws1');
const collectionDir2 = await createTmpDir('collection-ws2');
const app = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { wsLocation }
});
const page = await app.firstWindow();
await page.locator('[data-app-state="loaded"]').waitFor({ timeout: 30000 });
// On the default workspace, create a collection and a global env
await createCollection(page, 'WS1 Collection', collectionDir1);
await createEnvironment(page, 'Env Alpha', 'global');
await expect(page.locator('.current-environment')).toContainText('Env Alpha');
// Create a second workspace
await createWorkspace(page, 'Second Workspace');
// On the second workspace, create a collection and a different global env
await createCollection(page, 'WS2 Collection', collectionDir2);
await createEnvironment(page, 'Env Beta', 'global');
await expect(page.locator('.current-environment')).toContainText('Env Beta');
// Switch back to first workspace - "Env Alpha" should still be selected
await switchWorkspace(page, 'My Workspace');
await openCollection(page, 'WS1 Collection');
await expect(page.locator('.current-environment')).toContainText('Env Alpha');
// Switch to second workspace - "Env Beta" should still be selected
await switchWorkspace(page, 'Second Workspace');
await openCollection(page, 'WS2 Collection');
await expect(page.locator('.current-environment')).toContainText('Env Beta');
await closeElectronApp(app);
// Restart app and verify persistence across restart
const app2 = await launchElectronApp({ userDataPath });
const page2 = await app2.firstWindow();
await page2.locator('[data-app-state="loaded"]').waitFor({ timeout: 30000 });
// App opens to last active workspace - verify its env is still selected
const currentWorkspace = await page2.getByTestId('workspace-name').textContent();
if (currentWorkspace === 'Second Workspace') {
await openCollection(page2, 'WS2 Collection');
await expect(page2.locator('.current-environment')).toContainText('Env Beta');
await switchWorkspace(page2, 'My Workspace');
await openCollection(page2, 'WS1 Collection');
await expect(page2.locator('.current-environment')).toContainText('Env Alpha');
} else {
await openCollection(page2, 'WS1 Collection');
await expect(page2.locator('.current-environment')).toContainText('Env Alpha');
await switchWorkspace(page2, 'Second Workspace');
await openCollection(page2, 'WS2 Collection');
await expect(page2.locator('.current-environment')).toContainText('Env Beta');
}
await closeElectronApp(app2);
});
});

View File

@@ -0,0 +1,11 @@
{
"preferences": {
"onboarding": {
"hasLaunchedBefore": true,
"hasSeenWelcomeModal": true
},
"general": {
"defaultLocation": "{{wsLocation}}"
}
}
}