plugin-store-auto-refresh-on-enter.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {
  2. clearAppData,
  3. clickTestIdByDom,
  4. waitForDefaultWorkspaceReady,
  5. } from "../../../helpers/desktop-app.js";
  6. import {
  7. PLUGIN_CDN_ZIP_MARKETPLACE_ID,
  8. PLUGIN_STORE_AUTO_REFRESH_STALE_LAST_UPDATED,
  9. } from "../../../helpers/plugin-cdn-zip-marketplace.js";
  10. import {
  11. capturePluginManualReview,
  12. getPluginOverview,
  13. hasPluginControl,
  14. openPluginSettings,
  15. requirePluginManualReview,
  16. waitForPluginControl,
  17. } from "../../../helpers/plugin-management-lifecycle.js";
  18. // 真打公网 CDN:与 PLM-LC-007b 一致,刷新等待 180s。
  19. // 运行方式(2026-09-08 本机验证通过):
  20. // ZCODE_E2E_MANUAL_REVIEW=1 ZCODE_E2E_MANUAL_REVIEW_HOLD_MS=180000 \
  21. // E2E_PROVIDER_REPLAY_FIXTURE_PATH=packages/desktop/test/e2e/fixtures/upstream/common.json \
  22. // pnpm --filter @zcode/desktop test:e2e:serial -- --spec <本文件>
  23. // - manual-review 默认 capture 模式会给 agent 挂 MITM 抓包代理,agent 的市场 fetch 不认其 CA,
  24. // 官方市场刷新会以 "unable to get local issuer certificate" 失败;显式 replay fixture 让 worker
  25. // 恢复直连即可。
  26. // - wdio mocha 超时是配置级 120s 硬上限,用例内 this.timeout 无效,必须用 HOLD_MS 追加预算。
  27. const CDN_REFRESH_TIMEOUT_MS = 180000;
  28. const NO_REFRESH_SETTLE_MS = 5000;
  29. function readOfficialLastUpdated(
  30. overview: Awaited<ReturnType<typeof getPluginOverview>>,
  31. ): string | undefined {
  32. return overview.marketplaces.find((item) => item.id === PLUGIN_CDN_ZIP_MARKETPLACE_ID)
  33. ?.lastUpdated;
  34. }
  35. describe("PLM-LC-019 Desktop plugin store auto refresh on enter (real official CDN)", () => {
  36. after(async () => {
  37. await browser.electron.restoreAllMocks();
  38. await clearAppData();
  39. });
  40. it("官方市场刷新时间过期时,进入商店页不点刷新也会自动刷新,重进则命中节流", async function () {
  41. requirePluginManualReview();
  42. await waitForDefaultWorkspaceReady();
  43. // wdio seed 把官方市场 lastUpdated 写成远古时间且 pluginCount>0,必然落在节流窗口外。
  44. expect(readOfficialLastUpdated(await getPluginOverview())).toBe(
  45. PLUGIN_STORE_AUTO_REFRESH_STALE_LAST_UPDATED,
  46. );
  47. const enteredAt = Date.now();
  48. await openPluginSettings();
  49. let refreshedLastUpdated: string | undefined;
  50. let official: Awaited<ReturnType<typeof getPluginOverview>>["marketplaces"][number] | undefined;
  51. await browser
  52. .waitUntil(
  53. async () => {
  54. official = (await getPluginOverview()).marketplaces.find(
  55. (item) => item.id === PLUGIN_CDN_ZIP_MARKETPLACE_ID,
  56. );
  57. // 刷新失败会被持久化成 refreshFailure;直接带着诊断失败,不要空等到超时。
  58. if (official?.refreshFailure) {
  59. throw new Error(
  60. `Official marketplace auto refresh failed: ${JSON.stringify(official.refreshFailure)}`,
  61. );
  62. }
  63. refreshedLastUpdated = official?.lastUpdated;
  64. return (
  65. refreshedLastUpdated !== undefined &&
  66. refreshedLastUpdated !== PLUGIN_STORE_AUTO_REFRESH_STALE_LAST_UPDATED
  67. );
  68. },
  69. {
  70. timeout: CDN_REFRESH_TIMEOUT_MS,
  71. interval: 2000,
  72. timeoutMsg: "Entering the plugin store did not auto refresh the official marketplace",
  73. },
  74. )
  75. .catch((error: unknown) => {
  76. // 超时时把官方市场记录一并带出,便于区分「没触发」与「触发了但落盘异常」。
  77. throw new Error(
  78. `${error instanceof Error ? error.message : String(error)}; official=${JSON.stringify(official)}`,
  79. );
  80. });
  81. if (!refreshedLastUpdated) throw new Error("Official marketplace lastUpdated missing");
  82. // 允许 agent/renderer 时钟有秒级偏差。
  83. expect(Date.parse(refreshedLastUpdated)).toBeGreaterThanOrEqual(enteredAt - 60_000);
  84. expect(await hasPluginControl("plugin-store-error")).toBe(false);
  85. await capturePluginManualReview("PLM-LC-019", "auto-refreshed-on-enter");
  86. // 刚刷新过:离开再重进商店页命中 10 分钟窗口,不再刷新。
  87. // 商店顶栏返回聊天,再从侧栏进入,保证 PluginStorePage 真正重新挂载。
  88. await clickTestIdByDom("desktop-top-nav-back");
  89. await clickTestIdByDom("plugin-store-sidebar-open");
  90. await waitForPluginControl("plugin-store-root", {}, 30000);
  91. await browser.pause(NO_REFRESH_SETTLE_MS);
  92. expect(readOfficialLastUpdated(await getPluginOverview())).toBe(refreshedLastUpdated);
  93. });
  94. });