plugin-store-extension-compat.test.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { join, resolve } from "node:path";
  3. import type { ZCodePluginsOverviewResult } from "@zcode/shared";
  4. import { clearAppData, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  5. import {
  6. addMarketplaceSourceThroughAgent,
  7. capturePluginManualReview,
  8. clickPluginControl,
  9. getPluginControlText,
  10. getPluginOverview,
  11. hasPluginControl,
  12. installMarketplacePluginThroughAgent,
  13. installPluginThroughUi,
  14. openManageView,
  15. openPluginCard,
  16. openPluginSettings,
  17. requirePluginManualReview,
  18. selectPluginSettingsScope,
  19. setPluginPresentation,
  20. waitForPluginCard,
  21. waitForPluginControl,
  22. waitUntilPluginOverview,
  23. } from "#e2e/helpers/plugin-management-lifecycle";
  24. // PLM-EXT-STORE(docs/plugins/plugin-publishing.md §4.3–4.4):商店按插件 API 版本挡住不兼容的扩展插件,
  25. // 安装前在详情页显示「含常驻代码」与申请的权限。本地市场四个插件:
  26. // - ok:engines ^1.4.0(App 的 EXTENSION_API_VERSION 是 1.4.0),申请完全访问与系统通知,可以装;
  27. // - future:engines ^99.0.0,商店显示「需要更新 ZCode」,不能装;
  28. // - installed:先装兼容的 1.0.0,目录再升到不兼容的 2.0.0:不显示更新,「全部更新」跳过它;
  29. // - plain:不带扩展,安装与更新行为不变。
  30. const CASE_ID = "PLM-EXT-STORE";
  31. const MARKET = "e2e-ext-compat-market";
  32. const OK = `e2e-ext-compat-ok@${MARKET}`;
  33. const FUTURE = `e2e-ext-compat-future@${MARKET}`;
  34. const INSTALLED_NAME = "e2e-ext-compat-installed";
  35. const INSTALLED = `${INSTALLED_NAME}@${MARKET}`;
  36. const PLAIN_NAME = "e2e-ext-compat-plain";
  37. const PLAIN = `${PLAIN_NAME}@${MARKET}`;
  38. const FIXTURE_ROOT = resolve(
  39. process.cwd(),
  40. "test/e2e/fixtures/fs/plugins/plugin-store-extension-compat/store-market",
  41. );
  42. interface InstallButtonState {
  43. disabled: boolean;
  44. blocked: string | null;
  45. text: string;
  46. }
  47. /** 当前显示的那个安装按钮(列表卡片或详情页主按钮,同一时刻只显示一处)。 */
  48. async function readInstallButton(pluginId: string): Promise<InstallButtonState | null> {
  49. return browser.execute((id) => {
  50. const button = Array.from(
  51. document.querySelectorAll<HTMLButtonElement>('[data-testid="plugin-store-install"]'),
  52. ).find(
  53. (candidate) =>
  54. candidate.dataset.pluginId === id && candidate.getBoundingClientRect().height > 0,
  55. );
  56. if (!button) return null;
  57. return {
  58. disabled: button.disabled,
  59. blocked: button.dataset.installBlocked ?? null,
  60. text: button.textContent?.trim() ?? "",
  61. };
  62. }, pluginId);
  63. }
  64. /** 详情页「含常驻代码」一区:每个档位一行,完全访问另起一行。 */
  65. async function readExtensionNotice(pluginId: string) {
  66. return browser.execute((id) => {
  67. const section = Array.from(
  68. document.querySelectorAll<HTMLElement>('[data-testid="plugin-store-extension-notice"]'),
  69. ).find((candidate) => candidate.dataset.pluginId === id);
  70. if (!section) return null;
  71. const grants = Array.from(
  72. section.querySelectorAll<HTMLElement>('[data-testid="plugin-store-extension-grant"]'),
  73. ).map((row) => ({ grant: row.dataset.grant ?? "", text: row.textContent?.trim() ?? "" }));
  74. return {
  75. text: section.textContent ?? "",
  76. grants,
  77. fullAccess:
  78. section
  79. .querySelector<HTMLElement>('[data-testid="plugin-store-extension-full-access"]')
  80. ?.textContent?.trim() ?? null,
  81. noGrants: Boolean(section.querySelector('[data-testid="plugin-store-extension-no-grants"]')),
  82. };
  83. }, pluginId);
  84. }
  85. /**
  86. * 等顶栏刷新真正结束。刷新 = updateMarketplace(null),按顺序重拉全部市场,商店只在整个操作结束后才重载列表;
  87. * 测试机外网很慢时,默认的公网插件市场要等满 30 秒才中止(adapters/plugins/marketplace.ts 的
  88. * CLAUDE_MARKETPLACE_REFRESH_TIMEOUT_MS)。CLI 概览在本地市场更新后就已变化,
  89. * 若直接用 30 秒等角标,等待窗口会与这 30 秒几乎重合(运行时日志:刷新返回晚于等待超时约 100ms)。
  90. * 刷新中按钮是 disabled 的,以它作为可观察的结束信号,再等角标渲染。
  91. */
  92. async function waitForStoreRefreshSettled(): Promise<void> {
  93. await browser.waitUntil(
  94. async () =>
  95. browser.execute(() => {
  96. const button = document.querySelector<HTMLButtonElement>(
  97. '[data-testid="plugin-store-refresh"]',
  98. );
  99. return button !== null && !button.disabled;
  100. }),
  101. {
  102. timeout: 120000,
  103. interval: 200,
  104. timeoutMsg: "the store refresh button should be enabled again after the refresh finishes",
  105. },
  106. );
  107. }
  108. async function backToStoreList(): Promise<void> {
  109. await clickPluginControl("plugin-store-detail-back");
  110. await waitForPluginControl("plugin-store-list");
  111. }
  112. /** 目录与插件包都升到 2.0.0;installed 的新版要求 ^99.0.0。改的是添加市场时的来源目录,手动刷新时重新复制。 */
  113. async function bumpCatalog(market: string): Promise<void> {
  114. const catalogPath = join(market, ".claude-plugin", "marketplace.json");
  115. const catalog = JSON.parse(await readFile(catalogPath, "utf8")) as {
  116. plugins: Array<Record<string, unknown>>;
  117. };
  118. for (const entry of catalog.plugins) {
  119. if (entry.name !== INSTALLED_NAME && entry.name !== PLAIN_NAME) continue;
  120. entry.version = "2.0.0";
  121. if (entry.name === INSTALLED_NAME) {
  122. entry.extensionSummary = { engines: { zcode: "^99.0.0" }, grants: [] };
  123. }
  124. const manifestPath = join(
  125. market,
  126. "plugins",
  127. String(entry.name),
  128. ".zcode-plugin",
  129. "plugin.json",
  130. );
  131. const manifest = JSON.parse(await readFile(manifestPath, "utf8")) as Record<string, unknown>;
  132. manifest.version = "2.0.0";
  133. if (entry.name === INSTALLED_NAME) manifest.extension = { engines: { zcode: "^99.0.0" } };
  134. await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
  135. }
  136. await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`);
  137. }
  138. function findAvailable(overview: ZCodePluginsOverviewResult, id: string) {
  139. return overview.availablePlugins.find((plugin) => plugin.id === id);
  140. }
  141. function findInstalled(overview: ZCodePluginsOverviewResult, id: string) {
  142. return overview.installedPlugins.find((plugin) => plugin.id === id);
  143. }
  144. const screenshots: string[] = [];
  145. /** 列表与两个详情页:中文浅色、英文深色各一组。 */
  146. async function captureStoreViews(label: "zh-CN-zai-light" | "en-US-zai-dark"): Promise<void> {
  147. await waitForPluginCard(FUTURE);
  148. screenshots.push(await capturePluginManualReview(CASE_ID, `store-list-${label}`));
  149. await openPluginCard(FUTURE);
  150. await waitForPluginControl("plugin-store-extension-incompatible", { pluginId: FUTURE });
  151. screenshots.push(await capturePluginManualReview(CASE_ID, `future-detail-${label}`));
  152. await backToStoreList();
  153. await openPluginCard(OK);
  154. await waitForPluginControl("plugin-store-extension-notice", { pluginId: OK });
  155. screenshots.push(await capturePluginManualReview(CASE_ID, `compatible-detail-${label}`));
  156. await backToStoreList();
  157. }
  158. describe("PLM-EXT-STORE 商店按插件 API 版本挡住不兼容的扩展插件", () => {
  159. after(async () => {
  160. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  161. if (artifactDir) {
  162. await writeFile(
  163. join(artifactDir, "plm-ext-store-evidence.json"),
  164. JSON.stringify({ screenshots }, null, 2),
  165. ).catch(() => {});
  166. }
  167. await clearAppData();
  168. });
  169. it("不兼容不能装、安装前显示扩展信息、不兼容的新版不提示更新", async function () {
  170. this.timeout(600000);
  171. requirePluginManualReview();
  172. const { homeDir } = getE2EAppDataPaths();
  173. const market = join(homeDir, "fixtures", "ext-store-compat", "store-market");
  174. await mkdir(market, { recursive: true });
  175. await cp(FIXTURE_ROOT, market, { recursive: true, force: true });
  176. await addMarketplaceSourceThroughAgent(market);
  177. await waitUntilPluginOverview(
  178. (overview) =>
  179. [OK, FUTURE, INSTALLED, PLAIN].every((id) => findAvailable(overview, id) !== undefined),
  180. "the local market should list all four plugins",
  181. 60000,
  182. );
  183. await installMarketplacePluginThroughAgent(INSTALLED_NAME, MARKET);
  184. // services 读到 CLI 的能力位后才要扩展摘要(§4.3);没有扩展的插件不带,componentTypes 透传 extension。
  185. const initial = await getPluginOverview();
  186. expect(findAvailable(initial, OK)?.listing?.extensionSummary).toEqual({
  187. engines: { zcode: "^1.4.0" },
  188. grants: ["sessions.fullAccess", "notifications"],
  189. });
  190. expect(findAvailable(initial, OK)?.componentTypes).toContain("extension");
  191. expect(findAvailable(initial, FUTURE)?.listing?.extensionSummary?.engines.zcode).toBe(
  192. "^99.0.0",
  193. );
  194. expect(findAvailable(initial, PLAIN)?.listing?.extensionSummary).toBeUndefined();
  195. expect(findAvailable(initial, PLAIN)?.componentTypes ?? []).not.toContain("extension");
  196. // (a) 列表:不兼容的插件按钮是不能点的「需要更新 ZCode」;兼容的与普通的照常「安装」。
  197. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1400, height: 900 });
  198. await openPluginSettings();
  199. await clickPluginControl("plugin-store-segment-personal");
  200. await waitForPluginCard(FUTURE);
  201. expect(await readInstallButton(FUTURE)).toEqual({
  202. disabled: true,
  203. blocked: "requires-update",
  204. text: "需要更新 ZCode",
  205. });
  206. expect(await readInstallButton(OK)).toEqual({ disabled: false, blocked: null, text: "安装" });
  207. expect(await readInstallButton(PLAIN)).toEqual({
  208. disabled: false,
  209. blocked: null,
  210. text: "安装",
  211. });
  212. // (b) 不兼容插件的详情页:按钮不能点,简介下有说明;照样显示含常驻代码与申请的权限。
  213. await openPluginCard(FUTURE);
  214. expect(await readInstallButton(FUTURE)).toMatchObject({
  215. disabled: true,
  216. blocked: "requires-update",
  217. });
  218. expect(
  219. await getPluginControlText("plugin-store-extension-incompatible", { pluginId: FUTURE }),
  220. ).toContain("更新 ZCode");
  221. const futureNotice = await readExtensionNotice(FUTURE);
  222. expect(futureNotice?.text).toContain("含常驻代码");
  223. expect(futureNotice?.grants).toEqual([{ grant: "sessions.read", text: "读取会话" }]);
  224. expect(futureNotice?.fullAccess).toBeNull();
  225. await backToStoreList();
  226. // (c) 兼容插件的详情页:档位按授权对话框的档位名列出,完全访问单独一行写明含义。
  227. await openPluginCard(OK);
  228. expect(await hasPluginControl("plugin-store-extension-incompatible", { pluginId: OK })).toBe(
  229. false,
  230. );
  231. const okNotice = await readExtensionNotice(OK);
  232. expect(okNotice?.text).toContain("含常驻代码");
  233. expect(okNotice?.grants).toEqual([
  234. { grant: "sessions.fullAccess", text: "读写会话(含完全访问)" },
  235. { grant: "notifications", text: "发送系统通知" },
  236. ]);
  237. expect(okNotice?.fullAccess).toMatch(/^完全访问:/);
  238. expect(okNotice?.noGrants).toBe(false);
  239. await backToStoreList();
  240. // (d) 普通插件:详情页没有扩展信息。
  241. await openPluginCard(PLAIN);
  242. expect(await readExtensionNotice(PLAIN)).toBeNull();
  243. await backToStoreList();
  244. await captureStoreViews("zh-CN-zai-light");
  245. await setPluginPresentation({ locale: "en-US", theme: "zai-dark", width: 1400, height: 900 });
  246. await captureStoreViews("en-US-zai-dark");
  247. await openPluginCard(FUTURE);
  248. expect(await readInstallButton(FUTURE)).toMatchObject({
  249. disabled: true,
  250. text: "Requires newer ZCode",
  251. });
  252. expect((await readExtensionNotice(FUTURE))?.text).toContain("Includes resident code");
  253. await backToStoreList();
  254. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1400, height: 900 });
  255. // (e) 兼容插件与普通插件照常从商店安装;装上后详情页换成「权限」区块,不再显示安装前的说明。
  256. await openPluginCard(OK);
  257. await installPluginThroughUi(OK);
  258. await waitUntilPluginOverview(
  259. (overview) => findInstalled(overview, OK) !== undefined,
  260. "the compatible extension plugin should install",
  261. 60000,
  262. );
  263. await browser.waitUntil(
  264. async () => !(await hasPluginControl("plugin-store-extension-notice", { pluginId: OK })),
  265. { timeout: 15000, timeoutMsg: "the pre-install notice should leave after install" },
  266. );
  267. await backToStoreList();
  268. await openPluginCard(PLAIN);
  269. await installPluginThroughUi(PLAIN);
  270. await waitUntilPluginOverview(
  271. (overview) => findInstalled(overview, PLAIN) !== undefined,
  272. "the plain plugin should install",
  273. 60000,
  274. );
  275. await backToStoreList();
  276. // (f) 目录升级:installed 的新版要 ^99.0.0,plain 的新版普通。CLI 两个都报 update-available。
  277. await bumpCatalog(market);
  278. await clickPluginControl("plugin-store-refresh");
  279. await waitUntilPluginOverview(
  280. (overview) =>
  281. findInstalled(overview, INSTALLED)?.updateStatus === "update-available" &&
  282. findInstalled(overview, INSTALLED)?.listing?.extensionSummary?.engines.zcode ===
  283. "^99.0.0" &&
  284. findInstalled(overview, PLAIN)?.updateStatus === "update-available",
  285. "both installed plugins should report a newer catalog version",
  286. 60000,
  287. );
  288. await waitForStoreRefreshSettled();
  289. // App 显示前按兼容过滤:plain 有「可更新」,installed 没有角标、没有更新按钮。
  290. await waitForPluginControl("plugin-store-update-badge", { pluginId: PLAIN }, 30000);
  291. expect(await hasPluginControl("plugin-store-card-update", { pluginId: PLAIN })).toBe(true);
  292. expect(await hasPluginControl("plugin-store-update-badge", { pluginId: INSTALLED })).toBe(
  293. false,
  294. );
  295. expect(await hasPluginControl("plugin-store-card-update", { pluginId: INSTALLED })).toBe(false);
  296. // 刷新后个人分段还会列出其他市场:把本地市场的卡片滚到视野里再截图。
  297. await browser.execute((id) => {
  298. document
  299. .querySelector<HTMLElement>(`[data-testid="plugin-store-card"][data-plugin-id="${id}"]`)
  300. ?.scrollIntoView({ block: "center" });
  301. }, PLAIN);
  302. screenshots.push(await capturePluginManualReview(CASE_ID, "updates-zh-CN-zai-light"));
  303. await openPluginCard(INSTALLED);
  304. expect(await hasPluginControl("plugin-store-card-update", { pluginId: INSTALLED })).toBe(false);
  305. expect(await hasPluginControl("plugin-store-detail-update")).toBe(false);
  306. await backToStoreList();
  307. // (g) 「全部更新」只算 plain,执行后 installed 仍是 1.0.0,照常启用。
  308. await openManageView();
  309. await selectPluginSettingsScope("user");
  310. await waitForPluginControl("plugin-settings-update-all");
  311. expect(await getPluginControlText("plugin-settings-update-all")).toContain("(1)");
  312. screenshots.push(await capturePluginManualReview(CASE_ID, "update-all-zh-CN-zai-light"));
  313. await clickPluginControl("plugin-settings-update-all");
  314. await waitUntilPluginOverview(
  315. (overview) => findInstalled(overview, PLAIN)?.version === "2.0.0",
  316. "update-all should update the plain plugin",
  317. 60000,
  318. );
  319. await browser.waitUntil(async () => !(await hasPluginControl("plugin-settings-update-all")), {
  320. timeout: 15000,
  321. timeoutMsg: "update-all should leave once only the incompatible update remains",
  322. });
  323. const final = await getPluginOverview();
  324. expect(findInstalled(final, INSTALLED)).toMatchObject({
  325. version: "1.0.0",
  326. enabled: true,
  327. updateStatus: "update-available",
  328. });
  329. await setPluginPresentation({ locale: "en-US", theme: "zai-dark", width: 1400, height: 900 });
  330. screenshots.push(await capturePluginManualReview(CASE_ID, "after-update-all-en-US-zai-dark"));
  331. });
  332. });