officialMarketplaceAutoRefresh.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { beforeEach, describe, expect, it } from "vitest";
  2. import {
  3. OFFICIAL_MARKETPLACE_AUTO_REFRESH_INTERVAL_MS,
  4. claimMarketplaceAutoRefresh,
  5. resetMarketplaceAutoRefreshForTests,
  6. shouldAutoRefreshMarketplace,
  7. } from "@/settings/officialMarketplaceAutoRefresh.js";
  8. const NOW = Date.parse("2026-09-08T12:00:00.000Z");
  9. const minutesAgo = (minutes: number): string => new Date(NOW - minutes * 60_000).toISOString();
  10. describe("official marketplace auto refresh throttle", () => {
  11. beforeEach(() => {
  12. resetMarketplaceAutoRefreshForTests();
  13. });
  14. it("从未成功刷新或时间戳不可解析时立即刷新", () => {
  15. expect(shouldAutoRefreshMarketplace({ now: NOW })).toBe(true);
  16. expect(shouldAutoRefreshMarketplace({ lastUpdated: "not-a-date", now: NOW })).toBe(true);
  17. });
  18. it("距上次成功刷新不足 10 分钟跳过,到期后刷新", () => {
  19. expect(OFFICIAL_MARKETPLACE_AUTO_REFRESH_INTERVAL_MS).toBe(10 * 60_000);
  20. expect(shouldAutoRefreshMarketplace({ lastUpdated: minutesAgo(9), now: NOW })).toBe(false);
  21. expect(shouldAutoRefreshMarketplace({ lastUpdated: minutesAgo(10), now: NOW })).toBe(true);
  22. expect(shouldAutoRefreshMarketplace({ lastUpdated: minutesAgo(11), now: NOW })).toBe(true);
  23. });
  24. it("e2e 伪造的未来 lastUpdated 依旧跳过", () => {
  25. expect(
  26. shouldAutoRefreshMarketplace({ lastUpdated: "2099-01-01T00:00:00.000Z", now: NOW }),
  27. ).toBe(false);
  28. });
  29. it("最近尝试过(失败或仍在飞)时即使 lastUpdated 很旧也跳过", () => {
  30. expect(
  31. shouldAutoRefreshMarketplace({
  32. lastUpdated: minutesAgo(60),
  33. lastAttemptAt: NOW - 60_000,
  34. now: NOW,
  35. }),
  36. ).toBe(false);
  37. });
  38. it("claim 通过后同一市场在窗口内不再放行,不同市场互不影响", () => {
  39. expect(claimMarketplaceAutoRefresh("zcode-plugins-official", undefined, NOW)).toBe(true);
  40. expect(claimMarketplaceAutoRefresh("zcode-plugins-official", undefined, NOW + 1_000)).toBe(
  41. false,
  42. );
  43. expect(claimMarketplaceAutoRefresh("other-marketplace", undefined, NOW + 1_000)).toBe(true);
  44. expect(
  45. claimMarketplaceAutoRefresh(
  46. "zcode-plugins-official",
  47. undefined,
  48. NOW + OFFICIAL_MARKETPLACE_AUTO_REFRESH_INTERVAL_MS,
  49. ),
  50. ).toBe(true);
  51. });
  52. it("手动刷新成功后 lastUpdated 变新,窗口以其为准重置", () => {
  53. expect(claimMarketplaceAutoRefresh("zcode-plugins-official", minutesAgo(30), NOW)).toBe(true);
  54. // 5 分钟后手动刷新成功,lastUpdated 更新到 NOW+5min;再过 6 分钟进入商店页:
  55. // 距 attempt 已 11 分钟但距 lastUpdated 仅 6 分钟,应跳过。
  56. const manualRefreshAt = new Date(NOW + 5 * 60_000).toISOString();
  57. expect(
  58. claimMarketplaceAutoRefresh("zcode-plugins-official", manualRefreshAt, NOW + 11 * 60_000),
  59. ).toBe(false);
  60. });
  61. });