subagentSidePane.test.ts 8.6 KB

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