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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // S01 / S05 / S08 门禁:composer 聚合入口、兼容触发器与 Session Skill catalog 生命周期。
  2. // 证据层 L3:
  3. // 1) 首轮固定回复建立一个 session(写入 sessions-index);
  4. // 2) 新建 draft → `@` 与 `#` 都能发现同一 sessions-index 候选;
  5. // 3) 两条入口选中后都写入原 `#session-id` canonical mention;
  6. // 4) `$` / `¥` / `¥` 继续打开 Skills 面板并写入原 Skill mention;
  7. // 5) `+` 菜单展示 @ / / / $ 底部提示,手动输入 $ 打开技能候选;
  8. // 6) Session 初始化后手动新增 SKILL.md,旧 Session 目录冻结,新建任务重新发现。
  9. import { mkdir, rm, writeFile } from "node:fs/promises";
  10. import { join } from "node:path";
  11. import {
  12. clearAppData,
  13. getE2EAppDataPaths,
  14. setInputValueByTestIdDom,
  15. } from "../helpers/desktop-app.js";
  16. import { TID_CHAT_ATTACHMENT_BUTTON, TID_V4_COMPOSER_INPUT } from "@zcode/shared";
  17. import {
  18. clickV4SlashOption,
  19. getV4ComposerText,
  20. getV4SlashOptionIds,
  21. hasV4SlashPanel,
  22. prepareV4ConversationE2E,
  23. selectV4TaskById,
  24. sendV4Prompt,
  25. startNewV4Draft,
  26. waitForV4MentionOptionPrefix,
  27. waitForV4Pane,
  28. waitForV4TimelineContaining,
  29. } from "../helpers/v4-conversation.js";
  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. await startNewV4Draft();
  123. // 3) `@` 聚合入口加载 sessions-index,并仍写入 # canonical mention。
  124. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "@E2E_V4_MENTION_SEED", {
  125. timeout: 15000,
  126. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  127. });
  128. const atSessionOptionId = await waitForV4MentionOptionPrefix("session:");
  129. expect(atSessionOptionId.startsWith("session:")).toBe(true);
  130. expect(await clickV4SlashOption(atSessionOptionId)).toBe(true);
  131. await browser.waitUntil(
  132. async () => ((await getV4ComposerText()) ?? "").includes(`#${seedSessionId}`),
  133. {
  134. timeout: 15000,
  135. timeoutMsg: "@ 会话候选没有写入原 #session-id canonical mention",
  136. },
  137. );
  138. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  139. timeout: 15000,
  140. timeoutMsg: "选中 @ 会话候选后面板没有关闭",
  141. });
  142. // 4) 兼容 `#` 面板继续工作,并与 @ 使用同一 sessions-index authority。
  143. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "#E2E_V4_MENTION_SEED", {
  144. timeout: 15000,
  145. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  146. });
  147. const hashSessionOptionId = await waitForV4MentionOptionPrefix("session:");
  148. expect(hashSessionOptionId).toBe(atSessionOptionId);
  149. expect(await clickV4SlashOption(hashSessionOptionId)).toBe(true);
  150. await browser.waitUntil(
  151. async () => ((await getV4ComposerText()) ?? "").includes(`#${seedSessionId}`),
  152. {
  153. timeout: 15000,
  154. timeoutMsg: "# 会话候选没有写入原 #session-id canonical mention",
  155. },
  156. );
  157. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  158. timeout: 15000,
  159. timeoutMsg: "选中 # 会话候选后面板没有关闭",
  160. });
  161. // 5) Skills 旧触发器(含两种 Yen 键盘别名)继续打开同一候选面板。
  162. let expectedSkillOptionId: string | undefined;
  163. let expectedSkillMarkdown: string | undefined;
  164. for (const skillTrigger of ["$", "¥", "¥"] as const) {
  165. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, skillTrigger, {
  166. timeout: 15000,
  167. timeoutMsg: `无法输入 Skills 兼容触发器 ${skillTrigger}`,
  168. });
  169. const skillOptionId = await waitForV4MentionOptionPrefix("skill:");
  170. expect(skillOptionId.startsWith("skill:")).toBe(true);
  171. if (expectedSkillOptionId) {
  172. expect(skillOptionId).toBe(expectedSkillOptionId);
  173. } else {
  174. expectedSkillOptionId = skillOptionId;
  175. }
  176. expect(await clickV4SlashOption(skillOptionId)).toBe(true);
  177. let selectedSkillMarkdown: string | undefined;
  178. await browser.waitUntil(
  179. async () => {
  180. selectedSkillMarkdown = (await getV4ComposerText()) ?? undefined;
  181. return Boolean(
  182. selectedSkillMarkdown?.startsWith("[$") && selectedSkillMarkdown.includes("SKILL.md)"),
  183. );
  184. },
  185. {
  186. timeout: 15000,
  187. timeoutMsg: `${skillTrigger} 候选没有写入原 Skill canonical mention`,
  188. },
  189. );
  190. if (expectedSkillMarkdown) {
  191. expect(selectedSkillMarkdown).toBe(expectedSkillMarkdown);
  192. } else {
  193. expectedSkillMarkdown = selectedSkillMarkdown;
  194. }
  195. await browser.waitUntil(async () => !(await hasV4SlashPanel()), {
  196. timeout: 15000,
  197. timeoutMsg: `选中 ${skillTrigger} Skill 候选后面板没有关闭`,
  198. });
  199. }
  200. // 6) + 菜单以底部提示介绍快捷输入;提示不再是可点击的菜单项。
  201. // 放在 mention 断言之后,避免 Radix 菜单的关闭焦点与 Lexical 输入竞争。
  202. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  203. timeout: 15000,
  204. timeoutMsg: "验证动作菜单前无法清空输入框",
  205. });
  206. const actionMenuButton = await $(`[data-testid="${TID_CHAT_ATTACHMENT_BUTTON}"]`);
  207. await actionMenuButton.waitForDisplayed({ timeout: 15000 });
  208. await actionMenuButton.click();
  209. await browser.waitUntil(
  210. async () =>
  211. browser.execute(() => {
  212. const triggers = Array.from(
  213. document.querySelectorAll<HTMLElement>('[data-trigger="+"] code'),
  214. ).map((element) => element.textContent);
  215. return JSON.stringify(triggers) === JSON.stringify(["@", "/", "$"]);
  216. }),
  217. {
  218. timeout: 15000,
  219. timeoutMsg: "+ 菜单没有按顺序展示 @ / / / $ 提示",
  220. },
  221. );
  222. await browser.keys("Escape");
  223. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "$", { timeout: 15000 });
  224. expect((await waitForV4MentionOptionPrefix("skill:")).startsWith("skill:")).toBe(true);
  225. await browser.keys("Escape");
  226. // 清空草稿,避免旧 mention 干扰 S08 的唯一名称过滤。
  227. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  228. timeout: 15000,
  229. timeoutMsg: "v4 composer 输入框没有出现或不可输入",
  230. });
  231. // 7) 先回到已初始化的 S1,再从文件系统手动新增 Skill。
  232. // Bug 根因:旧 Composer 曾长期复用 workspace mount 时的 UI 快照,导致新任务也看不到文件变化;
  233. // 这里固定正确边界是旧 resident Session 不热更新,而新任务创建的新 runtime 会重新发现。
  234. await selectV4TaskById(seedSessionId);
  235. await mkdir(SESSION_SKILL_DIRECTORY, { recursive: true });
  236. await writeFile(
  237. SESSION_SKILL_PATH,
  238. [
  239. "---",
  240. `name: ${SESSION_SKILL_NAME}`,
  241. "description: E2E fixture for the Session Skill catalog lifecycle.",
  242. "---",
  243. "",
  244. "# Session Skill Catalog E2E",
  245. ].join("\n"),
  246. "utf-8",
  247. );
  248. // 8) S1 仍读取首次 context 初始化时冻结的 runtime catalog,/ 与 $ 都不能看到新 Skill。
  249. for (const trigger of ["/", "$"] as const) {
  250. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, `${trigger}${SESSION_SKILL_NAME}`, {
  251. timeout: 15000,
  252. timeoutMsg: `无法在旧 Session 输入 Skill 触发器 ${trigger}`,
  253. });
  254. await waitForSessionSkillAbsent(trigger);
  255. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  256. timeout: 15000,
  257. timeoutMsg: `旧 Session ${trigger} 断言后没有清空 composer`,
  258. });
  259. }
  260. // 9) 新建任务创建新的 prewarm/runtime;同一个真实 SKILL.md 必须同时进入 / 与 $ 候选。
  261. await startNewV4Draft();
  262. let slashSkillOptionId: string | undefined;
  263. for (const trigger of ["/", "$"] as const) {
  264. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, `${trigger}${SESSION_SKILL_NAME}`, {
  265. timeout: 15000,
  266. timeoutMsg: `无法在新任务输入 Skill 触发器 ${trigger}`,
  267. });
  268. const optionId = await waitForSessionSkillPresent(trigger);
  269. if (slashSkillOptionId) {
  270. expect(optionId).toBe(slashSkillOptionId);
  271. } else {
  272. slashSkillOptionId = optionId;
  273. }
  274. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, "", {
  275. timeout: 15000,
  276. timeoutMsg: `新任务 ${trigger} 断言后没有清空 composer`,
  277. });
  278. }
  279. });
  280. });