fix: Handle deleted environment variables in UI (#6703)

* fix: enhance environment variable management in collections slice

* test: refactor deleteEnvVar test
This commit is contained in:
Sanjai Kumar
2026-01-08 20:00:43 +05:30
committed by GitHub
parent 5b1b1b5541
commit 39a6fc837d
7 changed files with 100 additions and 0 deletions

View File

@@ -334,6 +334,9 @@ export const collectionsSlice = createSlice({
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;
@@ -369,6 +372,26 @@ export const collectionsSlice = createSlice({
}
}
});
// 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;
});
}
collection.runtimeVariables = runtimeVariables;

View File

@@ -0,0 +1,31 @@
import { test, expect } from '../../../playwright';
import { sendRequest, openRequest, selectEnvironment, openEnvironmentSelector, closeEnvironmentPanel, closeAllCollections } from '../../utils/page';
test.describe.serial('bru.deleteEnvVar(name)', () => {
test('should remove ephemeral variable from UI after deletion', async ({ pageWithUserData: page }) => {
await test.step('Open request and select environment', async () => {
await openRequest(page, 'collection', 'api-deleteEnvVar');
await selectEnvironment(page, 'Stage');
});
await test.step('Send request to set and delete variable', async () => {
await sendRequest(page, 200);
});
await test.step('Verify variable is removed from UI', async () => {
await openEnvironmentSelector(page, 'collection');
await page.getByText('Configure', { exact: true }).click();
const envTab = page.locator('.request-tab').filter({ hasText: 'Environments' });
await expect(envTab).toBeVisible();
await expect(page.getByRole('row', { name: 'host' })).toBeVisible();
await expect(page.getByRole('row', { name: 'tempToken' })).not.toBeVisible();
});
await test.step('Cleanup', async () => {
await closeEnvironmentPanel(page, 'collection');
await closeAllCollections(page);
});
});
});

View File

@@ -0,0 +1,20 @@
meta {
name: api-deleteEnvVar
type: http
seq: 1
}
get {
url: {{host}}/ping
body: none
auth: none
}
script:pre-request {
bru.setEnvVar("tempToken", "abc123");
}
script:post-response {
bru.deleteEnvVar("tempToken");
}

View File

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

View File

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

View File

@@ -0,0 +1,10 @@
{
"collections": [
{
"path": "{{projectRoot}}/tests/environments/api-deleteEnvVar/fixtures/collection",
"securityConfig": {
"jsSandboxMode": "safe"
}
}
]
}

View File

@@ -0,0 +1,6 @@
{
"maximized": false,
"lastOpenedCollections": [
"{{projectRoot}}/tests/environments/api-deleteEnvVar/fixtures/collection"
]
}