File size: 14,083 Bytes
488aedf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
 * Integration wiring tests: highspeed card application inside `proxyRequest`
 * (injected manager), covering model rewrite + auth swap + unsigned dispatch,
 * and the selectionFallback redispatch after a card rejection.
 */
import { describe, it, expect, afterEach } from "bun:test";
import { proxyRequest } from "./handler.js";
import type { ProxyConfig, ProxyIdentity } from "../config/types.js";
import { AuthManager } from "../auth/manager.js";
import { HighspeedManager } from "../highspeed/manager.js";
import { ClientSigningManager } from "./client-signing.js";

const IDENTITY: ProxyIdentity = {
  appVersion: "3.15.1",
  sourceTitle: "cli",
  refererOrigin: "https://zcode.z.ai",
};

const TEST_CONFIG: ProxyConfig = {
  server: { port: 8080, host: "0.0.0.0" },
  auth: {},
  provider: "zai",
  plan: "coding-plan",
  providers: {
    zai: { anthropicBase: "https://api.z.ai/api/anthropic", openaiBase: "https://api.z.ai/api/coding/paas/v4" },
    bigmodel: { anthropicBase: "https://open.bigmodel.cn/api/anthropic", openaiBase: "https://open.bigmodel.cn/api/coding/paas/v4" },
  },
  defaultModel: "glm-4.6",
  models: ["glm-4.6"],
  identity: IDENTITY,
  clientIdentity: { mode: "off", ttlSeconds: 900, maxSessions: 1024 },
  responses: { enabled: true, storeMaxEntries: 1000, storeTtlMs: 86400000 },
  endpointRouting: { enabled: false, origin: "https://zcode.z.ai" },
  clientSigning: { enabled: false },
  highspeed: { enabled: false, origin: "https://zcode.z.ai", models: [] },
  mcp: { enabled: true, webSearch: true, webReader: false, zread: false, gateway: { enabled: true, upstreamOrigin: "https://zcode.chatglm.site" } },
  async: { enabled: false, origin: "https://zcode.z.ai", pollIntervalMs: 5000, keepAliveIntervalMs: 3000, maxWaitMs: 0, maxRetries: 3, settleTimeoutMs: 8000, controlTimeoutMs: 15000, defaultModel: "" },
  claim: { enabled: false, auto: true, origin: "https://zcode.z.ai", pollIntervalMs: 300000, cooldownMs: 600000, planId: "" },
  logging: { level: "info" },
};

const CRED = "testkey.testsecret";
const JWT = "test-zcode-jwt";
const liveManagers: HighspeedManager[] = [];
afterEach(() => {
  for (const manager of liveManagers.splice(0)) manager.stop();
});

function oauthAuth(key = CRED): AuthManager {
  const [apiKey, secret] = key.split(".");
  const auth = new AuthManager();
  auth.setOAuthCredential({ apiKey, secret, provider: "zai", jwt: JWT });
  return auth;
}

function anthropicClientReq(): Request {
  return new Request("http://localhost:8080/v1/messages", {
    method: "POST",
    headers: { "content-type": "application/json" },
    body: JSON.stringify({ model: "glm-5.1", max_tokens: 32, messages: [{ role: "user", content: "hi" }] }),
  });
}

function anthropicOk(): Response {
  return new Response(
    JSON.stringify({
      id: "msg_wire", type: "message", role: "assistant", model: "glm-5.3-highspeed",
      content: [{ type: "text", text: "Hi" }], stop_reason: "end_turn", stop_sequence: null,
      usage: { input_tokens: 1, output_tokens: 1 },
    }),
    { status: 200, headers: { "content-type": "application/json" } },
  );
}

/** Manager pre-loaded with an active card (draw already settled). */
async function cardHoldingManager(): Promise<HighspeedManager> {
  const now = Date.now();
  const manager = new HighspeedManager({
    origin: "https://zcode.z.ai",
    models: ["glm-5.3", "glm-5.2", "glm-5.1"],
    fetchImpl: (async () => new Response(JSON.stringify({
      code: 0, msg: "success",
      data: {
        card: { card_id: "hsc_wiring_1", task_id: "t-1", provider: "builtin:zai-coding-plan", model: "glm-5.3-highspeed", issued_at: now, expires_at: now + 300_000 },
        next_draw_at: now + 120_000,
      },
    }), { status: 200 })) as unknown as typeof fetch,
  });
  liveManagers.push(manager);
  manager.onRequest("glm-5.1", "t-1", { jwt: JWT, codingPlanAuth: CRED, family: "zai" });
  await new Promise((r) => setTimeout(r, 5));
  expect(manager.activeCard()?.cardId).toBe("hsc_wiring_1");
  return manager;
}

/** Signer whose fetch records every call — proves carded requests never even attempt a handshake. */
function recordingSigner(calls: string[]): ClientSigningManager {
  return new ClientSigningManager({
    fetchImpl: (async (input: RequestInfo | URL) => {
      const url = String(input instanceof Request ? input.url : input);
      calls.push(url);
      throw new Error(`unexpected signing fetch: ${url}`);
    }) as unknown as typeof fetch,
  });
}

describe("proxyRequest highspeed wiring", () => {
  it("does not extend the old account's draw loop after switching to a credential without JWT", async () => {
    const clock = { now: 1_000_000 };
    let draws = 0;
    let finishIdleCheck!: (stopped: boolean) => void;
    const idleStopped = new Promise<boolean>((resolve) => { finishIdleCheck = resolve; });
    const manager = new HighspeedManager({
      origin: "https://zcode.z.ai",
      models: ["glm-5.1"],
      now: () => clock.now,
      retryMs: 2,
      onEvent: (message) => {
        if (message.includes("idle-stopped")) finishIdleCheck(true);
      },
      fetchImpl: (async () => {
        draws++;
        return new Response(JSON.stringify({ code: 0, data: { card: null } }));
      }) as unknown as typeof fetch,
    });
    liveManagers.push(manager);
    manager.onRequest("glm-5.1", "old-task", { jwt: JWT, codingPlanAuth: CRED, family: "zai" });
    expect(draws).toBe(1);

    // The serve panel can reload a coding-plan-only --import credential while
    // the process-wide manager still holds the previous account's draw params.
    const switchedAuth = oauthAuth();
    switchedAuth.setOAuthCredential({ apiKey: "imported-key", provider: "zai" });
    clock.now += 4 * 60_000;
    const response = await proxyRequest(anthropicClientReq(), "anthropic", {
      config: TEST_CONFIG, auth: switchedAuth, highspeed: manager,
      endpointRouting: null, clientSigning: null,
      fetchImpl: (async () => anthropicOk()) as unknown as typeof fetch,
    });
    expect(response.status).toBe(200);
    await response.text();

    const drawsBeforeIdle = draws;
    clock.now += 2 * 60_000; // Six minutes since the last JWT-backed request.
    const timeout = setTimeout(() => finishIdleCheck(false), 1_000);
    try {
      expect(await idleStopped).toBeTrue();
      expect(draws).toBe(drawsBeforeIdle);
    } finally {
      clearTimeout(timeout);
    }
  });

  it("rewrites the model, swaps auth to the JWT set, adds the card header, and skips signing", async () => {
    const manager = await cardHoldingManager();
    const upstreamCalls: { url: string; headers: Headers; body: string }[] = [];
    const signingFetches: string[] = [];

    const resp = await proxyRequest(anthropicClientReq(), "anthropic", {
      config: TEST_CONFIG,
      auth: oauthAuth(),
      fetchImpl: Object.assign(async (input: RequestInfo | URL, init?: RequestInit) => {
        const req = input instanceof Request ? input : new Request(String(input), init);
        upstreamCalls.push({ url: req.url, headers: req.headers, body: await req.clone().text() });
        return anthropicOk();
      }, { preconnect: () => {} }) as unknown as typeof fetch,
      endpointRouting: null,
      clientSigning: recordingSigner(signingFetches),
      highspeed: manager,
    });

    expect(resp.status).toBe(200);
    expect(upstreamCalls.length).toBe(1);
    const call = upstreamCalls[0]!;
    // model rewritten to the card model
    expect(JSON.parse(call.body).model).toBe("glm-5.3-highspeed");
    // coding-plan dual auth replaced by the JWT set + card headers
    expect(call.headers.get("x-api-key")).toBe(JWT);
    expect(call.headers.get("authorization")).toBe(`Bearer ${JWT}`);
    expect(call.headers.get("x-bigmodel-authorization")).toBe(`Bearer ${CRED}`);
    expect(call.headers.get("bigmodel-target-type")).toBe("PERSONAL");
    expect(call.headers.get("x-highspeed-card-id")).toBe("hsc_wiring_1");
    // official static rule: highspeed-mode requests are never signed — the
    // signer was never even consulted (no handshake fetch attempted)
    expect(signingFetches.length).toBe(0);
    expect(call.headers.get("x-client-sig")).toBeNull();
  });

  it("falls back to the original model and normal auth after a card rejection (401)", async () => {
    const manager = await cardHoldingManager();
    const upstreamCalls: { headers: Headers; body: string }[] = [];

    const resp = await proxyRequest(anthropicClientReq(), "anthropic", {
      config: TEST_CONFIG,
      auth: oauthAuth(),
      fetchImpl: Object.assign(async (input: RequestInfo | URL, init?: RequestInit) => {
        const req = input instanceof Request ? input : new Request(String(input), init);
        upstreamCalls.push({ headers: req.headers, body: await req.clone().text() });
        return upstreamCalls.length === 1
          ? new Response(JSON.stringify({ msg: "VERIFY_SIGNATURE_INVALID" }), { status: 401 })
          : anthropicOk();
      }, { preconnect: () => {} }) as unknown as typeof fetch,
      endpointRouting: null,
      clientSigning: null,
      highspeed: manager,
    });

    expect(resp.status).toBe(200);
    expect(upstreamCalls.length).toBe(2);
    // first: carded
    expect(upstreamCalls[0]!.headers.get("x-highspeed-card-id")).toBe("hsc_wiring_1");
    expect(JSON.parse(upstreamCalls[0]!.body).model).toBe("glm-5.3-highspeed");
    // fallback: original model, normal coding-plan dual auth, no card header
    expect(upstreamCalls[1]!.headers.get("x-highspeed-card-id")).toBeNull();
    expect(upstreamCalls[1]!.headers.get("x-api-key")).toBe(CRED);
    expect(upstreamCalls[1]!.headers.get("authorization")).toBe(`Bearer ${CRED}`);
    expect(JSON.parse(upstreamCalls[1]!.body).model).toBe("glm-5.1");
    // card dropped; the active session re-ignites the loop and the always-hit
    // draw mock hands back a fresh card (2026-10-12 max-rate loop contract)
    await new Promise((r) => setTimeout(r, 5));
    expect(manager.activeCard()?.cardId).toBe("hsc_wiring_1");
  });

  it("keeps card headers on connect-retry rebuilds (attempt ≥ 2 rebuilds from the swapped pairs)", async () => {
    const manager = await cardHoldingManager();
    const upstreamCalls: { headers: Headers; body: string }[] = [];
    let attempts = 0;

    const resp = await proxyRequest(anthropicClientReq(), "anthropic", {
      config: TEST_CONFIG,
      auth: oauthAuth(),
      fetchImpl: Object.assign(async (input: RequestInfo | URL, init?: RequestInit) => {
        attempts++;
        if (attempts === 1) throw new Error("Unable to connect");
        const req = input instanceof Request ? input : new Request(String(input), init);
        upstreamCalls.push({ headers: req.headers, body: await req.clone().text() });
        return anthropicOk();
      }, { preconnect: () => {} }) as unknown as typeof fetch,
      endpointRouting: null,
      clientSigning: null,
      highspeed: manager,
    });

    expect(resp.status).toBe(200);
    expect(attempts).toBe(2);
    expect(upstreamCalls.length).toBe(1);
    // the retried request must still carry the full carded auth set + card model
    expect(upstreamCalls[0]!.headers.get("x-highspeed-card-id")).toBe("hsc_wiring_1");
    expect(upstreamCalls[0]!.headers.get("x-api-key")).toBe(JWT);
    expect(upstreamCalls[0]!.headers.get("x-bigmodel-authorization")).toBe(`Bearer ${CRED}`);
    expect(JSON.parse(upstreamCalls[0]!.body).model).toBe("glm-5.3-highspeed");
  });

  it("leaves requests untouched when the model is not eligible", async () => {
    const base = await cardHoldingManager();
    let noteActivityCalls = 0;
    // Object.create wrapper (NOT Object.assign): mutating base in place would
    // rebind base.noteActivity to the spy itself → infinite recursion.
    const manager = Object.create(base) as HighspeedManager;
    manager.noteActivity = () => {
      noteActivityCalls += 1;
      base.noteActivity();
    };
    const upstreamCalls: { headers: Headers; body: string }[] = [];

    const resp = await proxyRequest(new Request("http://localhost:8080/v1/messages", {
      method: "POST",
      headers: { "content-type": "application/json" },
      body: JSON.stringify({ model: "glm-4.6", max_tokens: 32, messages: [{ role: "user", content: "hi" }] }),
    }), "anthropic", {
      config: TEST_CONFIG,
      auth: oauthAuth(),
      fetchImpl: Object.assign(async (input: RequestInfo | URL, init?: RequestInit) => {
        const req = input instanceof Request ? input : new Request(String(input), init);
        upstreamCalls.push({ headers: req.headers, body: await req.clone().text() });
        return anthropicOk();
      }, { preconnect: () => {} }) as unknown as typeof fetch,
      endpointRouting: null,
      clientSigning: null,
      highspeed: manager,
    });

    expect(resp.status).toBe(200);
    expect(upstreamCalls.length).toBe(1);
    expect(upstreamCalls[0]!.headers.get("x-highspeed-card-id")).toBeNull();
    expect(upstreamCalls[0]!.headers.get("x-api-key")).toBe(CRED);
    expect(JSON.parse(upstreamCalls[0]!.body).model).toBe("glm-4.6");
    // §H eligibility gate: in-flight activity must not re-arm the draw loop
    // for traffic that can never mount a card (idle-window invariant).
    expect(noteActivityCalls).toBe(0);
    base.stop();
  });

  it("notifies in-flight activity for an eligible model after the response completes", async () => {
    const base = await cardHoldingManager();
    let noteActivityCalls = 0;
    const manager = Object.create(base) as HighspeedManager;
    manager.noteActivity = () => {
      noteActivityCalls += 1;
      base.noteActivity();
    };

    const resp = await proxyRequest(anthropicClientReq(), "anthropic", {
      config: TEST_CONFIG,
      auth: oauthAuth(),
      fetchImpl: Object.assign(async () => anthropicOk(), { preconnect: () => {} }) as unknown as typeof fetch,
      endpointRouting: null,
      clientSigning: null,
      highspeed: manager,
    });

    expect(resp.status).toBe(200);
    expect(noteActivityCalls).toBe(1);
    base.stop();
  });
});