conversation-session-conversation-workspace.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { access } from "node:fs/promises";
  2. import {
  3. TID_COMPOSER_PROJECT_DETACH,
  4. TID_COMPOSER_WORKSPACE_TRIGGER,
  5. TID_COMPOSER_WORK_OUTSIDE_PROJECT,
  6. TID_CONVERSATION_SECTION,
  7. TID_PROJECT_SECTION,
  8. TID_V4_COMPOSER_INPUT,
  9. TID_VIRTUAL_PROJECT_ADD,
  10. TID_VIRTUAL_PROJECT_NEW_TASK,
  11. } from "@zcode/shared/test-ids";
  12. import {
  13. DEFAULT_CONVERSATION_WORKSPACE,
  14. DEFAULT_WORKSPACE,
  15. clearAppData,
  16. waitForWorkspaceApp,
  17. } from "#e2e/helpers/desktop-app";
  18. describe("非项目对话工作区 E2E", () => {
  19. after(async () => {
  20. await browser.electron.restoreAllMocks();
  21. await clearAppData();
  22. });
  23. it("全新用户直接进入对话,并持久化任务/项目二级分区顺序", async function () {
  24. this.timeout(90000);
  25. await waitForWorkspaceApp(DEFAULT_CONVERSATION_WORKSPACE, 30000);
  26. await expectPathExists(DEFAULT_CONVERSATION_WORKSPACE, true);
  27. await expectPathExists(DEFAULT_WORKSPACE, false);
  28. const snapshot = await browser.execute(
  29. (testIds) => ({
  30. elements: Object.fromEntries(
  31. Object.entries(testIds).map(([key, testId]) => {
  32. const element = document.querySelector<HTMLElement>(`[data-testid="${testId}"]`);
  33. return [
  34. key,
  35. element
  36. ? {
  37. exists: true,
  38. text: element.textContent?.trim() ?? "",
  39. expanded:
  40. element
  41. .querySelector<HTMLElement>('[data-slot="collapsible-trigger"]')
  42. ?.getAttribute("aria-expanded") ?? null,
  43. }
  44. : { exists: false, text: "", expanded: null },
  45. ];
  46. }),
  47. ),
  48. projectBeforeTask: (() => {
  49. const project = document.querySelector(`[data-testid="${testIds.projectSection}"]`);
  50. const conversation = document.querySelector(
  51. `[data-testid="${testIds.conversationSection}"]`,
  52. );
  53. return Boolean(
  54. project &&
  55. conversation &&
  56. project.compareDocumentPosition(conversation) & Node.DOCUMENT_POSITION_FOLLOWING,
  57. );
  58. })(),
  59. projectActionOutsideTrigger: (() => {
  60. const project = document.querySelector(`[data-testid="${testIds.projectSection}"]`);
  61. const trigger = project?.querySelector('[data-slot="collapsible-trigger"]');
  62. const action = project?.querySelector(`[data-testid="${testIds.projectAdd}"]`);
  63. return Boolean(trigger && action && !trigger.contains(action));
  64. })(),
  65. titlesAreDragTriggers: Object.values(testIds)
  66. .filter((testId) =>
  67. [testIds.projectSection, testIds.conversationSection].includes(testId),
  68. )
  69. .every((testId) => {
  70. const section = document.querySelector(`[data-testid="${testId}"]`);
  71. const trigger = section?.querySelector('[data-slot="collapsible-trigger"]');
  72. const dragHandle = section?.querySelector("[data-purpose-section-drag-trigger]");
  73. return Boolean(trigger && dragHandle && trigger === dragHandle);
  74. }),
  75. }),
  76. {
  77. composerInput: TID_V4_COMPOSER_INPUT,
  78. composerWorkspaceTrigger: TID_COMPOSER_WORKSPACE_TRIGGER,
  79. conversationSection: TID_CONVERSATION_SECTION,
  80. conversationNewTask: TID_VIRTUAL_PROJECT_NEW_TASK,
  81. projectSection: TID_PROJECT_SECTION,
  82. projectAdd: TID_VIRTUAL_PROJECT_ADD,
  83. projectDetach: TID_COMPOSER_PROJECT_DETACH,
  84. },
  85. );
  86. expect(snapshot.elements.composerInput?.exists).toBe(true);
  87. expect(snapshot.elements.composerWorkspaceTrigger?.exists).toBe(true);
  88. expect(snapshot.elements.conversationSection?.exists).toBe(true);
  89. expect(snapshot.elements.conversationSection?.expanded).toBe("true");
  90. expect(snapshot.elements.conversationNewTask?.exists).toBe(true);
  91. expect(snapshot.elements.projectSection?.exists).toBe(true);
  92. expect(snapshot.elements.projectSection?.expanded).toBe("true");
  93. expect(snapshot.elements.projectAdd?.exists).toBe(true);
  94. expect(snapshot.elements.projectDetach?.exists).toBe(false);
  95. expect(snapshot.projectBeforeTask).toBe(true);
  96. expect(snapshot.projectActionOutsideTrigger).toBe(true);
  97. expect(snapshot.titlesAreDragTriggers).toBe(true);
  98. await dragPurposeSectionOnto(TID_CONVERSATION_SECTION, TID_PROJECT_SECTION);
  99. await expectPurposeSectionOrder(TID_CONVERSATION_SECTION, TID_PROJECT_SECTION);
  100. const persistedOrder = await browser.execute(() => {
  101. const rawValue = window.localStorage.getItem("zcode-sidebar-purpose-section-preferences");
  102. if (!rawValue) {
  103. return null;
  104. }
  105. const parsed = JSON.parse(rawValue) as { sectionOrder?: unknown };
  106. return parsed.sectionOrder ?? null;
  107. });
  108. expect(persistedOrder).toEqual(["conversations", "projects"]);
  109. const projectToggle = await $(
  110. `[data-testid="${TID_PROJECT_SECTION}"] [data-slot="collapsible-trigger"]`,
  111. );
  112. const conversationToggle = await $(
  113. `[data-testid="${TID_CONVERSATION_SECTION}"] [data-slot="collapsible-trigger"]`,
  114. );
  115. await projectToggle.click();
  116. await expect(projectToggle).toHaveAttribute("aria-expanded", "false");
  117. await expect(conversationToggle).toHaveAttribute("aria-expanded", "true");
  118. await browser.execute(() => window.location.reload());
  119. await waitForWorkspaceApp(DEFAULT_CONVERSATION_WORKSPACE, 30000);
  120. await expectPurposeSectionOrder(TID_CONVERSATION_SECTION, TID_PROJECT_SECTION);
  121. const restoredProjectToggle = await $(
  122. `[data-testid="${TID_PROJECT_SECTION}"] [data-slot="collapsible-trigger"]`,
  123. );
  124. const restoredConversationToggle = await $(
  125. `[data-testid="${TID_CONVERSATION_SECTION}"] [data-slot="collapsible-trigger"]`,
  126. );
  127. await expect(restoredProjectToggle).toHaveAttribute("aria-expanded", "false");
  128. await expect(restoredConversationToggle).toHaveAttribute("aria-expanded", "true");
  129. await restoredConversationToggle.click();
  130. await expect(restoredConversationToggle).toHaveAttribute("aria-expanded", "false");
  131. await expect(restoredProjectToggle).toHaveAttribute("aria-expanded", "false");
  132. await $(`[data-testid="${TID_COMPOSER_WORKSPACE_TRIGGER}"]`).click();
  133. await browser.waitUntil(
  134. () =>
  135. browser.execute(
  136. (testId) => Boolean(document.querySelector(`[data-testid="${testId}"]`)),
  137. TID_COMPOSER_WORK_OUTSIDE_PROJECT,
  138. ),
  139. {
  140. timeout: 10000,
  141. timeoutMsg: "workspace 菜单没有显示“不在项目中工作”入口",
  142. },
  143. );
  144. });
  145. });
  146. async function dragPurposeSectionOnto(sourceTestId: string, targetTestId: string): Promise<void> {
  147. const positions = await browser.execute(
  148. (sourceId, targetId) => {
  149. const source = document.querySelector<HTMLElement>(`[data-testid="${sourceId}"]`);
  150. const target = document.querySelector<HTMLElement>(`[data-testid="${targetId}"]`);
  151. const dragHandle = source?.querySelector<HTMLElement>("[data-purpose-section-drag-trigger]");
  152. if (!dragHandle || !target) {
  153. return null;
  154. }
  155. const sourceRect = dragHandle.getBoundingClientRect();
  156. const targetRect = target.getBoundingClientRect();
  157. return {
  158. startX: Math.round(sourceRect.left + sourceRect.width / 2),
  159. startY: Math.round(sourceRect.top + sourceRect.height / 2),
  160. endX: Math.round(targetRect.left + targetRect.width / 2),
  161. endY: Math.round(targetRect.top + targetRect.height / 2),
  162. };
  163. },
  164. sourceTestId,
  165. targetTestId,
  166. );
  167. if (!positions) {
  168. throw new Error("purpose section 拖拽元素不存在");
  169. }
  170. await browser.performActions([
  171. {
  172. id: "purpose-section-drag-pointer",
  173. type: "pointer",
  174. parameters: { pointerType: "mouse" },
  175. actions: [
  176. { type: "pointerMove", duration: 0, x: positions.startX, y: positions.startY },
  177. { type: "pointerDown", button: 0 },
  178. { type: "pause", duration: 80 },
  179. {
  180. type: "pointerMove",
  181. duration: 120,
  182. x: positions.startX,
  183. y: positions.startY - 10,
  184. },
  185. { type: "pointerMove", duration: 260, x: positions.endX, y: positions.endY },
  186. { type: "pointerUp", button: 0 },
  187. ],
  188. },
  189. ]);
  190. await browser.releaseActions();
  191. // dnd-kit 会屏蔽拖拽后的紧随 click;先消耗该屏蔽,避免后续折叠按钮点击被吞。
  192. await browser.execute(() => {
  193. document.body.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
  194. });
  195. }
  196. async function expectPurposeSectionOrder(firstTestId: string, secondTestId: string): Promise<void> {
  197. let previousGeometry: string | null = null;
  198. let stablePolls = 0;
  199. await browser.waitUntil(
  200. async () => {
  201. const geometry = await browser.execute(
  202. (firstId, secondId) => {
  203. const first = document.querySelector(`[data-testid="${firstId}"]`);
  204. const second = document.querySelector(`[data-testid="${secondId}"]`);
  205. const firstRect = first?.getBoundingClientRect();
  206. const secondRect = second?.getBoundingClientRect();
  207. if (!first || !second || !firstRect || !secondRect) {
  208. return null;
  209. }
  210. return {
  211. ordered: Boolean(
  212. first.compareDocumentPosition(second) & Node.DOCUMENT_POSITION_FOLLOWING,
  213. ),
  214. firstTop: Math.round(firstRect.top),
  215. secondTop: Math.round(secondRect.top),
  216. };
  217. },
  218. firstTestId,
  219. secondTestId,
  220. );
  221. if (!geometry?.ordered || geometry.firstTop >= geometry.secondTop) {
  222. previousGeometry = null;
  223. stablePolls = 0;
  224. return false;
  225. }
  226. const nextGeometry = `${geometry.firstTop}:${geometry.secondTop}`;
  227. stablePolls = nextGeometry === previousGeometry ? stablePolls + 1 : 0;
  228. previousGeometry = nextGeometry;
  229. // 数据恢复和 dnd-kit 位移动画都会改变相邻分区坐标;连续稳定后再点击,避免命中正在交叉的另一行。
  230. return stablePolls >= 3;
  231. },
  232. { timeout: 10000, interval: 50, timeoutMsg: "purpose section 顺序没有按拖拽结果更新" },
  233. );
  234. }
  235. async function expectPathExists(path: string, expected: boolean): Promise<void> {
  236. let exists = true;
  237. try {
  238. await access(path);
  239. } catch {
  240. exists = false;
  241. }
  242. expect(exists).toBe(expected);
  243. }