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

@@ -445,18 +445,7 @@ const EnvironmentVariablesTable = ({
const otherCurrent = namedValues.filter((variable) => !belongsToActiveTab(variable));
const otherSaved = savedValues.filter((variable) => !belongsToActiveTab(variable));
// Compare against what's on disk: for an ephemeral overlay, that's
// `persistedValue`, not the scripted value Redux is holding.
const baselineForCompare = (v) => {
const stripped = stripEnvVarUid(v);
if (v?.ephemeral && v?.persistedValue !== undefined) {
stripped.value = v.persistedValue;
}
return stripped;
};
// Compare without UIDs; only the active tab's subset decides if there's anything to save.
const hasChanges
= JSON.stringify(activeCurrent.map(stripEnvVarUid)) !== JSON.stringify(activeSaved.map(baselineForCompare));
const hasChanges = JSON.stringify(activeCurrent.map(stripEnvVarUid)) !== JSON.stringify(activeSaved.map(stripEnvVarUid));
if (!hasChanges) {
toast.error('No changes to save');
return;
@@ -759,11 +748,6 @@ const EnvironmentVariablesTable = ({
isSecret={variable.secret}
onChange={(newValue) => {
formik.setFieldValue(`${actualIndex}.value`, newValue, true);
// Clear ephemeral metadata when user manually edits the value
if (variable.ephemeral) {
formik.setFieldValue(`${actualIndex}.ephemeral`, undefined, false);
formik.setFieldValue(`${actualIndex}.persistedValue`, undefined, false);
}
// Append a new empty row when editing value on the last row
if (isLastRow) {
setTimeout(() => {

View File

@@ -33,15 +33,15 @@ const ConfirmCloseEnvironment = ({ onCancel, onCloseWithoutSave, onSaveAndClose,
<div className="flex justify-between mt-6">
<div>
<Button color="danger" onClick={onCloseWithoutSave}>
<Button color="danger" onClick={onCloseWithoutSave} data-testid="env-unsaved-close-without-save">
Don't Save
</Button>
</div>
<div className="flex gap-2">
<Button size="sm" color="secondary" variant="ghost" onClick={onCancel}>
<Button size="sm" color="secondary" variant="ghost" onClick={onCancel} data-testid="env-unsaved-cancel">
Cancel
</Button>
<Button onClick={onSaveAndClose}>
<Button onClick={onSaveAndClose} data-testid="env-unsaved-save-and-close">
Save
</Button>
</div>

View File

@@ -219,7 +219,7 @@ class MultiLineEditor extends Component {
*/
secretEye = (isSecret) => {
return isSecret === true ? (
<button className="mx-2" onClick={() => this.toggleVisibleSecret()}>
<button className="mx-2" data-testid="secret-reveal-toggle" onClick={() => this.toggleVisibleSecret()}>
{this.state.maskInput === true ? (
<IconEyeOff size={18} strokeWidth={2} />
) : (

View File

@@ -302,7 +302,7 @@ class SingleLineEditor extends Component {
*/
secretEye = (isSecret) => {
return isSecret === true ? (
<button type="button" className="mx-2" onClick={() => this.toggleVisibleSecret()}>
<button type="button" className="mx-2" data-testid="secret-reveal-toggle" onClick={() => this.toggleVisibleSecret()}>
{this.state.maskInput === true ? (
<IconEyeOff size={18} strokeWidth={2} />
) : (

View File

@@ -22,10 +22,11 @@ import {
runFolderEvent,
runRequestEvent,
scriptEnvironmentUpdateEvent,
runtimeVariablesUpdateEvent,
streamDataReceived,
setDotEnvVariables
} from 'providers/ReduxStore/slices/collections';
import { collectionAddEnvFileEvent, openCollectionEvent, hydrateCollectionWithUiStateSnapshot, mergeAndPersistEnvironment } from 'providers/ReduxStore/slices/collections/actions';
import { collectionAddEnvFileEvent, openCollectionEvent, hydrateCollectionWithUiStateSnapshot, persistActiveEnvironment, collectionVariablesUpdateEvent } from 'providers/ReduxStore/slices/collections/actions';
import {
workspaceOpenedEvent,
workspaceConfigUpdatedEvent,
@@ -35,7 +36,7 @@ import { workspaceDotEnvUpdateEvent, setWorkspaceDotEnvVariables } from 'provide
import toast from 'react-hot-toast';
import { useDispatch, useStore } from 'react-redux';
import { isElectron } from 'utils/common/platform';
import { globalEnvironmentsUpdateEvent, updateGlobalEnvironments } from 'providers/ReduxStore/slices/global-environments';
import { globalEnvironmentsUpdateEvent, updateGlobalEnvironments, _clearScriptGlobalEnvBaseline } from 'providers/ReduxStore/slices/global-environments';
import { collectionAddOauth2CredentialsByUrl, collectionClearOauth2CredentialsByCredentialsId, updateCollectionLoadingState, collectionLoadedFromTree } from 'providers/ReduxStore/slices/collections/index';
import { addLog } from 'providers/ReduxStore/slices/logs';
import { loadNotifications } from 'providers/ReduxStore/slices/notifications';
@@ -201,23 +202,36 @@ const useIpcEvents = () => {
}
});
const removeScriptEnvUpdateListener = ipcRenderer.on('main:script-environment-update', (val) => {
dispatch(scriptEnvironmentUpdateEvent(val));
const removeRuntimeVariablesUpdateListener = ipcRenderer.on('main:runtime-variables-update', (val) => {
dispatch(runtimeVariablesUpdateEvent(val));
});
const removePersistentEnvVariablesUpdateListener = ipcRenderer.on('main:persistent-env-variables-update', (val) => {
dispatch(mergeAndPersistEnvironment(val));
const removeScriptEnvUpdateListener = ipcRenderer.on('main:script-environment-update', (val) => {
dispatch(scriptEnvironmentUpdateEvent(val));
if (val.collectionUid) {
dispatch(persistActiveEnvironment(val.collectionUid, val.requestUid));
}
});
const removeGlobalEnvironmentVariablesUpdateListener = ipcRenderer.on('main:global-environment-variables-update', (val) => {
dispatch(globalEnvironmentsUpdateEvent(val));
});
const removeCollectionVariablesUpdateListener = ipcRenderer.on('main:collection-variables-update', (val) => {
dispatch(collectionVariablesUpdateEvent(val));
});
const removeCollectionRenamedListener = ipcRenderer.on('main:collection-renamed', (val) => {
dispatch(collectionRenamedEvent(val));
});
const removeRunFolderEventListener = ipcRenderer.on('main:run-folder-event', (val) => {
// Folder runs reuse the workspace baseline across N requests; clear it
// per request so request N's global-env update doesn't diff against
// request N-1's pre-flush snapshot.
if (val.type === 'testrun-started' || val.type === 'request-queued') {
dispatch(_clearScriptGlobalEnvBaseline());
}
dispatch(runFolderEvent(val));
});
@@ -393,7 +407,8 @@ const useIpcEvents = () => {
removeHttpStreamNewDataListener();
removeHttpStreamEndListener();
removeCollectionLoadingStateListener();
removePersistentEnvVariablesUpdateListener();
removeCollectionVariablesUpdateListener();
removeRuntimeVariablesUpdateListener();
removeSystemResourcesListener();
gitVersionListener();
removeLoadNotificationsListener();

View File

@@ -60,6 +60,9 @@ import {
updateFolderVar,
addCollectionVar,
updateCollectionVar,
scriptUpdateCollectionVars,
setScriptCollVarBaseline,
_clearScriptCollectionBaselines,
addTransientDirectory,
addSaveTransientRequestModal,
updatePathParam,
@@ -85,12 +88,12 @@ import {
mergeHeaders
} from 'utils/collections/index';
import { sanitizeName } from 'utils/common/regex';
import { buildPersistedEnvVariables } from 'utils/environments';
import { applyScriptEnvVars, getScriptModifiedKeys } from 'utils/environments';
import { safeParseJSON, safeStringifyJSON } from 'utils/common/index';
import { resolveInheritedAuth } from 'utils/auth';
import { addTab } from 'providers/ReduxStore/slices/tabs';
import { updateSettingsSelectedTab } from './index';
import { saveGlobalEnvironment } from 'providers/ReduxStore/slices/global-environments';
import { saveGlobalEnvironment, _clearScriptGlobalEnvBaseline } from 'providers/ReduxStore/slices/global-environments';
import { getTabToFocusForCurrentWorkspace } from 'providers/ReduxStore/slices/workspaces/getTabToFocusForCurrentWorkspace';
import { clearPersistedScope } from 'hooks/usePersistedState/PersistedScopeProvider';
import {
@@ -501,6 +504,10 @@ export const wsConnectOnly = (item, collectionUid) => (dispatch, getState) => {
const environment = findEnvironmentInCollection(collectionCopy, collectionCopy.activeEnvironmentUid);
// WS connect does not run user scripts — no baseline to clear.
// Wiping baselines here would also wipe collection._scriptRequestUid, opening
// a window where a late HTTP post-response could pass the stale-update gate.
connectWS(itemCopy, collectionCopy, environment, collectionCopy.runtimeVariables, { connectOnly: true })
.then(resolve)
.catch((err) => {
@@ -619,6 +626,8 @@ export const sendRequest = (item, collectionUid) => (dispatch, getState) => {
return reject(error);
}
dispatch(clearScriptVariableBaselines(collectionUid));
await dispatch(
initRunRequestEvent({
requestUid,
@@ -2011,12 +2020,7 @@ export const copyEnvironment = (name, baseEnvUid, collectionUid) => (dispatch, g
const { ipcRenderer } = window;
// strip "ephemeral" metadata
const variablesToCopy = (baseEnv.variables || [])
.filter((v) => !v.ephemeral)
.map(({ ephemeral, ...rest }) => {
return rest;
});
const variablesToCopy = baseEnv.variables || [];
ipcRenderer
.invoke('renderer:create-environment', collection.pathname, sanitizedName, variablesToCopy)
@@ -2100,8 +2104,7 @@ export const saveEnvironment = (variables, environmentUid, collectionUid) => (di
return reject(new Error('Environment not found'));
}
const persisted = buildPersistedEnvVariables(variables, { mode: 'save' });
environment.variables = persisted;
environment.variables = variables;
const { ipcRenderer } = window;
const envForValidation = cloneDeep(environment);
@@ -2110,7 +2113,7 @@ export const saveEnvironment = (variables, environmentUid, collectionUid) => (di
.validate(environment)
.then(() => ipcRenderer.invoke('renderer:save-environment', collection.pathname, envForValidation))
.then(() => {
dispatch(_saveEnvironment({ variables: persisted, environmentUid, collectionUid }));
dispatch(_saveEnvironment({ variables, environmentUid, collectionUid }));
})
.then(resolve)
.catch(reject);
@@ -2247,9 +2250,7 @@ export const updateVariableInScope = (variableName, newValue, scopeInfo, collect
const updatedVariables = environment.variables.map((v) => {
if (v.uid === variable.uid) {
// Clear ephemeral metadata when user manually edits the value
const { ephemeral, persistedValue, ...rest } = v;
return { ...rest, value: newValue };
return { ...v, value: newValue };
}
return v;
});
@@ -2363,9 +2364,7 @@ export const updateVariableInScope = (variableName, newValue, scopeInfo, collect
const updatedVariables = environment.variables.map((v) => {
if (v.uid === variable.uid) {
// Clear ephemeral metadata when user manually edits the value
const { ephemeral, persistedValue, ...rest } = v;
return { ...rest, value: newValue };
return { ...v, value: newValue };
}
return v;
});
@@ -2404,81 +2403,100 @@ export const updateVariableInScope = (variableName, newValue, scopeInfo, collect
});
};
export const mergeAndPersistEnvironment
= ({ persistentEnvVariables, collectionUid }) =>
(_dispatch, getState) => {
return new Promise((resolve, reject) => {
const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid);
// Clears all three script-driven baselines for a given collection:
// collection-scope env baseline, collection-scope coll-vars baseline, and the
// workspace-scope global-env baseline. Call at the start of any request kickoff
// path so a stale baseline from a previous send can't leak into draft merging.
export const clearScriptVariableBaselines = (collectionUid) => (dispatch) => {
dispatch(_clearScriptCollectionBaselines({ collectionUid }));
dispatch(_clearScriptGlobalEnvBaseline());
};
if (!collection) {
return reject(new Error('Collection not found'));
}
export const persistActiveEnvironment = (collectionUid, requestUid) => (dispatch, getState) => {
const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid);
if (!collection) return;
const environmentUid = collection.activeEnvironmentUid;
if (!environmentUid) {
return reject(new Error('No active environment found'));
}
// Ignore stale updates from superseded requests so an in-flight pre/post
// from request N-1 can't trigger a disk write for request N.
if (requestUid && collection._scriptRequestUid && requestUid !== collection._scriptRequestUid) return;
const collectionCopy = cloneDeep(collection);
const environment = findEnvironmentInCollection(collectionCopy, environmentUid);
if (!environment) {
return reject(new Error('Environment not found'));
}
const environment = findEnvironmentInCollection(collection, collection.activeEnvironmentUid);
if (!environment) return;
// Only proceed if there are persistent variables to save
if (!persistentEnvVariables || Object.keys(persistentEnvVariables).length === 0) {
return resolve();
}
if (collection._scriptEnvBaseline) {
// Baseline exists — a draft was flushed earlier in this request cycle.
// Write to disk silently (without dispatching _saveEnvironment) to avoid
// racing with file-watcher callbacks.
const envCopy = { ...environment };
const { ipcRenderer } = window;
environmentSchema
.validate(envCopy)
.then(() => ipcRenderer.invoke('renderer:save-environment', collection.pathname, envCopy))
.catch((err) => console.error('Failed to persist environment during script execution:', err));
return;
}
let existingVars = environment.variables || [];
dispatch(saveEnvironment(environment.variables, environment.uid, collectionUid))
.catch((err) => console.error('Failed to persist environment during script execution:', err));
};
let normalizedNewVars = Object.entries(persistentEnvVariables).map(([name, value]) => {
const inferred = getDataTypeFromValue(value);
return {
uid: uuid(),
name,
value,
type: 'text',
enabled: true,
secret: false,
...(inferred !== 'string' ? { dataType: inferred } : {})
};
});
export const collectionVariablesUpdateEvent = ({ collectionVariables, collectionUid, requestUid }) => (dispatch, getState) => {
if (!collectionVariables || !collectionUid) return;
const merged = existingVars.map((v) => {
const found = normalizedNewVars.find((nv) => nv.name === v.name);
if (!found) return v;
const { dataType: _oldDataType, ...rest } = v;
return { ...rest, value: found.value, ...(found.dataType ? { dataType: found.dataType } : {}) };
});
normalizedNewVars.forEach((nv) => {
if (!merged.some((v) => v.name === nv.name)) {
merged.push(nv);
}
});
const state = getState();
const collection = findCollectionByUid(state.collections.collections, collectionUid);
if (!collection) return;
// Save all non-ephemeral vars and all variables that were previously persisted
const persistedNames = new Set(Object.keys(persistentEnvVariables));
// Ignore stale updates from superseded requests.
if (requestUid && collection._scriptRequestUid && requestUid !== collection._scriptRequestUid) {
return;
}
// Add all existing non-ephemeral variables to persistedNames so they are preserved
existingVars.forEach((v) => {
if (!v.ephemeral) {
persistedNames.add(v.name);
}
});
const savedVars = get(collection, 'root.request.vars.req', []);
const draftVars = collection.draft?.root
? get(collection, 'draft.root.request.vars.req', null)
: null;
const environmentToSave = cloneDeep(environment);
environmentToSave.variables = buildPersistedEnvVariables(merged, { mode: 'merge', persistedNames });
let baseline = collection._scriptCollVarBaseline || null;
const { ipcRenderer } = window;
environmentSchema
.validate(environmentToSave)
.then(() => ipcRenderer.invoke('renderer:save-environment', collection.pathname, environmentToSave))
.then(resolve)
.catch(reject);
});
};
if (!baseline && draftVars) {
baseline = {};
savedVars.forEach((v) => {
if (v.enabled) baseline[v.name] = v.value;
});
dispatch(setScriptCollVarBaseline({ collectionUid, baseline }));
}
let vars = cloneDeep(draftVars || savedVars);
vars = applyScriptEnvVars(vars, collectionVariables, baseline);
// Re-infer dataType only for vars the script actually modified; baseline-mode no-op writes
// must NOT overwrite a user's in-progress draft type change.
const modifiedKeys = getScriptModifiedKeys(collectionVariables, baseline);
modifiedKeys.forEach((name) => {
const existing = vars.find((v) => v.name === name);
if (!existing) return;
const inferred = getDataTypeFromValue(collectionVariables[name]);
if (inferred === 'string') {
delete existing.dataType;
} else {
existing.dataType = inferred;
}
});
dispatch(scriptUpdateCollectionVars({ collectionUid, vars }));
// Save from root (not draft) so draft headers/auth/scripts are not persisted to disk.
const fresh = findCollectionByUid(getState().collections.collections, collectionUid);
if (fresh) {
const collectionRootToSave = transformCollectionRootToSave({ root: fresh.root });
const { ipcRenderer } = window;
ipcRenderer.invoke('renderer:save-collection-root', fresh.pathname, collectionRootToSave, fresh.brunoConfig)
.catch((err) => console.error('Failed to persist collection variables:', err));
}
};
export const selectEnvironment = (environmentUid, collectionUid) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
@@ -2506,6 +2524,8 @@ export const selectEnvironment = (environmentUid, collectionUid) => (dispatch, g
}
});
dispatch(clearScriptVariableBaselines(collectionUid));
dispatch(_selectEnvironment({ environmentUid, collectionUid }));
resolve();
});

View File

@@ -0,0 +1,465 @@
jest.mock('nanoid', () => ({
customAlphabet: () => () => 'mock-uid'
}));
jest.mock('@usebruno/schema', () => ({
collectionSchema: { validate: () => Promise.resolve() },
environmentSchema: { validate: () => Promise.resolve() },
itemSchema: { validate: () => Promise.resolve() }
}));
import { configureStore } from '@reduxjs/toolkit';
import collectionsReducer from 'providers/ReduxStore/slices/collections';
import { collectionVariablesUpdateEvent } from 'providers/ReduxStore/slices/collections/actions';
const COLLECTION_UID = 'col-1';
const makeVar = (name, value, enabled = true) => ({
uid: `uid-${name}`,
name,
value,
enabled
});
const makeHeader = (name, value, enabled = true) => ({
uid: `hdr-${name}`,
name,
value,
description: '',
enabled
});
let invokedSaveArgs = [];
beforeEach(() => {
invokedSaveArgs = [];
window.ipcRenderer = {
invoke: jest.fn((...args) => {
invokedSaveArgs.push(args);
return Promise.resolve(true);
})
};
});
const createStore = ({ rootVars = [], rootHeaders = [], rootAuth, draft } = {}) => {
const root = {
request: {
headers: rootHeaders,
auth: rootAuth || { mode: 'none' },
script: { req: '', res: '' },
vars: {
req: rootVars,
res: []
},
tests: ''
}
};
const preloadedState = {
collections: {
collections: [
{
uid: COLLECTION_UID,
pathname: '/coll',
root,
draft: draft !== undefined ? draft : null,
brunoConfig: { version: '1', name: 'test', type: 'collection' },
environments: [],
items: []
}
],
collectionSortOrder: 'default',
activeWorkspaceUid: null
}
};
return configureStore({
reducer: {
collections: collectionsReducer
},
preloadedState
});
};
const getCollection = (store) => store.getState().collections.collections[0];
const getRootVars = (store) => getCollection(store).root?.request?.vars?.req || [];
const getRootHeaders = (store) => getCollection(store).root?.request?.headers || [];
const getRootAuth = (store) => getCollection(store).root?.request?.auth;
const getDraftHeaders = (store) => getCollection(store).draft?.root?.request?.headers;
const getDraft = (store) => getCollection(store).draft;
const getSavedRootPayload = () => {
const call = invokedSaveArgs.find(([channel]) => channel === 'renderer:save-collection-root');
// [channel, pathname, collectionRootToSave, brunoConfig]
return call ? call[2] : null;
};
describe('collectionVariablesUpdateEvent — draft isolation', () => {
describe('does not persist draft headers to disk', () => {
test('draft headers are NOT written to disk when script updates collection vars', () => {
const rootVars = [makeVar('HOST', 'https://saved.com')];
const rootHeaders = [makeHeader('X-Saved', 'original')];
const draftRoot = {
request: {
headers: [makeHeader('X-Saved', 'original'), makeHeader('X-Draft-Only', 'draft-value')],
auth: { mode: 'bearer', bearer: { token: 'draft-token' } },
script: { req: 'console.log("draft")', res: '' },
vars: {
req: [makeVar('HOST', 'https://saved.com')],
res: []
},
tests: ''
}
};
const store = createStore({
rootVars,
rootHeaders,
draft: { root: draftRoot }
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://updated.com' },
collectionUid: COLLECTION_UID
}));
const savedRoot = getSavedRootPayload();
expect(savedRoot).not.toBeNull();
// Saved headers should be from root, not draft
expect(savedRoot.request.headers).toHaveLength(1);
expect(savedRoot.request.headers[0].name).toBe('X-Saved');
expect(savedRoot.request.headers[0].value).toBe('original');
// Draft header should NOT be persisted
expect(savedRoot.request.headers.find((h) => h.name === 'X-Draft-Only')).toBeUndefined();
});
test('draft auth is NOT written to disk when script updates collection vars', () => {
const rootAuth = { mode: 'none' };
const draftRoot = {
request: {
headers: [],
auth: { mode: 'bearer', bearer: { token: 'draft-secret-token' } },
script: { req: '', res: '' },
vars: { req: [makeVar('HOST', 'https://saved.com')], res: [] },
tests: ''
}
};
const store = createStore({
rootVars: [makeVar('HOST', 'https://saved.com')],
rootAuth,
draft: { root: draftRoot }
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://updated.com' },
collectionUid: COLLECTION_UID
}));
const savedRoot = getSavedRootPayload();
expect(savedRoot).not.toBeNull();
// Auth on disk should be from root (none), not draft (bearer)
expect(savedRoot.request.auth).toEqual({ mode: 'none' });
// Root auth in Redux should also remain unchanged
expect(getRootAuth(store)).toEqual({ mode: 'none' });
});
test('draft scripts are NOT written to disk when script updates collection vars', () => {
const draftRoot = {
request: {
headers: [],
auth: { mode: 'none' },
script: { req: 'console.log("draft script")', res: 'console.log("draft post")' },
vars: { req: [], res: [] },
tests: 'test("draft test", () => {})'
}
};
const store = createStore({
draft: { root: draftRoot }
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { NEW_VAR: 'value' },
collectionUid: COLLECTION_UID
}));
const savedRoot = getSavedRootPayload();
expect(savedRoot).not.toBeNull();
// Scripts and tests on disk should be from root (empty), not draft
expect(savedRoot.request.script).toEqual({ req: '', res: '' });
expect(savedRoot.request.tests).toBe('');
});
});
describe('vars are saved correctly from root', () => {
test('updated vars are persisted to disk from saved state', () => {
const store = createStore({
rootVars: [makeVar('HOST', 'https://saved.com')]
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://updated.com' },
collectionUid: COLLECTION_UID
}));
const savedRoot = getSavedRootPayload();
expect(savedRoot).not.toBeNull();
expect(savedRoot.request.vars.req).toHaveLength(1);
expect(savedRoot.request.vars.req[0].value).toBe('https://updated.com');
});
test('new vars from script are persisted to disk', () => {
const store = createStore({ rootVars: [] });
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { TOKEN: 'abc123' },
collectionUid: COLLECTION_UID
}));
const savedRoot = getSavedRootPayload();
const vars = savedRoot.request.vars.req;
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('TOKEN');
expect(vars[0].value).toBe('abc123');
});
});
describe('draft is preserved after script-driven save', () => {
test('draft with header edits is NOT cleared by collectionVariablesUpdateEvent', () => {
const draftRoot = {
request: {
headers: [makeHeader('X-Draft', 'draft-val')],
auth: { mode: 'none' },
script: { req: '', res: '' },
vars: { req: [makeVar('HOST', 'https://saved.com')], res: [] },
tests: ''
}
};
const store = createStore({
rootVars: [makeVar('HOST', 'https://saved.com')],
rootHeaders: [],
draft: { root: draftRoot }
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://updated.com' },
collectionUid: COLLECTION_UID
}));
// Draft should still exist (not cleared)
const draft = getDraft(store);
expect(draft).not.toBeNull();
// Draft headers should be untouched
const draftHeaders = getDraftHeaders(store);
expect(draftHeaders).toHaveLength(1);
expect(draftHeaders[0].name).toBe('X-Draft');
});
test('draft vars are synced with script changes while draft remains', () => {
const draftRoot = {
request: {
headers: [makeHeader('X-Draft', 'keep-me')],
auth: { mode: 'none' },
script: { req: '', res: '' },
vars: { req: [makeVar('HOST', 'https://draft.com')], res: [] },
tests: ''
}
};
const store = createStore({
rootVars: [makeVar('HOST', 'https://saved.com')],
draft: { root: draftRoot }
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://script.com', NEW: 'val' },
collectionUid: COLLECTION_UID
}));
// Root should have the updated vars
const rootVars = getRootVars(store);
expect(rootVars.find((v) => v.name === 'HOST').value).toBe('https://script.com');
expect(rootVars.find((v) => v.name === 'NEW').value).toBe('val');
// Draft vars should also be synced
const col = getCollection(store);
const draftVars = col.draft.root.request.vars.req;
expect(draftVars.find((v) => v.name === 'HOST').value).toBe('https://script.com');
expect(draftVars.find((v) => v.name === 'NEW').value).toBe('val');
// Draft headers should be untouched
expect(col.draft.root.request.headers[0].name).toBe('X-Draft');
});
});
describe('disabled vars are preserved', () => {
test('disabled collection vars survive when script returns only enabled vars', () => {
const store = createStore({
rootVars: [
makeVar('HOST', 'https://example.com'),
makeVar('DEBUG', 'true', false) // disabled
]
});
// Script only returns enabled vars (disabled are not passed to script runtime)
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com' },
collectionUid: COLLECTION_UID
}));
const vars = getRootVars(store);
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'DEBUG')).toBeDefined();
expect(vars.find((v) => v.name === 'DEBUG').enabled).toBe(false);
});
test('disabled vars survive when script adds a new var', () => {
const store = createStore({
rootVars: [
makeVar('HOST', 'https://example.com'),
makeVar('OLD_SECRET', 'hidden', false) // disabled
]
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com', TOKEN: 'new-token' },
collectionUid: COLLECTION_UID
}));
const vars = getRootVars(store);
expect(vars).toHaveLength(3);
expect(vars.find((v) => v.name === 'OLD_SECRET')).toBeDefined();
expect(vars.find((v) => v.name === 'OLD_SECRET').enabled).toBe(false);
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('new-token');
});
test('disabled vars survive when script deletes an enabled var', () => {
const store = createStore({
rootVars: [
makeVar('HOST', 'https://example.com'),
makeVar('REMOVE_ME', 'bye'),
makeVar('KEEP_DISABLED', 'secret', false) // disabled
]
});
// Script deleted REMOVE_ME (not in output)
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com' },
collectionUid: COLLECTION_UID
}));
const vars = getRootVars(store);
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'HOST')).toBeDefined();
expect(vars.find((v) => v.name === 'KEEP_DISABLED')).toBeDefined();
expect(vars.find((v) => v.name === 'KEEP_DISABLED').enabled).toBe(false);
expect(vars.find((v) => v.name === 'REMOVE_ME')).toBeUndefined();
});
test('disabled vars are persisted to disk', () => {
const store = createStore({
rootVars: [
makeVar('HOST', 'https://example.com'),
makeVar('DISABLED_VAR', 'keep-me', false) // disabled
]
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://updated.com' },
collectionUid: COLLECTION_UID
}));
const savedRoot = getSavedRootPayload();
expect(savedRoot).not.toBeNull();
const savedVars = savedRoot.request.vars.req;
expect(savedVars).toHaveLength(2);
expect(savedVars.find((v) => v.name === 'DISABLED_VAR')).toBeDefined();
expect(savedVars.find((v) => v.name === 'DISABLED_VAR').enabled).toBe(false);
});
test('preserves var that user disabled in draft even when script does not return it', () => {
const draftRoot = {
request: {
headers: [],
auth: { mode: 'none' },
script: { req: '', res: '' },
vars: {
req: [
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret', false) // disabled in draft only
],
res: []
},
tests: ''
}
};
const store = createStore({
rootVars: [
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret') // still enabled in root
],
draft: { root: draftRoot }
});
// Script only returns enabled vars — TOKEN is disabled in draft, so not included
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com' },
collectionUid: COLLECTION_UID
}));
const vars = getRootVars(store);
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'TOKEN')).toBeDefined();
// The var should be preserved with its draft disabled state
expect(vars.find((v) => v.name === 'TOKEN').enabled).toBe(false);
});
test('disabled vars survive when collectionVariables is empty (no collection vars defined)', () => {
const store = createStore({
rootVars: [
makeVar('ONLY_DISABLED', 'value', false) // disabled
]
});
// Empty collectionVariables — happens when no enabled vars exist
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: {},
collectionUid: COLLECTION_UID
}));
const vars = getRootVars(store);
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('ONLY_DISABLED');
expect(vars[0].enabled).toBe(false);
});
});
describe('no draft — root headers remain unchanged', () => {
test('existing root headers are preserved when vars are updated', () => {
const store = createStore({
rootVars: [makeVar('HOST', 'https://saved.com')],
rootHeaders: [makeHeader('Authorization', 'Bearer saved-token')]
});
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://updated.com' },
collectionUid: COLLECTION_UID
}));
// Root headers should not change
const headers = getRootHeaders(store);
expect(headers).toHaveLength(1);
expect(headers[0].value).toBe('Bearer saved-token');
// No draft should be created
expect(getDraft(store)).toBeNull();
});
});
});

View File

@@ -0,0 +1,638 @@
jest.mock('nanoid', () => ({
customAlphabet: () => () => 'mock-uid'
}));
jest.mock('@usebruno/schema', () => ({
collectionSchema: { validate: () => Promise.resolve() },
environmentSchema: { validate: () => Promise.resolve() },
itemSchema: { validate: () => Promise.resolve() }
}));
import { configureStore } from '@reduxjs/toolkit';
import collectionsReducer, { initRunRequestEvent } from 'providers/ReduxStore/slices/collections';
import { collectionVariablesUpdateEvent } from 'providers/ReduxStore/slices/collections/actions';
const COLLECTION_UID = 'col-1';
const ITEM_UID = 'req-1';
const REQUEST_UID = 'run-1';
const makeVar = (name, value, enabled = true) => ({
uid: `uid-${name}`,
name,
value,
enabled
});
beforeAll(() => {
window.ipcRenderer = { invoke: jest.fn().mockResolvedValue(true) };
});
const createStore = (rootVars = [], opts = {}) => {
const root = {
request: {
vars: {
req: rootVars
}
}
};
const preloadedState = {
collections: {
collections: [
{
uid: COLLECTION_UID,
pathname: '/coll',
root,
draft: opts.draft !== undefined ? opts.draft : null,
brunoConfig: { version: '1', name: 'test', type: 'collection' },
environments: [],
items: [
{
uid: ITEM_UID,
name: 'ping',
type: 'http-request',
request: { url: 'https://example.com/ping', method: 'GET' }
}
]
}
],
collectionSortOrder: 'default',
activeWorkspaceUid: null
}
};
return configureStore({
reducer: {
collections: collectionsReducer
},
preloadedState
});
};
const getReqVars = (store) => {
const col = store.getState().collections.collections[0];
// scriptUpdateCollectionVars writes to root and syncs to draft if present
const root = col.root || {};
return root.request?.vars?.req || [];
};
const getCollection = (store) => store.getState().collections.collections[0];
describe('collectionVariablesUpdateEvent — draft-aware merge', () => {
describe('no draft pending', () => {
test('updates existing variable values', () => {
const store = createStore([makeVar('HOST', 'https://old.com')]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://new.com' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars).toHaveLength(1);
expect(vars[0].value).toBe('https://new.com');
});
test('adds new variables from script', () => {
const store = createStore([makeVar('HOST', 'https://example.com')]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com', TOKEN: 'abc' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('abc');
});
test('removes enabled variables deleted by script (deleteCollectionVar)', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('OLD_VAR', 'remove-me')
]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('HOST');
});
test('preserves disabled variables even if not in script output', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('DISABLED_VAR', 'keep', false)
]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'DISABLED_VAR')).toBeDefined();
});
test('deleteAllCollectionVars removes all enabled variables', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret'),
makeVar('DISABLED', 'keep', false)
]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: {},
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('DISABLED');
});
test('preserves typed values without stringifying', () => {
const store = createStore([]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { count: 42, flag: true },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
const countVar = vars.find((v) => v.name === 'count');
const flagVar = vars.find((v) => v.name === 'flag');
expect(countVar.value).toBe(42);
expect(countVar.dataType).toBe('number');
expect(flagVar.value).toBe(true);
expect(flagVar.dataType).toBe('boolean');
});
});
describe('draft pending — baseline-diff mode', () => {
test('preserves draft-only variables that script does not know about', () => {
const rootVars = [makeVar('HOST', 'https://saved.com')];
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://saved.com'), makeVar('DRAFT_NEW', 'from-user')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
// DRAFT_NEW was not in baseline — script can't delete it, so it survives
expect(vars.find((v) => v.name === 'DRAFT_NEW')).toBeDefined();
expect(vars.find((v) => v.name === 'DRAFT_NEW').value).toBe('from-user');
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://saved.com');
});
test('applies script modification over draft edit of same variable', () => {
const rootVars = [makeVar('TOKEN', 'saved-token')];
const draftRoot = {
request: {
vars: {
req: [makeVar('TOKEN', 'draft-token')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script changed TOKEN (different from saved)
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { TOKEN: 'script-token' },
collectionUid: COLLECTION_UID
}));
// Script's change wins because it explicitly modified the value
expect(getReqVars(store)[0].value).toBe('script-token');
});
test('preserves draft value when script returns unchanged value', () => {
const rootVars = [makeVar('HOST', 'https://saved.com')];
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://draft-edit.com')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script didn't change HOST, but added TOKEN
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com', TOKEN: 'new-token' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
// HOST: script didn't change it → draft value preserved
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft-edit.com');
// TOKEN: new from script → added
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('new-token');
// Baseline should be set
expect(getCollection(store)._scriptCollVarBaseline).toEqual({ HOST: 'https://saved.com' });
});
test('user deletes variable in draft, script returns same value — stays deleted', () => {
const rootVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('API_KEY', 'saved-key')
];
// User deleted API_KEY in the draft
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://saved.com')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script returns API_KEY unchanged
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com', API_KEY: 'saved-key' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
// API_KEY was not modified by script (same as baseline), so user's delete wins
expect(vars.find((v) => v.name === 'API_KEY')).toBeUndefined();
expect(vars).toHaveLength(1);
});
test('user deletes variable in draft, script sets new value — variable re-added', () => {
const rootVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('API_KEY', 'saved-key')
];
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://saved.com')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script explicitly changed API_KEY
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com', API_KEY: 'new-key-from-script' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars.find((v) => v.name === 'API_KEY').value).toBe('new-key-from-script');
});
test('deleteCollectionVar removes variable that existed in saved state', () => {
const rootVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token')
];
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://draft.com'), makeVar('TOKEN', 'draft-token')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script deleted TOKEN (absent from output)
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
// TOKEN was in baseline and removed by script — should be gone
expect(vars.find((v) => v.name === 'TOKEN')).toBeUndefined();
// HOST draft value preserved (script didn't change it)
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
});
test('deleteAllCollectionVars with draft preserves draft-only vars', () => {
const rootVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token')
];
const draftRoot = {
request: {
vars: {
req: [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token'),
makeVar('DRAFT_ONLY', 'user-added')
]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script called bru.deleteAllCollectionVars() — empty output
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: {},
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
// HOST and TOKEN were in baseline and deleted by script — gone
expect(vars.find((v) => v.name === 'HOST')).toBeUndefined();
expect(vars.find((v) => v.name === 'TOKEN')).toBeUndefined();
// DRAFT_ONLY was not in baseline — survives
expect(vars.find((v) => v.name === 'DRAFT_ONLY').value).toBe('user-added');
});
});
describe('baseline exists — subsequent script events', () => {
test('subsequent script events reuse baseline and preserve draft edits', () => {
const rootVars = [makeVar('HOST', 'https://saved.com')];
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://draft.com')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// First event: creates baseline
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com', TOKEN: 'abc' },
collectionUid: COLLECTION_UID
}));
expect(getReqVars(store).find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(getReqVars(store).find((v) => v.name === 'TOKEN').value).toBe('abc');
// Second event: baseline still active, draft edits still preserved
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com', TOKEN: 'abc', RESULT: 'ok' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('abc');
expect(vars.find((v) => v.name === 'RESULT').value).toBe('ok');
});
});
describe('mixed operations — set + delete in same script', () => {
test('script adds new var and deletes existing var', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('OLD_TOKEN', 'remove-me')
]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://example.com', NEW_TOKEN: 'fresh' },
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
expect(vars.find((v) => v.name === 'OLD_TOKEN')).toBeUndefined();
expect(vars.find((v) => v.name === 'NEW_TOKEN').value).toBe('fresh');
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://example.com');
});
test('with draft: script adds, modifies, and deletes vars — draft edits preserved', () => {
const rootVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token'),
makeVar('TO_DELETE', 'remove-me')
];
const draftRoot = {
request: {
vars: {
req: [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token'),
makeVar('TO_DELETE', 'draft-value'),
makeVar('DRAFT_NEW', 'from-user')
]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Script: modified TOKEN, deleted TO_DELETE, added SCRIPT_NEW, left HOST unchanged
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: {
HOST: 'https://saved.com',
TOKEN: 'script-token',
SCRIPT_NEW: 'from-script'
},
collectionUid: COLLECTION_UID
}));
const vars = getReqVars(store);
// HOST: unchanged by script → draft value preserved
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
// TOKEN: modified by script → script wins
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('script-token');
// TO_DELETE: was in baseline, absent from script → deleted
expect(vars.find((v) => v.name === 'TO_DELETE')).toBeUndefined();
// DRAFT_NEW: not in baseline → preserved
expect(vars.find((v) => v.name === 'DRAFT_NEW').value).toBe('from-user');
// SCRIPT_NEW: new from script → added
expect(vars.find((v) => v.name === 'SCRIPT_NEW').value).toBe('from-script');
});
});
describe('typed values — dataType inference', () => {
test('infers number dataType when script sets a numeric value', () => {
const store = createStore([makeVar('COUNT', '0')]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { COUNT: 42 },
collectionUid: COLLECTION_UID
}));
const v = getReqVars(store).find((v) => v.name === 'COUNT');
expect(v.dataType).toBe('number');
});
test('infers boolean dataType when script sets a boolean value', () => {
const store = createStore([makeVar('FLAG', 'false')]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { FLAG: true },
collectionUid: COLLECTION_UID
}));
const v = getReqVars(store).find((v) => v.name === 'FLAG');
expect(v.dataType).toBe('boolean');
});
test('infers object dataType when script sets an object value', () => {
const store = createStore([makeVar('CONFIG', '')]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { CONFIG: { port: 3000 } },
collectionUid: COLLECTION_UID
}));
const v = getReqVars(store).find((v) => v.name === 'CONFIG');
expect(v.dataType).toBe('object');
});
test('keeps existing dataType on a typed var the script did not touch', () => {
const typedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
const store = createStore([typedVar, makeVar('HOST', 'https://example.com')]);
// Script touched HOST only; the runtime payload still carries COUNT (unchanged).
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { COUNT: 42, HOST: 'https://new.com' },
collectionUid: COLLECTION_UID
}));
const v = getReqVars(store).find((v) => v.name === 'COUNT');
expect(v.dataType).toBe('number');
});
test('drops dataType when script replaces a typed value with a string', () => {
const typedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
const store = createStore([typedVar]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { COUNT: 'not-a-number' },
collectionUid: COLLECTION_UID
}));
const v = getReqVars(store).find((v) => v.name === 'COUNT');
expect(v.dataType).toBeUndefined();
});
test('updates dataType when script changes the value type', () => {
const typedVar = { ...makeVar('VAL', 42), dataType: 'number' };
const store = createStore([typedVar]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { VAL: true },
collectionUid: COLLECTION_UID
}));
const v = getReqVars(store).find((v) => v.name === 'VAL');
expect(v.dataType).toBe('boolean');
});
});
describe('baseline cleanup', () => {
test('initRunRequestEvent clears collection var baseline', () => {
const rootVars = [makeVar('HOST', 'https://saved.com')];
const draftRoot = {
request: {
vars: {
req: [makeVar('HOST', 'https://draft.com')]
}
}
};
const store = createStore(rootVars, { draft: { root: draftRoot } });
// Create baseline via script event
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { HOST: 'https://saved.com' },
collectionUid: COLLECTION_UID
}));
expect(getCollection(store)._scriptCollVarBaseline).toBeDefined();
// Start a new request — baseline should be cleared
store.dispatch(initRunRequestEvent({
requestUid: REQUEST_UID,
itemUid: ITEM_UID,
collectionUid: COLLECTION_UID
}));
expect(getCollection(store)._scriptCollVarBaseline).toBeUndefined();
});
});
describe('description / annotations preservation across script-driven updates', () => {
const annotations = [
{ name: 'number' },
{ name: 'description', value: 'server port' }
];
test('script touching a sibling var must NOT erase annotations on untouched vars', () => {
const annotated = { ...makeVar('PORT', 8080), dataType: 'number', annotations: annotations };
const sibling = makeVar('TOKEN', 'abc');
const store = createStore([annotated, sibling]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { PORT: 8080, TOKEN: 'new-token' },
collectionUid: COLLECTION_UID
}));
const port = getReqVars(store).find((v) => v.name === 'PORT');
expect(port.annotations).toEqual(annotations);
expect(port.dataType).toBe('number');
});
test('script overwriting a var\'s value preserves its annotations', () => {
const annotated = { ...makeVar('PORT', 8080), dataType: 'number', annotations: annotations };
const store = createStore([annotated]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { PORT: 9090 },
collectionUid: COLLECTION_UID
}));
const port = getReqVars(store).find((v) => v.name === 'PORT');
expect(port.value).toBe(9090);
expect(port.annotations).toEqual(annotations);
expect(port.dataType).toBe('number');
});
});
describe('object/array typed-var no-op writes preserve draft (deep-equal compare)', () => {
test('script re-writing a structurally-equal object value does NOT clobber draft edit', () => {
const savedVar = { ...makeVar('CFG', { port: 3000 }), dataType: 'object' };
const draftVar = { ...makeVar('CFG', { port: 4000 }), dataType: 'object' };
const draftRoot = { request: { vars: { req: [draftVar] } } };
const store = createStore([savedVar], { draft: { root: draftRoot } });
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { CFG: { port: 3000 } },
collectionUid: COLLECTION_UID
}));
const cfg = getReqVars(store).find((v) => v.name === 'CFG');
expect(cfg.value).toEqual({ port: 4000 });
});
});
describe('disabled-var name collision — script targets enabled slot only', () => {
test('script setting X writes to the enabled X, leaves the disabled X untouched', () => {
const disabledX = makeVar('X', 'archived', false);
const enabledX = makeVar('X', 'current');
const store = createStore([disabledX, enabledX]);
store.dispatch(collectionVariablesUpdateEvent({
collectionVariables: { X: 'updated' },
collectionUid: COLLECTION_UID
}));
const xVars = getReqVars(store).filter((v) => v.name === 'X');
expect(xVars).toHaveLength(2);
expect(xVars.find((v) => v.enabled === false).value).toBe('archived');
expect(xVars.find((v) => v.enabled === true).value).toBe('updated');
});
});
});

View File

@@ -0,0 +1,627 @@
jest.mock('nanoid', () => ({
customAlphabet: () => () => 'mock-uid'
}));
import reducer, {
scriptEnvironmentUpdateEvent,
initRunRequestEvent
} from 'providers/ReduxStore/slices/collections';
const COLLECTION_UID = 'col-1';
const ENV_UID = 'env-1';
const ITEM_UID = 'req-1';
const REQUEST_UID = 'run-1';
const makeVar = (name, value, enabled = true) => ({
uid: `uid-${name}`,
name,
value,
type: 'text',
secret: false,
enabled
});
const makeInitialState = (envVars = [], opts = {}) => ({
collections: [
{
uid: COLLECTION_UID,
pathname: '/coll',
activeEnvironmentUid: ENV_UID,
environments: [
{
uid: ENV_UID,
name: 'Test',
variables: envVars
}
],
environmentsDraft: opts.draft || null,
_scriptEnvBaseline: opts.baseline || undefined,
runtimeVariables: {},
items: [
{
uid: ITEM_UID,
name: 'ping',
type: 'http-request',
request: { url: 'https://example.com/ping', method: 'GET' }
}
]
}
],
collectionSortOrder: 'default',
activeWorkspaceUid: null
});
const scriptEvent = (envVariables) =>
scriptEnvironmentUpdateEvent({
collectionUid: COLLECTION_UID,
envVariables
});
const getEnv = (state) => state.collections[0].environments[0];
const getCollection = (state) => state.collections[0];
describe('scriptEnvironmentUpdateEvent — draft-aware merge', () => {
describe('no draft pending (original behavior)', () => {
test('updates existing variable values', () => {
let state = makeInitialState([makeVar('HOST', 'https://old.com')]);
state = reducer(state, scriptEvent({ HOST: 'https://new.com', __name__: 'Test' }));
expect(getEnv(state).variables).toHaveLength(1);
expect(getEnv(state).variables[0].value).toBe('https://new.com');
});
test('adds new variables from script', () => {
let state = makeInitialState([makeVar('HOST', 'https://example.com')]);
state = reducer(state, scriptEvent({ HOST: 'https://example.com', TOKEN: 'abc', __name__: 'Test' }));
expect(getEnv(state).variables).toHaveLength(2);
expect(getEnv(state).variables[1].name).toBe('TOKEN');
expect(getEnv(state).variables[1].value).toBe('abc');
});
test('removes enabled variables deleted by script', () => {
let state = makeInitialState([
makeVar('HOST', 'https://example.com'),
makeVar('OLD_VAR', 'remove-me')
]);
state = reducer(state, scriptEvent({ HOST: 'https://example.com', __name__: 'Test' }));
expect(getEnv(state).variables).toHaveLength(1);
expect(getEnv(state).variables[0].name).toBe('HOST');
});
test('preserves disabled variables even if not in script output', () => {
let state = makeInitialState([
makeVar('HOST', 'https://example.com'),
makeVar('DISABLED_VAR', 'keep-me', false)
]);
state = reducer(state, scriptEvent({ HOST: 'https://example.com', __name__: 'Test' }));
expect(getEnv(state).variables).toHaveLength(2);
expect(getEnv(state).variables[1].name).toBe('DISABLED_VAR');
});
});
describe('draft pending — first script event', () => {
test('flushes draft, creates baseline, and applies only script changes', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [makeVar('HOST', 'https://draft-edit.com')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script didn't change HOST, but added TOKEN
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
TOKEN: 'new-token',
__name__: 'Test'
}));
const vars = getEnv(state).variables;
// HOST should keep the draft value (script didn't change it)
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft-edit.com');
// TOKEN should be added (new in script)
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('new-token');
// Draft should be cleared
expect(getCollection(state).environmentsDraft).toBeNull();
// Baseline should be set
expect(getCollection(state)._scriptEnvBaseline).toEqual({ HOST: 'https://saved.com' });
});
test('applies script modification over draft edit of same variable', () => {
const savedVars = [makeVar('TOKEN', 'saved-token')];
const draftVars = [makeVar('TOKEN', 'draft-token')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script changed TOKEN (different from saved)
state = reducer(state, scriptEvent({ TOKEN: 'script-token', __name__: 'Test' }));
// Script's change wins because it explicitly modified the value
expect(getEnv(state).variables[0].value).toBe('script-token');
});
test('preserves draft-only variables that script does not know about', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('NEW_DRAFT_VAR', 'draft-only-value')
];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
state = reducer(state, scriptEvent({ HOST: 'https://saved.com', __name__: 'Test' }));
const vars = getEnv(state).variables;
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'NEW_DRAFT_VAR').value).toBe('draft-only-value');
});
test('user deletes variable in draft, script returns same value — stays deleted', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('API_KEY', 'saved-key')
];
// User deleted API_KEY in the draft
const draftVars = [makeVar('HOST', 'https://saved.com')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script returns API_KEY unchanged
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
API_KEY: 'saved-key',
__name__: 'Test'
}));
const vars = getEnv(state).variables;
// API_KEY was not modified by script (same as baseline), so user's delete wins
expect(vars.find((v) => v.name === 'API_KEY')).toBeUndefined();
expect(vars).toHaveLength(1);
});
test('user deletes variable in draft, script sets new value — variable re-added', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('API_KEY', 'saved-key')
];
const draftVars = [makeVar('HOST', 'https://saved.com')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script explicitly changed API_KEY
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
API_KEY: 'new-key-from-script',
__name__: 'Test'
}));
const vars = getEnv(state).variables;
expect(vars.find((v) => v.name === 'API_KEY').value).toBe('new-key-from-script');
});
test('ignores draft for non-active environment', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: 'other-env-uid', variables: [makeVar('HOST', 'https://draft.com')] }
});
state = reducer(state, scriptEvent({ HOST: 'https://new.com', __name__: 'Test' }));
// Draft for a different env should NOT be flushed
expect(getCollection(state).environmentsDraft).not.toBeNull();
// Should apply script output directly (no baseline)
expect(getEnv(state).variables[0].value).toBe('https://new.com');
expect(getCollection(state)._scriptEnvBaseline).toBeUndefined();
});
});
describe('baseline exists — subsequent script events', () => {
test('preserves draft edits across multiple script events', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [makeVar('HOST', 'https://draft.com')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// First event: flushes draft, creates baseline
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
TOKEN: 'abc',
__name__: 'Test'
}));
expect(getEnv(state).variables.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(getEnv(state).variables.find((v) => v.name === 'TOKEN').value).toBe('abc');
// Second event: baseline exists, no draft — should still preserve draft edits
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
TOKEN: 'abc',
__name__: 'Test'
}));
// HOST should still be the draft value
expect(getEnv(state).variables.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(getEnv(state).variables.find((v) => v.name === 'TOKEN').value).toBe('abc');
});
test('applies new changes from later script events', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [makeVar('HOST', 'https://draft.com')];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// First event: pre-request sets TOKEN
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
TOKEN: 'abc',
__name__: 'Test'
}));
// Second event: post-response sets RESULT (new var)
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
TOKEN: 'abc',
RESULT: 'ok',
__name__: 'Test'
}));
const vars = getEnv(state).variables;
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('abc');
expect(vars.find((v) => v.name === 'RESULT').value).toBe('ok');
});
});
describe('deleteEnvVar — script deletes a variable', () => {
test('no draft: deleteEnvVar removes the variable', () => {
let state = makeInitialState([
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret')
]);
// Script called bru.deleteEnvVar("TOKEN") — TOKEN absent from output
state = reducer(state, scriptEvent({ HOST: 'https://example.com', __name__: 'Test' }));
expect(getEnv(state).variables).toHaveLength(1);
expect(getEnv(state).variables[0].name).toBe('HOST');
});
test('with draft: deleteEnvVar removes variable that existed in saved state', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token')
];
const draftVars = [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token')
];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script deleted TOKEN (absent from output)
state = reducer(state, scriptEvent({ HOST: 'https://saved.com', __name__: 'Test' }));
const vars = getEnv(state).variables;
// TOKEN was in baseline and removed by script — should be gone
expect(vars.find((v) => v.name === 'TOKEN')).toBeUndefined();
// HOST draft value preserved (script didn't change it)
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
});
test('with draft: deleteEnvVar does not remove draft-only variables', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('DRAFT_ONLY', 'user-added')
];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script output only has HOST (DRAFT_ONLY was never in saved state)
state = reducer(state, scriptEvent({ HOST: 'https://saved.com', __name__: 'Test' }));
const vars = getEnv(state).variables;
// DRAFT_ONLY should survive — it wasn't in baseline, so script can't "delete" it
expect(vars.find((v) => v.name === 'DRAFT_ONLY').value).toBe('user-added');
});
});
describe('deleteAllEnvVars — script clears all variables', () => {
test('no draft: deleteAllEnvVars removes all enabled variables', () => {
let state = makeInitialState([
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret'),
makeVar('DISABLED', 'keep', false)
]);
// Script called bru.deleteAllEnvVars() — only __name__ remains
state = reducer(state, scriptEvent({ __name__: 'Test' }));
const vars = getEnv(state).variables;
// Only disabled var survives
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('DISABLED');
});
test('with draft: deleteAllEnvVars removes saved vars but keeps draft-only vars', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token')
];
const draftVars = [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token'),
makeVar('DRAFT_ONLY', 'user-added')
];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script called bru.deleteAllEnvVars() — only __name__ in output
state = reducer(state, scriptEvent({ __name__: 'Test' }));
const vars = getEnv(state).variables;
// HOST and TOKEN were in baseline and deleted by script — gone
expect(vars.find((v) => v.name === 'HOST')).toBeUndefined();
expect(vars.find((v) => v.name === 'TOKEN')).toBeUndefined();
// DRAFT_ONLY was not in baseline — survives
expect(vars.find((v) => v.name === 'DRAFT_ONLY').value).toBe('user-added');
});
test('with draft: deleteAllEnvVars preserves disabled variables', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('DISABLED', 'keep', false)
];
const draftVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('DISABLED', 'keep', false)
];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
state = reducer(state, scriptEvent({ __name__: 'Test' }));
const vars = getEnv(state).variables;
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('DISABLED');
expect(vars[0].enabled).toBe(false);
});
});
describe('mixed operations — set + delete in same script', () => {
test('no draft: script adds new var and deletes existing var', () => {
let state = makeInitialState([
makeVar('HOST', 'https://example.com'),
makeVar('OLD_TOKEN', 'remove-me')
]);
// Script deleted OLD_TOKEN and added NEW_TOKEN
state = reducer(state, scriptEvent({
HOST: 'https://example.com',
NEW_TOKEN: 'fresh',
__name__: 'Test'
}));
const vars = getEnv(state).variables;
expect(vars.find((v) => v.name === 'OLD_TOKEN')).toBeUndefined();
expect(vars.find((v) => v.name === 'NEW_TOKEN').value).toBe('fresh');
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://example.com');
});
test('with draft: script adds, modifies, and deletes vars — draft edits preserved', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token'),
makeVar('TO_DELETE', 'remove-me')
];
const draftVars = [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token'),
makeVar('TO_DELETE', 'draft-value'),
makeVar('DRAFT_NEW', 'from-user')
];
let state = makeInitialState(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script: modified TOKEN, deleted TO_DELETE, added SCRIPT_NEW, left HOST unchanged
state = reducer(state, scriptEvent({
HOST: 'https://saved.com',
TOKEN: 'script-token',
SCRIPT_NEW: 'from-script',
__name__: 'Test'
}));
const vars = getEnv(state).variables;
// HOST: unchanged by script → draft value preserved
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
// TOKEN: modified by script → script wins
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('script-token');
// TO_DELETE: was in baseline, absent from script → deleted
expect(vars.find((v) => v.name === 'TO_DELETE')).toBeUndefined();
// DRAFT_NEW: not in baseline → preserved
expect(vars.find((v) => v.name === 'DRAFT_NEW').value).toBe('from-user');
// SCRIPT_NEW: new from script → added
expect(vars.find((v) => v.name === 'SCRIPT_NEW').value).toBe('from-script');
});
});
describe('baseline cleanup', () => {
test('initRunRequestEvent clears baseline from previous request', () => {
let state = makeInitialState([makeVar('HOST', 'https://saved.com')], {
baseline: { HOST: 'https://saved.com' }
});
state = reducer(state, initRunRequestEvent({
requestUid: REQUEST_UID,
itemUid: ITEM_UID,
collectionUid: COLLECTION_UID
}));
expect(getCollection(state)._scriptEnvBaseline).toBeUndefined();
});
});
describe('typed values — dataType inference', () => {
test('infers number dataType when script sets a numeric value', () => {
let state = makeInitialState([makeVar('COUNT', '0')]);
state = reducer(state, scriptEvent({ COUNT: 42, __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'COUNT');
expect(v.value).toBe(42);
expect(v.dataType).toBe('number');
});
test('infers boolean dataType when script sets a boolean value', () => {
let state = makeInitialState([makeVar('FLAG', 'false')]);
state = reducer(state, scriptEvent({ FLAG: true, __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'FLAG');
expect(v.value).toBe(true);
expect(v.dataType).toBe('boolean');
});
test('infers object dataType when script sets an object value', () => {
let state = makeInitialState([makeVar('CONFIG', '')]);
state = reducer(state, scriptEvent({ CONFIG: { port: 3000 }, __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'CONFIG');
expect(v.value).toEqual({ port: 3000 });
expect(v.dataType).toBe('object');
});
test('keeps existing dataType on a typed var the script did not touch', () => {
const typedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
let state = makeInitialState([
typedVar,
makeVar('HOST', 'https://example.com')
]);
// Script touched HOST only; the runtime payload still carries COUNT (unchanged).
state = reducer(state, scriptEvent({ COUNT: 42, HOST: 'https://new.com', __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'COUNT');
expect(v.dataType).toBe('number');
});
test('drops dataType when script replaces a typed value with a string', () => {
const typedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
let state = makeInitialState([typedVar]);
state = reducer(state, scriptEvent({ COUNT: 'not-a-number', __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'COUNT');
expect(v.dataType).toBeUndefined();
});
test('updates dataType when script changes the value type', () => {
const typedVar = { ...makeVar('VAL', 42), dataType: 'number' };
let state = makeInitialState([typedVar]);
state = reducer(state, scriptEvent({ VAL: true, __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'VAL');
expect(v.value).toBe(true);
expect(v.dataType).toBe('boolean');
});
});
describe('object/array typed-var no-op writes preserve draft (deep-equal compare)', () => {
test('script re-writing a structurally-equal object value does NOT clobber draft edit', () => {
const savedVar = { ...makeVar('CFG', { port: 3000 }), dataType: 'object' };
const draftVar = { ...makeVar('CFG', { port: 4000 }), dataType: 'object' };
let state = makeInitialState([savedVar], {
draft: { environmentUid: ENV_UID, variables: [draftVar] }
});
// Script reads CFG (={port:3000}), passes it back unchanged. Pre-fix the strict-!==
// compare in applyScriptEnvVars saw two different references and clobbered the draft.
state = reducer(state, scriptEvent({ CFG: { port: 3000 }, __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'CFG');
expect(v.value).toEqual({ port: 4000 });
});
test('script re-writing a structurally-equal array value does NOT clobber draft edit', () => {
const savedVar = { ...makeVar('TAGS', [1, 2]), dataType: 'array' };
const draftVar = { ...makeVar('TAGS', [1, 2, 3]), dataType: 'array' };
let state = makeInitialState([savedVar], {
draft: { environmentUid: ENV_UID, variables: [draftVar] }
});
state = reducer(state, scriptEvent({ TAGS: [1, 2], __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'TAGS');
expect(v.value).toEqual([1, 2, 3]);
});
test('dataType inference is NOT applied when the merge skipped the var (no-op write)', () => {
// User is mid-edit converting `42` (number) → `'forty-two'` (string) in their draft.
const savedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
const draftVar = { ...makeVar('COUNT', 'forty-two') };
let state = makeInitialState([savedVar], {
draft: { environmentUid: ENV_UID, variables: [draftVar] }
});
// Script does a no-op write of the saved value. With the modifiedKeys-scoped inference,
// we must NOT reset the dataType back to 'number' — the draft's string-typed edit is preserved.
state = reducer(state, scriptEvent({ COUNT: 42, __name__: 'Test' }));
const v = getEnv(state).variables.find((v) => v.name === 'COUNT');
expect(v.value).toBe('forty-two');
expect(v.dataType).toBeUndefined();
});
});
describe('disabled-var name collision — script targets enabled slot only', () => {
test('script setting X writes to the enabled X, leaves the disabled X untouched', () => {
const disabledX = makeVar('X', 'archived', false);
const enabledX = makeVar('X', 'current');
let state = makeInitialState([disabledX, enabledX]);
state = reducer(state, scriptEvent({ X: 'updated', __name__: 'Test' }));
const xVars = getEnv(state).variables.filter((v) => v.name === 'X');
expect(xVars).toHaveLength(2);
const stillDisabled = xVars.find((v) => v.enabled === false);
const nowEnabled = xVars.find((v) => v.enabled === true);
expect(stillDisabled.value).toBe('archived');
expect(nowEnabled.value).toBe('updated');
});
test('when only a disabled X exists, the script write creates a NEW enabled slot', () => {
const disabledOnly = makeVar('X', 'archived', false);
let state = makeInitialState([disabledOnly]);
state = reducer(state, scriptEvent({ X: 'created', __name__: 'Test' }));
const xVars = getEnv(state).variables.filter((v) => v.name === 'X');
expect(xVars).toHaveLength(2);
expect(xVars.find((v) => v.enabled === false).value).toBe('archived');
expect(xVars.find((v) => v.enabled === true).value).toBe('created');
});
});
});

View File

@@ -18,6 +18,7 @@ import {
isItemARequest
} from 'utils/collections';
import { parsePathParams, splitOnFirst } from 'utils/url';
import { applyScriptEnvVars, getScriptModifiedKeys } from 'utils/environments';
import { getSubdirectoriesFromRoot } from 'utils/common/platform';
import toast from 'react-hot-toast';
import mime from 'mime-types';
@@ -217,6 +218,8 @@ const initiatedWsResponse = {
trailers: []
};
// Properties prefixed with `_` (e.g. `_scriptEnvBaseline`) are transient runtime state —
// never persisted to disk or included in exports.
export const collectionsSlice = createSlice({
name: 'collections',
initialState,
@@ -452,84 +455,60 @@ export const collectionsSlice = createSlice({
}
},
scriptEnvironmentUpdateEvent: (state, action) => {
const { collectionUid, envVariables, runtimeVariables, persistentEnvVariables } = action.payload;
const { collectionUid, envVariables, requestUid } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
const activeEnvironmentUid = collection.activeEnvironmentUid;
const activeEnvironment = findEnvironmentInCollection(collection, activeEnvironmentUid);
if (activeEnvironment) {
const existingEnvVarNames = new Set(Object.keys(envVariables));
// Update or add variables that exist in envVariables
forOwn(envVariables, (value, key) => {
const variable = find(activeEnvironment.variables, (v) => v.name === key);
const isPersistent = persistentEnvVariables && persistentEnvVariables[key] !== undefined;
if (variable) {
// For updates coming from scripts, treat them as ephemeral overlays unless they are persistent.
if (variable.value !== value) {
/*
Overlay (persist: false): keep new value in Redux for UI and mark ephemeral
so it isn't written to disk. persistedValue stores the previous on-disk value;
save/persist uses that base unless the key is explicitly persisted.
*/
const previousValue = variable.value;
const wasEphemeral = !!variable.ephemeral;
variable.value = value;
variable.ephemeral = !isPersistent;
// Capture the on-disk base only when shadowing a real (non-ephemeral) var for
// the first time. A script-created ephemeral has no on-disk value to restore,
// so giving it a persistedValue would leak its overlay value into the file.
if (variable.persistedValue === undefined && !wasEphemeral) {
variable.persistedValue = previousValue;
}
// Secrets carry a dataType too; infer it from the value like any other var.
const inferred = getDataTypeFromValue(value);
variable.dataType = inferred === 'string' ? undefined : inferred;
}
} else {
// __name__ is a private variable used to store the name of the environment
// this is not a user defined variable and hence should not be updated
if (key !== '__name__') {
const inferred = getDataTypeFromValue(value);
activeEnvironment.variables.push({
name: key,
value,
secret: false,
enabled: true,
type: 'text',
uid: uuid(),
ephemeral: !isPersistent,
...(inferred !== 'string' ? { dataType: inferred } : {})
});
}
}
});
// Handle variables that were deleted via bru.deleteEnvVar()
activeEnvironment.variables = activeEnvironment.variables.filter((variable) => {
// Variable still exists in envVariables after script execution - keep it
if (existingEnvVarNames.has(variable.name)) {
return true;
}
// Variable was deleted via bru.deleteEnvVar() - handle based on its state
// If variable was modified by script (has persistedValue), restore original value
if (variable.persistedValue !== undefined) {
variable.value = variable.persistedValue;
variable.ephemeral = false;
delete variable.persistedValue;
return true;
}
// Remove variable: either ephemeral (created by scripts) or non-ephemeral deleted via API
return false;
});
// Ignore stale updates from superseded requests so an in-flight pre/post
// from request N-1 can't clobber state for request N.
if (requestUid && collection._scriptRequestUid && requestUid !== collection._scriptRequestUid) {
return;
}
const activeEnvironment = findEnvironmentInCollection(collection, collection.activeEnvironmentUid);
if (activeEnvironment) {
const draft = collection.environmentsDraft;
if (draft && draft.environmentUid === activeEnvironment.uid && draft.variables) {
const baseline = {};
activeEnvironment.variables.forEach((v) => {
if (v.enabled) baseline[v.name] = v.value;
});
collection._scriptEnvBaseline = baseline;
activeEnvironment.variables = cloneDeep(draft.variables);
collection.environmentsDraft = null;
}
activeEnvironment.variables = applyScriptEnvVars(
activeEnvironment.variables,
envVariables,
collection._scriptEnvBaseline,
{ skipKeys: ['__name__'] }
);
// Re-infer dataType only for vars the script actually modified — otherwise a no-op
// script re-write would clobber a user's in-progress draft type change.
const modifiedKeys = getScriptModifiedKeys(envVariables, collection._scriptEnvBaseline, { skipKeys: ['__name__'] });
activeEnvironment.variables.forEach((v) => {
if (!modifiedKeys.has(v.name)) return;
const inferred = getDataTypeFromValue(envVariables[v.name]);
if (inferred === 'string') {
delete v.dataType;
} else {
v.dataType = inferred;
}
});
}
}
},
runtimeVariablesUpdateEvent: (state, action) => {
const { collectionUid, runtimeVariables, requestUid } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (collection) {
if (requestUid && collection._scriptRequestUid && requestUid !== collection._scriptRequestUid) {
return;
}
collection.runtimeVariables = runtimeVariables;
}
},
@@ -2761,6 +2740,42 @@ export const collectionsSlice = createSlice({
set(collection, 'draft.root.request.vars.res', mappedVars);
}
},
scriptUpdateCollectionVars: (state, action) => {
const { collectionUid, vars } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (!collection) return;
// Preserve description/annotations/secret/type and any other per-var metadata via `...rest`
// — earlier this reducer cherry-picked only {uid, name, value, enabled, dataType} which
// wiped those fields whenever a script touched any collection var.
const mappedVars = map(vars, ({ uid, name = '', value = '', enabled = true, dataType, ...rest }) => {
const out = { ...rest, uid: uid || uuid(), name, value, enabled };
if (dataType && dataType !== 'string') out.dataType = dataType;
return out;
});
set(collection, 'root.request.vars.req', mappedVars);
if (collection.draft?.root) {
set(collection, 'draft.root.request.vars.req', mappedVars);
}
},
setScriptCollVarBaseline: (state, action) => {
const { collectionUid, baseline } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (!collection) return;
collection._scriptCollVarBaseline = baseline;
},
_clearScriptCollectionBaselines: (state, action) => {
const { collectionUid } = action.payload;
const collection = findCollectionByUid(state.collections, collectionUid);
if (!collection) return;
delete collection._scriptEnvBaseline;
delete collection._scriptCollVarBaseline;
// Also drop the inflight request UID so updates from WS/OAuth2 paths (which
// don't dispatch initRunRequestEvent) aren't gated out by a stale HTTP UID.
delete collection._scriptRequestUid;
},
collectionAddFileEvent: (state, action) => {
const file = action.payload.file;
const isCollectionRoot = file.meta.collectionRoot ? true : false;
@@ -3014,32 +3029,11 @@ export const collectionsSlice = createSlice({
const existingEnv = collection.environments.find((e) => e.uid === environment.uid);
if (existingEnv) {
const prevEphemerals = (existingEnv.variables || []).filter((v) => v.ephemeral);
existingEnv.name = environment.name;
existingEnv.pathname = environment.pathname;
existingEnv.variables = environment.variables;
existingEnv.color = environment.color;
existingEnv.externalSecrets = environment.externalSecrets;
/*
Apply temporary (ephemeral) values only to variables that actually exist in the file. This prevents deleted temporaries from “popping back” after a save. If a variable is present in the file, we temporarily override the UI value while also remembering the on-disk value in persistedValue for future saves.
*/
prevEphemerals.forEach((ev) => {
const target = existingEnv.variables?.find((v) => v.name === ev.name);
if (target) {
if (target.value !== ev.value) {
if (target.persistedValue === undefined) target.persistedValue = target.value;
target.value = ev.value;
}
target.ephemeral = true;
} else if (ev.persistedValue === undefined) {
/*
No counterpart in the file. A script-created overlay (persistedValue undefined) never
existed on disk, so a sibling persist:true save must not erase it — keep it visible.
An ephemeral with persistedValue shadowed a now-absent disk var (deleted), so it drops.
*/
existingEnv.variables.push(ev);
}
});
} else {
collection.environments.push(environment);
collection.environments.sort((a, b) => a.name.localeCompare(b.name));
@@ -3093,6 +3087,10 @@ export const collectionsSlice = createSlice({
const collection = findCollectionByUid(state.collections, collectionUid);
if (!collection) return;
delete collection._scriptEnvBaseline;
delete collection._scriptCollVarBaseline;
collection._scriptRequestUid = requestUid;
const item = findItemInCollection(collection, itemUid);
if (!item) return;
@@ -3222,6 +3220,9 @@ export const collectionsSlice = createSlice({
// todo
// get startedAt and endedAt from the runner and display it in the UI
if (type === 'testrun-started') {
delete collection._scriptEnvBaseline;
delete collection._scriptCollVarBaseline;
const info = collection.runnerResult.info;
info.collectionUid = collectionUid;
info.folderUid = folderUid;
@@ -3242,6 +3243,13 @@ export const collectionsSlice = createSlice({
}
if (type === 'request-queued') {
// Folder runs reuse the same collection across N requests; clear baselines
// per request so request N's script-update doesn't diff against request N-1's
// pre-flush snapshot.
delete collection._scriptEnvBaseline;
delete collection._scriptCollVarBaseline;
collection._scriptRequestUid = action.payload.requestUid || null;
collection.runnerResult.items.push({
uid: request.uid,
status: 'queued'
@@ -3897,6 +3905,7 @@ export const {
renameItem,
cloneItem,
scriptEnvironmentUpdateEvent,
runtimeVariablesUpdateEvent,
processEnvUpdateEvent,
workspaceEnvUpdateEvent,
setDotEnvVariables,
@@ -3986,6 +3995,9 @@ export const {
updateCollectionVar,
deleteCollectionVar,
setCollectionVars,
scriptUpdateCollectionVars,
setScriptCollVarBaseline,
_clearScriptCollectionBaselines,
updateCollectionAuthMode,
updateCollectionAuth,
updateCollectionRequestScript,

View File

@@ -3,9 +3,7 @@ import { collectionsSlice } from './index';
const {
setRequestVars,
setFolderVars,
setCollectionVars,
collectionAddEnvFileEvent,
scriptEnvironmentUpdateEvent
setCollectionVars
} = collectionsSlice.actions;
const reducer = collectionsSlice.reducer;
@@ -33,102 +31,6 @@ const assertGuardedVars = (vars) => {
expect(vars[2].dataType).toBeUndefined();
};
describe('collectionAddEnvFileEvent — ephemeral env vars on disk reload', () => {
const stateWithEnv = (variables) => ({
collections: [
{
uid: 'col1',
items: [],
environments: [{ uid: 'env1', name: 'dev', pathname: '/dev.bru', variables }]
}
]
});
// A persist:true setEnvVar writes the file, which reloads only the persisted var.
const fileReload = reducer(
stateWithEnv([
{ uid: 'v1', name: 'test_env_var', value: 'test', ephemeral: true },
{ uid: 'v2', name: 'test_env_var_test', value: 'test', ephemeral: false }
]),
collectionAddEnvFileEvent({
collectionUid: 'col1',
environment: {
uid: 'env1',
name: 'dev',
pathname: '/dev.bru',
variables: [{ uid: 'v2', name: 'test_env_var_test', value: 'test' }]
}
})
);
const reloadedVars = fileReload.collections[0].environments[0].variables;
it('keeps a script-created ephemeral var absent from the reloaded file', () => {
const kept = reloadedVars.find((v) => v.name === 'test_env_var');
expect(kept).toMatchObject({ name: 'test_env_var', value: 'test', ephemeral: true });
});
it('drops an overlay ephemeral (persistedValue set) absent from the reloaded file', () => {
const next = reducer(
stateWithEnv([{ uid: 'v1', name: 'deleted_overlay', value: 'temp', ephemeral: true, persistedValue: 'orig' }]),
collectionAddEnvFileEvent({
collectionUid: 'col1',
environment: { uid: 'env1', name: 'dev', pathname: '/dev.bru', variables: [] }
})
);
expect(next.collections[0].environments[0].variables).toHaveLength(0);
});
});
describe('scriptEnvironmentUpdateEvent — re-updating a script-created ephemeral var', () => {
const stateWithEnvVar = (variable) => ({
collections: [
{
uid: 'col1',
items: [],
activeEnvironmentUid: 'env1',
environments: [{ uid: 'env1', name: 'dev', variables: [variable] }]
}
]
});
it('does not give a script-created ephemeral var a persistedValue when its value changes again', () => {
// First run left this in Redux: created by setEnvVar (persist:false), never on disk.
const existing = { uid: 'v1', name: 'test_env_var', value: 'test', enabled: true, ephemeral: true };
const next = reducer(
stateWithEnvVar(existing),
scriptEnvironmentUpdateEvent({
collectionUid: 'col1',
envVariables: { test_env_var: 'updated' },
runtimeVariables: {},
persistentEnvVariables: {}
})
);
const variable = next.collections[0].environments[0].variables[0];
expect(variable).toMatchObject({ name: 'test_env_var', value: 'updated', ephemeral: true });
expect(variable.persistedValue).toBeUndefined();
});
it('captures the on-disk base as persistedValue when first shadowing a real var', () => {
const onDisk = { uid: 'v1', name: 'api_url', value: 'https://disk', enabled: true };
const next = reducer(
stateWithEnvVar(onDisk),
scriptEnvironmentUpdateEvent({
collectionUid: 'col1',
envVariables: { api_url: 'https://overlay' },
runtimeVariables: {},
persistentEnvVariables: {}
})
);
const variable = next.collections[0].environments[0].variables[0];
expect(variable).toMatchObject({ value: 'https://overlay', ephemeral: true, persistedValue: 'https://disk' });
});
});
describe('setRequestVars — strips dataType: \'string\' (implicit default)', () => {
it('drops a stray string-dataType on request vars and preserves typed datatypes', () => {
const item = {

View File

@@ -1,27 +1,33 @@
import { createSlice } from '@reduxjs/toolkit';
import { uuid } from 'utils/common/index';
import { environmentSchema } from '@usebruno/schema';
import { getDataTypeFromValue, valueToString } from '@usebruno/common/utils';
import { cloneDeep, has } from 'lodash';
const typedFieldsFor = (value) => {
const inferred = getDataTypeFromValue(value);
return inferred === 'string' ? { dataType: undefined } : { dataType: inferred };
};
import { getDataTypeFromValue } from '@usebruno/common/utils';
import { cloneDeep } from 'lodash';
import { applyScriptEnvVars, getScriptModifiedKeys } from 'utils/environments';
const initialState = {
globalEnvironments: [],
activeGlobalEnvironmentUid: null,
globalEnvironmentDraft: null
globalEnvironmentDraft: null,
_scriptGlobalEnvBaseline: null
};
// Properties prefixed with `_` (e.g. `_scriptGlobalEnvBaseline`) are transient runtime state —
// never persisted to disk or included in exports.
export const globalEnvironmentsSlice = createSlice({
name: 'global-environments',
initialState,
reducers: {
updateGlobalEnvironments: (state, action) => {
state.globalEnvironments = action.payload?.globalEnvironments;
state.activeGlobalEnvironmentUid = action.payload?.activeGlobalEnvironmentUid;
const newEnvs = action.payload?.globalEnvironments || [];
const incomingActiveUid = action.payload?.activeGlobalEnvironmentUid ?? null;
const resolvedActiveUid = incomingActiveUid && newEnvs.some((e) => e?.uid === incomingActiveUid)
? incomingActiveUid
: null;
state.globalEnvironments = newEnvs;
state.activeGlobalEnvironmentUid = resolvedActiveUid;
},
_addGlobalEnvironment: (state, action) => {
const { name, uid, variables = [], color } = action.payload;
@@ -89,6 +95,12 @@ export const globalEnvironmentsSlice = createSlice({
clearGlobalEnvironmentDraft: (state) => {
state.globalEnvironmentDraft = null;
},
_setScriptGlobalEnvBaseline: (state, action) => {
state._scriptGlobalEnvBaseline = action.payload;
},
_clearScriptGlobalEnvBaseline: (state) => {
state._scriptGlobalEnvBaseline = null;
},
_updateGlobalEnvironmentColor: (state, action) => {
const { environmentUid, color } = action.payload;
if (environmentUid) {
@@ -108,7 +120,9 @@ export const {
_deleteGlobalEnvironment,
_updateGlobalEnvironmentColor,
setGlobalEnvironmentDraft,
clearGlobalEnvironmentDraft
clearGlobalEnvironmentDraft,
_setScriptGlobalEnvBaseline,
_clearScriptGlobalEnvBaseline
} = globalEnvironmentsSlice.actions;
const getWorkspaceContext = (state) => {
@@ -270,64 +284,71 @@ export const deleteGlobalEnvironment = ({ environmentUid }) => (dispatch, getSta
});
};
export const globalEnvironmentsUpdateEvent = ({ globalEnvironmentVariables }) => (dispatch, getState) => {
return new Promise((resolve, reject) => {
const { ipcRenderer } = window;
if (!globalEnvironmentVariables) resolve();
export const globalEnvironmentsUpdateEvent = ({ globalEnvironmentVariables, collectionUid, requestUid }) => (dispatch, getState) => {
if (!globalEnvironmentVariables) return;
const state = getState();
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
const globalEnvironments = state?.globalEnvironments?.globalEnvironments || [];
const environmentUid = state?.globalEnvironments?.activeGlobalEnvironmentUid;
const environment = globalEnvironments?.find((env) => env?.uid == environmentUid);
const state = getState();
if (!environment || !environmentUid) {
return resolve();
// Ignore stale updates from superseded requests on the originating collection.
if (collectionUid && requestUid) {
const sourceCollection = state?.collections?.collections?.find((c) => c.uid === collectionUid);
if (sourceCollection?._scriptRequestUid && requestUid !== sourceCollection._scriptRequestUid) {
return;
}
}
let variables = cloneDeep(environment?.variables);
const globalEnvironments = state?.globalEnvironments?.globalEnvironments || [];
const environmentUid = state?.globalEnvironments?.activeGlobalEnvironmentUid;
const environment = globalEnvironments?.find((env) => env?.uid == environmentUid);
variables = variables?.map?.((variable) => {
if (!has(globalEnvironmentVariables, variable?.name)) return variable;
const newValue = globalEnvironmentVariables[variable?.name];
if (!environment || !environmentUid) return;
return {
...variable,
value: newValue,
...typedFieldsFor(newValue)
};
const draft = state?.globalEnvironments?.globalEnvironmentDraft;
if (draft && draft.environmentUid === environmentUid && draft.variables) {
const baseline = {};
environment.variables?.forEach((v) => {
if (v.enabled) baseline[v.name] = v.value;
});
dispatch(_setScriptGlobalEnvBaseline(baseline));
Object.entries(globalEnvironmentVariables)?.forEach?.(([key, value]) => {
const isAnExistingVariable = variables?.find((v) => v?.name == key);
if (!isAnExistingVariable) {
variables.push({
uid: uuid(),
name: key,
value,
type: 'text',
secret: false,
enabled: true,
...typedFieldsFor(value)
});
}
});
dispatch(_saveGlobalEnvironment({ environmentUid, variables: draft.variables }));
dispatch(clearGlobalEnvironmentDraft());
}
const environmentToSave = { ...environment, variables };
const updatedState = getState();
const updatedEnv = updatedState?.globalEnvironments?.globalEnvironments?.find((env) => env?.uid == environmentUid);
const baseline = updatedState?.globalEnvironments?._scriptGlobalEnvBaseline;
let variables = cloneDeep(updatedEnv?.variables || []);
environmentSchema
.validate(environmentToSave)
.then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid,
variables,
color: environment.color,
workspaceUid,
workspacePath
}))
.then(() => dispatch(_saveGlobalEnvironment({ environmentUid, variables })))
.then(resolve)
.catch(reject);
variables = applyScriptEnvVars(variables, globalEnvironmentVariables, baseline, { skipKeys: ['__name__'] });
// Re-infer dataType only for vars the script actually modified — preserves draft-only type edits
// when a script does a structurally-equal no-op write.
const modifiedKeys = getScriptModifiedKeys(globalEnvironmentVariables, baseline, { skipKeys: ['__name__'] });
variables.forEach((v) => {
if (!modifiedKeys.has(v.name)) return;
const inferred = getDataTypeFromValue(globalEnvironmentVariables[v.name]);
if (inferred === 'string') {
delete v.dataType;
} else {
v.dataType = inferred;
}
});
dispatch(_saveGlobalEnvironment({ environmentUid, variables }));
const { ipcRenderer } = window;
const { workspaceUid, workspacePath } = getWorkspaceContext(state);
environmentSchema
.validate({ ...environment, variables })
.then(() => ipcRenderer.invoke('renderer:save-global-environment', {
environmentUid,
variables,
color: environment.color,
workspaceUid,
workspacePath
}))
.catch((err) => console.error('Failed to persist global environment:', err));
};
export const updateGlobalEnvironmentColor = (environmentUid, color) => (dispatch, getState) => {

View File

@@ -0,0 +1,531 @@
jest.mock('nanoid', () => ({
customAlphabet: () => () => 'mock-uid'
}));
jest.mock('@usebruno/schema', () => ({
environmentSchema: { validate: () => Promise.resolve() }
}));
import { configureStore } from '@reduxjs/toolkit';
import globalEnvironmentsReducer, {
globalEnvironmentsUpdateEvent,
_clearScriptGlobalEnvBaseline,
updateGlobalEnvironments
} from 'providers/ReduxStore/slices/global-environments';
const ENV_UID = 'genv-1';
const makeVar = (name, value, enabled = true) => ({
uid: `uid-${name}`,
name,
value,
type: 'text',
secret: false,
enabled
});
// Minimal mock for window.ipcRenderer used by the thunk's disk persistence
beforeAll(() => {
window.ipcRenderer = { invoke: jest.fn().mockResolvedValue(true) };
});
const createStore = (envVars = [], opts = {}) => {
const preloadedState = {
globalEnvironments: {
globalEnvironments: [
{ uid: ENV_UID, name: 'GlobalTest', variables: envVars, color: null }
],
activeGlobalEnvironmentUid: ENV_UID,
globalEnvironmentDraft: opts.draft || null,
_scriptGlobalEnvBaseline: opts.baseline || null
},
workspaces: {
activeWorkspaceUid: 'ws-1',
workspaces: [{ uid: 'ws-1', pathname: '/workspace' }]
}
};
return configureStore({
reducer: {
globalEnvironments: globalEnvironmentsReducer,
workspaces: (state = preloadedState.workspaces) => state
},
preloadedState
});
};
const getEnv = (store) => {
const state = store.getState();
return state.globalEnvironments.globalEnvironments.find((e) => e.uid === ENV_UID);
};
const getGlobalState = (store) => store.getState().globalEnvironments;
describe('globalEnvironmentsUpdateEvent — draft-aware merge', () => {
describe('no draft pending (original behavior)', () => {
test('updates existing variable values', () => {
const store = createStore([makeVar('HOST', 'https://old.com')]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { HOST: 'https://new.com' } }));
expect(getEnv(store).variables).toHaveLength(1);
expect(getEnv(store).variables[0].value).toBe('https://new.com');
});
test('adds new variables from script', () => {
const store = createStore([makeVar('HOST', 'https://example.com')]);
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://example.com', TOKEN: 'abc' }
}));
expect(getEnv(store).variables).toHaveLength(2);
expect(getEnv(store).variables.find((v) => v.name === 'TOKEN').value).toBe('abc');
});
test('removes enabled variables deleted by script', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('OLD_VAR', 'remove-me')
]);
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://example.com' }
}));
expect(getEnv(store).variables).toHaveLength(1);
expect(getEnv(store).variables[0].name).toBe('HOST');
});
test('preserves disabled variables even if not in script output', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('DISABLED_VAR', 'keep', false)
]);
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://example.com' }
}));
expect(getEnv(store).variables).toHaveLength(2);
expect(getEnv(store).variables.find((v) => v.name === 'DISABLED_VAR')).toBeDefined();
});
});
describe('draft pending — first script event', () => {
test('flushes draft, creates baseline, and applies only script changes', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [makeVar('HOST', 'https://draft-edit.com')];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com', TOKEN: 'new-token' }
}));
const vars = getEnv(store).variables;
// HOST: script didn't change it → draft value preserved
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft-edit.com');
// TOKEN: new from script → added
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('new-token');
// Draft cleared
expect(getGlobalState(store).globalEnvironmentDraft).toBeNull();
// Baseline set
expect(getGlobalState(store)._scriptGlobalEnvBaseline).toEqual({ HOST: 'https://saved.com' });
});
test('applies script modification over draft edit of same variable', () => {
const savedVars = [makeVar('TOKEN', 'saved-token')];
const draftVars = [makeVar('TOKEN', 'draft-token')];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { TOKEN: 'script-token' }
}));
expect(getEnv(store).variables[0].value).toBe('script-token');
});
test('preserves draft-only variables that script does not know about', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('NEW_DRAFT_VAR', 'draft-only-value')
];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com' }
}));
const vars = getEnv(store).variables;
expect(vars).toHaveLength(2);
expect(vars.find((v) => v.name === 'NEW_DRAFT_VAR').value).toBe('draft-only-value');
});
test('ignores draft for non-active environment', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const store = createStore(savedVars, {
draft: { environmentUid: 'other-env-uid', variables: [makeVar('HOST', 'https://draft.com')] }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://new.com' }
}));
// Draft should NOT be flushed (different env)
expect(getGlobalState(store).globalEnvironmentDraft).not.toBeNull();
// Should apply directly
expect(getEnv(store).variables[0].value).toBe('https://new.com');
expect(getGlobalState(store)._scriptGlobalEnvBaseline).toBeNull();
});
});
describe('baseline exists — subsequent script events', () => {
test('preserves draft edits across multiple script events', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [makeVar('HOST', 'https://draft.com')];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// First event
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com', TOKEN: 'abc' }
}));
// Second event — same data
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com', TOKEN: 'abc' }
}));
const vars = getEnv(store).variables;
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('abc');
});
test('applies new changes from later script events', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [makeVar('HOST', 'https://draft.com')];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// First event
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com', TOKEN: 'abc' }
}));
// Second event: post-response adds RESULT
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com', TOKEN: 'abc', RESULT: 'ok' }
}));
const vars = getEnv(store).variables;
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('abc');
expect(vars.find((v) => v.name === 'RESULT').value).toBe('ok');
});
});
describe('deleteGlobalEnvVar — script deletes a variable', () => {
test('no draft: removes the variable', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret')
]);
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://example.com' }
}));
expect(getEnv(store).variables).toHaveLength(1);
expect(getEnv(store).variables[0].name).toBe('HOST');
});
test('with draft: removes variable that existed in saved state', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token')
];
const draftVars = [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token')
];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
// Script deleted TOKEN
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com' }
}));
const vars = getEnv(store).variables;
expect(vars.find((v) => v.name === 'TOKEN')).toBeUndefined();
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
});
test('with draft: does not remove draft-only variables', () => {
const savedVars = [makeVar('HOST', 'https://saved.com')];
const draftVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('DRAFT_ONLY', 'user-added')
];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: { HOST: 'https://saved.com' }
}));
const vars = getEnv(store).variables;
expect(vars.find((v) => v.name === 'DRAFT_ONLY').value).toBe('user-added');
});
});
describe('deleteAllGlobalEnvVars — script clears all variables', () => {
test('no draft: removes all enabled variables', () => {
const store = createStore([
makeVar('HOST', 'https://example.com'),
makeVar('TOKEN', 'secret'),
makeVar('DISABLED', 'keep', false)
]);
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: {}
}));
const vars = getEnv(store).variables;
expect(vars).toHaveLength(1);
expect(vars[0].name).toBe('DISABLED');
});
test('with draft: removes saved vars but keeps draft-only vars', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token')
];
const draftVars = [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token'),
makeVar('DRAFT_ONLY', 'user-added')
];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: {}
}));
const vars = getEnv(store).variables;
expect(vars.find((v) => v.name === 'HOST')).toBeUndefined();
expect(vars.find((v) => v.name === 'TOKEN')).toBeUndefined();
expect(vars.find((v) => v.name === 'DRAFT_ONLY').value).toBe('user-added');
});
});
describe('mixed operations — set + delete in same script', () => {
test('with draft: script adds, modifies, and deletes — draft edits preserved', () => {
const savedVars = [
makeVar('HOST', 'https://saved.com'),
makeVar('TOKEN', 'saved-token'),
makeVar('TO_DELETE', 'remove-me')
];
const draftVars = [
makeVar('HOST', 'https://draft.com'),
makeVar('TOKEN', 'draft-token'),
makeVar('TO_DELETE', 'draft-value'),
makeVar('DRAFT_NEW', 'from-user')
];
const store = createStore(savedVars, {
draft: { environmentUid: ENV_UID, variables: draftVars }
});
store.dispatch(globalEnvironmentsUpdateEvent({
globalEnvironmentVariables: {
HOST: 'https://saved.com',
TOKEN: 'script-token',
SCRIPT_NEW: 'from-script'
}
}));
const vars = getEnv(store).variables;
expect(vars.find((v) => v.name === 'HOST').value).toBe('https://draft.com');
expect(vars.find((v) => v.name === 'TOKEN').value).toBe('script-token');
expect(vars.find((v) => v.name === 'TO_DELETE')).toBeUndefined();
expect(vars.find((v) => v.name === 'DRAFT_NEW').value).toBe('from-user');
expect(vars.find((v) => v.name === 'SCRIPT_NEW').value).toBe('from-script');
});
});
describe('typed values — dataType inference', () => {
test('infers number dataType when script sets a numeric value', () => {
const store = createStore([makeVar('COUNT', '0')]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { COUNT: 42 } }));
const v = getEnv(store).variables.find((v) => v.name === 'COUNT');
expect(v.value).toBe(42);
expect(v.dataType).toBe('number');
});
test('infers boolean dataType when script sets a boolean value', () => {
const store = createStore([makeVar('FLAG', 'false')]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { FLAG: true } }));
const v = getEnv(store).variables.find((v) => v.name === 'FLAG');
expect(v.value).toBe(true);
expect(v.dataType).toBe('boolean');
});
test('infers object dataType when script sets an object value', () => {
const store = createStore([makeVar('CONFIG', '')]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { CONFIG: { port: 3000 } } }));
const v = getEnv(store).variables.find((v) => v.name === 'CONFIG');
expect(v.value).toEqual({ port: 3000 });
expect(v.dataType).toBe('object');
});
test('keeps existing dataType on a typed var the script did not touch', () => {
const typedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
const store = createStore([typedVar, makeVar('HOST', 'https://example.com')]);
// Script touched HOST only; the runtime payload still carries COUNT (unchanged).
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { COUNT: 42, HOST: 'https://new.com' } }));
const v = getEnv(store).variables.find((v) => v.name === 'COUNT');
expect(v.dataType).toBe('number');
});
test('drops dataType when script replaces a typed value with a string', () => {
const typedVar = { ...makeVar('COUNT', 42), dataType: 'number' };
const store = createStore([typedVar]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { COUNT: 'not-a-number' } }));
const v = getEnv(store).variables.find((v) => v.name === 'COUNT');
expect(v.dataType).toBeUndefined();
});
test('updates dataType when script changes the value type', () => {
const typedVar = { ...makeVar('VAL', 42), dataType: 'number' };
const store = createStore([typedVar]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { VAL: true } }));
const v = getEnv(store).variables.find((v) => v.name === 'VAL');
expect(v.value).toBe(true);
expect(v.dataType).toBe('boolean');
});
});
describe('baseline cleanup', () => {
test('_clearScriptGlobalEnvBaseline clears the baseline', () => {
const store = createStore([makeVar('HOST', 'https://saved.com')], {
baseline: { HOST: 'https://saved.com' }
});
expect(getGlobalState(store)._scriptGlobalEnvBaseline).not.toBeNull();
store.dispatch(_clearScriptGlobalEnvBaseline());
expect(getGlobalState(store)._scriptGlobalEnvBaseline).toBeNull();
});
});
describe('object/array typed-var no-op writes preserve draft (deep-equal compare)', () => {
test('script re-writing a structurally-equal object value does NOT clobber draft edit', () => {
const savedVar = { ...makeVar('CFG', { port: 3000 }), dataType: 'object' };
const draftVar = { ...makeVar('CFG', { port: 4000 }), dataType: 'object' };
const store = createStore([savedVar], {
draft: { environmentUid: ENV_UID, variables: [draftVar] }
});
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { CFG: { port: 3000 } } }));
const v = getEnv(store).variables.find((v) => v.name === 'CFG');
expect(v.value).toEqual({ port: 4000 });
});
});
describe('disabled-var name collision — script targets enabled slot only', () => {
test('script setting X writes to the enabled X, leaves the disabled X untouched', () => {
const disabledX = makeVar('X', 'archived', false);
const enabledX = makeVar('X', 'current');
const store = createStore([disabledX, enabledX]);
store.dispatch(globalEnvironmentsUpdateEvent({ globalEnvironmentVariables: { X: 'updated' } }));
const xVars = getEnv(store).variables.filter((v) => v.name === 'X');
expect(xVars).toHaveLength(2);
expect(xVars.find((v) => v.enabled === false).value).toBe('archived');
expect(xVars.find((v) => v.enabled === true).value).toBe('updated');
});
});
});
describe('updateGlobalEnvironments — activeGlobalEnvironmentUid resolution', () => {
// The reducer trusts the caller-supplied active uid (every consumer derives it from the
// per-workspace electron store) and only validates it against the new env list, dropping
// to null if the uid doesn't match any env.
const setup = (preEnvs, preActiveUid, payloadEnvs, payloadActiveUid) => {
const preloadedState = {
globalEnvironments: {
globalEnvironments: preEnvs,
activeGlobalEnvironmentUid: preActiveUid,
globalEnvironmentDraft: null,
_scriptGlobalEnvBaseline: null
}
};
const store = configureStore({
reducer: { globalEnvironments: globalEnvironmentsReducer },
preloadedState
});
store.dispatch(updateGlobalEnvironments({
globalEnvironments: payloadEnvs,
activeGlobalEnvironmentUid: payloadActiveUid
}));
return store.getState().globalEnvironments.activeGlobalEnvironmentUid;
};
test('incoming uid present in new envs → resolved to incoming uid', () => {
const result = setup(
[{ uid: 'old-1', name: 'Stage' }],
'old-1',
[{ uid: 'new-1', name: 'Stage' }, { uid: 'new-2', name: 'Prod' }],
'new-2'
);
expect(result).toBe('new-2');
});
test('incoming uid not in new envs → null (stale uid is dropped)', () => {
const result = setup(
[{ uid: 'gone', name: 'DeletedEnv' }],
'gone',
[{ uid: 'other', name: 'Different' }],
'gone'
);
expect(result).toBeNull();
});
test('no incoming uid + no prior state → null', () => {
const result = setup(
[],
null,
[{ uid: 'env-A', name: 'Stage' }],
null
);
expect(result).toBeNull();
});
});

View File

@@ -3,9 +3,6 @@ import { mockDataFunctions } from '@usebruno/common';
const CodeMirror = require('codemirror');
// Static API hints - Bruno JavaScript API (subgrouped by category)
// TODO: Restore the commented-out APIs once the UI update fixes are live.
// Currently these APIs only work within the request lifecycle but fail to update the UI tables.
// e.g., setCollectionVar only sets the variable in the request lifecycle, fails to update the table in the UI.
const STATIC_API_HINTS = {
req: [
'req',
@@ -109,13 +106,12 @@ const STATIC_API_HINTS = {
'bru.getEnvVar(key)',
'bru.getFolderVar(key)',
'bru.getCollectionVar(key)',
// 'bru.setCollectionVar(key, value)',
'bru.setCollectionVar(key, value)',
'bru.hasCollectionVar(key)',
// 'bru.deleteCollectionVar(key)',
// 'bru.deleteAllCollectionVars()',
// 'bru.getAllCollectionVars()',
'bru.deleteCollectionVar(key)',
'bru.deleteAllCollectionVars()',
'bru.getAllCollectionVars()',
'bru.setEnvVar(key, value)',
'bru.setEnvVar(key, value, options)',
'bru.deleteEnvVar(key)',
'bru.getAllEnvVars()',
'bru.deleteAllEnvVars()',
@@ -139,9 +135,9 @@ const STATIC_API_HINTS = {
'bru.hasGlobalEnvVar(key)',
'bru.getGlobalEnvVar(key)',
'bru.setGlobalEnvVar(key, value)',
// 'bru.deleteGlobalEnvVar(key)',
'bru.deleteGlobalEnvVar(key)',
'bru.getAllGlobalEnvVars()',
// 'bru.deleteAllGlobalEnvVars()',
'bru.deleteAllGlobalEnvVars()',
'bru.runner',
'bru.runner.setNextRequest(requestName)',
'bru.runner.skipRequest()',

View File

@@ -1,6 +1,5 @@
import { cloneDeep, isEqual, sortBy, filter, map, isString, findIndex, find, each, get } from 'lodash';
import { uuid } from 'utils/common';
import { buildPersistedEnvVariables } from 'utils/environments';
import { sortByNameThenSequence } from 'utils/common/index';
import path from 'utils/common/path';
import { isRequestTagsIncluded } from '@usebruno/common';
@@ -603,11 +602,7 @@ export const transformCollectionToSaveToExportAsFile = (collection, options = {}
collectionToSave.version = '1';
collectionToSave.items = [];
collectionToSave.activeEnvironmentUid = collection.activeEnvironmentUid;
// Save environments without runtime metadata (ephemeral/persistedValue)
collectionToSave.environments = (collection.environments || []).map((env) => ({
...env,
variables: buildPersistedEnvVariables(env?.variables, { mode: 'save' })
}));
collectionToSave.environments = collection.environments || [];
collectionToSave.root = {
request: {}

View File

@@ -1,34 +1,6 @@
import { isEqual } from 'lodash';
import { uuid } from './common/index';
const isPersistableEnvVarForMerge = (persistedNames) => (v) => {
return !v?.ephemeral || v?.persistedValue !== undefined || (v?.name && persistedNames.has(v.name));
};
const toPersistedEnvVarForMerge = (persistedNames) => (v) => {
const { ephemeral, persistedValue, ...rest } = v || {};
if (v?.ephemeral && persistedValue !== undefined && !(v?.name && persistedNames.has(v.name))) {
return { ...rest, value: persistedValue };
}
return rest;
};
const toPersistedEnvVarForSave = (v) => {
const { ephemeral, persistedValue, ...rest } = v || {};
return rest;
};
// mode 'save': commit the visible value (Save button).
// mode 'merge': commit only allowed vars — non-ephemeral, ephemerals with
// persistedValue, or names explicitly persisted this run.
export const buildPersistedEnvVariables = (variables, { mode, persistedNames } = {}) => {
const src = Array.isArray(variables) ? variables : [];
if (mode === 'merge') {
const names = persistedNames instanceof Set ? persistedNames : new Set();
return src.filter(isPersistableEnvVarForMerge(names)).map(toPersistedEnvVarForMerge(names));
}
return src.map(toPersistedEnvVarForSave);
};
export const buildEnvVariable = ({ envVariable: obj, withUuid = false }) => {
const isSecret = !!obj.secret;
let envVariable = {
@@ -53,6 +25,84 @@ export const buildEnvVariable = ({ envVariable: obj, withUuid = false }) => {
};
};
/**
* Apply script-produced environment variables onto a variables array.
*
* With baseline: only applies values the script changed relative to the snapshot (preserves draft edits).
* Without baseline: direct apply — overwrites all values from script output.
* Disabled variables are always preserved; script writes target the enabled slot only — if no
* enabled var with `key` exists, a new enabled one is inserted (any same-named disabled var is left intact).
*
* Pure: does not mutate the input array or its entries. Returns a new array of new objects.
*/
export const applyScriptEnvVars = (variables, scriptVars, baseline, { skipKeys = [] } = {}) => {
const scriptVarNames = new Set(Object.keys(scriptVars));
const skip = new Set(skipKeys);
const next = (variables || []).map((v) => ({ ...v }));
if (baseline) {
Object.entries(scriptVars).forEach(([key, value]) => {
if (skip.has(key)) return;
const isNew = !(key in baseline);
// Deep-equal so object/array typed vars whose structurally-equal value is re-written by the
// script aren't treated as modifications (and thus don't clobber draft edits).
const isModified = !isNew && !isEqual(baseline[key], value);
if (isNew || isModified) {
// Target only the enabled slot — a draft-disabled var with the same name must be preserved.
const existing = next.find((v) => v.name === key && v.enabled);
if (existing) {
existing.value = value;
} else {
next.push({ uid: uuid(), name: key, value, type: 'text', secret: false, enabled: true });
}
}
});
return next.filter((v) => {
if (!v.enabled) return true;
if (v.name in baseline && !scriptVarNames.has(v.name)) return false;
return true;
});
}
Object.entries(scriptVars).forEach(([key, value]) => {
if (skip.has(key)) return;
const existing = next.find((v) => v.name === key && v.enabled);
if (existing) {
existing.value = value;
} else {
next.push({ uid: uuid(), name: key, value, type: 'text', secret: false, enabled: true });
}
});
return next.filter((v) => !v.enabled || scriptVarNames.has(v.name));
};
/**
* Returns the set of keys the script actually modified relative to a baseline (or all script keys
* when no baseline is supplied — direct-apply mode). Used by the slice reducers to scope dataType
* re-inference to vars that actually changed; without this the dataType loop would clobber a user's
* draft-only typed value edit on every no-op script re-run.
*/
export const getScriptModifiedKeys = (scriptVars, baseline, { skipKeys = [] } = {}) => {
const skip = new Set(skipKeys);
const out = new Set();
Object.entries(scriptVars || {}).forEach(([key, value]) => {
if (skip.has(key)) return;
if (baseline) {
const isNew = !(key in baseline);
if (!isNew && isEqual(baseline[key], value)) return;
}
out.add(key);
});
return out;
};
/**
* Strips the UID from an environment variable for comparison purposes.
* This is useful when comparing variables where UIDs may differ but the actual data is the same.
*/
export const stripEnvVarUid = (variable) => {
const { name, value, type, enabled, secret, dataType } = variable;
const result = { name, value, type, enabled, secret };

View File

@@ -3,7 +3,7 @@ jest.mock('nanoid', () => ({
customAlphabet: () => () => 'aaaaaaaaaaaaaaaaaaaa1'
}));
import { buildEnvVariable, stripEnvVarUid, buildPersistedEnvVariables } from './environments';
import { applyScriptEnvVars, buildEnvVariable, stripEnvVarUid } from './environments';
describe('buildEnvVariable — dataType preservation for env export/import', () => {
it('preserves non-string datatypes on non-secret variables', () => {
@@ -55,6 +55,238 @@ describe('stripEnvVarUid — datatype-aware comparison key', () => {
});
});
describe('applyScriptEnvVars', () => {
const v = (name, value, enabled = true) => ({
uid: `uid-${name}`,
name,
value,
type: 'text',
secret: false,
enabled
});
describe('direct-apply mode (no baseline)', () => {
it('updates the value of an existing variable', () => {
const result = applyScriptEnvVars([v('host', 'old')], { host: 'new' }, null);
expect(result.find((x) => x.name === 'host').value).toBe('new');
});
it('appends variables present in scriptVars but not in the array', () => {
const result = applyScriptEnvVars([v('host', 'h')], { host: 'h', token: 'abc' }, null);
expect(result).toHaveLength(2);
expect(result.find((x) => x.name === 'token')).toMatchObject({
name: 'token',
value: 'abc',
type: 'text',
secret: false,
enabled: true
});
});
it('removes enabled variables missing from scriptVars (script deleted them)', () => {
const result = applyScriptEnvVars([v('host', 'h'), v('stale', 'remove-me')], { host: 'h' }, null);
expect(result).toHaveLength(1);
expect(result[0].name).toBe('host');
});
it('preserves disabled variables even if missing from scriptVars', () => {
const result = applyScriptEnvVars([v('host', 'h'), v('keep', 'k', false)], { host: 'h' }, null);
expect(result.map((x) => x.name).sort()).toEqual(['host', 'keep']);
});
it('honors skipKeys — entries are neither applied nor used for the removal filter', () => {
const result = applyScriptEnvVars(
[v('host', 'h')],
{ host: 'h', __name__: 'Test' },
null,
{ skipKeys: ['__name__'] }
);
// __name__ is not pushed as a new var even though it appeared in scriptVars
expect(result.find((x) => x.name === '__name__')).toBeUndefined();
// host is still present (it IS in scriptVarNames, which is built before skipKeys is applied)
expect(result.find((x) => x.name === 'host')).toBeDefined();
});
it('preserves typed (non-string) values without coercion', () => {
const result = applyScriptEnvVars([], { count: 42, flag: true, cfg: { k: 1 } }, null);
expect(result.find((x) => x.name === 'count').value).toBe(42);
expect(result.find((x) => x.name === 'flag').value).toBe(true);
expect(result.find((x) => x.name === 'cfg').value).toEqual({ k: 1 });
});
it('returns an empty array when both inputs are empty', () => {
expect(applyScriptEnvVars([], {}, null)).toEqual([]);
});
it('preserves dataType on an existing var when only its value is updated', () => {
const existing = [{ ...v('count', 41), dataType: 'number' }];
const result = applyScriptEnvVars(existing, { count: 42 }, null);
const out = result.find((x) => x.name === 'count');
expect(out.value).toBe(42);
expect(out.dataType).toBe('number');
});
it('does NOT attach a dataType to newly-pushed vars — that is the caller\'s responsibility', () => {
// applyScriptEnvVars never calls getDataTypeFromValue; the slice that owns the
// dispatch (scriptEnvironmentUpdateEvent / globalEnvironmentsUpdateEvent /
// collectionVariablesUpdateEvent) infers and attaches dataType after this merge.
const result = applyScriptEnvVars([], { count: 42, flag: true, cfg: { k: 1 } }, null);
expect(result.find((x) => x.name === 'count').dataType).toBeUndefined();
expect(result.find((x) => x.name === 'flag').dataType).toBeUndefined();
expect(result.find((x) => x.name === 'cfg').dataType).toBeUndefined();
});
});
describe('baseline-diff mode (preserves draft edits)', () => {
it('does NOT overwrite a draft edit when the script value matches the baseline (unchanged)', () => {
const draftVars = [v('host', 'draft-edit')];
const baseline = { host: 'saved-value' };
const scriptVars = { host: 'saved-value' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
expect(result.find((x) => x.name === 'host').value).toBe('draft-edit');
});
it('overwrites the draft value when the script value differs from baseline (modified)', () => {
const draftVars = [v('host', 'draft-edit')];
const baseline = { host: 'saved-value' };
const scriptVars = { host: 'script-new-value' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
expect(result.find((x) => x.name === 'host').value).toBe('script-new-value');
});
it('adds variables that appear in scriptVars but not in baseline (new)', () => {
const draftVars = [v('host', 'h')];
const baseline = { host: 'h' };
const scriptVars = { host: 'h', fresh: 'from-script' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
expect(result.find((x) => x.name === 'fresh')).toMatchObject({
name: 'fresh',
value: 'from-script',
enabled: true
});
});
it('removes variables that were in baseline but missing from scriptVars (script deleted)', () => {
const draftVars = [v('host', 'h'), v('wasSaved', 'value')];
const baseline = { host: 'h', wasSaved: 'value' };
const scriptVars = { host: 'h' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
expect(result.find((x) => x.name === 'wasSaved')).toBeUndefined();
});
it('preserves draft-only variables (not in baseline, not in scriptVars)', () => {
const draftVars = [v('host', 'h'), v('draft-only', 'user-added')];
const baseline = { host: 'h' };
const scriptVars = { host: 'h' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
expect(result.find((x) => x.name === 'draft-only')).toMatchObject({
name: 'draft-only',
value: 'user-added'
});
});
it('preserves disabled variables even when they would otherwise be removed', () => {
const draftVars = [v('host', 'h'), v('disabled', 'keep', false)];
const baseline = { host: 'h', disabled: 'keep' };
const scriptVars = { host: 'h' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
// 'disabled' is in baseline and missing from scriptVars, but it's disabled so it stays
expect(result.find((x) => x.name === 'disabled')).toMatchObject({ name: 'disabled', enabled: false });
});
it('honors skipKeys — does not modify or add skipped entries', () => {
const draftVars = [v('host', 'h')];
const baseline = { host: 'h' };
const scriptVars = { host: 'h', __name__: 'Test' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline, { skipKeys: ['__name__'] });
expect(result.find((x) => x.name === '__name__')).toBeUndefined();
});
it('preserves dataType on an existing var when only its value is updated', () => {
const draftVars = [{ ...v('count', 41), dataType: 'number' }];
const baseline = { count: 40 };
const scriptVars = { count: 42 };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
const out = result.find((x) => x.name === 'count');
expect(out.value).toBe(42);
expect(out.dataType).toBe('number');
});
it('preserves dataType on a disabled typed var that the script does not touch', () => {
const draftVars = [v('host', 'h'), { ...v('flag', false, false), dataType: 'boolean' }];
const baseline = { host: 'h', flag: false };
const scriptVars = { host: 'h' };
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
const out = result.find((x) => x.name === 'flag');
expect(out).toMatchObject({ name: 'flag', enabled: false, dataType: 'boolean' });
});
it('combined: script adds, modifies, deletes; draft edits to unchanged vars are preserved', () => {
const draftVars = [
v('host', 'draft-host'), // user-edited, script will leave value matching baseline
v('token', 'draft-token'), // user-edited, script will override
v('stale', 'draft-stale'), // script will delete (was in baseline)
v('draft-only', 'user-added') // user-only, not in baseline
];
const baseline = {
host: 'saved-host',
token: 'saved-token',
stale: 'saved-stale'
};
const scriptVars = {
host: 'saved-host', // unchanged from baseline → draft 'draft-host' wins
token: 'script-new-token', // modified → script wins
added: 'from-script' // new → added
};
const result = applyScriptEnvVars(draftVars, scriptVars, baseline);
const byName = Object.fromEntries(result.map((x) => [x.name, x.value]));
expect(byName).toEqual({
'host': 'draft-host',
'token': 'script-new-token',
'added': 'from-script',
'draft-only': 'user-added'
});
expect(result.find((x) => x.name === 'stale')).toBeUndefined();
});
});
describe('secret flag preservation', () => {
const secretVar = (name, value, enabled = true) => ({
uid: `uid-${name}`,
name,
value,
type: 'text',
secret: true,
enabled
});
it('preserves secret: true when script updates an enabled secret var (baseline mode)', () => {
const result = applyScriptEnvVars(
[secretVar('apiToken', 'old')],
{ apiToken: 'new' },
{ apiToken: 'old' }
);
expect(result).toHaveLength(1);
expect(result[0]).toMatchObject({ name: 'apiToken', value: 'new', secret: true, enabled: true });
});
it('preserves secret: true in direct-apply (no baseline) mode', () => {
const result = applyScriptEnvVars([secretVar('apiToken', 'old')], { apiToken: 'new' }, null);
expect(result[0]).toMatchObject({ name: 'apiToken', value: 'new', secret: true });
});
});
});
describe('Env export → import round-trip via JSON', () => {
it('preserves dataType across export → JSON.stringify → JSON.parse → import for every supported type', () => {
const reduxEnvVars = [
@@ -89,61 +321,3 @@ describe('Env export → import round-trip via JSON', () => {
expect(reimported[5]).toMatchObject({ name: 'token', value: '', secret: true, dataType: 'number' });
});
});
describe('buildPersistedEnvVariables — save mode', () => {
// Regression guard: save mode must commit the visible value of an ephemeral
// var, not roll it back to persistedValue.
it('keeps the visible value when an ephemeral var has a persistedValue', () => {
const variables = [
{
name: 'apiKey',
value: 'testvalue',
type: 'text',
enabled: true,
secret: true,
ephemeral: true,
persistedValue: 'test'
}
];
expect(buildPersistedEnvVariables(variables, { mode: 'save' })).toEqual([
{ name: 'apiKey', value: 'testvalue', type: 'text', enabled: true, secret: true }
]);
});
it('keeps the visible value for a non-secret ephemeral var too', () => {
const variables = [
{
name: 'host',
value: 'localhost-from-script',
type: 'text',
enabled: true,
secret: false,
ephemeral: true,
persistedValue: 'localhost'
}
];
expect(buildPersistedEnvVariables(variables, { mode: 'save' })).toEqual([
{ name: 'host', value: 'localhost-from-script', type: 'text', enabled: true, secret: false }
]);
});
it('strips ephemeral/persistedValue from non-ephemeral vars', () => {
const variables = [
{
name: 'plain',
value: 'v',
type: 'text',
enabled: true,
secret: false,
ephemeral: false,
persistedValue: 'leftover'
}
];
expect(buildPersistedEnvVariables(variables, { mode: 'save' })).toEqual([
{ name: 'plain', value: 'v', type: 'text', enabled: true, secret: false }
]);
});
});

View File

@@ -4,7 +4,6 @@ const BRUNO_API_REFERENCE = `## Bruno API Reference
\`\`\`js
bru.getEnvVar(key)
bru.setEnvVar(key, value)
bru.setEnvVar(key, value, { persist: true })
bru.hasEnvVar(key)
bru.deleteEnvVar(key)
bru.getEnvName()

View File

@@ -506,7 +506,10 @@ const registerRendererEventHandlers = (mainWindow, watcher) => {
const filename = format === 'yml' ? 'opencollection.yml' : 'collection.bru';
const content = await stringifyCollection(collectionRoot, brunoConfig, { format });
await writeFile(path.join(collectionPathname, filename), content);
const filePath = path.join(collectionPathname, filename);
const existing = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : null;
if (content === existing) return; // skip write if content unchanged
await writeFile(filePath, content);
} catch (error) {
console.error('Error in save-collection-root:', error);
return Promise.reject(error);
@@ -766,6 +769,8 @@ const registerRendererEventHandlers = (mainWindow, watcher) => {
}
const content = await stringifyEnvironment(environment, { format });
const existing = fs.readFileSync(envFilePath, 'utf8');
if (content === existing) return; // skip write if content unchanged
await writeFile(envFilePath, content);
} catch (error) {
return Promise.reject(error);

View File

@@ -508,6 +508,41 @@ const registerNetworkIpc = (mainWindow) => {
};
};
const sendVariableUpdates = (result, { collectionUid, requestUid, collection }) => {
if (result.runtimeVariables) {
mainWindow.webContents.send('main:runtime-variables-update', {
runtimeVariables: result.runtimeVariables,
requestUid,
collectionUid
});
}
if (result.envVariables) {
mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
requestUid,
collectionUid
});
}
if (result.globalEnvironmentVariables) {
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: result.globalEnvironmentVariables,
requestUid,
collectionUid
});
collection.globalEnvironmentVariables = result.globalEnvironmentVariables;
}
if (result.collectionVariables) {
mainWindow.webContents.send('main:collection-variables-update', {
collectionVariables: result.collectionVariables,
requestUid,
collectionUid
});
}
};
const resetOauth2Credentials = ({ oauth2CredentialsToReset, request, collectionUid }) => {
if (!oauth2CredentialsToReset?.length) return;
for (const credentialId of oauth2CredentialsToReset) {
@@ -559,25 +594,7 @@ const registerNetworkIpc = (mainWindow) => {
collectionName
);
mainWindow.webContents.send('main:script-environment-update', {
envVariables: scriptResult.envVariables,
runtimeVariables: scriptResult.runtimeVariables,
persistentEnvVariables: scriptResult.persistentEnvVariables,
requestUid,
collectionUid
});
mainWindow.webContents.send('main:persistent-env-variables-update', {
persistentEnvVariables: scriptResult.persistentEnvVariables,
collectionUid
});
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: scriptResult.globalEnvironmentVariables
});
collection.globalEnvironmentVariables = scriptResult.globalEnvironmentVariables;
sendVariableUpdates(scriptResult, { collectionUid, requestUid, collection });
resetOauth2Credentials({ oauth2CredentialsToReset: scriptResult.oauth2CredentialsToReset, request, collectionUid });
const domainsWithCookies = await getDomainsWithCookies();
@@ -669,24 +686,7 @@ const registerNetworkIpc = (mainWindow) => {
);
if (result) {
mainWindow.webContents.send('main:script-environment-update', {
envVariables: result.envVariables,
runtimeVariables: result.runtimeVariables,
persistentEnvVariables: result.persistentEnvVariables,
requestUid,
collectionUid
});
mainWindow.webContents.send('main:persistent-env-variables-update', {
persistentEnvVariables: result.persistentEnvVariables,
collectionUid
});
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: result.globalEnvironmentVariables
});
collection.globalEnvironmentVariables = result.globalEnvironmentVariables;
sendVariableUpdates(result, { collectionUid, requestUid, collection });
}
if (result?.error) {
@@ -714,25 +714,7 @@ const registerNetworkIpc = (mainWindow) => {
collectionName
);
mainWindow.webContents.send('main:script-environment-update', {
envVariables: scriptResult.envVariables,
runtimeVariables: scriptResult.runtimeVariables,
persistentEnvVariables: scriptResult.persistentEnvVariables,
requestUid,
collectionUid
});
mainWindow.webContents.send('main:persistent-env-variables-update', {
persistentEnvVariables: scriptResult.persistentEnvVariables,
collectionUid
});
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: scriptResult.globalEnvironmentVariables
});
collection.globalEnvironmentVariables = scriptResult.globalEnvironmentVariables;
sendVariableUpdates(scriptResult, { collectionUid, requestUid, collection });
resetOauth2Credentials({ oauth2CredentialsToReset: scriptResult.oauth2CredentialsToReset, request, collectionUid });
const domainsWithCookiesPost = await getDomainsWithCookies();
@@ -741,12 +723,13 @@ const registerNetworkIpc = (mainWindow) => {
return scriptResult;
};
const runRequest = async ({ item, collection, envVars, processEnvVars, runtimeVariables, runInBackground = false, callerBru = null, parentExecutionMode = null, parentRunnerEventData = null }) => {
const runRequest = async ({ item, collection, envVars, processEnvVars, runtimeVariables, runInBackground = false, callerBru = null, parentExecutionMode = null, parentRunnerEventData = null, parentRequestUid = null }) => {
const collectionUid = collection.uid;
const collectionPath = collection.pathname;
const cancelTokenUid = uuid();
// Nested bru.runRequest() invocations have no item.requestUid; mint one.
const requestUid = item.requestUid || uuid();
// Nested bru.runRequest() invocations have no item.requestUid; inherit the parent's
// so script-driven variable updates aren't dropped by the renderer's requestUid gate.
const requestUid = item.requestUid || parentRequestUid || uuid();
const runRequestByItemPathname = async (relativeItemPathname, callerBru) => {
return new Promise(async (resolve, reject) => {
@@ -797,7 +780,7 @@ const registerNetworkIpc = (mainWindow) => {
const startedAt = Date.now();
let res, err;
try {
res = await runRequest({ item: _item, collection, envVars, processEnvVars, runtimeVariables, runInBackground: true, callerBru, parentExecutionMode, parentRunnerEventData });
res = await runRequest({ item: _item, collection, envVars, processEnvVars, runtimeVariables, runInBackground: true, callerBru, parentExecutionMode, parentRunnerEventData, parentRequestUid: requestUid });
} catch (e) {
err = e;
}
@@ -929,6 +912,9 @@ const registerNetworkIpc = (mainWindow) => {
if (preRequestError?.partialResults) {
preRequestScriptResult = preRequestError.partialResults;
// Forward any variable mutations the script made before throwing so the UI
// and disk stay in sync with the partial test results we're about to render.
sendVariableUpdates(preRequestScriptResult, { collectionUid, requestUid, collection });
}
emitScriptedRequestEvents('pre-request', preRequestScriptResult);
@@ -1127,6 +1113,8 @@ const registerNetworkIpc = (mainWindow) => {
// (e.g., if 2 tests pass then script throws, we still want to show those 2 passing tests)
if (postResponseError?.partialResults) {
postResponseScriptResult = postResponseError.partialResults;
// Forward any variable mutations the script made before throwing.
sendVariableUpdates(postResponseScriptResult, { collectionUid, requestUid, collection });
}
emitScriptedRequestEvents('post-response', postResponseScriptResult);
@@ -1221,24 +1209,7 @@ const registerNetworkIpc = (mainWindow) => {
collectionUid
});
mainWindow.webContents.send('main:script-environment-update', {
envVariables: testResults.envVariables,
runtimeVariables: testResults.runtimeVariables,
requestUid,
collectionUid
});
mainWindow.webContents.send('main:persistent-env-variables-update', {
persistentEnvVariables: testResults.persistentEnvVariables,
collectionUid
});
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: testResults.globalEnvironmentVariables
});
collection.globalEnvironmentVariables = testResults.globalEnvironmentVariables;
sendVariableUpdates(testResults, { collectionUid, requestUid, collection });
resetOauth2Credentials({ oauth2CredentialsToReset: testResults.oauth2CredentialsToReset, request, collectionUid });
!runInBackground && notifyScriptExecution({
@@ -1643,8 +1614,11 @@ const registerNetworkIpc = (mainWindow) => {
let timeStart;
let timeEnd;
const requestUid = uuid();
mainWindow.webContents.send('main:run-folder-event', {
type: 'request-queued',
requestUid,
...eventData
});
@@ -1669,8 +1643,6 @@ const registerNetworkIpc = (mainWindow) => {
const request = await prepareRequest(item, collection, abortController);
request.__bruno__executionMode = 'runner';
const requestUid = uuid();
const promptVars = await extractPromptVariablesForRequest({ request, collection, envVars, runtimeVariables, processEnvVars });
if (promptVars.length > 0) {
@@ -1729,6 +1701,7 @@ const registerNetworkIpc = (mainWindow) => {
if (preRequestError?.partialResults) {
preRequestScriptResult = preRequestError.partialResults;
sendVariableUpdates(preRequestScriptResult, { collectionUid, requestUid, collection });
}
preRequestScriptResult = appendScriptErrorResult('pre-request', preRequestScriptResult, preRequestError);
@@ -1987,6 +1960,7 @@ const registerNetworkIpc = (mainWindow) => {
// (e.g., if 2 tests pass then script throws, we still want to show those 2 passing tests)
if (postResponseError?.partialResults) {
postResponseScriptResult = postResponseError.partialResults;
sendVariableUpdates(postResponseScriptResult, { collectionUid, requestUid, collection });
}
postResponseScriptResult = appendScriptErrorResult('post-response', postResponseScriptResult, postResponseError);
@@ -2094,18 +2068,7 @@ const registerNetworkIpc = (mainWindow) => {
...eventData
});
mainWindow.webContents.send('main:script-environment-update', {
envVariables: testResults.envVariables,
runtimeVariables: testResults.runtimeVariables,
collectionUid
});
mainWindow.webContents.send('main:global-environment-variables-update', {
globalEnvironmentVariables: testResults.globalEnvironmentVariables
});
collection.globalEnvironmentVariables = testResults.globalEnvironmentVariables;
sendVariableUpdates(testResults, { collectionUid, requestUid, collection });
resetOauth2Credentials({ oauth2CredentialsToReset: testResults.oauth2CredentialsToReset, request, collectionUid });
notifyScriptExecution({

View File

@@ -0,0 +1,62 @@
const EnvironmentSecretsStore = require('../../src/store/env-secrets');
const { decryptStringSafe } = require('../../src/utils/encryption');
describe('EnvironmentSecretsStore', () => {
let secretsStore;
const collectionPath = '/tmp/test-collection';
beforeEach(() => {
secretsStore = new EnvironmentSecretsStore();
secretsStore.store.clear();
});
it('encrypts secret values before persisting them', () => {
const environment = {
name: 'Local',
variables: [
{ name: 'apiToken', value: 'new', enabled: true, secret: true },
{ name: 'host', value: 'http://localhost', enabled: true, secret: false }
]
};
secretsStore.storeEnvSecrets(collectionPath, environment);
const stored = secretsStore.getEnvSecrets(collectionPath, environment);
expect(stored).toHaveLength(1);
expect(stored[0].name).toBe('apiToken');
expect(stored[0].value).not.toBe('new');
expect(typeof stored[0].value).toBe('string');
expect(stored[0].value.length).toBeGreaterThan(0);
});
it('round-trips the secret value through decryptStringSafe', () => {
const environment = {
name: 'Local',
variables: [{ name: 'apiToken', value: 'new', enabled: true, secret: true }]
};
secretsStore.storeEnvSecrets(collectionPath, environment);
const [stored] = secretsStore.getEnvSecrets(collectionPath, environment);
const decrypted = decryptStringSafe(stored.value);
expect(decrypted.success).toBe(true);
expect(decrypted.value).toBe('new');
});
it('overwrites the prior encrypted value when the same secret is re-stored', () => {
const environment = {
name: 'Local',
variables: [{ name: 'apiToken', value: 'first', enabled: true, secret: true }]
};
secretsStore.storeEnvSecrets(collectionPath, environment);
const [first] = secretsStore.getEnvSecrets(collectionPath, environment);
environment.variables[0].value = 'second';
secretsStore.storeEnvSecrets(collectionPath, environment);
const after = secretsStore.getEnvSecrets(collectionPath, environment);
expect(after).toHaveLength(1);
expect(after[0].value).not.toBe(first.value);
expect(decryptStringSafe(after[0].value).value).toBe('second');
});
});

View File

@@ -1,4 +1,4 @@
const { cloneDeep } = require('lodash');
const { cloneDeep, isEqual } = require('lodash');
const xmlFormat = require('xml-formatter');
const { interpolate: _interpolate } = require('@usebruno/common');
const { createSendRequest } = require('@usebruno/requests').scripting;
@@ -76,8 +76,11 @@ class Bru {
createCookieJar,
getCookiesForUrl
});
// Holds variables that are marked as persistent by scripts
this.persistentEnvVariables = {};
// Dirty flags — set by mutators so runtimes can skip IPC/disk writes for unchanged scopes
this._envDirty = false;
this._globalEnvDirty = false;
this._collVarsDirty = false;
this._runtimeVarsDirty = false;
// Holds credential IDs to be reset after script execution
this.oauth2CredentialsToReset = [];
this.runner = {
@@ -192,7 +195,7 @@ class Bru {
return this.interpolate(this.envVariables[key]);
}
setEnvVar(key, value, options = {}) {
setEnvVar(key, value) {
if (!key) {
throw new Error('Creating a env variable without specifying a name is not allowed.');
}
@@ -203,19 +206,21 @@ class Bru {
);
}
this.envVariables[key] = value;
if (options?.persist) {
this.persistentEnvVariables[key] = value;
} else {
if (this.persistentEnvVariables[key]) {
delete this.persistentEnvVariables[key];
}
// Deep-equal compare so object/array writes that mutate in place
// (e.g. `const c = bru.getEnvVar('cfg'); c.port = 4000; bru.setEnvVar('cfg', c);`)
// still flip the dirty flag — strict `!==` returned false for same-reference writes.
if (!Object.hasOwn(this.envVariables, key) || !isEqual(this.envVariables[key], value)) {
this.envVariables[key] = value;
this._envDirty = true;
}
}
deleteEnvVar(key) {
delete this.envVariables[key];
if (key === '__name__') return;
if (Object.hasOwn(this.envVariables, key)) {
delete this.envVariables[key];
this._envDirty = true;
}
}
getAllEnvVars() {
@@ -225,15 +230,15 @@ class Bru {
}
deleteAllEnvVars() {
const envName = this.envVariables.__name__;
for (let key in this.envVariables) {
if (this.envVariables.hasOwnProperty(key)) {
delete this.envVariables[key];
}
}
if (envName !== undefined) {
this.envVariables.__name__ = envName;
// Iterate via Object.keys (own enumerable) so a user-set `hasOwnProperty` var
// can't shadow Object.prototype.hasOwnProperty and crash the loop.
let removed = false;
for (const key of Object.keys(this.envVariables)) {
if (key === '__name__') continue;
delete this.envVariables[key];
removed = true;
}
if (removed) this._envDirty = true;
}
hasGlobalEnvVar(key) {
@@ -249,28 +254,31 @@ class Bru {
throw new Error('Creating a env variable without specifying a name is not allowed.');
}
this.globalEnvironmentVariables[key] = value;
if (!Object.hasOwn(this.globalEnvironmentVariables, key) || !isEqual(this.globalEnvironmentVariables[key], value)) {
this.globalEnvironmentVariables[key] = value;
this._globalEnvDirty = true;
}
}
// TODO: deleteGlobalEnvVar works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// deleteGlobalEnvVar(key) {
// delete this.globalEnvironmentVariables[key];
// }
deleteGlobalEnvVar(key) {
if (Object.hasOwn(this.globalEnvironmentVariables, key)) {
delete this.globalEnvironmentVariables[key];
this._globalEnvDirty = true;
}
}
getAllGlobalEnvVars() {
return Object.assign({}, this.globalEnvironmentVariables);
}
// TODO: deleteAllGlobalEnvVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// deleteAllGlobalEnvVars() {
// for (let key in this.globalEnvironmentVariables) {
// if (this.globalEnvironmentVariables.hasOwnProperty(key)) {
// delete this.globalEnvironmentVariables[key];
// }
// }
// }
deleteAllGlobalEnvVars() {
const keys = Object.keys(this.globalEnvironmentVariables);
if (!keys.length) return;
for (const key of keys) {
delete this.globalEnvironmentVariables[key];
}
this._globalEnvDirty = true;
}
getOauth2CredentialVar(key) {
return this.interpolate(this.oauth2CredentialVariables[key]);
@@ -310,7 +318,10 @@ class Bru {
);
}
this.runtimeVariables[key] = value;
if (!Object.hasOwn(this.runtimeVariables, key) || !isEqual(this.runtimeVariables[key], value)) {
this.runtimeVariables[key] = value;
this._runtimeVarsDirty = true;
}
}
getVar(key) {
@@ -325,15 +336,19 @@ class Bru {
}
deleteVar(key) {
delete this.runtimeVariables[key];
if (Object.hasOwn(this.runtimeVariables, key)) {
delete this.runtimeVariables[key];
this._runtimeVarsDirty = true;
}
}
deleteAllVars() {
for (let key in this.runtimeVariables) {
if (this.runtimeVariables.hasOwnProperty(key)) {
delete this.runtimeVariables[key];
}
const keys = Object.keys(this.runtimeVariables);
if (!keys.length) return;
for (const key of keys) {
delete this.runtimeVariables[key];
}
this._runtimeVarsDirty = true;
}
getAllVars() {
@@ -344,48 +359,47 @@ class Bru {
return this.interpolate(this.collectionVariables[key]);
}
// TODO: setCollectionVar works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// setCollectionVar(key, value) {
// if (!key) {
// throw new Error('Creating a variable without specifying a name is not allowed.');
// }
//
// if (variableNameRegex.test(key) === false) {
// throw new Error(
// `Variable name: "${key}" contains invalid characters!`
// + ' Names must only contain alpha-numeric characters, "-", "_", "."'
// );
// }
//
// this.collectionVariables[key] = value;
// }
setCollectionVar(key, value) {
if (!key) {
throw new Error('Creating a variable without specifying a name is not allowed.');
}
if (variableNameRegex.test(key) === false) {
throw new Error(
`Variable name: "${key}" contains invalid characters!`
+ ' Names must only contain alpha-numeric characters, "-", "_", "."'
);
}
if (!Object.hasOwn(this.collectionVariables, key) || !isEqual(this.collectionVariables[key], value)) {
this.collectionVariables[key] = value;
this._collVarsDirty = true;
}
}
hasCollectionVar(key) {
return Object.hasOwn(this.collectionVariables, key);
}
// TODO: deleteCollectionVar works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// deleteCollectionVar(key) {
// delete this.collectionVariables[key];
// }
deleteCollectionVar(key) {
if (Object.hasOwn(this.collectionVariables, key)) {
delete this.collectionVariables[key];
this._collVarsDirty = true;
}
}
// TODO: deleteAllCollectionVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// deleteAllCollectionVars() {
// for (let key in this.collectionVariables) {
// if (this.collectionVariables.hasOwnProperty(key)) {
// delete this.collectionVariables[key];
// }
// }
// }
deleteAllCollectionVars() {
const keys = Object.keys(this.collectionVariables);
if (!keys.length) return;
for (const key of keys) {
delete this.collectionVariables[key];
}
this._collVarsDirty = true;
}
// TODO: getAllCollectionVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// getAllCollectionVars() {
// return Object.assign({}, this.collectionVariables);
// }
getAllCollectionVars() {
return Object.assign({}, this.collectionVariables);
}
getFolderVar(key) {
return this.interpolate(this.folderVariables[key]);

View File

@@ -89,10 +89,10 @@ class ScriptRuntime {
// Extracted to avoid duplication across runtime branches
const buildRequestScriptResult = () => ({
request,
envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables),
persistentEnvVariables: bru.persistentEnvVariables,
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
envVariables: bru._envDirty ? cleanJson(envVariables) : null,
runtimeVariables: bru._runtimeVarsDirty ? cleanJson(runtimeVariables) : null,
collectionVariables: bru._collVarsDirty ? cleanJson(collectionVariables) : null,
globalEnvironmentVariables: bru._globalEnvDirty ? cleanJson(globalEnvironmentVariables) : null,
oauth2CredentialsToReset: bru.oauth2CredentialsToReset,
results: cleanJson(__brunoTestResults.getResults()),
nextRequestName: bru.nextRequest,
@@ -225,10 +225,10 @@ class ScriptRuntime {
// Extracted to avoid duplication across runtime branches
const buildResponseScriptResult = () => ({
response,
envVariables: cleanJson(envVariables),
persistentEnvVariables: cleanJson(bru.persistentEnvVariables),
runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
envVariables: bru._envDirty ? cleanJson(envVariables) : null,
runtimeVariables: bru._runtimeVarsDirty ? cleanJson(runtimeVariables) : null,
collectionVariables: bru._collVarsDirty ? cleanJson(collectionVariables) : null,
globalEnvironmentVariables: bru._globalEnvDirty ? cleanJson(globalEnvironmentVariables) : null,
oauth2CredentialsToReset: bru.oauth2CredentialsToReset,
results: cleanJson(__brunoTestResults.getResults()),
nextRequestName: bru.nextRequest,

View File

@@ -62,9 +62,10 @@ class TestRuntime {
if (!testsFile || !testsFile.length) {
return {
request,
envVariables,
runtimeVariables,
globalEnvironmentVariables,
envVariables: null,
runtimeVariables: null,
collectionVariables: null,
globalEnvironmentVariables: null,
results: __brunoTestResults.getResults(),
nextRequestName: bru.nextRequest
};
@@ -125,10 +126,10 @@ class TestRuntime {
const result = {
request,
envVariables: cleanJson(envVariables),
runtimeVariables: cleanJson(runtimeVariables),
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
persistentEnvVariables: cleanJson(bru.persistentEnvVariables),
envVariables: bru._envDirty ? cleanJson(envVariables) : null,
runtimeVariables: bru._runtimeVarsDirty ? cleanJson(runtimeVariables) : null,
collectionVariables: bru._collVarsDirty ? cleanJson(collectionVariables) : null,
globalEnvironmentVariables: bru._globalEnvDirty ? cleanJson(globalEnvironmentVariables) : null,
oauth2CredentialsToReset: bru.oauth2CredentialsToReset,
results: cleanJson(__brunoTestResults.getResults()),
nextRequestName: bru.nextRequest,

View File

@@ -86,10 +86,10 @@ class VarsRuntime {
}
return {
envVariables,
runtimeVariables,
globalEnvironmentVariables: cleanJson(globalEnvironmentVariables),
persistentEnvVariables: cleanJson(bru.persistentEnvVariables),
envVariables: bru._envDirty ? cleanJson(envVariables) : null,
runtimeVariables: bru._runtimeVarsDirty ? cleanJson(runtimeVariables) : null,
collectionVariables: bru._collVarsDirty ? cleanJson(collectionVariables) : null,
globalEnvironmentVariables: bru._globalEnvDirty ? cleanJson(globalEnvironmentVariables) : null,
error
};
}

View File

@@ -54,8 +54,8 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'getEnvVar', getEnvVar);
getEnvVar.dispose();
let setEnvVar = vm.newFunction('setEnvVar', function (key, value, options = {}) {
bru.setEnvVar(vm.dump(key), vm.dump(value), vm.dump(options));
let setEnvVar = vm.newFunction('setEnvVar', function (key, value) {
bru.setEnvVar(vm.dump(key), vm.dump(value));
});
vm.setProp(bruObject, 'setEnvVar', setEnvVar);
setEnvVar.dispose();
@@ -102,13 +102,11 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'setGlobalEnvVar', setGlobalEnvVar);
setGlobalEnvVar.dispose();
// TODO: deleteGlobalEnvVar works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let deleteGlobalEnvVar = vm.newFunction('deleteGlobalEnvVar', function (key) {
// bru.deleteGlobalEnvVar(vm.dump(key));
// });
// vm.setProp(bruObject, 'deleteGlobalEnvVar', deleteGlobalEnvVar);
// deleteGlobalEnvVar.dispose();
let deleteGlobalEnvVar = vm.newFunction('deleteGlobalEnvVar', function (key) {
bru.deleteGlobalEnvVar(vm.dump(key));
});
vm.setProp(bruObject, 'deleteGlobalEnvVar', deleteGlobalEnvVar);
deleteGlobalEnvVar.dispose();
let getAllGlobalEnvVars = vm.newFunction('getAllGlobalEnvVars', function () {
return marshallToVm(bru.getAllGlobalEnvVars(), vm);
@@ -122,13 +120,11 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'hasGlobalEnvVar', hasGlobalEnvVar);
hasGlobalEnvVar.dispose();
// TODO: deleteAllGlobalEnvVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let deleteAllGlobalEnvVars = vm.newFunction('deleteAllGlobalEnvVars', function () {
// bru.deleteAllGlobalEnvVars();
// });
// vm.setProp(bruObject, 'deleteAllGlobalEnvVars', deleteAllGlobalEnvVars);
// deleteAllGlobalEnvVars.dispose();
let deleteAllGlobalEnvVars = vm.newFunction('deleteAllGlobalEnvVars', function () {
bru.deleteAllGlobalEnvVars();
});
vm.setProp(bruObject, 'deleteAllGlobalEnvVars', deleteAllGlobalEnvVars);
deleteAllGlobalEnvVars.dispose();
let hasVar = vm.newFunction('hasVar', function (key) {
return marshallToVm(bru.hasVar(vm.dump(key)), vm);
@@ -220,13 +216,11 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'getCollectionVar', getCollectionVar);
getCollectionVar.dispose();
// TODO: setCollectionVar works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let setCollectionVar = vm.newFunction('setCollectionVar', function (key, value) {
// bru.setCollectionVar(vm.dump(key), vm.dump(value));
// });
// vm.setProp(bruObject, 'setCollectionVar', setCollectionVar);
// setCollectionVar.dispose();
let setCollectionVar = vm.newFunction('setCollectionVar', function (key, value) {
bru.setCollectionVar(vm.dump(key), vm.dump(value));
});
vm.setProp(bruObject, 'setCollectionVar', setCollectionVar);
setCollectionVar.dispose();
let hasCollectionVar = vm.newFunction('hasCollectionVar', function (key) {
return marshallToVm(bru.hasCollectionVar(vm.dump(key)), vm);
@@ -234,29 +228,23 @@ const addBruShimToContext = (vm, bru) => {
vm.setProp(bruObject, 'hasCollectionVar', hasCollectionVar);
hasCollectionVar.dispose();
// TODO: deleteCollectionVar works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let deleteCollectionVar = vm.newFunction('deleteCollectionVar', function (key) {
// bru.deleteCollectionVar(vm.dump(key));
// });
// vm.setProp(bruObject, 'deleteCollectionVar', deleteCollectionVar);
// deleteCollectionVar.dispose();
let deleteCollectionVar = vm.newFunction('deleteCollectionVar', function (key) {
bru.deleteCollectionVar(vm.dump(key));
});
vm.setProp(bruObject, 'deleteCollectionVar', deleteCollectionVar);
deleteCollectionVar.dispose();
// TODO: deleteAllCollectionVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let deleteAllCollectionVars = vm.newFunction('deleteAllCollectionVars', function () {
// bru.deleteAllCollectionVars();
// });
// vm.setProp(bruObject, 'deleteAllCollectionVars', deleteAllCollectionVars);
// deleteAllCollectionVars.dispose();
let deleteAllCollectionVars = vm.newFunction('deleteAllCollectionVars', function () {
bru.deleteAllCollectionVars();
});
vm.setProp(bruObject, 'deleteAllCollectionVars', deleteAllCollectionVars);
deleteAllCollectionVars.dispose();
// TODO: getAllCollectionVars works in the request lifecycle but does not update the UI.
// Re-enable once the UI sync issue is resolved.
// let getAllCollectionVars = vm.newFunction('getAllCollectionVars', function () {
// return marshallToVm(bru.getAllCollectionVars(), vm);
// });
// vm.setProp(bruObject, 'getAllCollectionVars', getAllCollectionVars);
// getAllCollectionVars.dispose();
let getAllCollectionVars = vm.newFunction('getAllCollectionVars', function () {
return marshallToVm(bru.getAllCollectionVars(), vm);
});
vm.setProp(bruObject, 'getAllCollectionVars', getAllCollectionVars);
getAllCollectionVars.dispose();
let getTestResults = vm.newFunction('getTestResults', () => {
const promise = vm.newPromise();

View File

@@ -0,0 +1,217 @@
const Bru = require('../src/bru');
describe('Collection Variable APIs', () => {
const makeBru = (collectionVariables = {}) =>
new Bru({
runtime: 'quickjs',
envVariables: {},
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test',
collectionVariables
});
describe('bru.setCollectionVar', () => {
test('sets a new collection variable', () => {
const bru = makeBru();
bru.setCollectionVar('baseUrl', 'https://api.example.com');
expect(bru.collectionVariables.baseUrl).toBe('https://api.example.com');
});
test('overwrites an existing collection variable', () => {
const bru = makeBru({ baseUrl: 'https://old.com' });
bru.setCollectionVar('baseUrl', 'https://new.com');
expect(bru.collectionVariables.baseUrl).toBe('https://new.com');
});
test('allows non-string values', () => {
const bru = makeBru();
bru.setCollectionVar('count', 42);
expect(bru.collectionVariables.count).toBe(42);
bru.setCollectionVar('active', true);
expect(bru.collectionVariables.active).toBe(true);
bru.setCollectionVar('config', { port: 3000 });
expect(bru.collectionVariables.config).toEqual({ port: 3000 });
});
test('throws when key is empty', () => {
const bru = makeBru();
expect(() => bru.setCollectionVar('', 'v')).toThrow(/without specifying a name/);
});
test('throws when key has invalid characters', () => {
const bru = makeBru();
expect(() => bru.setCollectionVar('invalid key', 'v')).toThrow(/contains invalid characters/);
});
test('re-run: setting same key twice does not throw', () => {
const bru = makeBru();
bru.setCollectionVar('key', 'first');
bru.setCollectionVar('key', 'second');
expect(bru.collectionVariables.key).toBe('second');
});
});
describe('bru.setCollectionVar — dirty flag for typed values', () => {
test('setting a number trips the collection-vars dirty flag', () => {
const bru = makeBru();
expect(bru._collVarsDirty).toBe(false);
bru.setCollectionVar('count', 42);
expect(bru._collVarsDirty).toBe(true);
});
test('setting a boolean trips the collection-vars dirty flag', () => {
const bru = makeBru();
bru.setCollectionVar('active', true);
expect(bru._collVarsDirty).toBe(true);
});
test('setting an object trips the collection-vars dirty flag', () => {
const bru = makeBru();
bru.setCollectionVar('config', { port: 3000 });
expect(bru._collVarsDirty).toBe(true);
});
});
describe('bru.setCollectionVar — dirty flag for reference-mutation idiom', () => {
test('the getCollectionVar → mutate → setCollectionVar idiom trips the dirty flag', () => {
const bru = makeBru({ config: { port: 3000 } });
expect(bru._collVarsDirty).toBe(false);
// Real-script idiom: getCollectionVar deep-copies through interpolate's JSON roundtrip,
// so mutating the result leaves the store untouched and the deep-equal guard fires.
const config = bru.getCollectionVar('config');
config.port = 4000;
bru.setCollectionVar('config', config);
expect(bru._collVarsDirty).toBe(true);
expect(bru.collectionVariables.config).toEqual({ port: 4000 });
});
test('re-setting a structurally-equal object value does NOT trip the dirty flag', () => {
const bru = makeBru({ config: { port: 3000 } });
bru.setCollectionVar('config', { port: 3000 });
expect(bru._collVarsDirty).toBe(false);
});
test('re-setting a structurally-equal primitive value does NOT trip the dirty flag', () => {
const bru = makeBru({ token: 'abc' });
bru.setCollectionVar('token', 'abc');
expect(bru._collVarsDirty).toBe(false);
});
});
describe('bru.deleteCollectionVar — dirty flag contract', () => {
test('deleting an existing key trips the collection-vars dirty flag', () => {
const bru = makeBru({ token: 'abc' });
bru.deleteCollectionVar('token');
expect(bru._collVarsDirty).toBe(true);
});
test('deleting a non-existent key leaves the dirty flag clean', () => {
const bru = makeBru();
bru.deleteCollectionVar('missing');
expect(bru._collVarsDirty).toBe(false);
});
});
describe('bru.deleteAllCollectionVars — dirty flag contract', () => {
test('deleting populated scope trips the dirty flag', () => {
const bru = makeBru({ a: '1', b: '2' });
bru.deleteAllCollectionVars();
expect(bru._collVarsDirty).toBe(true);
});
test('calling on empty scope leaves the dirty flag clean', () => {
const bru = makeBru();
bru.deleteAllCollectionVars();
expect(bru._collVarsDirty).toBe(false);
});
});
describe('bru.getCollectionVar', () => {
test('returns the value of an existing variable', () => {
const bru = makeBru({ token: 'abc123' });
expect(bru.getCollectionVar('token')).toBe('abc123');
});
test('returns undefined for a non-existent variable', () => {
const bru = makeBru();
expect(bru.getCollectionVar('missing')).toBeUndefined();
});
});
describe('bru.hasCollectionVar', () => {
test('returns true for an existing variable', () => {
const bru = makeBru({ token: 'abc' });
expect(bru.hasCollectionVar('token')).toBe(true);
});
test('returns false for a non-existent variable', () => {
const bru = makeBru();
expect(bru.hasCollectionVar('missing')).toBe(false);
});
});
describe('bru.deleteCollectionVar', () => {
test('removes an existing variable', () => {
const bru = makeBru({ token: 'abc' });
bru.deleteCollectionVar('token');
expect(bru.collectionVariables.token).toBeUndefined();
expect(bru.hasCollectionVar('token')).toBe(false);
});
test('re-run: deleting a non-existent key is a silent no-op', () => {
const bru = makeBru();
expect(() => bru.deleteCollectionVar('missing')).not.toThrow();
});
test('re-run: deleting an already-deleted key is a silent no-op', () => {
const bru = makeBru({ token: 'abc' });
bru.deleteCollectionVar('token');
expect(() => bru.deleteCollectionVar('token')).not.toThrow();
});
});
describe('bru.deleteAllCollectionVars', () => {
test('removes all collection variables', () => {
const bru = makeBru({ a: '1', b: '2', c: '3' });
bru.deleteAllCollectionVars();
expect(bru.collectionVariables).toEqual({});
});
test('re-run: calling on empty scope is a silent no-op', () => {
const bru = makeBru();
expect(() => bru.deleteAllCollectionVars()).not.toThrow();
expect(bru.collectionVariables).toEqual({});
});
test('re-run: calling twice is a silent no-op', () => {
const bru = makeBru({ a: '1' });
bru.deleteAllCollectionVars();
expect(() => bru.deleteAllCollectionVars()).not.toThrow();
expect(bru.collectionVariables).toEqual({});
});
});
describe('bru.getAllCollectionVars', () => {
test('returns a copy of all collection variables', () => {
const bru = makeBru({ a: '1', b: '2' });
const all = bru.getAllCollectionVars();
expect(all).toEqual({ a: '1', b: '2' });
});
test('returned object is a copy, not a reference', () => {
const bru = makeBru({ a: '1' });
const all = bru.getAllCollectionVars();
all.a = 'mutated';
expect(bru.collectionVariables.a).toBe('1');
});
test('returns empty object when no variables exist', () => {
const bru = makeBru();
expect(bru.getAllCollectionVars()).toEqual({});
});
});
});

View File

@@ -0,0 +1,207 @@
const Bru = require('../src/bru');
describe('Global Environment Variable APIs', () => {
const makeBru = (globalEnvironmentVariables = {}) =>
new Bru({
runtime: 'quickjs',
envVariables: {},
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test',
globalEnvironmentVariables
});
describe('bru.setGlobalEnvVar', () => {
test('sets a new global env variable', () => {
const bru = makeBru();
bru.setGlobalEnvVar('apiKey', 'abc123');
expect(bru.globalEnvironmentVariables.apiKey).toBe('abc123');
});
test('overwrites an existing variable', () => {
const bru = makeBru({ apiKey: 'old' });
bru.setGlobalEnvVar('apiKey', 'new');
expect(bru.globalEnvironmentVariables.apiKey).toBe('new');
});
test('allows non-string values', () => {
const bru = makeBru();
bru.setGlobalEnvVar('count', 42);
expect(bru.globalEnvironmentVariables.count).toBe(42);
bru.setGlobalEnvVar('config', { port: 3000 });
expect(bru.globalEnvironmentVariables.config).toEqual({ port: 3000 });
});
test('throws when key is empty', () => {
const bru = makeBru();
expect(() => bru.setGlobalEnvVar('', 'v')).toThrow(/without specifying a name/);
});
test('re-run: setting same key twice does not throw', () => {
const bru = makeBru();
bru.setGlobalEnvVar('key', 'first');
bru.setGlobalEnvVar('key', 'second');
expect(bru.globalEnvironmentVariables.key).toBe('second');
});
});
describe('bru.setGlobalEnvVar — dirty flag for typed values', () => {
test('setting a number trips the global-env dirty flag', () => {
const bru = makeBru();
expect(bru._globalEnvDirty).toBe(false);
bru.setGlobalEnvVar('count', 42);
expect(bru._globalEnvDirty).toBe(true);
});
test('setting a boolean trips the global-env dirty flag', () => {
const bru = makeBru();
bru.setGlobalEnvVar('active', true);
expect(bru._globalEnvDirty).toBe(true);
});
test('setting an object trips the global-env dirty flag', () => {
const bru = makeBru();
bru.setGlobalEnvVar('config', { port: 3000 });
expect(bru._globalEnvDirty).toBe(true);
});
});
describe('bru.setGlobalEnvVar — dirty flag for reference-mutation idiom', () => {
test('the getGlobalEnvVar → mutate → setGlobalEnvVar idiom trips the dirty flag', () => {
const bru = makeBru({ config: { port: 3000 } });
expect(bru._globalEnvDirty).toBe(false);
// Real-script idiom: getGlobalEnvVar deep-copies through interpolate's JSON roundtrip,
// so mutating the result leaves the store untouched and the deep-equal guard fires.
const config = bru.getGlobalEnvVar('config');
config.port = 4000;
bru.setGlobalEnvVar('config', config);
expect(bru._globalEnvDirty).toBe(true);
expect(bru.globalEnvironmentVariables.config).toEqual({ port: 4000 });
});
test('re-setting a structurally-equal object value does NOT trip the dirty flag', () => {
const bru = makeBru({ config: { port: 3000 } });
bru.setGlobalEnvVar('config', { port: 3000 });
expect(bru._globalEnvDirty).toBe(false);
});
test('re-setting a structurally-equal primitive value does NOT trip the dirty flag', () => {
const bru = makeBru({ token: 'abc' });
bru.setGlobalEnvVar('token', 'abc');
expect(bru._globalEnvDirty).toBe(false);
});
});
describe('bru.deleteGlobalEnvVar — dirty flag contract', () => {
test('deleting an existing key trips the global-env dirty flag', () => {
const bru = makeBru({ token: 'abc' });
bru.deleteGlobalEnvVar('token');
expect(bru._globalEnvDirty).toBe(true);
});
test('deleting a non-existent key leaves the dirty flag clean', () => {
const bru = makeBru();
bru.deleteGlobalEnvVar('missing');
expect(bru._globalEnvDirty).toBe(false);
});
});
describe('bru.deleteAllGlobalEnvVars — dirty flag contract', () => {
test('deleting populated scope trips the dirty flag', () => {
const bru = makeBru({ a: '1', b: '2' });
bru.deleteAllGlobalEnvVars();
expect(bru._globalEnvDirty).toBe(true);
});
test('calling on empty scope leaves the dirty flag clean', () => {
const bru = makeBru();
bru.deleteAllGlobalEnvVars();
expect(bru._globalEnvDirty).toBe(false);
});
});
describe('bru.getGlobalEnvVar', () => {
test('returns value of an existing variable', () => {
const bru = makeBru({ token: 'abc' });
expect(bru.getGlobalEnvVar('token')).toBe('abc');
});
test('returns undefined for non-existent variable', () => {
const bru = makeBru();
expect(bru.getGlobalEnvVar('missing')).toBeUndefined();
});
});
describe('bru.hasGlobalEnvVar', () => {
test('returns true for existing variable', () => {
const bru = makeBru({ token: 'abc' });
expect(bru.hasGlobalEnvVar('token')).toBe(true);
});
test('returns false for non-existent variable', () => {
const bru = makeBru();
expect(bru.hasGlobalEnvVar('missing')).toBe(false);
});
});
describe('bru.deleteGlobalEnvVar', () => {
test('removes an existing variable', () => {
const bru = makeBru({ token: 'abc' });
bru.deleteGlobalEnvVar('token');
expect(bru.globalEnvironmentVariables.token).toBeUndefined();
expect(bru.hasGlobalEnvVar('token')).toBe(false);
});
test('re-run: deleting a non-existent key is a silent no-op', () => {
const bru = makeBru();
expect(() => bru.deleteGlobalEnvVar('missing')).not.toThrow();
});
test('re-run: deleting an already-deleted key is a silent no-op', () => {
const bru = makeBru({ token: 'abc' });
bru.deleteGlobalEnvVar('token');
expect(() => bru.deleteGlobalEnvVar('token')).not.toThrow();
});
});
describe('bru.deleteAllGlobalEnvVars', () => {
test('removes all global env variables', () => {
const bru = makeBru({ a: '1', b: '2', c: '3' });
bru.deleteAllGlobalEnvVars();
expect(bru.globalEnvironmentVariables).toEqual({});
});
test('re-run: calling on empty scope is a silent no-op', () => {
const bru = makeBru();
expect(() => bru.deleteAllGlobalEnvVars()).not.toThrow();
expect(bru.globalEnvironmentVariables).toEqual({});
});
test('re-run: calling twice is a silent no-op', () => {
const bru = makeBru({ a: '1' });
bru.deleteAllGlobalEnvVars();
expect(() => bru.deleteAllGlobalEnvVars()).not.toThrow();
});
});
describe('bru.getAllGlobalEnvVars', () => {
test('returns a copy of all global env variables', () => {
const bru = makeBru({ a: '1', b: '2' });
expect(bru.getAllGlobalEnvVars()).toEqual({ a: '1', b: '2' });
});
test('returned object is a copy, not a reference', () => {
const bru = makeBru({ a: '1' });
const all = bru.getAllGlobalEnvVars();
all.a = 'mutated';
expect(bru.globalEnvironmentVariables.a).toBe('1');
});
test('returns empty object when no variables exist', () => {
const bru = makeBru();
expect(bru.getAllGlobalEnvVars()).toEqual({});
});
});
});

View File

@@ -180,61 +180,121 @@ describe('runtime', () => {
});
});
describe('persistent environment variables validation', () => {
it('should persist non-string values without throwing', async () => {
describe('environment variables from scripts', () => {
it('should allow any value type', async () => {
const script = `
bru.setEnvVar('number', 42, {persist: true});
bru.setEnvVar('isActive', true, {persist: true});
bru.setEnvVar('config', {port: 3000}, {persist: true});
bru.setEnvVar('items', ['item1', 'item2'], {persist: true});
`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.envVariables.number).toBe(42);
expect(result.persistentEnvVariables.number).toBe(42);
expect(result.envVariables.isActive).toBe(true);
expect(result.persistentEnvVariables.isActive).toBe(true);
expect(result.envVariables.config).toEqual({ port: 3000 });
expect(result.persistentEnvVariables.config).toEqual({ port: 3000 });
expect(result.envVariables.items).toEqual(['item1', 'item2']);
expect(result.persistentEnvVariables.items).toEqual(['item1', 'item2']);
});
it('should allow string values when persist is true', async () => {
const script = `bru.setEnvVar('api_key', 'abc123', {persist: true});`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.envVariables.api_key).toBe('abc123');
});
it('should allow non-string values when persist is false', async () => {
const script = `
bru.setEnvVar('number', 42, {persist: false});
bru.setEnvVar('boolean', true, {persist: false});
bru.setEnvVar('object', {key: 'value'}, {persist: false});
bru.setEnvVar('array', [1, 2, 3], {persist: false});
bru.setEnvVar('str', 'hello');
bru.setEnvVar('number', 42);
bru.setEnvVar('boolean', true);
bru.setEnvVar('object', {key: 'value'});
bru.setEnvVar('array', [1, 2, 3]);
`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.envVariables.str).toBe('hello');
expect(result.envVariables.number).toBe(42);
expect(result.envVariables.boolean).toBe(true);
expect(result.envVariables.object).toEqual({ key: 'value' });
expect(result.envVariables.array).toEqual([1, 2, 3]);
});
it('should allow non-string values when persist is not specified', async () => {
const script = `bru.setEnvVar('number', 42);`;
it('should preserve typed values through the QuickJS shim', async () => {
await quickJsLoader();
const script = `
bru.setEnvVar('num', 42);
bru.setEnvVar('bool', true);
bru.setEnvVar('obj', { key: 'value' });
bru.setCollectionVar('collNum', 7);
bru.setGlobalEnvVar('globalBool', false);
`;
const runtime = new ScriptRuntime({ runtime: 'quickjs' });
const onConsoleLog = () => {};
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', onConsoleLog, process.env);
expect(typeof result.envVariables.num).toBe('number');
expect(result.envVariables.num).toBe(42);
expect(typeof result.envVariables.bool).toBe('boolean');
expect(result.envVariables.bool).toBe(true);
expect(typeof result.envVariables.obj).toBe('object');
expect(result.envVariables.obj).toEqual({ key: 'value' });
expect(typeof result.collectionVariables.collNum).toBe('number');
expect(result.collectionVariables.collNum).toBe(7);
expect(typeof result.globalEnvironmentVariables.globalBool).toBe('boolean');
expect(result.globalEnvironmentVariables.globalBool).toBe(false);
});
it('should return null for scopes the script did not touch (dirty-flag gating)', async () => {
const script = `bru.setEnvVar('only_env', 'val');`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.envVariables.number).toBe(42);
expect(result.envVariables).not.toBeNull();
expect(result.envVariables.only_env).toBe('val');
expect(result.collectionVariables).toBeNull();
expect(result.globalEnvironmentVariables).toBeNull();
});
it('should return null for scopes the script did not touch — QuickJS parity', async () => {
await quickJsLoader();
const script = `bru.setEnvVar('only_env', 'val');`;
const runtime = new ScriptRuntime({ runtime: 'quickjs' });
const onConsoleLog = () => {};
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', onConsoleLog, process.env);
expect(result.envVariables).not.toBeNull();
expect(result.envVariables.only_env).toBe('val');
expect(result.collectionVariables).toBeNull();
expect(result.globalEnvironmentVariables).toBeNull();
});
it('should not include persistentEnvVariables in result', async () => {
const script = `bru.setEnvVar('key', 'val');`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result).not.toHaveProperty('persistentEnvVariables');
});
it('should include collectionVariables in result', async () => {
const script = `bru.setCollectionVar('myVar', 'myValue');`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result = await runtime.runRequestScript(script, {}, {}, {}, '.', null, process.env);
expect(result.collectionVariables).toBeDefined();
expect(result.collectionVariables.myVar).toBe('myValue');
});
it('should silently ignore old persist flag as extra argument', async () => {
const scriptTrue = `bru.setEnvVar('key1', 'val1', { persist: true });`;
const scriptFalse = `bru.setEnvVar('key2', 'val2', { persist: false });`;
const runtime = new ScriptRuntime({ runtime: 'nodevm' });
const result1 = await runtime.runRequestScript(scriptTrue, {}, {}, {}, '.', null, process.env);
expect(result1.envVariables.key1).toBe('val1');
const result2 = await runtime.runRequestScript(scriptFalse, {}, {}, {}, '.', null, process.env);
expect(result2.envVariables.key2).toBe('val2');
});
it('should silently ignore old persist flag as extra argument — QuickJS parity', async () => {
await quickJsLoader();
const scriptTrue = `bru.setEnvVar('key1', 'val1', { persist: true });`;
const scriptFalse = `bru.setEnvVar('key2', 'val2', { persist: false });`;
const runtime = new ScriptRuntime({ runtime: 'quickjs' });
const onConsoleLog = () => {};
const result1 = await runtime.runRequestScript(scriptTrue, {}, {}, {}, '.', onConsoleLog, process.env);
expect(result1.envVariables.key1).toBe('val1');
const result2 = await runtime.runRequestScript(scriptFalse, {}, {}, {}, '.', onConsoleLog, process.env);
expect(result2.envVariables.key2).toBe('val2');
});
});

View File

@@ -1,112 +1,220 @@
const Bru = require('../src/bru');
const { valueToString } = require('@usebruno/common/utils');
describe('Bru.setEnvVar', () => {
const makeBru = () =>
new Bru({
const makeBru = () =>
new Bru({
runtime: 'quickjs',
envVariables: {},
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test'
});
describe('bru.setEnvVar', () => {
test('sets envVariables[key] to value', () => {
const bru = makeBru();
bru.setEnvVar('token', 'abc123');
expect(bru.envVariables.token).toBe('abc123');
});
test('allows non-string values', () => {
const bru = makeBru();
bru.setEnvVar('count', 42);
expect(bru.envVariables.count).toBe(42);
bru.setEnvVar('active', true);
expect(bru.envVariables.active).toBe(true);
bru.setEnvVar('config', { port: 3000 });
expect(bru.envVariables.config).toEqual({ port: 3000 });
});
test('overwrites existing value', () => {
const bru = makeBru();
bru.setEnvVar('key', 'old');
bru.setEnvVar('key', 'new');
expect(bru.envVariables.key).toBe('new');
});
test('throws when key is empty', () => {
const bru = makeBru();
expect(() => bru.setEnvVar('', 'v')).toThrow(/without specifying a name/);
});
test('rejects key with invalid characters', () => {
const bru = makeBru();
expect(() => bru.setEnvVar('invalid key', 'v')).toThrow(/contains invalid characters/);
});
});
describe('bru.setEnvVar — dirty flag for typed values', () => {
test('setting a number trips the env dirty flag', () => {
const bru = makeBru();
expect(bru._envDirty).toBe(false);
bru.setEnvVar('count', 42);
expect(bru._envDirty).toBe(true);
});
test('setting a boolean trips the env dirty flag', () => {
const bru = makeBru();
bru.setEnvVar('active', true);
expect(bru._envDirty).toBe(true);
});
test('setting an object trips the env dirty flag', () => {
const bru = makeBru();
bru.setEnvVar('config', { port: 3000 });
expect(bru._envDirty).toBe(true);
});
});
describe('bru.deleteEnvVar', () => {
test('removes an existing variable', () => {
const bru = makeBru();
bru.setEnvVar('token', 'abc');
bru.deleteEnvVar('token');
expect(bru.envVariables.token).toBeUndefined();
});
test('deleting a non-existent key is a silent no-op', () => {
const bru = makeBru();
expect(() => bru.deleteEnvVar('missing')).not.toThrow();
});
test('does not delete the internal __name__ marker', () => {
const bru = new Bru({
runtime: 'quickjs',
envVariables: {},
envVariables: { __name__: 'dev', token: 'abc' },
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test'
});
test('updates envVariables and does not mark persistent when persist=false', () => {
const bru = makeBru();
bru.setEnvVar('non_persist', 'value', { persist: false });
expect(bru.envVariables.non_persist).toBe('value');
expect(bru.persistentEnvVariables.non_persist).toBeUndefined();
});
test('updates envVariables and tracks persistent when persist=true (string only)', () => {
const bru = makeBru();
bru.setEnvVar('persist_me', 'value', { persist: true });
expect(bru.envVariables.persist_me).toBe('value');
expect(bru.persistentEnvVariables.persist_me).toBe('value');
});
test('updates envVariables when options are omitted (defaults to non-persistent)', () => {
const bru = makeBru();
bru.setEnvVar('no_options', 'value');
expect(bru.envVariables.no_options).toBe('value');
expect(bru.persistentEnvVariables.no_options).toBeUndefined();
});
describe('persist=true with non-string values', () => {
test('stores numbers as-is without throwing', () => {
const bru = makeBru();
expect(() => bru.setEnvVar('n', 123, { persist: true })).not.toThrow();
expect(bru.envVariables.n).toBe(123);
expect(bru.persistentEnvVariables.n).toBe(123);
});
test('stores booleans as-is without throwing', () => {
const bru = makeBru();
expect(() => bru.setEnvVar('b', true, { persist: true })).not.toThrow();
expect(bru.persistentEnvVariables.b).toBe(true);
});
test('stores plain objects and arrays by reference without throwing', () => {
const bru = makeBru();
const obj = { a: 1 };
const arr = [1, 2, 3];
bru.setEnvVar('o', obj, { persist: true });
bru.setEnvVar('a', arr, { persist: true });
expect(bru.persistentEnvVariables.o).toBe(obj);
expect(bru.persistentEnvVariables.a).toBe(arr);
});
test('stores functions and symbols without throwing — but they round-trip to "" via valueToString', () => {
const bru = makeBru();
const fn = () => 42;
const sym = Symbol('s');
bru.setEnvVar('fn', fn, { persist: true });
bru.setEnvVar('sym', sym, { persist: true });
// Raw values land in persistentEnvVariables...
expect(bru.persistentEnvVariables.fn).toBe(fn);
expect(bru.persistentEnvVariables.sym).toBe(sym);
// ...but the serializer used by mergeAndPersistEnvironment produces ''
// for both, so the value is silently lost on the next save round-trip.
expect(valueToString(fn)).toBe('');
expect(valueToString(sym)).toBe('');
});
test('stores circular objects without throwing — but they round-trip to "" via valueToString', () => {
const bru = makeBru();
const circular = { a: 1 };
circular.self = circular;
bru.setEnvVar('c', circular, { persist: true });
expect(bru.persistentEnvVariables.c).toBe(circular);
// JSON.stringify throws on circulars; valueToString swallows that and returns ''.
expect(valueToString(circular)).toBe('');
});
});
test('changing existing key to non-persistent removes prior persisted entry', () => {
const bru = makeBru();
bru.setEnvVar('same_key', 'old', { persist: true });
expect(bru.persistentEnvVariables.same_key).toBe('old');
bru.setEnvVar('same_key', 'new');
expect(bru.envVariables.same_key).toBe('new');
expect(bru.persistentEnvVariables.same_key).toBeUndefined();
});
test('changing existing key to persistent updates persisted value', () => {
const bru = makeBru();
bru.setEnvVar('same_key', 'old');
expect(bru.persistentEnvVariables.same_key).toBeUndefined();
bru.setEnvVar('same_key', 'new', { persist: true });
expect(bru.envVariables.same_key).toBe('new');
expect(bru.persistentEnvVariables.same_key).toBe('new');
});
test('validates key name - invalid characters are rejected', () => {
const bru = makeBru();
expect(() => bru.setEnvVar('invalid key', 'v')).toThrow(/contains invalid characters/);
bru.deleteEnvVar('__name__');
expect(bru.envVariables.__name__).toBe('dev');
expect(bru._envDirty).toBe(false);
});
});
describe('bru.setEnvVar — dirty flag for reference-mutation idiom', () => {
test('the getEnvVar → mutate → setEnvVar idiom trips the dirty flag', () => {
const bru = new Bru({
runtime: 'quickjs',
envVariables: { config: { port: 3000 } },
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test'
});
expect(bru._envDirty).toBe(false);
// Real-script idiom: `const c = bru.getEnvVar('config'); c.port = 4000; bru.setEnvVar('config', c);`
// `getEnvVar` deep-copies through interpolate's JSON roundtrip, so mutating the result
// leaves envVariables.config untouched; the deep-equal guard then sees a real change.
// Pre-fix the strict-`!==` guard missed cases where the script structurally rebuilt the value.
const config = bru.getEnvVar('config');
config.port = 4000;
bru.setEnvVar('config', config);
expect(bru._envDirty).toBe(true);
expect(bru.envVariables.config).toEqual({ port: 4000 });
});
test('re-setting a structurally-equal object value does NOT trip the dirty flag', () => {
const bru = new Bru({
runtime: 'quickjs',
envVariables: { config: { port: 3000 } },
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test'
});
bru.setEnvVar('config', { port: 3000 });
expect(bru._envDirty).toBe(false);
});
test('re-setting a structurally-equal primitive value does NOT trip the dirty flag', () => {
const bru = new Bru({
runtime: 'quickjs',
envVariables: { token: 'abc' },
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test'
});
bru.setEnvVar('token', 'abc');
expect(bru._envDirty).toBe(false);
});
});
describe('bru.deleteEnvVar — dirty flag contract', () => {
test('deleting an existing key trips the env dirty flag', () => {
const bru = makeBru();
bru.setEnvVar('token', 'abc');
bru._envDirty = false; // reset post-set
bru.deleteEnvVar('token');
expect(bru._envDirty).toBe(true);
});
test('deleting a non-existent key leaves the dirty flag clean', () => {
const bru = makeBru();
bru.deleteEnvVar('missing');
expect(bru._envDirty).toBe(false);
});
});
describe('bru.deleteAllEnvVars — dirty flag contract', () => {
test('deleting populated env trips the dirty flag', () => {
const bru = new Bru({
runtime: 'quickjs',
envVariables: { a: '1', b: '2' },
runtimeVariables: {},
processEnvVars: {},
collectionPath: '/',
collectionName: 'Test'
});
bru.deleteAllEnvVars();
expect(bru._envDirty).toBe(true);
});
test('calling on empty env leaves the dirty flag clean', () => {
const bru = makeBru();
bru.deleteAllEnvVars();
expect(bru._envDirty).toBe(false);
});
});
describe('bru.deleteAll* methods — resilient to user-shadowed Object.prototype methods', () => {
// Use Object.hasOwn (not the property accessor) to check deletion, since after
// delete the prototype's hasOwnProperty becomes visible again on a plain object.
test('deleteAllEnvVars works when a var named "hasOwnProperty" was set', () => {
const bru = makeBru();
bru.setEnvVar('hasOwnProperty', 'shadow');
bru.setEnvVar('other', 'value');
expect(() => bru.deleteAllEnvVars()).not.toThrow();
expect(Object.hasOwn(bru.envVariables, 'hasOwnProperty')).toBe(false);
expect(Object.hasOwn(bru.envVariables, 'other')).toBe(false);
});
test('deleteAllGlobalEnvVars works when a var named "hasOwnProperty" was set', () => {
const bru = makeBru();
bru.setGlobalEnvVar('hasOwnProperty', 'shadow');
bru.setGlobalEnvVar('other', 'value');
expect(() => bru.deleteAllGlobalEnvVars()).not.toThrow();
expect(Object.hasOwn(bru.globalEnvironmentVariables, 'hasOwnProperty')).toBe(false);
});
test('deleteAllCollectionVars works when a var named "hasOwnProperty" was set', () => {
const bru = makeBru();
bru.setCollectionVar('hasOwnProperty', 'shadow');
bru.setCollectionVar('other', 'value');
expect(() => bru.deleteAllCollectionVars()).not.toThrow();
expect(Object.hasOwn(bru.collectionVariables, 'hasOwnProperty')).toBe(false);
});
test('deleteAllVars works when a runtime var named "hasOwnProperty" was set', () => {
const bru = makeBru();
bru.setVar('hasOwnProperty', 'shadow');
bru.setVar('other', 'value');
expect(() => bru.deleteAllVars()).not.toThrow();
expect(Object.hasOwn(bru.runtimeVariables, 'hasOwnProperty')).toBe(false);
});
});

View File

@@ -12,4 +12,4 @@ get {
script:pre-request {
bru.runner.stopExecution();
}
}

View File

@@ -11,9 +11,6 @@ get {
}
script:pre-request {
// TODO: skipped because deleteAllCollectionVars does not update the UI
bru.runner.skipRequest();
return;
bru.setCollectionVar("testDelAllCollectionA", "a");
bru.setCollectionVar("testDelAllCollectionB", "b");
}
@@ -29,6 +26,12 @@ tests {
expect(valB).to.be.undefined;
});
test("should not throw when called on empty scope", function() {
expect(function() {
bru.deleteAllCollectionVars();
}).to.not.throw();
});
// Restore collection vars for subsequent requests
for (const [key, value] of Object.entries(savedCollectionVars)) {
bru.setCollectionVar(key, value);

View File

@@ -28,6 +28,12 @@ tests {
expect(envName).to.equal("Prod");
});
test("should not throw when called on empty scope", function() {
expect(function() {
bru.deleteAllEnvVars();
}).to.not.throw();
});
// Restore env vars for subsequent requests
for (const [key, value] of Object.entries(savedEnvVars)) {
bru.setEnvVar(key, value);

View File

@@ -11,9 +11,6 @@ get {
}
script:pre-request {
// TODO: skipped because deleteAllGlobalEnvVars does not update the UI
bru.runner.skipRequest();
return;
bru.setGlobalEnvVar("testDelAllGlobalA", "a");
bru.setGlobalEnvVar("testDelAllGlobalB", "b");
}
@@ -29,6 +26,12 @@ tests {
expect(valB).to.be.undefined;
});
test("should not throw when called on empty scope", function() {
expect(function() {
bru.deleteAllGlobalEnvVars();
}).to.not.throw();
});
// Restore global env vars for subsequent requests
for (const [key, value] of Object.entries(savedGlobalEnvVars)) {
bru.setGlobalEnvVar(key, value);

View File

@@ -11,10 +11,10 @@ get {
}
script:pre-request {
// TODO: skipped because deleteCollectionVar does not update the UI
bru.runner.skipRequest();
return;
bru.setCollectionVar("testDeleteCollectionVar", "to-be-deleted");
}
script:post-response {
bru.deleteCollectionVar("testDeleteCollectionVar");
}
@@ -23,4 +23,10 @@ tests {
const val = bru.getCollectionVar("testDeleteCollectionVar");
expect(val).to.be.undefined;
});
test("should not throw when deleting non-existent key", function() {
expect(function() {
bru.deleteCollectionVar("non-existent-key");
}).to.not.throw();
});
}

View File

@@ -0,0 +1,32 @@
meta {
name: deleteEnvVar
type: http
seq: 30
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:pre-request {
bru.setEnvVar("testDeleteEnvVar", "to-be-deleted");
}
script:post-response {
bru.deleteEnvVar("testDeleteEnvVar");
}
tests {
test("should delete env var", function() {
const val = bru.getEnvVar("testDeleteEnvVar");
expect(val).to.be.undefined;
});
test("should not throw when deleting non-existent key", function() {
expect(function() {
bru.deleteEnvVar("non-existent-key");
}).to.not.throw();
});
}

View File

@@ -11,10 +11,10 @@ get {
}
script:pre-request {
// TODO: skipped because deleteGlobalEnvVar does not update the UI
bru.runner.skipRequest();
return;
bru.setGlobalEnvVar("testDeleteGlobalEnvVar", "to-be-deleted");
}
script:post-response {
bru.deleteGlobalEnvVar("testDeleteGlobalEnvVar");
}
@@ -23,4 +23,10 @@ tests {
const val = bru.getGlobalEnvVar("testDeleteGlobalEnvVar");
expect(val).to.be.undefined;
});
test("should not throw when deleting non-existent key", function() {
expect(function() {
bru.deleteGlobalEnvVar("non-existent-key");
}).to.not.throw();
});
}

View File

@@ -11,9 +11,6 @@ get {
}
script:pre-request {
// TODO: skipped because getAllCollectionVars does not update the UI
bru.runner.skipRequest();
return;
bru.setCollectionVar("testCollectionA", "valueA");
bru.setCollectionVar("testCollectionB", "valueB");
}

View File

@@ -10,11 +10,6 @@ get {
auth: none
}
script:pre-request {
// TODO: skipped because setCollectionVar does not update the UI
bru.runner.skipRequest();
}
script:post-response {
bru.setCollectionVar("testSetCollectionVar", "collection-test-value")
}

View File

@@ -0,0 +1,28 @@
meta {
name: setGlobalEnvVar
type: http
seq: 31
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:post-response {
bru.setGlobalEnvVar("testSetGlobalEnvVar", "global-test-value");
}
tests {
test("should set global env var in scripts", function() {
const val = bru.getGlobalEnvVar("testSetGlobalEnvVar");
expect(val).to.equal("global-test-value");
});
test("should overwrite existing global env var", function() {
bru.setGlobalEnvVar("testSetGlobalEnvVar", "overwritten");
const val = bru.getGlobalEnvVar("testSetGlobalEnvVar");
expect(val).to.equal("overwritten");
});
}