project-token-retry.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { describe, expect, it, vi } from "vitest";
  2. import { APICallError } from "@ai-sdk/provider";
  3. import { AccountRequestRetry } from "../src/model/account-request-retry.js";
  4. import type { AiSdkModelTextRequest, ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  5. const scope = "a".repeat(64);
  6. const rejected = new APICallError({
  7. message: "Unauthorized",
  8. statusCode: 401,
  9. url: "https://model.test",
  10. requestBodyValues: {},
  11. });
  12. function fixture(
  13. mode = "individual-coding-plan",
  14. refresh = vi.fn(async () => ({
  15. headersApplied: true,
  16. requestAuth: { apiKey: "pat-1", accountScope: scope },
  17. })),
  18. ) {
  19. const request = {
  20. refreshRuntimeHeadersBeforeAttempt: refresh,
  21. } as unknown as AiSdkModelTextRequest;
  22. const retry = new AccountRequestRetry(request, { accountAccess: { mode } } as ResolvedAiSdkModel);
  23. const run = () =>
  24. retry.prepareRequest(request).refreshRuntimeHeadersBeforeAttempt!({
  25. attempt: 1,
  26. providerId: "account",
  27. modelId: "model",
  28. });
  29. return { retry, run, refresh, request };
  30. }
  31. describe("PAT 请求一次恢复", () => {
  32. it("401 后携带原作用域及失败 PAT 指纹,最多一次", async () => {
  33. const { retry, run, refresh } = fixture();
  34. await run();
  35. expect(retry.claim(rejected)).toBe(true);
  36. expect(retry.extraAttempts).toBe(1);
  37. await run();
  38. expect(refresh.mock.calls[1]?.[0]).toMatchObject({
  39. expectedAccountScope: scope,
  40. rejectedProjectTokenFingerprint: expect.stringMatching(/^[a-f0-9]{64}$/),
  41. });
  42. expect(retry.claim(rejected)).toBe(false);
  43. });
  44. it.each(["start-plan", "highspeed"])("%s 不进入 PAT 恢复", async (mode) => {
  45. const { retry, run } = fixture(mode);
  46. await run();
  47. expect(retry.claim(rejected)).toBe(false);
  48. });
  49. it("已提交输出、403、未返回 PAT 作用域和取消均不恢复", async () => {
  50. const { retry, run } = fixture();
  51. await run();
  52. expect(retry.claim(rejected, true)).toBe(false);
  53. expect(
  54. retry.claim(
  55. new APICallError({
  56. message: "Forbidden",
  57. statusCode: 403,
  58. url: "https://model.test",
  59. requestBodyValues: {},
  60. }),
  61. ),
  62. ).toBe(false);
  63. const manual = fixture(
  64. "individual-coding-plan",
  65. vi.fn(async () => ({ headersApplied: true, requestAuth: { apiKey: "manual" } })) as never,
  66. );
  67. await manual.run();
  68. expect(manual.retry.claim(rejected)).toBe(false);
  69. const cancelled = fixture();
  70. await cancelled.run();
  71. const controller = new AbortController();
  72. cancelled.request.abortSignal = controller.signal;
  73. controller.abort();
  74. expect(cancelled.retry.claim(rejected)).toBe(false);
  75. });
  76. it("换证响应作用域变化时不发起第二次模型请求", async () => {
  77. let calls = 0;
  78. const { retry, run } = fixture(
  79. "team-coding-plan",
  80. vi.fn(async () => ({
  81. headersApplied: true,
  82. requestAuth: { apiKey: "pat", accountScope: ++calls === 1 ? scope : "b".repeat(64) },
  83. })),
  84. );
  85. await run();
  86. expect(retry.claim(rejected)).toBe(true);
  87. await expect(run()).rejects.toThrow("project_token_scope_invalidated");
  88. });
  89. });