remoteCdn.test.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. isUtcPlusEightTimeZone,
  4. resolveRemoteCdnBaseUrls,
  5. shouldPreferDomesticRemoteCdn,
  6. } from "../src/main/remoteCdn.js";
  7. describe("remoteCdn", () => {
  8. it("中文且东八区时应优先国内 CDN", () => {
  9. expect(
  10. shouldPreferDomesticRemoteCdn({
  11. locale: "zh-CN",
  12. timeZone: "Asia/Shanghai",
  13. }),
  14. ).toBe(true);
  15. expect(
  16. resolveRemoteCdnBaseUrls({
  17. env: "production",
  18. locale: "zh-CN",
  19. timeZone: "Asia/Shanghai",
  20. version: "0.0.0-test",
  21. }),
  22. ).toEqual([
  23. "https://cdn.codegeex.cn/zcode/electron/releases/0.0.0-test",
  24. "https://cdn-zcode.z.ai/zcode/electron/releases/0.0.0-test",
  25. ]);
  26. });
  27. it("非中文或非东八区时应优先海外 CDN", () => {
  28. expect(
  29. shouldPreferDomesticRemoteCdn({
  30. locale: "en-US",
  31. timeZone: "Asia/Shanghai",
  32. }),
  33. ).toBe(false);
  34. expect(
  35. shouldPreferDomesticRemoteCdn({
  36. locale: "zh-CN",
  37. timeZone: "America/Los_Angeles",
  38. }),
  39. ).toBe(false);
  40. expect(
  41. resolveRemoteCdnBaseUrls({
  42. env: "production",
  43. locale: "zh-CN",
  44. timeZone: "America/Los_Angeles",
  45. version: "0.0.0-test",
  46. }),
  47. ).toEqual([
  48. "https://cdn-zcode.z.ai/zcode/electron/releases/0.0.0-test",
  49. "https://cdn.codegeex.cn/zcode/electron/releases/0.0.0-test",
  50. ]);
  51. });
  52. it("东八区判断应按 UTC 偏移而不是固定城市名单", () => {
  53. expect(isUtcPlusEightTimeZone("Asia/Shanghai")).toBe(true);
  54. expect(isUtcPlusEightTimeZone("Asia/Singapore")).toBe(true);
  55. expect(isUtcPlusEightTimeZone("Asia/Tokyo")).toBe(false);
  56. });
  57. it("测试产品环境应固定使用内网 SSH remote assets 根目录", () => {
  58. expect(
  59. resolveRemoteCdnBaseUrls({
  60. env: "test",
  61. locale: "zh-CN",
  62. timeZone: "Asia/Shanghai",
  63. version: "0.0.0-test",
  64. }),
  65. ).toEqual([
  66. "http://intranet.example.invalid:12345/ssh-remote-assets/0.0.0-test",
  67. ]);
  68. });
  69. it("显式覆盖 CDN 基址时不再按产品环境选择默认值", () => {
  70. expect(
  71. resolveRemoteCdnBaseUrls({
  72. env: "test",
  73. overrideBaseUrl: "https://cdn.example.com/releases///",
  74. version: "0.0.0-test",
  75. }),
  76. ).toEqual(["https://cdn.example.com/releases"]);
  77. });
  78. });