The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(desktop): prove the kept Browser body survives A -> B -> A, not the registration identity
teknium1ab713f7c2bd6
diff --git a/apps/desktop/src/app/chat/preview-tile-live-body.test.tsx b/apps/desktop/src/app/chat/preview-tile-live-body.test.tsxnew file mode 100644index 00000000000..7973d56c7ba--- /dev/null+++ b/apps/desktop/src/app/chat/preview-tile-live-body.test.tsx@@ -0,0 +1,103 @@+import { act, cleanup, render } from '@testing-library/react'+import { createElement, useEffect } from 'react'+import { afterEach, beforeEach, expect, it, vi } from 'vitest'++import { group } from '@/components/pane-shell/tree/model'+import { LayoutTreeRoot } from '@/components/pane-shell/tree/renderer'+import { declareDefaultTree, treePanesWithPrefix, watchContributedPanes } from '@/components/pane-shell/tree/store'+import { registry } from '@/contrib/registry'+import { $previewTabs, openPreview } from '@/store/preview'+import { $selectedStoredSessionId } from '@/store/session'+import { stubMenuDomApis } from '@/test/jsdom'++import { watchPreviewTiles } from './preview-tile'++// A native guest has a lifetime beyond React: a remount, or any detach of its+// element, destroys the Electron guest (unsaved input, page JS state). Count+// both, on a stand-in for the pane body.+const disconnected: HTMLElement[] = []+const mounts = new Map<string, number>()++class LiveGuest extends HTMLElement {+ disconnectedCallback() {+ disconnected.push(this)+ }+}++if (!customElements.get('preview-live-guest')) {+ customElements.define('preview-live-guest', LiveGuest)+}++vi.mock('./right-rail/preview', () => ({+ PreviewTilePane: ({ tabId }: { tabId: string }) => {+ useEffect(() => {+ mounts.set(tabId, (mounts.get(tabId) ?? 0) + 1)+ }, [tabId])++ return createElement('preview-live-guest', { 'data-tab': tabId }, createElement('input', { defaultValue: '' }))+ }+}))++vi.mock('./right-rail/preview-console-store', () => ({+ forgetPreviewConsole: () => undefined+}))++class ResizeObserverStub {+ observe() {}+ unobserve() {}+ disconnect() {}+}++beforeEach(() => {+ disconnected.length = 0+ mounts.clear()+ vi.stubGlobal('ResizeObserver', ResizeObserverStub)+ vi.stubGlobal('CSS', { ...globalThis.CSS, escape: (value: string) => value })+ stubMenuDomApis()+})++afterEach(() => {+ cleanup()+ vi.unstubAllGlobals()+})++// The behaviour a user sees: switching A -> B -> A shows A's Browser with the+// page they left — the same guest, never torn down in between — and B's drawer+// does not show it.+it("keeps a hidden session's live Browser body alive and shows the same one on return", () => {+ registry.register({+ area: 'panes',+ data: { placement: 'main', uncloseable: true },+ id: 'workspace',+ render: () => null,+ title: 'workspace'+ })+ declareDefaultTree(group(['workspace'], { active: 'workspace', id: 'grp-main' }))+ watchContributedPanes()+ watchPreviewTiles()++ $selectedStoredSessionId.set('sess-live-a')+ act(() => openPreview({ kind: 'url', label: 'A', source: 'https://a.example', url: 'https://a.example' }))+ const aTab = $previewTabs.get()[0]!.id++ const { container } = render(createElement(LayoutTreeRoot))++ const guestFor = (tabId: string) => container.querySelector<HTMLElement>(`preview-live-guest[data-tab="${tabId}"]`)++ const aGuest = guestFor(aTab)!+ const input = aGuest.querySelector('input')!+ input.value = 'unsaved in A'++ act(() => $selectedStoredSessionId.set('sess-live-b'))+ expect(treePanesWithPrefix('preview-tile:')).toEqual([])+ expect(aGuest.isConnected).toBe(true)+ expect(aGuest.closest('[inert]')).not.toBeNull()++ act(() => $selectedStoredSessionId.set('sess-live-a'))+ expect(treePanesWithPrefix('preview-tile:')).toEqual([`preview-tile:${aTab}`])+ expect(guestFor(aTab)).toBe(aGuest)+ expect(aGuest.closest('[inert]')).toBeNull()+ expect(input.value).toBe('unsaved in A')+ expect(disconnected).not.toContain(aGuest)+ expect(mounts.get(aTab)).toBe(1)+})diff --git a/apps/desktop/src/app/chat/preview-tile.test.ts b/apps/desktop/src/app/chat/preview-tile.test.tsindex 5f1359ceb5b..ac4fca45dfb 100644--- a/apps/desktop/src/app/chat/preview-tile.test.ts+++ b/apps/desktop/src/app/chat/preview-tile.test.ts@@ -239,32 +239,6 @@ describe('preview tiles mirror the visible session tabs', () => {const browserTarget = (url: string) => ({ kind: 'url', label: url, source: url, url }) as const- it("keeps a hidden session's live page mounted and re-docks the same body when it returns", async () => {- const { preview, session, tree } = await setup()- const { registry } = await import('@/contrib/registry')- const contribution = (id: string) => registry.getArea('panes').find(pane => pane.id === `preview-tile:${id}`)-- session.$selectedStoredSessionId.set('sess-1')- preview.openPreview(browserTarget('https://a.example'))- const aTab = preview.$previewTabs.get()[0]!.id- const aBody = contribution(aTab)-- expect(aBody).toBeDefined()-- // Off screen: out of the tree (no tab in B's drawer), body still registered.- session.$selectedStoredSessionId.set('sess-2')- preview.openPreview(browserTarget('https://b.example'))- expect(tree.treePanesWithPrefix('preview-tile:')).toEqual([- `preview-tile:${preview.$previewTabs.get().find(tab => tab.id !== aTab)!.id}`- ])- expect(contribution(aTab)).toBe(aBody)-- // Back on A: the very same registration (a re-register would remount it).- session.$selectedStoredSessionId.set('sess-1')- expect(tree.treePanesWithPrefix('preview-tile:')).toEqual([`preview-tile:${aTab}`])- expect(contribution(aTab)).toBe(aBody)- })-it('unmounts only the longest-hidden live page past the cap, and reopens it on return', async () => {const { preview, session, tree } = await setup()const { registry } = await import('@/contrib/registry')