feat(variables): persist scripted variable changes by default + re-enable disabled scripting APIs (#8315)

* feat(variables): add variable persistence with scripting

feat(collections): implement script-driven update for collection variables, ensuring direct root modification and draft synchronization

feat(collections): enhance script variable management with baseline tracking and draft preservation

* feat(variables): add runtime variable updates and optimize disk writes by implementing dirty flags

fix(collections): handle errors during environment persistence in script execution

feat(collections): implement baseline clearing for script execution and optimize variable update handling

feat(tests): add default persistence tests for environment variables and update runtime variable handling

refactor(collections): streamline variable update handling and improve draft management by removing redundant comments and optimizing code clarity

test(collection-vars): add verification for draft edits and script variable visibility in collection settings UI

refactor(collection-vars): update header value selection logic for improved clarity and accuracy in draft isolation tests

* feat(global-environments): enhance global environment updates to resolve stale active UIDs and improve persistence logic

- Updated the `updateGlobalEnvironments` reducer to handle stale active UIDs by matching against environment names.
- Improved the logic for setting global environments and active UIDs to ensure consistency after disk reloads.
- Removed outdated tests related to persisted values in favor of more relevant assertions for environment variable handling.

* feat(variables): enhance typed value handling and persistence in global and collection environments

- Added tests to infer data types (number, boolean, object) when setting environment and collection variables.
- Updated the logic to preserve existing data types when variables are not modified by scripts.
- Implemented dirty flags to track changes in typed variables, ensuring accurate persistence across sessions.
- Refactored related tests to verify the correct behavior of typed variables in various scenarios.

* refactor(variables): streamline data type inference and enhance deletion methods

- Removed redundant data type inference logic from global and collection variable updates to simplify the codebase.
- Updated deletion methods in the Bru class to use Object.keys for improved resilience against user-defined properties.
- Added tests to ensure deletion methods function correctly even when properties are shadowed.
- Enhanced clarity in draft merge tests by standardizing keyboard shortcuts for selecting all text.

* fix(tests): correct variable naming and improve environment panel interactions

- Updated test cases to reflect the correct variable name 'wasSaved' instead of 'was-saved'.
- Modified environment panel interaction to remove forced click, enhancing test reliability.
- Added a utility function to close the environment panel in safe mode tests for better readability and maintainability.

* feat(runtime): enhance variable management and cleanup logic

- Introduced a new method to clear script-driven variable baselines for collections, ensuring no stale data leaks into new requests.
- Updated the handling of runtime variables in the Bru class to track changes with a new dirty flag, improving state management.
- Refactored the application of script environment variables to prevent direct mutations, ensuring immutability and cleaner state updates.
- Enhanced the response handling in the script runtime to conditionally include runtime variables based on their dirty state.

* feat(variables): improve request handling and state management for collections and environments

- Enhanced event listeners to clear global environment baselines on both 'testrun-started' and 'request-queued' events, preventing stale data issues.
- Updated global environment and collection variable update events to ignore stale updates from superseded requests, ensuring accurate state management.
- Refactored the Bru class to optimize variable management, including checks for existing keys before updates and deletions, improving performance and reliability.
- Introduced request UID tracking to maintain consistency across variable updates during concurrent requests.

* refactor(collections): update action to clear script variable baselines

- Replaced the dispatch of `_clearScriptGlobalEnvBaseline` with `clearScriptVariableBaselines` to improve clarity and maintainability in the Redux action handling for collections.

* feat(environments): introduce getScriptModifiedKeys utility for improved variable management

- Added a new utility function, `getScriptModifiedKeys`, to identify keys modified by scripts relative to a baseline, enhancing the handling of data types during variable updates.
- Updated the application of script environment variables to prevent overwriting user-defined draft changes during no-op writes.
- Refactored related logic in collections and global environments to utilize the new utility, ensuring accurate state management and improved clarity in the Redux slices.

* refactor(global-environments): simplify active UID resolution logic in updateGlobalEnvironments reducer

- Streamlined the logic for resolving the active global environment UID by consolidating conditions into a more concise format.
- Removed outdated comments to enhance code clarity and maintainability.
- Updated tests to ensure accurate resolution of active UIDs based on incoming environment data.

* refactor(tests): remove outdated comments and streamline environment variable row expectations

- Eliminated comments related to state sync and inference issues to enhance code clarity.
- Adjusted expectations for environment variable row rendering in tests, focusing on relevant assertions.

* feat(tests): add comprehensive tests for secret variable persistence in environments

- Introduced new test cases to validate the preservation of secret variables when updated via scripts in both collection and global environments.
- Implemented tests to ensure that secret values are encrypted before storage and can be correctly decrypted for subsequent requests.
- Added fixtures and environment configurations for testing secret variable behavior in both bru and yml formats.
- Enhanced utility functions for managing environment configurations and interactions within the test suite.

* feat(tests): enhance environment variable tests and add global variable persistence

- Updated MultiLineEditor and SingleLineEditor components to include data-testid for secret reveal toggle buttons, improving testability.
- Introduced new tests for global environment variable persistence, ensuring non-secret variables survive app restarts and are correctly interpolated.
- Added fixtures for workspace and collections to support the new global variable tests, enhancing the overall test coverage for environment management.
- Refactored utility functions to streamline interactions with environment variables in tests.

* refactor(collections): optimize environment and collection saving logic

- Simplified the persistence logic for active environments by directly constructing the environment copy, reducing unnecessary cloning.
- Updated the collection saving process to utilize the fresh collection state, ensuring accurate data is saved without drafts.
- Enhanced error handling during the save operations to improve reliability and maintainability.

* feat(tests): implement collection variable persistence tests

- Added multiple test cases to validate the persistence of collection variables across app restarts, including typed values and multiple variable settings.
- Created new fixtures for collection variables to support the tests, ensuring accurate simulation of variable management scenarios.
- Enhanced the existing collection management logic to ensure that variables are correctly set and deleted as per the test requirements.

* feat(tests): add tests for typed global environment variable persistence

- Introduced a new test suite to validate the persistence of typed global environment variables across app restarts, ensuring correct data types are maintained.
- Created a fixture for the test collection to simulate setting global variables with various data types, including number, boolean, object, and string.
- Enhanced the test logic to verify that the environment file reflects the correct state before and after application restarts.

* fix(tests): update request tab close interaction in variable persistence tests

* fix(tests): improve hover interaction for collection actions in runner tests

- Updated the hover logic for revealing collection actions to handle sidebar re-renders more reliably.
- Replaced one-shot hover with a polling mechanism to ensure visibility of actions, enhancing test stability.

* refactor(environments): streamline environment variable handling and remove ephemeral metadata logic

- Simplified the comparison logic for environment variables by removing unnecessary ephemeral metadata handling.
- Updated the saving process to directly use the environment variables without stripping metadata, enhancing clarity and maintainability.
- Removed outdated comments and unused utility functions related to ephemeral variables, improving code cleanliness.

* fix(ipc): update persistActiveEnvironment to handle requestUid for stale updates

- Modified the persistActiveEnvironment function to accept a requestUid parameter, allowing for better management of stale updates.
- Enhanced the logic to prevent disk writes for superseded requests, improving data integrity during environment persistence.

* refactor(bru): remove unused envName variable in deleteAllEnvVars method

- Eliminated the envName variable from the deleteAllEnvVars method, simplifying the logic for deleting environment variables.
- Cleaned up the method by removing unnecessary checks related to the envName, enhancing code clarity and maintainability.

* fix(bru): prevent deletion of internal __name__ variable in deleteEnvVar method

- Added a check in the deleteEnvVar method to silently ignore attempts to delete the internal __name__ variable, preserving its integrity.
- Updated tests to verify that the __name__ variable remains unchanged when deleteEnvVar is called with this key.
- Enhanced runtime tests to ensure compatibility with QuickJS by confirming that environment variables set with persist options are handled correctly.

* feat(tests): add legacy support test for environment variable persistence

- Introduced a new test suite to validate that the legacy argument for setting environment variables with persistence is still functional in version 4.
- Created a fixture to simulate the legacy syntax, ensuring that the variable is correctly persisted on disk without errors.
- Enhanced integration testing to confirm that the legacy behavior aligns with the current implementation, maintaining backward compatibility.

* test(tests): enhance legacy environment variable persistence tests for safe and developer modes

- Updated the test suite for `bru.setEnvVar` to verify that the legacy persist flag is correctly handled in both safe and developer modes.
- Introduced a helper function to streamline the verification process and ensure consistent behavior across different execution contexts.
- Adjusted the test logic to reset the environment state between mode switches, maintaining test integrity.
- Improved hover interaction in multiple persistent variable tests to ensure reliable visibility of actions during execution.

* fix(EnvironmentVariablesTable): correct change detection logic for environment variables

- Updated the logic for determining changes in environment variables to compare active current and saved values instead of previously used variablesToSave and savedValues.
- This change ensures accurate detection of modifications before saving, improving user feedback when no changes are present.

* test(tests): enhance secret variable persistence tests for environment configurations

- Updated the test suites for `bru.setEnvVar` and `bru.setGlobalEnvVar` to include interactions with the secrets tab, ensuring visibility of secret variables during various states of the environment.
- Added checks to confirm that the eye toggle functionality correctly reveals the values of secret variables after setting and overwriting them.
- Improved test coverage for secret variable persistence, validating that the expected values are displayed in both collection and global environment contexts.
This commit is contained in:
sanish chirayath
2026-06-26 23:01:37 +05:30
committed by GitHub
parent 30b4512983
commit 87f74262bb
171 changed files with 7138 additions and 1018 deletions

View File

@@ -0,0 +1,66 @@
import fs from 'fs';
import path from 'path';
import { test, expect, closeElectronApp } from '../../../playwright';
import { sendRequest, waitForReadyPage } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
const COLLECTION_FILE_ORIGINAL = `meta {
name: collection
}
vars:pre-request {
host: https://testbench-sanity.usebruno.com
existingCollVar: original-coll-value
}
`;
const restoreCollectionFixture = (collectionFixturePath: string) => {
const collectionBru = path.join(collectionFixturePath, 'collection.bru');
fs.writeFileSync(collectionBru, COLLECTION_FILE_ORIGINAL, 'utf8');
};
test.describe.serial('bru.deleteCollectionVar(name) - removes var from collection.bru', () => {
test.afterEach(async ({ collectionFixturePath }) => {
if (collectionFixturePath) {
restoreCollectionFixture(collectionFixturePath);
}
});
test('collection var deletion via script persists across restart', async ({
pageWithUserData: page,
collectionFixturePath,
restartApp
}) => {
const collectionBruPath = path.join(collectionFixturePath!, 'collection.bru');
const locators = buildCommonLocators(page);
// Sanity: fixture starts with existingCollVar present.
expect(fs.readFileSync(collectionBruPath, 'utf8')).toMatch(/existingCollVar:\s*original-coll-value/);
await locators.sidebar.collection('collection').click();
await page.getByText('api-deleteCollectionVar', { exact: true }).click();
await sendRequest(page, 200);
// Pre-restart: collection.bru no longer contains existingCollVar.
await expect.poll(() => fs.readFileSync(collectionBruPath, 'utf8'), { timeout: 5000 })
.not.toMatch(/existingCollVar/);
// Pre-restart UI: the vars tab no longer lists existingCollVar.
await locators.sidebar.collection('collection').click();
await locators.paneTabs.collectionSettingsTab('vars').click();
await expect(locators.environment.variableRowByName('existingCollVar')).toHaveCount(0);
// Restart: the deletion survives.
const newApp = await restartApp();
const newPage = await waitForReadyPage(newApp);
const newLocators = buildCommonLocators(newPage);
await newLocators.sidebar.collection('collection').click();
await newLocators.paneTabs.collectionSettingsTab('vars').click();
await expect(newLocators.environment.variableRowByName('existingCollVar')).toHaveCount(0);
expect(fs.readFileSync(collectionBruPath, 'utf8')).not.toMatch(/existingCollVar/);
await closeElectronApp(newApp);
});
});

View File

@@ -0,0 +1,49 @@
import fs from 'fs';
import path from 'path';
import { test, expect, closeElectronApp } from '../../../playwright';
import { sendRequest, waitForReadyPage } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
test.describe.serial('bru.setCollectionVar(name, value) - default persistence', () => {
test('collection var set via script persists across restart', async ({
pageWithUserData: page,
restartApp,
collectionFixturePath
}) => {
const collectionBruPath = path.join(collectionFixturePath!, 'collection.bru');
const locators = buildCommonLocators(page);
const openCollectionVarsTab = async (p = page) => {
const l = buildCommonLocators(p);
await l.sidebar.collection('collection').click();
await l.paneTabs.collectionSettingsTab('vars').click();
};
// Open collection, run the request — script writes `token: secret` to collection vars.
await locators.sidebar.collection('collection').click();
await page.getByText('api-setCollectionVar-default-persistence', { exact: true }).click();
await sendRequest(page, 200);
// Pre-restart UI: vars tab shows the script-set var.
await openCollectionVarsTab();
await expect(locators.environment.variableRowByName('token')).toBeVisible();
await expect(locators.environment.variableValue('token')).toContainText('secret');
// Pre-restart disk: collection.bru contains `token: secret`.
await expect.poll(() => fs.readFileSync(collectionBruPath, 'utf8'), { timeout: 5000 })
.toMatch(/token:\s*secret/);
// Restart and re-verify both UI and disk.
const newApp = await restartApp();
const newPage = await waitForReadyPage(newApp);
const newLocators = buildCommonLocators(newPage);
await openCollectionVarsTab(newPage);
await expect(newLocators.environment.variableRowByName('token')).toBeVisible();
await expect(newLocators.environment.variableValue('token')).toContainText('secret');
expect(fs.readFileSync(collectionBruPath, 'utf8')).toMatch(/token:\s*secret/);
await closeElectronApp(newApp);
});
});

View File

@@ -0,0 +1,78 @@
import fs from 'fs';
import path from 'path';
import { test, expect, closeElectronApp } from '../../../playwright';
import { sendRequest, waitForReadyPage } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
const COLLECTION_FILE_ORIGINAL = `meta {
name: collection
}
vars:pre-request {
host: https://testbench-sanity.usebruno.com
existingCollVar: original-coll-value
}
`;
const restoreCollectionFixture = (collectionFixturePath: string) => {
const collectionBru = path.join(collectionFixturePath, 'collection.bru');
fs.writeFileSync(collectionBru, COLLECTION_FILE_ORIGINAL, 'utf8');
};
test.describe.serial('bru.setCollectionVar(name, value) — typed value persistence', () => {
test.afterEach(async ({ collectionFixturePath }) => {
if (collectionFixturePath) {
restoreCollectionFixture(collectionFixturePath);
}
});
test('persists number/boolean/object/string values with the correct dataType annotation', async ({
pageWithUserData: page,
collectionFixturePath,
restartApp
}) => {
const collectionBruPath = path.join(collectionFixturePath!, 'collection.bru');
const locators = buildCommonLocators(page);
await test.step('Run the typed-persist request', async () => {
await locators.sidebar.collection('collection').click();
await page.getByText('api-setCollectionVar-typed', { exact: true }).click();
await sendRequest(page, 200);
});
await test.step('collection.bru on disk carries the right dataType annotations', async () => {
await expect.poll(() => fs.readFileSync(collectionBruPath, 'utf8'), { timeout: 5000 })
.toMatch(/@number\s+coll_num:\s*42/);
const content = fs.readFileSync(collectionBruPath, 'utf8');
expect(content).toMatch(/@boolean\s+coll_bool:\s*true/);
// @object values are pretty-printed in a '''…''' block.
expect(content).toMatch(/@object\s+coll_obj:\s*'''[\s\S]*"k":\s*1[\s\S]*'''/);
// 'string' is the implicit default — not materialized.
expect(content).not.toMatch(/@string\s+coll_str:/);
expect(content).toMatch(/coll_str:\s*hello/);
});
await test.step('Restart and verify the collection vars tab reflects the persisted datatypes', async () => {
const newApp = await restartApp();
const newPage = await waitForReadyPage(newApp);
const newLocators = buildCommonLocators(newPage);
await newLocators.sidebar.collection('collection').click();
await newLocators.paneTabs.collectionSettingsTab('vars').click();
const numRow = newLocators.environment.variableRowByName('coll_num');
const boolRow = newLocators.environment.variableRowByName('coll_bool');
const objRow = newLocators.environment.variableRowByName('coll_obj');
const strRow = newLocators.environment.variableRowByName('coll_str');
await expect(numRow).toBeVisible();
await expect(newLocators.dataTypeSelector.typeLabel(numRow)).toHaveText('number', { timeout: 5000 });
await expect(newLocators.dataTypeSelector.typeLabel(boolRow)).toHaveText('boolean');
await expect(newLocators.dataTypeSelector.typeLabel(objRow)).toHaveText('object');
await expect(newLocators.dataTypeSelector.typeLabel(strRow)).toHaveText('string');
await closeElectronApp(newApp);
});
});
});

View File

@@ -0,0 +1,16 @@
meta {
name: api-deleteCollectionVar
type: http
seq: 3
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:post-response {
// Removes a pre-existing collection var declared in collection.bru.
bru.deleteCollectionVar("existingCollVar");
}

View File

@@ -0,0 +1,21 @@
meta {
name: api-multi-persist-coll-vars
type: http
seq: 4
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:pre-request {
// The folder runner rebuilds request.collectionVariables on every iteration
// from collection.root, so collection-var writes don't accumulate across
// folder requests the way env-var writes do (env-vars share one object —
// see packages/bruno-electron/src/ipc/network/index.js:1397).
// This spec covers the supported shape: one script persisting multiple keys.
bru.setCollectionVar("multiple-persist-coll-vars-key1", "value1");
bru.setCollectionVar("multiple-persist-coll-vars-key2", "value2");
}

View File

@@ -0,0 +1,15 @@
meta {
name: api-setCollectionVar-default-persistence
type: http
seq: 1
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:post-response {
bru.setCollectionVar("token", "secret");
}

View File

@@ -0,0 +1,21 @@
meta {
name: api-setCollectionVar-typed
type: http
seq: 2
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:pre-request {
// Typed values persist with their dataType annotation in collection.bru's
// vars:pre-request block.
bru.setCollectionVar("coll_num", 42);
bru.setCollectionVar("coll_bool", true);
bru.setCollectionVar("coll_obj", { k: 1 });
// Plain string — no dataType materialized.
bru.setCollectionVar("coll_str", "hello");
}

View File

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

View File

@@ -0,0 +1,8 @@
meta {
name: collection
}
vars:pre-request {
host: https://testbench-sanity.usebruno.com
existingCollVar: original-coll-value
}

View File

@@ -0,0 +1,10 @@
{
"collections": [
{
"path": "{{collectionPath}}",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,5 @@
{
"lastOpenedCollections": [
"{{collectionPath}}"
]
}

View File

@@ -0,0 +1,51 @@
import fs from 'fs';
import path from 'path';
import { test, expect } from '../../../playwright';
import { sendRequest } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
const COLLECTION_FILE_ORIGINAL = `meta {
name: collection
}
vars:pre-request {
host: https://testbench-sanity.usebruno.com
existingCollVar: original-coll-value
}
`;
test.describe.serial('bru.setCollectionVar multiple persistent variables', () => {
test.afterEach(async ({ collectionFixturePath }) => {
if (collectionFixturePath) {
const collectionBru = path.join(collectionFixturePath, 'collection.bru');
fs.writeFileSync(collectionBru, COLLECTION_FILE_ORIGINAL, 'utf8');
}
});
test('a single script can persist multiple collection vars', async ({
pageWithUserData: page,
collectionFixturePath
}) => {
const locators = buildCommonLocators(page);
await locators.sidebar.collection('collection').click();
await page.getByText('api-multi-persist-coll-vars', { exact: true }).click();
await sendRequest(page, 200);
await test.step('Both vars appear in the collection vars tab', async () => {
await locators.sidebar.collection('collection').click();
await locators.paneTabs.collectionSettingsTab('vars').click();
await expect(locators.environment.variableRowByName('multiple-persist-coll-vars-key1')).toBeVisible();
await expect(locators.environment.variableValue('multiple-persist-coll-vars-key1')).toContainText('value1');
await expect(locators.environment.variableRowByName('multiple-persist-coll-vars-key2')).toBeVisible();
await expect(locators.environment.variableValue('multiple-persist-coll-vars-key2')).toContainText('value2');
});
await test.step('Both vars are persisted to collection.bru', async () => {
const collectionBruPath = path.join(collectionFixturePath!, 'collection.bru');
await expect.poll(() => fs.readFileSync(collectionBruPath, 'utf8'), { timeout: 10000 })
.toMatch(/multiple-persist-coll-vars-key1:\s*value1[\s\S]*multiple-persist-coll-vars-key2:\s*value2/);
});
});
});

View File

@@ -0,0 +1,351 @@
import path from 'path';
import fs from 'fs';
import { test, expect, closeElectronApp } from '../../../playwright';
import {
openCollection,
openRequest,
sendRequest,
selectEnvironment,
expectResponseContains,
waitForReadyPage,
openEnvironmentConfigTab,
closeEnvironmentPanel
} from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
import { waitForCollectionMount } from '../../utils/page/mounting';
const NEW_VALUE = 'NEW_VALUE_collection_env_e2e_42';
const INITIAL_VALUE = 'INITIAL_VALUE_collection_env_e2e_001';
const initUserDataPath = path.join(__dirname, 'init-user-data');
const fixturesPath = path.join(__dirname, 'fixtures', 'collections');
type Format = {
label: 'bru' | 'yml';
subdir: string;
collectionName: string;
envFile: string;
expectInitial: (content: string) => void;
expectAfterScript: (content: string) => void;
envSettlePattern: RegExp;
};
const FORMATS: Format[] = [
{
label: 'bru',
subdir: 'bru',
collectionName: 'api-setEnvVar-secret-bru',
envFile: 'Local.bru',
envSettlePattern: /vars:secret\s*\[[^\]]*apiToken/,
expectInitial: (content) => {
expect(content).toMatch(/vars:secret\s*\[[^\]]*apiToken/);
expect(content).not.toContain(NEW_VALUE);
},
expectAfterScript: (content) => {
expect(content).toMatch(/vars:secret\s*\[[^\]]*apiToken/);
expect(content).not.toContain(NEW_VALUE);
expect(content).not.toMatch(/^\s*apiToken\s*:/m);
}
},
{
label: 'yml',
subdir: 'yml',
collectionName: 'api-setEnvVar-secret-yml',
envFile: 'Local.yml',
envSettlePattern: /name:\s*apiToken/,
expectInitial: (content) => {
expect(content).toMatch(/name:\s*apiToken/);
expect(content).toMatch(/secret:\s*true/);
expect(content).not.toContain(NEW_VALUE);
},
expectAfterScript: (content) => {
expect(content).toMatch(/name:\s*apiToken/);
expect(content).toMatch(/secret:\s*true/);
expect(content).not.toContain(NEW_VALUE);
}
}
];
const findApiTokenSecret = (secretsJson: any, collectionDir: string) => {
const collection = secretsJson.collections?.find((c: any) =>
path.normalize(c.path) === path.normalize(collectionDir)
);
const env = collection?.environments?.find((e: any) => e.name === 'Local');
return env?.secrets?.find((s: any) => s.name === 'apiToken');
};
test.describe('bru.setEnvVar(name, value) - secret variable persistence', () => {
for (const fmt of FORMATS) {
test(`(${fmt.label}) script-set secret encrypts to the secrets store and reaches a subsequent request via interpolation`, async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const collectionPath = await createTmpDir('collections');
await fs.promises.cp(fixturesPath, collectionPath, { recursive: true });
const envFilePath = path.join(collectionPath, fmt.subdir, 'environments', fmt.envFile);
const secretsPath = path.join(userDataPath, 'secrets.json');
const collectionDir = path.join(collectionPath, fmt.subdir);
await test.step('Fixture sanity: env file declares apiToken as secret with no value; no secrets store yet', () => {
fmt.expectInitial(fs.readFileSync(envFilePath, 'utf8'));
expect(fs.existsSync(secretsPath)).toBe(false);
});
const app = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { collectionPath }
});
const page = await waitForReadyPage(app);
const locators = buildCommonLocators(page);
try {
await test.step('Open the setter request and select the "Local" environment', async () => {
await waitForCollectionMount(page, fmt.collectionName);
await openCollection(page, fmt.collectionName);
await openRequest(page, fmt.collectionName, 'set-secret', { persist: true });
await selectEnvironment(page, 'Local', 'collection');
});
await test.step('Env panel BEFORE script: eye toggle reveals an empty initial value', async () => {
await openEnvironmentConfigTab(page, 'collection');
const envTab = locators.environment.collectionEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror').first())
.toHaveClass(/CodeMirror-empty/);
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.not.toContainText(NEW_VALUE);
await closeEnvironmentPanel(page, 'collection');
});
await test.step('Run the request whose post-response script sets apiToken', async () => {
await sendRequest(page, 200);
});
await test.step('On-disk env file: secret value is never written; secret marker preserved', async () => {
await expect.poll(() => fs.readFileSync(envFilePath, 'utf8'), { timeout: 5000 })
.toMatch(fmt.envSettlePattern);
fmt.expectAfterScript(fs.readFileSync(envFilePath, 'utf8'));
});
await test.step('Secrets store: encrypted apiToken entry is persisted under this collection', async () => {
await expect.poll(() => fs.existsSync(secretsPath), { timeout: 5000 }).toBe(true);
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), collectionDir);
return secret?.value ?? '';
}, { timeout: 5000 }).not.toBe('');
const secretsJson = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
const secret = findApiTokenSecret(secretsJson, collectionDir);
expect(secret).toBeDefined();
expect(typeof secret!.value).toBe('string');
expect(secret!.value.length).toBeGreaterThan(0);
expect(secret!.value).not.toContain(NEW_VALUE);
expect(JSON.stringify(secretsJson)).not.toContain(NEW_VALUE);
expect(fs.readFileSync(envFilePath, 'utf8')).not.toContain(secret!.value);
});
await test.step('Env panel AFTER script: eye toggle reveals NEW_VALUE, no draft icon', async () => {
await openEnvironmentConfigTab(page, 'collection');
const envTab = locators.environment.collectionEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await expect(envTab.locator('.close-gradient')).not.toHaveClass(/has-changes/);
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.toContainText(NEW_VALUE);
await closeEnvironmentPanel(page, 'collection');
});
await test.step('Interpolation: a subsequent request resolves {{apiToken}} to the new value', async () => {
await openRequest(page, fmt.collectionName, 'read-secret', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(app);
}
});
test(`(${fmt.label}) script overwrites a previously-set secret, encrypted store entry changes`, async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const collectionPath = await createTmpDir('collections');
await fs.promises.cp(fixturesPath, collectionPath, { recursive: true });
const envFilePath = path.join(collectionPath, fmt.subdir, 'environments', fmt.envFile);
const secretsPath = path.join(userDataPath, 'secrets.json');
const collectionDir = path.join(collectionPath, fmt.subdir);
const app = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { collectionPath }
});
const page = await waitForReadyPage(app);
const locators = buildCommonLocators(page);
try {
await test.step('Open the seed-secret request and select the "Local" environment', async () => {
await waitForCollectionMount(page, fmt.collectionName);
await openCollection(page, fmt.collectionName);
await openRequest(page, fmt.collectionName, 'seed-secret', { persist: true });
await selectEnvironment(page, 'Local', 'collection');
});
await test.step('Seed the secret: post-response script sets apiToken to INITIAL_VALUE', async () => {
await sendRequest(page, 200);
});
let initialEncryptedValue = '';
await test.step('After seed: env panel shows INITIAL_VALUE; encrypted entry persisted', async () => {
await openEnvironmentConfigTab(page, 'collection');
const envTab = locators.environment.collectionEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.toContainText(INITIAL_VALUE);
await closeEnvironmentPanel(page, 'collection');
await expect.poll(() => fs.existsSync(secretsPath), { timeout: 5000 }).toBe(true);
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), collectionDir);
return secret?.value ?? '';
}, { timeout: 5000 }).not.toBe('');
const secretsJson = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
const secret = findApiTokenSecret(secretsJson, collectionDir);
expect(secret!.value).not.toContain(INITIAL_VALUE);
expect(JSON.stringify(secretsJson)).not.toContain(INITIAL_VALUE);
expect(fs.readFileSync(envFilePath, 'utf8')).not.toContain(secret!.value);
initialEncryptedValue = secret!.value;
});
await test.step('Overwrite: open set-secret and run it (script writes NEW_VALUE over INITIAL_VALUE)', async () => {
await openRequest(page, fmt.collectionName, 'set-secret', { persist: true });
await sendRequest(page, 200);
});
await test.step('After overwrite: env panel shows NEW_VALUE (not INITIAL_VALUE); encrypted entry changed; .bru/.yml still clean', async () => {
await openEnvironmentConfigTab(page, 'collection');
const envTab = locators.environment.collectionEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await expect(envTab.locator('.close-gradient')).not.toHaveClass(/has-changes/);
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.toContainText(NEW_VALUE);
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.not.toContainText(INITIAL_VALUE);
await closeEnvironmentPanel(page, 'collection');
// Encrypted blob on disk changed — proves the secrets store was rewritten, not stale-cached.
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), collectionDir);
return secret?.value;
}, { timeout: 5000 }).not.toBe(initialEncryptedValue);
const secretsJson = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
const secret = findApiTokenSecret(secretsJson, collectionDir);
expect(secret!.value).not.toContain(NEW_VALUE);
expect(secret!.value.length).toBeGreaterThan(0);
expect(JSON.stringify(secretsJson)).not.toContain(NEW_VALUE);
expect(JSON.stringify(secretsJson)).not.toContain(INITIAL_VALUE);
const content = fs.readFileSync(envFilePath, 'utf8');
expect(content).not.toContain(NEW_VALUE);
expect(content).not.toContain(INITIAL_VALUE);
expect(content).not.toContain(secret!.value);
fmt.expectAfterScript(content);
});
await test.step('Interpolation: a subsequent request resolves {{apiToken}} to NEW_VALUE', async () => {
await openRequest(page, fmt.collectionName, 'read-secret', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(app);
}
});
}
test('persisted secret survives app restart and decrypts on the next launch', async ({
launchElectronApp,
createTmpDir
}) => {
// Encryption pathway is format-agnostic; one format is enough to prove the round-trip.
const fmt = FORMATS[0];
const userDataPath = await createTmpDir('userdata');
const collectionPath = await createTmpDir('collections');
await fs.promises.cp(fixturesPath, collectionPath, { recursive: true });
const secretsPath = path.join(userDataPath, 'secrets.json');
const collectionDir = path.join(collectionPath, fmt.subdir);
let blobBeforeRestart = '';
const firstApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { collectionPath }
});
try {
const page = await waitForReadyPage(firstApp);
await test.step('First launch: run set-secret and confirm the encrypted blob is on disk', async () => {
await waitForCollectionMount(page, fmt.collectionName);
await openCollection(page, fmt.collectionName);
await openRequest(page, fmt.collectionName, 'set-secret', { persist: true });
await selectEnvironment(page, 'Local', 'collection');
await sendRequest(page, 200);
await expect.poll(() => fs.existsSync(secretsPath), { timeout: 5000 }).toBe(true);
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), collectionDir);
return secret?.value ?? '';
}, { timeout: 5000 }).not.toBe('');
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), collectionDir);
blobBeforeRestart = secret!.value;
});
} finally {
await closeElectronApp(firstApp);
}
const secondApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { collectionPath }
});
try {
const page = await waitForReadyPage(secondApp);
await test.step('Second launch: encrypted blob unchanged on disk', async () => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), collectionDir);
expect(secret!.value).toBe(blobBeforeRestart);
});
await test.step('Decryption round-trip: read-secret interpolates {{apiToken}} to the original plaintext', async () => {
await waitForCollectionMount(page, fmt.collectionName);
await openCollection(page, fmt.collectionName);
await selectEnvironment(page, 'Local', 'collection');
await openRequest(page, fmt.collectionName, 'read-secret', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(secondApp);
}
});
});

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "api-setEnvVar-secret-bru",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,6 @@
vars {
baseUrl: http://localhost:8081
}
vars:secret [
apiToken
]

View File

@@ -0,0 +1,11 @@
meta {
name: read-secret
type: http
seq: 2
}
get {
url: {{baseUrl}}/query?token={{apiToken}}
body: none
auth: none
}

View File

@@ -0,0 +1,15 @@
meta {
name: seed-secret
type: http
seq: 3
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
bru.setEnvVar("apiToken", "INITIAL_VALUE_collection_env_e2e_001");
}

View File

@@ -0,0 +1,15 @@
meta {
name: set-secret
type: http
seq: 1
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
bru.setEnvVar("apiToken", "NEW_VALUE_collection_env_e2e_42");
}

View File

@@ -0,0 +1,6 @@
name: Local
variables:
- name: baseUrl
value: http://localhost:8081
- secret: true
name: apiToken

View File

@@ -0,0 +1,3 @@
opencollection: "1.0.0"
info:
name: api-setEnvVar-secret-yml

View File

@@ -0,0 +1,8 @@
info:
name: read-secret
type: http
seq: 2
http:
method: GET
url: '{{baseUrl}}/query?token={{apiToken}}'

View File

@@ -0,0 +1,14 @@
info:
name: seed-secret
type: http
seq: 3
http:
method: GET
url: '{{baseUrl}}/ping'
runtime:
scripts:
- type: after-response
code: |-
bru.setEnvVar("apiToken", "INITIAL_VALUE_collection_env_e2e_001");

View File

@@ -0,0 +1,14 @@
info:
name: set-secret
type: http
seq: 1
http:
method: GET
url: '{{baseUrl}}/ping'
runtime:
scripts:
- type: after-response
code: |-
bru.setEnvVar("apiToken", "NEW_VALUE_collection_env_e2e_42");

View File

@@ -0,0 +1,16 @@
{
"collections": [
{
"path": "{{collectionPath}}/bru",
"securityConfig": {
"jsSandboxMode": "safe"
}
},
{
"path": "{{collectionPath}}/yml",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,6 @@
{
"lastOpenedCollections": [
"{{collectionPath}}/bru",
"{{collectionPath}}/yml"
]
}

View File

@@ -1,63 +1,48 @@
import { test, expect, closeElectronApp } from '../../../playwright';
import { sendRequest, waitForReadyPage } from '../../utils/page';
import fs from 'fs';
import path from 'path';
import { test, expect } from '../../../playwright';
import { sendRequest, setSandboxMode } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
test.describe.serial('bru.setEnvVar(name, value, { persist: true })', () => {
test('set env var with persist using script', async ({ pageWithUserData: page, restartApp }) => {
// Select the collection and request
await page.locator('#sidebar-collection-name').click();
await page.getByText('api-setEnvVar-with-persist', { exact: true }).click();
test.describe('bru.setEnvVar(name, value, { persist: true }) - legacy arg', () => {
test('legacy persist flag is silently ignored and the var persists in both safe and developer mode', async ({
pageWithUserData: page,
collectionFixturePath
}) => {
const stageBruPath = path.join(collectionFixturePath!, 'environments', 'Stage.bru');
const stageOriginal = fs.readFileSync(stageBruPath, 'utf8');
const locators = buildCommonLocators(page);
// open environment dropdown
await page.getByTestId('environment-selector-trigger').click();
const runAndVerify = async () => {
await locators.sidebar.collection('collection').click();
// Substring `hasText` would also match `api-setEnvVar-with-persist-typed`.
await locators.sidebar.collectionsContainer()
.getByText('api-setEnvVar-with-persist', { exact: true })
.click();
// select stage environment
await expect(page.locator('.environment-list .dropdown-item', { hasText: 'Stage' })).toBeVisible();
await page.locator('.environment-list .dropdown-item', { hasText: 'Stage' }).click();
await expect(page.locator('.current-environment', { hasText: 'Stage' })).toBeVisible();
await locators.environment.selector().click();
await expect(locators.environment.listOption('Stage')).toBeVisible();
await locators.environment.listOption('Stage').click();
await expect(locators.environment.currentEnvironment()).toContainText('Stage');
// Send the request
await sendRequest(page, 200);
await sendRequest(page, 200);
// confirm that the environment variable is set
await page.getByTestId('environment-selector-trigger').hover();
await page.getByTestId('environment-selector-trigger').click();
// open environment configuration
await expect
.poll(() => fs.readFileSync(stageBruPath, 'utf8'), { timeout: 5000 })
.toMatch(/legacy_persist_var:\s*from-legacy-flag/);
};
await page.locator('#configure-env').waitFor({ state: 'visible' });
await page.locator('#configure-env').dispatchEvent('click');
await test.step('safe mode (quickjs)', async () => {
await setSandboxMode(page, 'collection', 'safe');
await runAndVerify();
});
const envTab = page.locator('.request-tab').filter({ has: page.locator('.tab-label', { hasText: 'Environments' }) });
await expect(envTab).toBeVisible();
// Reset so the developer-mode pass can't trivially match the safe-mode write.
fs.writeFileSync(stageBruPath, stageOriginal, 'utf8');
await expect(page.getByRole('row', { name: 'token' }).getByRole('cell').nth(1)).toBeVisible();
await expect(page.getByRole('row', { name: 'secret' }).getByRole('cell').nth(2)).toBeVisible();
await envTab.hover();
await envTab.getByTestId('request-tab-close-icon').click({ force: true });
// we restart the app to confirm that the environment variable is persisted
const newApp = await restartApp();
const newPage = await waitForReadyPage(newApp);
// select the collection and request
await newPage.locator('#sidebar-collection-name').click();
await newPage.getByText('api-setEnvVar-with-persist', { exact: true }).click();
// open environment dropdown
await newPage.getByTestId('environment-selector-trigger').click();
await newPage.locator('#configure-env').waitFor({ state: 'visible' });
await newPage.locator('#configure-env').dispatchEvent('click');
const newEnvTab = newPage.locator('.request-tab').filter({ hasText: 'Environments' });
await expect(newEnvTab).toBeVisible();
await newPage.locator('.environment-item', { hasText: 'Stage' }).click();
await expect(newPage.getByRole('row', { name: 'token' }).getByRole('cell').nth(1)).toBeVisible();
await expect(newPage.getByRole('row', { name: 'secret' }).getByRole('cell').nth(2)).toBeVisible();
await newEnvTab.hover();
await newEnvTab.getByTestId('request-tab-close-icon').click({ force: true });
await closeElectronApp(newApp);
await test.step('developer mode (nodevm)', async () => {
await setSandboxMode(page, 'collection', 'developer');
await runAndVerify();
});
});
});

View File

@@ -1,60 +0,0 @@
import { test, expect, closeElectronApp } from '../../../playwright';
import { sendRequest, waitForReadyPage } from '../../utils/page';
test.describe.serial('bru.setEnvVar(name, value)', () => {
test('set env var using script', async ({ pageWithUserData: page, restartApp }) => {
// Select the collection and request
await page.locator('#sidebar-collection-name').click();
await page.getByText('api-setEnvVar-without-persist', { exact: true }).click();
// open environment dropdown
await page.getByTestId('environment-selector-trigger').click();
// select stage environment
await expect(page.locator('.environment-list .dropdown-item', { hasText: 'Stage' })).toBeVisible();
await page.locator('.environment-list .dropdown-item', { hasText: 'Stage' }).click();
await expect(page.locator('.current-environment', { hasText: 'Stage' })).toBeVisible();
// Send the request
await sendRequest(page, 200);
// confirm that the environment variable is set
await page.getByTestId('environment-selector-trigger').hover();
await page.getByTestId('environment-selector-trigger').click();
await page.locator('#configure-env').waitFor({ state: 'visible' });
await page.locator('#configure-env').dispatchEvent('click');
const envTab = page.locator('.request-tab').filter({ has: page.locator('.tab-label', { hasText: 'Environments' }) });
await expect(envTab).toBeVisible();
await expect(page.getByRole('row', { name: 'token' }).getByRole('cell').nth(1)).toBeVisible();
await expect(page.getByRole('row', { name: 'secret' }).getByRole('cell').nth(2)).toBeVisible();
await envTab.hover();
await envTab.getByTestId('request-tab-close-icon').click({ force: true });
// we restart the app to confirm that the environment variable is not persisted
const newApp = await restartApp();
const newPage = await waitForReadyPage(newApp);
// select the collection and request
await newPage.locator('#sidebar-collection-name').click();
await newPage.getByText('api-setEnvVar-without-persist', { exact: true }).click();
// open environment dropdown
await newPage.getByTestId('environment-selector-trigger').hover();
await newPage.getByTestId('environment-selector-trigger').click();
await newPage.locator('#configure-env').waitFor({ state: 'visible' });
await newPage.locator('#configure-env').dispatchEvent('click');
const newEnvTab = newPage.locator('.request-tab').filter({ has: newPage.locator('.tab-label', { hasText: 'Environments' }) });
await expect(newEnvTab).toBeVisible();
await newPage.locator('.environment-item', { hasText: 'Stage' }).click();
await expect(newPage.locator('.table-container tbody')).not.toContainText('token');
await newEnvTab.hover();
await newEnvTab.getByTestId('request-tab-close-icon').click({ force: true });
await closeElectronApp(newApp);
});
});

View File

@@ -0,0 +1,82 @@
import fs from 'fs';
import path from 'path';
import { test, expect, closeElectronApp } from '../../../playwright';
import { sendRequest, waitForReadyPage } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
test.describe('bru.setEnvVar(name, value)', () => {
test('set env var using script persists by default across restart', async ({ pageWithUserData: page, restartApp, collectionFixturePath }) => {
const stageBruPath = path.join(collectionFixturePath!, 'environments', 'Stage.bru');
const locators = buildCommonLocators(page);
const envTab = page.locator('.request-tab').filter({ hasText: 'Environments' });
const selectStage = async () => {
await locators.environment.selector().click();
await expect(locators.environment.listOption('Stage')).toBeVisible();
await locators.environment.listOption('Stage').click();
await expect(locators.environment.currentEnvironment()).toContainText('Stage');
};
const openEnvEditor = async () => {
await locators.environment.selector().hover();
await locators.environment.selector().click();
await locators.environment.configureButton().waitFor({ state: 'visible' });
await locators.environment.configureButton().dispatchEvent('click');
await expect(envTab).toBeVisible();
};
// Select the collection and request
await page.locator('#sidebar-collection-name').click();
await page.getByText('api-setEnvVar', { exact: true }).click();
await selectStage();
await sendRequest(page, 200);
// Verify the script-set var is visible in the env editor before restart.
await openEnvEditor();
const tokenRow = locators.environment.varRow('token');
await tokenRow.scrollIntoViewIfNeeded();
await expect(tokenRow).toBeVisible();
await expect(locators.environment.varRowLine('token')).toHaveText('secret');
// On-disk env file: setEnvVar persisted `token` to Stage.bru.
await expect.poll(() => fs.readFileSync(stageBruPath, 'utf8'), { timeout: 5000 })
.toMatch(/token:\s*secret/);
await envTab.hover();
await envTab.getByTestId('request-tab-close-icon').click({ force: true });
// Restart to confirm the var was persisted to disk (default behavior in v4).
const newApp = await restartApp();
const newPage = await waitForReadyPage(newApp);
const newLocators = buildCommonLocators(newPage);
const newEnvTab = newPage.locator('.request-tab').filter({ hasText: 'Environments' });
await newPage.locator('#sidebar-collection-name').click();
await newPage.getByText('api-setEnvVar', { exact: true }).click();
// Re-select Stage — active env isn't guaranteed to persist across restart.
await newLocators.environment.selector().click();
await expect(newLocators.environment.listOption('Stage')).toBeVisible();
await newLocators.environment.listOption('Stage').click();
await expect(newLocators.environment.currentEnvironment()).toContainText('Stage');
await newLocators.environment.selector().hover();
await newLocators.environment.selector().click();
await newLocators.environment.configureButton().waitFor({ state: 'visible' });
await newLocators.environment.configureButton().dispatchEvent('click');
await expect(newEnvTab).toBeVisible();
const newTokenRow = newLocators.environment.varRow('token');
await newTokenRow.scrollIntoViewIfNeeded();
await expect(newTokenRow).toBeVisible();
await expect(newLocators.environment.varRowLine('token')).toHaveText('secret');
// On-disk env file survived the restart unchanged.
expect(fs.readFileSync(stageBruPath, 'utf8')).toMatch(/token:\s*secret/);
await newEnvTab.hover();
await newEnvTab.getByTestId('request-tab-close-icon').click({ force: true });
await closeElectronApp(newApp);
});
});

View File

@@ -1,7 +1,7 @@
meta {
name: api-setEnvVar-with-persist
type: http
seq: 1
seq: 2
}
get {
@@ -11,5 +11,5 @@ get {
}
script:pre-request {
bru.setEnvVar("token", "secret", { persist: true });
}
bru.setEnvVar("legacy_persist_var", "from-legacy-flag", { persist: true });
}

View File

@@ -1,5 +1,5 @@
meta {
name: api-setEnvVar-without-persist
name: api-setEnvVar
type: http
seq: 1
}

View File

@@ -11,5 +11,5 @@ get {
}
script:pre-request {
bru.setEnvVar("multiple-persist-vars-key1", "value1", { persist: true });
bru.setEnvVar("multiple-persist-vars-key1", "value1");
}

View File

@@ -11,5 +11,5 @@ get {
}
script:pre-request {
bru.setEnvVar("multiple-persist-vars-key2", "value2", { persist: true });
bru.setEnvVar("multiple-persist-vars-key2", "value2");
}

View File

@@ -55,9 +55,15 @@ test.describe.serial('bru.setEnvVar multiple persistent variables', () => {
// Ensure we're in the correct collection context before selecting the folder
await expect(page.locator('#sidebar-collection-name', { hasText: 'collection' })).toBeVisible();
// Hover on the folder and open context menu
await page.getByText('multiple-persist-vars-folder', { exact: true }).hover();
await page.locator('.collection-item-name').filter({ hasText: 'multiple-persist-vars-folder' }).locator('.menu-icon').click();
// Re-hover on each poll: CSS `:hover` reveals `.menu-icon`, but the cursor
// move between hover() and click() can lose the reveal.
const folderRow = page.locator('.collection-item-name').filter({ hasText: 'multiple-persist-vars-folder' });
const menuIcon = folderRow.locator('.menu-icon');
await expect(async () => {
await folderRow.hover();
await expect(menuIcon).toBeVisible({ timeout: 1000 });
}).toPass({ timeout: 10000 });
await menuIcon.click();
// Click on Run option
await page.getByText('Run', { exact: true }).click();

View File

@@ -0,0 +1,305 @@
import path from 'path';
import fs from 'fs';
import { test, expect, closeElectronApp } from '../../../playwright';
import {
openCollection,
openRequest,
sendRequest,
selectEnvironment,
expectResponseContains,
waitForReadyPage,
openEnvironmentConfigTab,
closeEnvironmentPanel
} from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
import { waitForCollectionMount } from '../../utils/page/mounting';
const NEW_VALUE = 'NEW_VALUE_global_env_e2e_42';
const INITIAL_VALUE = 'INITIAL_VALUE_global_env_e2e_001';
const initUserDataPath = path.join(__dirname, 'init-user-data');
const workspaceFixturePath = path.join(__dirname, 'fixtures', 'workspace');
const findApiTokenSecret = (secretsJson: any, workspaceDir: string) => {
const collection = secretsJson.collections?.find((c: any) =>
path.normalize(c.path) === path.normalize(workspaceDir)
);
const env = collection?.environments?.find((e: any) => e.name === 'Local');
return env?.secrets?.find((s: any) => s.name === 'apiToken');
};
test.describe('bru.setGlobalEnvVar(name, value) - secret variable persistence (workspace mode)', () => {
test('script-set global secret encrypts to the secrets store and reaches a subsequent request via interpolation', async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const workspacePath = await createTmpDir('workspace');
await fs.promises.cp(workspaceFixturePath, workspacePath, { recursive: true });
const envFilePath = path.join(workspacePath, 'environments', 'Local.yml');
const secretsPath = path.join(userDataPath, 'secrets.json');
await test.step('Fixture sanity: global env yml declares apiToken as secret; no secrets store yet', () => {
const initial = fs.readFileSync(envFilePath, 'utf8');
expect(initial).toMatch(/name:\s*apiToken/);
expect(initial).toMatch(/secret:\s*true/);
expect(initial).not.toContain(NEW_VALUE);
expect(fs.existsSync(secretsPath)).toBe(false);
});
const app = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
const page = await waitForReadyPage(app);
const locators = buildCommonLocators(page);
try {
await test.step('Open the setter request and select the "Local" global environment', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
// `persist: true` (double-click) pins the tab — otherwise opening the env config
// tab below replaces this preview tab, and the subsequent sendRequest has no target.
await openRequest(page, 'Test Collection', 'set-global-secret', { persist: true });
await selectEnvironment(page, 'Local', 'global');
});
await test.step('Global env panel BEFORE script: eye toggle reveals an empty initial value', async () => {
await openEnvironmentConfigTab(page, 'global');
const envTab = locators.environment.globalEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror').first())
.toHaveClass(/CodeMirror-empty/);
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.not.toContainText(NEW_VALUE);
await closeEnvironmentPanel(page, 'global');
});
await test.step('Run the request whose post-response script sets apiToken', async () => {
await sendRequest(page, 200);
});
await test.step('On-disk global env .yml: never contains plaintext', async () => {
await expect.poll(() => fs.readFileSync(envFilePath, 'utf8'), { timeout: 5000 })
.toMatch(/name:\s*apiToken/);
const content = fs.readFileSync(envFilePath, 'utf8');
expect(content).not.toContain(NEW_VALUE);
expect(content).toMatch(/secret:\s*true/);
});
await test.step('Secrets store: encrypted apiToken entry is persisted under this workspace', async () => {
await expect.poll(() => fs.existsSync(secretsPath), { timeout: 5000 }).toBe(true);
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), workspacePath);
return secret?.value ?? '';
}, { timeout: 5000 }).not.toBe('');
const secretsJson = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
const secret = findApiTokenSecret(secretsJson, workspacePath);
expect(secret).toBeDefined();
expect(typeof secret!.value).toBe('string');
expect(secret!.value.length).toBeGreaterThan(0);
expect(secret!.value).not.toContain(NEW_VALUE);
expect(JSON.stringify(secretsJson)).not.toContain(NEW_VALUE);
expect(fs.readFileSync(envFilePath, 'utf8')).not.toContain(secret!.value);
});
await test.step('Global env panel AFTER script: eye toggle reveals NEW_VALUE, no draft icon', async () => {
await openEnvironmentConfigTab(page, 'global');
const envTab = locators.environment.globalEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await expect(envTab.locator('.close-gradient')).not.toHaveClass(/has-changes/);
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.toContainText(NEW_VALUE);
await closeEnvironmentPanel(page, 'global');
});
await test.step('Interpolation: a subsequent request resolves {{apiToken}} to the new value', async () => {
await openRequest(page, 'Test Collection', 'read-global-secret', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(app);
}
});
test('script overwrites a previously-set global secret, encrypted store entry changes', async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const workspacePath = await createTmpDir('workspace');
await fs.promises.cp(workspaceFixturePath, workspacePath, { recursive: true });
const envFilePath = path.join(workspacePath, 'environments', 'Local.yml');
const secretsPath = path.join(userDataPath, 'secrets.json');
const app = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
const page = await waitForReadyPage(app);
const locators = buildCommonLocators(page);
try {
await test.step('Open seed-global-secret and select the "Local" global environment', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await openRequest(page, 'Test Collection', 'seed-global-secret', { persist: true });
await selectEnvironment(page, 'Local', 'global');
});
await test.step('Seed: post-response script sets apiToken to INITIAL_VALUE', async () => {
await sendRequest(page, 200);
});
let initialEncryptedValue = '';
await test.step('After seed: global env panel shows INITIAL_VALUE; encrypted entry persisted', async () => {
await openEnvironmentConfigTab(page, 'global');
const envTab = locators.environment.globalEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.toContainText(INITIAL_VALUE);
await closeEnvironmentPanel(page, 'global');
await expect.poll(() => fs.existsSync(secretsPath), { timeout: 5000 }).toBe(true);
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), workspacePath);
return secret?.value ?? '';
}, { timeout: 5000 }).not.toBe('');
const secretsJson = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
const secret = findApiTokenSecret(secretsJson, workspacePath);
expect(secret!.value).not.toContain(INITIAL_VALUE);
expect(JSON.stringify(secretsJson)).not.toContain(INITIAL_VALUE);
expect(fs.readFileSync(envFilePath, 'utf8')).not.toContain(secret!.value);
initialEncryptedValue = secret!.value;
});
await test.step('Overwrite: open set-global-secret and run it (NEW_VALUE replaces INITIAL_VALUE)', async () => {
await openRequest(page, 'Test Collection', 'set-global-secret', { persist: true });
await sendRequest(page, 200);
});
await test.step('After overwrite: panel shows NEW_VALUE (not INITIAL_VALUE); encrypted entry changed; .yml still clean', async () => {
await openEnvironmentConfigTab(page, 'global');
const envTab = locators.environment.globalEnvTab();
await locators.environment.secretsTab().click();
await expect(locators.environment.varRow('apiToken')).toBeVisible();
await expect(envTab.locator('.close-gradient')).not.toHaveClass(/has-changes/);
await locators.environment.varRowEyeToggle('apiToken').click();
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.toContainText(NEW_VALUE);
await expect(locators.environment.varRow('apiToken').locator('.CodeMirror'))
.not.toContainText(INITIAL_VALUE);
await closeEnvironmentPanel(page, 'global');
// Encrypted blob on disk changed — proves the secrets store was rewritten, not stale-cached.
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), workspacePath);
return secret?.value;
}, { timeout: 5000 }).not.toBe(initialEncryptedValue);
const secretsJson = JSON.parse(fs.readFileSync(secretsPath, 'utf8'));
const secret = findApiTokenSecret(secretsJson, workspacePath);
expect(secret!.value).not.toContain(NEW_VALUE);
expect(secret!.value.length).toBeGreaterThan(0);
expect(JSON.stringify(secretsJson)).not.toContain(NEW_VALUE);
expect(JSON.stringify(secretsJson)).not.toContain(INITIAL_VALUE);
const content = fs.readFileSync(envFilePath, 'utf8');
expect(content).not.toContain(NEW_VALUE);
expect(content).not.toContain(INITIAL_VALUE);
expect(content).not.toContain(secret!.value);
expect(content).toMatch(/secret:\s*true/);
});
await test.step('Interpolation: a subsequent request resolves {{apiToken}} to NEW_VALUE', async () => {
await openRequest(page, 'Test Collection', 'read-global-secret', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(app);
}
});
test('persisted global secret survives app restart and decrypts on the next launch', async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const workspacePath = await createTmpDir('workspace');
await fs.promises.cp(workspaceFixturePath, workspacePath, { recursive: true });
const secretsPath = path.join(userDataPath, 'secrets.json');
let blobBeforeRestart = '';
const firstApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
try {
const page = await waitForReadyPage(firstApp);
await test.step('First launch: run set-global-secret and confirm the encrypted blob is on disk', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await openRequest(page, 'Test Collection', 'set-global-secret', { persist: true });
await selectEnvironment(page, 'Local', 'global');
await sendRequest(page, 200);
await expect.poll(() => fs.existsSync(secretsPath), { timeout: 5000 }).toBe(true);
await expect.poll(() => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), workspacePath);
return secret?.value ?? '';
}, { timeout: 5000 }).not.toBe('');
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), workspacePath);
blobBeforeRestart = secret!.value;
});
} finally {
await closeElectronApp(firstApp);
}
const secondApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
try {
const page = await waitForReadyPage(secondApp);
await test.step('Second launch: encrypted blob unchanged on disk', async () => {
const secret = findApiTokenSecret(JSON.parse(fs.readFileSync(secretsPath, 'utf8')), workspacePath);
expect(secret!.value).toBe(blobBeforeRestart);
});
await test.step('Decryption round-trip: read-global-secret interpolates {{apiToken}} to the original plaintext', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await selectEnvironment(page, 'Local', 'global');
await openRequest(page, 'Test Collection', 'read-global-secret', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(secondApp);
}
});
});

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "Test Collection",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,11 @@
meta {
name: read-global-secret
type: http
seq: 2
}
get {
url: {{baseUrl}}/query?token={{apiToken}}
body: none
auth: none
}

View File

@@ -0,0 +1,15 @@
meta {
name: seed-global-secret
type: http
seq: 3
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
bru.setGlobalEnvVar("apiToken", "INITIAL_VALUE_global_env_e2e_001");
}

View File

@@ -0,0 +1,15 @@
meta {
name: set-global-secret
type: http
seq: 1
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
bru.setGlobalEnvVar("apiToken", "NEW_VALUE_global_env_e2e_42");
}

View File

@@ -0,0 +1,10 @@
name: Local
variables:
- name: baseUrl
value: http://localhost:8081
enabled: true
secret: false
- name: apiToken
value: ""
enabled: true
secret: true

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,10 @@
{
"collections": [
{
"path": "{{workspacePath}}/collections/test-collection",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,7 @@
{
"preferences": {
"general": {
"defaultWorkspacePath": "{{workspacePath}}"
}
}
}

View File

@@ -0,0 +1,86 @@
import path from 'path';
import fs from 'fs';
import { test, expect, closeElectronApp } from '../../../playwright';
import {
openCollection,
openRequest,
sendRequest,
selectEnvironment,
expectResponseContains,
waitForReadyPage
} from '../../utils/page';
import { waitForCollectionMount } from '../../utils/page/mounting';
const NEW_VALUE = 'VALUE_global_nonsecret_e2e_42';
const initUserDataPath = path.join(__dirname, 'init-user-data');
const workspaceFixturePath = path.join(__dirname, 'fixtures', 'workspace');
test.describe('bru.setGlobalEnvVar(name, value) - non-secret variable persistence (workspace mode)', () => {
test('persisted non-secret global var survives app restart and interpolates on the next launch', async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const workspacePath = await createTmpDir('workspace');
await fs.promises.cp(workspaceFixturePath, workspacePath, { recursive: true });
const envFilePath = path.join(workspacePath, 'environments', 'Local.yml');
await test.step('Fixture sanity: global env yml declares globalToken (non-secret) with empty value', () => {
const initial = fs.readFileSync(envFilePath, 'utf8');
expect(initial).toMatch(/name:\s*globalToken/);
expect(initial).toMatch(/secret:\s*false/);
expect(initial).not.toContain(NEW_VALUE);
});
const firstApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
try {
const page = await waitForReadyPage(firstApp);
await test.step('First launch: run set-global-var; non-secret value is written into the env yml on disk', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await openRequest(page, 'Test Collection', 'set-global-var', { persist: true });
await selectEnvironment(page, 'Local', 'global');
await sendRequest(page, 200);
await expect.poll(() => fs.readFileSync(envFilePath, 'utf8'), { timeout: 5000 })
.toContain(NEW_VALUE);
const content = fs.readFileSync(envFilePath, 'utf8');
expect(content).toMatch(/name:\s*globalToken/);
});
} finally {
await closeElectronApp(firstApp);
}
const contentBeforeRestart = fs.readFileSync(envFilePath, 'utf8');
const secondApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
try {
const page = await waitForReadyPage(secondApp);
await test.step('Second launch: env yml on disk is byte-identical to pre-restart', () => {
expect(fs.readFileSync(envFilePath, 'utf8')).toBe(contentBeforeRestart);
});
await test.step('Interpolation after restart: read-global-var resolves {{globalToken}} to the persisted value', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await selectEnvironment(page, 'Local', 'global');
await openRequest(page, 'Test Collection', 'read-global-var', { persist: true });
await sendRequest(page, 200);
await expectResponseContains(page, [NEW_VALUE]);
});
} finally {
await closeElectronApp(secondApp);
}
});
});

View File

@@ -0,0 +1,101 @@
import path from 'path';
import fs from 'fs';
import { test, expect, closeElectronApp } from '../../../playwright';
import {
openCollection,
openRequest,
sendRequest,
selectEnvironment,
waitForReadyPage
} from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
import { waitForCollectionMount } from '../../utils/page/mounting';
const initUserDataPath = path.join(__dirname, 'init-user-data');
const workspaceFixturePath = path.join(__dirname, 'fixtures', 'workspace');
test.describe('bru.setGlobalEnvVar(name, value) - typed value persistence (workspace mode)', () => {
test('persists number/boolean/object/string global vars with correct dataType across restart', async ({
launchElectronApp,
createTmpDir
}) => {
const userDataPath = await createTmpDir('userdata');
const workspacePath = await createTmpDir('workspace');
await fs.promises.cp(workspaceFixturePath, workspacePath, { recursive: true });
const envFilePath = path.join(workspacePath, 'environments', 'Local.yml');
const firstApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
try {
const page = await waitForReadyPage(firstApp);
await test.step('First launch: run set-typed-global-vars; Local.yml gains typed entries with `type` annotations', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await openRequest(page, 'Test Collection', 'set-typed-global-vars', { persist: true });
await selectEnvironment(page, 'Local', 'global');
await sendRequest(page, 200);
// The YAML serializer emits typed values as { value: { type, data } } objects —
// see packages/bruno-filestore/src/formats/yml/common/datatype.ts:34.
await expect.poll(() => fs.readFileSync(envFilePath, 'utf8'), { timeout: 5000 })
.toMatch(/name:\s*global_num[\s\S]*?type:\s*number[\s\S]*?data:\s*['"]?42/);
const content = fs.readFileSync(envFilePath, 'utf8');
expect(content).toMatch(/name:\s*global_bool[\s\S]*?type:\s*boolean[\s\S]*?data:\s*['"]?true/);
expect(content).toMatch(/name:\s*global_obj[\s\S]*?type:\s*object[\s\S]*?data:[\s\S]*?scope/);
// 'string' is the implicit default — the serializer emits a raw string value, no `type:` block.
expect(content).toMatch(/name:\s*global_str[\s\S]*?value:\s*hello/);
expect(content).not.toMatch(/name:\s*global_str[\s\S]*?type:\s*string/);
});
} finally {
await closeElectronApp(firstApp);
}
const contentBeforeRestart = fs.readFileSync(envFilePath, 'utf8');
const secondApp = await launchElectronApp({
initUserDataPath,
userDataPath,
templateVars: { workspacePath }
});
try {
const page = await waitForReadyPage(secondApp);
await test.step('Second launch: Local.yml on disk is byte-identical to pre-restart', () => {
expect(fs.readFileSync(envFilePath, 'utf8')).toBe(contentBeforeRestart);
});
await test.step('Second launch: the global env editor reflects the persisted dataTypes', async () => {
await waitForCollectionMount(page, 'Test Collection');
await openCollection(page, 'Test Collection');
await selectEnvironment(page, 'Local', 'global');
// Open the Global Environments config tab via the env selector → configure button.
const locators = buildCommonLocators(page);
await locators.environment.selector().click();
await locators.environment.globalTab().click();
await locators.environment.configureButton().waitFor({ state: 'visible' });
await locators.environment.configureButton().dispatchEvent('click');
await expect(locators.environment.globalEnvTab()).toBeVisible();
const numRow = locators.environment.variableRowByName('global_num');
const boolRow = locators.environment.variableRowByName('global_bool');
const objRow = locators.environment.variableRowByName('global_obj');
const strRow = locators.environment.variableRowByName('global_str');
await expect(numRow).toBeVisible();
await expect(locators.dataTypeSelector.typeLabel(numRow)).toHaveText('number', { timeout: 5000 });
await expect(locators.dataTypeSelector.typeLabel(boolRow)).toHaveText('boolean');
await expect(locators.dataTypeSelector.typeLabel(objRow)).toHaveText('object');
await expect(locators.dataTypeSelector.typeLabel(strRow)).toHaveText('string');
});
} finally {
await closeElectronApp(secondApp);
}
});
});

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "Test Collection",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,11 @@
meta {
name: read-global-var
type: http
seq: 2
}
get {
url: {{baseUrl}}/query?token={{globalToken}}
body: none
auth: none
}

View File

@@ -0,0 +1,15 @@
meta {
name: set-global-var
type: http
seq: 1
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
bru.setGlobalEnvVar("globalToken", "VALUE_global_nonsecret_e2e_42");
}

View File

@@ -0,0 +1,20 @@
meta {
name: set-typed-global-vars
type: http
seq: 2
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
// Typed values persist with their dataType annotation in Local.yml's variables list.
bru.setGlobalEnvVar("global_num", 42);
bru.setGlobalEnvVar("global_bool", true);
bru.setGlobalEnvVar("global_obj", { scope: "global" });
// Plain string — no dataType materialized.
bru.setGlobalEnvVar("global_str", "hello");
}

View File

@@ -0,0 +1,10 @@
name: Local
variables:
- name: baseUrl
value: http://localhost:8081
enabled: true
secret: false
- name: globalToken
value: ""
enabled: true
secret: false

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,10 @@
{
"collections": [
{
"path": "{{workspacePath}}/collections/test-collection",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,7 @@
{
"preferences": {
"general": {
"defaultWorkspacePath": "{{workspacePath}}"
}
}
}

View File

@@ -0,0 +1,62 @@
import { test, expect } from '../../../playwright';
import fs from 'fs';
import path from 'path';
import { openCollection, sendRequest, selectEnvironment } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
const selectAllShortcut = process.platform === 'darwin' ? 'Meta+a' : 'Control+a';
const PERSISTENCE_TIMEOUT = 10000;
test.describe('Collection variables draft merge with script-set variables', () => {
test('preserves unsaved draft edits when script sets a new collection variable', async ({
pageWithUserData: page,
collectionFixturePath
}) => {
const locators = buildCommonLocators(page);
await openCollection(page, 'collection-vars-draft-merge-test');
await selectEnvironment(page, 'Test');
await test.step('Open collection settings and edit existingCollVar (create draft)', async () => {
await locators.sidebar.collection('collection-vars-draft-merge-test').click();
await locators.paneTabs.collectionSettingsTab('vars').click();
await expect(locators.environment.variableRowByName('existingCollVar')).toBeVisible();
await locators.environment.variableValue('existingCollVar').click();
await page.keyboard.press(selectAllShortcut);
await page.keyboard.type('draft-edited-coll-value');
// Wait for draft debounce
await page.waitForTimeout(500);
});
await test.step('Open request and send it', async () => {
await locators.sidebar.request('set-collection-var').click();
await expect(locators.tabs.requestTab('set-collection-var')).toBeVisible();
await sendRequest(page, 200);
});
await test.step('Verify draft edit and script var in collection settings UI', async () => {
await locators.sidebar.collection('collection-vars-draft-merge-test').click();
await locators.paneTabs.collectionSettingsTab('vars').click();
await expect(locators.environment.variableRowByName('existingCollVar')).toBeVisible();
await expect(locators.environment.variableValue('existingCollVar')).toContainText('draft-edited-coll-value');
await expect(locators.environment.variableRowByName('scriptCollVar')).toBeVisible();
await expect(locators.environment.variableValue('scriptCollVar')).toContainText('from-script-789');
});
await test.step('Verify script var persisted to collection.bru', async () => {
const collectionBruPath = path.join(
collectionFixturePath!,
'collection-vars-draft-merge-test',
'collection.bru'
);
await expect.poll(() => {
const content = fs.readFileSync(collectionBruPath, 'utf8');
return content.includes('scriptCollVar') && content.includes('from-script-789');
}, { timeout: PERSISTENCE_TIMEOUT }).toBe(true);
});
});
});

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "collection-vars-draft-merge-test",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,4 @@
vars:pre-request {
host: https://testbench-sanity.usebruno.com
existingCollVar: original-coll-value
}

View File

@@ -0,0 +1,3 @@
vars {
host: https://testbench-sanity.usebruno.com
}

View File

@@ -0,0 +1,22 @@
meta {
name: set-collection-var
type: http
seq: 1
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:post-response {
bru.setCollectionVar("scriptCollVar", "from-script-789");
}
tests {
test("should set collection var", function() {
const val = bru.getCollectionVar("scriptCollVar");
expect(val).to.equal("from-script-789");
});
}

View File

@@ -0,0 +1,10 @@
{
"collections": [
{
"path": "{{collectionPath}}/collection-vars-draft-merge-test",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,5 @@
{
"lastOpenedCollections": [
"{{collectionPath}}/collection-vars-draft-merge-test"
]
}

View File

@@ -204,6 +204,17 @@ for (const { format, collectionName } of FORMATS) {
await fileChooser.setFiles(exportedFile);
});
await test.step(`Select the imported "${IMPORTED_ENV_NAME}" in the env editor sidebar`, async () => {
// After import, the editor stays on whichever env was previously selected.
// Explicitly switch to the imported env so the assertions exercise its rendering.
const importedItem = page
.locator('.environments-list .environment-item')
.filter({ hasText: IMPORTED_ENV_NAME });
await expect(importedItem).toBeVisible();
await importedItem.click();
await expect(importedItem).toHaveClass(/\bactive\b/);
});
await test.step('Verify the imported env editor shows datatypes correctly', async () => {
await expect(locators.tabs.activeRequestTab()).toContainText('Environments');

View File

@@ -0,0 +1,55 @@
import { test, expect } from '../../../playwright';
import fs from 'fs';
import path from 'path';
import { openCollection, selectEnvironment, openEnvironmentSelector, sendRequest } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
const PERSISTENCE_TIMEOUT = 10000;
const selectAllShortcut = process.platform === 'darwin' ? 'Meta+a' : 'Control+a';
test.describe('Draft environment merge with script-set variables', () => {
test('preserves unsaved draft edits when script sets a new variable', async ({ pageWithUserData: page, collectionFixturePath }) => {
const locators = buildCommonLocators(page);
await openCollection(page, 'draft-merge-test');
await selectEnvironment(page, 'Test');
await test.step('Edit existingVar in environment UI (create draft)', async () => {
await openEnvironmentSelector(page, 'collection');
await locators.environment.configureButton().click();
await expect(locators.environment.collectionEnvTab()).toBeVisible();
await expect(locators.environment.variableRowByName('existingVar')).toBeVisible();
await locators.environment.variableValue('existingVar').click();
await page.keyboard.press(selectAllShortcut);
await page.keyboard.type('draft-edited-value');
await expect(locators.environment.collectionEnvTab().locator('.close-gradient'))
.toHaveClass(/has-changes/);
});
await test.step('Open request and send it', async () => {
await locators.sidebar.request('set-env-var').click();
await expect(locators.tabs.requestTab('set-env-var')).toBeVisible();
await sendRequest(page, 200);
});
await test.step('Verify both values in environment UI', async () => {
await locators.environment.collectionEnvTab().click();
await expect(locators.environment.variableRowByName('existingVar')).toBeVisible();
await expect(locators.environment.variableValue('existingVar')).toContainText('draft-edited-value');
await expect(locators.environment.variableRowByName('scriptToken')).toBeVisible();
await expect(locators.environment.variableValue('scriptToken')).toContainText('from-script-123');
});
await test.step('Verify both values persisted to disk', async () => {
const envFilePath = path.join(collectionFixturePath!, 'draft-merge-test', 'environments', 'Test.bru');
await expect.poll(() => {
const content = fs.readFileSync(envFilePath, 'utf8');
return content.includes('draft-edited-value') && content.includes('from-script-123');
}, { timeout: PERSISTENCE_TIMEOUT }).toBe(true);
});
});
});

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "draft-merge-test",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,4 @@
vars {
host: https://testbench-sanity.usebruno.com
existingVar: original-value
}

View File

@@ -0,0 +1,22 @@
meta {
name: set-env-var
type: http
seq: 1
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:post-response {
bru.setEnvVar("scriptToken", "from-script-123");
}
tests {
test("should set env var", function() {
const val = bru.getEnvVar("scriptToken");
expect(val).to.equal("from-script-123");
});
}

View File

@@ -0,0 +1,10 @@
{
"collections": [
{
"path": "{{collectionPath}}/draft-merge-test",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,5 @@
{
"lastOpenedCollections": [
"{{collectionPath}}/draft-merge-test"
]
}

View File

@@ -0,0 +1,9 @@
{
"version": "1",
"name": "global-env-draft-merge-test",
"type": "collection",
"ignore": [
"node_modules",
".git"
]
}

View File

@@ -0,0 +1,22 @@
meta {
name: set-global-env-var
type: http
seq: 1
}
get {
url: {{baseUrl}}/ping
body: none
auth: none
}
script:post-response {
bru.setGlobalEnvVar("scriptGlobalToken", "from-script-456");
}
tests {
test("should set global env var", function() {
const val = bru.getGlobalEnvVar("scriptGlobalToken");
expect(val).to.equal("from-script-456");
});
}

View File

@@ -0,0 +1,48 @@
import { test, expect } from '../../../playwright';
import { openCollection, sendRequest, openEnvironmentSelector } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
const selectAllShortcut = process.platform === 'darwin' ? 'Meta+a' : 'Control+a';
test.describe('Global environment draft merge with script-set variables', () => {
test('preserves unsaved draft edits when script sets a new global env variable', async ({
pageWithUserData: page
}) => {
const locators = buildCommonLocators(page);
await openCollection(page, 'global-env-draft-merge-test');
await test.step('Edit existingVar in global environment UI (create draft)', async () => {
await openEnvironmentSelector(page, 'global');
await locators.environment.configureButton().click();
await expect(locators.environment.globalEnvTab()).toBeVisible();
await expect(locators.environment.variableRowByName('existingVar')).toBeVisible();
await locators.environment.variableValue('existingVar').click();
await page.keyboard.press(selectAllShortcut);
await page.keyboard.type('draft-edited-global-value');
await expect(locators.environment.globalEnvTab().locator('.close-gradient'))
.toHaveClass(/has-changes/);
});
await test.step('Open request and send it', async () => {
await locators.sidebar.request('set-global-env-var').click();
await expect(locators.tabs.requestTab('set-global-env-var')).toBeVisible();
await sendRequest(page, 200);
});
await test.step('Verify both draft edit and script variable in global env UI', async () => {
await locators.environment.globalEnvTab().click();
await expect(locators.environment.variableRowByName('existingVar')).toBeVisible();
await expect(locators.environment.variableValue('existingVar')).toContainText('draft-edited-global-value');
await expect(locators.environment.variableRowByName('scriptGlobalToken')).toBeVisible();
await expect(locators.environment.variableValue('scriptGlobalToken')).toContainText('from-script-456');
await expect(locators.environment.variableRowByName('baseUrl')).toBeVisible();
await expect(locators.environment.variableValue('baseUrl')).toContainText('https://testbench-sanity.usebruno.com');
});
});
});

View File

@@ -0,0 +1,10 @@
{
"collections": [
{
"path": "{{collectionPath}}/global-env-draft-merge-test",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,27 @@
{
"environments": [
{
"uid": "RrPsTcwRnHMv3yljQO3ex",
"name": "global",
"variables": [
{
"uid": "VXKOZdkYw0DyI4mlhn6Wr",
"name": "baseUrl",
"value": "https://testbench-sanity.usebruno.com",
"type": "text",
"secret": false,
"enabled": true
},
{
"uid": "NTwrSscXsaeh4uee6ocJN",
"name": "existingVar",
"value": "original-value",
"type": "text",
"secret": false,
"enabled": true
}
]
}
],
"activeGlobalEnvironmentUid": "RrPsTcwRnHMv3yljQO3ex"
}

View File

@@ -0,0 +1,5 @@
{
"lastOpenedCollections": [
"{{collectionPath}}/global-env-draft-merge-test"
]
}

View File

@@ -1,5 +1,6 @@
import { test, expect } from '../../../playwright';
import { closeAllCollections } from '../../utils/page';
import { buildCommonLocators } from '../../utils/page/locators';
test.describe('Global Environment Variable Update via Script', () => {
test.afterEach(async ({ pageWithUserData: page }) => {
@@ -9,54 +10,58 @@ test.describe('Global Environment Variable Update via Script', () => {
test('should update global environment values via script and verify the changes', async ({
pageWithUserData: page
}) => {
const locators = buildCommonLocators(page);
await test.step('Open the collection from sidebar', async () => {
await page.locator('#sidebar-collection-name').filter({ hasText: 'Global Environment Update' }).click();
await locators.sidebar.collection('Global Environment Update').click();
});
await test.step('Open the test request that has a pre-request script', async () => {
await page.locator('.collection-name', { hasText: 'Global Environment Update' }).click();
await page.locator('.collection-item-name', { hasText: 'Test Request' }).click();
await locators.sidebar.request('Test Request').click();
});
await test.step('Run the request', async () => {
await page.getByTestId('send-arrow-icon').click();
await locators.request.sendButton().click();
});
await test.step('Open the Global Environment Config tab', async () => {
await page.getByTestId('environment-selector-trigger').click();
await page.getByTestId('env-tab-global').click();
await locators.environment.selector().click();
await locators.environment.globalTab().click();
await page.getByText('Configure', { exact: true }).click();
const envTab = page.locator('.request-tab').filter({ hasText: 'Global Environments' });
await expect(envTab).toBeVisible();
await expect(locators.environment.globalEnvTab()).toBeVisible();
});
await test.step('Verify that the value of "existingEnvEnabled" is updated by the pre-request script', async () => {
const row = page.locator('tbody tr').filter({ has: page.locator('input[value="existingEnvEnabled"]') });
const value = await row.locator('.CodeMirror-line').first().textContent();
await expect(value).toContain('newExistingEnvEnabledValue');
await test.step('"existingEnvEnabled" is updated by the pre-request script', async () => {
await expect(
locators.environment.varRowsByValue('existingEnvEnabled', 'newExistingEnvEnabledValue')
).toHaveCount(1);
});
await test.step('Verify that the value of "existingEnvDisabled" is updated by the pre-request script', async () => {
const row = page.locator('tbody tr').filter({ has: page.locator('input[value="existingEnvDisabled"]') });
const value = await row.locator('.CodeMirror-line').first().textContent();
await expect(value).toContain('newExistingEnvDisabledValue');
await test.step('"existingEnvDisabled" — disabled slot preserved, script write creates a new enabled slot', async () => {
await expect(locators.environment.varRow('existingEnvDisabled')).toHaveCount(2);
await expect(
locators.environment.varRowsByValue('existingEnvDisabled', 'newExistingEnvDisabledValue')
).toHaveCount(1);
await expect(
locators.environment.varRowsByValue('existingEnvDisabled', /^existingEnvDisabledValue$/)
).toHaveCount(1);
});
await test.step('Verify that a new env variable "newEnv" is added by the pre-request script to the global environment', async () => {
const row = page.locator('tbody tr').filter({ has: page.locator('input[value="newEnv"]') });
const value = await row.locator('.CodeMirror-line').first().textContent();
await expect(value).toContain('newEnvValue');
await test.step('"newEnv" is added by the pre-request script', async () => {
await expect(
locators.environment.varRowsByValue('newEnv', 'newEnvValue')
).toHaveCount(1);
});
await test.step('Verify that the value of "baseUrl" is unchanged.', async () => {
const row = page.locator('tbody tr').filter({ has: page.locator('input[value="baseUrl"]') });
const value = await row.locator('.CodeMirror-line').first().textContent();
await expect(value).toContain('https://echo.usebruno.com');
await test.step('"baseUrl" is unchanged', async () => {
await expect(
locators.environment.varRowsByValue('baseUrl', 'https://echo.usebruno.com')
).toHaveCount(1);
});
await test.step('Close the global environment config tab.', async () => {
const envTab = page.locator('.request-tab').filter({ hasText: 'Global Environments' });
await test.step('Close the global environment config tab', async () => {
const envTab = locators.environment.globalEnvTab();
await envTab.hover();
await envTab.getByTestId('request-tab-close-icon').click({ force: true });
});