|
| 1 | +/** |
| 2 | + * @vitest-environment jsdom |
| 3 | + */ |
| 4 | +import { act } from 'react' |
| 5 | +import type { BrowserPageState } from '@sim/browser-protocol' |
| 6 | +import type { BrowserToolbarCommand } from '@sim/desktop-bridge' |
| 7 | +import { createRoot, type Root } from 'react-dom/client' |
| 8 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 9 | + |
| 10 | +const { desktop, navigateToSettings, removeResource } = vi.hoisted(() => ({ |
| 11 | + navigateToSettings: vi.fn(), |
| 12 | + removeResource: vi.fn(), |
| 13 | + desktop: { |
| 14 | + settings: { getPreferences: vi.fn(async () => ({ browserTheme: 'app' })) }, |
| 15 | + browserCredentials: { list: vi.fn(async () => []), onFillAvailability: vi.fn(() => () => {}) }, |
| 16 | + browserImport: { |
| 17 | + listChromeProfiles: vi.fn(async () => []), |
| 18 | + listSites: vi.fn(async () => []), |
| 19 | + importFromChrome: vi.fn(), |
| 20 | + }, |
| 21 | + browserAgent: { |
| 22 | + supportsAtomicPanelOcclusion: true, |
| 23 | + setTheme: vi.fn(), |
| 24 | + setPanelBounds: vi.fn(), |
| 25 | + setPanelFocused: vi.fn(), |
| 26 | + setPanelOccluded: vi.fn(async () => true), |
| 27 | + capturePanelSnapshot: vi.fn(async () => null), |
| 28 | + getKnownSessions: vi.fn(async () => ({ sessions: [] })), |
| 29 | + getDownloadsState: vi.fn(async () => ({ downloads: [] })), |
| 30 | + onAppearanceThemeChanged: vi.fn(() => () => {}), |
| 31 | + onToolbarCommand: vi.fn( |
| 32 | + (_callback: (command: BrowserToolbarCommand, scopeId: string) => void) => () => {} |
| 33 | + ), |
| 34 | + onAddToChat: vi.fn(() => () => {}), |
| 35 | + onFocusOmnibox: vi.fn(() => () => {}), |
| 36 | + onOpenFind: vi.fn(() => () => {}), |
| 37 | + onCloseFind: vi.fn(() => () => {}), |
| 38 | + onDownloadsState: vi.fn(() => () => {}), |
| 39 | + }, |
| 40 | + }, |
| 41 | +})) |
| 42 | + |
| 43 | +vi.mock('@/lib/desktop', () => ({ getDesktopBridge: () => desktop })) |
| 44 | +vi.mock('@/hooks/use-settings-navigation', () => ({ |
| 45 | + useSettingsNavigation: () => ({ navigateToSettings }), |
| 46 | +})) |
| 47 | +vi.mock('@/app/workspace/[workspaceId]/home/components/mothership-resources-context', () => ({ |
| 48 | + useMothershipResources: () => ({ removeResource }), |
| 49 | +})) |
| 50 | + |
| 51 | +import { BrowserSession } from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/components/browser-session/browser-session' |
| 52 | +import { useBrowserSessionStore } from '@/stores/browser-session/store' |
| 53 | + |
| 54 | +const PAGE: BrowserPageState = { |
| 55 | + scopeId: 'browser-ui-test', |
| 56 | + tabId: 'tab-1', |
| 57 | + url: 'about:blank', |
| 58 | + title: '', |
| 59 | + loading: false, |
| 60 | + canGoBack: false, |
| 61 | + canGoForward: false, |
| 62 | +} |
| 63 | +let container: HTMLDivElement |
| 64 | +let root: Root |
| 65 | + |
| 66 | +async function render(page: BrowserPageState = PAGE, visible = true) { |
| 67 | + await act(async () => { |
| 68 | + useBrowserSessionStore.getState().setPageState(page) |
| 69 | + root.render(<BrowserSession visible={visible} scopeId={PAGE.scopeId} />) |
| 70 | + }) |
| 71 | + await act(async () => vi.advanceTimersByTime(20)) |
| 72 | +} |
| 73 | + |
| 74 | +beforeEach(() => { |
| 75 | + ;(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true |
| 76 | + vi.useFakeTimers() |
| 77 | + vi.stubGlobal( |
| 78 | + 'ResizeObserver', |
| 79 | + class { |
| 80 | + observe() {} |
| 81 | + disconnect() {} |
| 82 | + unobserve() {} |
| 83 | + } |
| 84 | + ) |
| 85 | + vi.clearAllMocks() |
| 86 | + useBrowserSessionStore.setState({ sessions: {} }) |
| 87 | + container = document.createElement('div') |
| 88 | + document.body.appendChild(container) |
| 89 | + root = createRoot(container) |
| 90 | + vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({ |
| 91 | + x: 0, |
| 92 | + y: 0, |
| 93 | + width: 700, |
| 94 | + height: 600, |
| 95 | + top: 0, |
| 96 | + bottom: 600, |
| 97 | + left: 0, |
| 98 | + right: 700, |
| 99 | + toJSON: () => ({}), |
| 100 | + }) |
| 101 | +}) |
| 102 | + |
| 103 | +afterEach(() => { |
| 104 | + act(() => root.unmount()) |
| 105 | + container.remove() |
| 106 | + vi.useRealTimers() |
| 107 | + vi.restoreAllMocks() |
| 108 | +}) |
| 109 | + |
| 110 | +describe('browser empty state and in-place import', () => { |
| 111 | + it('replaces the native blank page with shared guidance and no extra actions', async () => { |
| 112 | + await render() |
| 113 | + const emptyState = container.querySelector('section[aria-label="New tab"]') |
| 114 | + expect(emptyState?.textContent).toContain('Browse the web') |
| 115 | + expect(emptyState?.textContent).toContain('Search or enter a URL above.') |
| 116 | + expect(emptyState?.querySelector('button')).toBeNull() |
| 117 | + expect(desktop.browserAgent.setPanelBounds).toHaveBeenLastCalledWith(null, null, PAGE.scopeId) |
| 118 | + expect(container.querySelector('input')).not.toBeNull() |
| 119 | + }) |
| 120 | + |
| 121 | + it('hands the surface back to the native page for navigation and restores it on a blank tab', async () => { |
| 122 | + await render() |
| 123 | + await render({ ...PAGE, loading: true }) |
| 124 | + expect(container.querySelector('section[aria-label="New tab"]')).toBeNull() |
| 125 | + expect(desktop.browserAgent.setPanelBounds.mock.calls.at(-1)?.[0]).toEqual( |
| 126 | + expect.objectContaining({ width: 700, height: 600 }) |
| 127 | + ) |
| 128 | + await render({ ...PAGE, url: 'https://example.com' }) |
| 129 | + expect(container.querySelector('section[aria-label="New tab"]')).toBeNull() |
| 130 | + await render() |
| 131 | + expect(container.querySelector('section[aria-label="New tab"]')).not.toBeNull() |
| 132 | + expect(desktop.browserAgent.setPanelBounds).toHaveBeenLastCalledWith(null, null, PAGE.scopeId) |
| 133 | + }) |
| 134 | + |
| 135 | + it('does not replace load errors with the new-tab state', async () => { |
| 136 | + await render({ |
| 137 | + ...PAGE, |
| 138 | + issue: { |
| 139 | + kind: 'load-error', |
| 140 | + url: 'https://missing.example', |
| 141 | + code: -105, |
| 142 | + description: 'ERR_NAME_NOT_RESOLVED', |
| 143 | + }, |
| 144 | + }) |
| 145 | + expect(container.textContent).toContain("This site can't be reached") |
| 146 | + expect(container.querySelector('section[aria-label="New tab"]')).toBeNull() |
| 147 | + }) |
| 148 | + |
| 149 | + it('opens import in place from the native toolbar command', async () => { |
| 150 | + await render() |
| 151 | + const callback = desktop.browserAgent.onToolbarCommand.mock.calls[0][0] |
| 152 | + await act(async () => callback('import', PAGE.scopeId)) |
| 153 | + expect(document.querySelector('[role="dialog"]')?.textContent).toContain( |
| 154 | + 'Import from your browser' |
| 155 | + ) |
| 156 | + expect(navigateToSettings).not.toHaveBeenCalled() |
| 157 | + expect(desktop.browserImport.listChromeProfiles).toHaveBeenCalledOnce() |
| 158 | + expect(document.querySelector<HTMLElement>('[role="dialog"]')?.style.visibility).not.toBe( |
| 159 | + 'hidden' |
| 160 | + ) |
| 161 | + expect(desktop.browserAgent.setPanelOccluded).toHaveBeenCalledWith(true, PAGE.scopeId, true) |
| 162 | + }) |
| 163 | +}) |
0 commit comments