conversation-session-v4-mention.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { openWorkspaceDraftFromSidebar } from "#e2e/helpers/workspace-agent-warmup";
  2. // S01 / S05 / S08 门禁:composer 聚合入口、兼容触发器与 Session Skill catalog 生命周期。
  3. // 证据层 L3:
  4. // 1) 首轮固定回复建立一个 session(写入 sessions-index);
  5. // 2) 新建 draft → `@` 与 `#` 都能发现同一 sessions-index 候选;
  6. // 3) 两条入口选中后都写入原 `#session-id` canonical mention;
  7. // 4) `$` / `¥` / `¥` 继续打开 Skills 面板并写入原 Skill mention;
  8. // 5) `+` 菜单展示 @ / / / $ 底部提示,手动输入 $ 打开技能候选;
  9. // 6) Session 初始化后手动新增 SKILL.md,旧 Session 目录冻结,新建任务重新发现。
  10. import { mkdir, rm, writeFile } from "node:fs/promises";
  11. import { join } from "node:path";
  12. import {
  13. clearAppData,
  14. getE2EAppDataPaths,
  15. setInputValueByTestIdDom,
  16. } from "#e2e/helpers/desktop-app";
  17. import { TID_CHAT_ATTACHMENT_BUTTON, TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  18. import {
  19. clickV4SlashOption,
  20. getV4ComposerText,
  21. getV4SlashOptionIds,
  22. hasV4SlashPanel,
  23. prepareV4ConversationE2E,
  24. selectV4TaskById,
  25. sendV4Prompt,
  26. waitForV4MentionOptionPrefix,
  27. waitForV4Pane,
  28. waitForV4TimelineContaining,
  29. } from "#e2e/helpers/v4-conversation";
  30. const SESSION_SKILL_NAME = "e2e-session-skill-catalog";
  31. const SESSION_SKILL_DIRECTORY = join(
  32. getE2EAppDataPaths().workspace,
  33. ".zcode",
  34. "skills",
  35. SESSION_SKILL_NAME,
  36. );
  37. const SESSION_SKILL_PATH = join(SESSION_SKILL_DIRECTORY, "SKILL.md");
  38. function isSessionSkillOption(optionId: string) {
  39. return optionId.startsWith("skill:") && optionId.includes(SESSION_SKILL_NAME);
  40. }
  41. async function waitForSessionSkillAbsent(trigger: "/" | "$") {
  42. let snapshot: { hasTarget: boolean; status: string | null } | undefined;
  43. let stableEmptyPolls = 0;
  44. await browser.waitUntil(
  45. async () => {
  46. if (!(await hasV4SlashPanel())) {
  47. stableEmptyPolls = 0;
  48. return false;
  49. }
  50. snapshot = await browser.execute((skillName) => {
  51. const optionIds = Array.from(
  52. document.querySelectorAll<HTMLElement>("[data-option-id]"),
  53. ).map((element) => element.dataset.optionId ?? "");
  54. const status = document
  55. .querySelector<HTMLElement>('[data-section-id="skills"][data-status]')
  56. ?.getAttribute("data-status");
  57. return {
  58. hasTarget: optionIds.some(
  59. (optionId) => optionId.startsWith("skill:") && optionId.includes(skillName),
  60. ),
  61. status: status ?? null,
  62. };
  63. }, SESSION_SKILL_NAME);
  64. if (snapshot.hasTarget || snapshot.status === "empty" || snapshot.status === "error") {
  65. return true;
  66. }
  67. if (snapshot.status === "loading") {
  68. stableEmptyPolls = 0;
  69. return false;
  70. }
  71. // `$` 的单分组面板在过滤结果为空时会直接省略 Skills row,因此没有 explicit empty 状态。
  72. // 连续观察到面板存在、无 loading 且无目标候选,避免把首帧未完成误判为最终空结果。
  73. stableEmptyPolls += 1;
  74. return stableEmptyPolls >= 3;
  75. },
  76. {
  77. timeout: 15000,
  78. timeoutMsg: `${trigger} 没有完成旧 Session Skill catalog 查询`,
  79. },
  80. );
  81. expect(snapshot?.status).not.toBe("error");
  82. expect(snapshot?.hasTarget).toBe(false);
  83. }
  84. async function waitForSessionSkillPresent(trigger: "/" | "$") {
  85. let optionId: string | undefined;
  86. await browser.waitUntil(
  87. async () => {
  88. optionId = (await getV4SlashOptionIds()).find(isSessionSkillOption);
  89. return Boolean(optionId);
  90. },
  91. {
  92. timeout: 15000,
  93. timeoutMsg: `${trigger} 没有展示新任务重新发现的 Skill: ${SESSION_SKILL_NAME}`,
  94. },
  95. );
  96. return optionId as string;
  97. }
  98. describe("S01 / S05 / S08:composer 候选入口与 Session Skill catalog", () => {
  99. before(async () => {
  100. await rm(SESSION_SKILL_DIRECTORY, { recursive: true, force: true });
  101. });
  102. after(async () => {
  103. await browser.electron.restoreAllMocks();
  104. await rm(SESSION_SKILL_DIRECTORY, { recursive: true, force: true });
  105. await clearAppData();
  106. });
  107. it("@ 与 # 共用 sessions-index,+ 菜单突出 @、/ 和 $", async () => {
  108. await prepareV4ConversationE2E();
  109. // 1) 建立一个 session(供 @ / # 目录共同命中)
  110. await sendV4Prompt("E2E_V4_MENTION_SEED 建立可被 mention 的会话");
  111. await waitForV4TimelineContaining("V4_MENTION_SEED_OK", 45000);
  112. const seedPane = await waitForV4Pane(
  113. (s) => !s.canStop && s.sessionId !== "draft" && s.sessionId !== null,
  114. "首轮没有回到空闲态且绑定 session",
  115. 45000,
  116. );
  117. const seedSessionId = seedPane.sessionId;
  118. if (!seedSessionId || seedSessionId === "draft") {
  119. throw new Error(`没有拿到可引用的 seed session: ${JSON.stringify(seedPane)}`);
  120. }
  121. // 2) 新建 draft → 干净输入框
  122. // 全局新建会分配虚拟目录;Hook、Skill 与会话候选必须继续使用夹具所属项目。
  123. await openWorkspaceDraftFromSidebar(getE2EAppDataPaths().workspace);
  124. // 3) `@` 聚合入口加载 sessions-index,并仍写入 # canonical mention。
  125. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "@E2E_V4_MENTION_SEED", {
  126. timeout: 15000,
  127. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  128. });
  129. const atSessionOptionId = await waitForV4MentionOptionPrefix("session:");
  130. expect(atSessionOptionId.startsWith("session:")).toBe(true);
  131. expect(await clickV4SlashOption(atSessionOptionId)).toBe(true);
  132. await browser.waitUntil(
  133. async () => ((await getV4ComposerText()) ?? "").includes(`#${seedSessionId}`),
  134. {
  135. timeout: 15000,
  136. timeoutMsg: "@ 会话候选没有写入原 #session-id canonical mention",
  137. },
  138. );
  139. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  140. timeout: 15000,
  141. timeoutMsg: "选中 @ 会话候选后面板没有关闭",
  142. });
  143. // 4) 兼容 `#` 面板继续工作,并与 @ 使用同一 sessions-index authority。
  144. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "#E2E_V4_MENTION_SEED", {
  145. timeout: 15000,
  146. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  147. });
  148. const hashSessionOptionId = await waitForV4MentionOptionPrefix("session:");
  149. expect(hashSessionOptionId).toBe(atSessionOptionId);
  150. expect(await clickV4SlashOption(hashSessionOptionId)).toBe(true);
  151. await browser.waitUntil(
  152. async () => ((await getV4ComposerText()) ?? "").includes(`#${seedSessionId}`),
  153. {
  154. timeout: 15000,
  155. timeoutMsg: "# 会话候选没有写入原 #session-id canonical mention",
  156. },
  157. );
  158. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  159. timeout: 15000,
  160. timeoutMsg: "选中 # 会话候选后面板没有关闭",
  161. });
  162. // 5) Skills 旧触发器(含两种 Yen 键盘别名)继续打开同一候选面板。
  163. let expectedSkillOptionId: string | undefined;
  164. let expectedSkillMarkdown: string | undefined;
  165. for (const skillTrigger of ["$", "¥", "¥"] as const) {
  166. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, skillTrigger, {
  167. timeout: 15000,
  168. timeoutMsg: `无法输入 Skills 兼容触发器 ${skillTrigger}`,
  169. });
  170. const skillOptionId = await waitForV4MentionOptionPrefix("skill:");
  171. expect(skillOptionId.startsWith("skill:")).toBe(true);
  172. if (expectedSkillOptionId) {
  173. expect(skillOptionId).toBe(expectedSkillOptionId);
  174. } else {
  175. expectedSkillOptionId = skillOptionId;
  176. }
  177. expect(await clickV4SlashOption(skillOptionId)).toBe(true);
  178. let selectedSkillMarkdown: string | undefined;
  179. await browser.waitUntil(
  180. async () => {
  181. selectedSkillMarkdown = (await getV4ComposerText()) ?? undefined;
  182. return Boolean(
  183. selectedSkillMarkdown?.startsWith("[$") && selectedSkillMarkdown.includes("SKILL.md)"),
  184. );
  185. },
  186. {
  187. timeout: 15000,
  188. timeoutMsg: `${skillTrigger} 候选没有写入原 Skill canonical mention`,
  189. },
  190. );
  191. if (expectedSkillMarkdown) {
  192. expect(selectedSkillMarkdown).toBe(expectedSkillMarkdown);
  193. } else {
  194. expectedSkillMarkdown = selectedSkillMarkdown;
  195. }
  196. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  197. timeout: 15000,
  198. timeoutMsg: `选中 ${skillTrigger} Skill 候选后面板没有关闭`,
  199. });
  200. }
  201. // 6) + 菜单以底部提示介绍快捷输入;提示不再是可点击的菜单项。
  202. // 放在 mention 断言之后,避免 Radix 菜单的关闭焦点与 Lexical 输入竞争。
  203. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  204. timeout: 15000,
  205. timeoutMsg: "验证动作菜单前无法清空输入框",
  206. });
  207. const actionMenuButton = await $(`[data-testid="${TID_CHAT_ATTACHMENT_BUTTON}"]`);
  208. await actionMenuButton.waitForDisplayed({ timeout: 15000 });
  209. await actionMenuButton.click();
  210. await browser.waitUntil(
  211. async () =>
  212. browser.execute(() => {
  213. const triggers = Array.from(
  214. document.querySelectorAll<HTMLElement>('[data-trigger="+"] code'),
  215. ).map((element) => element.textContent);
  216. return JSON.stringify(triggers) === JSON.stringify(["@", "/", "$"]);
  217. }),
  218. {
  219. timeout: 15000,
  220. timeoutMsg: "+ 菜单没有按顺序展示 @ / / / $ 提示",
  221. },
  222. );
  223. await browser.keys("Escape");
  224. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "$", { timeout: 15000 });
  225. expect((await waitForV4MentionOptionPrefix("skill:")).startsWith("skill:")).toBe(true);
  226. await browser.keys("Escape");
  227. // 清空草稿,避免旧 mention 干扰 S08 的唯一名称过滤。
  228. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  229. timeout: 15000,
  230. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  231. });
  232. // 7) 先回到已初始化的 S1,再从文件系统手动新增 Skill。
  233. // Bug 根因:旧 Composer 曾长期复用 workspace mount 时的 UI 快照,导致新任务也看不到文件变化;
  234. // 这里固定正确边界是旧 resident Session 不热更新,而新任务创建的新 runtime 会重新发现。
  235. await selectV4TaskById(seedSessionId);
  236. await mkdir(SESSION_SKILL_DIRECTORY, { recursive: true });
  237. await writeFile(
  238. SESSION_SKILL_PATH,
  239. [
  240. "---",
  241. `name: ${SESSION_SKILL_NAME}`,
  242. "description: E2E fixture for the Session Skill catalog lifecycle.",
  243. "---",
  244. "",
  245. "# Session Skill Catalog E2E",
  246. ].join("\n"),
  247. "utf-8",
  248. );
  249. // 8) S1 仍读取首次 context 初始化时冻结的 runtime catalog,/ 与 $ 都不能看到新 Skill。
  250. for (const trigger of ["/", "$"] as const) {
  251. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, `${trigger}${SESSION_SKILL_NAME}`, {
  252. timeout: 15000,
  253. timeoutMsg: `无法在旧 Session 输入 Skill 触发器 ${trigger}`,
  254. });
  255. await waitForSessionSkillAbsent(trigger);
  256. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  257. timeout: 15000,
  258. timeoutMsg: `旧 Session ${trigger} 断言后没有清空 composer`,
  259. });
  260. }
  261. // 9) 新建任务创建新的 prewarm/runtime;同一个真实 SKILL.md 必须同时进入 / 与 $ 候选。
  262. // 全局新建会分配虚拟目录;Hook、Skill 与会话候选必须继续使用夹具所属项目。
  263. await openWorkspaceDraftFromSidebar(getE2EAppDataPaths().workspace);
  264. let slashSkillOptionId: string | undefined;
  265. for (const trigger of ["/", "$"] as const) {
  266. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, `${trigger}${SESSION_SKILL_NAME}`, {
  267. timeout: 15000,
  268. timeoutMsg: `无法在新任务输入 Skill 触发器 ${trigger}`,
  269. });
  270. const optionId = await waitForSessionSkillPresent(trigger);
  271. if (slashSkillOptionId) {
  272. expect(optionId).toBe(slashSkillOptionId);
  273. } else {
  274. slashSkillOptionId = optionId;
  275. }
  276. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  277. timeout: 15000,
  278. timeoutMsg: `新任务 ${trigger} 断言后没有清空 composer`,
  279. });
  280. }
  281. });
  282. });