general-coding-mode.test.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import { mkdir } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. TID_LOGIN_TRIGGER,
  5. TID_SETTINGS_BACK_BUTTON,
  6. TID_SETTINGS_PAGE,
  7. TID_SETTINGS_SECTION_NAV,
  8. TID_SIDE_PANE_TOGGLE,
  9. TID_TASK_SETTINGS_BUTTON,
  10. TID_TERMINAL_TOGGLE,
  11. testId,
  12. } from "@zcode/shared/test-ids";
  13. import { clearAppData, clickTestIdByDom } from "../helpers/desktop-app.js";
  14. import { prepareV4ConversationE2E } from "../helpers/v4-conversation.js";
  15. // MODE-01/02/03:只操作应用 UI 和本地 PTY,不发送模型请求。
  16. describe("办公与编程模式 M1", () => {
  17. before(async () => {
  18. await prepareV4ConversationE2E({ skipProvider: true });
  19. });
  20. after(async () => {
  21. await browser.electron.restoreAllMocks();
  22. await clearAppData();
  23. });
  24. it("MODE-01 设置页与头像菜单共用偏好,刷新后保留", async () => {
  25. await clickTestIdByDom(TID_TASK_SETTINGS_BUTTON);
  26. await $(`[data-testid="${TID_SETTINGS_PAGE}"]`).waitForDisplayed();
  27. // 界面模式已迁到常规设置并更名 office,旧 appearance/general 断言无法进入待测交互。
  28. await clickTestIdByDom(testId(TID_SETTINGS_SECTION_NAV, "general"));
  29. const select = '[role="combobox"][aria-label="界面模式"]';
  30. await $(select).waitForDisplayed();
  31. await $(select).click();
  32. await clickText('[role="option"]', "办公模式");
  33. await waitForMode("office");
  34. await clickTestIdByDom(TID_SETTINGS_BACK_BUTTON);
  35. await switchMode("coding");
  36. await browser.refresh();
  37. await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).waitForDisplayed({ timeout: 30000 });
  38. await waitForMode("coding");
  39. expect(await $(`[data-testid="${TID_TERMINAL_TOGGLE}"]`).isExisting()).toBe(true);
  40. });
  41. it("MODE-02/03 隐藏新建入口,保留已打开审查、终端和 PTY", async () => {
  42. await switchMode("office");
  43. await clickTestIdByDom(TID_SIDE_PANE_TOGGLE);
  44. await $('[data-side-pane-open-tab-item="browser"]').waitForDisplayed();
  45. expect(await $('[data-side-pane-open-tab-item="review"]').isExisting()).toBe(false);
  46. expect(await $('[data-side-pane-open-tab-item="terminal"]').isExisting()).toBe(false);
  47. expect(await $(`[data-testid="${TID_TERMINAL_TOGGLE}"]`).isExisting()).toBe(false);
  48. await switchMode("coding");
  49. await $('[data-side-pane-open-tab-item="review"]').click();
  50. await $('[data-side-pane-tab-id="git"]').waitForDisplayed();
  51. await $("[data-side-pane-add-tab-trigger]").click();
  52. await $('[data-state="active"] [data-side-pane-open-tab-item="terminal"]').click();
  53. await $('[data-side-pane-tab-id^="terminal:"]').waitForDisplayed();
  54. await clickTestIdByDom(TID_TERMINAL_TOGGLE);
  55. await browser.waitUntil(async () => (await terminalHeight()) > 50);
  56. const before = await readTabs();
  57. const terminalId = before.find((tab) => tab.id.startsWith("terminal:"))!.id;
  58. await switchMode("office");
  59. expect(await readTabs()).toEqual(before);
  60. expect(await terminalHeight()).toBeGreaterThan(50);
  61. expect(await $(`[data-testid="${TID_TERMINAL_TOGGLE}"]`).isExisting()).toBe(false);
  62. expect(await $('button[aria-label="新建终端"]').isExisting()).toBe(false);
  63. await $("[data-side-pane-add-tab-trigger]").click();
  64. expect(
  65. await $('[data-state="active"] [data-side-pane-open-tab-item="terminal"]').isExisting(),
  66. ).toBe(false);
  67. expect(await visibleTexts('[role="menuitem"]')).not.toContain("审查");
  68. await $(`[data-side-pane-tab-id="${terminalId}"]`).click();
  69. const terminalInput = $("[data-workspace-side-frame] .xterm-helper-textarea");
  70. await terminalInput.waitForExist();
  71. // xterm 的输入 textarea 位于负 z-index,不能作为真实点击目标;点击可见终端后校验焦点。
  72. await $("[data-workspace-side-frame] .xterm-screen").click();
  73. await browser.waitUntil(async () => terminalInput.isFocused());
  74. await browser.keys("printf 'MODE_%s\\n' PTY_ALIVE");
  75. await browser.keys("Enter");
  76. try {
  77. await browser.waitUntil(async () =>
  78. browser.execute(() =>
  79. [...document.querySelectorAll("[data-workspace-side-frame] .xterm-rows > div")].some(
  80. (row) => row.textContent?.trim() === "MODE_PTY_ALIVE",
  81. ),
  82. ),
  83. );
  84. } finally {
  85. const artifactDir = join(process.cwd(), ".e2e-artifacts", "general-coding-mode");
  86. await mkdir(artifactDir, { recursive: true });
  87. await browser.saveScreenshot(join(artifactDir, "general-mode-pty.png"));
  88. }
  89. await browser.execute(() =>
  90. document.querySelector<HTMLElement>('[data-side-pane-tab-id="git"]')!.click(),
  91. );
  92. await browser.waitUntil(
  93. async () => (await readTabs()).find((tab) => tab.id === "git")?.active === true,
  94. );
  95. await switchMode("coding");
  96. await switchMode("office");
  97. expect((await readTabs()).find((tab) => tab.id === "git")?.active).toBe(true);
  98. expect((await readTabs()).map((tab) => tab.id)).toContain(terminalId);
  99. await browser.execute((id) => {
  100. const tab = [...document.querySelectorAll<HTMLElement>("[data-side-pane-tab-id]")].find(
  101. (item) => item.dataset.sidePaneTabId === id,
  102. );
  103. tab?.querySelector<HTMLButtonElement>("button")?.click();
  104. }, terminalId);
  105. await browser.waitUntil(async () => !(await readTabs()).some((tab) => tab.id === terminalId));
  106. expect((await readTabs()).find((tab) => tab.id === "git")?.active).toBe(true);
  107. });
  108. });
  109. async function switchMode(mode: "office" | "coding") {
  110. // Radix 的退出动画期间仍有旧菜单层;等待它卸载,避免下一次点击被当作关闭旧层。
  111. await browser.waitUntil(
  112. async () => !(await $('[data-slot="dropdown-menu-content"]').isExisting()),
  113. );
  114. await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).waitForDisplayed();
  115. await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).click();
  116. await clickText('[role="menuitem"]', "界面模式");
  117. await clickText('[role="menuitemradio"]', mode === "office" ? "办公模式" : "编程模式");
  118. await browser.keys("Escape");
  119. await waitForMode(mode);
  120. await browser.waitUntil(
  121. async () =>
  122. (await $(`[data-testid="${TID_LOGIN_TRIGGER}"]`).getAttribute("aria-expanded")) === "false",
  123. );
  124. }
  125. async function clickText(selector: string, text: string) {
  126. try {
  127. await browser.waitUntil(
  128. async () =>
  129. browser.execute(
  130. (query, label) => {
  131. const element = [...document.querySelectorAll<HTMLElement>(query)].find(
  132. (item) =>
  133. item.getBoundingClientRect().width > 0 && item.textContent?.trim() === label,
  134. );
  135. if (!element) return false;
  136. element.click();
  137. return true;
  138. },
  139. selector,
  140. text,
  141. ),
  142. { timeout: 10000, timeoutMsg: `未找到 ${text}` },
  143. );
  144. } catch (error) {
  145. const diagnostic = await browser.execute(() => ({
  146. trigger: document.querySelector('[data-testid="login-trigger"]')?.outerHTML,
  147. menus: [...document.querySelectorAll('[role*="menu"]')].map((item) => ({
  148. role: item.getAttribute("role"),
  149. text: item.textContent,
  150. state: item.getAttribute("data-state"),
  151. rect: item.getBoundingClientRect().toJSON(),
  152. })),
  153. bodyPointerEvents: document.body.style.pointerEvents,
  154. }));
  155. throw new Error(`${String(error)}; UI=${JSON.stringify(diagnostic)}`);
  156. }
  157. }
  158. async function waitForMode(mode: string) {
  159. await browser.waitUntil(async () =>
  160. browser.execute((expected) => localStorage.getItem("zcode-interface-mode") === expected, mode),
  161. );
  162. }
  163. async function readTabs() {
  164. return browser.execute(() =>
  165. [...document.querySelectorAll<HTMLElement>("[data-side-pane-tab-id]")].map((tab) => ({
  166. id: tab.dataset.sidePaneTabId!,
  167. active: tab.dataset.state === "active",
  168. })),
  169. );
  170. }
  171. async function terminalHeight() {
  172. return browser.execute(
  173. () =>
  174. document.querySelector("[data-workspace-terminal-frame]")?.getBoundingClientRect().height ??
  175. 0,
  176. );
  177. }
  178. async function visibleTexts(selector: string) {
  179. return browser.execute(
  180. (query) =>
  181. [...document.querySelectorAll<HTMLElement>(query)]
  182. .filter((item) => item.getBoundingClientRect().width > 0)
  183. .map((item) => item.textContent?.trim()),
  184. selector,
  185. );
  186. }