conversation-session-plugin-skill-live-catalog.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // 运行:pnpm --filter @zcode/desktop exec wdio run wdio.conf.ts --spec ./test/e2e/conversation-session/plugins/conversation-session-plugin-skill-live-catalog.test.ts
  2. // 合同:docs/skills-live-catalog.md。打开的会话跟随 skill 目录变化:新 skill 只追加一行,
  3. // 插件停用后它的 skill 不再能加载,已发送的列表不改写、不发删除通知。
  4. import { mkdir, readFile, writeFile } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { clearAppData, getE2EAppDataPaths } from "#e2e/helpers/desktop-app";
  7. import { PLUGIN_MCP_PLUGIN_ID } from "#e2e/helpers/plugin-mcp-skill";
  8. import {
  9. getLatestUpstreamToolResultByToolCallId,
  10. getUpstreamRequestEvidence,
  11. waitForUpstreamRequestContaining,
  12. } from "#e2e/helpers/conversation-session-network";
  13. import {
  14. prepareV4ConversationE2E,
  15. sendV4Prompt,
  16. waitForV4AssistantMessageContaining,
  17. } from "#e2e/helpers/v4-conversation";
  18. import { ensureToolCrossProductFullAccessMode } from "#e2e/helpers/conversation-session-tool-cross-product";
  19. const FIRST_MARKER = "E2E_SKILL_LIVE_FIRST";
  20. const SECOND_MARKER = "E2E_SKILL_LIVE_SECOND";
  21. const THIRD_MARKER = "E2E_SKILL_LIVE_THIRD";
  22. const FIRST_REPLY = "skill-live-first-ok";
  23. const SECOND_REPLY = "skill-live-second-ok";
  24. const THIRD_REPLY = "skill-live-third-ok";
  25. const LOAD_LIVE_TOOL_ID = "toolu_e2e_skill_live_load";
  26. const LOAD_PLUGIN_TOOL_ID = "toolu_e2e_skill_live_plugin";
  27. const LIVE_SKILL_NAME = "e2e-live-skill";
  28. const LIVE_SKILL_LINE = `- ${LIVE_SKILL_NAME}: E2E live catalog skill marker`;
  29. // 正文标记只出现在 SKILL.md 与 tool_result 里,不参与 provider 匹配。
  30. const LIVE_SKILL_BODY = "skill-live-body-marker";
  31. const PLUGIN_SKILL_NAME = "e2e-plugin-mcp-ping:plugin-mcp-ping";
  32. const PLUGIN_SKILL_LINE = `- ${PLUGIN_SKILL_NAME}: E2E workspace Plugin skill marker`;
  33. describe("Skill live catalog in an open session E2E", () => {
  34. after(async () => {
  35. await browser.electron.restoreAllMocks();
  36. await clearAppData();
  37. });
  38. it("appends a new workspace skill and stops loading a disabled plugin skill", async function () {
  39. this.timeout(300000);
  40. await prepareV4ConversationE2E();
  41. await ensureToolCrossProductFullAccessMode();
  42. await sendV4Prompt(`${FIRST_MARKER}: Do not call tools; reply with exactly ${FIRST_REPLY}.`);
  43. await waitForUpstreamRequestContaining(FIRST_MARKER, 60000);
  44. await waitForV4AssistantMessageContaining(FIRST_REPLY);
  45. const first = await lastMainRequest(FIRST_MARKER);
  46. expect(countOf(first, PLUGIN_SKILL_LINE)).toBe(1);
  47. expect(countOf(first, LIVE_SKILL_LINE)).toBe(0);
  48. // 同一个会话里:先停用工作区插件,再新建工作区 skill。每次重读都是全量,
  49. // 读到新 skill 的那次一定也读到了停用配置。
  50. await disableWorkspacePlugin();
  51. await writeWorkspaceSkill();
  52. await sendV4Prompt(
  53. `${SECOND_MARKER}: Call Skill with ${LIVE_SKILL_NAME}, then reply with exactly ${SECOND_REPLY}.`,
  54. );
  55. await waitForV4AssistantMessageContaining(SECOND_REPLY);
  56. // Skill tool 未命中时立即全量重读一次:刚写入的 skill 不受去抖窗口影响。
  57. const loaded = await getLatestUpstreamToolResultByToolCallId(LOAD_LIVE_TOOL_ID);
  58. expect(loaded?.content).toContain(LIVE_SKILL_BODY);
  59. const second = await lastMainRequest(SECOND_MARKER);
  60. // 新 skill 追加一行;已发送的列表不改写,停用的插件 skill 不发删除通知。
  61. expect(countOf(second, LIVE_SKILL_LINE)).toBe(1);
  62. expect(countOf(second, PLUGIN_SKILL_LINE)).toBe(1);
  63. await sendV4Prompt(
  64. `${THIRD_MARKER}: Call Skill with ${PLUGIN_SKILL_NAME}, then reply with exactly ${THIRD_REPLY}.`,
  65. );
  66. await waitForV4AssistantMessageContaining(THIRD_REPLY);
  67. const refused = await getLatestUpstreamToolResultByToolCallId(LOAD_PLUGIN_TOOL_ID);
  68. // 找不到的 skill 由 Skill handler 返回失败、executor 包装(00-tool-change-chain.md「工具错误的包装与文字」)。
  69. expect(refused?.content).toContain(`<tool_use_error>Unknown skill: ${PLUGIN_SKILL_NAME}`);
  70. const third = await lastMainRequest(THIRD_MARKER);
  71. // 之后的请求不重复公告。
  72. expect(countOf(third, LIVE_SKILL_LINE)).toBe(1);
  73. });
  74. });
  75. async function disableWorkspacePlugin(): Promise<void> {
  76. const { workspace } = getE2EAppDataPaths();
  77. const configPath = join(workspace, ".zcode", "config.json");
  78. const config = JSON.parse(await readFile(configPath, "utf8")) as {
  79. plugins?: { enabledPlugins?: Record<string, boolean> };
  80. };
  81. await writeFile(
  82. configPath,
  83. `${JSON.stringify(
  84. {
  85. ...config,
  86. plugins: {
  87. ...config.plugins,
  88. enabledPlugins: { ...config.plugins?.enabledPlugins, [PLUGIN_MCP_PLUGIN_ID]: false },
  89. },
  90. },
  91. null,
  92. 2,
  93. )}\n`,
  94. "utf8",
  95. );
  96. }
  97. async function writeWorkspaceSkill(): Promise<void> {
  98. const { workspace } = getE2EAppDataPaths();
  99. const directory = join(workspace, ".zcode", "skills", LIVE_SKILL_NAME);
  100. await mkdir(directory, { recursive: true });
  101. await writeFile(
  102. join(directory, "SKILL.md"),
  103. `---\nname: ${LIVE_SKILL_NAME}\ndescription: E2E live catalog skill marker\n---\n\n${LIVE_SKILL_BODY}\n`,
  104. "utf8",
  105. );
  106. }
  107. /** 本轮最后一次主请求(不含标题 sidecar);它带着到目前为止的完整历史。 */
  108. async function lastMainRequest(marker: string): Promise<unknown> {
  109. const records = await getUpstreamRequestEvidence({ includes: [marker] });
  110. const record = records.at(-1);
  111. expect(record).toBeDefined();
  112. return record?.requestJson;
  113. }
  114. function countOf(requestJson: unknown, text: string): number {
  115. return JSON.stringify(requestJson).split(text).length - 1;
  116. }