|
| 1 | +/** @vitest-environment jsdom */ |
| 2 | +import { act } from 'react' |
| 3 | +import type { |
| 4 | + ComputerUseActivity, |
| 5 | + ComputerUseAppPermission, |
| 6 | + ComputerUseStatus, |
| 7 | +} from '@sim/desktop-bridge' |
| 8 | +import { toast } from '@sim/emcn' |
| 9 | +import { libDesktopMock, libDesktopMockFns } from '@sim/testing/mocks/lib-desktop.mock' |
| 10 | +import { createRoot, type Root } from 'react-dom/client' |
| 11 | +import { afterEach, beforeEach, expect, it, vi } from 'vitest' |
| 12 | +import { FeatureFlagsProvider } from '@/app/workspace/[workspaceId]/providers/feature-flags-provider' |
| 13 | +import { ComputerUseSettings } from '@/app/workspace/[workspaceId]/settings/components/desktop/computer-use' |
| 14 | + |
| 15 | +const native = vi.hoisted(() => ({ |
| 16 | + getStatus: vi.fn<() => Promise<ComputerUseStatus>>(), |
| 17 | + listAppPermissions: vi.fn<() => Promise<ComputerUseAppPermission[]>>(), |
| 18 | + revokeApp: vi.fn<(bundleId: string) => Promise<void>>(), |
| 19 | + cancel: vi.fn<() => Promise<void>>(), |
| 20 | + onActivity: vi.fn<(listener: (activity: ComputerUseActivity | null) => void) => () => void>(), |
| 21 | +})) |
| 22 | +vi.mock('@/lib/desktop', () => libDesktopMock) |
| 23 | + |
| 24 | +const initialPermissions = [ |
| 25 | + { bundleId: 'com.example.First', displayName: 'First app' }, |
| 26 | + { bundleId: 'com.example.Second', displayName: 'Second app' }, |
| 27 | +] |
| 28 | +let permissions: ComputerUseAppPermission[] |
| 29 | +let activity: ComputerUseActivity | null |
| 30 | +let listeners: Set<(activity: ComputerUseActivity | null) => void> |
| 31 | +let enabled: boolean |
| 32 | +let root: Root |
| 33 | +let container: HTMLDivElement |
| 34 | + |
| 35 | +beforeEach(() => { |
| 36 | + enabled = true |
| 37 | + permissions = [...initialPermissions] |
| 38 | + activity = null |
| 39 | + listeners = new Set() |
| 40 | + native.getStatus.mockImplementation(async () => ({ |
| 41 | + supported: true, |
| 42 | + enabled: true, |
| 43 | + permissions: { accessibility: true, screenCapture: true }, |
| 44 | + activeAction: activity, |
| 45 | + })) |
| 46 | + native.listAppPermissions.mockImplementation(async () => [...permissions]) |
| 47 | + native.revokeApp.mockImplementation(async (bundleId) => { |
| 48 | + permissions = permissions.filter((app) => app.bundleId !== bundleId) |
| 49 | + }) |
| 50 | + native.cancel.mockResolvedValue(undefined) |
| 51 | + native.onActivity.mockImplementation((listener) => { |
| 52 | + listeners.add(listener) |
| 53 | + return () => { |
| 54 | + listeners.delete(listener) |
| 55 | + } |
| 56 | + }) |
| 57 | + libDesktopMockFns.mockGetDesktopBridge.mockReturnValue({ computerUse: native }) |
| 58 | + vi.spyOn(toast, 'error').mockImplementation(() => '') |
| 59 | + vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true) |
| 60 | + container = document.createElement('div') |
| 61 | + document.body.append(container) |
| 62 | + root = createRoot(container) |
| 63 | +}) |
| 64 | + |
| 65 | +afterEach(async () => { |
| 66 | + await act(async () => root.unmount()) |
| 67 | + container.remove() |
| 68 | +}) |
| 69 | + |
| 70 | +function renderSettings() { |
| 71 | + root.render( |
| 72 | + <FeatureFlagsProvider |
| 73 | + flags={{ |
| 74 | + dashboards: false, |
| 75 | + 'table-row-ttl': false, |
| 76 | + 'mothership-model-selector': false, |
| 77 | + 'mothership-plan-mode': false, |
| 78 | + 'mothership-desktop-background-executor': false, |
| 79 | + 'mothership-computer-use': enabled, |
| 80 | + }} |
| 81 | + > |
| 82 | + <ComputerUseSettings /> |
| 83 | + </FeatureFlagsProvider> |
| 84 | + ) |
| 85 | +} |
| 86 | + |
| 87 | +it('keeps settings dormant while rollout is off, including focus and activity changes', async () => { |
| 88 | + enabled = false |
| 89 | + await act(async () => renderSettings()) |
| 90 | + await act(async () => window.dispatchEvent(new Event('focus'))) |
| 91 | + expect(native.getStatus).not.toHaveBeenCalled() |
| 92 | + expect(native.listAppPermissions).not.toHaveBeenCalled() |
| 93 | + expect(container.textContent).toBe('') |
| 94 | + |
| 95 | + enabled = true |
| 96 | + await act(async () => renderSettings()) |
| 97 | + expect(native.getStatus).toHaveBeenCalled() |
| 98 | + expect(appRow('com.example.First')).toBeDefined() |
| 99 | + await act(async () => { |
| 100 | + activity = { toolCallId: 'in-flight', scopeId: 'chat', action: 'click', startedAt: Date.now() } |
| 101 | + for (const listener of listeners) listener(activity) |
| 102 | + }) |
| 103 | + enabled = false |
| 104 | + await act(async () => renderSettings()) |
| 105 | + native.getStatus.mockClear() |
| 106 | + native.listAppPermissions.mockClear() |
| 107 | + await act(async () => { |
| 108 | + window.dispatchEvent(new Event('focus')) |
| 109 | + for (const listener of listeners) listener(activity) |
| 110 | + }) |
| 111 | + expect(native.getStatus).not.toHaveBeenCalled() |
| 112 | + expect(native.listAppPermissions).not.toHaveBeenCalled() |
| 113 | + const stop = container.querySelector<HTMLButtonElement>('button') |
| 114 | + expect(stop?.textContent).toContain('Stop') |
| 115 | + await act(async () => stop?.click()) |
| 116 | + expect(native.cancel).toHaveBeenCalledOnce() |
| 117 | +}) |
| 118 | + |
| 119 | +function appRow(bundleId: string) { |
| 120 | + return container.querySelector<HTMLElement>(`[title="${bundleId}"]`)?.parentElement |
| 121 | +} |
0 commit comments