test: add redacted Qoder protocol recorder and replays

Co-authored-by: Simon Shi <simonsmh@users.noreply.github.com>
This commit is contained in:
Cursor Agent
2026-08-31 16:14:37 +00:00
co-authored by Simon Shi
parent 1eee02455c
commit 6d5cdfd171
14 changed files with 860 additions and 11 deletions
+49
View File
@@ -0,0 +1,49 @@
import { readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
export interface FixtureHttpMessage {
method?: string;
url?: string;
headers: Record<string, string>;
body: unknown;
}
export interface FixtureInteraction {
request: FixtureHttpMessage;
response: {
status: number;
headers: Record<string, string>;
body: unknown;
};
}
export interface LiveProtocolFixture {
formatVersion: 1;
source: "sample" | "recorded";
region: "global" | "cn";
recordedAt: string | null;
interactions: {
patExchange: FixtureInteraction;
userinfo: FixtureInteraction;
modelList: FixtureInteraction;
chat: FixtureInteraction;
};
}
const fixtureDir = join(dirname(fileURLToPath(import.meta.url)), "..", "__fixtures__", "live");
export function loadLiveFixture(region: "global" | "cn" = "global"): LiveProtocolFixture {
return JSON.parse(readFileSync(join(fixtureDir, `sample-${region}.json`), "utf8")) as LiveProtocolFixture;
}
export function responseFromFixture(interaction: FixtureInteraction): Response {
const body =
typeof interaction.response.body === "string"
? interaction.response.body
: JSON.stringify(interaction.response.body);
return new Response(body, {
status: interaction.response.status,
headers: interaction.response.headers,
});
}
+42
View File
@@ -0,0 +1,42 @@
import { describe, expect, it } from "vitest";
import { loadLiveFixture } from "./live-fixture.js";
describe.each(["global", "cn"] as const)("%s protocol replay fixture", (region) => {
const fixture = loadLiveFixture(region);
it("contains all live recorder protocol stages", () => {
expect(fixture).toMatchObject({
formatVersion: 1,
source: "sample",
region,
recordedAt: null,
});
expect(Object.keys(fixture.interactions).sort()).toEqual(["chat", "modelList", "patExchange", "userinfo"]);
});
it("preserves the Qoder SSE envelope with a JSON-string body", () => {
const body = fixture.interactions.chat.response.body;
expect(typeof body).toBe("string");
const dataLines = (body as string).split("\n").filter((line) => line.startsWith("data:"));
expect(dataLines.length).toBeGreaterThanOrEqual(3);
const firstEnvelope = JSON.parse(dataLines[0].slice(5)) as {
body: string;
statusCodeValue: number;
};
expect(firstEnvelope.statusCodeValue).toBe(200);
expect(typeof firstEnvelope.body).toBe("string");
expect(() => JSON.parse(firstEnvelope.body)).not.toThrow();
expect(dataLines.some((line) => line.includes('"body":"[DONE]"'))).toBe(true);
});
it("contains placeholders, not credential-shaped values", () => {
const serialized = JSON.stringify(fixture);
expect(serialized).toContain("<redacted:");
expect(serialized).not.toMatch(/\b(?:pt|jt|jrt)-[A-Za-z0-9._~-]{4,}\b/);
expect(serialized).not.toMatch(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i);
expect(serialized).not.toMatch(/Bearer\s+COSY\./i);
expect(serialized).not.toMatch(/"authorization"\s*:/i);
expect(serialized).not.toMatch(/"cookie"\s*:/i);
});
});
+12
View File
@@ -3,6 +3,7 @@ import { homedir } from "node:os";
import { join } from "node:path";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { getCachedModels, updateQoderModelsCache } from "../models.js";
import { loadLiveFixture, responseFromFixture } from "./live-fixture.js";
const CACHE_PATH = join(homedir(), ".pi", "agent", "qoder-models-cache.json");
let originalCache: string | undefined;
@@ -19,6 +20,17 @@ afterEach(() => {
});
describe("Qoder model cache", () => {
it("replays the recorded-format model-list schema", async () => {
const interaction = loadLiveFixture("global").interactions.modelList;
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(responseFromFixture(interaction)));
await updateQoderModelsCache("access-token", "user-id", "Test User", "test@example.com", "global");
const cache = JSON.parse(readFileSync(CACHE_PATH, "utf8"));
expect(cache.models.map((model: { id: string }) => model.id)).toEqual(["lite", "gm51model"]);
expect(cache.models.map((model: { contextWindow: number }) => model.contextWindow)).toEqual([1_000_000, 200_000]);
});
it("keeps only enabled service models without adding auto as a fallback", async () => {
vi.stubGlobal(
"fetch",
+30
View File
@@ -5,6 +5,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { updateQoderModelsCache } from "../models.js";
import { autoLoginQoderFromEnvironment, getCachedCredentials, getQoderPatForMode } from "../oauth.js";
import { credentialsFromPat } from "../pat.js";
import { loadLiveFixture } from "./live-fixture.js";
const AUTH_FILE = join(homedir(), ".pi", "agent", "auth.json");
@@ -87,4 +88,33 @@ describe("oauth autoLoginQoderFromEnvironment", () => {
"global",
);
});
it("passes a recorded-format identity into the model catalog refresh", async () => {
const identity = loadLiveFixture("global").interactions.userinfo.response.body as {
id: string;
email: string;
name: string;
};
vi.mocked(credentialsFromPat).mockResolvedValueOnce({
access: "<redacted:job-token>",
refresh: "<redacted:refresh-token>",
expires: Date.now() + 3600000,
userID: identity.id,
email: identity.email,
name: identity.name,
machineID: "<redacted:machine-id>",
type: "oauth",
} as never);
process.env.QODER_PAT = "test-only-pat";
await autoLoginQoderFromEnvironment("qoder-fixture-provider", "global");
expect(updateQoderModelsCache).toHaveBeenCalledWith(
"<redacted:job-token>",
identity.id,
identity.name,
identity.email,
"global",
);
});
});
+50 -2
View File
@@ -1,5 +1,17 @@
import { describe, expect, it } from "vitest";
import { decodePatRefresh, encodePatRefresh, isPatRefresh, PAT_REFRESH_PREFIX } from "../pat.js";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
decodePatRefresh,
encodePatRefresh,
exchangeJobToken,
fetchUserInfo,
isPatRefresh,
PAT_REFRESH_PREFIX,
} from "../pat.js";
import { loadLiveFixture, responseFromFixture } from "./live-fixture.js";
afterEach(() => {
vi.unstubAllGlobals();
});
// ── isPatRefresh ──────────────────────────────────────────────────────────
@@ -67,3 +79,39 @@ describe("PAT_REFRESH_PREFIX", () => {
expect(PAT_REFRESH_PREFIX).toBe("pat");
});
});
describe("recorded-format PAT protocol fixtures", () => {
const fixture = loadLiveFixture("global");
it("replays PAT exchange response shape", async () => {
const interaction = fixture.interactions.patExchange;
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(responseFromFixture(interaction)));
const result = await exchangeJobToken("test-pat", "global");
expect(result.jobToken).toBe("<redacted:job-token>");
expect(result.jobRefreshToken).toBe("<redacted:refresh-token>");
expect(result.expiresAt).toBeGreaterThan(Date.now());
expect(fetch).toHaveBeenCalledWith(
interaction.request.url,
expect.objectContaining({
method: interaction.request.method,
headers: expect.objectContaining({
"Cosy-Version": interaction.request.headers["cosy-version"],
}),
}),
);
});
it("replays userinfo response shape", async () => {
const interaction = fixture.interactions.userinfo;
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(responseFromFixture(interaction)));
const body = interaction.response.body as { id: string; email: string; name: string };
await expect(fetchUserInfo("test-job-token", "global")).resolves.toEqual({
userID: body.id,
email: body.email,
name: body.name,
});
});
});
+3 -7
View File
@@ -9,6 +9,7 @@ import type {
} from "@earendil-works/pi-ai";
import { afterEach, describe, expect, it, vi } from "vitest";
import { streamQoder } from "../stream.js";
import { loadLiveFixture } from "./live-fixture.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
@@ -76,12 +77,7 @@ function finishChunk(finish_reason: string, extra: object = {}): object {
};
}
const SUCCESS_SSE =
sseEnvelope(chunk({ role: "assistant" })) +
sseEnvelope(chunk({ reasoning_content: "The user wants OK.", role: "assistant" })) +
sseEnvelope(chunk({ content: "OK", role: "assistant" })) +
sseEnvelope(finishChunk("stop")) +
DONE_SSE;
const SUCCESS_SSE = loadLiveFixture("global").interactions.chat.response.body as string;
const BLOCKED_SSE = sseEnvelope(
{ code: "provider_error", message: "Session blocked", request_id: "r", type: "provider_error" },
@@ -125,7 +121,7 @@ describe("streamQoder", () => {
vi.restoreAllMocks();
});
it("parses a successful SSE stream into text + stop", async () => {
it("replays a recorded-format SSE fixture into text + stop", async () => {
globalThis.fetch = mockFetch(SUCCESS_SSE);
const stream = streamQoder(makeModel(), makeContext(), { apiKey: "fake" });
const events = await consume(stream);