|
| 1 | +import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 2 | +import { createServer, type Server } from 'node:http' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { fileURLToPath } from 'node:url' |
| 6 | +import { type ElectronApplication, _electron as electron, expect, test } from '@playwright/test' |
| 7 | +import type { SimDesktopApi } from '@sim/desktop-bridge' |
| 8 | + |
| 9 | +const DESKTOP_DIR = fileURLToPath(new URL('..', import.meta.url)) |
| 10 | + |
| 11 | +test('computer use reaches the actual helper only with canonical one-shot authorization', async () => { |
| 12 | + test.skip(process.platform !== 'darwin', 'The native helper is macOS-only.') |
| 13 | + const root = mkdtempSync(join(tmpdir(), 'sim-computer-use-e2e-')) |
| 14 | + let server: Server | undefined |
| 15 | + let app: ElectronApplication | undefined |
| 16 | + let claimed = false |
| 17 | + let authorizationCount = 0 |
| 18 | + let releaseAuthorization: () => void = () => {} |
| 19 | + let markAuthorizationStarted: () => void = () => {} |
| 20 | + const authorizationStarted = new Promise<void>((resolve) => { |
| 21 | + markAuthorizationStarted = resolve |
| 22 | + }) |
| 23 | + const authorizationReleased = new Promise<void>((resolve) => { |
| 24 | + releaseAuthorization = resolve |
| 25 | + }) |
| 26 | + try { |
| 27 | + server = createServer(async (request, response) => { |
| 28 | + const path = new URL(request.url ?? '/', 'http://127.0.0.1').pathname |
| 29 | + if (path === '/api/auth/get-session') { |
| 30 | + response.writeHead(200, { 'Content-Type': 'application/json' }).end( |
| 31 | + JSON.stringify({ |
| 32 | + user: { id: 'computer-test' }, |
| 33 | + session: { id: 'computer-test-session' }, |
| 34 | + }) |
| 35 | + ) |
| 36 | + return |
| 37 | + } |
| 38 | + if (path === '/api/desktop/computer/authorize') { |
| 39 | + authorizationCount += 1 |
| 40 | + let body = '' |
| 41 | + for await (const chunk of request) body += chunk.toString() |
| 42 | + const input = JSON.parse(body) |
| 43 | + expect(Object.keys(input)).toEqual(['toolCallId']) |
| 44 | + if (input.toolCallId === 'delayed') { |
| 45 | + markAuthorizationStarted() |
| 46 | + await authorizationReleased |
| 47 | + } else if (input.toolCallId !== 'status-once' || claimed) { |
| 48 | + response.writeHead(403, { 'Content-Type': 'application/json' }).end('{}') |
| 49 | + return |
| 50 | + } |
| 51 | + claimed = true |
| 52 | + response.writeHead(200, { 'Content-Type': 'application/json' }).end( |
| 53 | + JSON.stringify({ |
| 54 | + chatId: 'computer-chat', |
| 55 | + toolName: 'computer', |
| 56 | + args: { action: 'status' }, |
| 57 | + }) |
| 58 | + ) |
| 59 | + return |
| 60 | + } |
| 61 | + response |
| 62 | + .writeHead(200, { |
| 63 | + 'Content-Type': 'text/html', |
| 64 | + 'Set-Cookie': 'better-auth.session_token=fixture; HttpOnly; SameSite=Lax; Path=/', |
| 65 | + }) |
| 66 | + .end( |
| 67 | + `<!doctype html><title>Computer Use fixture</title><h1>Computer Use</h1><button id="disable">Disable Computer Use</button><output id="status"></output><script>document.getElementById('disable').onclick=async()=>{const state=await window.simDesktop.computerUse.setEnabled(false);document.getElementById('status').textContent=String(state.enabled)}</script>` |
| 68 | + ) |
| 69 | + }) |
| 70 | + await new Promise<void>((resolve) => server?.listen(0, '127.0.0.1', resolve)) |
| 71 | + const address = server.address() |
| 72 | + if (!address || typeof address === 'string') throw new Error('Missing local fixture port') |
| 73 | + const profile = join(root, 'profile') |
| 74 | + mkdirSync(profile) |
| 75 | + writeFileSync( |
| 76 | + join(profile, 'settings.json'), |
| 77 | + JSON.stringify({ origin: `http://127.0.0.1:${address.port}`, computerUseEnabled: true }) |
| 78 | + ) |
| 79 | + app = await electron.launch({ |
| 80 | + args: ['.'], |
| 81 | + cwd: DESKTOP_DIR, |
| 82 | + env: { |
| 83 | + ...process.env, |
| 84 | + SIM_DESKTOP_ORIGIN: `http://127.0.0.1:${address.port}`, |
| 85 | + SIM_DESKTOP_USER_DATA: profile, |
| 86 | + }, |
| 87 | + }) |
| 88 | + const window = await app.firstWindow() |
| 89 | + await expect(window.getByRole('heading')).toHaveText('Computer Use') |
| 90 | + const status = await window.evaluate(async () => { |
| 91 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 92 | + return api.computerUse?.getStatus() |
| 93 | + }) |
| 94 | + expect(status).toMatchObject({ supported: true, enabled: true, activeAction: null }) |
| 95 | + const result = await window.evaluate(async () => { |
| 96 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 97 | + return api.computerUse?.executeTool('status-once', { |
| 98 | + action: 'get_app_state', |
| 99 | + bundleId: 'com.apple.systempreferences', |
| 100 | + }) |
| 101 | + }) |
| 102 | + expect(result).toMatchObject({ kind: 'status', platform: 'darwin' }) |
| 103 | + const replay = await window.evaluate(async () => { |
| 104 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 105 | + try { |
| 106 | + await api.computerUse?.executeTool('status-once', { action: 'status' }) |
| 107 | + return 'unexpected success' |
| 108 | + } catch { |
| 109 | + return 'rejected' |
| 110 | + } |
| 111 | + }) |
| 112 | + expect(replay).toBe('rejected') |
| 113 | + expect(authorizationCount).toBe(2) |
| 114 | + const delayed = window.evaluate(async () => { |
| 115 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 116 | + try { |
| 117 | + await api.computerUse?.executeTool('delayed', { action: 'status' }) |
| 118 | + return 'unexpected success' |
| 119 | + } catch { |
| 120 | + return 'stopped' |
| 121 | + } |
| 122 | + }) |
| 123 | + await authorizationStarted |
| 124 | + await window.evaluate(async () => { |
| 125 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 126 | + await api.computerUse?.cancel('delayed') |
| 127 | + }) |
| 128 | + releaseAuthorization() |
| 129 | + expect(await delayed).toBe('stopped') |
| 130 | + expect(authorizationCount).toBe(3) |
| 131 | + await window.getByRole('button', { name: 'Disable Computer Use' }).click() |
| 132 | + await expect(window.locator('#status')).toHaveText('false') |
| 133 | + const disabled = await window.evaluate(async () => { |
| 134 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 135 | + try { |
| 136 | + await api.computerUse?.executeTool('disabled', { action: 'status' }) |
| 137 | + return 'unexpected success' |
| 138 | + } catch { |
| 139 | + return 'rejected' |
| 140 | + } |
| 141 | + }) |
| 142 | + expect(disabled).toBe('rejected') |
| 143 | + expect(authorizationCount).toBe(3) |
| 144 | + } finally { |
| 145 | + releaseAuthorization() |
| 146 | + await app?.close() |
| 147 | + await new Promise<void>((resolve) => (server ? server.close(() => resolve()) : resolve())) |
| 148 | + rmSync(root, { recursive: true, force: true }) |
| 149 | + } |
| 150 | +}) |
0 commit comments