git / tests / pulls.test.mjsmain at 517348b
// tests/pulls.test.mjs — a pull, from a pushed branch to one commit on main (../pulls.ts), and the
// pages that show it (../pages.ts). Each test makes a repo with git itself; local-mind.mjs answers
// the mind's part, and holds the repo's events as the mind does.
// 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 { baseOf, mergePull, pullsOf } from "../pulls.ts";
import { dataOf, linksIn, repoWith } from "./fixtures.mjs";
import { localMind } from "./local-mind.mjs";
const { gitApp } = await import("../app.ts");
test("a pushed branch is compared with main: its own commits, what it changes, and for a member the box that opens a pull", async () => {
using site = repoWith([
{ message: "Start", files: { "header.css": "header { color: black; }\n", "README.md": "# site\n" } },
{ on: "red-header", message: "Make the header red\n\nBlack was hard to read.", files: { "header.css": "header { color: red; }\n" } },
{ on: "red-header", message: "Say so in the README", files: { "README.md": "# site\n\nThe header is red.\n" } },
]);
const [, red, readme] = site.commits;
const mind = localMind({ folders: { "/repos/site": site.folder }, member: "ada@example.com" });
const git = gitApp({ public: ["/repos/site"], ...look });
const answer = await git.fetch(new Request("https://git.example.com/repos/site/compare/red-header"), mind);
const html = await answer.text();
assert.equal(answer.status, 200);
assert.ok(html.includes("<code>red-header</code> adds 2 commits to <code>main</code>."));
assert.deepEqual(linksIn(html, "commits"), [
[`/repos/site/commit/${readme}`, readme.slice(0, 7)],
[`/repos/site/commit/${red}`, red.slice(0, 7)],
]);
assert.deepEqual(linksIn(html, "changes"), [
["#diff-0", "README.md"],
["#diff-1", "header.css"],
]);
// the pull starts as the branch's first commit says it
assert.deepEqual(dataOf(html), {
repo: "/repos/site",
me: "ada",
compare: { branch: "red-header", title: "Make the header red", body: "Black was hard to read." },
diffs: [
{ path: "README.md", before: "# site\n", after: "# site\n\nThe header is red.\n" },
{ path: "header.css", before: "header { color: black; }\n", after: "header { color: red; }\n" },
],
});
assert.ok(html.includes(`<div id="open-pull"></div>`));
});
test("a pull is opened, talked about and merged, each by one event on the repo's context; it lands as one commit on main, named after the pull", async () => {
using site = repoWith([
{ message: "Start", files: { "header.css": "header { color: black; }\n" } },
{ on: "red-header", message: "wip", files: { "header.css": "header { color: red; }\n" } },
{ on: "red-header", message: "more", files: { "note.txt": "red\n" } },
]);
const repo = "/repos/site";
const mind = localMind({ folders: { [repo]: site.folder }, member: "ada@example.com" });
const git = gitApp({ public: [repo], ...look });
const read = async (path) =>
(await git.fetch(new Request(`https://git.example.com${path}`), mind)).text();
const ada = mind.as("ada@example.com").cd(repo);
const grace = mind.as("grace@example.com").cd(repo);
// Ada's page opens the pull. It is the repo's first, so it is #1.
await ada.append({
type: "pull/opened",
payload: { branch: "red-header", title: "Make the header red", body: "Black was hard to read." },
});
// Grace says something about it, and something about one line, which Ada answers and resolves.
await grace.append({
type: "pull/commented",
payload: { pull: 1, body: "Looks right to me.", replyTo: null, at: null },
});
const line = { path: "header.css", side: "additions", line: 1, quote: "header { color: red; }" };
const [onLine] = await grace.append({
type: "pull/commented",
payload: { pull: 1, body: "Why not `crimson`?", replyTo: null, at: line },
});
await ada.append({
type: "pull/commented",
payload: { pull: 1, body: "Red is in the brand book.", replyTo: onLine.offset, at: null },
});
await ada.append({ type: "pull/resolved", payload: { pull: 1, comment: onLine.offset } });
const open = await read("/repos/site/pull/1");
assert.ok(open.includes(`<h1>Make the header red <span class="by">#1</span></h1>`));
assert.ok(open.includes(`<span class="tag open">open</span> ada wants <code>red-header</code> squashed into <code>main</code>`));
assert.ok(open.includes(`<pre data-markdown data-dir="">Black was hard to read.</pre>`));
// the conversation is in the page as it stands, with no script: who, when, and their words
assert.ok(open.includes(`<p class="by">grace, 2026-11-24 12:02 UTC</p><pre>Looks right to me.</pre>`));
assert.ok(open.includes(`<pre>Why not \`crimson\`?</pre>`));
// the thread on a line, resolved: folded under one line that says so
assert.ok(
open.includes(
`<details class="thread resolved" id="comment-3"><summary class="place"><a href="#diff-0"><code>header.css</code></a> line 1, resolved <span class="count">2 comments</span></summary>`,
),
);
assert.deepEqual(dataOf(open), {
repo,
me: "ada",
pull: { number: 1, title: "Make the header red", branch: "red-header", status: "open" },
mergeable: true,
threads: [
{
id: 2,
place: null,
resolved: false,
comments: [{ id: 2, by: "grace", at: "2026-11-24T12:02:00.000Z", body: "Looks right to me." }],
diff: null,
outdated: false,
},
{
// the comment on a line: on the page's first file, where the line still reads the same
id: 3,
place: { ...line, endLine: 1 },
resolved: true,
comments: [
{ id: 3, by: "grace", at: "2026-11-24T12:03:00.000Z", body: "Why not `crimson`?" },
{ id: 4, by: "ada", at: "2026-11-24T12:04:00.000Z", body: "Red is in the brand book." },
],
diff: 0,
outdated: false,
},
],
diffs: [
{ path: "header.css", before: "header { color: black; }\n", after: "header { color: red; }\n" },
{ path: "note.txt", before: "", after: "red\n" },
],
});
// The merge: one commit on main with the branch's files, by the one who opened the pull.
const { commitOid } = await mergePull(mind.getItx(), repo, 1);
assert.equal(
site.git("log", "--format=%an <%ae>%n%B", "main", "-1"),
"ada <ada@example.com>\nMake the header red (#1)\n\nBlack was hard to read.",
);
assert.equal(site.git("rev-list", "--count", "main"), "2");
assert.equal(site.git("show", "main:header.css"), "header { color: red; }");
assert.equal(site.git("show", "main:note.txt"), "red");
assert.deepEqual(mind.events[repo].at(-1).payload, { pull: 1, commitOid });
const merged = await read("/repos/site/pull/1");
// what became of it and when, apart from who opened it and when
assert.ok(
merged.includes(
`<span class="tag merged">merged</span> <code>red-header</code> was squashed into <code>main</code> as <a href="/repos/site/commit/${commitOid}"><code>${commitOid.slice(0, 7)}</code></a>, 2026-11-24 12:06 UTC. ada opened it 2026-11-24 12:01 UTC.</p>`,
),
);
// what it shows now is what that commit changed
assert.deepEqual(linksIn(merged, "changes"), [
["#diff-0", "header.css"],
["#diff-1", "note.txt"],
]);
assert.equal(dataOf(merged).mergeable, false);
// the branch is done: the branches page names the pull it became, and offers no compare
const branches = await read("/repos/site/branches");
assert.ok(branches.includes(`merged as <a href="/repos/site/pull/1">#1 Make the header red</a>`));
assert.ok(!branches.includes("/repos/site/compare/red-header"));
// and it is merged once: a second try is refused in words
await assert.rejects(mergePull(mind.getItx(), repo, 1), /#1 is merged already/);
});
test("a pull's number is its place among the pulls that were opened, and an event that is no pull's changes nothing", () => {
const events = [
event(1, "events.iterate.com/repo/pushed", { ref: "refs/heads/main" }),
event(2, "pull/opened", { branch: "one", title: "The first", body: "" }, "ada@example.com"),
// a payload that is no pull's: no number is spent on it
event(3, "pull/opened", { title: "No branch" }, "ada@example.com"),
event(4, "pull/opened", { branch: "two", title: "The second", body: "why" }, "grace@example.com"),
event(5, "pull/commented", { pull: 2, body: "On the second.", replyTo: null, at: null }, "ada@example.com"),
// about a pull that does not exist, and a reply to a comment that does not: neither is lost or loud
event(6, "pull/commented", { pull: 9, body: "Nowhere.", replyTo: null, at: null }, "ada@example.com"),
event(7, "pull/commented", { pull: 2, body: "A thread of its own.", replyTo: 99, at: null }, "ada@example.com"),
event(8, "pull/edited", { pull: 1, title: "The first, renamed", body: "now with a body" }, "ada@example.com"),
event(9, "pull/closed", { pull: 1 }, "grace@example.com"),
// code with nobody behind it is shown as the context it ran in
event(10, "pull/commented", { pull: 2, body: "From an agent.", replyTo: 5, at: null }, null),
];
assert.deepEqual(
pullsOf(events).map(({ number, branch, title, body, by, status, doneBy, doneAt, threads }) => ({
number,
branch,
title,
body,
by,
status,
done: doneAt && `${doneBy} at ${doneAt}`,
said: threads.map(({ id, comments }) => [id, ...comments.map(({ by, body }) => `${by}: ${body}`)]),
})),
[
{
number: 1,
branch: "one",
title: "The first, renamed",
body: "now with a body",
by: "ada",
status: "closed",
// grace closed it, a while after ada opened it
done: "grace at 2026-11-24T12:09:00.000Z",
said: [],
},
{
number: 2,
branch: "two",
title: "The second",
body: "why",
by: "grace",
status: "open",
done: null,
said: [
[5, "ada: On the second.", "/agents/reviewer: From an agent."],
[7, "ada: A thread of its own."],
],
},
],
);
});
test("a comment on lines stays on them while they read as they did, and says so when one of them has changed", async () => {
using site = repoWith([
{ message: "Start", files: { "header.css": "header {\n color: black;\n}\n" } },
{ on: "red-header", message: "Red", files: { "header.css": "header {\n color: red;\n margin: 0;\n}\n" } },
]);
const repo = "/repos/site";
const mind = localMind({ folders: { [repo]: site.folder }, member: "ada@example.com" });
const git = gitApp({ public: [repo], ...look });
const page = async () =>
(await git.fetch(new Request("https://git.example.com/repos/site/pull/1"), mind)).text();
const threads = async () =>
dataOf(await page()).threads.map(({ place, diff, outdated }) => ({ lines: [place.line, place.endLine], diff, outdated }));
const ada = mind.as("ada@example.com").cd(repo);
await ada.append({ type: "pull/opened", payload: { branch: "red-header", title: "Red", body: "" } });
// one comment on the two new lines, and one on the last line alone, which names no end
const two = { path: "header.css", side: "additions", line: 2, endLine: 3, quote: " color: red;\n margin: 0;" };
const one = { path: "header.css", side: "additions", line: 4, quote: "}" };
await ada.append({ type: "pull/commented", payload: { pull: 1, body: "Too bright?", replyTo: null, at: two } });
await ada.append({ type: "pull/commented", payload: { pull: 1, body: "Fine.", replyTo: null, at: one } });
assert.deepEqual(await threads(), [
{ lines: [2, 3], diff: 0, outdated: false },
{ lines: [4, 4], diff: 0, outdated: false },
]);
// in the conversation a comment on lines is folded under one line: it is read in the diff
assert.ok(
(await page()).includes(
`<details class="thread" id="comment-2"><summary class="place"><a href="#diff-0"><code>header.css</code></a> lines 2 to 3 <span class="count">1 comment</span></summary><code class="quote"> color: red;\n margin: 0;</code>`,
),
);
// a new commit on the branch changes one of the two lines, and leaves the last line as it was
site.more({ on: "red-header", message: "Crimson", files: { "header.css": "header {\n color: crimson;\n margin: 0;\n}\n" } });
assert.deepEqual(await threads(), [
{ lines: [2, 3], diff: 0, outdated: true },
{ lines: [4, 4], diff: 0, outdated: false },
]);
// its lines have changed, so it is nowhere in the diff: here it is open, and says so
assert.ok(
(await page()).includes(
`<details class="thread" id="comment-2" open><summary class="place"><a href="#diff-0"><code>header.css</code></a> lines 2 to 3, changed since <span class="count">1 comment</span></summary>`,
),
);
});
test("a squash is taken only while the branch has main's newest commit: when main has moved, the branch merges main in first", async () => {
using site = repoWith([
{ message: "Start", files: { "a.txt": "a\n" } },
{ on: "add-b", message: "Add b", files: { "b.txt": "b\n" } },
{ message: "Meanwhile on main", files: { "c.txt": "c\n" } },
]);
const repo = "/repos/site";
const mind = localMind({ folders: { [repo]: site.folder }, member: "ada@example.com" });
const git = gitApp({ public: [repo], ...look });
const page = async () =>
(await git.fetch(new Request("https://git.example.com/repos/site/pull/1"), mind)).text();
await mind.as("ada@example.com").cd(repo).append({ type: "pull/opened", payload: { branch: "add-b", title: "Add b", body: "" } });
// the page shows what the branch itself changed, not what main did meanwhile, and offers no merge
const behind = await page();
assert.deepEqual(linksIn(behind, "changes"), [["#diff-0", "b.txt"]]);
assert.ok(behind.includes("main has moved since add-b last had it. To merge: merge main into add-b, and push."));
assert.equal(dataOf(behind).mergeable, false);
await assert.rejects(mergePull(mind.getItx(), repo, 1), /main has moved since add-b last had it/);
site.more({ on: "add-b", merge: "main" });
const ready = await page();
assert.deepEqual(linksIn(ready, "changes"), [["#diff-0", "b.txt"]]);
assert.equal(dataOf(ready).mergeable, true);
await mergePull(mind.getItx(), repo, 1);
assert.equal(site.git("log", "--format=%s", "main"), "Add b (#1)\nMeanwhile on main\nStart");
assert.deepEqual(site.git("ls-tree", "--name-only", "main").split("\n"), ["a.txt", "b.txt", "c.txt"]);
});
test("where a branch stands on main is read from the two histories: where it began, or the commit of main it last merged in", () => {
const commit = (oid, ...parents) => ({ oid, parents });
const main = [commit("m3", "m2"), commit("m2", "m1"), commit("m1")];
// began at m1, and never took main in since
assert.deepEqual(baseOf(main, [commit("b2", "b1"), commit("b1", "m1"), commit("m1")]), {
base: "m1",
own: [commit("b2", "b1"), commit("b1", "m1")],
upToDate: false,
});
// began at m1, then merged m3 in: it has main's newest
const merge = commit("b3", "b1", "m3");
assert.deepEqual(baseOf(main, [merge, commit("b1", "m1"), commit("m1")]), {
base: "m3",
own: [merge, commit("b1", "m1")],
upToDate: true,
});
// nothing of its own
assert.deepEqual(baseOf(main, [commit("m3", "m2")]), { base: "m3", own: [], upToDate: true });
// no commit in common in what was read
assert.deepEqual(baseOf(main, [commit("x1")]), { base: null, own: [commit("x1")], upToDate: false });
});
test("the pulls page lists the open pulls and then the ones that are done; the branches page lists every branch, with its pull or the page that compares it with main", async () => {
using site = repoWith([
{ message: "Start", files: { "a.txt": "a\n" } },
{ on: "one", message: "One", files: { "one.txt": "1\n" } },
{ on: "two", message: "Two", files: { "two.txt": "2\n" } },
{ on: "three", message: "Three", files: { "three.txt": "3\n" } },
]);
const [start, one, two, three] = site.commits;
const repo = "/repos/site";
const mind = localMind({ folders: { [repo]: site.folder }, member: null });
const git = gitApp({ public: [repo], ...look });
// anyone reads a public repo's pulls and branches
const read = (path) => git.fetch(new Request(`https://git.example.com${path}`), mind);
const ada = mind.as("ada@example.com").cd(repo);
await ada.append({ type: "pull/opened", payload: { branch: "one", title: "The first", body: "" } });
await ada.append({ type: "pull/opened", payload: { branch: "two", title: "The second", body: "" } });
await ada.append({ type: "pull/commented", payload: { pull: 2, body: "Hm.", replyTo: null, at: null } });
await ada.append({ type: "pull/closed", payload: { pull: 1 } });
const [open, done] = (await (await read("/repos/site/pulls")).text()).split(`<ul class="pulls">`).slice(1);
assert.ok(open.startsWith(`<li><a href="/repos/site/pull/2">The second</a> <span class="by">#2, <code>two</code>, ada, 2026-11-24 12:02 UTC, 1 comment</span></li></ul>`));
assert.ok(done.startsWith(`<li><a href="/repos/site/pull/1">The first</a> <span class="by">#1, closed, <code>one</code>`));
const branches = await (await read("/repos/site/branches")).text();
assert.deepEqual(
branches.split(`<ul class="branches">`)[1].split("</ul>")[0].split("</li>").filter(Boolean).map((row) => row.replace(/<[^>]+>/g, "")),
[
`main at ${start.slice(0, 7)}, what the mind runs`,
// its pull was closed, so the branch can be proposed again
`one at ${one.slice(0, 7)}, compare with main`,
`three at ${three.slice(0, 7)}, compare with main`,
`two at ${two.slice(0, 7)}, open as #2 The second`,
],
);
assert.deepEqual(linksIn(branches, "branches").filter(([href]) => !href.includes("/commit/")), [
["/repos/site/compare/one", "compare with main"],
["/repos/site/compare/three", "compare with main"],
["/repos/site/pull/2", "#2 The second"],
]);
// a branch that has an open pull goes to its pull
const compared = await read("/repos/site/compare/two");
assert.equal(compared.status, 302);
assert.equal(compared.headers.get("location"), "https://git.example.com/repos/site/pull/2");
});
test("anyone reads a public repo's pull and is offered the sign-in to write; a pull of any other repo is for members", async () => {
using site = repoWith([
{ message: "Start", files: { "a.txt": "a\n" } },
{ on: "one", message: "One", files: { "one.txt": "1\n" } },
]);
const repo = "/repos/site";
const mind = localMind({ folders: { [repo]: site.folder }, member: null });
await mind.as("ada@example.com").cd(repo).append({ type: "pull/opened", payload: { branch: "one", title: "The first", body: "" } });
const read = (publicRepos) =>
gitApp({ public: publicRepos, ...look }).fetch(new Request("https://git.example.com/repos/site/pull/1"), mind);
const html = await (await read([repo])).text();
assert.ok(html.includes(`<a href="/sign-in?to=%2Frepos%2Fsite%2Fpull%2F1">Sign in</a> to comment or to merge.`));
assert.ok(!html.includes(`id="merge"`));
assert.equal(dataOf(html).me, null);
const refused = await read([]);
assert.equal(refused.status, 401);
const missing = await gitApp({ public: [repo], ...look }).fetch(new Request("https://git.example.com/repos/site/pull/7"), mind);
assert.equal(missing.status, 404);
assert.ok((await missing.text()).includes("site has no pull #7."));
});
// ── the fixtures ──
/** What worker.ts hands the app: the mind's icon. */
const look = { icon: () => new Response("the icon") };
/** An event as a context's log holds it, written by a member (their address) or, with no
* address, by code that ran in the context /agents/reviewer. */
function event(offset, type, payload, address) {
const source = address
? { origin: "/repos/site", principal: { actor: address, email: address } }
: { origin: "/agents/reviewer" };
return { offset, type, payload, createdAt: `2026-11-24T12:${String(offset).padStart(2, "0")}:00.000Z`, path: "/repos/site", source };
}