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
+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);