sidePaneTabTrigger.test.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // @vitest-environment jsdom
  2. import { createElement, type ReactNode } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { cleanup, render } from "@testing-library/react";
  5. import { afterEach, describe, expect, it, vi } from "vitest";
  6. import {
  7. getPatchFileDisplayTarget,
  8. getSidePaneTabTitle,
  9. SidePaneTabIcon,
  10. SortableSidePaneTabTrigger,
  11. } from "@/app-shell/SidePaneTabTrigger.js";
  12. import { Tabs, TabsList } from "@/components/ui/tabs.js";
  13. import type { WorkspaceSidePaneTab } from "@/lib/workspaceSidePane.js";
  14. vi.mock("@dnd-kit/sortable", () => ({
  15. useSortable: () => ({
  16. attributes: {},
  17. listeners: {},
  18. setNodeRef: vi.fn(),
  19. transform: null,
  20. transition: undefined,
  21. isDragging: false,
  22. }),
  23. }));
  24. vi.mock("@/components/ui/context-menu.js", () => {
  25. const passthrough = ({ children }: { children: ReactNode }) => children;
  26. return {
  27. ContextMenu: passthrough,
  28. ContextMenuTrigger: passthrough,
  29. ContextMenuContent: ({ children }: { children: ReactNode }) =>
  30. createElement("div", { "data-testid": "context-menu-content" }, children),
  31. ContextMenuItem: ({ children }: { children: ReactNode }) =>
  32. createElement("div", { role: "menuitem" }, children),
  33. };
  34. });
  35. vi.mock("@/components/ui/tooltip.js", () => {
  36. const passthrough = ({ children }: { children: ReactNode }) => children;
  37. return {
  38. Tooltip: ({ children, open }: { children: ReactNode; open?: boolean }) =>
  39. createElement("div", { "data-tooltip-open": String(Boolean(open)) }, children),
  40. TooltipProvider: ({
  41. children,
  42. delayDuration,
  43. skipDelayDuration,
  44. }: {
  45. children: ReactNode;
  46. delayDuration?: number;
  47. skipDelayDuration?: number;
  48. }) =>
  49. createElement(
  50. "div",
  51. {
  52. "data-tooltip-delay": delayDuration,
  53. "data-tooltip-skip-delay": skipDelayDuration,
  54. },
  55. children,
  56. ),
  57. TooltipTrigger: passthrough,
  58. TooltipContent: ({ children }: { children: ReactNode }) =>
  59. createElement("div", { "data-testid": "side-pane-tab-tooltip" }, children),
  60. };
  61. });
  62. afterEach(cleanup);
  63. const browserTab: WorkspaceSidePaneTab = {
  64. id: "browser:test",
  65. type: "browser",
  66. faviconUrl: null,
  67. initialUrl: null,
  68. title: "Docs",
  69. };
  70. const workflowRunTab: Extract<WorkspaceSidePaneTab, { type: "workflow-run" }> = {
  71. id: "workflow-run:%2Fworkspace:parent:dwfrun-1",
  72. type: "workflow-run",
  73. workspaceKey: "/workspace",
  74. workspacePath: "/workspace",
  75. parentSessionId: "parent",
  76. toolCallId: "tool-wf-1",
  77. runId: "dwfrun-1",
  78. workflowName: "Three-way review",
  79. };
  80. const workflowActorTab: Extract<WorkspaceSidePaneTab, { type: "workflow-actor-session" }> = {
  81. id: "workflow-actor-session:%2Fworkspace:parent:dwf-dwfrun-1-actor_1%402",
  82. type: "workflow-actor-session",
  83. workspaceKey: "/workspace",
  84. workspacePath: "/workspace",
  85. parentSessionId: "parent",
  86. runId: "dwfrun-1",
  87. actorSessionId: "dwf-dwfrun-1-actor_1@2",
  88. siteId: "actor#1",
  89. ordinal: 2,
  90. actorName: "reviewer",
  91. };
  92. const workflowWorkspaceTab: Extract<WorkspaceSidePaneTab, { type: "workflow-workspace" }> = {
  93. id: "workflow-workspace:%2Fworkspace:parent:dwfrun-1",
  94. type: "workflow-workspace",
  95. workspaceKey: "/workspace",
  96. workspacePath: "/workspace",
  97. parentSessionId: "parent",
  98. toolCallId: "tool-wf-1",
  99. runId: "dwfrun-1",
  100. workflowName: "Three-way review",
  101. };
  102. function renderSidePaneTab(
  103. isActive: boolean,
  104. title = "Docs",
  105. tab: WorkspaceSidePaneTab = browserTab,
  106. ) {
  107. return renderToStaticMarkup(
  108. createElement(
  109. Tabs,
  110. { value: isActive ? tab.id : "other" },
  111. createElement(
  112. TabsList,
  113. null,
  114. createElement(SortableSidePaneTabTrigger, {
  115. tab,
  116. title,
  117. closeTabLabel: "Close Docs",
  118. closeTabMenuLabel: "Close tab",
  119. closeOtherTabsLabel: "Close other tabs",
  120. closeAllTabsLabel: "Close all tabs",
  121. diffBadgeLabel: "Diff",
  122. isActive,
  123. onActivateTab: vi.fn(),
  124. onCloseTab: vi.fn(),
  125. onCloseOtherTabs: vi.fn(),
  126. onCloseAllTabs: vi.fn(),
  127. canCloseOtherTabs: false,
  128. }),
  129. ),
  130. ),
  131. );
  132. }
  133. describe("getPatchFileDisplayTarget", () => {
  134. it("falls back to patch headers when patch path is /dev/null", () => {
  135. expect(
  136. getPatchFileDisplayTarget({
  137. title: "Diff",
  138. path: "/dev/null",
  139. patch:
  140. "--- /dev/null\n" +
  141. "+++ b/packages/ui/src/app-shell/SidePaneTabTrigger.tsx\n" +
  142. "@@\n" +
  143. "+content",
  144. }),
  145. ).toBe("packages/ui/src/app-shell/SidePaneTabTrigger.tsx");
  146. });
  147. it("strips unified diff timestamps before file icon detection", () => {
  148. expect(
  149. getPatchFileDisplayTarget({
  150. title: "Diff",
  151. patch:
  152. "--- a/packages/ui/src/app-shell/SidePaneTabTrigger.tsx\t2026-04-21\n" +
  153. "+++ b/packages/ui/src/app-shell/SidePaneTabTrigger.tsx\t2026-04-21\n" +
  154. "@@\n" +
  155. "-old\n" +
  156. "+new",
  157. }),
  158. ).toBe("packages/ui/src/app-shell/SidePaneTabTrigger.tsx");
  159. });
  160. it("supports quoted git diff paths", () => {
  161. expect(
  162. getPatchFileDisplayTarget({
  163. title: "Diff",
  164. patch:
  165. 'diff --git "a/packages/ui/src/Demo View.tsx" "b/packages/ui/src/Demo View.tsx"\n' +
  166. "--- a/packages/ui/src/Demo View.tsx\n" +
  167. "+++ b/packages/ui/src/Demo View.tsx\n" +
  168. "@@\n" +
  169. "-old\n" +
  170. "+new",
  171. }),
  172. ).toBe("packages/ui/src/Demo View.tsx");
  173. });
  174. });
  175. describe("getSidePaneTabTitle", () => {
  176. const subagentTab: Extract<WorkspaceSidePaneTab, { type: "subagent-session" }> = {
  177. id: "subagent-session:workspace:parent:child",
  178. type: "subagent-session",
  179. workspaceKey: "/workspace",
  180. workspacePath: "/workspace",
  181. parentSessionId: "parent",
  182. childSessionId: "child",
  183. subagentType: "Explore",
  184. title: "Explore project structure",
  185. };
  186. it("uses the authoritative subagent title without type or ordinal", () => {
  187. expect(getSidePaneTabTitle(subagentTab, ({ id }) => id)).toBe("Explore project structure");
  188. });
  189. it("falls back to the localized generic label when title is empty", () => {
  190. expect(
  191. getSidePaneTabTitle({ ...subagentTab, title: " " }, ({ id }) =>
  192. id === "sidePane.subagent" ? "Subagent" : id,
  193. ),
  194. ).toBe("Subagent");
  195. });
  196. it("appends the auxiliary conversation ordinal to the localized label", () => {
  197. expect(
  198. getSidePaneTabTitle(
  199. {
  200. id: "selection-side-chat:workspace:parent:child",
  201. type: "selection-side-chat",
  202. workspaceKey: "/workspace",
  203. workspacePath: "/workspace",
  204. parentSessionId: "parent",
  205. childSessionId: "child",
  206. ordinal: 2,
  207. },
  208. ({ id }) => (id === "sidePane.selectionChat" ? "Auxiliary conversation" : id),
  209. ),
  210. ).toBe("Auxiliary conversation 2");
  211. });
  212. it("prefers the frozen workflow name over the generic workflow run label", () => {
  213. expect(
  214. getSidePaneTabTitle(workflowRunTab, ({ id }) =>
  215. id === "sidePane.workflowRun" ? "Workflow run" : id,
  216. ),
  217. ).toBe("Three-way review");
  218. });
  219. it("falls back to the localized workflow run label when no name was captured", () => {
  220. const { workflowName: _dropped, ...unnamed } = workflowRunTab;
  221. expect(
  222. getSidePaneTabTitle(unnamed, ({ id }) =>
  223. id === "sidePane.workflowRun" ? "Workflow run" : id,
  224. ),
  225. ).toBe("Workflow run");
  226. });
  227. it("titles an actor transcript with its name and always with its instance ordinal", () => {
  228. // 同一车道族的实例名字相同(脚本里只写了一次 `agent("reviewer")`),所以序号是标题里
  229. // 唯一能区分它们的东西——tab 条上两个「reviewer」是无法辨认的。序号用拼接而不是
  230. // 本地化模板:照 selection-side-chat 的先例(`${label} ${ordinal}`),标签映射表因此
  231. // 仍然是「id → 串」,不必背插值。
  232. expect(getSidePaneTabTitle(workflowActorTab, ({ id }) => id)).toBe("reviewer #2");
  233. });
  234. it("falls back to the localized actor label, still with the ordinal", () => {
  235. const { actorName: _dropped, ...unnamed } = workflowActorTab;
  236. expect(
  237. getSidePaneTabTitle(unnamed, ({ id }) =>
  238. id === "sidePane.workflowActor" ? "Workflow subagent" : id,
  239. ),
  240. ).toBe("Workflow subagent #2");
  241. });
  242. it("titles a workspace transcript with the run name: one run, one workspace transcript", () => {
  243. // docs/dynamic-workflow/transcript-and-notifications.md「Opening the tab」:标题是 run 名,类型词在 tooltip 里。
  244. expect(getSidePaneTabTitle(workflowWorkspaceTab, ({ id }) => id)).toBe("Three-way review");
  245. });
  246. it("falls back to the localized script label when no name was captured", () => {
  247. const { workflowName: _dropped, ...unnamed } = workflowWorkspaceTab;
  248. expect(
  249. getSidePaneTabTitle(unnamed, ({ id }) =>
  250. id === "sidePane.workflowScript" ? "Script steps" : id,
  251. ),
  252. ).toBe("Script steps");
  253. });
  254. });
  255. describe("SortableSidePaneTabTrigger", () => {
  256. it("keeps active state markers on the real side pane tab element", () => {
  257. const html = renderSidePaneTab(true);
  258. const tabMarkup = html.match(/<div data-side-pane-tab-id="browser:test"[^>]*>/)?.[0] ?? "";
  259. // Bugfix: ContextMenuTrigger + TabsTrigger asChild 会让 Radix 状态落点不稳定。
  260. // 右侧面板 tab 的 active 样式依赖 data-active,所以必须由受控 isActive 补到真实 DOM。
  261. expect(tabMarkup).toContain('data-side-pane-tab-id="browser:test"');
  262. expect(tabMarkup).toMatch(/\sdata-active(=|\s|>)/);
  263. expect(tabMarkup).toContain('data-state="active"');
  264. });
  265. it("does not mark inactive side pane tabs as active", () => {
  266. const html = renderSidePaneTab(false);
  267. const tabMarkup = html.match(/<div data-side-pane-tab-id="browser:test"[^>]*>/)?.[0] ?? "";
  268. expect(tabMarkup).toContain('data-side-pane-tab-id="browser:test"');
  269. expect(tabMarkup).not.toMatch(/\sdata-active(=|\s|>)/);
  270. expect(tabMarkup).toContain('data-state="inactive"');
  271. });
  272. it("exposes browser residency on the logical tab shell", () => {
  273. const html = renderSidePaneTab(false, "Browser", {
  274. ...browserTab,
  275. residency: "suspended",
  276. });
  277. const tabMarkup = html.match(/<div data-side-pane-tab-id="browser:test"[^>]*>/)?.[0] ?? "";
  278. expect(tabMarkup).toContain('data-browser-tab-residency="suspended"');
  279. });
  280. it("loads the browser favicon without leaking the host page referrer", () => {
  281. const html = renderSidePaneTab(false, "Browser", {
  282. ...browserTab,
  283. faviconUrl: "https://example.com/favicon.ico",
  284. });
  285. expect(html).toContain('src="https://example.com/favicon.ico"');
  286. expect(html).toContain('referrerPolicy="no-referrer"');
  287. });
  288. it("renders close, close other, and close all context menu actions", () => {
  289. const html = renderSidePaneTab(true);
  290. expect(html).toContain("Close tab");
  291. expect(html).toContain("Close other tabs");
  292. expect(html).toContain("Close all tabs");
  293. });
  294. it("always renders the close button for active and inactive tabs", () => {
  295. expect(renderSidePaneTab(true)).toContain('aria-label="Close Docs"');
  296. expect(renderSidePaneTab(false)).toContain('aria-label="Close Docs"');
  297. });
  298. it("closes an inactive tab on middle-click without activating or bubbling it", () => {
  299. const onActivateTab = vi.fn();
  300. const onCloseTab = vi.fn();
  301. const onParentAuxClick = vi.fn();
  302. const { container } = render(
  303. createElement(
  304. "div",
  305. { onAuxClick: onParentAuxClick },
  306. createElement(
  307. Tabs,
  308. { value: "other" },
  309. createElement(
  310. TabsList,
  311. null,
  312. createElement(SortableSidePaneTabTrigger, {
  313. tab: browserTab,
  314. title: "Docs",
  315. closeTabLabel: "Close Docs",
  316. closeTabMenuLabel: "Close tab",
  317. closeOtherTabsLabel: "Close other tabs",
  318. closeAllTabsLabel: "Close all tabs",
  319. diffBadgeLabel: "Diff",
  320. isActive: false,
  321. onActivateTab,
  322. onCloseTab,
  323. onCloseOtherTabs: vi.fn(),
  324. onCloseAllTabs: vi.fn(),
  325. canCloseOtherTabs: false,
  326. }),
  327. ),
  328. ),
  329. ),
  330. );
  331. const tabShell = container.querySelector<HTMLElement>('[data-side-pane-tab-id="browser:test"]');
  332. expect(tabShell).not.toBeNull();
  333. const auxclick = new MouseEvent("auxclick", {
  334. bubbles: true,
  335. cancelable: true,
  336. button: 1,
  337. });
  338. const preventDefault = vi.spyOn(auxclick, "preventDefault");
  339. const stopPropagation = vi.spyOn(auxclick, "stopPropagation");
  340. const notCanceled = tabShell!.dispatchEvent(auxclick);
  341. expect(notCanceled).toBe(false);
  342. expect(auxclick.defaultPrevented).toBe(true);
  343. expect(preventDefault).toHaveBeenCalledOnce();
  344. expect(stopPropagation).toHaveBeenCalledOnce();
  345. expect(onCloseTab).toHaveBeenCalledWith(browserTab.id);
  346. expect(onActivateTab).not.toHaveBeenCalled();
  347. expect(onParentAuxClick).not.toHaveBeenCalled();
  348. });
  349. });
  350. describe("SidePaneTabIcon browser-use operation state", () => {
  351. it("temporarily replaces the favicon with an animated mouse pointer", () => {
  352. const html = renderToStaticMarkup(
  353. createElement(SidePaneTabIcon, {
  354. tab: {
  355. id: "browser-use:tab-1",
  356. type: "browser-use",
  357. sessionId: "sess-1",
  358. tabId: "tab-1",
  359. workspaceKey: "/workspace",
  360. faviconUrl: "https://example.com/favicon.ico",
  361. browserUseOperationUntil: Date.now() + 5_000,
  362. },
  363. }),
  364. );
  365. expect(html).toContain('data-browser-use-operation-indicator="active"');
  366. expect(html).toContain("browser-use-operation-breathe");
  367. expect(html).not.toContain("https://example.com/favicon.ico");
  368. });
  369. it("restores the real favicon after the operation deadline", () => {
  370. const html = renderToStaticMarkup(
  371. createElement(SidePaneTabIcon, {
  372. tab: {
  373. id: "browser-use:tab-1",
  374. type: "browser-use",
  375. sessionId: "sess-1",
  376. tabId: "tab-1",
  377. workspaceKey: "/workspace",
  378. faviconUrl: "https://example.com/favicon.ico",
  379. browserUseOperationUntil: Date.now() - 1,
  380. },
  381. }),
  382. );
  383. expect(html).not.toContain("data-browser-use-operation-indicator");
  384. expect(html).toContain("https://example.com/favicon.ico");
  385. });
  386. });