|
| 1 | +import { execFile } from 'node:child_process' |
| 2 | +import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises' |
| 3 | +import * as http from 'node:http' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { dirname, join } from 'node:path' |
| 6 | +import { fileURLToPath } from 'node:url' |
| 7 | +import { promisify } from 'node:util' |
| 8 | +import { describe, expect, it } from 'vitest' |
| 9 | +import { PI_EVENT_FILTER_SOURCE } from '@/executor/handlers/pi/cloud/event-filter-source' |
| 10 | +import { buildPiScript } from '@/executor/handlers/pi/cloud/shared' |
| 11 | +import { |
| 12 | + FIXTURE_KEY, |
| 13 | + startPiProviderFixture, |
| 14 | +} from '@/executor/handlers/pi/core/__fixtures__/provider' |
| 15 | +import { applyPiEvent, createPiTotals, parseJsonLine } from '@/executor/handlers/pi/core/events' |
| 16 | +import { PI_SEARCH_EXTENSION_SOURCE } from '@/executor/handlers/pi/search/extension-source' |
| 17 | + |
| 18 | +const exec = promisify(execFile) |
| 19 | + |
| 20 | +describe('installed Pi CLI wire', () => { |
| 21 | + it('loads explicit Sim search with --no-extensions and keeps warming disabled', async () => { |
| 22 | + const root = await mkdtemp(join(tmpdir(), 'sim-pi-installed-cli-')) |
| 23 | + const workspace = join(root, 'workspace') |
| 24 | + const fixture = await startPiProviderFixture(http) |
| 25 | + try { |
| 26 | + const repo = join(workspace, 'repo') |
| 27 | + const bin = join(root, 'bin') |
| 28 | + const providerPath = join(root, 'provider.ts') |
| 29 | + const searchPath = join(workspace, 'sim-search-extension.ts') |
| 30 | + const preload = join(root, 'preload.mjs') |
| 31 | + await mkdir(join(repo, '.pi', 'extensions'), { recursive: true }) |
| 32 | + await mkdir(bin) |
| 33 | + await writeFile( |
| 34 | + join(repo, '.pi', 'extensions', 'poison.ts'), |
| 35 | + 'throw new Error("repository extension loaded")' |
| 36 | + ) |
| 37 | + await writeFile(join(workspace, 'pi-prompt.txt'), 'search-fixture') |
| 38 | + await writeFile(join(workspace, 'sim-pi-event-filter.mjs'), PI_EVENT_FILTER_SOURCE) |
| 39 | + await writeFile(searchPath, PI_SEARCH_EXTENSION_SOURCE) |
| 40 | + await writeFile( |
| 41 | + providerPath, |
| 42 | + `export default function(pi) { pi.registerProvider("fixture", ${JSON.stringify({ ...fixture.provider, apiKey: FIXTURE_KEY })}) }` |
| 43 | + ) |
| 44 | + await writeFile( |
| 45 | + preload, |
| 46 | + `const guard = (originalFetch) => (input, init) => { |
| 47 | + const url = new URL(typeof input === "string" ? input : input instanceof URL ? input.href : input.url); |
| 48 | + if (url.origin === "https://api.exa.ai" && url.pathname === "/search") return originalFetch(${JSON.stringify(`${fixture.baseUrl}/exa`)}, init); |
| 49 | + if (url.origin === ${JSON.stringify(fixture.baseUrl)}) return originalFetch(input, init); |
| 50 | + throw new Error("Unexpected external fixture traffic: " + url.origin); |
| 51 | +}; |
| 52 | +let guardedFetch = guard(globalThis.fetch); |
| 53 | +Object.defineProperty(globalThis, "fetch", { configurable: true, get: () => guardedFetch, set: (fetch) => { guardedFetch = guard(fetch); } });` |
| 54 | + ) |
| 55 | + const cli = join( |
| 56 | + dirname(fileURLToPath(import.meta.resolve('@earendil-works/pi-coding-agent'))), |
| 57 | + 'bundle', |
| 58 | + 'cli.js' |
| 59 | + ) |
| 60 | + await writeFile(join(bin, 'pi'), `#!/bin/bash\nexec node "${cli}" "$@"\n`, { mode: 0o700 }) |
| 61 | + const script = buildPiScript('/workspace/sim-search-extension.ts', { |
| 62 | + disableRepositoryResources: true, |
| 63 | + }) |
| 64 | + .replaceAll('/workspace', workspace) |
| 65 | + .replace(' < ', ` -e ${providerPath} < `) |
| 66 | + const { stdout } = await exec('/bin/bash', ['-c', script], { |
| 67 | + env: { |
| 68 | + ...process.env, |
| 69 | + PATH: `${bin}:${process.env.PATH}`, |
| 70 | + NODE_OPTIONS: `--import ${preload}`, |
| 71 | + PI_OFFLINE: '1', |
| 72 | + PI_PROVIDER: 'fixture', |
| 73 | + PI_MODEL: 'fixture-model', |
| 74 | + PI_THINKING: 'medium', |
| 75 | + SIM_SEARCH_PROVIDER: 'exa', |
| 76 | + SIM_SEARCH_API_KEY: FIXTURE_KEY, |
| 77 | + }, |
| 78 | + timeout: 15_000, |
| 79 | + }) |
| 80 | + const totals = createPiTotals() |
| 81 | + for (const line of stdout.split('\n')) { |
| 82 | + const event = parseJsonLine(line) |
| 83 | + if (event) applyPiEvent(totals, event) |
| 84 | + } |
| 85 | + expect(JSON.stringify(fixture.requests.at(-1)?.body.messages)).toContain( |
| 86 | + 'Offline search evidence' |
| 87 | + ) |
| 88 | + expect(totals).toEqual({ |
| 89 | + finalText: 'fixture complete', |
| 90 | + inputTokens: 14, |
| 91 | + outputTokens: 6, |
| 92 | + toolCalls: [{ name: 'web_search', isError: false }], |
| 93 | + }) |
| 94 | + expect(fixture.requests.map(({ path }) => path)).toEqual([ |
| 95 | + '/v1/chat/completions', |
| 96 | + '/exa', |
| 97 | + '/v1/chat/completions', |
| 98 | + ]) |
| 99 | + const agentDir = join(workspace, 'sim-pi-agent') |
| 100 | + expect(JSON.parse(await readFile(join(agentDir, 'settings.json'), 'utf8'))).toEqual({ |
| 101 | + cacheWarming: 'off', |
| 102 | + }) |
| 103 | + expect(JSON.parse(await readFile(join(agentDir, 'auth.json'), 'utf8'))).toEqual({}) |
| 104 | + } finally { |
| 105 | + await fixture.close() |
| 106 | + await rm(root, { recursive: true, force: true }) |
| 107 | + } |
| 108 | + }, 30_000) |
| 109 | +}) |
0 commit comments