Skip to content
refs
History
42 lines · 2.7 KBRaw
1import assert from "node:assert/strict";
2import { execFileSync } from "node:child_process";
3import fs from "node:fs";
4import os from "node:os";
5import path from "node:path";
6import { test } from "node:test";
7import { starterFiles, writeFirstCommit } from "../starter.ts";
8
9const line = (text: string) => `${(text.length + 4).toString(16).padStart(4, "0")}${text}`;
10
11test("a new repository's first commit holds the agent set-up files, readable by git", async (t) => {
12 const files = starterFiles({ repo: "acme/shop", name: "shop", description: "The shop.", origin: "https://useref.dev", branch: "trunk" });
13 assert.deepEqual(files.map((file) => file.path), ["AGENTS.md", "CLAUDE.md", "README.md"]);
14 assert.match(files[0].text, /call `refs_connect` with `acme\/shop`/);
15 assert.match(files[0].text, /https:\/\/useref\.dev\/llms\.txt/);
16 assert.match(files[0].text, /never `trunk`/);
17 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}\``));
18 assert.equal(files[1].text, "@AGENTS.md\n");
19 assert.equal(files[2].text, "# shop\n\nThe shop.\n");
20 assert.equal(starterFiles({ repo: "acme/shop", name: "shop", description: "", origin: "https://useref.dev", branch: "main" })[2].text, "# shop\n");
21
22 let sent: Uint8Array | null = null;
23 t.mock.method(globalThis, "fetch", async (_url: string, init: RequestInit) => {
24 sent = new Uint8Array(init.body as Uint8Array);
25 return new Response(`${line("unpack ok\n")}${line("ok refs/heads/main\n")}0000`);
26 });
27 const handle = { info: async () => ({ remote: "https://artifacts.test/shop" }), createToken: async () => ({ plaintext: "t" }) } as unknown as ArtifactsRepo;
28 const written = await writeFirstCommit(handle, "main", files, { name: "Aman", email: "a@x" }, 0, "Set up\n\nDecision: none");
29 assert.ok(written.ok);
30
31 const body = Buffer.from(sent!);
32 const command = body.subarray(4, body.indexOf(0));
33 assert.equal(command.toString(), `${"0".repeat(40)} ${written.commit} refs/heads/main`);
34 const repo = fs.mkdtempSync(path.join(os.tmpdir(), "refs-starter-"));
35 t.after(() => fs.rmSync(repo, { recursive: true, force: true }));
36 const git = (args: string[], input?: Buffer) => execFileSync("git", ["-C", repo, ...args], { input, encoding: "utf8" });
37 git(["init", "-q"]);
38 git(["index-pack", "--stdin", "--fix-thin"], body.subarray(body.indexOf("PACK")));
39 assert.deepEqual(git(["ls-tree", "--name-only", written.commit]).trim().split("\n"), ["AGENTS.md", "CLAUDE.md", "README.md"]);
40 assert.equal(git(["show", `${written.commit}:CLAUDE.md`]), "@AGENTS.md\n");
41 assert.equal(git(["log", "--format=%an %s", written.commit]).trim(), "Aman Set up");
42});