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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. async function backToStoreList(): Promise<void> {
  86. await clickPluginControl("plugin-store-detail-back");
  87. await waitForPluginControl("plugin-store-list");
  88. }
  89. /** 目录与插件包都升到 2.0.0;installed 的新版要求 ^99.0.0。改的是添加市场时的来源目录,手动刷新时重新复制。 */
  90. async function bumpCatalog(market: string): Promise<void> {
  91. const catalogPath = join(market, ".claude-plugin", "marketplace.json");
  92. const catalog = JSON.parse(await readFile(catalogPath, "utf8")) as {
  93. plugins: Array<Record<string, unknown>>;
  94. };
  95. for (const entry of catalog.plugins) {
  96. if (entry.name !== INSTALLED_NAME && entry.name !== PLAIN_NAME) continue;
  97. entry.version = "2.0.0";
  98. if (entry.name === INSTALLED_NAME) {
  99. entry.extensionSummary = { engines: { zcode: "^99.0.0" }, grants: [] };
  100. }
  101. const manifestPath = join(
  102. market,
  103. "plugins",
  104. String(entry.name),
  105. ".zcode-plugin",
  106. "plugin.json",
  107. );
  108. const manifest = JSON.parse(await readFile(manifestPath, "utf8")) as Record<string, unknown>;
  109. manifest.version = "2.0.0";
  110. if (entry.name === INSTALLED_NAME) manifest.extension = { engines: { zcode: "^99.0.0" } };
  111. await writeFile(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
  112. }
  113. await writeFile(catalogPath, `${JSON.stringify(catalog, null, 2)}\n`);
  114. }
  115. function findAvailable(overview: ZCodePluginsOverviewResult, id: string) {
  116. return overview.availablePlugins.find((plugin) => plugin.id === id);
  117. }
  118. function findInstalled(overview: ZCodePluginsOverviewResult, id: string) {
  119. return overview.installedPlugins.find((plugin) => plugin.id === id);
  120. }
  121. const screenshots: string[] = [];
  122. /** 列表与两个详情页:中文浅色、英文深色各一组。 */
  123. async function captureStoreViews(label: "zh-CN-zai-light" | "en-US-zai-dark"): Promise<void> {
  124. await waitForPluginCard(FUTURE);
  125. screenshots.push(await capturePluginManualReview(CASE_ID, `store-list-${label}`));
  126. await openPluginCard(FUTURE);
  127. await waitForPluginControl("plugin-store-extension-incompatible", { pluginId: FUTURE });
  128. screenshots.push(await capturePluginManualReview(CASE_ID, `future-detail-${label}`));
  129. await backToStoreList();
  130. await openPluginCard(OK);
  131. await waitForPluginControl("plugin-store-extension-notice", { pluginId: OK });
  132. screenshots.push(await capturePluginManualReview(CASE_ID, `compatible-detail-${label}`));
  133. await backToStoreList();
  134. }
  135. describe("PLM-EXT-STORE 商店按插件 API 版本挡住不兼容的扩展插件", () => {
  136. after(async () => {
  137. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR;
  138. if (artifactDir) {
  139. await writeFile(
  140. join(artifactDir, "plm-ext-store-evidence.json"),
  141. JSON.stringify({ screenshots }, null, 2),
  142. ).catch(() => {});
  143. }
  144. await clearAppData();
  145. });
  146. it("不兼容不能装、安装前显示扩展信息、不兼容的新版不提示更新", async function () {
  147. this.timeout(600000);
  148. requirePluginManualReview();
  149. const { homeDir } = getE2EAppDataPaths();
  150. const market = join(homeDir, "fixtures", "ext-store-compat", "store-market");
  151. await mkdir(market, { recursive: true });
  152. await cp(FIXTURE_ROOT, market, { recursive: true, force: true });
  153. await addMarketplaceSourceThroughAgent(market);
  154. await waitUntilPluginOverview(
  155. (overview) =>
  156. [OK, FUTURE, INSTALLED, PLAIN].every((id) => findAvailable(overview, id) !== undefined),
  157. "the local market should list all four plugins",
  158. 60000,
  159. );
  160. await installMarketplacePluginThroughAgent(INSTALLED_NAME, MARKET);
  161. // services 读到 CLI 的能力位后才要扩展摘要(§4.3);没有扩展的插件不带,componentTypes 透传 extension。
  162. const initial = await getPluginOverview();
  163. expect(findAvailable(initial, OK)?.listing?.extensionSummary).toEqual({
  164. engines: { zcode: "^1.4.0" },
  165. grants: ["sessions.fullAccess", "notifications"],
  166. });
  167. expect(findAvailable(initial, OK)?.componentTypes).toContain("extension");
  168. expect(findAvailable(initial, FUTURE)?.listing?.extensionSummary?.engines.zcode).toBe(
  169. "^99.0.0",
  170. );
  171. expect(findAvailable(initial, PLAIN)?.listing?.extensionSummary).toBeUndefined();
  172. expect(findAvailable(initial, PLAIN)?.componentTypes ?? []).not.toContain("extension");
  173. // (a) 列表:不兼容的插件按钮是不能点的「需要更新 ZCode」;兼容的与普通的照常「安装」。
  174. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1400, height: 900 });
  175. await openPluginSettings();
  176. await clickPluginControl("plugin-store-segment-personal");
  177. await waitForPluginCard(FUTURE);
  178. expect(await readInstallButton(FUTURE)).toEqual({
  179. disabled: true,
  180. blocked: "requires-update",
  181. text: "需要更新 ZCode",
  182. });
  183. expect(await readInstallButton(OK)).toEqual({ disabled: false, blocked: null, text: "安装" });
  184. expect(await readInstallButton(PLAIN)).toEqual({
  185. disabled: false,
  186. blocked: null,
  187. text: "安装",
  188. });
  189. // (b) 不兼容插件的详情页:按钮不能点,简介下有说明;照样显示含常驻代码与申请的权限。
  190. await openPluginCard(FUTURE);
  191. expect(await readInstallButton(FUTURE)).toMatchObject({
  192. disabled: true,
  193. blocked: "requires-update",
  194. });
  195. expect(
  196. await getPluginControlText("plugin-store-extension-incompatible", { pluginId: FUTURE }),
  197. ).toContain("更新 ZCode");
  198. const futureNotice = await readExtensionNotice(FUTURE);
  199. expect(futureNotice?.text).toContain("含常驻代码");
  200. expect(futureNotice?.grants).toEqual([{ grant: "sessions.read", text: "读取会话" }]);
  201. expect(futureNotice?.fullAccess).toBeNull();
  202. await backToStoreList();
  203. // (c) 兼容插件的详情页:档位按授权对话框的档位名列出,完全访问单独一行写明含义。
  204. await openPluginCard(OK);
  205. expect(await hasPluginControl("plugin-store-extension-incompatible", { pluginId: OK })).toBe(
  206. false,
  207. );
  208. const okNotice = await readExtensionNotice(OK);
  209. expect(okNotice?.text).toContain("含常驻代码");
  210. expect(okNotice?.grants).toEqual([
  211. { grant: "sessions.fullAccess", text: "读写会话(含完全访问)" },
  212. { grant: "notifications", text: "发送系统通知" },
  213. ]);
  214. expect(okNotice?.fullAccess).toMatch(/^完全访问:/);
  215. expect(okNotice?.noGrants).toBe(false);
  216. await backToStoreList();
  217. // (d) 普通插件:详情页没有扩展信息。
  218. await openPluginCard(PLAIN);
  219. expect(await readExtensionNotice(PLAIN)).toBeNull();
  220. await backToStoreList();
  221. await captureStoreViews("zh-CN-zai-light");
  222. await setPluginPresentation({ locale: "en-US", theme: "zai-dark", width: 1400, height: 900 });
  223. await captureStoreViews("en-US-zai-dark");
  224. await openPluginCard(FUTURE);
  225. expect(await readInstallButton(FUTURE)).toMatchObject({
  226. disabled: true,
  227. text: "Requires newer ZCode",
  228. });
  229. expect((await readExtensionNotice(FUTURE))?.text).toContain("Includes resident code");
  230. await backToStoreList();
  231. await setPluginPresentation({ locale: "zh-CN", theme: "zai-light", width: 1400, height: 900 });
  232. // (e) 兼容插件与普通插件照常从商店安装;装上后详情页换成「权限」区块,不再显示安装前的说明。
  233. await openPluginCard(OK);
  234. await installPluginThroughUi(OK);
  235. await waitUntilPluginOverview(
  236. (overview) => findInstalled(overview, OK) !== undefined,
  237. "the compatible extension plugin should install",
  238. 60000,
  239. );
  240. await browser.waitUntil(
  241. async () => !(await hasPluginControl("plugin-store-extension-notice", { pluginId: OK })),
  242. { timeout: 15000, timeoutMsg: "the pre-install notice should leave after install" },
  243. );
  244. await backToStoreList();
  245. await openPluginCard(PLAIN);
  246. await installPluginThroughUi(PLAIN);
  247. await waitUntilPluginOverview(
  248. (overview) => findInstalled(overview, PLAIN) !== undefined,
  249. "the plain plugin should install",
  250. 60000,
  251. );
  252. await backToStoreList();
  253. // (f) 目录升级:installed 的新版要 ^99.0.0,plain 的新版普通。CLI 两个都报 update-available。
  254. await bumpCatalog(market);
  255. await clickPluginControl("plugin-store-refresh");
  256. await waitUntilPluginOverview(
  257. (overview) =>
  258. findInstalled(overview, INSTALLED)?.updateStatus === "update-available" &&
  259. findInstalled(overview, INSTALLED)?.listing?.extensionSummary?.engines.zcode ===
  260. "^99.0.0" &&
  261. findInstalled(overview, PLAIN)?.updateStatus === "update-available",
  262. "both installed plugins should report a newer catalog version",
  263. 60000,
  264. );
  265. // App 显示前按兼容过滤:plain 有「可更新」,installed 没有角标、没有更新按钮。
  266. await waitForPluginControl("plugin-store-update-badge", { pluginId: PLAIN }, 30000);
  267. expect(await hasPluginControl("plugin-store-card-update", { pluginId: PLAIN })).toBe(true);
  268. expect(await hasPluginControl("plugin-store-update-badge", { pluginId: INSTALLED })).toBe(
  269. false,
  270. );
  271. expect(await hasPluginControl("plugin-store-card-update", { pluginId: INSTALLED })).toBe(false);
  272. // 刷新后个人分段还会列出其他市场:把本地市场的卡片滚到视野里再截图。
  273. await browser.execute((id) => {
  274. document
  275. .querySelector<HTMLElement>(`[data-testid="plugin-store-card"][data-plugin-id="${id}"]`)
  276. ?.scrollIntoView({ block: "center" });
  277. }, PLAIN);
  278. screenshots.push(await capturePluginManualReview(CASE_ID, "updates-zh-CN-zai-light"));
  279. await openPluginCard(INSTALLED);
  280. expect(await hasPluginControl("plugin-store-card-update", { pluginId: INSTALLED })).toBe(false);
  281. expect(await hasPluginControl("plugin-store-detail-update")).toBe(false);
  282. await backToStoreList();
  283. // (g) 「全部更新」只算 plain,执行后 installed 仍是 1.0.0,照常启用。
  284. await openManageView();
  285. await selectPluginSettingsScope("user");
  286. await waitForPluginControl("plugin-settings-update-all");
  287. expect(await getPluginControlText("plugin-settings-update-all")).toContain("(1)");
  288. screenshots.push(await capturePluginManualReview(CASE_ID, "update-all-zh-CN-zai-light"));
  289. await clickPluginControl("plugin-settings-update-all");
  290. await waitUntilPluginOverview(
  291. (overview) => findInstalled(overview, PLAIN)?.version === "2.0.0",
  292. "update-all should update the plain plugin",
  293. 60000,
  294. );
  295. await browser.waitUntil(async () => !(await hasPluginControl("plugin-settings-update-all")), {
  296. timeout: 15000,
  297. timeoutMsg: "update-all should leave once only the incompatible update remains",
  298. });
  299. const final = await getPluginOverview();
  300. expect(findInstalled(final, INSTALLED)).toMatchObject({
  301. version: "1.0.0",
  302. enabled: true,
  303. updateStatus: "update-available",
  304. });
  305. await setPluginPresentation({ locale: "en-US", theme: "zai-dark", width: 1400, height: 900 });
  306. screenshots.push(await capturePluginManualReview(CASE_ID, "after-update-all-en-US-zai-dark"));
  307. });
  308. });