conversation-session-plugin-store-try-states.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. import { cp, mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { dirname, join } from "node:path";
  3. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  4. import { clearAppData, setInputValueByTestIdDom } from "../helpers/desktop-app.js";
  5. import {
  6. ELECTRON_WINDOW_RECORDING_CAPTURE_MODE,
  7. startElectronWindowRecording,
  8. } from "../helpers/electron-window-recorder.js";
  9. import {
  10. getV4ComposerText,
  11. waitForV4MentionOptionPrefix,
  12. waitForV4Pane,
  13. } from "../helpers/v4-conversation.js";
  14. import {
  15. PLUGIN_LIFECYCLE_EXAMPLE_PROMPT,
  16. PLUGIN_LIFECYCLE_ID,
  17. PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  18. capturePluginManualReview,
  19. clickPluginControl,
  20. createPluginLifecycleFixture,
  21. openPluginCard,
  22. openPluginSettings,
  23. pluginStoragePath,
  24. requirePluginManualReview,
  25. getPluginList,
  26. waitUntilPluginList,
  27. waitForPluginControl,
  28. } from "../helpers/plugin-management-lifecycle.js";
  29. const CASE_TIMEOUT_MS = 300_000;
  30. const CASE_ID = "PLG12-TRY-STATES";
  31. const ARTIFACT_DIR = join(
  32. process.cwd(),
  33. ".e2e-artifacts",
  34. "plugin-management",
  35. CASE_ID.toLowerCase(),
  36. );
  37. const VIDEO_PATH = join(ARTIFACT_DIR, "plugin-store-try-states.webm");
  38. const VIDEO_MANIFEST_PATH = join(ARTIFACT_DIR, "plugin-store-try-states.recording.json");
  39. describe("PLG12 Plugin 商店试用状态视觉验收", () => {
  40. after(async () => {
  41. await browser.electron.restoreAllMocks();
  42. await clearAppData();
  43. });
  44. it("覆盖未安装、enabled、disabled 与保留的 @ Plugin 候选面板", async function () {
  45. this.timeout(CASE_TIMEOUT_MS);
  46. requirePluginManualReview();
  47. const fixture = await createPluginLifecycleFixture("1.0.0");
  48. await seedLifecycleMarketplaceCache(fixture.marketplaceRoot);
  49. await openPluginSettings();
  50. await clickPluginControl("plugin-store-segment-personal");
  51. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  52. await waitForPluginControl("plugin-store-example-prompt", {
  53. pluginId: PLUGIN_LIFECYCLE_ID,
  54. });
  55. const recording = await startElectronWindowRecording({
  56. frameIntervalMs: 250,
  57. outputPath: VIDEO_PATH,
  58. preRollDurationMs: 500,
  59. });
  60. try {
  61. await capturePluginManualReview(CASE_ID, "01-uninstalled-detail");
  62. // 未安装:点击示例提示词只安装并留在详情页,不能提前创建对话草稿。
  63. await clickPluginControl("plugin-store-example-prompt", {
  64. pluginId: PLUGIN_LIFECYCLE_ID,
  65. });
  66. await waitForPluginControl(
  67. "plugin-store-item-menu",
  68. { pluginId: PLUGIN_LIFECYCLE_ID },
  69. 120_000,
  70. );
  71. expect(await isPluginSettingsVisible()).toBe(true);
  72. await waitForEnabledState(true);
  73. await capturePluginManualReview(CASE_ID, "02-installed-stays-detail");
  74. // enabled:再次点击才进入标准新草稿;canonical + prompt 只预填,不发送。
  75. await clickPluginControl("plugin-store-example-prompt", {
  76. pluginId: PLUGIN_LIFECYCLE_ID,
  77. });
  78. await expectTryDraft("enabled");
  79. await capturePluginManualReview(CASE_ID, "03-enabled-try-draft");
  80. expect(await countCapturedRequestsContaining(PLUGIN_LIFECYCLE_EXAMPLE_PROMPT)).toBe(0);
  81. // disabled:仍允许生成同形草稿;disabled_in_session 只在真正发送时由 Agent 判定。
  82. await openPluginSettings();
  83. await clickPluginControl("plugin-store-segment-personal");
  84. await openPluginCard(PLUGIN_LIFECYCLE_ID);
  85. // 详情页启停已进入真实菜单,旧独立开关仅保留在管理页。
  86. await clickPluginControl("plugin-store-item-menu", { pluginId: PLUGIN_LIFECYCLE_ID });
  87. await clickPluginControl("plugin-store-menu-enabled", { pluginId: PLUGIN_LIFECYCLE_ID });
  88. await waitForEnabledSwitchState(false);
  89. await capturePluginManualReview(CASE_ID, "04-disabled-detail");
  90. await clickPluginControl("plugin-store-example-prompt", {
  91. pluginId: PLUGIN_LIFECYCLE_ID,
  92. });
  93. await expectTryDraft("disabled");
  94. await capturePluginManualReview(CASE_ID, "05-disabled-try-draft");
  95. expect(await countCapturedRequestsContaining(PLUGIN_LIFECYCLE_EXAMPLE_PROMPT)).toBe(0);
  96. // 试用入口不能替代或关闭旧能力:返回 composer 后 @ Plugin 候选仍可打开。
  97. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "@", {
  98. timeout: 15_000,
  99. timeoutMsg: "PLG12 visual review could not edit the v4 composer",
  100. });
  101. const optionId = await waitForV4MentionOptionPrefix("plugin:", 30_000);
  102. expect(optionId.startsWith("plugin:")).toBe(true);
  103. await capturePluginManualReview(CASE_ID, "06-at-plugin-picker-retained");
  104. } finally {
  105. const recordingResult = await recording.stop({
  106. tailDurationMs: 1_000,
  107. timeoutMs: 60_000,
  108. });
  109. expect(recordingResult.captureMode).toBe(ELECTRON_WINDOW_RECORDING_CAPTURE_MODE);
  110. expect(recordingResult.frameCount).toBeGreaterThan(1);
  111. expect(recordingResult.captureError).toBeUndefined();
  112. await writeFile(
  113. VIDEO_MANIFEST_PATH,
  114. `${JSON.stringify(
  115. {
  116. caseId: CASE_ID,
  117. frameCount: recordingResult.frameCount,
  118. states: [
  119. "uninstalled-install-only",
  120. "installed-enabled-canonical-draft",
  121. "installed-disabled-canonical-draft",
  122. "composer-at-plugin-picker-retained",
  123. ],
  124. video_capture_mode: recordingResult.captureMode,
  125. videoPath: recordingResult.videoPath,
  126. },
  127. null,
  128. 2,
  129. )}\n`,
  130. "utf-8",
  131. );
  132. }
  133. });
  134. });
  135. async function expectTryDraft(pluginState: "enabled" | "disabled"): Promise<void> {
  136. await waitForV4Pane(
  137. (state) => state.sessionId === "draft" && !state.canStop,
  138. `Plugin Store try did not enter an idle draft for ${pluginState} Plugin`,
  139. 30_000,
  140. );
  141. await browser.waitUntil(
  142. async () => {
  143. const composerText = (await getV4ComposerText()) ?? "";
  144. return (
  145. composerText.includes(`plugin://${PLUGIN_LIFECYCLE_ID}`) &&
  146. composerText.endsWith(` ${PLUGIN_LIFECYCLE_EXAMPLE_PROMPT}`)
  147. );
  148. },
  149. {
  150. timeout: 30_000,
  151. timeoutMsg: `Plugin Store try did not prefill canonical Plugin reference and prompt for ${pluginState} Plugin`,
  152. },
  153. );
  154. }
  155. async function isPluginSettingsVisible(): Promise<boolean> {
  156. return browser.execute(() =>
  157. Boolean(document.querySelector('[data-testid="plugin-store-root"]')),
  158. );
  159. }
  160. async function seedLifecycleMarketplaceCache(marketplaceRoot: string): Promise<void> {
  161. const managedMarketplaceRoot = pluginStoragePath("marketplaces", PLUGIN_LIFECYCLE_MARKETPLACE_ID);
  162. await mkdir(dirname(managedMarketplaceRoot), { recursive: true });
  163. await cp(marketplaceRoot, managedMarketplaceRoot, {
  164. force: true,
  165. recursive: true,
  166. });
  167. // 插件运行时读取的是托管缓存根目录的 marketplace.json;fixture 原始目录使用
  168. // Claude marketplace 的 .claude-plugin/marketplace.json 布局,直接复制目录会让
  169. // known record 存在但商店无法加载 manifest,因此这里补齐运行时缓存形态。
  170. await writeFile(
  171. join(managedMarketplaceRoot, "marketplace.json"),
  172. await readFile(join(marketplaceRoot, ".claude-plugin", "marketplace.json"), "utf-8"),
  173. "utf-8",
  174. );
  175. await writeFile(
  176. pluginStoragePath("known_marketplaces.json"),
  177. `${JSON.stringify(
  178. {
  179. version: 1,
  180. marketplaces: [
  181. // Settings 首次进入会按需刷新两个官方市场;若不预置成功时间,Claude
  182. // marketplace 的 git clone 会占用插件协议串行锁 90 秒,令本地 fixture 的
  183. // install/enable 操作看似卡死。该隔离 HOME 只需声明已刷新,PLG12 不验证公网目录。
  184. {
  185. addedAt: "2026-07-31T00:00:00.000Z",
  186. description: "Deterministic offline ZCode marketplace marker",
  187. id: "zcode-plugins-official",
  188. lastUpdated: "2099-01-01T00:00:00.000Z",
  189. name: "zcode-plugins-official",
  190. pluginCount: 1,
  191. source: {
  192. source: "url",
  193. url: "https://cdn-zcode.z.ai/zcode/official-plugin/marketplace.json",
  194. },
  195. },
  196. {
  197. addedAt: "2026-07-31T00:00:00.000Z",
  198. description: "Deterministic offline Claude marketplace marker",
  199. id: "claude-plugins-official",
  200. lastUpdated: "2099-01-01T00:00:00.000Z",
  201. name: "claude-plugins-official",
  202. pluginCount: 1,
  203. source: {
  204. repo: "anthropics/claude-plugins-official",
  205. source: "github",
  206. },
  207. },
  208. {
  209. addedAt: "2026-07-31T00:00:00.000Z",
  210. description: "Deterministic PLG12 visual review marketplace",
  211. id: PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  212. name: PLUGIN_LIFECYCLE_MARKETPLACE_ID,
  213. pluginCount: 8,
  214. source: {
  215. path: marketplaceRoot,
  216. source: "directory",
  217. },
  218. },
  219. ],
  220. },
  221. null,
  222. 2,
  223. )}\n`,
  224. "utf-8",
  225. );
  226. }
  227. async function waitForEnabledState(enabled: boolean): Promise<void> {
  228. await waitUntilPluginList(
  229. (list) =>
  230. list.plugins.some(
  231. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID && plugin.enabled === enabled,
  232. ),
  233. `Plugin enabled state did not become ${enabled}`,
  234. );
  235. }
  236. async function waitForEnabledSwitchState(enabled: boolean): Promise<void> {
  237. // 试用入口创建未归类草稿,市场启停写入当前 workspace;不能再查询初始项目的覆盖值。
  238. // 商店替换主区域时 workspace badge 不挂载,读取当前 Tab 的只读身份。
  239. const workspacePath = await browser.execute(
  240. () =>
  241. (
  242. window as Window & {
  243. __zcodeTabStoreE2E?: { getState(): { activeWorkspacePath?: string | null } };
  244. }
  245. ).__zcodeTabStoreE2E?.getState().activeWorkspacePath,
  246. );
  247. if (!workspacePath) throw new Error("Plugin 试用没有当前 workspace");
  248. await browser.waitUntil(
  249. async () =>
  250. (await getPluginList(workspacePath)).plugins.some(
  251. (plugin) => plugin.id === PLUGIN_LIFECYCLE_ID && plugin.enabled === enabled,
  252. ),
  253. {
  254. timeout: 30000,
  255. timeoutMsg: `Plugin enabled state did not become ${enabled} in ${workspacePath}`,
  256. },
  257. );
  258. }
  259. async function countCapturedRequestsContaining(marker: string): Promise<number> {
  260. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
  261. if (!capturePath) return 0;
  262. try {
  263. const artifact = JSON.parse(await readFile(capturePath, "utf-8")) as {
  264. records?: Array<{ requestJson?: unknown }>;
  265. };
  266. return (artifact.records ?? []).filter((record) =>
  267. JSON.stringify(record.requestJson).includes(marker),
  268. ).length;
  269. } catch {
  270. return 0;
  271. }
  272. }