import * as fs from "node:fs"; import path from "node:path"; import { createTestSession, type TestSession, } from "@marcfargas/pi-test-harness"; import { afterEach, describe, expect, it } from "vitest"; import { __test } from "../index.ts"; describe("tune pi-test-harness", () => { let t: TestSession | undefined; async function activeTune() { const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); fs.mkdirSync(path.join(t.cwd, ".pi", "tune"), { recursive: true }); fs.writeFileSync( path.join(t.cwd, ".pi", "tune", "session.jsonl"), '{"type":"config","name":"runtime","metricName":"score","metricUnit":"","bestDirection":"lower"}\n', ); __test.acquireTuneLock(t.cwd, t.session.sessionManager.getSessionId()); await t.session.extensionRunner.emit({ type: "session_tree", newLeafId: null, oldLeafId: null, }); return t.session.extensionRunner.extensions[0]; } afterEach(() => { t?.dispose(); t = undefined; }); it("loads in real Pi extension runtime", async () => { const extensionPath = path.resolve(import.meta.dirname, "../index.ts"); t = await createTestSession({ extensions: [extensionPath] }); expect(t.session.extensionRunner.extensions).toHaveLength(1); expect(t.session.extensionRunner.extensions[0].path).toBe(extensionPath); }); it("exposes and runs the fixed benchmark without a command", async () => { const extension = await activeTune(); const tool = extension.tools.get("run_experiment").definition; expect(tool.parameters.properties).not.toHaveProperty("command"); fs.writeFileSync( path.join(t.cwd, ".pi", "tune", "benchmark.mjs"), 'console.error("stderr"); console.log("METRIC score=42"); console.log("METRIC bytes_kb=7");\n', ); const result = await tool.execute( "run", {}, new AbortController().signal, undefined, t.session.extensionRunner.createContext(), ); expect(result.details).toMatchObject({ exitCode: 0, passed: true, parsedPrimary: 42, parsedMetrics: { score: 42, bytes_kb: 7 }, }); expect(result.content[0].text).toContain("stderr"); }); it("fails before launch when benchmark.mjs is missing", async () => { const extension = await activeTune(); const result = await extension.tools .get("run_experiment") .definition.execute( "run", {}, new AbortController().signal, undefined, t.session.extensionRunner.createContext(), ); expect(result.content[0].text).toContain("Create .pi/tune/benchmark.mjs"); }); it("runs checks with Pi's Node executable", async () => { const extension = await activeTune(); const tuneDir = path.join(t.cwd, ".pi", "tune"); fs.writeFileSync(path.join(tuneDir, "benchmark.mjs"), "process.exit(0);\n"); fs.writeFileSync( path.join(tuneDir, "checks.mjs"), "console.log(process.execPath);\n", ); const result = await extension.tools .get("run_experiment") .definition.execute( "run", {}, new AbortController().signal, undefined, t.session.extensionRunner.createContext(), ); expect(result.details).toMatchObject({ checksPass: true }); expect(result.content[0].text).toContain("Checks passed"); }); it("times out the fixed benchmark", async () => { const extension = await activeTune(); fs.writeFileSync( path.join(t.cwd, ".pi", "tune", "benchmark.mjs"), "setInterval(() => {}, 1000);\n", ); const result = await extension.tools .get("run_experiment") .definition.execute( "run", { timeout_seconds: 0.05 }, new AbortController().signal, undefined, t.session.extensionRunner.createContext(), ); expect(result.details).toMatchObject({ timedOut: true, passed: false }); expect(result.content[0].text).toContain("TIMEOUT"); }); });