|
| 1 | +import { execFile } from 'node:child_process' |
| 2 | +import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from 'node:fs/promises' |
| 3 | +import { tmpdir } from 'node:os' |
| 4 | +import { join } from 'node:path' |
| 5 | +import { promisify } from 'node:util' |
| 6 | +import { describe, expect, it } from 'vitest' |
| 7 | +import { PI_EVENT_FILTER_SOURCE } from '@/executor/handlers/pi/cloud/event-filter-source' |
| 8 | +import { buildPiScript } from '@/executor/handlers/pi/cloud/shared' |
| 9 | +import { PI_PACKAGE_VERSION } from '@/scripts/pi-sandbox-packages' |
| 10 | + |
| 11 | +const exec = promisify(execFile) |
| 12 | + |
| 13 | +async function invokeSandbox(version: string) { |
| 14 | + const root = await mkdtemp(join(tmpdir(), 'sim-pi-cli-')) |
| 15 | + const workspace = join(root, 'workspace') |
| 16 | + const repo = join(workspace, 'repo') |
| 17 | + const bin = join(root, 'bin') |
| 18 | + await mkdir(repo, { recursive: true }) |
| 19 | + await mkdir(bin) |
| 20 | + await writeFile(join(workspace, 'pi-prompt.txt'), 'fixture prompt') |
| 21 | + await writeFile(join(workspace, 'sim-pi-event-filter.mjs'), PI_EVENT_FILTER_SOURCE) |
| 22 | + await writeFile( |
| 23 | + join(bin, 'pi'), |
| 24 | + `#!/bin/bash |
| 25 | +if [ "$1" = "--version" ]; then |
| 26 | + printf "%s\\n" "${version}" |
| 27 | + exit 0 |
| 28 | +fi |
| 29 | +printf "%s" "$PI_CODING_AGENT_DIR" > "${root}/invoked" |
| 30 | +printf "%s\\n" '{"type":"message_update","assistantMessageEvent":{"type":"text_delta","delta":"ok"}}' |
| 31 | +exit 0 |
| 32 | +`, |
| 33 | + { mode: 0o700 } |
| 34 | + ) |
| 35 | + const script = buildPiScript().replaceAll('/workspace', workspace) |
| 36 | + try { |
| 37 | + const result = await exec('/bin/bash', ['-c', script], { |
| 38 | + env: { |
| 39 | + ...process.env, |
| 40 | + PATH: `${bin}:${process.env.PATH}`, |
| 41 | + PI_PROVIDER: 'fixture', |
| 42 | + PI_MODEL: 'fixture', |
| 43 | + PI_THINKING: 'off', |
| 44 | + }, |
| 45 | + }).then( |
| 46 | + ({ stdout, stderr }) => ({ code: 0, stdout, stderr }), |
| 47 | + (error: { code: number; stdout: string; stderr: string }) => error |
| 48 | + ) |
| 49 | + const invoked = await readFile(join(root, 'invoked'), 'utf8').catch(() => null) |
| 50 | + const settings = invoked |
| 51 | + ? await readFile(join(invoked, 'settings.json'), 'utf8').catch(() => null) |
| 52 | + : null |
| 53 | + const storedFiles = invoked ? await readdir(invoked).catch(() => []) : [] |
| 54 | + return { ...result, invoked, settings, storedFiles, repo } |
| 55 | + } finally { |
| 56 | + await rm(root, { recursive: true, force: true }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +describe('sandbox Pi runtime boundary', () => { |
| 61 | + it.each(['0.80.10', 'unexpected-version'])( |
| 62 | + 'rejects %s before starting the agent', |
| 63 | + async (version) => { |
| 64 | + const result = await invokeSandbox(version) |
| 65 | + expect(result.code).not.toBe(0) |
| 66 | + expect(result.invoked).toBeNull() |
| 67 | + expect(result.stderr).toMatch(/rebuild|update/i) |
| 68 | + expect(result.stderr).toContain(PI_PACKAGE_VERSION) |
| 69 | + } |
| 70 | + ) |
| 71 | + |
| 72 | + it('uses private settings outside the repository with warming off and no saved credentials', async () => { |
| 73 | + const result = await invokeSandbox(PI_PACKAGE_VERSION) |
| 74 | + expect(result.code).toBe(0) |
| 75 | + expect(result.invoked).toBeTruthy() |
| 76 | + expect(result.invoked?.startsWith(result.repo)).toBe(false) |
| 77 | + expect(JSON.parse(result.settings ?? '{}')).toEqual({ cacheWarming: 'off' }) |
| 78 | + expect(result.storedFiles).toEqual(['settings.json']) |
| 79 | + expect(JSON.parse(result.stdout.trim())).toEqual({ |
| 80 | + type: 'message_update', |
| 81 | + assistantMessageEvent: { type: 'text_delta', delta: 'ok' }, |
| 82 | + }) |
| 83 | + }) |
| 84 | +}) |
0 commit comments