From 79320721bf373fb08bd05218a131874999da0194 Mon Sep 17 00:00:00 2001 From: "posthog[bot]" <206114724+posthog[bot]@users.noreply.github.com> Date: Sun, 27 Sep 2026 17:55:17 +0000 Subject: [PATCH] test: stop unit tests from sending events to production PostHog Mock posthog-node in a global vitest setup file, so the analytics singleton and other clients never reach production error tracking from a test run. The real client's exception autocapture hid an unhandled rejection in the pi check_env_keys test. The test now handles the promise it creates. Co-Authored-By: Claude Opus 5.5 Generated-By: PostHog Desktop Task-Id: bcd9fae6-64f8-45bc-9515-41ba7a7bf497 --- src/agent/runner/harness/pi/__tests__/tools.test.ts | 13 ++++++++----- tsconfig.json | 1 + vitest.config.ts | 1 + vitest.setup.ts | 4 ++++ 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 vitest.setup.ts diff --git a/src/agent/runner/harness/pi/__tests__/tools.test.ts b/src/agent/runner/harness/pi/__tests__/tools.test.ts index 0fa075634..08cb94249 100644 --- a/src/agent/runner/harness/pi/__tests__/tools.test.ts +++ b/src/agent/runner/harness/pi/__tests__/tools.test.ts @@ -236,11 +236,14 @@ describe('pi check_env_keys — failures arrive as rejections', () => { // instead of arriving as a failed tool call. Dropping `async` here — the // scan removed the last `await` — is what made that reachable. const { checkEnvKeys } = makeTools({}); - const run = () => - call(checkEnvKeys, { keys: ['ANY'], filePath: '../../etc/passwd' }); - - expect(run).not.toThrow(); - return expect(run()).rejects.toThrow('Path traversal rejected'); + let result: Promise | undefined; + expect(() => { + result = call(checkEnvKeys, { + keys: ['ANY'], + filePath: '../../etc/passwd', + }); + }).not.toThrow(); + return expect(result).rejects.toThrow('Path traversal rejected'); }); it('answers normally for a path inside the working directory', async () => { diff --git a/tsconfig.json b/tsconfig.json index 9947307f6..f2a7c51d7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -15,6 +15,7 @@ "bin.ts", "tsdown.config.ts", "vitest.config.ts", + "vitest.setup.ts", "spec/**/*", "src/**/*", "test/**/*", diff --git a/vitest.config.ts b/vitest.config.ts index 16492b363..09463d143 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -100,6 +100,7 @@ export default defineConfig({ test: { globals: true, environment: 'node', + setupFiles: [r('vitest.setup.ts')], projects: [ project('agent', AGENT_TESTS), project('programs', PROGRAM_TESTS), diff --git a/vitest.setup.ts b/vitest.setup.ts new file mode 100644 index 000000000..00857e2e8 --- /dev/null +++ b/vitest.setup.ts @@ -0,0 +1,4 @@ +import { vi } from 'vitest'; + +// Tests must never send events or exceptions to production PostHog. +vi.mock('posthog-node');