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

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