import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { test } from "node:test"; import { starterFiles, writeFirstCommit } from "../starter.ts"; const line = (text: string) => `${(text.length + 4).toString(16).padStart(4, "0")}${text}`; test("a new repository's first commit holds the agent set-up files, readable by git", async (t) => { const files = starterFiles({ repo: "acme/shop", name: "shop", description: "The shop.", origin: "https://useref.dev", branch: "trunk" }); assert.deepEqual(files.map((file) => file.path), ["AGENTS.md", "CLAUDE.md", "README.md"]); assert.match(files[0].text, /call `refs_connect` with `acme\/shop`/); assert.match(files[0].text, /https:\/\/useref\.dev\/llms\.txt/); assert.match(files[0].text, /never `trunk`/); for (const tool of ["refs_take", "refs_events", "refs_open_pull", "refs_view_pull", "refs_reply", "refs_release", "refs_end"]) assert.match(files[0].text, new RegExp(`\`${tool}\``)); assert.equal(files[1].text, "@AGENTS.md\n"); assert.equal(files[2].text, "# shop\n\nThe shop.\n"); assert.equal(starterFiles({ repo: "acme/shop", name: "shop", description: "", origin: "https://useref.dev", branch: "main" })[2].text, "# shop\n"); let sent: Uint8Array | null = null; t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => { sent = new Uint8Array(init.body as Uint8Array); return new Response(`${line("unpack ok\n")}${line("ok refs/heads/main\n")}0000`); }); const handle = { info: async () => ({ remote: "https://artifacts.test/shop" }), createToken: async () => ({ plaintext: "t" }) } as unknown as ArtifactsRepo; const written = await writeFirstCommit(handle, "main", files, { name: "Aman", email: "a@x" }, 0, "Set up\n\nDecision: none"); assert.ok(written.ok); const body = Buffer.from(sent!); const command = body.subarray(4, body.indexOf(0)); assert.equal(command.toString(), `${"0".repeat(40)} ${written.commit} refs/heads/main`); const repo = fs.mkdtempSync(path.join(os.tmpdir(), "refs-starter-")); t.after(() => fs.rmSync(repo, { recursive: true, force: true })); const git = (args: string[], input?: Buffer) => execFileSync("git", ["-C", repo, ...args], { input, encoding: "utf8" }); git(["init", "-q"]); git(["index-pack", "--stdin", "--fix-thin"], body.subarray(body.indexOf("PACK"))); assert.deepEqual(git(["ls-tree", "--name-only", written.commit]).trim().split("\n"), ["AGENTS.md", "CLAUDE.md", "README.md"]); assert.equal(git(["show", `${written.commit}:CLAUDE.md`]), "@AGENTS.md\n"); assert.equal(git(["log", "--format=%an %s", written.commit]).trim(), "Aman Set up"); });