subagentSidePane.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { createElement, type ReactNode } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { afterEach, describe, expect, it, vi } from "vitest";
  4. import { SubagentSessionSidePane } from "@/app-shell/SubagentSessionSidePane.js";
  5. import {
  6. closeSidePaneTabs,
  7. getVisibleSidePaneTabs,
  8. openSubagentSessionSidePane,
  9. selectSidePaneTabsForScope,
  10. } from "@/lib/workspaceSidePane.js";
  11. import { openSubagentSessionFromSummary } from "@/v4/ConversationAgentToolCallRow.js";
  12. import type { ConversationRowRenderContext } from "@/v4/conversationRowContext.js";
  13. const paneState = vi.hoisted(() => ({ props: null as unknown }));
  14. vi.mock("@/v4/SessionPane.js", async () => {
  15. const React = await import("react");
  16. return {
  17. SessionPane: (props: Record<string, unknown>) => {
  18. paneState.props = props;
  19. return React.createElement("div", { "data-testid": "mock-session-pane" });
  20. },
  21. };
  22. });
  23. vi.mock("@/v4/V4ConversationContext.js", async () => {
  24. const React = await import("react");
  25. return {
  26. V4PaneConversationProvider: ({ children }: { children?: ReactNode }) =>
  27. React.createElement(React.Fragment, null, children),
  28. };
  29. });
  30. describe("subagent side pane tabs", () => {
  31. afterEach(() => {
  32. paneState.props = null;
  33. });
  34. it("opens foreground, background, mobile and nested child sessions through the same callback", () => {
  35. const requests: unknown[] = [];
  36. const context = {
  37. sessionId: "parent-session",
  38. rootSessionId: "root-session",
  39. workspacePath: "/workspace",
  40. onOpenSubagentSession: (request: unknown) => requests.push(request),
  41. } as unknown as ConversationRowRenderContext;
  42. expect(
  43. openSubagentSessionFromSummary({
  44. childSessionId: "child-session",
  45. context,
  46. subagentType: "Explore",
  47. title: "Explore project structure",
  48. }),
  49. ).toBe(true);
  50. expect(requests).toEqual([
  51. {
  52. rootSessionId: "root-session",
  53. parentSessionId: "parent-session",
  54. childSessionId: "child-session",
  55. subagentType: "Explore",
  56. title: "Explore project structure",
  57. },
  58. ]);
  59. for (const allowedContext of [
  60. { ...context, compactForRemoteControl: true },
  61. { ...context, inSubagentDrilldown: true, sessionId: "nested-parent" },
  62. ]) {
  63. expect(
  64. openSubagentSessionFromSummary({
  65. backgrounded: true,
  66. childSessionId: "child-session",
  67. context: allowedContext,
  68. subagentType: "Explore",
  69. title: "Explore project structure",
  70. }),
  71. ).toBe(true);
  72. }
  73. for (const blockedContext of [
  74. { ...context, sessionId: null },
  75. { ...context, onOpenSubagentSession: undefined },
  76. ]) {
  77. expect(
  78. openSubagentSessionFromSummary({
  79. childSessionId: "child-session",
  80. context: blockedContext,
  81. subagentType: "Explore",
  82. title: "Explore project structure",
  83. }),
  84. ).toBe(false);
  85. }
  86. expect(
  87. openSubagentSessionFromSummary({
  88. childSessionId: undefined,
  89. context,
  90. subagentType: "Explore",
  91. title: "Explore project structure",
  92. }),
  93. ).toBe(false);
  94. expect(requests).toHaveLength(3);
  95. });
  96. it("opens more than four child tabs and reuses a child identity", () => {
  97. let state = null;
  98. for (let index = 1; index <= 6; index += 1) {
  99. state = openSubagentSessionSidePane(state, {
  100. workspaceKey: "/workspace",
  101. workspacePath: "/workspace",
  102. parentSessionId: "parent-a",
  103. childSessionId: `child-${index}`,
  104. subagentType: index % 2 === 0 ? "Explore" : "general-purpose",
  105. title: `Task title ${index}`,
  106. });
  107. }
  108. const reopened = openSubagentSessionSidePane(state, {
  109. workspaceKey: "/workspace",
  110. workspacePath: "/workspace",
  111. parentSessionId: "parent-a",
  112. childSessionId: "child-2",
  113. subagentType: "Explore",
  114. title: "Updated task title",
  115. });
  116. expect(reopened.tabs).toHaveLength(6);
  117. expect(reopened.activeTabId).toContain("child-2");
  118. expect(reopened.tabs.find((tab) => tab.id === reopened.activeTabId)).toMatchObject({
  119. title: "Updated task title",
  120. });
  121. const reopenedWithoutTitle = openSubagentSessionSidePane(reopened, {
  122. workspaceKey: "/workspace",
  123. workspacePath: "/workspace",
  124. parentSessionId: "parent-a",
  125. childSessionId: "child-2",
  126. subagentType: "Explore",
  127. title: " ",
  128. });
  129. expect(
  130. reopenedWithoutTitle.tabs.find((tab) => tab.id === reopenedWithoutTitle.activeTabId),
  131. ).toMatchObject({ title: "Updated task title" });
  132. });
  133. it("shows shared tabs plus the active parent group and preserves hidden groups", () => {
  134. const shared = {
  135. activeTabId: "git",
  136. tabs: [{ id: "git", type: "git" as const }],
  137. };
  138. const withA = openSubagentSessionSidePane(shared, {
  139. workspaceKey: "/workspace",
  140. workspacePath: "/workspace",
  141. parentSessionId: "parent-a",
  142. childSessionId: "child-a",
  143. subagentType: "Explore",
  144. title: "Explore parent A",
  145. });
  146. const withB = openSubagentSessionSidePane(withA, {
  147. workspaceKey: "/workspace",
  148. workspacePath: "/workspace",
  149. parentSessionId: "parent-b",
  150. childSessionId: "child-b",
  151. subagentType: "general-purpose",
  152. title: "Explore parent B",
  153. });
  154. expect(
  155. getVisibleSidePaneTabs(withB.tabs, {
  156. workspaceKey: "/workspace",
  157. ownerTaskId: "parent-a",
  158. }).map((tab) => tab.id),
  159. ).toEqual(["git", expect.stringContaining("child-a")]);
  160. const selectedA = selectSidePaneTabsForScope(withB, {
  161. workspaceKey: "/workspace",
  162. ownerTaskId: "parent-a",
  163. });
  164. expect(selectedA?.activeTabId).toContain("child-a");
  165. const closedVisibleA = closeSidePaneTabs(
  166. selectedA,
  167. getVisibleSidePaneTabs(selectedA?.tabs ?? [], {
  168. workspaceKey: "/workspace",
  169. ownerTaskId: "parent-a",
  170. }).map((tab) => tab.id),
  171. { workspaceKey: "/workspace", ownerTaskId: "parent-a" },
  172. );
  173. expect(closedVisibleA?.tabs).toHaveLength(1);
  174. expect(closedVisibleA?.tabs[0]).toMatchObject({
  175. type: "subagent-session",
  176. parentSessionId: "parent-b",
  177. });
  178. expect(closedVisibleA?.activeTabId).toBe("");
  179. });
  180. it("isolates same-path remote workspaces by workspace identity", () => {
  181. const first = openSubagentSessionSidePane(null, {
  182. workspaceKey: "remote:ssh:host-a:/workspace",
  183. workspacePath: "/workspace",
  184. workspaceIdentity: "remote:ssh:host-a:/workspace",
  185. parentSessionId: "parent",
  186. childSessionId: "child",
  187. subagentType: "Explore",
  188. title: "Explore host A",
  189. });
  190. const second = openSubagentSessionSidePane(first, {
  191. workspaceKey: "remote:ssh:host-b:/workspace",
  192. workspacePath: "/workspace",
  193. workspaceIdentity: "remote:ssh:host-b:/workspace",
  194. parentSessionId: "parent",
  195. childSessionId: "child",
  196. subagentType: "Explore",
  197. title: "Explore host B",
  198. });
  199. expect(second.tabs).toHaveLength(2);
  200. expect(second.tabs[0]?.id).not.toBe(second.tabs[1]?.id);
  201. expect(second.tabs).toEqual(
  202. expect.arrayContaining([
  203. expect.objectContaining({
  204. workspaceKey: "remote:ssh:host-a:/workspace",
  205. workspacePath: "/workspace",
  206. workspaceIdentity: "remote:ssh:host-a:/workspace",
  207. }),
  208. expect.objectContaining({
  209. workspaceKey: "remote:ssh:host-b:/workspace",
  210. workspacePath: "/workspace",
  211. workspaceIdentity: "remote:ssh:host-b:/workspace",
  212. }),
  213. ]),
  214. );
  215. });
  216. it("keeps the child conversation read-only while enabling its workspace file rewind", () => {
  217. const state = openSubagentSessionSidePane(null, {
  218. workspaceKey: "/workspace",
  219. workspacePath: "/workspace",
  220. parentSessionId: "parent-session",
  221. rootSessionId: "root-session",
  222. childSessionId: "child-session",
  223. subagentType: "Explore",
  224. title: "Explore child",
  225. });
  226. const tab = state.tabs.find((candidate) => candidate.type === "subagent-session");
  227. renderToStaticMarkup(
  228. createElement(SubagentSessionSidePane, {
  229. tab: tab!,
  230. focused: true,
  231. onOpenSubagentSession: vi.fn(),
  232. }),
  233. );
  234. expect(paneState.props).toMatchObject({
  235. allowWorkspaceFileRewind: true,
  236. compactForRemoteControl: false,
  237. readOnly: true,
  238. sessionId: "child-session",
  239. });
  240. renderToStaticMarkup(
  241. createElement(SubagentSessionSidePane, {
  242. tab: tab!,
  243. focused: true,
  244. compactForRemoteControl: true,
  245. onOpenSubagentSession: vi.fn(),
  246. }),
  247. );
  248. expect(paneState.props).toMatchObject({
  249. compactForRemoteControl: true,
  250. readOnly: true,
  251. sessionId: "child-session",
  252. });
  253. });
  254. });