import { mkdir, mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; export type Fixture = { bucketDir: string; repo: string; repoDir: string; /** Every save, newest first, checkpoint included. */ saveIds: string[]; /** Strips a path from every save with koh3, so tests can sync before and after. */ strip(path: string): Promise; }; // The smallest valid PNG (1×1), so the fixture has a real binary file. const PNG = Uint8Array.from( atob( "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==", ), (c) => c.charCodeAt(0), ); const README = `# Demo A small repo for **Kepr's** tests. See [the guide](docs/guide.md), [the site](https://example.com) and [a trap](javascript:alert(1)). ![logo](docs/logo.png) `; /** * Builds a genuine Koh 3 folder remote with koh3 itself, under a throwaway HOME. The line is: * "add docs, a secret and a deep helper" (flagged) → a second later "add readme and entry point" * (agent and session) → a checkpoint → "say hello" (head). */ export async function makeKohBucket(repo = "demo"): Promise { const root = await mkdtemp(join(tmpdir(), "kepr-fixture-")); const home = join(root, "home"); const bucketDir = join(root, "bucket"); const repoDir = join(root, "work", repo); await mkdir(home); await mkdir(join(repoDir, "src/lib/deep"), { recursive: true }); await mkdir(join(repoDir, "docs")); const koh = (...args: string[]) => run(repoDir, home, args); koh("init"); koh("remote", "folder", bucketDir); await writeFile(join(repoDir, "docs/guide.md"), "# Guide\n\nRead me.\n"); await writeFile(join(repoDir, "docs/logo.png"), PNG); await writeFile(join(repoDir, "secret.txt"), "hunter2\n"); await writeFile(join(repoDir, "src/lib/deep/util.ts"), "export const one = 1;\n"); koh("save", "add docs, a secret and a deep helper"); const first = JSON.parse(koh("log", "--json")).head_id as string; koh("flag", first, "--reason", "holds a secret"); // Saves a second apart, so the timeline has time to spread over. Bun.sleepSync(1100); await writeFile(join(repoDir, "README.md"), README); await writeFile(join(repoDir, "src/main.ts"), "// Entry point.\nconsole.log('hi');\n"); koh("save", "add readme and entry point", "--agent", "test-agent", "--session", "fixture"); koh("checkpoint", "before the second change"); await writeFile(join(repoDir, "src/main.ts"), "// Entry point.\nconsole.log('hello');\n"); koh("save", "say hello"); koh("offer"); const log = JSON.parse(koh("log", "--json")) as { saves: { id: string }[] }; return { bucketDir, repo, repoDir, saveIds: log.saves.map((save) => save.id), strip: (path) => strip(repoDir, home, path), }; } function run(cwd: string, home: string, args: string[]): string { const result = Bun.spawnSync(["koh3", ...args], { cwd, env: env(home) }); if (result.exitCode !== 0) { throw new Error( `koh3 ${args.join(" ")} failed: ${result.stderr.toString()}${result.stdout.toString()}`, ); } return result.stdout.toString(); } function env(home: string) { return { PATH: process.env.PATH, HOME: home, NO_COLOR: "1" }; } // `koh strip` insists on a terminal and a typed confirmation, so it runs under a Bun PTY. async function strip(cwd: string, home: string, path: string): Promise { let output = ""; const proc = Bun.spawn(["koh3", "strip", path], { cwd, env: env(home), terminal: { cols: 120, rows: 24, data: (_term, data) => (output += data.toString()) }, }); proc.terminal?.write(`y\n${path}\n`); const code = await proc.exited; proc.terminal?.close(); if (code !== 0) throw new Error(`koh3 strip ${path} failed: ${output}`); }