workspaceSidePane.test.ts 96 KB

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