communityUrl.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { describe, expect, it, vi } from "vitest";
  2. import { resolveWebCommunityUrl, resolveWebHelpConfig } from "../src/communityUrl.js";
  3. const LOCAL_CONFIG = {
  4. community_urls: {
  5. "zh-CN": "https://local.example.com/feishu",
  6. "en-US": "https://local.example.com/discord",
  7. },
  8. };
  9. describe("resolveWebCommunityUrl", () => {
  10. it.each([404, 500, 503])("HTTP %i 时回退内置同语言入口", async (status) => {
  11. const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
  12. ok: false,
  13. status,
  14. } as Response);
  15. await expect(
  16. resolveWebCommunityUrl("zh-CN", { fetchImpl, localConfig: LOCAL_CONFIG }),
  17. ).resolves.toBe("https://local.example.com/feishu");
  18. await expect(
  19. resolveWebCommunityUrl("en-US", { fetchImpl, localConfig: LOCAL_CONFIG }),
  20. ).resolves.toBe("https://local.example.com/discord");
  21. });
  22. it("远端缺少当前语言时回退内置同语言入口", async () => {
  23. const fetchImpl = vi.fn<typeof fetch>().mockResolvedValue({
  24. ok: true,
  25. json: async () => ({
  26. code: 0,
  27. data: {
  28. configs: {
  29. feedbackUrl: {
  30. community_urls: { "zh-CN": "https://remote.example.com/feishu" },
  31. },
  32. },
  33. },
  34. }),
  35. } as Response);
  36. await expect(
  37. resolveWebCommunityUrl("en-US", { fetchImpl, localConfig: LOCAL_CONFIG }),
  38. ).resolves.toBe("https://local.example.com/discord");
  39. });
  40. });
  41. it("使用部署 endpoint 的新接口并提取反馈地址", async () => {
  42. const fetchImpl = vi.fn<typeof fetch>().mockImplementation(async () =>
  43. Response.json({
  44. code: 0,
  45. data: {
  46. configs: {
  47. feedbackUrl: {
  48. feedback_url: "https://remote.feedback",
  49. community_urls: { "zh-CN": "https://remote.community" },
  50. },
  51. },
  52. },
  53. }),
  54. );
  55. expect(
  56. (await resolveWebHelpConfig({ fetchImpl, endpointOrigin: "https://test.example.com" }))
  57. .feedback_url,
  58. ).toBe("https://remote.feedback");
  59. expect(new URL(String(fetchImpl.mock.calls[0][0])).searchParams.has("platform")).toBe(false);
  60. expect(fetchImpl.mock.calls[0][0]).toContain("https://test.example.com/api/v1/client/configs?");
  61. expect(await resolveWebCommunityUrl("zh-CN", { fetchImpl })).toBe("https://remote.community");
  62. });