|
| 1 | +// @vitest-environment jsdom |
| 2 | +import React, { act, Profiler } from 'react'; |
| 3 | +import { createRoot } from 'react-dom/client'; |
| 4 | +import { expect, it, vi } from 'vitest'; |
| 5 | +import type { TabGroupItem } from '@openbitfun/ui'; |
| 6 | +import SceneBar from './SceneBar'; |
| 7 | +import { flowChatStore } from '@/flow_chat/store/FlowChatStore'; |
| 8 | +import type { Session } from '@/flow_chat/types/flow-chat'; |
| 9 | +import { resetSceneStoreForTest, useSceneStore } from '../../stores/sceneStore'; |
| 10 | +import { createPaneState } from '../../workbench/scenePaneModel'; |
| 11 | +import { useContentResourceStore } from '../../workbench/contentResourceStore'; |
| 12 | +import { activateSurface } from '@/infrastructure/peer-device/deviceSurface'; |
| 13 | +import { useDeviceDirectory } from '@/infrastructure/account/deviceDirectory'; |
| 14 | +import type { SceneTab } from './types'; |
| 15 | + |
| 16 | +const captured = vi.hoisted(() => ({ items: [] as TabGroupItem[][] })); |
| 17 | +vi.mock('@openbitfun/ui', async importOriginal => { |
| 18 | + const actual = await importOriginal<typeof import('@openbitfun/ui')>(); |
| 19 | + const React = await import('react'); |
| 20 | + return { ...actual, TabGroup: React.forwardRef<HTMLDivElement, React.ComponentProps<typeof actual.TabGroup>>((props, ref) => { |
| 21 | + captured.items.push(props.items); |
| 22 | + return <actual.TabGroup {...props} ref={ref} />; |
| 23 | + }) }; |
| 24 | +}); |
| 25 | +vi.mock('@/infrastructure/runtime', async importOriginal => ({ |
| 26 | + ...await importOriginal<typeof import('@/infrastructure/runtime')>(), supportsNativeWindowDragging: () => false, |
| 27 | +})); |
| 28 | +vi.mock('@/shared/utils/logger', () => ({ |
| 29 | + createLogger: () => ({ debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() }), |
| 30 | +})); |
| 31 | + |
| 32 | +globalThis.IS_REACT_ACT_ENVIRONMENT = true; |
| 33 | +it('reuses compiled tabs across parent renders and ignores history-only updates', async () => { |
| 34 | + resetSceneStoreForTest(); |
| 35 | + const tabs: SceneTab[] = Array.from({ length: 24 }, (_, i) => ({ |
| 36 | + id: `session:perf-${i}`, lastUsed: i, |
| 37 | + session: { surfaceId: 'local', workspaceKey: 'perf', sessionId: `perf-${i}` }, |
| 38 | + })); |
| 39 | + useSceneStore.setState({ ...createPaneState(tabs, tabs[0].id), activeTabId: tabs[0].id }); |
| 40 | + const container = document.createElement('div'); |
| 41 | + document.body.appendChild(container); |
| 42 | + const root = createRoot(container); |
| 43 | + const durations: number[] = []; |
| 44 | + const render = () => root.render(<Profiler id="scene-bar" onRender={(_id, _phase, duration) => durations.push(duration)}><SceneBar /></Profiler>); |
| 45 | + try { |
| 46 | + await act(async () => { render(); }); |
| 47 | + for (let i = 0; i < 10; i++) act(render); |
| 48 | + const measure = (name: string, update: (i: number) => void) => { |
| 49 | + captured.items.length = 0; |
| 50 | + durations.length = 0; |
| 51 | + for (let i = 0; i < 100; i++) act(() => update(i)); |
| 52 | + const ordered = [...durations].sort((a, b) => a - b); |
| 53 | + const result = { scenario: name, renders: captured.items.length, compilations: new Set(captured.items).size, |
| 54 | + totalRenderMs: +durations.reduce((a, b) => a + b, 0).toFixed(2), |
| 55 | + p50Ms: +(ordered[Math.floor(ordered.length * .5)] ?? 0).toFixed(3), |
| 56 | + p95Ms: +(ordered[Math.floor(ordered.length * .95)] ?? 0).toFixed(3) }; |
| 57 | + if (process.env.SCENE_BAR_BENCH) process.stdout.write(`${JSON.stringify(result)}\n`); |
| 58 | + return result; |
| 59 | + }; |
| 60 | + const parents = measure('parent-rerender', render); |
| 61 | + if (!process.env.SCENE_BAR_BASELINE) expect(parents.compilations).toBe(1); |
| 62 | + const history = measure('history-only', i => useSceneStore.setState({ navCursor: i })); |
| 63 | + if (!process.env.SCENE_BAR_BASELINE) expect(history.renders).toBe(0); |
| 64 | + // Model committed navigation, including the real lastUsed update. |
| 65 | + measure('selection-with-lastUsed', i => { |
| 66 | + const id = tabs[i % tabs.length].id; |
| 67 | + const openTabs = useSceneStore.getState().openTabs.map(tab => tab.id === id ? { ...tab, lastUsed: 1000 + i } : tab); |
| 68 | + useSceneStore.setState({ ...createPaneState(openTabs, id), activeTabId: id, navigationSequence: i }); |
| 69 | + }); |
| 70 | + } finally { |
| 71 | + act(() => root.unmount()); |
| 72 | + container.remove(); |
| 73 | + resetSceneStoreForTest(); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +it('invalidates cached file badges, titles, pinning, pane membership and device groups', async () => { |
| 78 | + resetSceneStoreForTest(); |
| 79 | + activateSurface('local'); |
| 80 | + const resource = useContentResourceStore.getState().open( |
| 81 | + { type: 'code-editor', title: 'before.ts', data: { filePath: '/project/before.ts' } }, |
| 82 | + { surfaceId: 'local', remoteConnectionId: 'ssh-fixture', workspaceId: 'project' }, |
| 83 | + ); |
| 84 | + const file: SceneTab = { id: `content:${resource}`, contentId: resource, lastUsed: 1 }; |
| 85 | + const peer: SceneTab = { id: 'session:peer-fixture', lastUsed: 2, |
| 86 | + session: { surfaceId: 'peer-fixture', workspaceKey: 'project', sessionId: 'remote' } }; |
| 87 | + useSceneStore.setState({ ...createPaneState([file, peer], file.id), activeTabId: file.id }); |
| 88 | + const container = document.createElement('div'); |
| 89 | + document.body.appendChild(container); |
| 90 | + const root = createRoot(container); |
| 91 | + const latest = () => captured.items[captured.items.length - 1]; |
| 92 | + try { |
| 93 | + await act(async () => root.render(<SceneBar />)); |
| 94 | + expect(latest().find(item => item.value === file.id)?.label).toBe('before.ts'); |
| 95 | + expect(container.querySelectorAll('[data-openbitfun-part="tabDevice"]')).toHaveLength(2); |
| 96 | + const initial = latest(); |
| 97 | + act(() => useContentResourceStore.getState().update(resource, { isDirty: true, fileMissing: true })); |
| 98 | + expect(latest()).not.toBe(initial); |
| 99 | + expect(latest().find(item => item.value === file.id)?.labelSuffix).toBeTruthy(); |
| 100 | + act(() => useContentResourceStore.getState().update(resource, { isDirty: false, fileMissing: false, |
| 101 | + content: { type: 'code-editor', title: 'after.ts', data: { filePath: '/project/after.ts' } } })); |
| 102 | + expect(latest().find(item => item.value === file.id)?.label).toBe('after.ts'); |
| 103 | + expect(latest().find(item => item.value === file.id)?.labelSuffix).toBeUndefined(); |
| 104 | + act(() => useSceneStore.getState().togglePinScene(file.id)); |
| 105 | + expect(latest().find(item => item.value === file.id)?.labelSuffix).toBeTruthy(); |
| 106 | + act(() => useDeviceDirectory.setState({ devices: [{ device_id: 'peer-fixture', device_name: 'Renamed peer' } as never] })); |
| 107 | + expect(container.textContent).toContain('Renamed peer'); |
| 108 | + act(() => activateSurface('peer-fixture')); |
| 109 | + expect(container.querySelectorAll('[data-openbitfun-part="tabDevice"]')).toHaveLength(0); |
| 110 | + const session: Session = { sessionId: 'remote', title: 'Remote title', config: {}, |
| 111 | + dialogTurns: [], status: 'idle', createdAt: 1, lastActiveAt: 1, error: null }; |
| 112 | + act(() => flowChatStore.setState(state => ({ ...state, sessions: new Map([['remote', session]]) }))); |
| 113 | + expect(latest().find(item => item.value === peer.id)?.label).toBe('Remote title'); |
| 114 | + act(() => flowChatStore.setState(state => ({ ...state, |
| 115 | + sessions: new Map([['remote', { ...session, title: 'Renamed session' }]]) }))); |
| 116 | + expect(latest().find(item => item.value === peer.id)?.label).toBe('Renamed session'); |
| 117 | + act(() => root.render(<SceneBar pane="secondary" />)); |
| 118 | + expect(latest()).toHaveLength(0); |
| 119 | + act(() => root.render(<SceneBar pane="primary" />)); |
| 120 | + expect(latest()).toHaveLength(2); |
| 121 | + } finally { |
| 122 | + act(() => root.unmount()); |
| 123 | + container.remove(); |
| 124 | + activateSurface('local'); |
| 125 | + flowChatStore.discardSurfaceState('peer-fixture'); |
| 126 | + useDeviceDirectory.setState({ devices: [], localId: null }); |
| 127 | + useContentResourceStore.getState().remove(resource); |
| 128 | + resetSceneStoreForTest(); |
| 129 | + } |
| 130 | +}); |
0 commit comments