git / tests / releases.test.mjsmain at 517348b
// tests/releases.test.mjs — a release, from main's newest commit to a tag of git (../releases.ts),
// and the page that shows the releases (../pages.ts). Each test makes a repo with git itself;
// git-http.mjs serves it as git's own smart HTTP, which is what Cloudflare Artifacts is to the
// app, and git itself then says what the tag is.
// Run it from the repo's root with `node --test "tests/*.test.mjs"`.
import "@repos/stdlib/testing/mind-loader.mjs";
import assert from "node:assert/strict";
import { test } from "node:test";
import { compareVersions, cutRelease, nextVersion, releasesIn, versionOf } from "../releases.ts";
import { repoWith } from "./fixtures.mjs";
import { gitServer } from "./git-http.mjs";
import { localMind } from "./local-mind.mjs";
const { gitApp } = await import("../app.ts");
const look = { icon: () => new Response("<svg/>") };
const repo = "/repos/kernel";
/** A repo, git's own server over it, and the mind that reaches it. */
async function served(history, member = null) {
const kernel = repoWith(history);
const server = await gitServer({ [repo]: kernel.folder });
const mind = localMind({ folders: { [repo]: kernel.folder }, member, remote: server.origin });
return { kernel, server, mind, async [Symbol.asyncDispose]() { await server.close(); kernel[Symbol.dispose](); } };
}
test("a version is three numbers; the next one raises the patch, and a repo's first is 0.1.0", () => {
assert.deepEqual(versionOf("v1.2.7"), [1, 2, 7]);
assert.deepEqual(versionOf("1.2.7"), [1, 2, 7]);
for (const text of ["v1.2", "1.2.7-rc1", "v01.2.3", "latest", "v1.2.7\n", ""]) assert.equal(versionOf(text), null);
assert.deepEqual(nextVersion(null), [0, 1, 0]);
assert.deepEqual(nextVersion([1, 2, 7]), [1, 2, 8]);
assert.deepEqual(nextVersion([1, 2, 7], "minor"), [1, 3, 0]);
assert.deepEqual(nextVersion([1, 2, 7], "major"), [2, 0, 0]);
assert.ok(compareVersions([1, 10, 0], [1, 9, 9]) > 0);
});
test("the first cut is v0.1.0: an annotated tag on main's newest commit, as git itself reads it, and one event on the repo's context", async () => {
await using site = await served([
{ message: "Start", files: { "README.md": "# kernel\n" } },
{ message: "Add the SDK", files: { "sdk.ts": "export {};\n" } },
]);
const { kernel, mind } = site;
const tip = kernel.commits[1];
const outcome = await cutRelease(mind.as("ada@example.com"), repo);
assert.deepEqual(outcome, {
cut: true,
tag: "v0.1.0",
version: "0.1.0",
commitOid: tip,
previous: null,
notes: `The first release: main at ${tip.slice(0, 7)}.`,
});
// git's own word on what was pushed
assert.equal(kernel.git("cat-file", "-t", "v0.1.0"), "tag");
assert.equal(kernel.git("rev-parse", "v0.1.0^{commit}"), tip);
assert.equal(
kernel.git("for-each-ref", "--format=%(taggername) %(taggeremail)|%(contents:subject)|%(contents:body)", "refs/tags/v0.1.0"),
`releases <releases@example.com>|kernel v0.1.0|The first release: main at ${tip.slice(0, 7)}.`,
);
kernel.git("fsck", "--strict");
// main is where it was
assert.equal(kernel.git("rev-parse", "main"), tip);
const [event] = mind.events[repo];
assert.equal(event.type, "release/cut");
assert.deepEqual(event.payload, { tag: "v0.1.0", version: "0.1.0", commitOid: tip, previous: null, notes: outcome.notes });
assert.equal(event.source.principal.email, "ada@example.com");
});
test("nothing is cut while main is at the newest release; a new commit makes the next patch, whose notes are main's commits since", async () => {
await using site = await served([{ message: "Start", files: { "README.md": "# kernel\n" } }]);
const { kernel, mind } = site;
const itx = mind.getItx();
await cutRelease(itx, repo);
assert.deepEqual(await cutRelease(itx, repo), {
cut: false,
why: "main is at v0.1.0 already: nothing is new",
tag: "v0.1.0",
commitOid: kernel.commits[0],
});
assert.equal(mind.events[repo].length, 1);
const sdk = kernel.more({ message: "Add the SDK (#1)\n\nIt was missing.", files: { "sdk.ts": "export {};\n" } });
const fix = kernel.more({ message: "Fix the SDK (#2)", files: { "sdk.ts": "export const one = 1;\n" } });
const second = await cutRelease(itx, repo);
assert.equal(second.tag, "v0.1.1");
assert.equal(second.previous, "v0.1.0");
assert.equal(second.commitOid, fix);
assert.equal(
second.notes,
`Since v0.1.0:\n\n- Fix the SDK (#2) (${fix.slice(0, 7)})\n- Add the SDK (#1) (${sdk.slice(0, 7)})`,
);
assert.equal(kernel.git("rev-parse", "v0.1.1^{commit}"), fix);
assert.equal(kernel.git("rev-parse", "v0.1.0^{commit}"), kernel.commits[0]);
});
test("a version can be named, with notes of its own; one that is not newer, or is no version, is refused and nothing is tagged", async () => {
await using site = await served([{ message: "Start", files: { "README.md": "# kernel\n" } }]);
const { kernel, mind } = site;
const itx = mind.getItx();
await cutRelease(itx, repo, { version: "1.2.7", notes: "The one with the SDK." });
assert.equal(kernel.git("for-each-ref", "--format=%(contents:body)", "refs/tags/v1.2.7"), "The one with the SDK.");
kernel.more({ message: "More", files: { "more.txt": "more\n" } });
await assert.rejects(cutRelease(itx, repo, { version: "1.2.7" }), /v1\.2\.7 is not newer than v1\.2\.7, the newest release/);
await assert.rejects(cutRelease(itx, repo, { version: "1.2.0" }), /not newer/);
await assert.rejects(cutRelease(itx, repo, { version: "soon" }), /"soon" is no version/);
assert.equal(kernel.git("tag", "--list"), "v1.2.7");
const minor = await cutRelease(itx, repo, { bump: "minor", tagger: { name: "Ada <Author>", email: "ada@example.com" } });
assert.equal(minor.tag, "v1.3.0");
assert.equal(kernel.git("for-each-ref", "--format=%(taggername) %(taggeremail)", "refs/tags/v1.3.0"), "Ada Author <ada@example.com>");
kernel.git("fsck", "--strict");
});
test("a tag is never moved: the remote refuses a second push of a name that it has", async () => {
await using site = await served([{ message: "Start", files: { "README.md": "# kernel\n" } }]);
const { kernel, server, mind } = site;
const { pushTag, tagObject } = await import("../releases.ts");
await cutRelease(mind.getItx(), repo);
const other = kernel.more({ message: "More", files: { "more.txt": "more\n" } });
const object = tagObject({ tag: "v0.1.0", commitOid: other, tagger: { name: "x", email: "x@example.com" }, seconds: 1, message: "again" });
await assert.rejects(pushTag({ address: `${server.origin}${repo}`, authorization: null }, "v0.1.0", object), /refused the tag v0\.1\.0|did not say/);
assert.equal(kernel.git("rev-parse", "v0.1.0^{commit}"), kernel.commits[0]);
});
test("the releases page lists the releases newest first, each with its commit, who cut it and its notes; a tag that git pushed is there with its commit alone; anyone reads a public repo's", async () => {
await using site = await served([{ message: "Start", files: { "README.md": "# kernel\n" } }]);
const { kernel, mind } = site;
await cutRelease(mind.as("ada@example.com"), repo);
const fix = kernel.more({ message: "Fix <it>", files: { "fix.txt": "fixed\n" } });
await cutRelease(mind.getItx(), repo);
kernel.git("tag", "by-hand", kernel.commits[0]);
assert.deepEqual(
(await releasesIn(mind.getItx(), repo)).map(({ tag, version, commitOid, by, previous }) => ({ tag, version, commitOid, by, previous })),
[
{ tag: "v0.1.1", version: "0.1.1", commitOid: fix, by: null, previous: "v0.1.0" },
{ tag: "v0.1.0", version: "0.1.0", commitOid: kernel.commits[0], by: "ada", previous: null },
{ tag: "by-hand", version: null, commitOid: kernel.commits[0], by: null, previous: null },
],
);
const git = gitApp({ public: [repo], ...look });
const eventsRead = [];
const own = mind.getItx();
const cd = own.cd;
own.cd = (path) => ({ ...cd(path), readEvents: async (...args) => (eventsRead.push(path), cd(path).readEvents(...args)) });
mind.minted.length = 0;
const answer = await git.fetch(new Request("https://git.example.com/repos/kernel/releases"), mind);
const html = await answer.text();
assert.equal(answer.status, 200);
// ANYONE MAY ASK, AND OFTEN: a second page mints no second token, and no page reads the log
await (await git.fetch(new Request("https://git.example.com/repos/kernel/releases"), mind)).text();
assert.deepEqual(mind.minted, ["read /repos/kernel"]);
assert.deepEqual(eventsRead, []);
assert.ok(html.includes(`<a href="/repos/kernel/releases" aria-current="page">Releases</a>`));
const rows = html.slice(html.indexOf(`<ul class="releases">`), html.indexOf("</ul>", html.indexOf(`<ul class="releases">`)));
assert.deepEqual([...rows.matchAll(/<li id="([^"]+)"><b>([^<]+)<\/b>/g)].map((row) => row[2]), ["v0.1.1", "v0.1.0", "by-hand"]);
assert.ok(rows.includes(`<li id="v0.1.0"><b>v0.1.0</b> <span class="by">at <a href="/repos/kernel/commit/${kernel.commits[0]}"><code>${kernel.commits[0].slice(0, 7)}</code></a>, cut by ada, `));
// the notes are text, never markup
assert.ok(rows.includes(`<pre class="notes">Since v0.1.0:\n\n- Fix <it> (${fix.slice(0, 7)})</pre>`));
assert.ok(rows.includes(`<li id="by-hand"><b>by-hand</b> <span class="by">at <a href="/repos/kernel/commit/${kernel.commits[0]}"><code>${kernel.commits[0].slice(0, 7)}</code></a>, a tag with no release notes</span></li>`));
assert.ok(html.includes("git clone --branch v0.1.1 https://git.example.com/repos/kernel"));
// a repo that is not public answers the mind's sign-in to anyone who is no member
const closed = gitApp({ public: [], ...look });
assert.equal((await closed.fetch(new Request("https://git.example.com/repos/kernel/releases"), mind)).status, 401);
});
test("a repo with no release says so", async () => {
await using site = await served([{ message: "Start", files: { "README.md": "# kernel\n" } }]);
const git = gitApp({ public: [repo], ...look });
const html = await (await git.fetch(new Request("https://git.example.com/repos/kernel/releases"), site.mind)).text();
assert.ok(html.includes(`<p class="note">No release yet.</p>`));
});