From 39a6fc837d08648e3c5cf5b9e5c2171dc17d2828 Mon Sep 17 00:00:00 2001 From: Sanjai Kumar <161328623+sanjaikumar-bruno@users.noreply.github.com> Date: Thu, 8 Jan 2026 20:00:43 +0530 Subject: [PATCH] fix: Handle deleted environment variables in UI (#6703) * fix: enhance environment variable management in collections slice * test: refactor deleteEnvVar test --- .../ReduxStore/slices/collections/index.js | 23 ++++++++++++++ .../api-deleteEnvVar/api-deleteEnvVar.spec.ts | 31 +++++++++++++++++++ .../fixtures/collection/api-deleteEnvVar.bru | 20 ++++++++++++ .../fixtures/collection/bruno.json | 6 ++++ .../collection/environments/Stage.bru | 4 +++ .../init-user-data/collection-security.json | 10 ++++++ .../init-user-data/preferences.json | 6 ++++ 7 files changed, 100 insertions(+) create mode 100644 tests/environments/api-deleteEnvVar/api-deleteEnvVar.spec.ts create mode 100644 tests/environments/api-deleteEnvVar/fixtures/collection/api-deleteEnvVar.bru create mode 100644 tests/environments/api-deleteEnvVar/fixtures/collection/bruno.json create mode 100644 tests/environments/api-deleteEnvVar/fixtures/collection/environments/Stage.bru create mode 100644 tests/environments/api-deleteEnvVar/init-user-data/collection-security.json create mode 100644 tests/environments/api-deleteEnvVar/init-user-data/preferences.json diff --git a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js index 366566192..68c0e12ad 100644 --- a/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js +++ b/packages/bruno-app/src/providers/ReduxStore/slices/collections/index.js @@ -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; diff --git a/tests/environments/api-deleteEnvVar/api-deleteEnvVar.spec.ts b/tests/environments/api-deleteEnvVar/api-deleteEnvVar.spec.ts new file mode 100644 index 000000000..4a0e159b7 --- /dev/null +++ b/tests/environments/api-deleteEnvVar/api-deleteEnvVar.spec.ts @@ -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); + }); + }); +}); diff --git a/tests/environments/api-deleteEnvVar/fixtures/collection/api-deleteEnvVar.bru b/tests/environments/api-deleteEnvVar/fixtures/collection/api-deleteEnvVar.bru new file mode 100644 index 000000000..5916443a6 --- /dev/null +++ b/tests/environments/api-deleteEnvVar/fixtures/collection/api-deleteEnvVar.bru @@ -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"); +} + diff --git a/tests/environments/api-deleteEnvVar/fixtures/collection/bruno.json b/tests/environments/api-deleteEnvVar/fixtures/collection/bruno.json new file mode 100644 index 000000000..d7898e527 --- /dev/null +++ b/tests/environments/api-deleteEnvVar/fixtures/collection/bruno.json @@ -0,0 +1,6 @@ +{ + "version": "1", + "name": "collection", + "type": "collection" +} + diff --git a/tests/environments/api-deleteEnvVar/fixtures/collection/environments/Stage.bru b/tests/environments/api-deleteEnvVar/fixtures/collection/environments/Stage.bru new file mode 100644 index 000000000..59e5c1b12 --- /dev/null +++ b/tests/environments/api-deleteEnvVar/fixtures/collection/environments/Stage.bru @@ -0,0 +1,4 @@ +vars { + host: https://testbench-sanity.usebruno.com +} + diff --git a/tests/environments/api-deleteEnvVar/init-user-data/collection-security.json b/tests/environments/api-deleteEnvVar/init-user-data/collection-security.json new file mode 100644 index 000000000..a21c4a268 --- /dev/null +++ b/tests/environments/api-deleteEnvVar/init-user-data/collection-security.json @@ -0,0 +1,10 @@ +{ + "collections": [ + { + "path": "{{projectRoot}}/tests/environments/api-deleteEnvVar/fixtures/collection", + "securityConfig": { + "jsSandboxMode": "safe" + } + } + ] +} diff --git a/tests/environments/api-deleteEnvVar/init-user-data/preferences.json b/tests/environments/api-deleteEnvVar/init-user-data/preferences.json new file mode 100644 index 000000000..f73e81920 --- /dev/null +++ b/tests/environments/api-deleteEnvVar/init-user-data/preferences.json @@ -0,0 +1,6 @@ +{ + "maximized": false, + "lastOpenedCollections": [ + "{{projectRoot}}/tests/environments/api-deleteEnvVar/fixtures/collection" + ] +}