mention-selection-cases.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { mkdir } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { TID_V4_COMPOSER_INPUT } from "@zcode/shared/test-ids";
  4. import { waitForDefaultWorkspaceReady } from "./desktop-app.js";
  5. const LABEL = "selection-demo.ts";
  6. const MARKDOWN = `[${LABEL}](src/${LABEL})`;
  7. export function registerMentionSelectionCases() {
  8. for (const position of ["text", "leading", "middle", "trailing", "only", "adjacent"]) {
  9. it(`selects and replaces the complete draft with ${position} mentions`, async () => {
  10. await waitForDefaultWorkspaceReady(30000);
  11. const expected = await seedMentionSelection(position);
  12. const modifier = process.platform === "darwin" ? "\uE03D" : "\uE009";
  13. await browser.keys([modifier, "a", "\uE000"]);
  14. await browser.waitUntil(
  15. async () => {
  16. const selection = await readMentionSelection();
  17. return selection.selected === expected && !selection.collapsed;
  18. },
  19. { timeout: 5000, timeoutMsg: `${position}: native selection did not cover the draft` },
  20. );
  21. if (position === "middle") {
  22. const directory = join(process.cwd(), ".e2e-artifacts", "mention-selection");
  23. await mkdir(directory, { recursive: true });
  24. await browser.saveScreenshot(join(directory, "native-select-all.png"));
  25. }
  26. await browser.keys("E2E_REPLACEMENT");
  27. await browser.waitUntil(
  28. async () => {
  29. const selection = await readMentionSelection();
  30. return selection.markdown === "E2E_REPLACEMENT" && selection.mentions === 0;
  31. },
  32. { timeout: 5000, timeoutMsg: `${position}: replacement left old content behind` },
  33. );
  34. });
  35. }
  36. it("copies and cuts canonical mentions through the native clipboard", async () => {
  37. await waitForDefaultWorkspaceReady(30000);
  38. const previous = await browser.electron.execute((electron) => electron.clipboard.readText());
  39. try {
  40. await seedMentionSelection("middle");
  41. const modifier = process.platform === "darwin" ? "\uE03D" : "\uE009";
  42. await browser.keys([modifier, "a", "\uE000"]);
  43. await browser.keys([modifier, "c", "\uE000"]);
  44. await browser.waitUntil(
  45. async () =>
  46. (await browser.electron.execute((electron) => electron.clipboard.readText())) ===
  47. `hello ${MARKDOWN} world`,
  48. );
  49. expect((await readMentionSelection()).markdown).toBe(`hello ${MARKDOWN} world`);
  50. await browser.keys([modifier, "x", "\uE000"]);
  51. await browser.waitUntil(async () => (await readMentionSelection()).markdown === "");
  52. expect(await browser.electron.execute((electron) => electron.clipboard.readText())).toBe(
  53. `hello ${MARKDOWN} world`,
  54. );
  55. } finally {
  56. await browser.electron.execute(
  57. (electron, text) => electron.clipboard.writeText(text),
  58. previous,
  59. );
  60. }
  61. });
  62. it("deletes and restores selected mentions through native undo", async () => {
  63. await waitForDefaultWorkspaceReady(30000);
  64. await seedMentionSelection("only");
  65. const modifier = process.platform === "darwin" ? "\uE03D" : "\uE009";
  66. await browser.keys([modifier, "a", "\uE000"]);
  67. await browser.keys("Backspace");
  68. await browser.waitUntil(async () => (await readMentionSelection()).markdown === "");
  69. await browser.keys([modifier, "z", "\uE000"]);
  70. await browser.waitUntil(
  71. async () => {
  72. const selection = await readMentionSelection();
  73. return selection.markdown === MARKDOWN && selection.mentions === 1;
  74. },
  75. { timeout: 5000, timeoutMsg: "undo did not restore the canonical mention" },
  76. );
  77. });
  78. }
  79. async function seedMentionSelection(position: string) {
  80. const expected = await browser.execute(
  81. (testId, label, markdown, placement) => {
  82. const input = document.querySelector<HTMLElement>(`[data-testid="${testId}"]`);
  83. const bridge = (
  84. input as HTMLElement & {
  85. __zcodeLexicalInputE2E?: { setEditorStateJson: (s: string) => void; focus: () => void };
  86. }
  87. )?.__zcodeLexicalInputE2E;
  88. if (!input || !bridge) throw new Error("Lexical bridge unavailable");
  89. const text = (value: string) => ({
  90. type: "text",
  91. version: 1,
  92. text: value,
  93. format: 0,
  94. detail: 0,
  95. mode: "normal",
  96. style: "",
  97. });
  98. const token = {
  99. ...text(label),
  100. type: "prompt-mention",
  101. mode: "token",
  102. mentionId: "E2E_SELECTION_FILE",
  103. category: "files",
  104. value: `src/${label}`,
  105. markdown,
  106. data: { path: `src/${label}` },
  107. };
  108. const children =
  109. placement === "text"
  110. ? [text("hello world")]
  111. : placement === "leading"
  112. ? [token, text(" world")]
  113. : placement === "middle"
  114. ? [text("hello "), token, text(" world")]
  115. : placement === "trailing"
  116. ? [text("hello "), token]
  117. : placement === "adjacent"
  118. ? [token, { ...token, mentionId: "E2E_SELECTION_FILE_2" }]
  119. : [token];
  120. bridge.setEditorStateJson(
  121. JSON.stringify({
  122. root: {
  123. type: "root",
  124. version: 1,
  125. format: "",
  126. indent: 0,
  127. direction: null,
  128. children: [
  129. { type: "paragraph", version: 1, format: "", indent: 0, direction: null, children },
  130. ],
  131. },
  132. }),
  133. );
  134. bridge.focus();
  135. return children.map((child) => child.text).join("");
  136. },
  137. TID_V4_COMPOSER_INPUT,
  138. LABEL,
  139. MARKDOWN,
  140. position,
  141. );
  142. // 首次挂载的 focus 请求异步提交;真实点击后确认焦点,再发送全选键,避免选中整页。
  143. await browser.$(`[data-testid="${TID_V4_COMPOSER_INPUT}"]`).click();
  144. await browser.waitUntil(() =>
  145. browser.execute((testId) => {
  146. const input = document.querySelector(`[data-testid="${testId}"]`);
  147. return Boolean(
  148. input && (document.activeElement === input || input.contains(document.activeElement)),
  149. );
  150. }, TID_V4_COMPOSER_INPUT),
  151. );
  152. return expected;
  153. }
  154. async function readMentionSelection() {
  155. return browser.execute((testId) => {
  156. const input = document.querySelector<HTMLElement>(`[data-testid="${testId}"]`);
  157. const bridge = (input as HTMLElement & { __zcodeLexicalInputE2E?: { getText: () => string } })
  158. ?.__zcodeLexicalInputE2E;
  159. return {
  160. selected: window.getSelection()?.toString(),
  161. collapsed: window.getSelection()?.isCollapsed,
  162. markdown: bridge?.getText(),
  163. mentions: input?.querySelectorAll("[data-mention-id]").length,
  164. };
  165. }, TID_V4_COMPOSER_INPUT);
  166. }