|
1 | 1 | import { describe, expect, it } from "bun:test"; |
2 | | -import { EndpointRoutingService } from "./endpoint-routing.js"; |
| 2 | +import { EndpointRoutingService, requiresClaimCaptcha } from "./endpoint-routing.js"; |
3 | 3 | import type { ProxyIdentity } from "../config/types.js"; |
4 | 4 |
|
5 | 5 | const identity: ProxyIdentity = { appVersion: "3.8.1", sourceTitle: "cli", refererOrigin: "https://zcode.z.ai" }; |
@@ -282,3 +282,92 @@ describe("EndpointRoutingService", () => { |
282 | 282 | }); |
283 | 283 | }); |
284 | 284 | }); |
| 285 | + |
| 286 | +describe("3.16.3 configs.captcha request-verification switch", () => { |
| 287 | + function configFetchWith(captcha: unknown): typeof fetch { |
| 288 | + return (async () => new Response(JSON.stringify({ |
| 289 | + code: 0, |
| 290 | + data: { proxyEndpoint: { mapping: [{ from: ZAI_ANTHROPIC, to: ZAI_ULTRA }] }, configs: { captcha } }, |
| 291 | + }), { status: 200 })) as unknown as typeof fetch; |
| 292 | + } |
| 293 | + |
| 294 | + it("parses data.configs.captcha (snake_case → camelCase) on the same snapshot", async () => { |
| 295 | + const svc = new EndpointRoutingService({ |
| 296 | + identity, |
| 297 | + fetchImpl: configFetchWith({ enabled: true, skip_model_request: false }), |
| 298 | + }); |
| 299 | + expect(await svc.getRequestVerificationConfig()).toEqual({ enabled: true, skipModelRequest: false }); |
| 300 | + }); |
| 301 | + |
| 302 | + it("returns null while the server has not shipped the block (live state 2026-10-23)", async () => { |
| 303 | + const svc = new EndpointRoutingService({ |
| 304 | + identity, |
| 305 | + fetchImpl: (async () => new Response(JSON.stringify({ |
| 306 | + code: 0, |
| 307 | + data: { proxyEndpoint: { mapping: [] }, configs: {} }, |
| 308 | + }), { status: 200 })) as unknown as typeof fetch, |
| 309 | + }); |
| 310 | + expect(await svc.getRequestVerificationConfig()).toBeNull(); |
| 311 | + }); |
| 312 | + |
| 313 | + it("records only the decisive booleans; non-boolean/garbage values degrade neutrally", async () => { |
| 314 | + const neutral = new EndpointRoutingService({ |
| 315 | + identity, |
| 316 | + fetchImpl: configFetchWith({ enabled: "false", skip_model_request: "true" }), |
| 317 | + }); |
| 318 | + expect(await neutral.getRequestVerificationConfig()).toEqual({}); |
| 319 | + const garbage = new EndpointRoutingService({ identity, fetchImpl: configFetchWith("not-an-object") }); |
| 320 | + expect(await garbage.getRequestVerificationConfig()).toBeNull(); |
| 321 | + }); |
| 322 | + |
| 323 | + it("a malformed captcha block never fails the mapping refresh", async () => { |
| 324 | + const svc = new EndpointRoutingService({ identity, fetchImpl: configFetchWith([1, 2]) }); |
| 325 | + const resolved = await svc.resolve(ZAI_ANTHROPIC); |
| 326 | + expect(resolved.routed).toBeTrue(); |
| 327 | + expect(await svc.getRequestVerificationConfig()).toBeNull(); |
| 328 | + }); |
| 329 | + |
| 330 | + it("fails open on fetch errors (null → captcha stays required)", async () => { |
| 331 | + const svc = new EndpointRoutingService({ |
| 332 | + identity, |
| 333 | + fetchImpl: (async () => { |
| 334 | + throw new Error("network down"); |
| 335 | + }) as unknown as typeof fetch, |
| 336 | + }); |
| 337 | + expect(await svc.getRequestVerificationConfig()).toBeNull(); |
| 338 | + }); |
| 339 | + |
| 340 | + it("honors the snapshot TTL: a fresh cache does not refetch for the config read", async () => { |
| 341 | + let clock = 0; |
| 342 | + let calls = 0; |
| 343 | + const svc = new EndpointRoutingService({ |
| 344 | + identity, |
| 345 | + now: () => clock, |
| 346 | + fetchImpl: (async () => { |
| 347 | + calls++; |
| 348 | + return new Response(JSON.stringify({ |
| 349 | + code: 0, |
| 350 | + data: { proxyEndpoint: { mapping: [] }, configs: { captcha: { skip_model_request: true } } }, |
| 351 | + }), { status: 200 }); |
| 352 | + }) as unknown as typeof fetch, |
| 353 | + }); |
| 354 | + expect(await svc.getRequestVerificationConfig()).toEqual({ skipModelRequest: true }); |
| 355 | + expect(await svc.getRequestVerificationConfig()).toEqual({ skipModelRequest: true }); |
| 356 | + expect(calls).toBe(1); |
| 357 | + clock = 301_000; |
| 358 | + expect(await svc.getRequestVerificationConfig()).toEqual({ skipModelRequest: true }); |
| 359 | + expect(calls).toBe(2); |
| 360 | + }); |
| 361 | + |
| 362 | + it("requiresClaimCaptcha mirrors requiresRequestVerification's defaults", () => { |
| 363 | + // absent / null / neutral → required (pre-3.16.3 wire) |
| 364 | + expect(requiresClaimCaptcha(undefined)).toBeTrue(); |
| 365 | + expect(requiresClaimCaptcha(null)).toBeTrue(); |
| 366 | + expect(requiresClaimCaptcha({})).toBeTrue(); |
| 367 | + expect(requiresClaimCaptcha({ enabled: true, skipModelRequest: false })).toBeTrue(); |
| 368 | + // explicit server switches → skipped |
| 369 | + expect(requiresClaimCaptcha({ enabled: false })).toBeFalse(); |
| 370 | + expect(requiresClaimCaptcha({ skipModelRequest: true })).toBeFalse(); |
| 371 | + expect(requiresClaimCaptcha({ enabled: false, skipModelRequest: true })).toBeFalse(); |
| 372 | + }); |
| 373 | +}); |
0 commit comments