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"], + }, +});