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

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