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