helpAppConfig.test.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createHelpAppConfigReader, resolveHelpAppConfig } from "../src/helpAppConfig.js";
  3. const envelope = (feedbackUrl: unknown) => ({ code: 0, data: { configs: { feedbackUrl } } });
  4. const local = {
  5. community_urls: { "zh-CN": "https://local.cn", "en-US": "https://local.en" },
  6. feedback_url: "https://local.feedback",
  7. feedback_use_external_form: true,
  8. };
  9. describe("help app config", () => {
  10. it("按字段和语言回退,远端 false 覆盖本地 true", () => {
  11. expect(
  12. resolveHelpAppConfig(
  13. { community_urls: { "zh-CN": "https://remote.cn" }, feedback_use_external_form: false },
  14. local,
  15. ),
  16. ).toEqual({
  17. community_urls: { "zh-CN": "https://remote.cn", "en-US": "https://local.en" },
  18. feedback_url: "https://local.feedback",
  19. feedback_use_external_form: false,
  20. });
  21. });
  22. it("有效 envelope、no-store、并发合并、TTL 和 endpoint 隔离", async () => {
  23. let now = 0;
  24. const fetchImpl = vi
  25. .fn<typeof fetch>()
  26. .mockImplementation(async () =>
  27. Response.json(envelope({ feedback_url: "https://remote.feedback" })),
  28. );
  29. const read = createHelpAppConfigReader({ fetchImpl, now: () => now });
  30. const [a, b] = await Promise.all([read("https://a/configs"), read("https://a/configs")]);
  31. expect(a).toEqual(b);
  32. expect(a.feedback_url).toBe("https://remote.feedback");
  33. expect(fetchImpl).toHaveBeenCalledTimes(1);
  34. expect(fetchImpl.mock.calls[0][1]).toMatchObject({ cache: "no-store", credentials: "omit" });
  35. await read("https://a/configs");
  36. await read("https://b/configs");
  37. expect(fetchImpl).toHaveBeenCalledTimes(2);
  38. now = 3_600_001;
  39. await read("https://a/configs");
  40. expect(fetchImpl).toHaveBeenCalledTimes(3);
  41. });
  42. it.each([{}, { code: 1, data: { configs: { feedbackUrl: {} } } }, envelope(null)])(
  43. "无效响应不进入缓存 %j",
  44. async (body) => {
  45. const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue(Response.json(body));
  46. const read = createHelpAppConfigReader({ fetchImpl });
  47. await expect(read("https://a/configs")).rejects.toThrow();
  48. await expect(read("https://a/configs")).rejects.toThrow();
  49. expect(fetchImpl).toHaveBeenCalledTimes(2);
  50. },
  51. );
  52. it.each([new Response("bad", { status: 503 }), new Response("not json")])(
  53. "HTTP 和 JSON 错误不缓存",
  54. async (response) => {
  55. const fetchImpl = vi.fn<typeof fetch>().mockImplementation(async () => response.clone());
  56. const read = createHelpAppConfigReader({ fetchImpl });
  57. await expect(read("https://a/configs")).rejects.toThrow();
  58. await expect(read("https://a/configs")).rejects.toThrow();
  59. expect(fetchImpl).toHaveBeenCalledTimes(2);
  60. },
  61. );
  62. });
  63. it("超时信号传递到网络层,失败后可重试", async () => {
  64. const fetchImpl = vi
  65. .fn<typeof fetch>()
  66. .mockRejectedValue(new DOMException("timeout", "TimeoutError"));
  67. const read = createHelpAppConfigReader({ fetchImpl });
  68. await expect(read("https://a/configs")).rejects.toThrow("timeout");
  69. expect(fetchImpl.mock.calls[0][1]?.signal).toBeInstanceOf(AbortSignal);
  70. fetchImpl.mockImplementation(async () =>
  71. Response.json(envelope({ feedback_url: "https://recovered" })),
  72. );
  73. expect((await read("https://a/configs")).feedback_url).toBe("https://recovered");
  74. });
  75. it("旧 endpoint 的迟到响应不覆盖新 endpoint", async () => {
  76. let finishOld!: (response: Response) => void;
  77. const fetchImpl = vi.fn<typeof fetch>().mockImplementation(async (url) => {
  78. if (String(url).includes("old"))
  79. return new Promise<Response>((resolve) => {
  80. finishOld = resolve;
  81. });
  82. return Response.json(envelope({ feedback_url: "https://new" }));
  83. });
  84. const read = createHelpAppConfigReader({ fetchImpl });
  85. const old = read("https://old/configs");
  86. expect((await read("https://new/configs")).feedback_url).toBe("https://new");
  87. finishOld(Response.json(envelope({ feedback_url: "https://old" })));
  88. await old;
  89. expect((await read("https://new/configs")).feedback_url).toBe("https://new");
  90. });