fixture.ts

3.7 KB · raw

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	/** Every save, newest first, checkpoint included. */10	saveIds: string[];11	/** Strips a path from every save with koh3, so tests can sync before and after. */12	strip(path: string): Promise<void>;13};1415// The smallest valid PNG (1×1), so the fixture has a real binary file.16const 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)).2829![logo](docs/logo.png)3031<script>alert("raw html")</script>32`;3334/**35 * Builds a genuine Koh 3 folder remote with koh3 itself, under a throwaway HOME. The line is:36 * "add docs, a secret and a deep helper" (flagged) → a second later "add readme and entry point"37 * (agent and session) → a checkpoint → "say hello" (head).38 */39export 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	// Saves a second apart, so the timeline has time to spread over.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}9192// `koh strip` insists on a terminal and a typed confirmation, so it runs under a Bun PTY.93async 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}