conversation-session-fork-side-pane-empty-state.test.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { TID_V4_COMPOSER_INPUT, TID_V4_SESSION_PANE } from "@zcode/shared";
  2. import { clearAppData } from "../../../helpers/desktop-app.js";
  3. import {
  4. clickFirstV4Fork,
  5. prepareV4ConversationE2E,
  6. selectV4TaskById,
  7. sendV4Prompt,
  8. waitForV4Fork,
  9. waitForV4Pane,
  10. waitForV4TimelineContaining,
  11. } from "../../../helpers/v4-conversation.js";
  12. const PARENT_MARKER = "E2E_SSC_FORK_SIDE_PANE_PARENT";
  13. const PARENT_REPLY = "SSC_FORK_SIDE_PANE_REPLY";
  14. const SIDE_DRAFT = "SSC_FORK_SIDE_PANE_DRAFT_KEEP";
  15. describe("fork 后 Side Pane 空态 manual review", () => {
  16. after(async () => {
  17. await browser.electron.restoreAllMocks();
  18. await clearAppData();
  19. });
  20. it("SSC22/FX12: child 无可见 tab 时显示启动页,切回 parent 恢复辅助对话", async function () {
  21. this.timeout(150000);
  22. await prepareV4ConversationE2E();
  23. await sendV4Prompt(PARENT_MARKER);
  24. await waitForV4TimelineContaining(PARENT_REPLY, 60000);
  25. await waitForV4Fork();
  26. const parent = await waitForV4Pane(
  27. (snapshot) =>
  28. Boolean(snapshot.sessionId && snapshot.sessionId !== "draft"),
  29. "SSC22 parent session 没有建立",
  30. );
  31. const parentSessionId = parent.sessionId;
  32. if (!parentSessionId || parentSessionId === "draft") {
  33. throw new Error("SSC22 parent sessionId missing");
  34. }
  35. await expandSidePane();
  36. await waitForOpenTabLauncher("parent Side Pane 启动页没有出现");
  37. await clickOpenTabItem("selection-side-conversation");
  38. const parentSideChat = await waitForSelectionSidePane();
  39. await setComposerText(parentSideChat.paneTestId, SIDE_DRAFT);
  40. const clickedFork = await clickFirstV4Fork();
  41. expect(clickedFork).toBe(true);
  42. const child = await waitForV4Pane(
  43. (snapshot) =>
  44. Boolean(
  45. snapshot.sessionId &&
  46. snapshot.sessionId !== "draft" &&
  47. snapshot.sessionId !== parentSessionId,
  48. ),
  49. "fork 后没有切换到 child session",
  50. 60000,
  51. );
  52. expect(child.sessionId).not.toBe(parentSessionId);
  53. await expandSidePane();
  54. await waitForOpenTabLauncher(
  55. "fork child 没有可见 tab 时未显示启动页,Side Pane 仍为空白",
  56. );
  57. const childSidePane = await readOpenSidePaneState();
  58. expect(childSidePane.launcherVisible).toBe(true);
  59. expect(childSidePane.visibleTabIds).toEqual([]);
  60. expect(childSidePane.openItemIds).toContain("selection-side-conversation");
  61. await selectV4TaskById(parentSessionId);
  62. await expandSidePane();
  63. const restoredSideChat = await waitForSelectionSidePane();
  64. expect(restoredSideChat.tabId).toBe(parentSideChat.tabId);
  65. expect(restoredSideChat.childSessionId).toBe(parentSideChat.childSessionId);
  66. expect(await readComposerText(restoredSideChat.paneTestId)).toBe(
  67. SIDE_DRAFT,
  68. );
  69. });
  70. });
  71. async function expandSidePane(): Promise<void> {
  72. const alreadyOpen = await browser.execute(() =>
  73. Boolean(document.querySelector("button svg.lucide-panel-right-close")),
  74. );
  75. if (!alreadyOpen) {
  76. const clicked = await browser.execute(() => {
  77. const icon = document.querySelector("button svg.lucide-panel-right-open");
  78. const button = icon?.closest<HTMLButtonElement>("button");
  79. if (!button) return false;
  80. button.click();
  81. return true;
  82. });
  83. expect(clicked).toBe(true);
  84. }
  85. await browser.waitUntil(
  86. async () =>
  87. browser.execute(() =>
  88. Boolean(document.querySelector("button svg.lucide-panel-right-close")),
  89. ),
  90. { timeout: 15000, timeoutMsg: "Side Pane 没有展开" },
  91. );
  92. }
  93. async function waitForOpenTabLauncher(timeoutMsg: string): Promise<void> {
  94. await browser.waitUntil(
  95. async () => (await readOpenSidePaneState()).launcherVisible,
  96. { timeout: 15000, timeoutMsg },
  97. );
  98. }
  99. async function clickOpenTabItem(itemId: string): Promise<void> {
  100. const clicked = await browser.execute((nextItemId) => {
  101. const button = document.querySelector<HTMLButtonElement>(
  102. `[data-side-pane-open-tab-item="${nextItemId}"]`,
  103. );
  104. if (!button) return false;
  105. button.click();
  106. return true;
  107. }, itemId);
  108. expect(clicked).toBe(true);
  109. }
  110. async function readOpenSidePaneState(): Promise<{
  111. launcherVisible: boolean;
  112. openItemIds: string[];
  113. visibleTabIds: string[];
  114. }> {
  115. return browser.execute(() => {
  116. const isVisible = (element: Element): boolean => {
  117. const rect = element.getBoundingClientRect();
  118. const style = window.getComputedStyle(element);
  119. return (
  120. rect.width > 0 &&
  121. rect.height > 0 &&
  122. style.display !== "none" &&
  123. style.visibility !== "hidden"
  124. );
  125. };
  126. const launcher = document.querySelector(".side-pane-open-tab-shell");
  127. return {
  128. launcherVisible: Boolean(launcher && isVisible(launcher)),
  129. openItemIds: Array.from(
  130. document.querySelectorAll<HTMLElement>(
  131. "[data-side-pane-open-tab-item]",
  132. ),
  133. )
  134. .filter(isVisible)
  135. .map((element) => element.dataset.sidePaneOpenTabItem ?? "")
  136. .filter(Boolean),
  137. visibleTabIds: Array.from(
  138. document.querySelectorAll<HTMLElement>("[data-side-pane-tab-id]"),
  139. )
  140. .filter(isVisible)
  141. .map((element) => element.dataset.sidePaneTabId ?? "")
  142. .filter(Boolean),
  143. };
  144. });
  145. }
  146. async function waitForSelectionSidePane(): Promise<{
  147. childSessionId: string;
  148. paneTestId: string;
  149. tabId: string;
  150. }> {
  151. let result: {
  152. childSessionId: string;
  153. paneTestId: string;
  154. tabId: string;
  155. } | null = null;
  156. await browser.waitUntil(
  157. async () => {
  158. result = await browser.execute((panePrefix) => {
  159. const tab = document.querySelector<HTMLElement>(
  160. '[data-side-pane-tab-id^="selection-side-chat:"]',
  161. );
  162. const tabId = tab?.dataset.sidePaneTabId;
  163. if (!tabId) return null;
  164. const paneTestId = `${panePrefix}-${tabId}`;
  165. const pane = document.querySelector<HTMLElement>(
  166. `[data-testid="${paneTestId}"]`,
  167. );
  168. const childSessionId = pane?.dataset.sessionId;
  169. return childSessionId ? { childSessionId, paneTestId, tabId } : null;
  170. }, TID_V4_SESSION_PANE);
  171. return result !== null;
  172. },
  173. { timeout: 30000, timeoutMsg: "辅助对话 Side Pane 没有打开" },
  174. );
  175. if (!result) throw new Error("selection side pane result missing");
  176. return result;
  177. }
  178. async function setComposerText(
  179. paneTestId: string,
  180. text: string,
  181. ): Promise<void> {
  182. await browser.waitUntil(
  183. async () =>
  184. browser.execute(
  185. (nextPaneTestId, inputTestId, nextText) => {
  186. const pane = document.querySelector<HTMLElement>(
  187. `[data-testid="${nextPaneTestId}"]`,
  188. );
  189. const input = pane?.querySelector<HTMLElement>(
  190. `[data-testid="${inputTestId}"]`,
  191. );
  192. const bridge = (
  193. input as
  194. | (HTMLElement & {
  195. __zcodeLexicalInputE2E?: {
  196. getText: () => string;
  197. setText: (value: string) => void;
  198. };
  199. })
  200. | undefined
  201. )?.__zcodeLexicalInputE2E;
  202. if (!bridge) return false;
  203. bridge.setText(nextText);
  204. return bridge.getText() === nextText;
  205. },
  206. paneTestId,
  207. TID_V4_COMPOSER_INPUT,
  208. text,
  209. ),
  210. { timeout: 15000, timeoutMsg: `辅助对话 composer 不可输入: ${paneTestId}` },
  211. );
  212. }
  213. async function readComposerText(paneTestId: string): Promise<string | null> {
  214. return browser.execute(
  215. (nextPaneTestId, inputTestId) => {
  216. const pane = document.querySelector<HTMLElement>(
  217. `[data-testid="${nextPaneTestId}"]`,
  218. );
  219. const input = pane?.querySelector<HTMLElement>(
  220. `[data-testid="${inputTestId}"]`,
  221. );
  222. return (
  223. (
  224. input as
  225. | (HTMLElement & {
  226. __zcodeLexicalInputE2E?: { getText: () => string };
  227. })
  228. | undefined
  229. )?.__zcodeLexicalInputE2E?.getText() ?? null
  230. );
  231. },
  232. paneTestId,
  233. TID_V4_COMPOSER_INPUT,
  234. );
  235. }