conversation-session-plus-menu.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { readFile } from "node:fs/promises";
  2. import { TID_CHAT_ATTACHMENT_BUTTON, TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  3. import { clearAppData, setInputValueByTestIdDom } from "#e2e/helpers/desktop-app";
  4. import {
  5. getV4ComposerText,
  6. getV4SlashOptionIds,
  7. prepareV4ConversationE2E,
  8. waitForV4SlashOption,
  9. } from "#e2e/helpers/v4-conversation";
  10. const panel = '[data-trigger="+"]';
  11. async function openMenu() {
  12. await $(`[data-testid="${TID_CHAT_ATTACHMENT_BUTTON}"]`).click();
  13. await $(panel).waitForDisplayed();
  14. }
  15. async function clearDraft(text = "") {
  16. await setInputValueByTestIdDom(TID_V4_COMPOSER_INPUT, text, { stabilizeLexicalCaretAtEnd: true });
  17. }
  18. // E2E_PLUS_MENU:只编辑草稿,不发送 prompt;空 provider fixture 是有意的合同。
  19. describe("PLUS composer context menu", () => {
  20. before(async () => {
  21. await prepareV4ConversationE2E();
  22. });
  23. afterEach(async () => {
  24. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
  25. if (!capturePath) throw new Error("Missing provider capture evidence");
  26. expect(JSON.parse(await readFile(capturePath, "utf8")).records).toEqual([]);
  27. });
  28. after(async () => {
  29. await browser.electron.restoreAllMocks();
  30. await clearAppData();
  31. });
  32. it("PLUS02 offers Goal only in an empty new draft and keeps it at the message start", async () => {
  33. await clearDraft("before after");
  34. for (let i = 0; i < 5; i++) await browser.keys("ArrowLeft");
  35. await openMenu();
  36. expect(await $(`${panel} [data-option-id="add-goal"]`).isExisting()).toBe(false);
  37. await browser.keys("Escape");
  38. expect(await getV4ComposerText()).toBe("before after");
  39. await clearDraft();
  40. await openMenu();
  41. await $(`${panel} [data-option-id="add-goal"]`).click();
  42. await $(panel).waitForDisplayed({ reverse: true });
  43. await browser.keys("E2E_PLUS_MENU objective");
  44. await browser.waitUntil(
  45. async () => (await getV4ComposerText()) === "/goal E2E_PLUS_MENU objective",
  46. );
  47. });
  48. it("PLUS05 offers Workflow after Goal in an empty draft and keeps it at the message start", async () => {
  49. // 先等 CLI catalog(含内置命令 /workflow)送达,并断言 `/` 面板里 workflow 紧随 goal;
  50. // + 菜单在打开瞬间快照 catalog,必须在 catalog 就绪后再打开。
  51. await clearDraft("/");
  52. await waitForV4SlashOption("slash:workflow");
  53. const slashIds = await getV4SlashOptionIds();
  54. expect(slashIds.indexOf("slash:workflow")).toBe(slashIds.indexOf("slash:goal") + 1);
  55. await clearDraft();
  56. await openMenu();
  57. const workflow = `${panel} [data-option-id="add-workflow"]`;
  58. await $(workflow).waitForDisplayed();
  59. const addIds = await browser.execute(
  60. (selector) =>
  61. [...document.querySelectorAll(`${selector} [data-option-id^="add-"]`)].map((element) =>
  62. element.getAttribute("data-option-id"),
  63. ),
  64. panel,
  65. );
  66. expect(addIds.indexOf("add-workflow")).toBe(addIds.indexOf("add-goal") + 1);
  67. await $(workflow).click();
  68. await $(panel).waitForDisplayed({ reverse: true });
  69. await browser.keys("E2E_PLUS_MENU task");
  70. await browser.waitUntil(
  71. async () => (await getV4ComposerText()) === "/workflow E2E_PLUS_MENU task",
  72. );
  73. await openMenu();
  74. expect(await $(workflow).isExisting()).toBe(false);
  75. await browser.keys("Escape");
  76. await $(panel).waitForDisplayed({ reverse: true });
  77. });
  78. it("PLUS01/03 shares the enabled plugin candidate with @ and supports Escape", async () => {
  79. await clearDraft("@skill-creator");
  80. const plugin = '[data-option-id="plugin:skill-creator@zcode-plugins-official"]';
  81. await $(`[data-trigger="@"] ${plugin}`).waitForDisplayed({ timeout: 30000 });
  82. await browser.keys("Escape");
  83. await clearDraft("E2E_PLUS_MENU ");
  84. await openMenu();
  85. await $(`${panel} ${plugin}`).waitForDisplayed({ timeout: 30000 });
  86. await $(`${panel} ${plugin}`).click();
  87. await browser.waitUntil(
  88. async () =>
  89. (await getV4ComposerText())?.includes("plugin://skill-creator@zcode-plugins-official") ===
  90. true,
  91. );
  92. expect(await getV4ComposerText()).toContain("E2E_PLUS_MENU ");
  93. const before = await getV4ComposerText();
  94. await openMenu();
  95. await browser.keys("Escape");
  96. await $(panel).waitForDisplayed({ reverse: true });
  97. expect(await getV4ComposerText()).toBe(before);
  98. });
  99. it("PLUS02 inserts into the current draft after the saved selection becomes stale", async () => {
  100. await clearDraft("E2E_PLUS_MENU old draft");
  101. await openMenu();
  102. const plugin = `${panel} [data-option-id="plugin:skill-creator@zcode-plugins-official"]`;
  103. await $(plugin).waitForDisplayed({ timeout: 30000 });
  104. // 通过现有 E2E bridge 模拟程序回填;菜单应关闭,重开后使用新草稿。
  105. await browser.execute((testId) => {
  106. const input = document.querySelector(`[data-testid="${testId}"]`) as HTMLElement & {
  107. __zcodeLexicalInputE2E: { setText: (text: string) => void };
  108. };
  109. input.__zcodeLexicalInputE2E.setText("E2E_PLUS_MENU replacement ");
  110. }, TID_V4_COMPOSER_INPUT);
  111. await browser.waitUntil(
  112. async () => (await getV4ComposerText()) === "E2E_PLUS_MENU replacement ",
  113. );
  114. await $(panel).waitForDisplayed({ reverse: true });
  115. await openMenu();
  116. await $(plugin).click();
  117. await browser.waitUntil(
  118. async () =>
  119. (await getV4ComposerText())?.includes("plugin://skill-creator@zcode-plugins-official") ===
  120. true,
  121. );
  122. expect(await getV4ComposerText()).toContain("E2E_PLUS_MENU replacement ");
  123. expect(await getV4ComposerText()).not.toContain("old draft");
  124. });
  125. it("PLUS04 keeps the composer anchor across repeated opens", async () => {
  126. await clearDraft();
  127. for (let attempt = 0; attempt < 4; attempt++) {
  128. await openMenu();
  129. // 旧按钮锚点卸载后会得到零尺寸;必须检查真实宽度,不能仅以 displayed 判定打开成功。
  130. await browser.waitUntil(async () => {
  131. const layout = await readLayout();
  132. return (
  133. layout.settled && layout.width > 100 && Math.abs(layout.width - layout.composerWidth) < 2
  134. );
  135. });
  136. await browser.keys("Escape");
  137. await $(panel).waitForDisplayed({ reverse: true });
  138. }
  139. });
  140. it("PLUS04 lays out a single desktop footer row and wraps inside a narrow viewport", async () => {
  141. const size = await browser.electron.execute((electron) =>
  142. electron.BrowserWindow.getAllWindows()
  143. .find((w) => w.isVisible())!
  144. .getBounds(),
  145. );
  146. try {
  147. await resizeWindow(1440, 1000);
  148. await clearDraft();
  149. await openMenu();
  150. await $(
  151. `${panel} [data-option-id="plugin:skill-creator@zcode-plugins-official"]`,
  152. ).waitForDisplayed({ timeout: 30000 });
  153. // 候选异步加载且 Popover 有打开动画,需等待实际列表扩展后再检查布局。
  154. await browser.waitUntil(async () => (await readLayout()).listHeight > 224, {
  155. timeout: 10000,
  156. });
  157. const wide = await readLayout();
  158. expect(wide.footerY.every((y) => Math.abs(y - wide.footerY[0]!) < 2)).toBe(true);
  159. expect(wide.listHeight).toBeGreaterThan(224);
  160. await browser.keys("Escape");
  161. await resizeWindow(540, 900);
  162. await openMenu();
  163. await browser.waitUntil(async () => {
  164. const layout = await readLayout();
  165. return layout.left >= 0 && layout.right <= layout.viewport && layout.settled;
  166. });
  167. const narrow = await readLayout();
  168. expect(narrow.left).toBeGreaterThanOrEqual(0);
  169. expect(narrow.right).toBeLessThanOrEqual(narrow.viewport);
  170. expect(narrow.scrollWidth).toBeLessThanOrEqual(narrow.width + 1);
  171. await browser.keys("Escape");
  172. } finally {
  173. await browser.electron.execute(
  174. (electron, bounds) =>
  175. electron.BrowserWindow.getAllWindows()
  176. .find((w) => w.isVisible())!
  177. .setBounds(bounds),
  178. size,
  179. );
  180. }
  181. });
  182. });
  183. async function readLayout() {
  184. return browser.execute((selector) => {
  185. const element = document.querySelector<HTMLElement>(selector)!;
  186. const rect = element.getBoundingClientRect();
  187. const codes = [...element.querySelectorAll("code")];
  188. const footer = codes[0]?.parentElement?.parentElement;
  189. return {
  190. footerY: [...(footer?.children ?? [])].map((child) => child.getBoundingClientRect().y),
  191. listHeight: element.querySelector('[role="listbox"]')!.getBoundingClientRect().height,
  192. // scrollWidth 不含 transform,必须等 Popover 缩放动画结束后与 rect 比较。
  193. settled: Math.abs(rect.width - element.offsetWidth) < 1,
  194. left: rect.left,
  195. right: rect.right,
  196. width: rect.width,
  197. composerWidth: document
  198. .querySelector(`[data-testid="chat-attachment-button"]`)!
  199. .closest("form")!
  200. .getBoundingClientRect().width,
  201. scrollWidth: element.scrollWidth,
  202. viewport: window.innerWidth,
  203. };
  204. }, panel);
  205. }
  206. async function resizeWindow(width: number, height: number) {
  207. await browser.electron.execute(
  208. (electron, nextWidth, nextHeight) => {
  209. const window = electron.BrowserWindow.getAllWindows().find((w) => w.isVisible())!;
  210. window.setMinimumSize(320, 400);
  211. window.setContentSize(nextWidth, nextHeight);
  212. },
  213. width,
  214. height,
  215. );
  216. await browser.waitUntil(async () => (await browser.execute(() => window.innerWidth)) === width);
  217. }