From 142194550cb448cae7baa1102cf45e2427dcef62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20Sol=C3=A1r?= Date: Thu, 10 Sep 2026 14:58:08 +0200 Subject: [PATCH] fix: isolate detectAiAgent test from ambient environment The "returns undefined when no agent env vars are set" case relied on the ambient environment being clean. It failed deterministically for anyone running the suite from inside Claude Code, Cursor, or a similar tool, since those set CLAUDECODE and friends. CI is green because it sets none of them. Stub all nine agent env vars to empty in beforeEach and restore them with vi.unstubAllEnvs. The assertion is unchanged. Co-Authored-By: Claude Opus 5 --- .../hooks/telemetry/detectEnvironment.test.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/test/lib/hooks/telemetry/detectEnvironment.test.ts b/test/lib/hooks/telemetry/detectEnvironment.test.ts index ac0ddb408..08398bff8 100644 --- a/test/lib/hooks/telemetry/detectEnvironment.test.ts +++ b/test/lib/hooks/telemetry/detectEnvironment.test.ts @@ -17,24 +17,30 @@ describe('detectAiAgent', () => { 'OPENCLAW_SHELL', ]; - afterEach(() => { + // The suite may run inside an AI coding tool, which sets some of these, + // so clear them all instead of relying on the ambient environment. + beforeEach(() => { for (const key of agentEnvVars) { - delete process.env[key]; + vi.stubEnv(key, ''); } }); + afterEach(() => { + vi.unstubAllEnvs(); + }); + test('returns undefined when no agent env vars are set', () => { expect(detectAiAgent()).toBeUndefined(); }); test('returns correct agent for a known env var', () => { - process.env.GEMINI_CLI = '1'; + vi.stubEnv('GEMINI_CLI', '1'); expect(detectAiAgent()).toBe('gemini_cli'); }); test('returns first match when multiple agent env vars are set', () => { - process.env.CURSOR_AGENT = '1'; - process.env.GEMINI_CLI = '1'; + vi.stubEnv('CURSOR_AGENT', '1'); + vi.stubEnv('GEMINI_CLI', '1'); // CURSOR_AGENT appears before GEMINI_CLI in the lookup table expect(detectAiAgent()).toBe('cursor');