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

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