1import { mkdir, mkdtemp, writeFile } from "node:fs/promises";2import { tmpdir } from "node:os";3import { join } from "node:path";45export type Fixture = { bucketDir: string; repo: string; repoDir: string; saveIds: string[] };678export async function makeKohBucket(repo = "demo"): Promise<Fixture> {9 const root = await mkdtemp(join(tmpdir(), "kepr-fixture-"));10 const home = join(root, "home");11 const bucketDir = join(root, "bucket");12 const repoDir = join(root, "work", repo);13 await mkdir(home);14 await mkdir(join(repoDir, "src"), { recursive: true });15 const koh = (...args: string[]) => run(repoDir, home, args);1617 koh("init");18 koh("remote", "folder", bucketDir);19 await writeFile(join(repoDir, "README.md"), "# Demo\n\nA small repo for Kepr's tests.\n");20 await writeFile(join(repoDir, "src/main.ts"), "// Entry point.\nconsole.log('hi');\n");21 koh("save", "add readme and entry point", "--agent", "test-agent", "--session", "fixture");22 koh("checkpoint", "before the second change");23 await writeFile(join(repoDir, "src/main.ts"), "// Entry point.\nconsole.log('hello');\n");24 koh("save", "say hello");25 koh("offer");2627 const log = JSON.parse(koh("log", "--json")) as { saves: { id: string }[] };28 return { bucketDir, repo, repoDir, saveIds: log.saves.map((save) => save.id) };29}3031function run(cwd: string, home: string, args: string[]): string {32 const result = Bun.spawnSync(["koh3", ...args], {33 cwd,34 env: { PATH: process.env.PATH, HOME: home, NO_COLOR: "1" },35 });36 if (result.exitCode !== 0) {37 throw new Error(38 `koh3 ${args.join(" ")} failed: ${result.stderr.toString()}${result.stdout.toString()}`,39 );40 }41 return result.stdout.toString();42}