v4WorkspacePaneActiveSync.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import { act, createElement } from "react";
  2. import { createRoot, type Root } from "react-dom/client";
  3. import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
  4. import {
  5. INITIAL_PANE_LAYOUT,
  6. V4_PRIMARY_PANE_ID,
  7. leafPaneIds,
  8. usePaneLayoutStore,
  9. } from "@/v4/paneLayoutStore.js";
  10. import { V4WorkspaceChatArea } from "@/v4/V4WorkspaceChatArea.js";
  11. import type { WorkbenchLeafPaneProps } from "@/v4/WorkbenchPane.js";
  12. import { useWorkbenchGroupStore } from "@/v4/workbenchGroupStore.js";
  13. const leafPaneCapture = vi.hoisted(() => ({
  14. props: [] as WorkbenchLeafPaneProps[],
  15. }));
  16. const mountedRoots: Root[] = [];
  17. vi.mock("@/v4/WorkbenchPane.js", async () => {
  18. const React = await import("react");
  19. return {
  20. WorkbenchLeafPane: (props: WorkbenchLeafPaneProps) => {
  21. leafPaneCapture.props.push(props);
  22. return React.createElement("div", {
  23. "data-pane-id": props.paneId,
  24. });
  25. },
  26. };
  27. });
  28. vi.mock("@/v4/WorkbenchSplitDivider.js", async () => {
  29. const React = await import("react");
  30. return {
  31. WorkbenchSplitDivider: () =>
  32. React.createElement("div", { "data-testid": "mock-divider" }),
  33. };
  34. });
  35. function resetWorkbenchState(): void {
  36. for (const root of mountedRoots.splice(0)) {
  37. act(() => {
  38. root.unmount();
  39. });
  40. }
  41. usePaneLayoutStore.setState({
  42. root: INITIAL_PANE_LAYOUT.root,
  43. panes: INITIAL_PANE_LAYOUT.panes,
  44. focusedPaneId: INITIAL_PANE_LAYOUT.focusedPaneId,
  45. });
  46. useWorkbenchGroupStore.getState().resetWorkbenchGroups();
  47. leafPaneCapture.props = [];
  48. delete (globalThis as { document?: unknown }).document;
  49. delete (globalThis as { window?: unknown }).window;
  50. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  51. }
  52. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  53. const element = {
  54. addEventListener: () => {},
  55. appendChild: (child: { parentNode?: unknown }) => {
  56. child.parentNode = element;
  57. return child;
  58. },
  59. childNodes: [] as unknown[],
  60. getAttribute: () => null,
  61. insertBefore: (child: { parentNode?: unknown }) => {
  62. child.parentNode = element;
  63. return child;
  64. },
  65. nodeName: tagName.toUpperCase(),
  66. nodeType: 1,
  67. ownerDocument,
  68. parentNode: null as unknown,
  69. removeAttribute: () => {},
  70. removeChild: (child: { parentNode?: unknown }) => {
  71. child.parentNode = null;
  72. return child;
  73. },
  74. removeEventListener: () => {},
  75. setAttribute: () => {},
  76. style: {
  77. removeProperty: () => {},
  78. setProperty: () => {},
  79. },
  80. tagName: tagName.toUpperCase(),
  81. };
  82. return element as unknown as Element;
  83. }
  84. function installMinimalDom() {
  85. const documentMock = {
  86. addEventListener: () => {},
  87. createElement: (tagName: string) =>
  88. createMinimalElement(documentMock as unknown as Document, tagName),
  89. createTextNode: (nodeValue: string) => ({
  90. nodeType: 3,
  91. nodeValue,
  92. ownerDocument: documentMock,
  93. parentNode: null,
  94. }),
  95. nodeType: 9,
  96. removeEventListener: () => {},
  97. } as unknown as Document;
  98. const windowMock = {
  99. addEventListener: () => {},
  100. document: documentMock,
  101. HTMLIFrameElement: function HTMLIFrameElement() {},
  102. HTMLElement: function HTMLElement() {},
  103. Node: function Node() {},
  104. removeEventListener: () => {},
  105. };
  106. Object.defineProperty(globalThis, "document", {
  107. configurable: true,
  108. value: documentMock,
  109. });
  110. Object.defineProperty(globalThis, "window", {
  111. configurable: true,
  112. value: windowMock,
  113. });
  114. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  115. configurable: true,
  116. value: true,
  117. });
  118. return createMinimalElement(documentMock, "div");
  119. }
  120. function renderClient(element: ReturnType<typeof createElement>): void {
  121. const root = createRoot(installMinimalDom());
  122. mountedRoots.push(root);
  123. act(() => {
  124. root.render(element);
  125. });
  126. }
  127. function createGroupedWorkbenchState({
  128. readOnlyChild = false,
  129. }: { readOnlyChild?: boolean } = {}): void {
  130. useWorkbenchGroupStore.getState().splitSessionIntoGroup(
  131. V4_PRIMARY_PANE_ID,
  132. "right",
  133. {
  134. workspaceScope: { workspacePath: "/workspace" },
  135. sessionId: "child-session",
  136. ...(readOnlyChild ? { readOnly: true } : {}),
  137. },
  138. {
  139. workspaceScope: { workspacePath: "/workspace" },
  140. sessionId: "main-session",
  141. },
  142. );
  143. const activeGroupId = useWorkbenchGroupStore.getState().activeGroupId;
  144. const activeGroup = activeGroupId
  145. ? useWorkbenchGroupStore.getState().groups[activeGroupId]
  146. : null;
  147. expect(activeGroup ? leafPaneIds(activeGroup.root) : []).toEqual([
  148. V4_PRIMARY_PANE_ID,
  149. "pane-1",
  150. ]);
  151. }
  152. function renderWorkspace({
  153. onPaneActiveSessionChange = vi.fn(),
  154. }: {
  155. onPaneActiveSessionChange?: (
  156. scope: { workspacePath: string; workspaceIdentity?: string },
  157. sessionId: string,
  158. ) => void;
  159. } = {}) {
  160. renderClient(
  161. createElement(V4WorkspaceChatArea, {
  162. sessionId: "main-session",
  163. workspacePath: "/workspace",
  164. onPaneActiveSessionChange,
  165. }),
  166. );
  167. return { onPaneActiveSessionChange };
  168. }
  169. describe("V4WorkspaceChatArea pane active sync", () => {
  170. beforeEach(resetWorkbenchState);
  171. afterEach(resetWorkbenchState);
  172. it("restores the remaining main session as shell active when closing a focused split pane", () => {
  173. createGroupedWorkbenchState();
  174. const { onPaneActiveSessionChange } = renderWorkspace();
  175. const childPane = leafPaneCapture.props.find((props) => props.paneId === "pane-1");
  176. expect(childPane).toBeTruthy();
  177. act(() => {
  178. childPane!.onClosePane("pane-1");
  179. });
  180. expect(onPaneActiveSessionChange).toHaveBeenCalledTimes(1);
  181. expect(onPaneActiveSessionChange).toHaveBeenCalledWith(
  182. { workspacePath: "/workspace" },
  183. "main-session",
  184. );
  185. });
  186. it("does not sync read-only subagent pane focus into the shell active task", () => {
  187. createGroupedWorkbenchState({ readOnlyChild: true });
  188. const groupId = useWorkbenchGroupStore.getState().activeGroupId;
  189. expect(groupId).toBeTruthy();
  190. useWorkbenchGroupStore.getState().focusPane(groupId!, V4_PRIMARY_PANE_ID);
  191. const { onPaneActiveSessionChange } = renderWorkspace();
  192. const childPane = leafPaneCapture.props.find((props) => props.paneId === "pane-1");
  193. expect(childPane).toBeTruthy();
  194. act(() => {
  195. childPane!.onFocusRequest("pane-1");
  196. });
  197. expect(onPaneActiveSessionChange).not.toHaveBeenCalled();
  198. });
  199. });