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 <simonsmh@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-08-31 15:57:41 +00:00
co-authored by Simon Shi
parent e98526a8cd
commit 1dd1b0988b
3 changed files with 39 additions and 0 deletions
+17
View File
@@ -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 });
+15
View File
@@ -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: <JSON string>, statusCodeValue, statusCode }
+7
View File
@@ -0,0 +1,7 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
test: {
setupFiles: ["./src/__tests__/setup.ts"],
},
});