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
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "https://biomejs.dev/schemas/2.4.2/schema.json",
"files": {
"includes": ["src/**", "test/**", "*.ts", "*.json"]
"includes": ["src/**", "scripts/record-live-fixtures.ts", "test/**", "*.ts", "*.json"]
},
"formatter": {
"enabled": true,
+20
View File
@@ -13,6 +13,7 @@
"@earendil-works/pi-ai": "^0.80.2",
"@earendil-works/pi-coding-agent": "^0.84.1",
"esbuild": "^0.28.1",
"tsx": "^4.23.13",
"typescript": "^5.7.0",
"vitest": "^4.1.9"
}
@@ -5122,6 +5123,25 @@
"dev": true,
"license": "0BSD"
},
"node_modules/tsx": {
"version": "4.23.13",
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.23.13.tgz",
"integrity": "sha512-BL5MGkRln6aDYhb0xbQlEAGw743BaZYWdbWtdJOBriYJboKgUUYCadFp2/FpBBZquBC/ezNBn7wMMPx7FDZUDw==",
"dev": true,
"license": "MIT",
"dependencies": {
"esbuild": "~0.28.0"
},
"bin": {
"tsx": "dist/cli.mjs"
},
"engines": {
"node": ">=18.0.0"
},
"optionalDependencies": {
"fsevents": "~2.3.3"
}
},
"node_modules/typebox": {
"version": "1.1.38",
"resolved": "https://registry.npmjs.org/typebox/-/typebox-1.1.38.tgz",
+2
View File
@@ -25,6 +25,7 @@
"lint:fix": "node node_modules/@biomejs/biome/bin/biome check --write .",
"format": "node node_modules/@biomejs/biome/bin/biome format --write .",
"test": "vitest run",
"test:live": "tsx scripts/record-live-fixtures.ts",
"test:watch": "vitest",
"prepare": "npm run build",
"prepublishOnly": "npm run check && npm run build"
@@ -39,6 +40,7 @@
"@earendil-works/pi-ai": "^0.80.2",
"@earendil-works/pi-coding-agent": "^0.84.1",
"esbuild": "^0.28.1",
"tsx": "^4.23.13",
"typescript": "^5.7.0",
"vitest": "^4.1.9"
},
+358
View File
@@ -0,0 +1,358 @@
/**
* Opt-in recorder for the real Qoder protocol.
*
* It exercises PAT exchange, userinfo, model list, and one tiny streaming chat
* through the provider's production functions, then writes a redacted replay
* fixture. Nothing here runs under `npm test`.
*/
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
type Region = "global" | "cn";
type Stage = "patExchange" | "userinfo" | "modelList" | "chat";
interface RecordedInteraction {
request: {
method: string;
url: string;
headers: Record<string, string>;
body: unknown;
};
response: {
status: number;
headers: Record<string, string>;
body: unknown;
};
}
const regions = parseRegions(process.argv.slice(2));
const pats = new Map<Region, string>();
for (const region of regions) {
const pat = getPat(region);
if (!pat) {
const names =
region === "global"
? "QODER_PAT / QODER_PERSONAL_ACCESS_TOKEN / QODER_API_KEY"
: "QODERCN_PAT / QODERCN_PERSONAL_ACCESS_TOKEN / QODERCN_API_KEY";
console.error(`[live] ${region}: missing ${names}; no requests were made`);
process.exit(2);
}
pats.set(region, pat);
}
const workspace = join(dirname(fileURLToPath(import.meta.url)), "..");
const fixtureDir = join(workspace, "src", "__fixtures__", "live");
const temporaryHome = mkdtempSync(join(tmpdir(), "pi-provider-qoder-live-"));
process.env.HOME = temporaryHome;
process.env.USERPROFILE = temporaryHome;
const originalFetch = globalThis.fetch;
try {
for (const region of regions) {
await recordRegion(region, pats.get(region) as string);
}
} finally {
globalThis.fetch = originalFetch;
rmSync(temporaryHome, { recursive: true, force: true });
}
function parseRegions(args: string[]): Region[] {
const index = args.indexOf("--region");
if (index === -1) return ["global", "cn"];
const value = args[index + 1];
if (value === "global" || value === "cn") return [value];
console.error("Usage: npm run test:live -- [--region global|cn]");
process.exit(2);
}
function getPat(region: Region): string {
if (region === "cn") {
return process.env.QODERCN_PAT || process.env.QODERCN_PERSONAL_ACCESS_TOKEN || process.env.QODERCN_API_KEY || "";
}
return process.env.QODER_PAT || process.env.QODER_PERSONAL_ACCESS_TOKEN || process.env.QODER_API_KEY || "";
}
async function recordRegion(region: Region, pat: string): Promise<void> {
console.log(`[live] ${region}: recording PAT exchange, userinfo, model list, and chat`);
const interactions = new Map<Stage, RecordedInteraction>();
const pending: Promise<void>[] = [];
globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => {
const url = typeof input === "string" || input instanceof URL ? input.toString() : input.url;
const stage = classify(url);
const request = recordRequest(input, init, stage);
const response = await originalFetch(input, init);
const clone = response.clone();
pending.push(
readResponseText(clone, stage).then((text) => {
interactions.set(stage, {
request,
response: {
status: clone.status,
headers: recordHeaders(clone.headers),
body: redactResponseBody(stage, text),
},
});
}),
);
return response;
}) as typeof fetch;
const { credentialsFromPat } = await import("../src/pat.js");
const { getCachedModels, updateQoderModelsCache } = await import("../src/models.js");
const { streamQoder } = await import("../src/stream.js");
const credentials = (await credentialsFromPat(pat, region)) as unknown as {
access: string;
userID: string;
email: string;
name: string;
machineID: string;
};
if (!credentials.access || !credentials.userID) {
throw new Error(`[live] ${region}: PAT exchange/userinfo did not return a usable identity`);
}
const providerID = region === "cn" ? "qoder-cn" : "qoder";
const authPath = join(temporaryHome, ".pi", "agent", "auth.json");
mkdirSync(dirname(authPath), { recursive: true });
const auth = existsSync(authPath) ? JSON.parse(readFileSync(authPath, "utf8")) : {};
auth[providerID] = { type: "oauth", ...credentials };
writeFileSync(authPath, JSON.stringify(auth), { mode: 0o600 });
await updateQoderModelsCache(credentials.access, credentials.userID, credentials.name, credentials.email, region);
const models = getCachedModels(region);
const model = models.find((candidate) => (region === "global" ? candidate.id === "lite" : true));
if (!model) throw new Error(`[live] ${region}: model catalog returned no enabled model`);
const events: Array<{
type: string;
message?: { content?: Array<{ type: string; text?: string }> };
error?: unknown;
}> = [];
for await (const event of streamQoder(
{ ...model, provider: providerID } as never,
{
systemPrompt: "",
messages: [{ role: "user", content: "Reply with exactly: OK" }],
tools: [],
} as never,
{ apiKey: credentials.access, maxTokens: 16 } as never,
)) {
events.push(event as (typeof events)[number]);
if (event.type === "done" || event.type === "error") break;
}
const done = events.find((event) => event.type === "done");
const text = done?.message?.content
?.filter((part) => part.type === "text")
.map((part) => part.text || "")
.join("")
.trim();
if (text !== "OK") throw new Error(`[live] ${region}: expected chat response "OK", received ${JSON.stringify(text)}`);
await Promise.all(pending);
for (const stage of ["patExchange", "userinfo", "modelList", "chat"] as const) {
if (!interactions.has(stage)) throw new Error(`[live] ${region}: did not capture ${stage}`);
}
const fixture = {
formatVersion: 1,
source: "recorded",
region,
recordedAt: new Date().toISOString(),
interactions: Object.fromEntries(interactions),
};
const output = `${JSON.stringify(fixture, null, 2)}\n`;
assertRedacted(output);
mkdirSync(fixtureDir, { recursive: true });
const outputPath = join(fixtureDir, `recorded-${region}.json`);
writeFileSync(outputPath, output, { mode: 0o600 });
console.log(`[live] ${region}: PASS; wrote ${outputPath}`);
}
async function readResponseText(response: Response, stage: Stage): Promise<string> {
if (stage !== "chat" || !response.body) return response.text();
// Qoder sometimes sends the [DONE] sentinel without closing the HTTP body.
// Stop the recording branch at that sentinel just like streamQoder does.
const reader = response.body.getReader();
const decoder = new TextDecoder();
let text = "";
try {
while (true) {
const { done, value } = await reader.read();
if (done) break;
text += decoder.decode(value, { stream: true });
if (text.includes('"body":"[DONE]"') || /(?:^|\n)data:\s*\[DONE\]\s*(?:\n|$)/.test(text)) {
await reader.cancel();
break;
}
}
return text + decoder.decode();
} finally {
reader.releaseLock();
}
}
function classify(url: string): Stage {
if (url.includes("/jobToken/exchange")) return "patExchange";
if (url.includes("/userinfo")) return "userinfo";
if (url.includes("/model/list")) return "modelList";
if (url.includes("/chat")) return "chat";
throw new Error(`[live] refusing to record unexpected endpoint: ${new URL(url).pathname}`);
}
function recordRequest(input: string | URL | Request, init: RequestInit | undefined, stage: Stage) {
const url = typeof input === "string" || input instanceof URL ? input.toString() : input.url;
const method = init?.method || (input instanceof Request ? input.method : "GET");
const headers = new Headers(input instanceof Request ? input.headers : undefined);
if (init?.headers) {
for (const [name, value] of new Headers(init.headers)) headers.set(name, value);
}
let body: unknown = null;
if (stage === "patExchange") {
body = { personal_token: "<redacted:pat>" };
} else if (stage === "chat") {
const byteLength =
typeof init?.body === "string"
? Buffer.byteLength(init.body)
: init?.body instanceof Uint8Array
? init.body.byteLength
: 0;
body = { encoding: "qoder-waf", byteLength, value: "<redacted:encoded-request-body>" };
}
return { method, url, headers: recordHeaders(headers), body };
}
const retainedHeaders = new Set([
"accept",
"content-type",
"user-agent",
"cosy-version",
"cosy-clienttype",
"x-model-key",
"x-model-source",
]);
function recordHeaders(headers: Headers): Record<string, string> {
return Object.fromEntries(
[...headers.entries()]
.filter(([name]) => retainedHeaders.has(name.toLowerCase()))
.map(([name, value]) => [name.toLowerCase(), value]),
);
}
function redactResponseBody(stage: Stage, text: string): unknown {
if (stage === "chat") return redactSse(text);
let value: unknown;
try {
value = JSON.parse(text);
} catch {
return redactString(text);
}
if (stage === "userinfo") return redactIdentity(value);
return redactValue(value);
}
function redactIdentity(value: unknown): unknown {
if (Array.isArray(value)) return value.map(redactIdentity);
if (!value || typeof value !== "object") return redactValue(value);
return Object.fromEntries(
Object.entries(value as Record<string, unknown>).map(([key, child]) => {
const normalized = key.toLowerCase();
if (["id", "user_id", "userid", "uid"].includes(normalized)) return [key, "<redacted:user-id>"];
if (normalized === "email") return [key, "<redacted:email>"];
if (normalized === "name" || normalized === "username") return [key, "<redacted:name>"];
return [key, redactIdentity(child)];
}),
);
}
function redactSse(text: string): string {
return text
.split("\n")
.map((line) => {
if (!line.startsWith("data:")) return line;
const payload = line.slice(5).trim();
if (!payload || payload === "[DONE]") return line;
try {
const envelope = JSON.parse(payload) as Record<string, unknown>;
if (typeof envelope.body === "string" && envelope.body !== "[DONE]") {
const envelopeBody = envelope.body;
try {
envelope.body = JSON.stringify(redactValue(JSON.parse(envelopeBody)));
} catch {
envelope.body = redactString(envelopeBody);
}
}
return `data:${JSON.stringify(redactValue(envelope))}`;
} catch {
return `data:${redactString(payload)}`;
}
})
.join("\n");
}
const sensitiveKeys = new Set([
"token",
"access_token",
"refresh_token",
"job_token",
"job_refresh_token",
"security_oauth_token",
"personal_token",
"authorization",
"cookie",
"set-cookie",
"user_id",
"userid",
"uid",
"machine_id",
"machineid",
"request_id",
"requestid",
"request_set_id",
"chat_record_id",
"session_id",
]);
function redactValue(value: unknown, key = ""): unknown {
const normalizedKey = key.toLowerCase();
if (sensitiveKeys.has(normalizedKey) || normalizedKey === "id") {
return `<redacted:${normalizedKey.replaceAll("_", "-")}>`;
}
if (typeof value === "string") return redactString(value);
if (Array.isArray(value)) return value.map((item) => redactValue(item));
if (value && typeof value === "object") {
return Object.fromEntries(
Object.entries(value as Record<string, unknown>).map(([childKey, child]) => [
childKey,
redactValue(child, childKey),
]),
);
}
return value;
}
function redactString(value: string): string {
return value
.replace(/\b(?:pt|jt|jrt)-[A-Za-z0-9._~-]+\b/g, "<redacted:token>")
.replace(/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi, "<redacted:email>");
}
function assertRedacted(output: string): void {
const forbidden = [
/\b(?:pt|jt|jrt)-[A-Za-z0-9._~-]{4,}\b/i,
/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i,
/"authorization"\s*:/i,
/"cookie"\s*:/i,
/"cosy-key"\s*:/i,
/Bearer\s+COSY\./i,
];
for (const pattern of forbidden) {
if (pattern.test(output)) throw new Error(`[live] redaction failed: output matched ${pattern}`);
}
}
+47
View File
@@ -0,0 +1,47 @@
# Qoder protocol replay fixtures
These JSON files preserve the four protocol interactions used by this provider:
1. PAT to job-token exchange
2. userinfo/identity lookup
3. model catalog
4. streaming chat (including Qoder's outer SSE envelope and JSON-string `body`)
`sample-global.json` and `sample-cn.json` are deliberately hand-authored examples,
identified by `"source": "sample"` and `"recordedAt": null`. They are not claimed
to be captures of live traffic. Offline unit tests replay these files so the same
format produced by the live recorder is always exercised.
## Re-recording
Use Node 24 or newer and provide both regional PATs:
```sh
QODER_PAT=... QODERCN_PAT=... npm run test:live
```
Long-form aliases are also accepted:
- Global: `QODER_PAT`, `QODER_PERSONAL_ACCESS_TOKEN`, or `QODER_API_KEY`
- China: `QODERCN_PAT`, `QODERCN_PERSONAL_ACCESS_TOKEN`, or `QODERCN_API_KEY`
To record one region while diagnosing credentials, pass `--region`:
```sh
npm run test:live -- --region global
npm run test:live -- --region cn
```
By default the command requires and records both regions. It exits non-zero
before making any request when a required PAT is missing. A successful run
writes `recorded-global.json` and/or `recorded-cn.json` in this directory.
Review every generated file before committing it. The recorder removes
authorization, cookies, COSY keys/signatures, PATs, job/refresh tokens, emails,
user IDs, machine IDs, names, and request/session/chat IDs. It retains
non-sensitive protocol headers, response schema, model metadata, and SSE
framing. A final leak check rejects the output if it still resembles a token,
email address, Authorization header, cookie, or COSY signature material.
The recorder uses a temporary `HOME`, never writes credentials to the repository,
and does not modify the normal `~/.pi` cache.
+120
View File
@@ -0,0 +1,120 @@
{
"formatVersion": 1,
"source": "sample",
"region": "cn",
"recordedAt": null,
"interactions": {
"patExchange": {
"request": {
"method": "POST",
"url": "https://openapi.qoder.com.cn/api/v1/jobToken/exchange",
"headers": {
"content-type": "application/json",
"cosy-version": "1.0.1",
"cosy-clienttype": "5"
},
"body": {
"personal_token": "<redacted:pat>"
}
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"token": "<redacted:job-token>",
"refresh_token": "<redacted:refresh-token>",
"expires_in": 86400000
}
}
},
"userinfo": {
"request": {
"method": "GET",
"url": "https://openapi.qoder.com.cn/api/v1/userinfo",
"headers": {
"accept": "application/json",
"cosy-version": "1.0.1",
"cosy-clienttype": "5"
},
"body": null
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"id": "<redacted:user-id>",
"email": "<redacted:email>",
"name": "<redacted:name>"
}
}
},
"modelList": {
"request": {
"method": "GET",
"url": "https://gateway.qoder.com.cn/algo/api/v2/model/list",
"headers": {
"accept": "application/json",
"cosy-version": "1.1.38",
"cosy-clienttype": "5"
},
"body": null
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"chat": [
{
"key": "qmodel",
"enable": true,
"display_name": "Qwen3.7 Plus",
"context_config": {
"200K": {
"token_count": 200000
},
"1M": {
"token_count": 1000000,
"is_default": true
}
},
"is_reasoning": true,
"source": "system"
}
]
}
}
},
"chat": {
"request": {
"method": "POST",
"url": "https://gateway.qoder.com.cn/algo/api/v2/chat",
"headers": {
"content-type": "application/json",
"accept": "text/event-stream",
"cosy-version": "1.1.38",
"cosy-clienttype": "5",
"x-model-key": "qmodel",
"x-model-source": "system"
},
"body": {
"encoding": "qoder-waf",
"byteLength": 512,
"value": "<redacted:encoded-request-body>"
}
},
"response": {
"status": 200,
"headers": {
"content-type": "text/event-stream"
},
"body": "data:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"{\\\"choices\\\":[{\\\"delta\\\":{\\\"content\\\":\\\"OK\\\",\\\"role\\\":\\\"assistant\\\"},\\\"index\\\":0}],\\\"created\\\":1,\\\"id\\\":\\\"<redacted:request-id>\\\",\\\"model\\\":\\\"qmodel\\\",\\\"object\\\":\\\"chat.completion.chunk\\\"}\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\ndata:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"{\\\"choices\\\":[{\\\"finish_reason\\\":\\\"stop\\\",\\\"index\\\":0}],\\\"created\\\":1,\\\"id\\\":\\\"<redacted:request-id>\\\",\\\"model\\\":\\\"qmodel\\\",\\\"object\\\":\\\"chat.completion.chunk\\\",\\\"usage\\\":{\\\"completion_tokens\\\":1,\\\"prompt_tokens\\\":10,\\\"total_tokens\\\":11}}\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\ndata:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"[DONE]\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\n"
}
}
}
}
+125
View File
@@ -0,0 +1,125 @@
{
"formatVersion": 1,
"source": "sample",
"region": "global",
"recordedAt": null,
"interactions": {
"patExchange": {
"request": {
"method": "POST",
"url": "https://openapi.qoder.sh/api/v1/jobToken/exchange",
"headers": {
"content-type": "application/json",
"cosy-version": "1.0.1",
"cosy-clienttype": "5"
},
"body": {
"personal_token": "<redacted:pat>"
}
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"token": "<redacted:job-token>",
"refresh_token": "<redacted:refresh-token>",
"expires_in": 86400000
}
}
},
"userinfo": {
"request": {
"method": "GET",
"url": "https://openapi.qoder.sh/api/v1/userinfo",
"headers": {
"accept": "application/json",
"cosy-version": "1.0.1",
"cosy-clienttype": "5"
},
"body": null
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"id": "<redacted:user-id>",
"email": "<redacted:email>",
"name": "<redacted:name>"
}
}
},
"modelList": {
"request": {
"method": "GET",
"url": "https://api3.qoder.sh/algo/api/v2/model/list",
"headers": {
"accept": "application/json",
"cosy-version": "1.1.38",
"cosy-clienttype": "5"
},
"body": null
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"chat": [
{
"key": "lite",
"enable": true,
"display_name": "Lite",
"max_input_tokens": 180000,
"is_reasoning": false,
"source": "system"
},
{
"key": "gm51model",
"enable": true,
"display_name": "GLM 5.2",
"context_config": {
"default": {
"token_count": 200000,
"is_default": true
}
},
"is_reasoning": true,
"source": "system"
}
]
}
}
},
"chat": {
"request": {
"method": "POST",
"url": "https://api3.qoder.sh/algo/api/v2/chat",
"headers": {
"content-type": "application/json",
"accept": "text/event-stream",
"cosy-version": "1.1.38",
"cosy-clienttype": "5",
"x-model-key": "lite",
"x-model-source": "system"
},
"body": {
"encoding": "qoder-waf",
"byteLength": 512,
"value": "<redacted:encoded-request-body>"
}
},
"response": {
"status": 200,
"headers": {
"content-type": "text/event-stream"
},
"body": "data:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"{\\\"choices\\\":[{\\\"delta\\\":{\\\"role\\\":\\\"assistant\\\"},\\\"index\\\":0}],\\\"created\\\":1,\\\"id\\\":\\\"<redacted:request-id>\\\",\\\"model\\\":\\\"lite\\\",\\\"object\\\":\\\"chat.completion.chunk\\\"}\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\ndata:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"{\\\"choices\\\":[{\\\"delta\\\":{\\\"content\\\":\\\"OK\\\",\\\"role\\\":\\\"assistant\\\"},\\\"index\\\":0}],\\\"created\\\":1,\\\"id\\\":\\\"<redacted:request-id>\\\",\\\"model\\\":\\\"lite\\\",\\\"object\\\":\\\"chat.completion.chunk\\\"}\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\ndata:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"{\\\"choices\\\":[{\\\"finish_reason\\\":\\\"stop\\\",\\\"index\\\":0}],\\\"created\\\":1,\\\"id\\\":\\\"<redacted:request-id>\\\",\\\"model\\\":\\\"lite\\\",\\\"object\\\":\\\"chat.completion.chunk\\\",\\\"usage\\\":{\\\"completion_tokens\\\":1,\\\"prompt_tokens\\\":10,\\\"total_tokens\\\":11}}\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\ndata:{\"headers\":{\"Content-Type\":[\"application/json\"]},\"body\":\"[DONE]\",\"statusCodeValue\":200,\"statusCode\":\"OK\"}\n\n"
}
}
}
}
+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);
+1 -1
View File
@@ -11,6 +11,6 @@
"declarationMap": true,
"sourceMap": true
},
"include": ["src"],
"include": ["src", "scripts/record-live-fixtures.ts"],
"exclude": ["node_modules", "dist", "test"]
}