workspaceSidePane.test.ts 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. activateBrowserSidePane,
  4. activateDeveloperToolsSidePane,
  5. applyBrowserUseSidePaneEvent,
  6. applyBrowserUseSidePaneVisibilityEvent,
  7. BROWSER_USE_OPERATION_INDICATOR_DURATION_MS,
  8. closeAllSidePaneTabs,
  9. closeCodeViewerSidePane,
  10. closeOtherSidePaneTabs,
  11. closeSidePaneTab,
  12. getActiveSelectionSideChatTab,
  13. getActiveSidePaneTab,
  14. getVisibleSidePaneTabs,
  15. isSidePaneTabVisibleForParent,
  16. isWorkspaceGlobalSidePaneTab,
  17. markBrowserUseSidePaneTabOperation,
  18. normalizeWorkspaceSidePaneState,
  19. openSelectionSideChatPane,
  20. openPlanDetailSidePane,
  21. openSubagentDirectorySidePane,
  22. openSubagentSessionSidePane,
  23. openWorkflowRunSidePane,
  24. openWorkflowActorSessionSidePane,
  25. openWorkflowArtifactSidePane,
  26. openWorkflowWorkspaceSidePane,
  27. openBrowserSidePane,
  28. openBrowserPermissionsSidePane,
  29. openBrowserUseSidePane,
  30. openOrActivateBrowserSidePaneByUrl,
  31. findBrowserSidePaneTabByUrl,
  32. openCodeViewerSidePane,
  33. openCodeViewerSidePanes,
  34. openTerminalSidePane,
  35. reorderSidePaneTab,
  36. resolveActiveTabForOwner,
  37. resolveSidePaneScopeState,
  38. restoreSidePaneTab,
  39. selectSidePaneTabsForParent,
  40. syncSubagentSessionSidePaneTabs,
  41. shouldMountSidePaneContent,
  42. applyBrowserTabResidencyEvent,
  43. restoreBrowserTabShells,
  44. stampSidePaneTabsOwnership,
  45. toggleBrowserSidePane,
  46. toggleGitSidePane,
  47. updateBrowserSidePaneTab,
  48. type WorkspaceSidePaneTab,
  49. } from "../src/lib/workspaceSidePane.js";
  50. describe("workspaceSidePane", () => {
  51. const viewerSource = {
  52. type: "file" as const,
  53. title: "demo.ts",
  54. path: "/workspace/demo.ts",
  55. };
  56. function withoutOpenedAt<T>(value: T): T {
  57. if (Array.isArray(value)) {
  58. return value.map((item) => withoutOpenedAt(item)) as T;
  59. }
  60. if (value && typeof value === "object") {
  61. return Object.fromEntries(
  62. Object.entries(value)
  63. .filter(([key]) => key !== "openedAt")
  64. .map(([key, item]) => [key, withoutOpenedAt(item)]),
  65. ) as T;
  66. }
  67. return value;
  68. }
  69. it("折叠或切换对话时仍为 Browser/browser-use 后台挂载内容", () => {
  70. expect(shouldMountSidePaneContent(false, [])).toBe(false);
  71. expect(shouldMountSidePaneContent(false, [{ id: "browser:human", type: "browser" }])).toBe(
  72. true,
  73. );
  74. expect(
  75. shouldMountSidePaneContent(false, [
  76. {
  77. id: "browser-use:tab-1",
  78. type: "browser-use",
  79. sessionId: "sess-1",
  80. tabId: "tab-1",
  81. },
  82. ]),
  83. ).toBe(true);
  84. expect(shouldMountSidePaneContent(true, [])).toBe(true);
  85. });
  86. it("BTL06: suspend 只卸载 guest,保留 logical tab shell;restore 保持 stable tabId", () => {
  87. const opened = stampSidePaneTabsOwnership(
  88. openBrowserSidePane(null, { tabId: "browser:stable" }),
  89. {
  90. ownerTaskId: "session-a",
  91. workspaceKey: "workspace-a",
  92. },
  93. );
  94. const suspended = applyBrowserTabResidencyEvent(opened, {
  95. tabId: "browser:stable",
  96. generation: 3,
  97. residency: "suspended",
  98. });
  99. expect(suspended).toMatchObject({
  100. activeTabId: "browser:stable",
  101. tabs: [
  102. {
  103. id: "browser:stable",
  104. residency: "suspended",
  105. residencyGeneration: 3,
  106. },
  107. ],
  108. });
  109. expect(
  110. applyBrowserTabResidencyEvent(suspended, {
  111. tabId: "browser:stable",
  112. generation: 4,
  113. residency: "restoring",
  114. }),
  115. ).toMatchObject({
  116. tabs: [
  117. {
  118. id: "browser:stable",
  119. residency: "restoring",
  120. residencyGeneration: 4,
  121. },
  122. ],
  123. });
  124. });
  125. it("拒绝迟到 generation,跨重启 shell 默认以 suspended 恢复且不覆盖其它 scope", () => {
  126. const current = applyBrowserTabResidencyEvent(
  127. stampSidePaneTabsOwnership(openBrowserSidePane(null, { tabId: "browser:stable" }), {
  128. ownerTaskId: "session-a",
  129. workspaceKey: "workspace-a",
  130. }),
  131. {
  132. tabId: "browser:stable",
  133. generation: 7,
  134. residency: "restoring",
  135. },
  136. );
  137. expect(
  138. applyBrowserTabResidencyEvent(current, {
  139. tabId: "browser:stable",
  140. generation: 6,
  141. residency: "suspended",
  142. }),
  143. ).toBe(current);
  144. const restored = restoreBrowserTabShells(current, [
  145. {
  146. tabId: "browser:restored",
  147. workspaceKey: "workspace-a",
  148. sessionId: "session-a",
  149. browserId: "browser-restored-real",
  150. browserGeneration: 9,
  151. origin: "user",
  152. restoreUrl: "https://example.com/restored",
  153. title: "Restored",
  154. faviconUrl: null,
  155. openedAt: 2,
  156. lastSelectedAt: null,
  157. },
  158. ]);
  159. expect(restored.tabs).toEqual(
  160. expect.arrayContaining([
  161. expect.objectContaining({
  162. id: "browser:restored",
  163. ownerTaskId: "session-a",
  164. residency: "suspended",
  165. initialUrl: "https://example.com/restored",
  166. }),
  167. ]),
  168. );
  169. });
  170. it("BTL17/18: restore terminal live 收敛状态,并保留冷恢复的真实 browser identity", () => {
  171. const restored = restoreBrowserTabShells(null, [
  172. {
  173. tabId: "browser:agent-restored",
  174. workspaceKey: "workspace-a",
  175. sessionId: "session-a",
  176. browserId: "browser-real",
  177. browserGeneration: 7,
  178. origin: "agent",
  179. restoreUrl: "https://example.com/restored",
  180. title: "Restored",
  181. faviconUrl: null,
  182. openedAt: 2,
  183. lastSelectedAt: 10,
  184. },
  185. ]);
  186. const restoring = applyBrowserTabResidencyEvent(restored, {
  187. tabId: "browser:agent-restored",
  188. workspaceKey: "workspace-a",
  189. sessionId: "session-a",
  190. browserId: "browser-real",
  191. browserGeneration: 7,
  192. generation: 3,
  193. residency: "restoring",
  194. });
  195. const live = applyBrowserTabResidencyEvent(restoring, {
  196. tabId: "browser:agent-restored",
  197. workspaceKey: "workspace-a",
  198. sessionId: "session-a",
  199. browserId: "browser-real",
  200. browserGeneration: 7,
  201. generation: 3,
  202. residency: "live-background",
  203. });
  204. expect(live?.tabs[0]).toMatchObject({
  205. browserId: "browser-real",
  206. browserGeneration: 7,
  207. residency: "live-background",
  208. residencyGeneration: 3,
  209. });
  210. });
  211. it("BTL22: 冷恢复保持 openedAt 顺序,但激活当前 scope 最后选中的 tab", () => {
  212. const restored = restoreBrowserTabShells(
  213. null,
  214. [
  215. {
  216. tabId: "browser:first",
  217. workspaceKey: "workspace-a",
  218. sessionId: "session-a",
  219. browserId: "unclaimed-iab",
  220. browserGeneration: 0,
  221. origin: "user",
  222. restoreUrl: "https://example.com/first",
  223. title: "First",
  224. faviconUrl: null,
  225. openedAt: 1,
  226. lastSelectedAt: 100,
  227. },
  228. {
  229. tabId: "browser:last-selected",
  230. workspaceKey: "workspace-a",
  231. sessionId: "session-a",
  232. browserId: "unclaimed-iab",
  233. browserGeneration: 0,
  234. origin: "user",
  235. restoreUrl: "https://example.com/last",
  236. title: "Last",
  237. faviconUrl: null,
  238. openedAt: 2,
  239. lastSelectedAt: 500,
  240. },
  241. ],
  242. {
  243. workspaceKey: "workspace-a",
  244. sessionId: "session-a",
  245. },
  246. );
  247. expect(restored.tabs.map((tab) => tab.id)).toEqual(["browser:first", "browser:last-selected"]);
  248. expect(restored.activeTabId).toBe("browser:last-selected");
  249. });
  250. it("BTL22: 草稿态 unscoped 持久值恢复为 null owner,保持当前草稿可见", () => {
  251. const restored = restoreBrowserTabShells(
  252. null,
  253. [
  254. {
  255. tabId: "browser:draft",
  256. workspaceKey: "workspace-a",
  257. sessionId: "unscoped",
  258. browserId: "unclaimed-iab",
  259. browserGeneration: 0,
  260. origin: "user",
  261. restoreUrl: "https://example.com/draft",
  262. title: "Draft",
  263. faviconUrl: null,
  264. openedAt: 1,
  265. lastSelectedAt: 100,
  266. },
  267. ],
  268. {
  269. workspaceKey: "workspace-a",
  270. sessionId: "unscoped",
  271. },
  272. );
  273. expect(restored.tabs[0]).toMatchObject({ ownerTaskId: null });
  274. expect(
  275. getVisibleSidePaneTabs(restored.tabs, {
  276. workspaceKey: "workspace-a",
  277. ownerTaskId: null,
  278. }),
  279. ).toHaveLength(1);
  280. });
  281. it("opens browser pane from closed state", () => {
  282. const next = toggleBrowserSidePane(null);
  283. expect(next).not.toBeNull();
  284. if (!next) {
  285. throw new Error("Expected browser side pane state");
  286. }
  287. expect(next.activeTabId).toMatch(/^browser:/);
  288. expect(withoutOpenedAt(next.tabs)).toEqual([
  289. {
  290. id: next.activeTabId,
  291. type: "browser",
  292. faviconUrl: null,
  293. initialUrl: null,
  294. title: null,
  295. },
  296. ]);
  297. });
  298. it("filters hidden treemapping tabs and keeps a visible active tab", () => {
  299. expect(
  300. withoutOpenedAt(
  301. normalizeWorkspaceSidePaneState({
  302. activeTabId: "treemapping",
  303. tabs: [
  304. { id: "git", type: "git" },
  305. { id: "treemapping", type: "treemapping" },
  306. ],
  307. }),
  308. ),
  309. ).toEqual({
  310. activeTabId: "git",
  311. tabs: [{ id: "git", type: "git" }],
  312. });
  313. });
  314. it("backfills missing selection side chat ordinals without disturbing existing ones", () => {
  315. // 多开引入 ordinal 之前的旧内存/HMR 状态没有该字段;normalize 边界必须按 parent
  316. // 分组回填最小可用编号,且不改动已有编号(review SG-01)。
  317. // 刻意省略 ordinal 模拟旧结构,用 unknown 中转绕过新类型必填约束。
  318. const legacyTab = {
  319. id: "selection-side-chat:ws:parent-a:child-legacy",
  320. type: "selection-side-chat",
  321. workspaceKey: "ws",
  322. workspacePath: "/ws",
  323. parentSessionId: "parent-a",
  324. childSessionId: "child-legacy",
  325. } as unknown as WorkspaceSidePaneTab & { type: "selection-side-chat" };
  326. const modernTab = {
  327. ...legacyTab,
  328. id: "selection-side-chat:ws:parent-a:child-modern",
  329. childSessionId: "child-modern",
  330. ordinal: 1,
  331. };
  332. const otherParentLegacyTab = {
  333. ...legacyTab,
  334. id: "selection-side-chat:ws:parent-b:child-other",
  335. parentSessionId: "parent-b",
  336. childSessionId: "child-other",
  337. };
  338. const normalized = normalizeWorkspaceSidePaneState({
  339. activeTabId: modernTab.id,
  340. tabs: [legacyTab, modernTab, otherParentLegacyTab],
  341. });
  342. const ordinals = new Map(
  343. (normalized?.tabs ?? []).flatMap((tab) =>
  344. tab.type === "selection-side-chat" ? [[tab.childSessionId, tab.ordinal]] : [],
  345. ),
  346. );
  347. expect(ordinals.get("child-modern")).toBe(1);
  348. expect(ordinals.get("child-legacy")).toBe(2);
  349. expect(ordinals.get("child-other")).toBe(1);
  350. expect(normalized?.activeTabId).toBe(modernTab.id);
  351. });
  352. it("preserves an intentionally empty active tab while only hidden parent tabs remain", () => {
  353. const state = openSubagentSessionSidePane(null, {
  354. workspaceKey: "/workspace",
  355. workspacePath: "/workspace",
  356. rootSessionId: "parent-a",
  357. parentSessionId: "parent-a",
  358. childSessionId: "child-a",
  359. subagentType: "Explore",
  360. title: "Explore project structure",
  361. });
  362. expect(normalizeWorkspaceSidePaneState({ ...state, activeTabId: "" })?.activeTabId).toBe("");
  363. });
  364. it("groups child detail tabs by root session and reuses a child across nesting levels", () => {
  365. const first = openSubagentSessionSidePane(null, {
  366. workspaceKey: "ssh://host/workspace",
  367. workspacePath: "/workspace",
  368. workspaceIdentity: "ssh://host/workspace",
  369. rootSessionId: "root-a",
  370. parentSessionId: "parent-a",
  371. childSessionId: "child-a",
  372. subagentType: "Explore",
  373. title: "Inspect files",
  374. });
  375. const second = openSubagentSessionSidePane(first, {
  376. workspaceKey: "ssh://host/workspace",
  377. workspacePath: "/workspace",
  378. workspaceIdentity: "ssh://host/workspace",
  379. rootSessionId: "root-a",
  380. parentSessionId: "nested-parent",
  381. childSessionId: "child-a",
  382. subagentType: "Explore",
  383. title: "Inspect files again",
  384. });
  385. expect(second.tabs).toHaveLength(1);
  386. expect(second.tabs[0]).toMatchObject({
  387. id: "subagent-session:ssh%3A%2F%2Fhost%2Fworkspace:root-a:child-a",
  388. rootSessionId: "root-a",
  389. parentSessionId: "nested-parent",
  390. childSessionId: "child-a",
  391. title: "Inspect files again",
  392. });
  393. });
  394. it("opens one reusable subagent directory per workspace and root parent", () => {
  395. const first = openSubagentDirectorySidePane(null, {
  396. workspaceKey: "/workspace",
  397. workspacePath: "/workspace",
  398. rootSessionId: "root-a",
  399. parentSessionId: "parent-a",
  400. });
  401. const second = openSubagentDirectorySidePane(first, {
  402. workspaceKey: "/workspace",
  403. workspacePath: "/workspace",
  404. rootSessionId: "root-a",
  405. parentSessionId: "parent-a",
  406. });
  407. expect(second.tabs).toHaveLength(1);
  408. expect(second).toMatchObject({
  409. activeTabId: "subagent-directory:%2Fworkspace:root-a:parent-a",
  410. tabs: [
  411. {
  412. type: "subagent-directory",
  413. rootSessionId: "root-a",
  414. parentSessionId: "parent-a",
  415. },
  416. ],
  417. });
  418. });
  419. it("drops invalid child tabs without closing the directory and falls back to it", () => {
  420. const directory = openSubagentDirectorySidePane(null, {
  421. workspaceKey: "/workspace",
  422. workspacePath: "/workspace",
  423. rootSessionId: "root-a",
  424. parentSessionId: "parent-a",
  425. });
  426. const withValid = openSubagentSessionSidePane(directory, {
  427. workspaceKey: "/workspace",
  428. workspacePath: "/workspace",
  429. rootSessionId: "root-a",
  430. parentSessionId: "parent-a",
  431. childSessionId: "child-valid",
  432. subagentType: "Explore",
  433. title: "Valid",
  434. });
  435. const withInvalid = openSubagentSessionSidePane(withValid, {
  436. workspaceKey: "/workspace",
  437. workspacePath: "/workspace",
  438. rootSessionId: "root-a",
  439. parentSessionId: "parent-a",
  440. childSessionId: "child-old-branch",
  441. subagentType: "Explore",
  442. title: "Old branch",
  443. });
  444. const next = syncSubagentSessionSidePaneTabs(withInvalid, {
  445. rootSessionId: "root-a",
  446. parentSessionId: "parent-a",
  447. validChildSessionIds: ["child-valid"],
  448. });
  449. expect(next?.tabs.map((tab) => tab.id)).toEqual([
  450. "subagent-directory:%2Fworkspace:root-a:parent-a",
  451. "subagent-session:%2Fworkspace:root-a:child-valid",
  452. ]);
  453. expect(next?.activeTabId).toBe("subagent-directory:%2Fworkspace:root-a:parent-a");
  454. });
  455. it("按 childSessionId 多开 selection side chat tab,并为同父实例分配序号", () => {
  456. const first = openSelectionSideChatPane(null, {
  457. workspaceKey: "ssh://host/workspace",
  458. workspacePath: "/workspace",
  459. workspaceIdentity: "ssh://host/workspace",
  460. parentSessionId: "parent-a",
  461. childSessionId: "child-a",
  462. });
  463. const second = openSelectionSideChatPane(first, {
  464. workspaceKey: "ssh://host/workspace",
  465. workspacePath: "/workspace",
  466. workspaceIdentity: "ssh://host/workspace",
  467. parentSessionId: "parent-a",
  468. childSessionId: "child-b",
  469. });
  470. const reopenedFirst = openSelectionSideChatPane(second, {
  471. workspaceKey: "ssh://host/workspace",
  472. workspacePath: "/workspace",
  473. workspaceIdentity: "ssh://host/workspace",
  474. parentSessionId: "parent-a",
  475. childSessionId: "child-a",
  476. });
  477. expect(second.tabs).toHaveLength(2);
  478. expect(second.tabs).toMatchObject([
  479. {
  480. id: "selection-side-chat:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:child-a",
  481. type: "selection-side-chat",
  482. parentSessionId: "parent-a",
  483. childSessionId: "child-a",
  484. ordinal: 1,
  485. },
  486. {
  487. id: "selection-side-chat:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:child-b",
  488. type: "selection-side-chat",
  489. parentSessionId: "parent-a",
  490. childSessionId: "child-b",
  491. ordinal: 2,
  492. },
  493. ]);
  494. expect(reopenedFirst.tabs).toHaveLength(2);
  495. expect(reopenedFirst.activeTabId).toBe(
  496. "selection-side-chat:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:child-a",
  497. );
  498. expect(reopenedFirst.tabs[0]).toMatchObject({
  499. childSessionId: "child-a",
  500. ordinal: 1,
  501. });
  502. });
  503. it("只把当前 active 且属于同 workspace + parent 的辅助 tab 解析为划词目标", () => {
  504. const first = openSelectionSideChatPane(null, {
  505. workspaceKey: "/workspace",
  506. workspacePath: "/workspace",
  507. parentSessionId: "parent-a",
  508. childSessionId: "child-a",
  509. });
  510. const second = openSelectionSideChatPane(first, {
  511. workspaceKey: "/workspace",
  512. workspacePath: "/workspace",
  513. parentSessionId: "parent-a",
  514. childSessionId: "child-b",
  515. });
  516. expect(
  517. getActiveSelectionSideChatTab(second, {
  518. workspaceKey: "/workspace",
  519. parentSessionId: "parent-a",
  520. }),
  521. ).toMatchObject({ childSessionId: "child-b" });
  522. const browserActive = openBrowserSidePane(second, {
  523. tabId: "browser:active",
  524. });
  525. expect(
  526. getActiveSelectionSideChatTab(browserActive, {
  527. workspaceKey: "/workspace",
  528. parentSessionId: "parent-a",
  529. }),
  530. ).toBeNull();
  531. expect(
  532. getActiveSelectionSideChatTab(second, {
  533. workspaceKey: "/other",
  534. parentSessionId: "parent-a",
  535. }),
  536. ).toBeNull();
  537. });
  538. it("按 workspace identity + parent + toolCall 复用并隔离计划详情 tab", () => {
  539. const first = openPlanDetailSidePane(null, {
  540. workspaceKey: "ssh://host/workspace",
  541. workspacePath: "/workspace",
  542. workspaceIdentity: "ssh://host/workspace",
  543. parentSessionId: "parent-a",
  544. toolCallId: "tool-plan-a",
  545. markdown: "# 计划 A",
  546. });
  547. const refreshed = openPlanDetailSidePane(first, {
  548. workspaceKey: "ssh://host/workspace",
  549. workspacePath: "/workspace",
  550. workspaceIdentity: "ssh://host/workspace",
  551. parentSessionId: "parent-a",
  552. toolCallId: "tool-plan-a",
  553. markdown: "# 计划 A\n\n已更新",
  554. });
  555. const second = openPlanDetailSidePane(refreshed, {
  556. workspaceKey: "ssh://host/workspace",
  557. workspacePath: "/workspace",
  558. workspaceIdentity: "ssh://host/workspace",
  559. parentSessionId: "parent-a",
  560. toolCallId: "tool-plan-b",
  561. markdown: "# 计划 B",
  562. });
  563. expect(second.tabs).toHaveLength(2);
  564. expect(second.tabs[0]).toMatchObject({
  565. type: "plan-detail",
  566. toolCallId: "tool-plan-a",
  567. markdown: "# 计划 A\n\n已更新",
  568. });
  569. expect(second.tabs[1]).toMatchObject({
  570. type: "plan-detail",
  571. toolCallId: "tool-plan-b",
  572. });
  573. expect(getVisibleSidePaneTabs(second, "parent-a")).toHaveLength(2);
  574. expect(getVisibleSidePaneTabs(second, "parent-b")).toHaveLength(0);
  575. });
  576. it("run 详情 tab 的落点(追记「五枚药丸与一扇门」):随请求落、再开不带就删键、openedAt 刷新", () => {
  577. const base = {
  578. workspaceKey: "/workspace",
  579. workspacePath: "/workspace",
  580. parentSessionId: "parent-a",
  581. toolCallId: "tool-wf-1",
  582. runId: "dwfrun-a",
  583. };
  584. const first = openWorkflowRunSidePane(null, {
  585. ...base,
  586. workflowName: "Fan-out review",
  587. phaseId: "phase#a",
  588. });
  589. expect(first.tabs).toHaveLength(1);
  590. expect(first.tabs[0]).toMatchObject({
  591. type: "workflow-run",
  592. focusPhaseId: "phase#a",
  593. workflowName: "Fan-out review",
  594. });
  595. const openedAt = (first.tabs[0] as { openedAt?: number }).openedAt!;
  596. // 从另一站再点:同一个 tab,落点换成那一站,openedAt 刷新(同一站再点也据此再落)。
  597. const relanded = openWorkflowRunSidePane(first, { ...base, phaseId: "phase#b" });
  598. expect(relanded.tabs).toHaveLength(1);
  599. expect(relanded.tabs[0]).toMatchObject({
  600. focusPhaseId: "phase#b",
  601. workflowName: "Fan-out review",
  602. });
  603. expect((relanded.tabs[0] as { openedAt?: number }).openedAt).toBeGreaterThanOrEqual(openedAt);
  604. // 不带站的请求(表头的 ⤢):落点删掉,不是沿用上一次的。
  605. const unlanded = openWorkflowRunSidePane(relanded, base);
  606. expect(unlanded.tabs).toHaveLength(1);
  607. expect(unlanded.tabs[0]).not.toHaveProperty("focusPhaseId");
  608. });
  609. it("按 workspace identity + parent + run 复用并隔离 workflow run 详情 tab", () => {
  610. const first = openWorkflowRunSidePane(null, {
  611. workspaceKey: "ssh://host/workspace",
  612. workspacePath: "/workspace",
  613. workspaceIdentity: "ssh://host/workspace",
  614. remoteSessionId: "remote-1",
  615. parentSessionId: "parent-a",
  616. toolCallId: "tool-wf-a",
  617. runId: "dwfrun-a",
  618. workflowName: "三路评审",
  619. });
  620. // 同一个 run 再次点击必须落回同一个 tab(结构化 id 幂等),而不是并排开第二份。
  621. const reclicked = openWorkflowRunSidePane(first, {
  622. workspaceKey: "ssh://host/workspace",
  623. workspacePath: "/workspace",
  624. workspaceIdentity: "ssh://host/workspace",
  625. remoteSessionId: "remote-1",
  626. parentSessionId: "parent-a",
  627. toolCallId: "tool-wf-a",
  628. runId: "dwfrun-a",
  629. workflowName: "三路评审(改名)",
  630. });
  631. const secondRun = openWorkflowRunSidePane(reclicked, {
  632. workspaceKey: "ssh://host/workspace",
  633. workspacePath: "/workspace",
  634. workspaceIdentity: "ssh://host/workspace",
  635. remoteSessionId: "remote-1",
  636. parentSessionId: "parent-a",
  637. toolCallId: "tool-wf-b",
  638. runId: "dwfrun-b",
  639. });
  640. expect(reclicked.tabs).toHaveLength(1);
  641. expect(reclicked.activeTabId).toBe(
  642. "workflow-run:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:dwfrun-a",
  643. );
  644. // 再次点击用卡片当前展示名刷新兜底标题;权威运行态仍来自父会话投影。
  645. expect(reclicked.tabs[0]).toMatchObject({
  646. type: "workflow-run",
  647. runId: "dwfrun-a",
  648. toolCallId: "tool-wf-a",
  649. workflowName: "三路评审(改名)",
  650. remoteSessionId: "remote-1",
  651. });
  652. expect(secondRun.tabs).toHaveLength(2);
  653. expect(secondRun.tabs[1]).toMatchObject({
  654. id: "workflow-run:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:dwfrun-b",
  655. type: "workflow-run",
  656. runId: "dwfrun-b",
  657. });
  658. // 可见性按 owning conversation 收窄,照 plan-detail。
  659. expect(getVisibleSidePaneTabs(secondRun, "parent-a")).toHaveLength(2);
  660. expect(getVisibleSidePaneTabs(secondRun, "parent-b")).toHaveLength(0);
  661. });
  662. it("同一个 runId 在不同 workspace / 不同 parent 下互不复用", () => {
  663. const inWorkspaceA = openWorkflowRunSidePane(null, {
  664. workspaceKey: "/workspace-a",
  665. workspacePath: "/workspace-a",
  666. parentSessionId: "parent-a",
  667. toolCallId: "tool-wf",
  668. runId: "dwfrun-1",
  669. });
  670. const inWorkspaceB = openWorkflowRunSidePane(inWorkspaceA, {
  671. workspaceKey: "/workspace-b",
  672. workspacePath: "/workspace-b",
  673. parentSessionId: "parent-a",
  674. toolCallId: "tool-wf",
  675. runId: "dwfrun-1",
  676. });
  677. const inOtherParent = openWorkflowRunSidePane(inWorkspaceB, {
  678. workspaceKey: "/workspace-a",
  679. workspacePath: "/workspace-a",
  680. parentSessionId: "parent-b",
  681. toolCallId: "tool-wf",
  682. runId: "dwfrun-1",
  683. });
  684. expect(inOtherParent.tabs).toHaveLength(3);
  685. expect(new Set(inOtherParent.tabs.map((tab) => tab.id)).size).toBe(3);
  686. });
  687. it("workflow run tab 不是 workspace 全局 tab,且 8-run 淘汰不回收它", () => {
  688. const state = openWorkflowRunSidePane(null, {
  689. workspaceKey: "/workspace",
  690. workspacePath: "/workspace",
  691. parentSessionId: "parent-a",
  692. toolCallId: "tool-wf",
  693. runId: "dwfrun-1",
  694. });
  695. const tab = state.tabs[0]!;
  696. expect(isWorkspaceGlobalSidePaneTab(tab)).toBe(false);
  697. expect(isSidePaneTabVisibleForParent(tab, "parent-a")).toBe(true);
  698. expect(isSidePaneTabVisibleForParent(tab, "parent-b")).toBe(false);
  699. // GC 语义(照 plan-detail:无 GC)。事件日志读的是 journal,不是 workflowRuns 投影,
  700. // 所以被 8-run 上限淘汰的 run 仍有完整可读的事件日志——那恰恰是用户会留着这个 tab
  701. // 的场景。子会话那套 sync 回收只作用于 subagent-session,绝不能连带清掉它。
  702. expect(
  703. syncSubagentSessionSidePaneTabs(state, {
  704. rootSessionId: "parent-a",
  705. parentSessionId: "parent-a",
  706. validChildSessionIds: [],
  707. }),
  708. ).toBe(state);
  709. });
  710. it("切回对话时优先激活 workflow run tab(与 plan-detail 同档)", () => {
  711. const withBrowser = openBrowserSidePane(null, { tabId: "browser:1" });
  712. const withRun = openWorkflowRunSidePane(withBrowser, {
  713. workspaceKey: "/workspace",
  714. workspacePath: "/workspace",
  715. parentSessionId: "parent-a",
  716. toolCallId: "tool-wf",
  717. runId: "dwfrun-1",
  718. });
  719. // 先把 active 挪到一个当前 parent 下不可见的位置,逼 select 走 findLast 分支。
  720. const selected = selectSidePaneTabsForParent(
  721. { ...withRun, activeTabId: "missing" },
  722. "parent-a",
  723. );
  724. expect(selected?.activeTabId).toBe("workflow-run:%2Fworkspace:parent-a:dwfrun-1");
  725. });
  726. // ── actor transcript tab(docs/dynamic-workflow/presentation.md「Subagent transcripts」)──
  727. it("按 workspace + parent + run + 槽位复用 actor transcript tab,重复点击幂等", () => {
  728. const first = openWorkflowActorSessionSidePane(null, {
  729. workspaceKey: "ssh://host/workspace",
  730. workspacePath: "/workspace",
  731. workspaceIdentity: "ssh://host/workspace",
  732. remoteSessionId: "remote-1",
  733. parentSessionId: "parent-a",
  734. runId: "dwfrun-a",
  735. actorSessionId: "dwf-dwfrun-a-actor_1@1",
  736. siteId: "actor#1",
  737. ordinal: 1,
  738. actorName: "reviewer",
  739. });
  740. const reclicked = openWorkflowActorSessionSidePane(first, {
  741. workspaceKey: "ssh://host/workspace",
  742. workspacePath: "/workspace",
  743. workspaceIdentity: "ssh://host/workspace",
  744. remoteSessionId: "remote-1",
  745. parentSessionId: "parent-a",
  746. runId: "dwfrun-a",
  747. actorSessionId: "dwf-dwfrun-a-actor_1@1",
  748. siteId: "actor#1",
  749. ordinal: 1,
  750. actorName: "reviewer",
  751. });
  752. // 同一车道的第二个实例是**另一个** tab:实例身份是 (runId, siteId, ordinal),不是车道。
  753. const sibling = openWorkflowActorSessionSidePane(reclicked, {
  754. workspaceKey: "ssh://host/workspace",
  755. workspacePath: "/workspace",
  756. workspaceIdentity: "ssh://host/workspace",
  757. remoteSessionId: "remote-1",
  758. parentSessionId: "parent-a",
  759. runId: "dwfrun-a",
  760. actorSessionId: "dwf-dwfrun-a-actor_1@2",
  761. siteId: "actor#1",
  762. ordinal: 2,
  763. actorName: "reviewer",
  764. });
  765. expect(reclicked.tabs).toHaveLength(1);
  766. expect(reclicked.activeTabId).toBe(
  767. "workflow-actor-session:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:dwfrun-a:actor%231%401",
  768. );
  769. expect(reclicked.tabs[0]).toMatchObject({
  770. type: "workflow-actor-session",
  771. parentSessionId: "parent-a",
  772. runId: "dwfrun-a",
  773. actorSessionId: "dwf-dwfrun-a-actor_1@1",
  774. siteId: "actor#1",
  775. ordinal: 1,
  776. actorName: "reviewer",
  777. remoteSessionId: "remote-1",
  778. });
  779. expect(sibling.tabs).toHaveLength(2);
  780. expect(sibling.activeTabId).toContain("actor%231%402");
  781. });
  782. it("槽位 tab:先从未启动的药丸开(无会话 id),后从已启动的药丸再开是同一个 tab 并补上会话 id", () => {
  783. // docs/dynamic-workflow/presentation.md「The pill」。
  784. const base = {
  785. workspaceKey: "/workspace",
  786. workspacePath: "/workspace",
  787. parentSessionId: "parent-a",
  788. runId: "dwfrun-a",
  789. siteId: "actor#1",
  790. ordinal: 1,
  791. actorName: "reviewer",
  792. };
  793. const slot = openWorkflowActorSessionSidePane(null, base);
  794. expect(slot.tabs).toHaveLength(1);
  795. expect(slot.tabs[0]).not.toHaveProperty("actorSessionId");
  796. expect(slot.tabs[0]).toMatchObject({ runId: "dwfrun-a", siteId: "actor#1", ordinal: 1 });
  797. const started = openWorkflowActorSessionSidePane(slot, {
  798. ...base,
  799. actorSessionId: "dwf-dwfrun-a-actor_1@1",
  800. });
  801. expect(started.tabs).toHaveLength(1);
  802. expect(started.tabs[0]!.id).toBe(slot.tabs[0]!.id);
  803. expect(started.tabs[0]).toMatchObject({ actorSessionId: "dwf-dwfrun-a-actor_1@1" });
  804. // 再来一次不带会话的请求也不会把已知的会话 id 抹掉。
  805. const again = openWorkflowActorSessionSidePane(started, base);
  806. expect(again.tabs).toHaveLength(1);
  807. expect(again.tabs[0]).toMatchObject({ actorSessionId: "dwf-dwfrun-a-actor_1@1" });
  808. // 另一条 run 里的同一槽位是另一个 tab。
  809. const otherRun = openWorkflowActorSessionSidePane(again, { ...base, runId: "dwfrun-b" });
  810. expect(otherRun.tabs).toHaveLength(2);
  811. });
  812. it("actor transcript tab 按所属对话收窄可见性,且不是 workspace 全局 tab", () => {
  813. const state = openWorkflowActorSessionSidePane(null, {
  814. workspaceKey: "/workspace",
  815. workspacePath: "/workspace",
  816. parentSessionId: "parent-a",
  817. runId: "dwfrun-1",
  818. actorSessionId: "dwf-dwfrun-1-actor_1@1",
  819. siteId: "actor#1",
  820. ordinal: 1,
  821. });
  822. const tab = state.tabs[0]!;
  823. expect(isWorkspaceGlobalSidePaneTab(tab)).toBe(false);
  824. expect(isSidePaneTabVisibleForParent(tab, "parent-a")).toBe(true);
  825. expect(isSidePaneTabVisibleForParent(tab, "parent-b")).toBe(false);
  826. expect(getVisibleSidePaneTabs(state, "parent-a")).toHaveLength(1);
  827. expect(getVisibleSidePaneTabs(state, "parent-b")).toHaveLength(0);
  828. expect(
  829. resolveActiveTabForOwner(state, { ownerTaskId: "parent-a", workspaceKey: "/workspace" }),
  830. ).toBe(tab.id);
  831. expect(
  832. resolveActiveTabForOwner(state, { ownerTaskId: "parent-b", workspaceKey: "/workspace" }),
  833. ).toBeNull();
  834. });
  835. it("子会话那套回收绝不吃 actor transcript tab", () => {
  836. // 这是这个 tab 用**独立类型**而不是给 subagent-session 加个变体标记的理由:
  837. // syncSubagentSessionSidePaneTabs 会删掉 childSessionId 不在有效集里的 subagent tab,
  838. // 而 actor 会话永远不在那个集合里(它不是 subagent 的子会话)——复用类型等于每次
  839. // 子会话投影一更新就把这个 tab 关掉。
  840. const state = openWorkflowActorSessionSidePane(null, {
  841. workspaceKey: "/workspace",
  842. workspacePath: "/workspace",
  843. parentSessionId: "parent-a",
  844. runId: "dwfrun-1",
  845. actorSessionId: "dwf-dwfrun-1-actor_1@1",
  846. siteId: "actor#1",
  847. ordinal: 1,
  848. });
  849. expect(
  850. syncSubagentSessionSidePaneTabs(state, {
  851. rootSessionId: "parent-a",
  852. parentSessionId: "parent-a",
  853. validChildSessionIds: [],
  854. }),
  855. ).toBe(state);
  856. // 连 actor 会话 id 被当成「有效子会话」误传进来也不改变结论:类型不匹配,扫不到。
  857. expect(
  858. syncSubagentSessionSidePaneTabs(state, {
  859. rootSessionId: "parent-a",
  860. parentSessionId: "parent-a",
  861. validChildSessionIds: ["dwf-dwfrun-1-actor_1@1"],
  862. }),
  863. ).toBe(state);
  864. });
  865. it("切回对话时 actor transcript tab 与 workflow run 同档参与优先激活", () => {
  866. const withBrowser = openBrowserSidePane(null, { tabId: "browser:1" });
  867. const withActor = openWorkflowActorSessionSidePane(withBrowser, {
  868. workspaceKey: "/workspace",
  869. workspacePath: "/workspace",
  870. parentSessionId: "parent-a",
  871. runId: "dwfrun-1",
  872. actorSessionId: "dwf-dwfrun-1-actor_1@1",
  873. siteId: "actor#1",
  874. ordinal: 1,
  875. });
  876. const selected = selectSidePaneTabsForParent(
  877. { ...withActor, activeTabId: "missing" },
  878. "parent-a",
  879. );
  880. expect(selected?.activeTabId).toContain("workflow-actor-session:");
  881. });
  882. // ── 产物 tab(docs/dynamic-workflow/authoring.md「How the user sees them」)──
  883. // ⚠ 术语:这里的 artifact 是脚本经 `artifact.*` 交付给用户的产出,不是引擎内部那个
  884. // 「脚本顶层返回值」的同名词。
  885. it("身份是 (workspace, 父会话, run, 产物 id)——**不含版本**,同一产物再点只聚焦同一个 tab", () => {
  886. const first = openWorkflowArtifactSidePane(null, {
  887. workspaceKey: "/workspace",
  888. workspacePath: "/workspace",
  889. parentSessionId: "parent-a",
  890. runId: "dwfrun-1",
  891. artifactId: "book",
  892. version: 1,
  893. title: "审计报告",
  894. });
  895. // 同一个产物发了新版之后再点一次(chip 从不带版本号)。
  896. const reclicked = openWorkflowArtifactSidePane(first, {
  897. workspaceKey: "/workspace",
  898. workspacePath: "/workspace",
  899. parentSessionId: "parent-a",
  900. runId: "dwfrun-1",
  901. artifactId: "book",
  902. title: "审计报告",
  903. });
  904. expect(first.tabs).toHaveLength(1);
  905. expect(reclicked.tabs).toHaveLength(1);
  906. expect(reclicked.activeTabId).toBe("workflow-artifact:%2Fworkspace:parent-a:dwfrun-1:book");
  907. // **回到最新版**:合并时旧 tab 上停着的 version 必须被丢掉,否则再点一次仍停在 v1。
  908. expect(reclicked.tabs[0]).toMatchObject({
  909. type: "workflow-artifact",
  910. parentSessionId: "parent-a",
  911. runId: "dwfrun-1",
  912. artifactId: "book",
  913. title: "审计报告",
  914. });
  915. expect("version" in reclicked.tabs[0]!).toBe(false);
  916. // 另一个产物是另一个 tab。
  917. const sibling = openWorkflowArtifactSidePane(reclicked, {
  918. workspaceKey: "/workspace",
  919. workspacePath: "/workspace",
  920. parentSessionId: "parent-a",
  921. runId: "dwfrun-1",
  922. artifactId: "perf",
  923. });
  924. expect(sibling.tabs).toHaveLength(2);
  925. });
  926. it("显式带版本时那一版才是落点", () => {
  927. const first = openWorkflowArtifactSidePane(null, {
  928. workspaceKey: "/workspace",
  929. workspacePath: "/workspace",
  930. parentSessionId: "parent-a",
  931. runId: "dwfrun-1",
  932. artifactId: "book",
  933. });
  934. const pinned = openWorkflowArtifactSidePane(first, {
  935. workspaceKey: "/workspace",
  936. workspacePath: "/workspace",
  937. parentSessionId: "parent-a",
  938. runId: "dwfrun-1",
  939. artifactId: "book",
  940. version: 2,
  941. });
  942. expect(pinned.tabs).toHaveLength(1);
  943. expect(pinned.tabs[0]).toMatchObject({ version: 2 });
  944. });
  945. it("产物 tab 按所属对话收窄可见性,且不是 workspace 全局 tab", () => {
  946. const state = openWorkflowArtifactSidePane(null, {
  947. workspaceKey: "/workspace",
  948. workspacePath: "/workspace",
  949. parentSessionId: "parent-a",
  950. runId: "dwfrun-1",
  951. artifactId: "book",
  952. });
  953. const tab = state.tabs[0]!;
  954. expect(isWorkspaceGlobalSidePaneTab(tab)).toBe(false);
  955. expect(isSidePaneTabVisibleForParent(tab, "parent-a")).toBe(true);
  956. expect(isSidePaneTabVisibleForParent(tab, "parent-b")).toBe(false);
  957. expect(getVisibleSidePaneTabs(state, "parent-a")).toHaveLength(1);
  958. expect(getVisibleSidePaneTabs(state, "parent-b")).toHaveLength(0);
  959. });
  960. it("子会话那套回收绝不吃产物 tab(产物的字节发布即钉住,run 被淘汰也还看得见)", () => {
  961. const state = openWorkflowArtifactSidePane(null, {
  962. workspaceKey: "/workspace",
  963. workspacePath: "/workspace",
  964. parentSessionId: "parent-a",
  965. runId: "dwfrun-1",
  966. artifactId: "book",
  967. });
  968. expect(
  969. syncSubagentSessionSidePaneTabs(state, {
  970. rootSessionId: "parent-a",
  971. parentSessionId: "parent-a",
  972. validChildSessionIds: [],
  973. }),
  974. ).toBe(state);
  975. });
  976. it("切回对话时产物 tab 与 workflow run 同档参与优先激活", () => {
  977. const withBrowser = openBrowserSidePane(null, { tabId: "browser:1" });
  978. const withArtifact = openWorkflowArtifactSidePane(withBrowser, {
  979. workspaceKey: "/workspace",
  980. workspacePath: "/workspace",
  981. parentSessionId: "parent-a",
  982. runId: "dwfrun-1",
  983. artifactId: "book",
  984. });
  985. const selected = selectSidePaneTabsForParent(
  986. { ...withArtifact, activeTabId: "missing" },
  987. "parent-a",
  988. );
  989. expect(selected?.activeTabId).toContain("workflow-artifact:");
  990. });
  991. it("远程 workspace 的产物 tab 按 workspaceIdentity 建 key,并留住 remoteSessionId", () => {
  992. const state = openWorkflowArtifactSidePane(null, {
  993. workspaceKey: "ssh://host/workspace",
  994. workspacePath: "/workspace",
  995. workspaceIdentity: "ssh://host/workspace",
  996. remoteSessionId: "remote-1",
  997. parentSessionId: "parent-a",
  998. runId: "dwfrun-1",
  999. artifactId: "book",
  1000. });
  1001. expect(state.activeTabId).toBe(
  1002. "workflow-artifact:ssh%3A%2F%2Fhost%2Fworkspace:parent-a:dwfrun-1:book",
  1003. );
  1004. expect(state.tabs[0]).toMatchObject({
  1005. workspaceIdentity: "ssh://host/workspace",
  1006. remoteSessionId: "remote-1",
  1007. });
  1008. });
  1009. // ── 脚本 transcript tab(docs/dynamic-workflow/transcript-and-notifications.md「Opening the tab」)──
  1010. it("身份是 (workspace, 父会话, run)——一个 run 一份脚本 transcript;再点只聚焦并重新落点", () => {
  1011. const first = openWorkflowWorkspaceSidePane(null, {
  1012. workspaceKey: "/workspace",
  1013. workspacePath: "/workspace",
  1014. parentSessionId: "parent-a",
  1015. toolCallId: "tool-wf-1",
  1016. runId: "dwfrun-a",
  1017. workflowName: "Fan-out review",
  1018. phaseId: "phase#a",
  1019. });
  1020. expect(first.tabs).toHaveLength(1);
  1021. expect(first.activeTabId).toBe("workflow-workspace:%2Fworkspace:parent-a:dwfrun-a");
  1022. expect(first.tabs[0]).toMatchObject({
  1023. type: "workflow-workspace",
  1024. parentSessionId: "parent-a",
  1025. toolCallId: "tool-wf-1",
  1026. runId: "dwfrun-a",
  1027. workflowName: "Fan-out review",
  1028. focusPhaseId: "phase#a",
  1029. });
  1030. // 从另一站再点:同一个 tab,落点换成那一站。
  1031. const relanded = openWorkflowWorkspaceSidePane(first, {
  1032. workspaceKey: "/workspace",
  1033. workspacePath: "/workspace",
  1034. parentSessionId: "parent-a",
  1035. toolCallId: "tool-wf-1",
  1036. runId: "dwfrun-a",
  1037. phaseId: "phase#b",
  1038. });
  1039. expect(relanded.tabs).toHaveLength(1);
  1040. expect(relanded.tabs[0]).toMatchObject({
  1041. focusPhaseId: "phase#b",
  1042. workflowName: "Fan-out review",
  1043. });
  1044. // 不带站的请求(例如从 tab 总览重开):落点**删掉**,不是沿用上一次的。
  1045. const unlanded = openWorkflowWorkspaceSidePane(relanded, {
  1046. workspaceKey: "/workspace",
  1047. workspacePath: "/workspace",
  1048. parentSessionId: "parent-a",
  1049. toolCallId: "tool-wf-1",
  1050. runId: "dwfrun-a",
  1051. });
  1052. expect(unlanded.tabs).toHaveLength(1);
  1053. expect(unlanded.tabs[0]).not.toHaveProperty("focusPhaseId");
  1054. // 另一条 run 是另一个 tab。
  1055. const otherRun = openWorkflowWorkspaceSidePane(unlanded, {
  1056. workspaceKey: "/workspace",
  1057. workspacePath: "/workspace",
  1058. parentSessionId: "parent-a",
  1059. toolCallId: "tool-wf-2",
  1060. runId: "dwfrun-b",
  1061. });
  1062. expect(otherRun.tabs).toHaveLength(2);
  1063. });
  1064. it("脚本 transcript tab 按所属对话收窄可见性,且不是 workspace 全局 tab", () => {
  1065. const state = openWorkflowWorkspaceSidePane(null, {
  1066. workspaceKey: "/workspace",
  1067. workspacePath: "/workspace",
  1068. parentSessionId: "parent-a",
  1069. toolCallId: "tool-wf-1",
  1070. runId: "dwfrun-1",
  1071. });
  1072. const tab = state.tabs[0]!;
  1073. expect(isWorkspaceGlobalSidePaneTab(tab)).toBe(false);
  1074. expect(isSidePaneTabVisibleForParent(tab, "parent-a")).toBe(true);
  1075. expect(isSidePaneTabVisibleForParent(tab, "parent-b")).toBe(false);
  1076. expect(getVisibleSidePaneTabs(state, "parent-a")).toHaveLength(1);
  1077. expect(getVisibleSidePaneTabs(state, "parent-b")).toHaveLength(0);
  1078. });
  1079. it("子会话那套回收绝不吃脚本 transcript tab;切回对话时它与 workflow run 同档参与优先激活", () => {
  1080. const withBrowser = openBrowserSidePane(null, { tabId: "browser:1" });
  1081. const state = openWorkflowWorkspaceSidePane(withBrowser, {
  1082. workspaceKey: "/workspace",
  1083. workspacePath: "/workspace",
  1084. parentSessionId: "parent-a",
  1085. toolCallId: "tool-wf-1",
  1086. runId: "dwfrun-1",
  1087. });
  1088. expect(
  1089. syncSubagentSessionSidePaneTabs(state, {
  1090. rootSessionId: "parent-a",
  1091. parentSessionId: "parent-a",
  1092. validChildSessionIds: [],
  1093. }),
  1094. ).toBe(state);
  1095. const selected = selectSidePaneTabsForParent({ ...state, activeTabId: "missing" }, "parent-a");
  1096. expect(selected?.activeTabId).toContain("workflow-workspace:");
  1097. });
  1098. it("reuses the developer tools tab", () => {
  1099. const first = activateDeveloperToolsSidePane(null);
  1100. const second = activateDeveloperToolsSidePane(first);
  1101. expect(withoutOpenedAt(second)).toEqual({
  1102. activeTabId: "developer-tools",
  1103. tabs: [
  1104. {
  1105. id: "developer-tools",
  1106. type: "developer-tools",
  1107. },
  1108. ],
  1109. });
  1110. });
  1111. it("keeps browser and code viewer side by side and activates browser", () => {
  1112. const current = openCodeViewerSidePane(null, viewerSource);
  1113. const next = toggleBrowserSidePane(current);
  1114. expect(next).not.toBeNull();
  1115. if (!next) {
  1116. throw new Error("Expected browser side pane state");
  1117. }
  1118. expect(next.activeTabId).toMatch(/^browser:/);
  1119. expect(withoutOpenedAt(next)).toEqual({
  1120. activeTabId: next.activeTabId,
  1121. tabs: [
  1122. {
  1123. id: "code-viewer:file:/workspace/demo.ts",
  1124. type: "code-viewer",
  1125. source: viewerSource,
  1126. sourceKey: "file:/workspace/demo.ts",
  1127. },
  1128. {
  1129. id: next.activeTabId,
  1130. type: "browser",
  1131. faviconUrl: null,
  1132. initialUrl: null,
  1133. title: null,
  1134. },
  1135. ],
  1136. });
  1137. });
  1138. it("closes browser pane when toggled again and keeps other tabs", () => {
  1139. const current = activateBrowserSidePane(openCodeViewerSidePane(null, viewerSource), {
  1140. tabId: "browser:a",
  1141. });
  1142. expect(withoutOpenedAt(toggleBrowserSidePane(current))).toEqual({
  1143. activeTabId: "code-viewer:file:/workspace/demo.ts",
  1144. tabs: [
  1145. {
  1146. id: "code-viewer:file:/workspace/demo.ts",
  1147. type: "code-viewer",
  1148. source: viewerSource,
  1149. sourceKey: "file:/workspace/demo.ts",
  1150. },
  1151. ],
  1152. });
  1153. });
  1154. it("only closes code viewer pane for code viewer state", () => {
  1155. const viewerState = openCodeViewerSidePane(null, viewerSource);
  1156. expect(closeCodeViewerSidePane(viewerState)).toBeNull();
  1157. expect(
  1158. withoutOpenedAt(
  1159. closeCodeViewerSidePane({
  1160. activeTabId: "browser:a",
  1161. tabs: [{ id: "browser:a", type: "browser" }],
  1162. }),
  1163. ),
  1164. ).toEqual({
  1165. activeTabId: "browser:a",
  1166. tabs: [{ id: "browser:a", type: "browser" }],
  1167. });
  1168. });
  1169. it("can open multiple browser tabs", () => {
  1170. const first = openBrowserSidePane(null, {
  1171. tabId: "browser:a",
  1172. initialUrl: "https://example.com/a",
  1173. });
  1174. const second = openBrowserSidePane(first, {
  1175. tabId: "browser:b",
  1176. initialUrl: "https://example.com/b",
  1177. });
  1178. expect(withoutOpenedAt(second)).toEqual({
  1179. activeTabId: "browser:b",
  1180. tabs: [
  1181. {
  1182. id: "browser:a",
  1183. type: "browser",
  1184. faviconUrl: null,
  1185. initialUrl: "https://example.com/a",
  1186. title: null,
  1187. },
  1188. {
  1189. id: "browser:b",
  1190. type: "browser",
  1191. faviconUrl: null,
  1192. initialUrl: "https://example.com/b",
  1193. title: null,
  1194. },
  1195. ],
  1196. });
  1197. });
  1198. it("保留模型 popup 标记,供视图跳过 human viewport 偏好", () => {
  1199. const state = openBrowserSidePane(null, {
  1200. tabId: "browser:agent-popup",
  1201. initialUrl: "https://example.com/next",
  1202. agentOpened: true,
  1203. });
  1204. expect(state.tabs[0]).toMatchObject({
  1205. id: "browser:agent-popup",
  1206. type: "browser",
  1207. agentOpened: true,
  1208. });
  1209. });
  1210. it("can open multiple terminal tabs with workspace titles", () => {
  1211. const first = openTerminalSidePane(null, { title: "z-code" });
  1212. const second = openTerminalSidePane(first, { title: "z-code 2" });
  1213. expect(withoutOpenedAt(second)).toEqual({
  1214. activeTabId: second.activeTabId,
  1215. tabs: [
  1216. {
  1217. id: first.activeTabId,
  1218. type: "terminal",
  1219. title: "z-code",
  1220. },
  1221. {
  1222. id: second.activeTabId,
  1223. type: "terminal",
  1224. title: "z-code 2",
  1225. },
  1226. ],
  1227. });
  1228. expect(first.activeTabId).toMatch(/^terminal:/);
  1229. expect(second.activeTabId).toMatch(/^terminal:/);
  1230. expect(second.activeTabId).not.toBe(first.activeTabId);
  1231. });
  1232. it("updates metadata for the targeted browser tab", () => {
  1233. const current = openBrowserSidePane(openBrowserSidePane(null, { tabId: "browser:a" }), {
  1234. tabId: "browser:b",
  1235. });
  1236. expect(
  1237. withoutOpenedAt(
  1238. updateBrowserSidePaneTab(current, "browser:b", {
  1239. faviconUrl: "https://example.com/favicon.ico",
  1240. title: "Example",
  1241. }),
  1242. ),
  1243. ).toEqual({
  1244. activeTabId: "browser:b",
  1245. tabs: [
  1246. {
  1247. id: "browser:a",
  1248. type: "browser",
  1249. faviconUrl: null,
  1250. initialUrl: null,
  1251. title: null,
  1252. },
  1253. {
  1254. id: "browser:b",
  1255. type: "browser",
  1256. faviconUrl: "https://example.com/favicon.ico",
  1257. initialUrl: null,
  1258. title: "Example",
  1259. },
  1260. ],
  1261. });
  1262. });
  1263. it("activates an existing browser tab when toggled from another tab", () => {
  1264. const current = openCodeViewerSidePane(
  1265. openBrowserSidePane(null, { tabId: "browser:a" }),
  1266. viewerSource,
  1267. );
  1268. expect(withoutOpenedAt(toggleBrowserSidePane(current))).toEqual({
  1269. activeTabId: "browser:a",
  1270. tabs: [
  1271. {
  1272. id: "browser:a",
  1273. type: "browser",
  1274. faviconUrl: null,
  1275. initialUrl: null,
  1276. title: null,
  1277. },
  1278. {
  1279. id: "code-viewer:file:/workspace/demo.ts",
  1280. type: "code-viewer",
  1281. source: viewerSource,
  1282. sourceKey: "file:/workspace/demo.ts",
  1283. },
  1284. ],
  1285. });
  1286. });
  1287. it("reuses the same code viewer tab for the same file path", () => {
  1288. const first = openCodeViewerSidePane(null, viewerSource);
  1289. const second = openCodeViewerSidePane(first, {
  1290. ...viewerSource,
  1291. offset: 128,
  1292. });
  1293. expect(second.tabs).toHaveLength(1);
  1294. expect(getActiveSidePaneTab(second)).toMatchObject({
  1295. id: "code-viewer:file:/workspace/demo.ts",
  1296. type: "code-viewer",
  1297. source: {
  1298. ...viewerSource,
  1299. offset: 128,
  1300. },
  1301. });
  1302. });
  1303. it("批量打开多份 PPTX,并激活卡片顺序中的第一份", () => {
  1304. const current = activateBrowserSidePane(null, { tabId: "browser:kept" });
  1305. const sources = [
  1306. {
  1307. type: "file" as const,
  1308. title: "first.pptx",
  1309. path: "/workspace/first.pptx",
  1310. workspacePath: "/workspace",
  1311. },
  1312. {
  1313. type: "file" as const,
  1314. title: "second.pptx",
  1315. path: "/workspace/second.pptx",
  1316. workspacePath: "/workspace",
  1317. },
  1318. ];
  1319. const next = openCodeViewerSidePanes(current, sources, "session-a");
  1320. expect(next.tabs).toHaveLength(3);
  1321. expect(next.tabs[0]).toMatchObject({ id: "browser:kept", type: "browser" });
  1322. expect(next.tabs.slice(1)).toMatchObject([
  1323. {
  1324. type: "code-viewer",
  1325. source: sources[0],
  1326. sourceKey: "/workspace:pptx:/workspace/first.pptx",
  1327. },
  1328. {
  1329. type: "code-viewer",
  1330. source: sources[1],
  1331. sourceKey: "/workspace:pptx:/workspace/second.pptx",
  1332. },
  1333. ]);
  1334. expect(next.activeTabId).toBe("code-viewer:/workspace:pptx:/workspace/first.pptx");
  1335. });
  1336. it("批量打开会按 workspace sourceKey 去重并复用现有 Tab", () => {
  1337. const source = {
  1338. type: "file" as const,
  1339. title: "deck.pptx",
  1340. path: "/workspace/deck.pptx",
  1341. workspacePath: "/workspace",
  1342. workspaceIdentity: "remote:ssh:host-a:/workspace",
  1343. };
  1344. const first = stampSidePaneTabsOwnership(openCodeViewerSidePane(null, source), {
  1345. ownerTaskId: "session-a",
  1346. workspaceKey: "remote:ssh:host-a:/workspace",
  1347. remoteSessionId: null,
  1348. });
  1349. const next = openCodeViewerSidePanes(
  1350. first,
  1351. [source, { ...source, title: "deck updated.pptx" }],
  1352. "session-a",
  1353. );
  1354. expect(next.tabs).toHaveLength(1);
  1355. expect(getActiveSidePaneTab(next)).toMatchObject({
  1356. type: "code-viewer",
  1357. ownerTaskId: "session-a",
  1358. source: source,
  1359. sourceKey: "remote:ssh:host-a:/workspace:pptx:/workspace/deck.pptx",
  1360. });
  1361. });
  1362. it("reuses the same code-review tab for one file while refreshing the model review", () => {
  1363. const source = {
  1364. type: "code-review" as const,
  1365. title: "demo.ts",
  1366. path: "/workspace/demo.ts",
  1367. workspacePath: "/workspace",
  1368. workspaceIdentity: "remote:ssh:host-a:/workspace",
  1369. review: {
  1370. requestId: "review-1",
  1371. title: "First review",
  1372. body: "First body",
  1373. startLine: 2,
  1374. endLine: 3,
  1375. },
  1376. };
  1377. const first = openCodeViewerSidePane(null, source);
  1378. const second = openCodeViewerSidePane(first, {
  1379. ...source,
  1380. review: {
  1381. ...source.review,
  1382. requestId: "review-2",
  1383. title: "Second review",
  1384. body: "Second body",
  1385. startLine: 8,
  1386. endLine: 8,
  1387. },
  1388. });
  1389. expect(second.tabs).toHaveLength(1);
  1390. expect(getActiveSidePaneTab(second)).toMatchObject({
  1391. id: "code-viewer:remote:ssh:host-a:/workspace:code-review:/workspace/demo.ts",
  1392. sourceKey: "remote:ssh:host-a:/workspace:code-review:/workspace/demo.ts",
  1393. source: {
  1394. type: "code-review",
  1395. review: {
  1396. requestId: "review-2",
  1397. title: "Second review",
  1398. body: "Second body",
  1399. startLine: 8,
  1400. endLine: 8,
  1401. },
  1402. },
  1403. });
  1404. });
  1405. it("keeps ordinary file and code-review tabs separate for the same path", () => {
  1406. const reviewSource = {
  1407. type: "code-review" as const,
  1408. title: "demo.ts",
  1409. path: "/workspace/demo.ts",
  1410. review: {
  1411. requestId: "review-1",
  1412. title: "Review",
  1413. body: "Body",
  1414. },
  1415. };
  1416. const first = openCodeViewerSidePane(null, viewerSource);
  1417. const second = openCodeViewerSidePane(first, reviewSource);
  1418. expect(second.tabs).toHaveLength(2);
  1419. expect(second.tabs.map((tab) => (tab.type === "code-viewer" ? tab.sourceKey : null))).toEqual([
  1420. "file:/workspace/demo.ts",
  1421. "code-review:/workspace/demo.ts",
  1422. ]);
  1423. });
  1424. it("reuses the same code viewer tab for the same pdf path", () => {
  1425. const pdfSource = {
  1426. type: "pdf" as const,
  1427. title: "report.pdf",
  1428. path: "/workspace/report.pdf",
  1429. };
  1430. const first = openCodeViewerSidePane(null, pdfSource);
  1431. const second = openCodeViewerSidePane(first, pdfSource);
  1432. expect(second.tabs).toHaveLength(1);
  1433. expect(getActiveSidePaneTab(second)).toMatchObject({
  1434. id: "code-viewer:pdf:/workspace/report.pdf",
  1435. type: "code-viewer",
  1436. source: pdfSource,
  1437. });
  1438. });
  1439. it("reuses the same PPTX tab while refreshing reference navigation intent", () => {
  1440. const source = {
  1441. type: "pptx" as const,
  1442. title: "deck.pptx",
  1443. path: "/workspace/deck.pptx",
  1444. referenceNavigation: {
  1445. requestId: "navigation-1",
  1446. pageIndex: 0,
  1447. expectedSourceFingerprint: `sha256:${"a".repeat(64)}`,
  1448. },
  1449. };
  1450. const first = openCodeViewerSidePane(null, source);
  1451. const second = openCodeViewerSidePane(first, {
  1452. ...source,
  1453. referenceNavigation: {
  1454. ...source.referenceNavigation,
  1455. requestId: "navigation-2",
  1456. pageIndex: 2,
  1457. },
  1458. });
  1459. expect(second.tabs).toHaveLength(1);
  1460. expect(getActiveSidePaneTab(second)).toMatchObject({
  1461. id: "code-viewer:pptx:/workspace/deck.pptx",
  1462. source: {
  1463. referenceNavigation: {
  1464. requestId: "navigation-2",
  1465. pageIndex: 2,
  1466. },
  1467. },
  1468. });
  1469. });
  1470. it("reuses the file-tree PPTX tab when a presentation reference opens the same file", () => {
  1471. const fileTreeSource = {
  1472. type: "file" as const,
  1473. title: "deck.pptx",
  1474. path: "/workspace/deck.pptx",
  1475. workspacePath: "/workspace",
  1476. };
  1477. const referenceSource = {
  1478. type: "pptx" as const,
  1479. title: "deck.pptx",
  1480. path: "/workspace/deck.pptx",
  1481. workspacePath: "/workspace",
  1482. referenceNavigation: {
  1483. requestId: "navigation-from-reference",
  1484. pageIndex: 2,
  1485. expectedSourceFingerprint: `sha256:${"a".repeat(64)}`,
  1486. },
  1487. };
  1488. const first = openCodeViewerSidePane(null, fileTreeSource);
  1489. const second = openCodeViewerSidePane(first, referenceSource);
  1490. expect(second.tabs).toHaveLength(1);
  1491. expect(getActiveSidePaneTab(second)).toMatchObject({
  1492. id: "code-viewer:/workspace:pptx:/workspace/deck.pptx",
  1493. sourceKey: "/workspace:pptx:/workspace/deck.pptx",
  1494. source: referenceSource,
  1495. });
  1496. });
  1497. it("reuses the reference-opened PPTX tab when the file tree opens the same file", () => {
  1498. const referenceSource = {
  1499. type: "pptx" as const,
  1500. title: "deck.pptx",
  1501. path: "/workspace/deck.pptx",
  1502. workspacePath: "/workspace",
  1503. referenceNavigation: {
  1504. requestId: "navigation-from-reference",
  1505. pageIndex: 2,
  1506. expectedSourceFingerprint: `sha256:${"a".repeat(64)}`,
  1507. },
  1508. };
  1509. const fileTreeSource = {
  1510. type: "file" as const,
  1511. title: "deck.pptx",
  1512. path: "/workspace/deck.pptx",
  1513. workspacePath: "/workspace",
  1514. };
  1515. const first = openCodeViewerSidePane(null, referenceSource);
  1516. const second = openCodeViewerSidePane(first, fileTreeSource);
  1517. expect(second.tabs).toHaveLength(1);
  1518. expect(getActiveSidePaneTab(second)).toMatchObject({
  1519. id: "code-viewer:/workspace:pptx:/workspace/deck.pptx",
  1520. sourceKey: "/workspace:pptx:/workspace/deck.pptx",
  1521. source: fileTreeSource,
  1522. });
  1523. });
  1524. it("keeps same-path file previews isolated by workspace identity", () => {
  1525. const first = openCodeViewerSidePane(null, {
  1526. ...viewerSource,
  1527. workspaceIdentity: "remote:ssh:host-a:/workspace",
  1528. });
  1529. const second = openCodeViewerSidePane(first, {
  1530. ...viewerSource,
  1531. workspaceIdentity: "remote:ssh:host-b:/workspace",
  1532. });
  1533. expect(second.tabs).toHaveLength(2);
  1534. expect(second.tabs.map((tab) => (tab.type === "code-viewer" ? tab.sourceKey : null))).toEqual([
  1535. "remote:ssh:host-a:/workspace:file:/workspace/demo.ts",
  1536. "remote:ssh:host-b:/workspace:file:/workspace/demo.ts",
  1537. ]);
  1538. });
  1539. it("normalizes uri-encoded code viewer paths before creating tab keys", () => {
  1540. const encodedSource = {
  1541. type: "file" as const,
  1542. title: "engine.ts",
  1543. path: "/Users/dev/ZCodeProject/323/Auto%20Snake/src/game/engine.ts",
  1544. };
  1545. const decodedPath = "/Users/dev/ZCodeProject/323/Auto Snake/src/game/engine.ts";
  1546. const first = openCodeViewerSidePane(null, encodedSource);
  1547. const second = openCodeViewerSidePane(first, {
  1548. ...encodedSource,
  1549. path: decodedPath,
  1550. });
  1551. expect(second.tabs).toHaveLength(1);
  1552. expect(getActiveSidePaneTab(second)).toMatchObject({
  1553. id: `code-viewer:file:${decodedPath}`,
  1554. type: "code-viewer",
  1555. source: {
  1556. ...encodedSource,
  1557. path: decodedPath,
  1558. },
  1559. sourceKey: `file:${decodedPath}`,
  1560. });
  1561. });
  1562. it("keeps different patch previews for the same file in separate tabs", () => {
  1563. const firstPatch = {
  1564. type: "patch" as const,
  1565. title: "demo.ts",
  1566. path: "/workspace/demo.ts",
  1567. patch: "--- a/demo.ts\n+++ b/demo.ts\n@@\n-old\n+new",
  1568. };
  1569. const secondPatch = {
  1570. ...firstPatch,
  1571. patch: "--- a/demo.ts\n+++ b/demo.ts\n@@\n-old\n+newer",
  1572. };
  1573. const first = openCodeViewerSidePane(null, firstPatch);
  1574. const second = openCodeViewerSidePane(first, secondPatch);
  1575. const reopenedSecond = openCodeViewerSidePane(second, secondPatch);
  1576. expect(second.tabs).toHaveLength(2);
  1577. expect(second.tabs.map((tab) => (tab.type === "code-viewer" ? tab.source : null))).toEqual([
  1578. firstPatch,
  1579. secondPatch,
  1580. ]);
  1581. expect(reopenedSecond.tabs).toHaveLength(2);
  1582. expect(reopenedSecond.activeTabId).toBe(second.activeTabId);
  1583. });
  1584. it("keeps different MultiFileDiff previews for the same file in separate tabs", () => {
  1585. const firstDiff = {
  1586. type: "multi-file-diff" as const,
  1587. title: "demo.ts",
  1588. path: "/workspace/demo.ts",
  1589. beforeContent: "const value = 1;\n",
  1590. afterContent: "const value = 2;\n",
  1591. };
  1592. const secondDiff = {
  1593. ...firstDiff,
  1594. afterContent: "const value = 3;\n",
  1595. };
  1596. const first = openCodeViewerSidePane(null, firstDiff);
  1597. const second = openCodeViewerSidePane(first, secondDiff);
  1598. const reopenedSecond = openCodeViewerSidePane(second, secondDiff);
  1599. expect(second.tabs).toHaveLength(2);
  1600. expect(second.tabs.map((tab) => (tab.type === "code-viewer" ? tab.source : null))).toEqual([
  1601. firstDiff,
  1602. secondDiff,
  1603. ]);
  1604. expect(reopenedSecond.tabs).toHaveLength(2);
  1605. expect(reopenedSecond.activeTabId).toBe(second.activeTabId);
  1606. });
  1607. it("activates git without dropping existing tabs", () => {
  1608. const current = activateBrowserSidePane(openCodeViewerSidePane(null, viewerSource), {
  1609. tabId: "browser:a",
  1610. });
  1611. expect(withoutOpenedAt(toggleGitSidePane(current))).toEqual({
  1612. activeTabId: "git",
  1613. tabs: [
  1614. {
  1615. id: "code-viewer:file:/workspace/demo.ts",
  1616. type: "code-viewer",
  1617. source: viewerSource,
  1618. sourceKey: "file:/workspace/demo.ts",
  1619. },
  1620. {
  1621. id: "browser:a",
  1622. type: "browser",
  1623. faviconUrl: null,
  1624. initialUrl: null,
  1625. title: null,
  1626. },
  1627. { id: "git", type: "git" },
  1628. ],
  1629. });
  1630. });
  1631. it("reorders tabs without changing active tab", () => {
  1632. const current = activateBrowserSidePane(openCodeViewerSidePane(null, viewerSource), {
  1633. tabId: "browser:a",
  1634. });
  1635. const withGit = toggleGitSidePane(current)!;
  1636. expect(
  1637. withoutOpenedAt(reorderSidePaneTab(withGit, "git", "code-viewer:file:/workspace/demo.ts")),
  1638. ).toEqual({
  1639. activeTabId: "git",
  1640. tabs: [
  1641. { id: "git", type: "git" },
  1642. {
  1643. id: "code-viewer:file:/workspace/demo.ts",
  1644. type: "code-viewer",
  1645. source: viewerSource,
  1646. sourceKey: "file:/workspace/demo.ts",
  1647. },
  1648. {
  1649. id: "browser:a",
  1650. type: "browser",
  1651. faviconUrl: null,
  1652. initialUrl: null,
  1653. title: null,
  1654. },
  1655. ],
  1656. });
  1657. });
  1658. it("keeps state unchanged when reordering unknown tabs", () => {
  1659. const current = activateBrowserSidePane(openCodeViewerSidePane(null, viewerSource), {
  1660. tabId: "browser:a",
  1661. });
  1662. expect(reorderSidePaneTab(current, "missing", "browser:a")).toBe(current);
  1663. expect(reorderSidePaneTab(current, "browser:a", "missing")).toBe(current);
  1664. });
  1665. it("closes the active tab and falls back to the neighbor on the right", () => {
  1666. const state = {
  1667. activeTabId: "browser:a",
  1668. tabs: [
  1669. {
  1670. id: "code-viewer:file:/workspace/demo.ts",
  1671. type: "code-viewer" as const,
  1672. source: viewerSource,
  1673. sourceKey: "file:/workspace/demo.ts",
  1674. },
  1675. { id: "browser:a", type: "browser" as const },
  1676. { id: "git", type: "git" as const },
  1677. ],
  1678. };
  1679. expect(withoutOpenedAt(closeSidePaneTab(state, "browser:a"))).toEqual({
  1680. activeTabId: "git",
  1681. tabs: [
  1682. {
  1683. id: "code-viewer:file:/workspace/demo.ts",
  1684. type: "code-viewer",
  1685. source: viewerSource,
  1686. sourceKey: "file:/workspace/demo.ts",
  1687. },
  1688. { id: "git", type: "git" },
  1689. ],
  1690. });
  1691. });
  1692. it("closes other tabs and activates the retained tab", () => {
  1693. const state = {
  1694. activeTabId: "browser:a",
  1695. tabs: [
  1696. {
  1697. id: "code-viewer:file:/workspace/demo.ts",
  1698. type: "code-viewer" as const,
  1699. source: viewerSource,
  1700. sourceKey: "file:/workspace/demo.ts",
  1701. },
  1702. { id: "browser:a", type: "browser" as const },
  1703. { id: "git", type: "git" as const },
  1704. ],
  1705. };
  1706. expect(withoutOpenedAt(closeOtherSidePaneTabs(state, "git"))).toEqual({
  1707. activeTabId: "git",
  1708. tabs: [{ id: "git", type: "git" }],
  1709. });
  1710. });
  1711. it("closes every side pane tab", () => {
  1712. expect(closeAllSidePaneTabs()).toBeNull();
  1713. });
  1714. it("restores a closed tab as the active tab", () => {
  1715. const state = restoreSidePaneTab(null, {
  1716. id: "browser:a",
  1717. type: "browser",
  1718. initialUrl: "https://example.com",
  1719. openedAt: 123,
  1720. });
  1721. expect(withoutOpenedAt(state)).toEqual({
  1722. activeTabId: "browser:a",
  1723. tabs: [
  1724. {
  1725. id: "browser:a",
  1726. type: "browser",
  1727. initialUrl: "https://example.com",
  1728. },
  1729. ],
  1730. });
  1731. });
  1732. describe("openBrowserUseSidePane", () => {
  1733. it("按 tabId 建 browser-use tab,并写入 origin session/workspace 归属", () => {
  1734. const state = openBrowserUseSidePane(null, {
  1735. workspaceKey: "/workspace",
  1736. sessionId: "sess-1",
  1737. tabId: "tab-1",
  1738. });
  1739. expect(state.activeTabId).toBe("browser-use:tab-1");
  1740. expect(state.tabs).toHaveLength(1);
  1741. const tab = state.tabs[0];
  1742. expect(tab.type).toBe("browser-use");
  1743. expect(tab).toMatchObject({
  1744. id: "browser-use:tab-1",
  1745. ownerTaskId: "sess-1",
  1746. workspaceKey: "/workspace",
  1747. sessionId: "sess-1",
  1748. tabId: "tab-1",
  1749. });
  1750. });
  1751. it("同一 tabId 重放 ready 时复用同一 tab(不重复开)", () => {
  1752. const first = openBrowserUseSidePane(null, {
  1753. workspaceKey: "/workspace",
  1754. sessionId: "sess-1",
  1755. tabId: "tab-1",
  1756. });
  1757. const second = openBrowserUseSidePane(first, {
  1758. workspaceKey: "/workspace",
  1759. sessionId: "sess-1",
  1760. tabId: "tab-1",
  1761. });
  1762. expect(second.tabs).toHaveLength(1);
  1763. expect(second.activeTabId).toBe("browser-use:tab-1");
  1764. });
  1765. it("不同 sessionId 各开一个 tab", () => {
  1766. const first = openBrowserUseSidePane(null, {
  1767. workspaceKey: "/workspace",
  1768. sessionId: "sess-1",
  1769. tabId: "tab-1",
  1770. });
  1771. const second = openBrowserUseSidePane(first, {
  1772. workspaceKey: "/workspace",
  1773. sessionId: "sess-2",
  1774. tabId: "tab-2",
  1775. });
  1776. expect(second.tabs).toHaveLength(2);
  1777. expect(second.activeTabId).toBe("browser-use:tab-2");
  1778. });
  1779. it("显式 activate=false 可后台追加,后续 activate=true 再激活", () => {
  1780. const current = openBrowserSidePane(null, { tabId: "browser:human" });
  1781. const background = openBrowserUseSidePane(current, {
  1782. workspaceKey: "/workspace",
  1783. sessionId: "sess-1",
  1784. tabId: "tab-1",
  1785. activate: false,
  1786. });
  1787. expect(background.activeTabId).toBe("browser:human");
  1788. const visible = openBrowserUseSidePane(background, {
  1789. workspaceKey: "/workspace",
  1790. sessionId: "sess-1",
  1791. tabId: "tab-1",
  1792. activate: true,
  1793. });
  1794. expect(visible.activeTabId).toBe("browser-use:tab-1");
  1795. });
  1796. it("updateBrowserSidePaneTab 回填 browser-use tab 的真实标题/favicon", () => {
  1797. const opened = openBrowserUseSidePane(null, {
  1798. workspaceKey: "/workspace",
  1799. sessionId: "sess-1",
  1800. tabId: "tab-1",
  1801. });
  1802. const updated = updateBrowserSidePaneTab(opened, "browser-use:tab-1", {
  1803. title: "Example Domain",
  1804. faviconUrl: "https://example.com/favicon.ico",
  1805. });
  1806. const tab = updated?.tabs[0];
  1807. expect(tab).toMatchObject({
  1808. type: "browser-use",
  1809. title: "Example Domain",
  1810. faviconUrl: "https://example.com/favicon.ico",
  1811. });
  1812. });
  1813. it("重开同一 browser-use tab 保留已回填的标题/favicon(握手期 guest 重建不清空)", () => {
  1814. const opened = openBrowserUseSidePane(null, {
  1815. workspaceKey: "/workspace",
  1816. sessionId: "sess-1",
  1817. tabId: "tab-1",
  1818. });
  1819. const withMeta = updateBrowserSidePaneTab(opened, "browser-use:tab-1", {
  1820. title: "Example Domain",
  1821. faviconUrl: "https://example.com/favicon.ico",
  1822. });
  1823. // 模拟 guest 销毁后再命令 → 再次广播 BrowserViewReady → 再次 open。
  1824. const reopened = openBrowserUseSidePane(withMeta, {
  1825. workspaceKey: "/workspace",
  1826. sessionId: "sess-1",
  1827. tabId: "tab-1",
  1828. });
  1829. expect(reopened.tabs).toHaveLength(1);
  1830. expect(reopened.tabs[0]).toMatchObject({
  1831. type: "browser-use",
  1832. title: "Example Domain",
  1833. faviconUrl: "https://example.com/favicon.ico",
  1834. });
  1835. });
  1836. it("按 workspace/session/tab 标记 5 秒操作状态且不覆盖 favicon", () => {
  1837. const opened = openBrowserUseSidePane(null, {
  1838. workspaceKey: "remote:ssh:host:/workspace",
  1839. sessionId: "sess-1",
  1840. tabId: "tab-1",
  1841. browserId: "iab-1",
  1842. browserGeneration: 7,
  1843. });
  1844. const withMeta = updateBrowserSidePaneTab(opened, "browser-use:tab-1", {
  1845. faviconUrl: "https://example.com/favicon.ico",
  1846. });
  1847. const operationUntil = 10_000 + BROWSER_USE_OPERATION_INDICATOR_DURATION_MS;
  1848. const marked = markBrowserUseSidePaneTabOperation(withMeta, {
  1849. workspaceKey: "remote:ssh:host:/workspace",
  1850. sessionId: "sess-1",
  1851. browserId: "iab-1",
  1852. browserGeneration: 7,
  1853. tabId: "tab-1",
  1854. operationUntil,
  1855. });
  1856. expect(marked?.tabs[0]).toMatchObject({
  1857. faviconUrl: "https://example.com/favicon.ico",
  1858. browserUseOperationUntil: operationUntil,
  1859. });
  1860. expect(
  1861. markBrowserUseSidePaneTabOperation(marked, {
  1862. workspaceKey: "/workspace",
  1863. sessionId: "sess-1",
  1864. browserId: "iab-1",
  1865. browserGeneration: 7,
  1866. tabId: "tab-1",
  1867. operationUntil: operationUntil + 1,
  1868. }),
  1869. ).toBe(marked);
  1870. expect(
  1871. markBrowserUseSidePaneTabOperation(marked, {
  1872. workspaceKey: "remote:ssh:host:/workspace",
  1873. sessionId: "sess-1",
  1874. browserId: "iab-1",
  1875. browserGeneration: 8,
  1876. tabId: "tab-1",
  1877. operationUntil: operationUntil + 1,
  1878. }),
  1879. ).toBe(marked);
  1880. });
  1881. it("同一 tab 的后续操作刷新截止时间,只有布局命令推进 resize baseline 版本", () => {
  1882. const opened = openBrowserUseSidePane(null, {
  1883. workspaceKey: "/workspace",
  1884. sessionId: "sess-1",
  1885. browserId: "iab-1",
  1886. browserGeneration: 1,
  1887. tabId: "tab-1",
  1888. });
  1889. const first = markBrowserUseSidePaneTabOperation(opened, {
  1890. workspaceKey: "/workspace",
  1891. sessionId: "sess-1",
  1892. browserId: "iab-1",
  1893. browserGeneration: 1,
  1894. tabId: "tab-1",
  1895. operationUntil: 5_000,
  1896. resetsResizeBaseline: true,
  1897. });
  1898. const second = markBrowserUseSidePaneTabOperation(first, {
  1899. workspaceKey: "/workspace",
  1900. sessionId: "sess-1",
  1901. browserId: "iab-1",
  1902. browserGeneration: 1,
  1903. tabId: "tab-1",
  1904. operationUntil: 9_000,
  1905. });
  1906. const third = markBrowserUseSidePaneTabOperation(second, {
  1907. workspaceKey: "/workspace",
  1908. sessionId: "sess-1",
  1909. browserId: "iab-1",
  1910. browserGeneration: 1,
  1911. tabId: "tab-1",
  1912. operationUntil: 12_000,
  1913. resetsResizeBaseline: true,
  1914. });
  1915. expect(second?.tabs[0]).toMatchObject({
  1916. browserUseOperationUntil: 9_000,
  1917. browserUseResizeBaselineVersion: 1,
  1918. });
  1919. expect(third?.tabs[0]).toMatchObject({
  1920. browserUseOperationUntil: 12_000,
  1921. browserUseResizeBaselineVersion: 2,
  1922. });
  1923. });
  1924. it("A 的迟到 ready 在 B active 时后台归属 A,切回 A 后可恢复", () => {
  1925. const current = openBrowserSidePane(null, { tabId: "browser:b" });
  1926. const stampedCurrent = stampSidePaneTabsOwnership(current, {
  1927. workspaceKey: "/workspace",
  1928. ownerTaskId: "sess-b",
  1929. });
  1930. const result = applyBrowserUseSidePaneEvent(
  1931. stampedCurrent,
  1932. {
  1933. workspaceKey: "/workspace",
  1934. sessionId: "sess-a",
  1935. tabId: "tab-a",
  1936. browserId: "iab",
  1937. browserGeneration: 1,
  1938. },
  1939. { workspaceKey: "/workspace", ownerTaskId: "sess-b" },
  1940. );
  1941. expect(result.shouldReveal).toBe(false);
  1942. expect(result.state.activeTabId).toBe("browser:b");
  1943. expect(result.state.tabs.at(-1)).toMatchObject({
  1944. id: "browser-use:tab-a",
  1945. ownerTaskId: "sess-a",
  1946. workspaceKey: "/workspace",
  1947. sessionId: "sess-a",
  1948. });
  1949. expect(
  1950. resolveActiveTabForOwner(result.state, {
  1951. workspaceKey: "/workspace",
  1952. ownerTaskId: "sess-a",
  1953. }),
  1954. ).toBe("browser-use:tab-a");
  1955. });
  1956. it("A 的 browser-use tab 在切到无 tab 的 B 后保持挂载,切回 A 主动展开", () => {
  1957. const stateA = openBrowserUseSidePane(null, {
  1958. workspaceKey: "/workspace",
  1959. sessionId: "sess-a",
  1960. tabId: "tab-a",
  1961. });
  1962. const stateB = resolveSidePaneScopeState(stateA, {
  1963. workspaceKey: "/workspace",
  1964. ownerTaskId: "sess-b",
  1965. });
  1966. expect(stateB.sidePaneState).toMatchObject({
  1967. activeTabId: "",
  1968. tabs: [{ id: "browser-use:tab-a", tabId: "tab-a" }],
  1969. });
  1970. expect(stateB.isSidePaneCollapsed).toBe(true);
  1971. expect(shouldMountSidePaneContent(false, stateB.sidePaneState?.tabs ?? [])).toBe(true);
  1972. const restoredA = resolveSidePaneScopeState(stateB.sidePaneState, {
  1973. workspaceKey: "/workspace",
  1974. ownerTaskId: "sess-a",
  1975. });
  1976. expect(restoredA.sidePaneState).toMatchObject({
  1977. activeTabId: "browser-use:tab-a",
  1978. tabs: [{ id: "browser-use:tab-a", tabId: "tab-a" }],
  1979. });
  1980. expect(restoredA.isSidePaneCollapsed).toBe(false);
  1981. });
  1982. it("A 主动收起后切到 B 再切回 A 仍保持收起", () => {
  1983. const stateA = openBrowserUseSidePane(null, {
  1984. workspaceKey: "/workspace",
  1985. sessionId: "sess-a",
  1986. tabId: "tab-a",
  1987. });
  1988. const stateB = resolveSidePaneScopeState(
  1989. stateA,
  1990. { workspaceKey: "/workspace", ownerTaskId: "sess-b" },
  1991. undefined,
  1992. true,
  1993. );
  1994. const restoredA = resolveSidePaneScopeState(
  1995. stateB.sidePaneState,
  1996. { workspaceKey: "/workspace", ownerTaskId: "sess-a" },
  1997. undefined,
  1998. true,
  1999. );
  2000. expect(restoredA.sidePaneState).toMatchObject({
  2001. activeTabId: "browser-use:tab-a",
  2002. tabs: [{ id: "browser-use:tab-a", tabId: "tab-a" }],
  2003. });
  2004. expect(restoredA.isSidePaneCollapsed).toBe(true);
  2005. });
  2006. it("origin session 仍 active 时立即展开并激活", () => {
  2007. const result = applyBrowserUseSidePaneEvent(
  2008. null,
  2009. { workspaceKey: "/workspace", sessionId: "sess-a", tabId: "tab-a" },
  2010. { workspaceKey: "/workspace", ownerTaskId: "sess-a" },
  2011. );
  2012. expect(result.shouldReveal).toBe(true);
  2013. expect(result.state.activeTabId).toBe("browser-use:tab-a");
  2014. });
  2015. it("BTL19: remote visibility 重放不会清空既有 remoteSessionId", () => {
  2016. const ready = openBrowserUseSidePane(null, {
  2017. workspaceKey: "ssh://host/repo",
  2018. remoteSessionId: "remote-a",
  2019. sessionId: "sess-a",
  2020. tabId: "tab-a",
  2021. browserId: "iab-a",
  2022. browserGeneration: 2,
  2023. });
  2024. const result = applyBrowserUseSidePaneEvent(
  2025. ready,
  2026. {
  2027. workspaceKey: "ssh://host/repo",
  2028. remoteSessionId: "remote-a",
  2029. sessionId: "sess-a",
  2030. tabId: "tab-a",
  2031. browserId: "iab-a",
  2032. browserGeneration: 2,
  2033. },
  2034. {
  2035. workspaceKey: "ssh://host/repo",
  2036. remoteSessionId: "remote-a",
  2037. ownerTaskId: "sess-a",
  2038. },
  2039. );
  2040. const legacyReplay = applyBrowserUseSidePaneEvent(
  2041. result.state,
  2042. {
  2043. workspaceKey: "ssh://host/repo",
  2044. sessionId: "sess-a",
  2045. tabId: "tab-a",
  2046. browserId: "iab-a",
  2047. browserGeneration: 2,
  2048. },
  2049. {
  2050. workspaceKey: "ssh://host/repo",
  2051. remoteSessionId: "remote-a",
  2052. ownerTaskId: "sess-a",
  2053. },
  2054. );
  2055. expect(result.state.tabs[0]).toMatchObject({ remoteSessionId: "remote-a" });
  2056. expect(legacyReplay.state.tabs[0]).toMatchObject({ remoteSessionId: "remote-a" });
  2057. });
  2058. it("同一 session 多个 browser-use tab 时 show 事件激活指定 tab", () => {
  2059. const first = openBrowserUseSidePane(null, {
  2060. workspaceKey: "/workspace",
  2061. sessionId: "sess-a",
  2062. tabId: "tab-a",
  2063. });
  2064. const withBackground = openBrowserUseSidePane(first, {
  2065. workspaceKey: "/workspace",
  2066. sessionId: "sess-a",
  2067. tabId: "tab-b",
  2068. activate: false,
  2069. });
  2070. const result = applyBrowserUseSidePaneEvent(
  2071. withBackground,
  2072. { workspaceKey: "/workspace", sessionId: "sess-a", tabId: "tab-b" },
  2073. { workspaceKey: "/workspace", ownerTaskId: "sess-a" },
  2074. );
  2075. expect(result.shouldReveal).toBe(true);
  2076. expect(result.state.activeTabId).toBe("browser-use:tab-b");
  2077. });
  2078. it("visibility 只激活已有 browser-use tab,不创建已关闭的 tab shell", () => {
  2079. const first = openBrowserUseSidePane(null, {
  2080. workspaceKey: "/workspace",
  2081. sessionId: "sess-a",
  2082. tabId: "tab-a",
  2083. });
  2084. const withBackground = openBrowserUseSidePane(first, {
  2085. workspaceKey: "/workspace",
  2086. sessionId: "sess-a",
  2087. tabId: "tab-b",
  2088. activate: false,
  2089. });
  2090. const activeScope = { workspaceKey: "/workspace", ownerTaskId: "sess-a" };
  2091. const activated = applyBrowserUseSidePaneVisibilityEvent(
  2092. withBackground,
  2093. { workspaceKey: "/workspace", sessionId: "sess-a", tabId: "tab-b" },
  2094. activeScope,
  2095. );
  2096. expect(activated.shouldReveal).toBe(true);
  2097. expect(activated.state.activeTabId).toBe("browser-use:tab-b");
  2098. const closed = closeSidePaneTab(activated.state, "browser-use:tab-b");
  2099. const delayedVisibility = applyBrowserUseSidePaneVisibilityEvent(
  2100. closed,
  2101. { workspaceKey: "/workspace", sessionId: "sess-a", tabId: "tab-b" },
  2102. activeScope,
  2103. );
  2104. expect(delayedVisibility.shouldReveal).toBe(false);
  2105. expect(delayedVisibility.state).toBe(closed);
  2106. expect(delayedVisibility.state.tabs).toHaveLength(1);
  2107. expect(delayedVisibility.state.tabs[0]?.tabId).toBe("tab-a");
  2108. });
  2109. it("后台 session 激活 tab 时不抢前台,切回可按 preferred tab 恢复", () => {
  2110. const backgroundA = openBrowserUseSidePane(null, {
  2111. workspaceKey: "/workspace",
  2112. sessionId: "sess-x",
  2113. tabId: "tab-x-a",
  2114. });
  2115. const backgroundB = openBrowserUseSidePane(backgroundA, {
  2116. workspaceKey: "/workspace",
  2117. sessionId: "sess-x",
  2118. tabId: "tab-x-b",
  2119. activate: false,
  2120. });
  2121. const foreground = openBrowserUseSidePane(backgroundB, {
  2122. workspaceKey: "/workspace",
  2123. sessionId: "sess-y",
  2124. tabId: "tab-y",
  2125. });
  2126. const result = applyBrowserUseSidePaneEvent(
  2127. foreground,
  2128. { workspaceKey: "/workspace", sessionId: "sess-x", tabId: "tab-x-b" },
  2129. { workspaceKey: "/workspace", ownerTaskId: "sess-y" },
  2130. );
  2131. expect(result.shouldReveal).toBe(false);
  2132. expect(result.state.activeTabId).toBe("browser-use:tab-y");
  2133. expect(
  2134. resolveActiveTabForOwner(
  2135. result.state,
  2136. { workspaceKey: "/workspace", ownerTaskId: "sess-x" },
  2137. "browser-use:tab-x-b",
  2138. ),
  2139. ).toBe("browser-use:tab-x-b");
  2140. });
  2141. it("其它 workspace 的 ready 也只后台挂载,不抢当前对话", () => {
  2142. const current = openBrowserSidePane(null, { tabId: "browser:b" });
  2143. const stampedCurrent = stampSidePaneTabsOwnership(current, {
  2144. workspaceKey: "/workspace-b",
  2145. ownerTaskId: "sess-a",
  2146. });
  2147. const result = applyBrowserUseSidePaneEvent(
  2148. stampedCurrent,
  2149. { workspaceKey: "/workspace-a", sessionId: "sess-a", tabId: "tab-a" },
  2150. { workspaceKey: "/workspace-b", ownerTaskId: "sess-a" },
  2151. );
  2152. expect(result.shouldReveal).toBe(false);
  2153. expect(result.state.activeTabId).toBe("browser:b");
  2154. expect(result.state.tabs.at(-1)).toMatchObject({
  2155. workspaceKey: "/workspace-a",
  2156. });
  2157. });
  2158. });
  2159. describe("按 (项目, 对话) 隔离", () => {
  2160. // 便捷构造可见性作用域;workspaceKey 缺省 null(tab.workspaceKey==null 时对任意项目可见)。
  2161. const scope = (ownerTaskId: string | null, workspaceKey: string | null = null) => ({
  2162. workspaceKey,
  2163. ownerTaskId,
  2164. });
  2165. it("stampSidePaneTabsOwnership 只给未打标(undefined)的新 tab 打归属(对话+项目),已打标的不动", () => {
  2166. const opened = openBrowserSidePane(null, { tabId: "browser:1" });
  2167. expect(opened.tabs[0]!.ownerTaskId).toBeUndefined();
  2168. const stamped = stampSidePaneTabsOwnership(opened, {
  2169. ownerTaskId: "task-a",
  2170. workspaceKey: "ws-1",
  2171. })!;
  2172. expect(stamped.tabs[0]!.ownerTaskId).toBe("task-a");
  2173. expect(stamped.tabs[0]!.workspaceKey).toBe("ws-1");
  2174. // 再次 stamp 用别的归属,不应改动已打标的。
  2175. const restamped = stampSidePaneTabsOwnership(stamped, {
  2176. ownerTaskId: "task-b",
  2177. workspaceKey: "ws-2",
  2178. })!;
  2179. expect(restamped.tabs[0]!.ownerTaskId).toBe("task-a");
  2180. expect(restamped).toBe(stamped);
  2181. });
  2182. it("getVisibleSidePaneTabs 按对话过滤;undefined/null 归到草稿;workspace 工具对本项目所有对话可见", () => {
  2183. const tabs = [
  2184. { id: "a", type: "browser" as const, ownerTaskId: "task-a" },
  2185. { id: "b", type: "browser" as const, ownerTaskId: "task-b" },
  2186. { id: "d", type: "browser" as const, ownerTaskId: null },
  2187. { id: "u", type: "browser" as const },
  2188. { id: "git", type: "git" as const, ownerTaskId: "task-a" },
  2189. ];
  2190. expect(getVisibleSidePaneTabs(tabs, scope("task-a")).map((t) => t.id)).toEqual(["a", "git"]);
  2191. expect(getVisibleSidePaneTabs(tabs, scope("task-b")).map((t) => t.id)).toEqual(["b", "git"]);
  2192. expect(getVisibleSidePaneTabs(tabs, scope(null)).map((t) => t.id)).toEqual(["d", "u", "git"]);
  2193. });
  2194. it("getVisibleSidePaneTabs 按项目(workspaceKey)隔离:切项目只看本项目的 tab(含 workspace 工具)", () => {
  2195. const tabs = [
  2196. {
  2197. id: "a1",
  2198. type: "browser" as const,
  2199. ownerTaskId: "task-a",
  2200. workspaceKey: "ws-1",
  2201. },
  2202. {
  2203. id: "b1",
  2204. type: "browser" as const,
  2205. ownerTaskId: "task-b",
  2206. workspaceKey: "ws-2",
  2207. },
  2208. {
  2209. id: "git1",
  2210. type: "git" as const,
  2211. ownerTaskId: "task-a",
  2212. workspaceKey: "ws-1",
  2213. },
  2214. {
  2215. id: "git2",
  2216. type: "git" as const,
  2217. ownerTaskId: "task-b",
  2218. workspaceKey: "ws-2",
  2219. },
  2220. ];
  2221. // 项目 ws-1、对话 task-a:只看 ws-1 的 tab(a1 + ws-1 的 git)。
  2222. expect(getVisibleSidePaneTabs(tabs, scope("task-a", "ws-1")).map((t) => t.id)).toEqual([
  2223. "a1",
  2224. "git1",
  2225. ]);
  2226. // 项目 ws-2、对话 task-b:只看 ws-2 的 tab。
  2227. expect(getVisibleSidePaneTabs(tabs, scope("task-b", "ws-2")).map((t) => t.id)).toEqual([
  2228. "b1",
  2229. "git2",
  2230. ]);
  2231. });
  2232. it("resolveActiveTabForOwner:preferred 优先 → 当前 active → 该作用域最新 → 无则 null", () => {
  2233. const state = {
  2234. activeTabId: "a2",
  2235. tabs: [
  2236. { id: "a1", type: "browser" as const, ownerTaskId: "task-a" },
  2237. { id: "a2", type: "browser" as const, ownerTaskId: "task-a" },
  2238. { id: "b1", type: "browser" as const, ownerTaskId: "task-b" },
  2239. ],
  2240. };
  2241. expect(resolveActiveTabForOwner(state, scope("task-a"), "a1")).toBe("a1");
  2242. expect(resolveActiveTabForOwner(state, scope("task-a"), "b1")).toBe("a2");
  2243. expect(resolveActiveTabForOwner(state, scope("task-b"))).toBe("b1");
  2244. expect(resolveActiveTabForOwner(state, scope("task-c"))).toBeNull();
  2245. });
  2246. it("toggle/activate browser 只复用当前对话的 browser tab,不串用别的对话", () => {
  2247. const stateA = stampSidePaneTabsOwnership(openBrowserSidePane(null, { tabId: "browser:a" }), {
  2248. ownerTaskId: "task-a",
  2249. workspaceKey: "ws-1",
  2250. })!;
  2251. expect(getVisibleSidePaneTabs(stateA.tabs, scope("task-a", "ws-1")).map((t) => t.id)).toEqual(
  2252. ["browser:a"],
  2253. );
  2254. // 在对话 B 点浏览器开关:不能复用 A 的 tab,应为 B 新建;A 的 tab 仍保留(不销毁)。
  2255. const toggled = stampSidePaneTabsOwnership(toggleBrowserSidePane(stateA, "task-b"), {
  2256. ownerTaskId: "task-b",
  2257. workspaceKey: "ws-1",
  2258. })!;
  2259. expect(toggled.tabs).toHaveLength(2);
  2260. const bVisible = getVisibleSidePaneTabs(toggled.tabs, scope("task-b", "ws-1"));
  2261. expect(bVisible).toHaveLength(1);
  2262. expect(bVisible[0]!.id).not.toBe("browser:a");
  2263. expect(toggled.activeTabId).toBe(bVisible[0]!.id);
  2264. expect(
  2265. getVisibleSidePaneTabs(toggled.tabs, scope("task-a", "ws-1")).map((t) => t.id),
  2266. ).toEqual(["browser:a"]);
  2267. });
  2268. });
  2269. describe("openOrActivateBrowserSidePaneByUrl", () => {
  2270. const REPORT_URL = "file:///repo/report.html";
  2271. it("同一 workspace/owner 的同一 URL 只激活已有 tab,并保留它的字段", () => {
  2272. const first = openOrActivateBrowserSidePaneByUrl(null, {
  2273. initialUrl: REPORT_URL,
  2274. tabId: "browser:first",
  2275. ownerTaskId: "task-a",
  2276. workspaceKey: "ws-1",
  2277. });
  2278. const other = openBrowserSidePane(first, { tabId: "browser:other" });
  2279. const second = openOrActivateBrowserSidePaneByUrl(other, {
  2280. initialUrl: REPORT_URL,
  2281. tabId: "browser:second",
  2282. ownerTaskId: "task-a",
  2283. workspaceKey: "ws-1",
  2284. });
  2285. expect(second.tabs.filter((tab) => tab.id === "browser:first")).toHaveLength(1);
  2286. expect(second.tabs.some((tab) => tab.id === "browser:second")).toBe(false);
  2287. expect(second.activeTabId).toBe("browser:first");
  2288. // 复用不改任何字段:initialUrl 就是键,要重新取字节由调用方另发导航请求。
  2289. expect(second.tabs.find((tab) => tab.id === "browser:first")).toEqual(
  2290. first.tabs.find((tab) => tab.id === "browser:first"),
  2291. );
  2292. });
  2293. it("URL / owner / workspace 任一不同都另开一个 tab", () => {
  2294. const base = openOrActivateBrowserSidePaneByUrl(null, {
  2295. initialUrl: REPORT_URL,
  2296. tabId: "browser:base",
  2297. ownerTaskId: "task-a",
  2298. workspaceKey: "ws-1",
  2299. });
  2300. const otherUrl = openOrActivateBrowserSidePaneByUrl(base, {
  2301. initialUrl: "file:///repo/other.html",
  2302. tabId: "browser:url",
  2303. ownerTaskId: "task-a",
  2304. workspaceKey: "ws-1",
  2305. });
  2306. expect(otherUrl.activeTabId).toBe("browser:url");
  2307. const otherOwner = openOrActivateBrowserSidePaneByUrl(base, {
  2308. initialUrl: REPORT_URL,
  2309. tabId: "browser:owner",
  2310. ownerTaskId: "task-b",
  2311. workspaceKey: "ws-1",
  2312. });
  2313. expect(otherOwner.activeTabId).toBe("browser:owner");
  2314. const otherWorkspace = openOrActivateBrowserSidePaneByUrl(base, {
  2315. initialUrl: REPORT_URL,
  2316. tabId: "browser:workspace",
  2317. ownerTaskId: "task-a",
  2318. workspaceKey: "ws-2",
  2319. });
  2320. expect(otherWorkspace.activeTabId).toBe("browser:workspace");
  2321. });
  2322. it("tabId 缺席时现生成(share handover 的老调用形状不变)", () => {
  2323. const state = openOrActivateBrowserSidePaneByUrl(null, {
  2324. initialUrl: "https://example.com/share/abc",
  2325. ownerTaskId: "task-a",
  2326. workspaceKey: "ws-1",
  2327. });
  2328. expect(state.tabs).toHaveLength(1);
  2329. expect(state.tabs[0]!.id.startsWith("browser:")).toBe(true);
  2330. expect(state.tabs[0]).toMatchObject({
  2331. type: "browser",
  2332. initialUrl: "https://example.com/share/abc",
  2333. ownerTaskId: "task-a",
  2334. workspaceKey: "ws-1",
  2335. });
  2336. });
  2337. it("findBrowserSidePaneTabByUrl 与 open 落到同一个 tab(调用方据它发导航请求)", () => {
  2338. // useAppPanels 的直开路径:建 tab 时不带 ownerTaskId,归属由 stamping 盖当前 owner,
  2339. // 复查时用同一个 owner —— 两处必须认出同一个 tab,否则第二次点击会重复开。
  2340. const opened = stampSidePaneTabsOwnership(
  2341. openOrActivateBrowserSidePaneByUrl(null, {
  2342. initialUrl: REPORT_URL,
  2343. tabId: "browser:stamped",
  2344. workspaceKey: "ws-1",
  2345. }),
  2346. { ownerTaskId: "task-a", workspaceKey: "ws-1" },
  2347. )!;
  2348. const found = findBrowserSidePaneTabByUrl(opened, {
  2349. initialUrl: REPORT_URL,
  2350. ownerTaskId: "task-a",
  2351. workspaceKey: "ws-1",
  2352. });
  2353. expect(found?.id).toBe("browser:stamped");
  2354. const again = openOrActivateBrowserSidePaneByUrl(opened, {
  2355. initialUrl: REPORT_URL,
  2356. tabId: "browser:new",
  2357. ownerTaskId: "task-a",
  2358. workspaceKey: "ws-1",
  2359. });
  2360. expect(again.tabs).toHaveLength(1);
  2361. expect(again.activeTabId).toBe("browser:stamped");
  2362. });
  2363. it("没有匹配 URL 时返回 undefined", () => {
  2364. const state = openBrowserSidePane(null, { tabId: "browser:a" });
  2365. expect(
  2366. findBrowserSidePaneTabByUrl(state, { initialUrl: REPORT_URL, ownerTaskId: "task-a" }),
  2367. ).toBeUndefined();
  2368. expect(findBrowserSidePaneTabByUrl(null, { initialUrl: REPORT_URL })).toBeUndefined();
  2369. });
  2370. });
  2371. describe("openBrowserPermissionsSidePane", () => {
  2372. it("同站点 + 同工作区 + 同对话复用既有标签并激活,不新建", () => {
  2373. const opened = openBrowserPermissionsSidePane(null, {
  2374. origin: "https://example.com",
  2375. ownerTaskId: "task-a",
  2376. workspaceKey: "ws-1",
  2377. });
  2378. expect(opened.tabs).toHaveLength(1);
  2379. const tab = opened.tabs[0]!;
  2380. expect(tab.type).toBe("browser-permissions");
  2381. const again = openBrowserPermissionsSidePane(
  2382. { ...opened, activeTabId: "" },
  2383. { origin: "https://example.com", ownerTaskId: "task-a", workspaceKey: "ws-1" },
  2384. );
  2385. expect(again.tabs).toHaveLength(1);
  2386. expect(again.activeTabId).toBe(tab.id);
  2387. });
  2388. it("不同站点、不同工作区或不同对话各自开新标签", () => {
  2389. const opened = openBrowserPermissionsSidePane(null, {
  2390. origin: "https://example.com",
  2391. ownerTaskId: "task-a",
  2392. workspaceKey: "ws-1",
  2393. });
  2394. const otherOrigin = openBrowserPermissionsSidePane(opened, {
  2395. origin: "https://other.com",
  2396. ownerTaskId: "task-a",
  2397. workspaceKey: "ws-1",
  2398. });
  2399. const otherWorkspace = openBrowserPermissionsSidePane(otherOrigin, {
  2400. origin: "https://example.com",
  2401. ownerTaskId: "task-a",
  2402. workspaceKey: "ws-2",
  2403. });
  2404. const otherOwner = openBrowserPermissionsSidePane(otherWorkspace, {
  2405. origin: "https://example.com",
  2406. ownerTaskId: "task-b",
  2407. workspaceKey: "ws-1",
  2408. });
  2409. expect(otherOwner.tabs).toHaveLength(4);
  2410. });
  2411. it("按 owner 作用域过滤:归属 task-a 的设置标签对 task-b 不可见", () => {
  2412. const opened = openBrowserPermissionsSidePane(null, {
  2413. origin: "https://example.com",
  2414. ownerTaskId: "task-a",
  2415. workspaceKey: "ws-1",
  2416. });
  2417. expect(
  2418. resolveActiveTabForOwner(opened, { workspaceKey: "ws-1", ownerTaskId: "task-a" }),
  2419. ).toBe(opened.tabs[0]!.id);
  2420. expect(
  2421. resolveActiveTabForOwner(opened, { workspaceKey: "ws-1", ownerTaskId: "task-b" }),
  2422. ).toBeNull();
  2423. });
  2424. });
  2425. });