sidePaneTabTrigger.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. // @vitest-environment jsdom
  2. import { createElement, type ReactNode } from "react";
  3. import { renderToStaticMarkup } from "react-dom/server";
  4. import { readFile } from "node:fs/promises";
  5. import { resolve } from "node:path";
  6. import { cleanup, render } from "@testing-library/react";
  7. import { afterEach, describe, expect, it, vi } from "vitest";
  8. import {
  9. getPatchFileDisplayTarget,
  10. getSidePaneTabTitle,
  11. SidePaneTabDragOverlay,
  12. SidePaneTabIcon,
  13. SortableSidePaneTabTrigger,
  14. } from "@/app-shell/SidePaneTabTrigger.js";
  15. import { SidePaneTabTitleTooltip } from "@/app-shell/SidePaneTabTitleTooltip.js";
  16. import { Tabs, TabsList } from "@/components/ui/tabs.js";
  17. import type { WorkspaceSidePaneTab } from "@/lib/workspaceSidePane.js";
  18. vi.mock("@dnd-kit/sortable", () => ({
  19. useSortable: () => ({
  20. attributes: {},
  21. listeners: {},
  22. setNodeRef: vi.fn(),
  23. transform: null,
  24. transition: undefined,
  25. isDragging: false,
  26. }),
  27. }));
  28. vi.mock("@/components/ui/context-menu.js", () => {
  29. const passthrough = ({ children }: { children: ReactNode }) => children;
  30. return {
  31. ContextMenu: passthrough,
  32. ContextMenuTrigger: passthrough,
  33. ContextMenuContent: ({ children }: { children: ReactNode }) =>
  34. createElement("div", { "data-testid": "context-menu-content" }, children),
  35. ContextMenuItem: ({ children }: { children: ReactNode }) =>
  36. createElement("div", { role: "menuitem" }, children),
  37. };
  38. });
  39. vi.mock("@/components/ui/tooltip.js", () => {
  40. const passthrough = ({ children }: { children: ReactNode }) => children;
  41. return {
  42. Tooltip: ({ children, open }: { children: ReactNode; open?: boolean }) =>
  43. createElement("div", { "data-tooltip-open": String(Boolean(open)) }, children),
  44. TooltipProvider: ({
  45. children,
  46. delayDuration,
  47. skipDelayDuration,
  48. }: {
  49. children: ReactNode;
  50. delayDuration?: number;
  51. skipDelayDuration?: number;
  52. }) =>
  53. createElement(
  54. "div",
  55. {
  56. "data-tooltip-delay": delayDuration,
  57. "data-tooltip-skip-delay": skipDelayDuration,
  58. },
  59. children,
  60. ),
  61. TooltipTrigger: passthrough,
  62. TooltipContent: ({ children }: { children: ReactNode }) =>
  63. createElement("div", { "data-testid": "side-pane-tab-tooltip" }, children),
  64. };
  65. });
  66. afterEach(cleanup);
  67. const browserTab: WorkspaceSidePaneTab = {
  68. id: "browser:test",
  69. type: "browser",
  70. faviconUrl: null,
  71. initialUrl: null,
  72. title: "Docs",
  73. };
  74. const workflowRunTab: Extract<WorkspaceSidePaneTab, { type: "workflow-run" }> = {
  75. id: "workflow-run:%2Fworkspace:parent:dwfrun-1",
  76. type: "workflow-run",
  77. workspaceKey: "/workspace",
  78. workspacePath: "/workspace",
  79. parentSessionId: "parent",
  80. toolCallId: "tool-wf-1",
  81. runId: "dwfrun-1",
  82. workflowName: "Three-way review",
  83. };
  84. const workflowActorTab: Extract<WorkspaceSidePaneTab, { type: "workflow-actor-session" }> = {
  85. id: "workflow-actor-session:%2Fworkspace:parent:dwf-dwfrun-1-actor_1%402",
  86. type: "workflow-actor-session",
  87. workspaceKey: "/workspace",
  88. workspacePath: "/workspace",
  89. parentSessionId: "parent",
  90. runId: "dwfrun-1",
  91. actorSessionId: "dwf-dwfrun-1-actor_1@2",
  92. siteId: "actor#1",
  93. ordinal: 2,
  94. actorName: "reviewer",
  95. };
  96. const workflowWorkspaceTab: Extract<WorkspaceSidePaneTab, { type: "workflow-workspace" }> = {
  97. id: "workflow-workspace:%2Fworkspace:parent:dwfrun-1",
  98. type: "workflow-workspace",
  99. workspaceKey: "/workspace",
  100. workspacePath: "/workspace",
  101. parentSessionId: "parent",
  102. toolCallId: "tool-wf-1",
  103. runId: "dwfrun-1",
  104. workflowName: "Three-way review",
  105. };
  106. function renderSidePaneTab(
  107. isActive: boolean,
  108. title = "Docs",
  109. tab: WorkspaceSidePaneTab = browserTab,
  110. ) {
  111. return renderToStaticMarkup(
  112. createElement(
  113. Tabs,
  114. { value: isActive ? tab.id : "other" },
  115. createElement(
  116. TabsList,
  117. null,
  118. createElement(SortableSidePaneTabTrigger, {
  119. tab,
  120. title,
  121. closeTabLabel: "Close Docs",
  122. closeTabMenuLabel: "Close tab",
  123. closeOtherTabsLabel: "Close other tabs",
  124. closeAllTabsLabel: "Close all tabs",
  125. diffBadgeLabel: "Diff",
  126. isActive,
  127. onActivateTab: vi.fn(),
  128. onCloseTab: vi.fn(),
  129. onCloseOtherTabs: vi.fn(),
  130. onCloseAllTabs: vi.fn(),
  131. canCloseOtherTabs: false,
  132. }),
  133. ),
  134. ),
  135. );
  136. }
  137. describe("getPatchFileDisplayTarget", () => {
  138. it("falls back to patch headers when patch path is /dev/null", () => {
  139. expect(
  140. getPatchFileDisplayTarget({
  141. title: "Diff",
  142. path: "/dev/null",
  143. patch:
  144. "--- /dev/null\n" +
  145. "+++ b/packages/ui/src/app-shell/SidePaneTabTrigger.tsx\n" +
  146. "@@\n" +
  147. "+content",
  148. }),
  149. ).toBe("packages/ui/src/app-shell/SidePaneTabTrigger.tsx");
  150. });
  151. it("strips unified diff timestamps before file icon detection", () => {
  152. expect(
  153. getPatchFileDisplayTarget({
  154. title: "Diff",
  155. patch:
  156. "--- a/packages/ui/src/app-shell/SidePaneTabTrigger.tsx\t2026-04-21\n" +
  157. "+++ b/packages/ui/src/app-shell/SidePaneTabTrigger.tsx\t2026-04-21\n" +
  158. "@@\n" +
  159. "-old\n" +
  160. "+new",
  161. }),
  162. ).toBe("packages/ui/src/app-shell/SidePaneTabTrigger.tsx");
  163. });
  164. it("supports quoted git diff paths", () => {
  165. expect(
  166. getPatchFileDisplayTarget({
  167. title: "Diff",
  168. patch:
  169. 'diff --git "a/packages/ui/src/Demo View.tsx" "b/packages/ui/src/Demo View.tsx"\n' +
  170. "--- a/packages/ui/src/Demo View.tsx\n" +
  171. "+++ b/packages/ui/src/Demo View.tsx\n" +
  172. "@@\n" +
  173. "-old\n" +
  174. "+new",
  175. }),
  176. ).toBe("packages/ui/src/Demo View.tsx");
  177. });
  178. });
  179. describe("getSidePaneTabTitle", () => {
  180. const subagentTab: Extract<WorkspaceSidePaneTab, { type: "subagent-session" }> = {
  181. id: "subagent-session:workspace:parent:child",
  182. type: "subagent-session",
  183. workspaceKey: "/workspace",
  184. workspacePath: "/workspace",
  185. parentSessionId: "parent",
  186. childSessionId: "child",
  187. subagentType: "Explore",
  188. title: "Explore project structure",
  189. };
  190. it("uses the authoritative subagent title without type or ordinal", () => {
  191. expect(getSidePaneTabTitle(subagentTab, ({ id }) => id)).toBe("Explore project structure");
  192. });
  193. it("falls back to the localized generic label when title is empty", () => {
  194. expect(
  195. getSidePaneTabTitle({ ...subagentTab, title: " " }, ({ id }) =>
  196. id === "sidePane.subagent" ? "Subagent" : id,
  197. ),
  198. ).toBe("Subagent");
  199. });
  200. it("appends the auxiliary conversation ordinal to the localized label", () => {
  201. expect(
  202. getSidePaneTabTitle(
  203. {
  204. id: "selection-side-chat:workspace:parent:child",
  205. type: "selection-side-chat",
  206. workspaceKey: "/workspace",
  207. workspacePath: "/workspace",
  208. parentSessionId: "parent",
  209. childSessionId: "child",
  210. ordinal: 2,
  211. },
  212. ({ id }) => (id === "sidePane.selectionChat" ? "Auxiliary conversation" : id),
  213. ),
  214. ).toBe("Auxiliary conversation 2");
  215. });
  216. it("prefers the frozen workflow name over the generic workflow run label", () => {
  217. expect(
  218. getSidePaneTabTitle(workflowRunTab, ({ id }) =>
  219. id === "sidePane.workflowRun" ? "Workflow run" : id,
  220. ),
  221. ).toBe("Three-way review");
  222. });
  223. it("falls back to the localized workflow run label when no name was captured", () => {
  224. const { workflowName: _dropped, ...unnamed } = workflowRunTab;
  225. expect(
  226. getSidePaneTabTitle(unnamed, ({ id }) =>
  227. id === "sidePane.workflowRun" ? "Workflow run" : id,
  228. ),
  229. ).toBe("Workflow run");
  230. });
  231. it("titles an actor transcript with its name and always with its instance ordinal", () => {
  232. // 同一车道族的实例名字相同(脚本里只写了一次 `agent("reviewer")`),所以序号是标题里
  233. // 唯一能区分它们的东西——tab 条上两个「reviewer」是无法辨认的。序号用拼接而不是
  234. // 本地化模板:照 selection-side-chat 的先例(`${label} ${ordinal}`),标签映射表因此
  235. // 仍然是「id → 串」,不必背插值。
  236. expect(getSidePaneTabTitle(workflowActorTab, ({ id }) => id)).toBe("reviewer #2");
  237. });
  238. it("falls back to the localized actor label, still with the ordinal", () => {
  239. const { actorName: _dropped, ...unnamed } = workflowActorTab;
  240. expect(
  241. getSidePaneTabTitle(unnamed, ({ id }) =>
  242. id === "sidePane.workflowActor" ? "Workflow subagent" : id,
  243. ),
  244. ).toBe("Workflow subagent #2");
  245. });
  246. it("titles a workspace transcript with the run name: one run, one workspace transcript", () => {
  247. // docs/dynamic-workflow/transcript-and-notifications.md「Opening the tab」:标题是 run 名,类型词在 tooltip 里。
  248. expect(getSidePaneTabTitle(workflowWorkspaceTab, ({ id }) => id)).toBe("Three-way review");
  249. });
  250. it("falls back to the localized script label when no name was captured", () => {
  251. const { workflowName: _dropped, ...unnamed } = workflowWorkspaceTab;
  252. expect(
  253. getSidePaneTabTitle(unnamed, ({ id }) =>
  254. id === "sidePane.workflowScript" ? "Script steps" : id,
  255. ),
  256. ).toBe("Script steps");
  257. });
  258. });
  259. describe("SortableSidePaneTabTrigger", () => {
  260. it("keeps active state markers on the real side pane tab element", () => {
  261. const html = renderSidePaneTab(true);
  262. const tabMarkup = html.match(/<div data-side-pane-tab-id="browser:test"[^>]*>/)?.[0] ?? "";
  263. // Bugfix: ContextMenuTrigger + TabsTrigger asChild 会让 Radix 状态落点不稳定。
  264. // 右侧面板 tab 的 active 样式依赖 data-active,所以必须由受控 isActive 补到真实 DOM。
  265. expect(tabMarkup).toContain('data-side-pane-tab-id="browser:test"');
  266. expect(tabMarkup).toMatch(/\sdata-active(=|\s|>)/);
  267. expect(tabMarkup).toContain('data-state="active"');
  268. });
  269. it("does not mark inactive side pane tabs as active", () => {
  270. const html = renderSidePaneTab(false);
  271. const tabMarkup = html.match(/<div data-side-pane-tab-id="browser:test"[^>]*>/)?.[0] ?? "";
  272. expect(tabMarkup).toContain('data-side-pane-tab-id="browser:test"');
  273. expect(tabMarkup).not.toMatch(/\sdata-active(=|\s|>)/);
  274. expect(tabMarkup).toContain('data-state="inactive"');
  275. });
  276. it("exposes browser residency on the logical tab shell", () => {
  277. const html = renderSidePaneTab(false, "Browser", {
  278. ...browserTab,
  279. residency: "suspended",
  280. });
  281. const tabMarkup = html.match(/<div data-side-pane-tab-id="browser:test"[^>]*>/)?.[0] ?? "";
  282. expect(tabMarkup).toContain('data-browser-tab-residency="suspended"');
  283. });
  284. it("loads the browser favicon without leaking the host page referrer", () => {
  285. const html = renderSidePaneTab(false, "Browser", {
  286. ...browserTab,
  287. faviconUrl: "https://example.com/favicon.ico",
  288. });
  289. expect(html).toContain('src="https://example.com/favicon.ico"');
  290. expect(html).toContain('referrerPolicy="no-referrer"');
  291. });
  292. it("renders close, close other, and close all context menu actions", () => {
  293. const html = renderSidePaneTab(true);
  294. expect(html).toContain("Close tab");
  295. expect(html).toContain("Close other tabs");
  296. expect(html).toContain("Close all tabs");
  297. expect(html).not.toContain('role="separator"');
  298. });
  299. it("always renders the close button for active and inactive tabs", () => {
  300. expect(renderSidePaneTab(true)).toContain('aria-label="Close Docs"');
  301. expect(renderSidePaneTab(false)).toContain('aria-label="Close Docs"');
  302. });
  303. it("closes an inactive tab on middle-click without activating or bubbling it", () => {
  304. const onActivateTab = vi.fn();
  305. const onCloseTab = vi.fn();
  306. const onParentAuxClick = vi.fn();
  307. const { container } = render(
  308. createElement(
  309. "div",
  310. { onAuxClick: onParentAuxClick },
  311. createElement(
  312. Tabs,
  313. { value: "other" },
  314. createElement(
  315. TabsList,
  316. null,
  317. createElement(SortableSidePaneTabTrigger, {
  318. tab: browserTab,
  319. title: "Docs",
  320. closeTabLabel: "Close Docs",
  321. closeTabMenuLabel: "Close tab",
  322. closeOtherTabsLabel: "Close other tabs",
  323. closeAllTabsLabel: "Close all tabs",
  324. diffBadgeLabel: "Diff",
  325. isActive: false,
  326. onActivateTab,
  327. onCloseTab,
  328. onCloseOtherTabs: vi.fn(),
  329. onCloseAllTabs: vi.fn(),
  330. canCloseOtherTabs: false,
  331. }),
  332. ),
  333. ),
  334. ),
  335. );
  336. const tabShell = container.querySelector<HTMLElement>('[data-side-pane-tab-id="browser:test"]');
  337. expect(tabShell).not.toBeNull();
  338. const auxclick = new MouseEvent("auxclick", {
  339. bubbles: true,
  340. cancelable: true,
  341. button: 1,
  342. });
  343. const preventDefault = vi.spyOn(auxclick, "preventDefault");
  344. const stopPropagation = vi.spyOn(auxclick, "stopPropagation");
  345. const notCanceled = tabShell!.dispatchEvent(auxclick);
  346. expect(notCanceled).toBe(false);
  347. expect(auxclick.defaultPrevented).toBe(true);
  348. expect(preventDefault).toHaveBeenCalledOnce();
  349. expect(stopPropagation).toHaveBeenCalledOnce();
  350. expect(onCloseTab).toHaveBeenCalledWith(browserTab.id);
  351. expect(onActivateTab).not.toHaveBeenCalled();
  352. expect(onParentAuxClick).not.toHaveBeenCalled();
  353. });
  354. it("shows the full title through an independently delayed tooltip", () => {
  355. const html = renderSidePaneTab(true, "A complete tab title");
  356. expect(html).toContain('data-tooltip-delay="1500"');
  357. expect(html).toContain('data-tooltip-skip-delay="0"');
  358. expect(html).toContain('data-testid="side-pane-tab-tooltip"');
  359. expect(html).toContain("A complete tab title");
  360. });
  361. it("uses a 156px preferred width and an equal-shrink 60px minimum", () => {
  362. const longTitle = "A very long browser tab title that must be truncated";
  363. const tabHtml = renderSidePaneTab(true, longTitle);
  364. const overlayHtml = renderToStaticMarkup(
  365. createElement(SidePaneTabDragOverlay, {
  366. tab: browserTab,
  367. title: longTitle,
  368. diffBadgeLabel: "Diff",
  369. }),
  370. );
  371. expect(tabHtml).toContain("flex-[1_1_9.75rem]");
  372. expect(tabHtml).toContain("min-w-15");
  373. expect(tabHtml).toContain("max-w-39");
  374. expect(tabHtml).toContain("flex-1");
  375. expect(tabHtml).toContain('data-side-pane-tab-content=""');
  376. expect(tabHtml).toContain("mask-image");
  377. expect(tabHtml).not.toMatch(/class="[^"]*\btruncate\b/);
  378. expect(overlayHtml).toContain("w-39");
  379. expect(overlayHtml).toContain('data-side-pane-tab-content=""');
  380. expect(overlayHtml).toContain("mask-image");
  381. expect(overlayHtml).not.toMatch(/class="[^"]*\btruncate\b/);
  382. });
  383. });
  384. describe("SidePaneTabTitleTooltip", () => {
  385. it("uses the shared tooltip default side offset", async () => {
  386. const source = await readFile(
  387. resolve(process.cwd(), "packages/ui/src/app-shell/SidePaneTabTitleTooltip.tsx"),
  388. "utf8",
  389. );
  390. expect(source).not.toContain("sideOffset=");
  391. });
  392. it("preserves the trigger shrink behavior required by responsive tab widths", () => {
  393. const html = renderToStaticMarkup(
  394. createElement(
  395. SidePaneTabTitleTooltip,
  396. { isDragging: false, title: "Docs" },
  397. createElement("div", { "data-testid": "responsive-tab-trigger" }),
  398. ),
  399. );
  400. const triggerMarkup = html.match(/<div data-testid="responsive-tab-trigger"[^>]*>/)?.[0] ?? "";
  401. expect(triggerMarkup).toContain('class="shrink"');
  402. expect(triggerMarkup).not.toContain("shrink-0");
  403. });
  404. });
  405. describe("SidePaneTabIcon browser-use operation state", () => {
  406. it("temporarily replaces the favicon with an animated mouse pointer", () => {
  407. const html = renderToStaticMarkup(
  408. createElement(SidePaneTabIcon, {
  409. tab: {
  410. id: "browser-use:tab-1",
  411. type: "browser-use",
  412. sessionId: "sess-1",
  413. tabId: "tab-1",
  414. workspaceKey: "/workspace",
  415. faviconUrl: "https://example.com/favicon.ico",
  416. browserUseOperationUntil: Date.now() + 5_000,
  417. },
  418. }),
  419. );
  420. expect(html).toContain('data-browser-use-operation-indicator="active"');
  421. expect(html).toContain("browser-use-operation-breathe");
  422. expect(html).not.toContain("motion-safe:animate-pulse");
  423. expect(html).not.toContain("https://example.com/favicon.ico");
  424. });
  425. it("restores the real favicon after the operation deadline", () => {
  426. const html = renderToStaticMarkup(
  427. createElement(SidePaneTabIcon, {
  428. tab: {
  429. id: "browser-use:tab-1",
  430. type: "browser-use",
  431. sessionId: "sess-1",
  432. tabId: "tab-1",
  433. workspaceKey: "/workspace",
  434. faviconUrl: "https://example.com/favicon.ico",
  435. browserUseOperationUntil: Date.now() - 1,
  436. },
  437. }),
  438. );
  439. expect(html).not.toContain("data-browser-use-operation-indicator");
  440. expect(html).toContain("https://example.com/favicon.ico");
  441. });
  442. });
  443. // Bug 原因:plan-detail tab 由 switch-mode(ExitPlanMode)工具调用卡片打开,
  444. // 来源卡片用 NotepadTextIcon。tab 图标之前误用 ListChecksIcon,导致用户点击
  445. // 卡片打开 tab 时图标跳变,且 ListChecksIcon 与状态面板 Todo section 撞图标。
  446. // 固化约定:plan-detail tab 图标必须与来源卡片一致,用 NotepadTextIcon。
  447. describe("SidePaneTabIcon plan-detail", () => {
  448. it("uses NotepadTextIcon to match the switch-mode source card", () => {
  449. const html = renderToStaticMarkup(
  450. createElement(SidePaneTabIcon, {
  451. tab: {
  452. id: "plan-detail:test",
  453. type: "plan-detail",
  454. workspaceKey: "/workspace",
  455. workspacePath: "/workspace",
  456. parentSessionId: "parent",
  457. toolCallId: "tool-1",
  458. markdown: "# Plan",
  459. },
  460. }),
  461. );
  462. expect(html).toContain("lucide-notepad-text");
  463. // 防止回退到与 Todo 撞图的 ListChecksIcon
  464. expect(html).not.toContain("lucide-list-checks");
  465. });
  466. });
  467. // 同一条约定:workflow-run tab 由 CreateWorkflow 工具卡打开,来源卡片用 lucide Workflow,
  468. // tab 必须与它一致,否则点击卡片进详情时图标会跳变。
  469. describe("SidePaneTabIcon workflow-run", () => {
  470. it("uses the same Workflow icon as the CreateWorkflow source card", () => {
  471. const html = renderToStaticMarkup(
  472. createElement(SidePaneTabIcon, { tab: workflowRunTab }),
  473. );
  474. expect(html).toContain("lucide-workflow");
  475. });
  476. });
  477. // actor transcript 是「一个 actor 的对话记录」,所以它既不能用 run 的 Workflow 图标
  478. // (那是整次运行),也不能用 subagent 的 Bot 图标——tab 条上分不清这两类会话,而它们的
  479. // 可见性与回收语义完全不同。
  480. describe("SidePaneTabIcon workflow-actor-session", () => {
  481. it("is visually distinct from both the run tab and a subagent tab", () => {
  482. const html = renderToStaticMarkup(
  483. createElement(SidePaneTabIcon, { tab: workflowActorTab }),
  484. );
  485. expect(html).toContain("lucide-bot-message-square");
  486. expect(html).not.toContain("lucide-workflow");
  487. });
  488. });
  489. // 脚本 transcript 是「这个 run 对工作区做过什么」,所以用终端图标:既不是 run 的 Workflow
  490. // (整次运行),也不是 actor 的对话气泡(那是一个子代理的 transcript)。
  491. describe("SidePaneTabIcon workflow-workspace", () => {
  492. it("uses the terminal icon, distinct from run and actor tabs", () => {
  493. const html = renderToStaticMarkup(
  494. createElement(SidePaneTabIcon, { tab: workflowWorkspaceTab }),
  495. );
  496. expect(html).toContain("lucide-terminal");
  497. expect(html).not.toContain("lucide-workflow");
  498. expect(html).not.toContain("lucide-bot-message-square");
  499. });
  500. });