conversation-session-plugin-reference.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // PLG01(wire 半区)+ PLG12/PLG15(商店试用)+ S01(plugins 分组半区)formal E2E:
  2. // 1) @ 面板出现 plugins 分组候选,选中后 composer 插入 canonical `plugin://` markdown;
  3. // 2) Plugin 商店详情页顶部 Try now 只预填 canonical 引用;示例提示词继续预填引用 + prompt;
  4. // 两条入口都不自动发送;
  5. // 3) 发送含 canonical Plugin 引用的消息后,provider 请求包含 identifiers-only 的
  6. // `plugin_reference` reminder(确定性 replay fixture 以 bodyIncludes "<plugin_reference>"
  7. // 才放行主响应,并驱动一次 Read 测试工具调用),最终回复 marker 出现在时间线;
  8. // reminder 不出现在用户可见时间线,请求中不含路径/env 等禁injected 字段。
  9. // 引用目标使用默认启用的官方纯内容插件 skill-creator(stable id 确定,无需安装步骤)。
  10. import { mkdir, writeFile } from "node:fs/promises";
  11. import { join } from "node:path";
  12. import {
  13. clearAppData,
  14. DEFAULT_WORKSPACE,
  15. setInputValueByTestIdDom,
  16. } from "../helpers/desktop-app.js";
  17. import { waitForUpstreamNetworkCapture } from "../helpers/upstream-capture.js";
  18. import {
  19. TID_PROMPT_SUGGESTION_OPTION,
  20. TID_V4_COMPOSER_INPUT,
  21. testId,
  22. } from "@zcode/shared";
  23. import {
  24. clickV4SlashOption,
  25. getV4ComposerText,
  26. hasV4SlashPanel,
  27. prepareV4ConversationE2E,
  28. sendV4Prompt,
  29. startNewV4Draft,
  30. waitForV4MentionOptionPrefix,
  31. waitForV4Pane,
  32. waitForV4TimelineContaining,
  33. } from "../helpers/v4-conversation.js";
  34. import {
  35. clickPluginControl,
  36. openPluginCard,
  37. openPluginSettings,
  38. } from "../helpers/plugin-management-lifecycle.js";
  39. const PLUGIN_STABLE_ID = "skill-creator@zcode-plugins-official";
  40. const STORE_TRY_PLUGIN_STABLE_ID = "zcode-guide@zcode-plugins-official";
  41. const STORE_TRY_PLUGIN_ICON =
  42. "https://cdn-zcode.z.ai/zcode/official-plugin/assets/zcode-guide/icon.png";
  43. const PROMPT_MARKER = "E2E_PLUGIN_REFERENCE";
  44. const REPLY_MARKER = "PLUGIN_REFERENCE_REPLY_OK";
  45. const NOTE_RELATIVE_PATH = "plugin-reference-e2e/note.md";
  46. const NOTE_BODY = "PLUGIN_REFERENCE_NOTE_BODY";
  47. const SCREENSHOT_DIR = join(process.cwd(), ".e2e-artifacts", "plugin-reference");
  48. // 截图只作为人工复盘 artifact,不作为通过判定(conversation-session-ui-testid-contract)。
  49. async function captureEvidence(label: string): Promise<void> {
  50. await mkdir(SCREENSHOT_DIR, { recursive: true });
  51. await browser.saveScreenshot(join(SCREENSHOT_DIR, `${label}.png`));
  52. }
  53. async function readPluginMentionPresentation(pluginId: string): Promise<{
  54. found: boolean;
  55. iconSrc: string | null;
  56. label: string;
  57. }> {
  58. return browser.execute(
  59. (inputTestId, mentionId) => {
  60. const input = document.querySelector<HTMLElement>(
  61. `[data-testid="${inputTestId}"]`,
  62. );
  63. const mention = Array.from(
  64. input?.querySelectorAll<HTMLElement>("[data-mention-category='plugins']") ?? [],
  65. ).find((element) => element.getAttribute("data-mention-id") === mentionId);
  66. return {
  67. found: Boolean(mention),
  68. iconSrc: mention?.querySelector<HTMLImageElement>(
  69. "img[data-plugin-mention-icon='true']",
  70. )?.src ?? null,
  71. label: mention?.textContent ?? "",
  72. };
  73. },
  74. TID_V4_COMPOSER_INPUT,
  75. `plugin:${pluginId}`,
  76. );
  77. }
  78. // 走产品自身的 editor.focus()(和 Try now 的 requestFocus 同一条路径),
  79. // 让原生 selection 按真实逻辑回灌,再发真实按键。
  80. async function focusV4Composer(): Promise<void> {
  81. await browser.execute((inputTestId) => {
  82. const input = document.querySelector<
  83. HTMLElement & { __zcodeLexicalInputE2E?: { focus: () => void } }
  84. >(`[data-testid="${inputTestId}"]`);
  85. input?.__zcodeLexicalInputE2E?.focus();
  86. }, TID_V4_COMPOSER_INPUT);
  87. }
  88. describe("PLG plugin 引用:@ 面板 plugins 分组与 plugin_reference reminder", () => {
  89. after(async () => {
  90. await browser.electron.restoreAllMocks();
  91. await clearAppData();
  92. });
  93. it("@ 面板展示 plugins 分组候选,选中后插入 canonical plugin:// markdown", async () => {
  94. await prepareV4ConversationE2E();
  95. await startNewV4Draft();
  96. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "@zcode-guide", {
  97. timeout: 15000,
  98. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  99. });
  100. const pluginOptionId = await waitForV4MentionOptionPrefix(
  101. `plugin:${STORE_TRY_PLUGIN_STABLE_ID}`,
  102. 30000,
  103. );
  104. expect(pluginOptionId).toBe(`plugin:${STORE_TRY_PLUGIN_STABLE_ID}`);
  105. const pickerIcon = await browser.execute(
  106. (optionTestId) =>
  107. document
  108. .querySelector<HTMLElement>(`[data-testid="${optionTestId}"]`)
  109. ?.querySelector<HTMLImageElement>("img")?.src ?? null,
  110. testId(TID_PROMPT_SUGGESTION_OPTION, pluginOptionId),
  111. );
  112. expect(pickerIcon).toBe(STORE_TRY_PLUGIN_ICON);
  113. await captureEvidence("01-picker-plugins-group");
  114. expect(await clickV4SlashOption(pluginOptionId)).toBe(true);
  115. await browser.waitUntil(
  116. async () => ((await getV4ComposerText()) ?? "").includes("plugin://"),
  117. {
  118. timeout: 15000,
  119. timeoutMsg: "选中 plugin mention 后 composer 没有插入 canonical plugin:// markdown",
  120. },
  121. );
  122. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  123. timeout: 15000,
  124. timeoutMsg: "选中 plugin mention 后面板没有关闭",
  125. });
  126. expect(await readPluginMentionPresentation(STORE_TRY_PLUGIN_STABLE_ID)).toEqual({
  127. found: true,
  128. iconSrc: STORE_TRY_PLUGIN_ICON,
  129. label: "zcode-guide",
  130. });
  131. await captureEvidence("02-composer-plugin-chip");
  132. // 清空草稿,避免影响后续用例。
  133. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  134. timeout: 15000,
  135. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  136. });
  137. });
  138. it("Plugin 商店顶部 Try now 只预填 canonical 引用且不自动发送", async () => {
  139. await openPluginSettings();
  140. await openPluginCard(STORE_TRY_PLUGIN_STABLE_ID);
  141. await captureEvidence("03-plugin-store-try-now-detail");
  142. await clickPluginControl("plugin-store-try-now", {
  143. pluginId: STORE_TRY_PLUGIN_STABLE_ID,
  144. });
  145. await waitForV4Pane(
  146. (state) => state.sessionId === "draft" && !state.canStop,
  147. "Plugin 商店 Try now 没有通过标准新建任务进入空闲草稿",
  148. 30000,
  149. );
  150. await browser.waitUntil(
  151. async () => ((await getV4ComposerText()) ?? "").includes(
  152. `plugin://${STORE_TRY_PLUGIN_STABLE_ID}`,
  153. ),
  154. {
  155. timeout: 30000,
  156. timeoutMsg: "Plugin 商店 Try now 没有预填 canonical Plugin 引用",
  157. },
  158. );
  159. const tryNowText = (await getV4ComposerText()) ?? "";
  160. for (const prompt of [
  161. "How do I configure MCP servers in ZCode?",
  162. "Diagnose my current ZCode setup",
  163. "ZCode 里怎么配置 MCP 服务器?",
  164. "帮我诊断当前的 ZCode 配置",
  165. ]) {
  166. expect(tryNowText).not.toContain(prompt);
  167. }
  168. const tryNowMention = await readPluginMentionPresentation(STORE_TRY_PLUGIN_STABLE_ID);
  169. expect(tryNowMention.found).toBe(true);
  170. expect(tryNowMention.iconSrc).toBe(STORE_TRY_PLUGIN_ICON);
  171. expect(["ZCode Guide", "ZCode 使用指南"]).toContain(tryNowMention.label);
  172. await captureEvidence("04-plugin-store-try-now-draft");
  173. // 回归守卫:Try now 只预填引用、没有尾随文本时,光标会落在 mention token 内部,
  174. // 真实按一下空格会让 Lexical 按 token 语义整节点替换,引用直接消失(v3.7.1~v3.7.6)。
  175. // 这里必须走真实按键,jsdom 结构断言覆盖不到原生 selection 回灌这一环。
  176. await focusV4Composer();
  177. await browser.keys(" ");
  178. const afterSpaceMention = await readPluginMentionPresentation(STORE_TRY_PLUGIN_STABLE_ID);
  179. expect(afterSpaceMention.found).toBe(true);
  180. expect((await getV4ComposerText()) ?? "").toContain(
  181. `plugin://${STORE_TRY_PLUGIN_STABLE_ID}`,
  182. );
  183. await captureEvidence("04b-plugin-store-try-now-after-space");
  184. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  185. timeout: 15000,
  186. timeoutMsg: "Plugin 商店 Try now 后的 v4 composer 不可编辑",
  187. });
  188. });
  189. it("Plugin 商店示例提示词预填 canonical 引用与提示词但不自动发送", async () => {
  190. await openPluginSettings();
  191. await openPluginCard(STORE_TRY_PLUGIN_STABLE_ID);
  192. await clickPluginControl("plugin-store-example-prompt", {
  193. pluginId: STORE_TRY_PLUGIN_STABLE_ID,
  194. });
  195. await waitForV4Pane(
  196. (state) => state.sessionId === "draft" && !state.canStop,
  197. "Plugin 商店试用没有通过标准新建任务进入空闲草稿",
  198. 30000,
  199. );
  200. await browser.waitUntil(
  201. async () => {
  202. const text = (await getV4ComposerText()) ?? "";
  203. return (
  204. text.includes(`plugin://${STORE_TRY_PLUGIN_STABLE_ID}`) &&
  205. [
  206. "How do I configure MCP servers in ZCode?",
  207. "Diagnose my current ZCode setup",
  208. "ZCode 里怎么配置 MCP 服务器?",
  209. "帮我诊断当前的 ZCode 配置",
  210. ].some((prompt) => text.includes(prompt))
  211. );
  212. },
  213. {
  214. timeout: 30000,
  215. timeoutMsg: "Plugin 商店试用没有预填 canonical Plugin 引用与示例提示词",
  216. },
  217. );
  218. const storeMention = await readPluginMentionPresentation(STORE_TRY_PLUGIN_STABLE_ID);
  219. expect(storeMention.found).toBe(true);
  220. expect(storeMention.iconSrc).toBe(STORE_TRY_PLUGIN_ICON);
  221. expect(["ZCode Guide", "ZCode 使用指南"]).toContain(storeMention.label);
  222. await captureEvidence("03-plugin-store-try-draft");
  223. // 试用只预填,不自动发送;保持 draft 且手动清空,避免影响 wire 用例。
  224. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  225. timeout: 15000,
  226. timeoutMsg: "Plugin 商店试用后的 v4 composer 不可编辑",
  227. });
  228. });
  229. it("发送 canonical plugin 引用后 provider 收到 identifiers-only reminder 并驱动测试工具", async function () {
  230. this.timeout(240000);
  231. // Read 测试工具的目标文件(workspace 相对路径,遵守 e2e runtime 路径契约)。
  232. await mkdir(join(DEFAULT_WORKSPACE, "plugin-reference-e2e"), { recursive: true });
  233. await writeFile(join(DEFAULT_WORKSPACE, "plugin-reference-e2e", "note.md"), NOTE_BODY, "utf8");
  234. await sendV4Prompt(
  235. `${PROMPT_MARKER} read ${NOTE_RELATIVE_PATH} with [@skill-creator](plugin://${PLUGIN_STABLE_ID})`,
  236. );
  237. await waitForV4TimelineContaining(REPLY_MARKER, 60000);
  238. await waitForV4Pane(
  239. (state) => !state.canStop && state.sessionId !== "draft" && state.sessionId !== null,
  240. "plugin 引用回合没有回到空闲态",
  241. 60000,
  242. );
  243. await captureEvidence("04-timeline-plugin-reference-reply");
  244. // wire 断言:主请求包含且仅包含一条 identifiers-only plugin_reference reminder。
  245. const capture = await waitForUpstreamNetworkCapture("<plugin_reference>");
  246. const serialized = JSON.stringify(capture.requestJson);
  247. expect(serialized).toContain(PLUGIN_STABLE_ID);
  248. expect((serialized.match(/<plugin_reference>/g) ?? []).length).toBe(1);
  249. // identifiers-only:禁injected 字段(路径、插件环境变量、rootPath 字段名)不得进入 provider 输入。
  250. expect(serialized).not.toContain("ZCODE_PLUGIN_ROOT");
  251. expect(serialized).not.toContain('"rootPath"');
  252. // reminder 是 model-only:用户可见时间线不得渲染 plugin_reference 标签。
  253. const timelineHasReminder = await browser.execute(() =>
  254. (document.body.textContent ?? "").includes("<plugin_reference>"),
  255. );
  256. expect(timelineHasReminder).toBe(false);
  257. });
  258. });