git / tests / fixtures.mjsmain at 517348b
// tests/fixtures.mjs — what the tests of the git app's pages share: a repo made with git itself,
// and two readers of a page's HTML.
import assert from "node:assert/strict";
import { execFileSync } from "node:child_process";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
/** A repo in a folder of its own, made by one step for each entry of `history`, a minute apart
* from 10:01 UTC on 24 Nov 2026, all by Ada Author.
*
* { message, files } a commit on main: each file written, or deleted when null
* { on: "a-fix", message, files } a commit on that branch, which starts at main's newest
* { on: "a-fix", merge: "main" } that branch takes main in, as a merge commit
*
* `commits` are the oids in the order made, and `more(step)` makes one more later in the test.
* The folder goes when the test ends. */
export function repoWith(history) {
const folder = mkdtempSync(join(tmpdir(), "git-app-"));
const git = (args, minute) =>
execFileSync("git", ["-C", folder, ...args], {
env: {
PATH: process.env.PATH,
// this machine's own git settings stay out of the test (hooks, signing)
GIT_CONFIG_GLOBAL: "/dev/null",
GIT_CONFIG_SYSTEM: "/dev/null",
GIT_AUTHOR_NAME: "Ada Author",
GIT_AUTHOR_EMAIL: "ada@example.com",
GIT_COMMITTER_NAME: "Ada Author",
GIT_COMMITTER_EMAIL: "ada@example.com",
GIT_AUTHOR_DATE: `2026-11-24T10:${String(minute).padStart(2, "0")}:00Z`,
GIT_COMMITTER_DATE: `2026-11-24T10:${String(minute).padStart(2, "0")}:00Z`,
},
})
.toString()
.trim();
git(["init", "--quiet", "--initial-branch=main"], 0);
const branches = new Set(["main"]);
let checkedOut = "main";
const checkOut = (branch, minute) => {
if (branch === checkedOut) return;
// a branch that is new starts where main is
git(["checkout", "--quiet", ...(branches.has(branch) ? [] : ["-b"]), branch], minute);
branches.add(branch);
checkedOut = branch;
};
let minute = 0;
const made = (step) => {
minute += 1;
checkOut(step.on || "main", minute);
if (step.merge) git(["merge", "--quiet", "--no-ff", "-m", `Merge ${step.merge}`, step.merge], minute);
else {
for (const [path, content] of Object.entries(step.files)) {
const file = join(folder, path);
if (content === null) rmSync(file);
else {
mkdirSync(dirname(file), { recursive: true });
writeFileSync(file, content);
}
}
git(["add", "--all"], minute);
git(["commit", "--quiet", "--message", step.message], minute);
}
const oid = git(["rev-parse", "HEAD"], minute);
checkOut("main", minute);
return oid;
};
return {
folder,
commits: history.map(made),
/** One more step, later in the test: its commit's oid. */
more: made,
/** What git itself says of the repo: `site.git("log", "-1", "--format=%B", "main")`. */
git: (...args) => git(args, 59),
[Symbol.dispose]: () => rmSync(folder, { recursive: true, force: true }),
};
}
/** The links in the element of the page with that class, each as [href, text]. */
export function linksIn(html, className) {
const start = html.indexOf(`class="${className}"`);
assert.ok(start > 0, `the page has an element with class ${className}`);
const end = html.indexOf(className === "crumbs" ? "</p>" : "</ul>", start);
const links = html.slice(start, end).matchAll(/<a href="([^"]*)"[^>]*>(.*?)<\/a>/g);
return [...links].map(([, href, text]) => [href, text.replace(/<[^>]+>/g, "")]);
}
/** What the page hands its script. */
export function dataOf(html) {
return JSON.parse(/<script type="application\/json" id="data">(.*?)<\/script>/s.exec(html)[1]);
}