From 1dd1b0988b012dfc56c0467d531bc23bd949a495 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Mon, 31 Aug 2026 15:57:41 +0000 Subject: [PATCH] test: isolate suites from the real ~/.pi and from each other Several suites read and write ~/.pi/agent/{auth,qoder-models-cache}.json. Vitest runs files in parallel, so one file could truncate a file another was reading, and a run also clobbered the developer's own ~/.pi state. Give each test file its own HOME. Isolation exposed a latent coupling in the stream suite: with no resolved identity on disk, streamQoder fetches /userinfo first and consumes the single mocked Response, so the chat read failed on a locked stream. The suite only passed because another file left credentials behind. Stub the identity so the mock only ever serves the chat request. Co-authored-by: Simon Shi --- src/__tests__/setup.ts | 17 +++++++++++++++++ src/__tests__/stream.test.ts | 15 +++++++++++++++ vitest.config.ts | 7 +++++++ 3 files changed, 39 insertions(+) create mode 100644 src/__tests__/setup.ts create mode 100644 vitest.config.ts diff --git a/src/__tests__/setup.ts b/src/__tests__/setup.ts new file mode 100644 index 0000000..bb12aa5 --- /dev/null +++ b/src/__tests__/setup.ts @@ -0,0 +1,17 @@ +import { mkdirSync, mkdtempSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +/** + * Give every test file its own HOME. + * + * The provider stores credentials and the model catalog under + * `~/.pi/agent/*.json`, and several suites write those paths. Vitest runs test + * files in parallel, so a shared HOME let one file truncate a file another was + * reading (an intermittent failure in the stream suite). A per-file HOME also + * keeps the suite from touching the real `~/.pi` of whoever runs it. + */ +const home = mkdtempSync(join(tmpdir(), "pi-provider-qoder-test-")); +process.env.HOME = home; +process.env.USERPROFILE = home; +mkdirSync(join(home, ".pi", "agent"), { recursive: true }); diff --git a/src/__tests__/stream.test.ts b/src/__tests__/stream.test.ts index 7acc3de..ecc9ed8 100644 --- a/src/__tests__/stream.test.ts +++ b/src/__tests__/stream.test.ts @@ -10,6 +10,21 @@ import type { import { afterEach, describe, expect, it, vi } from "vitest"; import { streamQoder } from "../stream.js"; +// Pin the identity so the mocked fetch below only ever serves the chat request. +// Without a resolved identity, streamQoder fetches /userinfo first and consumes +// the mock response, leaving the chat read to fail on a locked stream. +vi.mock("../oauth.js", () => ({ + resolveQoderIdentity: vi.fn().mockResolvedValue({ + access: "fake", + userID: "test-user", + email: "test@example.com", + name: "Test User", + machineID: "test-machine", + refresh: "", + expires: 0, + }), +})); + /** * Build a single SSE `data:` line carrying a Qoder envelope: * { headers, body: , statusCodeValue, statusCode } diff --git a/vitest.config.ts b/vitest.config.ts new file mode 100644 index 0000000..e64c5cf --- /dev/null +++ b/vitest.config.ts @@ -0,0 +1,7 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + setupFiles: ["./src/__tests__/setup.ts"], + }, +});