Spaces:
Paused
Paused
File size: 20,191 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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | /**
* Tests for `src/async/client.ts`.
*
* Each test injects a mock `fetchImpl` that asserts the request shape (URL,
* method, headers, body) and returns a canned response. Verifies:
* - happy-path response parsing + snake_case → camelCase mapping
* - 4xx/5xx error propagation with correct `OffPeakServerError` shape
* - settle 4xx-as-success semantics (mirrors `settleOne` in the bundle)
* - per-call timeout enforcement via `setTimeout + abort`
* - external AbortSignal cancellation
* - batchStatus truncation at 100 tickets
* - credential header composition (jwt + apiKey + optional bigmodel-* headers)
*/
import { describe, it, expect, mock } from "bun:test";
import { createOffPeakClient } from "./client.js";
import { OffPeakServerError } from "./types.js";
import type { OffPeakCredentials } from "./types.js";
const CRED: OffPeakCredentials = {
jwt: "test-jwt-token",
codingPlanApiKey: "test-api-key",
};
function makeMockFetch(impl: (req: Request, init?: RequestInit) => Promise<Response>): typeof fetch {
return mock((url: string | URL | Request, init?: RequestInit) => {
const req = new Request(typeof url === "string" ? url : url.toString(), init);
return new Promise<Response>((resolve, reject) => {
const onAbort = (): void => {
const err = new Error("The operation was aborted");
(err as Error & { name: string }).name = "AbortError";
reject(err);
};
if (init?.signal?.aborted) {
onAbort();
return;
}
init?.signal?.addEventListener("abort", onAbort, { once: true });
Promise.resolve(impl(req, init))
.then(resolve, reject)
.finally(() => init?.signal?.removeEventListener("abort", onAbort));
});
}) as unknown as typeof fetch;
}
function jsonResp(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), {
status,
headers: { "content-type": "application/json" },
});
}
function captureReq(req: Request) {
return {
method: req.method,
url: req.url,
authHeader: req.headers.get("authorization"),
apiKeyHeader: req.headers.get("x-coding-plan-api-key"),
bigmodelOrg: req.headers.get("bigmodel-organization"),
bigmodelProj: req.headers.get("bigmodel-project"),
contentType: req.headers.get("content-type"),
};
}
describe("createOffPeakClient — header composition", () => {
it("sends Authorization + X-Coding-Plan-Api-Key on every request", async () => {
let captured: ReturnType<typeof captureReq> | undefined;
const fetchImpl = makeMockFetch(async (req) => {
captured = captureReq(req);
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
await client.getAvailability();
expect(captured?.authHeader).toBe("Bearer test-jwt-token");
expect(captured?.apiKeyHeader).toBe("test-api-key");
expect(captured?.bigmodelOrg).toBeNull();
expect(captured?.bigmodelProj).toBeNull();
});
it("includes bigmodel-organization / bigmodel-project when provided", async () => {
let captured: ReturnType<typeof captureReq> | undefined;
const fetchImpl = makeMockFetch(async (req) => {
captured = captureReq(req);
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({
origin: "https://zcode.z.ai",
credentials: { ...CRED, bigmodelOrganization: "org-123", bigmodelProject: "proj-456" },
fetchImpl,
});
await client.getAvailability();
expect(captured?.bigmodelOrg).toBe("org-123");
expect(captured?.bigmodelProj).toBe("proj-456");
});
it("strips trailing slashes from origin", async () => {
let url = "";
const fetchImpl = makeMockFetch(async (req) => {
url = req.url;
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai/////", credentials: CRED, fetchImpl });
await client.getAvailability();
expect(url).toBe("https://zcode.z.ai/api/v1/off-peak/ticket/availability");
});
it("sets content-type only when body is present", async () => {
let getCT: string | null = "";
const fetchImpl = makeMockFetch(async (req) => {
getCT = req.headers.get("content-type");
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
await client.getAvailability();
expect(getCT).toBeNull();
let postCT = "";
const fetchImpl2 = makeMockFetch(async (req) => {
postCT = req.headers.get("content-type") ?? "";
return jsonResp({ ticket_id: "t-1", state: "queued" });
});
const client2 = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl: fetchImpl2 });
await client2.takeTicket("task-1");
expect(postCT).toBe("application/json");
});
});
describe("getAvailability", () => {
it("parses can_take_number=true response (no nextTakeAt)", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ can_take_number: true }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.getAvailability();
expect(result).toEqual({ canTakeNumber: true });
});
it("parses can_take_number=false with next_take_at", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ can_take_number: false, next_take_at: 1234567890 }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.getAvailability();
expect(result).toEqual({ canTakeNumber: false, nextTakeAt: 1234567890 });
});
it("throws when can_take_number=false but next_take_at missing", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ can_take_number: false }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
expect(client.getAvailability()).rejects.toThrow(/missing next_take_at/);
});
});
describe("takeTicket", () => {
it("parses queued response with position + next_poll_after", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({
ticket_id: "t-abc",
state: "queued",
position: 42,
next_poll_after: 5,
}));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.takeTicket("offpeak-xyz");
expect(result.ticketId).toBe("t-abc");
expect(result.state).toBe("queued");
expect(result.position).toBe(42);
expect(result.nextPollAfterMs).toBe(5000);
expect(typeof result.registeredAt).toBe("number");
});
it("parses ready response (no position, no nextPollAfter)", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ ticket_id: "t-1", state: "ready" }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.takeTicket("offpeak-xyz");
expect(result.state).toBe("ready");
expect(result.position).toBeUndefined();
expect(result.nextPollAfterMs).toBeUndefined();
});
it("sends body as {task_id: ...}", async () => {
let bodyText = "";
const fetchImpl = makeMockFetch(async (req) => {
bodyText = await req.text();
return jsonResp({ ticket_id: "t-1", state: "queued" });
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
await client.takeTicket("offpeak-123");
expect(JSON.parse(bodyText)).toEqual({ task_id: "offpeak-123" });
});
it("rejects empty taskId", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({}));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const empty = "" as string;
expect(client.takeTicket(empty)).rejects.toThrow(/taskId/);
});
it("throws on malformed response (missing ticket_id)", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ state: "queued" }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
expect(client.takeTicket("task-1")).rejects.toThrow(/malformed/);
});
});
describe("batchStatus", () => {
it("returns empty result for empty input without making HTTP call", async () => {
let called = false;
const fetchImpl = makeMockFetch(async () => {
called = true;
return jsonResp({});
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.batchStatus([]);
expect(result).toEqual({ tickets: [] });
expect(called).toBe(false);
});
it("parses tickets[] with position + active_deadline", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({
next_poll_after: 10,
tickets: [
{ ticket_id: "t-1", state: "ready", active_deadline: 9999999 },
{ ticket_id: "t-2", state: "queued", position: 3 },
],
}));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.batchStatus(["t-1", "t-2"]);
expect(result.nextPollAfterMs).toBe(10000);
expect(result.tickets).toEqual([
{ ticketId: "t-1", state: "ready", activeDeadline: 9999999 },
{ ticketId: "t-2", state: "queued", position: 3 },
]);
});
it("truncates at 100 tickets", async () => {
let bodyText = "";
const fetchImpl = makeMockFetch(async (req) => {
bodyText = await req.text();
return jsonResp({ tickets: [] });
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const ids = Array.from({ length: 150 }, (_, i) => `t-${i}`);
await client.batchStatus(ids);
const parsed = JSON.parse(bodyText);
expect(parsed.ticket_ids.length).toBe(100);
expect(parsed.ticket_ids[0]).toBe("t-0");
expect(parsed.ticket_ids[99]).toBe("t-99");
});
it("throws on malformed tickets entry", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({
tickets: [{ ticket_id: "t-1" }],
}));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
expect(client.batchStatus(["t-1"])).rejects.toThrow(/malformed/);
});
});
describe("settle", () => {
it("POSTs to /ticket/{id}/settle with no body", async () => {
let captured: { method: string; url: string; bodyText: string } = { method: "", url: "", bodyText: "" };
const fetchImpl = makeMockFetch(async (req) => {
captured = { method: req.method, url: req.url, bodyText: await req.text().catch(() => "") };
return new Response(null, { status: 200 });
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
await client.settle("t-abc/def");
expect(captured.method).toBe("POST");
expect(captured.url).toBe("https://zcode.z.ai/api/v1/off-peak/ticket/t-abc%2Fdef/settle");
expect(captured.bodyText).toBe("");
});
it("resolves on 200 with empty body", async () => {
const fetchImpl = makeMockFetch(async () => new Response(null, { status: 200 }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
await client.settle("t-1");
});
it("4xx resolves as success by default (server already cleaned up)", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ code: 404, msg: "ticket not found" }, 404));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
await client.settle("t-1");
});
it("4xx throws when settleAsSuccess=false", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ code: 404, msg: "ticket not found" }, 404));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
expect(client.settle("t-1", { settleAsSuccess: false })).rejects.toThrow(OffPeakServerError);
});
it("5xx always throws", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ msg: "upstream down" }, 502));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
expect(client.settle("t-1")).rejects.toThrow(/upstream down/);
});
it("uses settleTimeoutMs when provided (separate from controlTimeoutMs)", async () => {
// Hung fetch: if settle() used controlTimeoutMs (30s) this test would hang;
// it must abort on the 100ms settle budget. Falsifies the name — the old
// immediately-resolving mock passed even when settleTimeoutMs was ignored.
const fetchImpl = makeMockFetch(async () => {
await new Promise((r) => setTimeout(r, 5_000));
return new Response(null, { status: 200 });
});
const client = createOffPeakClient({
origin: "https://zcode.z.ai",
credentials: CRED,
fetchImpl,
controlTimeoutMs: 30_000,
settleTimeoutMs: 100,
});
const started = Date.now();
try {
await client.settle("t-1");
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
expect((e as OffPeakServerError).message).toMatch(/abort|network/i);
}
expect(Date.now() - started).toBeLessThan(2_000);
});
});
describe("response envelope unwrap", () => {
it("unwraps {code:0, data:{...}} envelope on HTTP 200", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({
code: 0,
msg: "ok",
data: { ticket_id: "t-1", state: "queued", position: 5 },
}));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.takeTicket("task-1");
expect(result.ticketId).toBe("t-1");
expect(result.state).toBe("queued");
expect(result.position).toBe(5);
});
it("non-zero code on HTTP 200 throws OffPeakServerError with bizCode", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({
code: 3103,
msg: "quota exceeded",
data: null,
}));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
try {
await client.takeTicket("task-1");
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
const err = e as OffPeakServerError;
expect(err.bizCode).toBe("3103");
expect(err.message).toContain("quota exceeded");
}
});
it("response without envelope (no code/data fields) returned as-is", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ ticket_id: "t-raw", state: "ready" }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
const result = await client.takeTicket("task-1");
expect(result.ticketId).toBe("t-raw");
});
});
describe("error handling", () => {
it("5xx with JSON {code,msg} → OffPeakServerError carries httpStatus + bizCode", async () => {
const fetchImpl = makeMockFetch(async () => jsonResp({ code: 3103, msg: "quota exceeded" }, 429));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
try {
await client.takeTicket("task-1");
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
const err = e as OffPeakServerError;
expect(err.httpStatus).toBe(429);
expect(err.bizCode).toBe("3103");
expect(err.message).toContain("quota exceeded");
}
});
it("5xx with plain text body", async () => {
const fetchImpl = makeMockFetch(async () => new Response("internal error", { status: 500 }));
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
try {
await client.takeTicket("task-1");
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
expect((e as OffPeakServerError).httpStatus).toBe(500);
expect((e as OffPeakServerError).message).toContain("internal error");
}
});
it("network error wraps in OffPeakServerError with httpStatus=0", async () => {
const fetchImpl = makeMockFetch(async () => {
throw new TypeError("fetch failed: ECONNREFUSED");
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
try {
await client.getAvailability();
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
const err = e as OffPeakServerError;
expect(err.httpStatus).toBe(0);
expect(err.message).toContain("network error");
expect(err.message).toContain("ECONNREFUSED");
}
});
it("abort error wraps in OffPeakServerError with httpStatus=0", async () => {
const fetchImpl = makeMockFetch(async () => {
const err = new Error("The operation was aborted");
(err as Error & { name: string }).name = "AbortError";
throw err;
});
const client = createOffPeakClient({ origin: "https://zcode.z.ai", credentials: CRED, fetchImpl });
try {
await client.getAvailability();
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
expect((e as OffPeakServerError).httpStatus).toBe(0);
expect((e as OffPeakServerError).message).toContain("aborted");
}
});
it("per-call timeout triggers abort when controlTimeoutMs is tiny", async () => {
const fetchImpl = makeMockFetch(async () => {
await new Promise((r) => setTimeout(r, 200));
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({
origin: "https://zcode.z.ai",
credentials: CRED,
fetchImpl,
controlTimeoutMs: 30,
});
try {
await client.getAvailability();
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
expect((e as OffPeakServerError).message).toMatch(/abort|network/i);
}
});
it("external AbortSignal cancellation propagates to fetch", async () => {
const fetchImpl = makeMockFetch(async (_req, init) => {
// Wait long enough for the test to cancel us.
await new Promise((_r, rej) => {
const t = setTimeout(() => _r(null), 1000);
init?.signal?.addEventListener("abort", () => {
clearTimeout(t);
const err = new Error("aborted");
(err as Error & { name: string }).name = "AbortError";
rej(err);
});
});
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({
origin: "https://zcode.z.ai",
credentials: CRED,
fetchImpl,
controlTimeoutMs: 10000,
});
const controller = new AbortController();
const p = client.getAvailability(controller.signal);
setTimeout(() => controller.abort(), 50);
try {
await p;
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
expect((e as OffPeakServerError).message).toMatch(/abort/i);
}
});
it("pre-aborted signal aborts immediately", async () => {
let called = false;
const fetchImpl = makeMockFetch(async () => {
called = true;
return jsonResp({ can_take_number: true });
});
const client = createOffPeakClient({
origin: "https://zcode.z.ai",
credentials: CRED,
fetchImpl,
controlTimeoutMs: 5000,
});
const controller = new AbortController();
controller.abort();
try {
await client.getAvailability(controller.signal);
expect(true).toBe(false);
} catch (e) {
expect(e).toBeInstanceOf(OffPeakServerError);
expect((e as OffPeakServerError).message).toMatch(/abort/i);
}
expect(called).toBe(false);
});
});
|