Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

test(with-env): overlapping calls restore the value from before the first, not one they set

garrytan5e9c24d4d8c5
diff --git a/test/helpers/with-env.test.ts b/test/helpers/with-env.test.ts
index 56626e99f0..6a05739c47 100644
--- a/test/helpers/with-env.test.ts
+++ b/test/helpers/with-env.test.ts
@@ -92,4 +92,24 @@ describe('withEnv', () => {
for (const tz of ['Asia/Tokyo', 'America/Los_Angeles', 'UTC']) await withEnv({ TZ: tz }, () => expect(zone()).toBe(tz));
expect(zone()).toBe(before);
});
+
+ test('overlapping calls that finish out of order leave the key as it was before either', async () => {
+ delete process.env[KEY];
+ let release!: () => void;
+ const gate = new Promise<void>(resolve => { release = resolve; });
+ const first = withEnv({ [KEY]: 'first' }, () => gate);
+ const second = withEnv({ [KEY]: 'second' }, async () => { release(); await gate; });
+ await first;
+ expect(process.env[KEY]).toBe('second');
+ await second;
+ expect(process.env[KEY]).toBeUndefined();
+ });
+
+ test('un-awaited calls back to back leave the key as it was', async () => {
+ process.env[KEY] = 'original';
+ const calls = [withEnv({ [KEY]: 'a' }, () => {}), withEnv({ [KEY]: 'b' }, () => {})];
+ await Promise.all(calls);
+ expect(process.env[KEY]).toBe('original');
+ delete process.env[KEY];
+ });
});
diff --git a/test/helpers/with-env.ts b/test/helpers/with-env.ts
index 04651e28a5..8485d686f1 100644
--- a/test/helpers/with-env.ts
+++ b/test/helpers/with-env.ts
@@ -45,34 +45,42 @@ import { join } from 'node:path';
* expect(process.env.K).toBe('outer');
* });
*/
+type EnvFrame = { prior: string | undefined };
+const activeFrames = new Map<string, EnvFrame[]>();
+
+function setEnv(key: string, value: string | undefined): void {
+ if (value === undefined) delete process.env[key];
+ else process.env[key] = value;
+}
+
export async function withEnv<T>(
overrides: Record<string, string | undefined>,
fn: () => T | Promise<T>,
): Promise<T> {
- const keys = Object.keys(overrides);
- const prior: Record<string, string | undefined> = {};
- for (const key of keys) {
- prior[key] = process.env[key];
- }
- // Bun keeps the last explicitly set zone when TZ is deleted (bun test pins
- // UTC without setting TZ), so restore an absent TZ as the zone in effect.
- if ('TZ' in overrides && prior.TZ === undefined) prior.TZ = Intl.DateTimeFormat().resolvedOptions().timeZone;
+ const frames = Object.keys(overrides).map(key => {
+ // Bun keeps the last explicitly set zone when TZ is deleted (bun test pins
+ // UTC without setting TZ), so restore an absent TZ as the zone in effect.
+ const prior = key === 'TZ' && process.env.TZ === undefined ? Intl.DateTimeFormat().resolvedOptions().timeZone : process.env[key];
+ const frame: EnvFrame = { prior };
+ const stack = activeFrames.get(key) ?? [];
+ stack.push(frame);
+ activeFrames.set(key, stack);
+ return [key, stack, frame] as const;
+ });
try {
- for (const [key, value] of Object.entries(overrides)) {
- if (value === undefined) {
- delete process.env[key];
- } else {
- process.env[key] = value;
- }
- }
+ for (const [key, value] of Object.entries(overrides)) setEnv(key, value);
return await fn();
} finally {
- for (const [key, value] of Object.entries(prior)) {
- if (value === undefined) {
- delete process.env[key];
- } else {
- process.env[key] = value;
- }
+ // Overlapping calls (Promise.all, an un-awaited withEnv) can finish out of
+ // order. The last frame restores; an earlier one hands its prior to the
+ // frame that captured its override, so the key ends at the value it had
+ // before the first call instead of a value one of them set.
+ for (const [key, stack, frame] of frames) {
+ const at = stack.indexOf(frame);
+ if (at === stack.length - 1) setEnv(key, frame.prior);
+ else stack[at + 1]!.prior = frame.prior;
+ stack.splice(at, 1);
+ if (stack.length === 0) activeFrames.delete(key);
}
}
}
diff --git a/test/postgres-inflight-budget-6543.test.ts b/test/postgres-inflight-budget-6543.test.ts
index 3e6858b3fb..24881735e5 100644
--- a/test/postgres-inflight-budget-6543.test.ts
+++ b/test/postgres-inflight-budget-6543.test.ts
@@ -52,14 +52,14 @@ describe('statementTimeoutChanges: what one statement does to the connection sta
});
describe('poolHealthOptions: one budget caps both silence and total age', () => {
- test('inflight_max equals inflight_timeout, inflight_grace is the grace in seconds, and grace off disables both', () => {
- withEnv({ GBRAIN_PG_STUCK_GRACE_MS: '2000' }, () => {
+ test('inflight_max equals inflight_timeout, inflight_grace is the grace in seconds, and grace off disables both', async () => {
+ await withEnv({ GBRAIN_PG_STUCK_GRACE_MS: '2000' }, () => {
const options = poolHealthOptions(undefined, '3000');
expect(options.inflight_timeout).toBe(5);
expect(options.inflight_max).toBe(5);
expect(options.inflight_grace).toBe(2);
});
- withEnv({ GBRAIN_PG_STUCK_GRACE_MS: 'off' }, () => {
+ await withEnv({ GBRAIN_PG_STUCK_GRACE_MS: 'off' }, () => {
const options = poolHealthOptions(undefined, '3000');
expect(options.inflight_timeout).toBeNull();
expect(options.inflight_max).toBeNull();