1import { mkdir, mkdtemp, writeFile } from "node:fs/promises";2import { tmpdir } from "node:os";3import { join } from "node:path";45export type Fixture = {6 bucketDir: string;7 repo: string;8 repoDir: string;9 10 saveIds: string[];11 12 strip(path: string): Promise<void>;13};141516const PNG = Uint8Array.from(17 atob(18 "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==",19 ),20 (c) => c.charCodeAt(0),21);2223const README = `# Demo2425A small repo for **Kepr's** tests.2627See [the guide](docs/guide.md), [the site](https://example.com) and [a trap](javascript:alert(1)).28293031<script>alert("raw html")</script>32`;33343536373839export async function makeKohBucket(repo = "demo"): Promise<Fixture> {40 const root = await mkdtemp(join(tmpdir(), "kepr-fixture-"));41 const home = join(root, "home");42 const bucketDir = join(root, "bucket");43 const repoDir = join(root, "work", repo);44 await mkdir(home);45 await mkdir(join(repoDir, "src/lib/deep"), { recursive: true });46 await mkdir(join(repoDir, "docs"));47 const koh = (...args: string[]) => run(repoDir, home, args);4849 koh("init");50 koh("remote", "folder", bucketDir);51 await writeFile(join(repoDir, "docs/guide.md"), "# Guide\n\nRead me.\n");52 await writeFile(join(repoDir, "docs/logo.png"), PNG);53 await writeFile(join(repoDir, "secret.txt"), "hunter2\n");54 await writeFile(join(repoDir, "src/lib/deep/util.ts"), "export const one = 1;\n");55 koh("save", "add docs, a secret and a deep helper");56 const first = JSON.parse(koh("log", "--json")).head_id as string;57 koh("flag", first, "--reason", "holds a secret");58 59 Bun.sleepSync(1100);60 await writeFile(join(repoDir, "README.md"), README);61 await writeFile(join(repoDir, "src/main.ts"), "// Entry point.\nconsole.log('hi');\n");62 koh("save", "add readme and entry point", "--agent", "test-agent", "--session", "fixture");63 koh("checkpoint", "before the second change");64 await writeFile(join(repoDir, "src/main.ts"), "// Entry point.\nconsole.log('hello');\n");65 koh("save", "say hello");66 koh("offer");6768 const log = JSON.parse(koh("log", "--json")) as { saves: { id: string }[] };69 return {70 bucketDir,71 repo,72 repoDir,73 saveIds: log.saves.map((save) => save.id),74 strip: (path) => strip(repoDir, home, path),75 };76}7778function run(cwd: string, home: string, args: string[]): string {79 const result = Bun.spawnSync(["koh3", ...args], { cwd, env: env(home) });80 if (result.exitCode !== 0) {81 throw new Error(82 `koh3 ${args.join(" ")} failed: ${result.stderr.toString()}${result.stdout.toString()}`,83 );84 }85 return result.stdout.toString();86}8788function env(home: string) {89 return { PATH: process.env.PATH, HOME: home, NO_COLOR: "1" };90}919293async function strip(cwd: string, home: string, path: string): Promise<void> {94 let output = "";95 const proc = Bun.spawn(["koh3", "strip", path], {96 cwd,97 env: env(home),98 terminal: { cols: 120, rows: 24, data: (_term, data) => (output += data.toString()) },99 });100 proc.terminal?.write(`y\n${path}\n`);101 const code = await proc.exited;102 proc.terminal?.close();103 if (code !== 0) throw new Error(`koh3 strip ${path} failed: ${output}`);104}