|
| 1 | +import { existsSync } from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { fileURLToPath } from "node:url"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { |
| 6 | + NodeRuntime, |
| 7 | + allowAll, |
| 8 | + createNodeDriver, |
| 9 | + createNodeRuntimeDriverFactory, |
| 10 | +} from "../../src/index.js"; |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 13 | +const SECURE_EXEC_ROOT = path.resolve(__dirname, "../.."); |
| 14 | +const PI_CONFIG_ENTRY = path.resolve( |
| 15 | + SECURE_EXEC_ROOT, |
| 16 | + "node_modules/@mariozechner/pi-coding-agent/dist/config.js", |
| 17 | +); |
| 18 | + |
| 19 | +function skipUnlessPiInstalled(): string | false { |
| 20 | + return existsSync(PI_CONFIG_ENTRY) |
| 21 | + ? false |
| 22 | + : "@mariozechner/pi-coding-agent not installed"; |
| 23 | +} |
| 24 | + |
| 25 | +function parseLastJsonLine(stdout: string): Record<string, unknown> { |
| 26 | + const line = stdout |
| 27 | + .trim() |
| 28 | + .split("\n") |
| 29 | + .map((entry) => entry.trim()) |
| 30 | + .filter(Boolean) |
| 31 | + .at(-1); |
| 32 | + |
| 33 | + if (!line) { |
| 34 | + throw new Error(`sandbox produced no JSON output: ${JSON.stringify(stdout)}`); |
| 35 | + } |
| 36 | + |
| 37 | + return JSON.parse(line) as Record<string, unknown>; |
| 38 | +} |
| 39 | + |
| 40 | +describe.skipIf(skipUnlessPiInstalled())("Pi SDK bootstrap in NodeRuntime", () => { |
| 41 | + let runtime: NodeRuntime | undefined; |
| 42 | + |
| 43 | + afterEach(async () => { |
| 44 | + await runtime?.terminate(); |
| 45 | + runtime = undefined; |
| 46 | + }); |
| 47 | + |
| 48 | + it("resolves Pi package assets from the package root after config bootstrap", async () => { |
| 49 | + const stdout: string[] = []; |
| 50 | + const stderr: string[] = []; |
| 51 | + |
| 52 | + runtime = new NodeRuntime({ |
| 53 | + onStdio: (event) => { |
| 54 | + if (event.channel === "stdout") stdout.push(event.message); |
| 55 | + if (event.channel === "stderr") stderr.push(event.message); |
| 56 | + }, |
| 57 | + systemDriver: createNodeDriver({ |
| 58 | + moduleAccess: { cwd: SECURE_EXEC_ROOT }, |
| 59 | + permissions: allowAll, |
| 60 | + }), |
| 61 | + runtimeDriverFactory: createNodeRuntimeDriverFactory(), |
| 62 | + }); |
| 63 | + |
| 64 | + const result = await runtime.exec( |
| 65 | + ` |
| 66 | + const fs = require("node:fs"); |
| 67 | + (async () => { |
| 68 | + try { |
| 69 | + const config = await import(${JSON.stringify(PI_CONFIG_ENTRY)}); |
| 70 | + const packageJsonPath = config.getPackageJsonPath(); |
| 71 | + const readmePath = config.getReadmePath(); |
| 72 | + const themesDir = config.getThemesDir(); |
| 73 | + console.log(JSON.stringify({ |
| 74 | + ok: true, |
| 75 | + appName: config.APP_NAME, |
| 76 | + version: config.VERSION, |
| 77 | + packageJsonPath, |
| 78 | + packageJsonExists: fs.existsSync(packageJsonPath), |
| 79 | + readmePath, |
| 80 | + readmeExists: fs.existsSync(readmePath), |
| 81 | + themesDir, |
| 82 | + themesDirExists: fs.existsSync(themesDir), |
| 83 | + })); |
| 84 | + } catch (error) { |
| 85 | + console.log(JSON.stringify({ |
| 86 | + ok: false, |
| 87 | + error: String(error), |
| 88 | + stack: error && typeof error === "object" && "stack" in error ? error.stack : undefined, |
| 89 | + })); |
| 90 | + process.exitCode = 1; |
| 91 | + } |
| 92 | + })(); |
| 93 | + `, |
| 94 | + { cwd: SECURE_EXEC_ROOT }, |
| 95 | + ); |
| 96 | + |
| 97 | + expect(result.code, stderr.join("")).toBe(0); |
| 98 | + |
| 99 | + const payload = parseLastJsonLine(stdout.join("")); |
| 100 | + expect(payload.ok).toBe(true); |
| 101 | + expect(payload.appName).toBe("pi"); |
| 102 | + expect(payload.version).toBe("0.60.0"); |
| 103 | + expect(payload.packageJsonExists).toBe(true); |
| 104 | + expect(String(payload.packageJsonPath)).toMatch( |
| 105 | + /node_modules\/@mariozechner\/pi-coding-agent\/package\.json$/, |
| 106 | + ); |
| 107 | + expect(String(payload.packageJsonPath)).not.toMatch(/\/dist\/package\.json$/); |
| 108 | + expect(payload.readmeExists).toBe(true); |
| 109 | + expect(String(payload.readmePath)).toMatch( |
| 110 | + /node_modules\/@mariozechner\/pi-coding-agent\/README\.md$/, |
| 111 | + ); |
| 112 | + expect(payload.themesDirExists).toBe(true); |
| 113 | + expect(String(payload.themesDir)).toMatch( |
| 114 | + /node_modules\/@mariozechner\/pi-coding-agent\/dist\/modes\/interactive\/theme$/, |
| 115 | + ); |
| 116 | + }); |
| 117 | +}); |
0 commit comments