|
| 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 { DesktopLocalFileRequest, SimDesktopApi } from '@sim/desktop-bridge' |
| 8 | + |
| 9 | +const DESKTOP_DIR = fileURLToPath(new URL('..', import.meta.url)) |
| 10 | + |
| 11 | +test('native file tools read and import through the installed preload without Sim folder grants', async () => { |
| 12 | + const root = mkdtempSync(join(tmpdir(), 'sim-native-files-e2e-')) |
| 13 | + const source = join(root, 'Reports') |
| 14 | + mkdirSync(join(source, 'empty'), { recursive: true }) |
| 15 | + writeFileSync(join(source, 'report.txt'), 'native file contents') |
| 16 | + const png = |
| 17 | + 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAusB9Y9Zl1sAAAAASUVORK5CYII=' |
| 18 | + writeFileSync(join(source, 'image.png'), Buffer.from(png, 'base64')) |
| 19 | + let claimed = false |
| 20 | + const calls: Record<string, { toolName: string; args: Record<string, unknown> }> = { |
| 21 | + text: { toolName: 'read_local_file', args: { path: join(source, 'report.txt') } }, |
| 22 | + image: { toolName: 'read_local_file', args: { path: join(source, 'image.png') } }, |
| 23 | + import: { |
| 24 | + toolName: 'import_local_files', |
| 25 | + args: { path: source, targetWorkspaceId: 'target-workspace' }, |
| 26 | + }, |
| 27 | + } |
| 28 | + let server: Server | undefined |
| 29 | + let app: ElectronApplication | undefined |
| 30 | + try { |
| 31 | + server = createServer(async (request, response) => { |
| 32 | + const path = new URL(request.url ?? '/', 'http://127.0.0.1').pathname |
| 33 | + if (path === '/api/auth/get-session') { |
| 34 | + response.writeHead(200, { 'Content-Type': 'application/json' }).end( |
| 35 | + JSON.stringify({ |
| 36 | + user: { id: 'local-file-user' }, |
| 37 | + session: { id: 'local-file-session' }, |
| 38 | + }) |
| 39 | + ) |
| 40 | + return |
| 41 | + } |
| 42 | + if (path === '/api/desktop/tool/authorize') { |
| 43 | + let body = '' |
| 44 | + for await (const chunk of request) body += chunk.toString() |
| 45 | + const input = JSON.parse(body) |
| 46 | + const call = calls[input.toolCallId] |
| 47 | + if (!call || (input.claim && claimed)) { |
| 48 | + response.writeHead(call ? 409 : 403, { 'Content-Type': 'application/json' }).end('{}') |
| 49 | + return |
| 50 | + } |
| 51 | + if (input.claim) claimed = true |
| 52 | + response |
| 53 | + .writeHead(200, { 'Content-Type': 'application/json' }) |
| 54 | + .end(JSON.stringify({ ...call, chatId: 'org-chat' })) |
| 55 | + return |
| 56 | + } |
| 57 | + response |
| 58 | + .writeHead(200, { |
| 59 | + 'Content-Type': 'text/html', |
| 60 | + 'Set-Cookie': 'better-auth.session_token=fixture; HttpOnly; SameSite=Lax; Path=/', |
| 61 | + }) |
| 62 | + .end('<!doctype html><title>Local file fixture</title><h1>Local files</h1>') |
| 63 | + }) |
| 64 | + await new Promise<void>((resolve) => server?.listen(0, '127.0.0.1', resolve)) |
| 65 | + const address = server.address() |
| 66 | + if (!address || typeof address === 'string') throw new Error('Missing fixture address') |
| 67 | + app = await electron.launch({ |
| 68 | + args: ['.'], |
| 69 | + cwd: DESKTOP_DIR, |
| 70 | + env: { |
| 71 | + ...process.env, |
| 72 | + SIM_DESKTOP_ORIGIN: `http://127.0.0.1:${address.port}`, |
| 73 | + SIM_DESKTOP_USER_DATA: join(root, 'profile'), |
| 74 | + }, |
| 75 | + }) |
| 76 | + const window = await app.firstWindow() |
| 77 | + await expect(window.getByRole('heading')).toHaveText('Local files') |
| 78 | + const invoke = (input: DesktopLocalFileRequest) => |
| 79 | + window.evaluate(async (request) => { |
| 80 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 81 | + if (!api.localFiles) throw new Error('Native file bridge missing') |
| 82 | + return api.localFiles(request) |
| 83 | + }, input) |
| 84 | + await expect |
| 85 | + .poll(async () => (await invoke({ operation: 'read', toolCallId: 'text' })).ok) |
| 86 | + .toBe(true) |
| 87 | + expect(await invoke({ operation: 'read', toolCallId: 'text' })).toMatchObject({ |
| 88 | + ok: true, |
| 89 | + data: { representation: 'text', text: 'native file contents' }, |
| 90 | + }) |
| 91 | + expect(await invoke({ operation: 'read', toolCallId: 'image' })).toMatchObject({ |
| 92 | + ok: true, |
| 93 | + data: { observations: [{ mediaType: 'image/png', data: png }] }, |
| 94 | + }) |
| 95 | + const result = await invoke({ operation: 'manifest', toolCallId: 'import' }) |
| 96 | + if (!result.ok || result.data.kind !== 'manifest') throw new Error(JSON.stringify(result)) |
| 97 | + expect(result.data.targetWorkspaceId).toBe('target-workspace') |
| 98 | + expect(result.data.entries.map((entry) => entry.relativePath)).toEqual([ |
| 99 | + '', |
| 100 | + 'empty', |
| 101 | + 'image.png', |
| 102 | + 'report.txt', |
| 103 | + ]) |
| 104 | + const file = result.data.entries.find((entry) => entry.relativePath === 'report.txt') |
| 105 | + if (!file) throw new Error('Missing import file') |
| 106 | + const chunk = await invoke({ |
| 107 | + operation: 'chunk', |
| 108 | + toolCallId: 'import', |
| 109 | + relativePath: file.relativePath, |
| 110 | + revision: file.revision, |
| 111 | + offset: 0, |
| 112 | + }) |
| 113 | + if (!chunk.ok || chunk.data.kind !== 'chunk') throw new Error(JSON.stringify(chunk)) |
| 114 | + expect(Object.values(chunk.data.bytes)).toEqual([...Buffer.from('native file contents')]) |
| 115 | + expect(chunk.data.eof).toBe(true) |
| 116 | + expect( |
| 117 | + await window.evaluate( |
| 118 | + async (input) => { |
| 119 | + const api = (globalThis as typeof globalThis & { simDesktop: SimDesktopApi }).simDesktop |
| 120 | + const response = await api.localFiles?.(input) |
| 121 | + if (!response?.ok || response.data.kind !== 'chunk') |
| 122 | + throw new Error('Missing native chunk') |
| 123 | + const file = new File([new Uint8Array(response.data.bytes)], 'report.txt') |
| 124 | + return file.text() |
| 125 | + }, |
| 126 | + { |
| 127 | + operation: 'chunk' as const, |
| 128 | + toolCallId: 'import', |
| 129 | + relativePath: file.relativePath, |
| 130 | + revision: file.revision, |
| 131 | + offset: 0, |
| 132 | + } |
| 133 | + ) |
| 134 | + ).toBe('native file contents') |
| 135 | + expect(await invoke({ operation: 'manifest', toolCallId: 'import' })).toMatchObject({ |
| 136 | + ok: false, |
| 137 | + code: 'ALREADY_STARTED', |
| 138 | + }) |
| 139 | + } finally { |
| 140 | + await app?.close() |
| 141 | + server?.close() |
| 142 | + rmSync(root, { recursive: true, force: true }) |
| 143 | + } |
| 144 | +}) |
0 commit comments