rootNewTaskWorkbenchContext.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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 { useRootWorkspaceActions } from "@/root/useRootWorkspaceActions.js";
  5. import type { CreateTaskRequest } from "@/app-shell/types.js";
  6. import { useZCodeSessionStore } from "@/store/zcodeSessionStore.js";
  7. import {
  8. INITIAL_PANE_LAYOUT,
  9. V4_PRIMARY_PANE_ID,
  10. countPanes,
  11. usePaneLayoutStore,
  12. } from "@/v4/paneLayoutStore.js";
  13. import { useWorkbenchGroupStore } from "@/v4/workbenchGroupStore.js";
  14. const mountedRoots: Root[] = [];
  15. function createMinimalElement(ownerDocument: Document, tagName = "div") {
  16. const element = {
  17. addEventListener: () => {},
  18. appendChild: (child: { parentNode?: unknown }) => {
  19. child.parentNode = element;
  20. return child;
  21. },
  22. childNodes: [] as unknown[],
  23. getAttribute: () => null,
  24. insertBefore: (child: { parentNode?: unknown }) => {
  25. child.parentNode = element;
  26. return child;
  27. },
  28. nodeName: tagName.toUpperCase(),
  29. nodeType: 1,
  30. ownerDocument,
  31. parentNode: null as unknown,
  32. removeAttribute: () => {},
  33. removeChild: (child: { parentNode?: unknown }) => {
  34. child.parentNode = null;
  35. return child;
  36. },
  37. removeEventListener: () => {},
  38. setAttribute: () => {},
  39. style: {
  40. removeProperty: () => {},
  41. setProperty: () => {},
  42. },
  43. tagName: tagName.toUpperCase(),
  44. };
  45. return element as unknown as Element;
  46. }
  47. function installMinimalDom() {
  48. const documentMock = {
  49. addEventListener: () => {},
  50. createElement: (tagName: string) =>
  51. createMinimalElement(documentMock as unknown as Document, tagName),
  52. createTextNode: (nodeValue: string) => ({
  53. nodeType: 3,
  54. nodeValue,
  55. ownerDocument: documentMock,
  56. parentNode: null,
  57. }),
  58. nodeType: 9,
  59. removeEventListener: () => {},
  60. } as unknown as Document;
  61. const windowMock = {
  62. addEventListener: () => {},
  63. document: documentMock,
  64. HTMLIFrameElement: function HTMLIFrameElement() {},
  65. HTMLElement: function HTMLElement() {},
  66. Node: function Node() {},
  67. removeEventListener: () => {},
  68. };
  69. Object.defineProperty(globalThis, "document", {
  70. configurable: true,
  71. value: documentMock,
  72. });
  73. Object.defineProperty(globalThis, "window", {
  74. configurable: true,
  75. value: windowMock,
  76. });
  77. Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
  78. configurable: true,
  79. value: true,
  80. });
  81. return createMinimalElement(documentMock, "div");
  82. }
  83. function resetState(): void {
  84. for (const root of mountedRoots.splice(0)) {
  85. act(() => {
  86. root.unmount();
  87. });
  88. }
  89. useZCodeSessionStore.setState((state) => ({
  90. ...state,
  91. workspaces: {},
  92. }));
  93. usePaneLayoutStore.setState({
  94. root: INITIAL_PANE_LAYOUT.root,
  95. panes: INITIAL_PANE_LAYOUT.panes,
  96. focusedPaneId: INITIAL_PANE_LAYOUT.focusedPaneId,
  97. });
  98. useWorkbenchGroupStore.getState().resetWorkbenchGroups();
  99. delete (globalThis as { document?: unknown }).document;
  100. delete (globalThis as { window?: unknown }).window;
  101. delete (globalThis as { IS_REACT_ACT_ENVIRONMENT?: unknown }).IS_REACT_ACT_ENVIRONMENT;
  102. }
  103. function renderRootActions({
  104. activeWorkspacePath = "/workspace-a",
  105. activeWorkspaceIdentity = null,
  106. workbenchGroupClientMode = "desktop-continuous",
  107. }: {
  108. activeWorkspacePath?: string;
  109. activeWorkspaceIdentity?: string | null;
  110. workbenchGroupClientMode?: "desktop-continuous" | "web-remote-replayable";
  111. } = {}) {
  112. let handleCreateTask: ((request?: CreateTaskRequest) => void) | null = null;
  113. let handleSelectConversationWorkspace: ((path: string) => void) | null = null;
  114. const tabState = {
  115. // 修复原因:新任务动作会在真实 tab store 上检查失效 workspace;测试 mock 也必须
  116. // 提供完整的只读判定输入,不能用缺失 tabs 的对象绕过生产契约。
  117. tabs: [],
  118. activeWorkspacePath,
  119. activeWorkspaceIdentity,
  120. activateTabByPath: vi.fn(
  121. (workspacePath: string, options?: { workspaceIdentity?: string }) => {
  122. tabState.activeWorkspacePath = workspacePath;
  123. tabState.activeWorkspaceIdentity = options?.workspaceIdentity ?? null;
  124. return true;
  125. },
  126. ),
  127. };
  128. const addTab = vi.fn((workspacePath: string, options?: { workspaceIdentity?: string }) => {
  129. tabState.activeWorkspacePath = workspacePath;
  130. tabState.activeWorkspaceIdentity = options?.workspaceIdentity ?? null;
  131. });
  132. function Probe() {
  133. const actions = useRootWorkspaceActions({
  134. intl: { formatMessage: ({ id }: { id: string }) => id },
  135. platform: {} as never,
  136. services: {} as never,
  137. tabStoreApi: { getState: () => tabState } as never,
  138. addTab,
  139. activeWorkspacePath: tabState.activeWorkspacePath,
  140. activeWorkspaceIdentity: tabState.activeWorkspaceIdentity,
  141. supportsSettings: true,
  142. allowOpenWorkspace: true,
  143. preferDirectoryBrowser: false,
  144. refreshProviderState: vi.fn(),
  145. updateAppSettings: vi.fn(),
  146. setOAuthError: vi.fn(),
  147. setUser: vi.fn(),
  148. workbenchGroupClientMode,
  149. });
  150. handleCreateTask = actions.handleCreateTask;
  151. handleSelectConversationWorkspace = actions.handleSelectConversationWorkspace;
  152. return createElement("span", null, "probe");
  153. }
  154. const root = createRoot(installMinimalDom());
  155. mountedRoots.push(root);
  156. act(() => {
  157. root.render(createElement(Probe));
  158. });
  159. return {
  160. addTab,
  161. getHandleCreateTask: () => {
  162. if (!handleCreateTask) {
  163. throw new Error("handleCreateTask was not captured");
  164. }
  165. return handleCreateTask;
  166. },
  167. getHandleSelectConversationWorkspace: () => {
  168. if (!handleSelectConversationWorkspace) {
  169. throw new Error("handleSelectConversationWorkspace was not captured");
  170. }
  171. return handleSelectConversationWorkspace;
  172. },
  173. tabState,
  174. };
  175. }
  176. function workspaceState(workspacePath: string, workspaceIdentity?: string) {
  177. return useZCodeSessionStore
  178. .getState()
  179. .getWorkspaceState(workspacePath, workspaceIdentity);
  180. }
  181. describe("root new task workbench context", () => {
  182. beforeEach(resetState);
  183. afterEach(resetState);
  184. it("creates the draft in the focused read-only subagent pane workspace and exits split view", () => {
  185. useZCodeSessionStore.getState().setActiveTaskId("/workspace-a", "main-session");
  186. useWorkbenchGroupStore.getState().splitSessionIntoGroup(
  187. V4_PRIMARY_PANE_ID,
  188. "right",
  189. {
  190. workspaceScope: {
  191. workspacePath: "/workspace-b",
  192. workspaceIdentity: "remote:ssh:dev:/workspace-b",
  193. },
  194. sessionId: "child-session",
  195. readOnly: true,
  196. },
  197. {
  198. workspaceScope: { workspacePath: "/workspace-a" },
  199. sessionId: "main-session",
  200. },
  201. );
  202. const { getHandleCreateTask, tabState } = renderRootActions();
  203. act(() => {
  204. getHandleCreateTask()();
  205. });
  206. expect(tabState.activeWorkspacePath).toBe("/workspace-b");
  207. expect(tabState.activeWorkspaceIdentity).toBe("remote:ssh:dev:/workspace-b");
  208. expect(workspaceState("/workspace-a").activeTaskId).toBe("main-session");
  209. expect(
  210. workspaceState("/workspace-b", "remote:ssh:dev:/workspace-b").activeTaskId,
  211. ).toBeNull();
  212. expect(
  213. workspaceState("/workspace-b", "remote:ssh:dev:/workspace-b").draftFocusVersion,
  214. ).toBe(1);
  215. expect(useWorkbenchGroupStore.getState().activeGroupId).toBeNull();
  216. expect(countPanes(usePaneLayoutStore.getState())).toBe(1);
  217. });
  218. it("creates the draft in a focused ordinary split pane workspace and resets to one panel", () => {
  219. useZCodeSessionStore.getState().setActiveTaskId("/workspace-a", "main-session");
  220. usePaneLayoutStore.getState().splitPaneWithBinding(V4_PRIMARY_PANE_ID, "right", {
  221. workspaceScope: { workspacePath: "/workspace-b" },
  222. sessionId: "session-b",
  223. });
  224. const { getHandleCreateTask, tabState } = renderRootActions();
  225. act(() => {
  226. getHandleCreateTask()();
  227. });
  228. expect(tabState.activeWorkspacePath).toBe("/workspace-b");
  229. expect(workspaceState("/workspace-a").activeTaskId).toBe("main-session");
  230. expect(workspaceState("/workspace-b").activeTaskId).toBeNull();
  231. expect(workspaceState("/workspace-b").draftFocusVersion).toBe(1);
  232. expect(countPanes(usePaneLayoutStore.getState())).toBe(1);
  233. });
  234. it("ignores a hidden workbench group when remote control creates a new task", () => {
  235. useWorkbenchGroupStore.getState().splitSessionIntoGroup(
  236. V4_PRIMARY_PANE_ID,
  237. "right",
  238. {
  239. workspaceScope: {
  240. workspacePath: "/workspace-b",
  241. workspaceIdentity: "remote:ssh:dev:/workspace-b",
  242. },
  243. sessionId: "secondary-session",
  244. },
  245. {
  246. workspaceScope: {
  247. workspacePath: "/workspace-a",
  248. workspaceIdentity: "remote:ssh:dev:/workspace-a",
  249. },
  250. sessionId: "main-session",
  251. },
  252. );
  253. usePaneLayoutStore.getState().splitPaneWithBinding(V4_PRIMARY_PANE_ID, "right", {
  254. workspaceScope: {
  255. workspacePath: "/workspace-b",
  256. workspaceIdentity: "remote:ssh:dev:/workspace-b",
  257. },
  258. sessionId: "hidden-secondary-session",
  259. });
  260. const { getHandleCreateTask, tabState } = renderRootActions({
  261. activeWorkspaceIdentity: "remote:ssh:dev:/workspace-a",
  262. workbenchGroupClientMode: "web-remote-replayable",
  263. });
  264. act(() => {
  265. getHandleCreateTask()();
  266. });
  267. expect(tabState.activeWorkspacePath).toBe("/workspace-a");
  268. expect(tabState.activeWorkspaceIdentity).toBe("remote:ssh:dev:/workspace-a");
  269. expect(
  270. workspaceState("/workspace-a", "remote:ssh:dev:/workspace-a").activeTaskId,
  271. ).toBeNull();
  272. });
  273. it("preserves initial prompt trailing space in the targeted remote identity bucket", () => {
  274. const workspacePath = "/workspace-a";
  275. const workspaceIdentity = "remote:ssh:dev:/workspace-a";
  276. const otherWorkspaceIdentity = "remote:ssh:other:/workspace-a";
  277. const mention = {
  278. id: "skill:skill-creator",
  279. category: "skills" as const,
  280. label: "skill-creator",
  281. value: "skill-creator",
  282. markdown: "$skill-creator",
  283. };
  284. const { getHandleCreateTask } = renderRootActions({
  285. activeWorkspacePath: workspacePath,
  286. activeWorkspaceIdentity: workspaceIdentity,
  287. });
  288. act(() => {
  289. getHandleCreateTask()({
  290. initialPrompt: "$skill-creator ",
  291. initialPromptMention: mention,
  292. });
  293. });
  294. expect(workspaceState(workspacePath, workspaceIdentity).composerTextInsertRequest).toEqual({
  295. requestId: 1,
  296. text: "$skill-creator ",
  297. mention,
  298. });
  299. expect(
  300. workspaceState(workspacePath, otherWorkspaceIdentity).composerTextInsertRequest,
  301. ).toBeNull();
  302. });
  303. it("selects a conversation backing workspace without treating it as a project", () => {
  304. const { addTab, getHandleSelectConversationWorkspace } = renderRootActions();
  305. act(() => {
  306. getHandleSelectConversationWorkspace()("/Users/demo/.zcode/workspace/default");
  307. });
  308. expect(addTab).toHaveBeenCalledWith("/Users/demo/.zcode/workspace/default", {
  309. workspacePurpose: "conversation",
  310. });
  311. });
  312. });