zaiStartPlanBalanceSharing.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { fetchZaiStartPlanBalanceEnvelope } from "../src/model-provider/zaiStartPlanBilling.js";
  3. const response = () => new Response(JSON.stringify({ code: 0, data: { plans: [], balances: [] } }));
  4. afterEach(() => vi.useRealTimers());
  5. describe("Start balance 共享读取", () => {
  6. it("权益检查完成后,用量查询在一秒内复用;到期后重新请求", async () => {
  7. vi.useFakeTimers();
  8. const api = { request: vi.fn(async () => response()) };
  9. const first = await fetchZaiStartPlanBalanceEnvelope(api, "Bearer account-a");
  10. await vi.advanceTimersByTimeAsync(999);
  11. expect(await fetchZaiStartPlanBalanceEnvelope(api, "Bearer account-a")).toBe(first);
  12. expect(api.request).toHaveBeenCalledTimes(1);
  13. await vi.advanceTimersByTimeAsync(1);
  14. await fetchZaiStartPlanBalanceEnvelope(api, "Bearer account-a");
  15. expect(api.request).toHaveBeenCalledTimes(2);
  16. });
  17. it("429 在窗口内不重复发起,下一窗口可以重试", async () => {
  18. vi.useFakeTimers();
  19. const api = { request: vi.fn(async () => new Response("limited", { status: 429 })) };
  20. await expect(fetchZaiStartPlanBalanceEnvelope(api, "a")).rejects.toThrow();
  21. await expect(fetchZaiStartPlanBalanceEnvelope(api, "a")).rejects.toThrow();
  22. expect(api.request).toHaveBeenCalledTimes(1);
  23. await vi.advanceTimersByTimeAsync(1000);
  24. await expect(fetchZaiStartPlanBalanceEnvelope(api, "a")).rejects.toThrow();
  25. expect(api.request).toHaveBeenCalledTimes(2);
  26. });
  27. it("不同账号不共享", async () => {
  28. const api = { request: vi.fn(async () => response()) };
  29. await fetchZaiStartPlanBalanceEnvelope(api, "a");
  30. await fetchZaiStartPlanBalanceEnvelope(api, "b");
  31. expect(api.request).toHaveBeenCalledTimes(2);
  32. });
  33. });
  34. it("失效后重新读取,旧请求完成不得覆盖或删除新快照", async () => {
  35. vi.useFakeTimers();
  36. let finish!: (value: Response) => void;
  37. const api = {
  38. request: vi
  39. .fn()
  40. .mockImplementationOnce(
  41. () =>
  42. new Promise<Response>((r) => {
  43. finish = r;
  44. }),
  45. )
  46. .mockImplementation(async () => response()),
  47. };
  48. const old = fetchZaiStartPlanBalanceEnvelope(api, "a");
  49. await vi.advanceTimersByTimeAsync(1100);
  50. const current = await fetchZaiStartPlanBalanceEnvelope(api, "a", true);
  51. finish(response());
  52. await old;
  53. expect(await fetchZaiStartPlanBalanceEnvelope(api, "a")).toBe(current);
  54. expect(api.request).toHaveBeenCalledTimes(2);
  55. });
  56. it("请求超过一秒仍合并未完成的读取", async () => {
  57. vi.useFakeTimers();
  58. let finish!: (value: Response) => void;
  59. const api = {
  60. request: vi.fn(
  61. () =>
  62. new Promise<Response>((r) => {
  63. finish = r;
  64. }),
  65. ),
  66. };
  67. const first = fetchZaiStartPlanBalanceEnvelope(api, "a");
  68. await vi.advanceTimersByTimeAsync(1100);
  69. const second = fetchZaiStartPlanBalanceEnvelope(api, "a");
  70. expect(api.request).toHaveBeenCalledTimes(1);
  71. finish(response());
  72. expect(await second).toBe(await first);
  73. });
  74. it("HTTP Date 已越过到期日时不信任旧 JSON 时间和 active 状态", async () => {
  75. const body = {
  76. code: 0,
  77. data: {
  78. server_time: 1788448680,
  79. plans: [
  80. { user_plan_id: "old", plan_id: "start", status: "active", ends_at: 1788796799 },
  81. { user_plan_id: "new", plan_id: "start", status: "active", ends_at: 1791000000 },
  82. ],
  83. balances: [
  84. { user_plan_id: "old", plan_id: "start", remaining_units: 3000000 },
  85. { user_plan_id: "new", plan_id: "start", remaining_units: 5000000 },
  86. ],
  87. },
  88. };
  89. const api = {
  90. request: vi.fn(
  91. async () =>
  92. new Response(JSON.stringify(body), {
  93. headers: { Date: "Thu, 17 Sep 2026 13:42:46 GMT" },
  94. }),
  95. ),
  96. };
  97. const result = await fetchZaiStartPlanBalanceEnvelope(api, "expired-account");
  98. expect(result.data?.plans?.map((p) => p.status)).toEqual(["expired", "active"]);
  99. expect(result.data?.balances?.map((b) => b.user_plan_id)).toEqual(["new"]);
  100. expect(body.data.plans[0].status).toBe("active");
  101. });