iterate.com / git

git clone https://git.iterate.com/repos/git

git / tests / pages.test.mjsmain at 517348b

// tests/pages.test.mjs — the pages a person reads a repo with (../app.ts). Each test makes a repo
// with git itself, and local-mind.mjs answers the mind's part from it. The pages are whole
// without their script, so each test reads the HTML that the app answers.
// 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 { dataOf, linksIn, repoWith } from "./fixtures.mjs";
import { localMind } from "./local-mind.mjs";

const { gitApp } = await import("../app.ts");

test("a repo's own page lists its files, folders first, shows its README and says how to clone it", async () => {
  using kernel = repoWith([
    {
      message: "Start",
      files: {
        "README.md": "# kernel\n\nThe platform, and [its SDK](core/sdk.ts).\n",
        "core/sdk.ts": "export const one = 1;\n",
        "core/lib/api.ts": "export type Api = {};\n",
        "package.json": "{}\n",
      },
    },
  ]);
  const mind = localMind({ folders: { "/repos/kernel": kernel.folder }, member: null });
  const git = gitApp({ public: ["/repos/kernel"], ...look });

  const answer = await git.fetch(new Request("https://git.example.com/repos/kernel"), mind);
  const html = await answer.text();

  assert.equal(answer.status, 200);
  assert.deepEqual(linksIn(html, "entries"), [
    ["/repos/kernel/tree/main/core", "core/"],
    ["/repos/kernel/tree/main/README.md", "README.md"],
    ["/repos/kernel/tree/main/package.json", "package.json"],
  ]);
  // the README's text, marked for the script to draw as markdown, its links read against the root
  assert.ok(
    html.includes(
      `<article class="readme"><h2>README.md</h2><pre data-markdown data-dir=""># kernel\n\nThe platform, and [its SDK](core/sdk.ts).\n</pre>`,
    ),
  );
  assert.ok(html.includes("git clone https://git.example.com/repos/kernel"));
  assert.ok(html.includes(`main at <a href="/repos/kernel/commit/${kernel.commits[0]}">`));
  // what the file tree is drawn from
  assert.deepEqual(dataOf(html), {
    repo: "/repos/kernel",
    me: null,
    paths: ["README.md", "core/lib/api.ts", "core/sdk.ts", "package.json"],
    path: "",
  });
});

test("a folder's page lists what is in it, and a file's page has the file's text and the way back up", async () => {
  using kernel = repoWith([
    {
      message: "Start",
      files: {
        "core/sdk.ts": "export const html = `<b>${1 < 2 && \"yes\"}</b>`;\n",
        "core/lib/api.ts": "export type Api = {};\n",
      },
    },
  ]);
  const mind = localMind({ folders: { "/repos/kernel": kernel.folder }, member: null });
  const git = gitApp({ public: ["/repos/kernel"], ...look });
  const read = async (path) =>
    (await git.fetch(new Request(`https://git.example.com${path}`), mind)).text();

  assert.deepEqual(linksIn(await read("/repos/kernel/tree/main/core"), "entries"), [
    ["/repos/kernel/tree/main/core/lib", "lib/"],
    ["/repos/kernel/tree/main/core/sdk.ts", "sdk.ts"],
  ]);

  const file = await read("/repos/kernel/tree/main/core/sdk.ts");
  // the text is text: nothing in a file is markup of the page
  assert.ok(
    file.includes(
      `<pre data-file="core/sdk.ts">export const html = \`&lt;b&gt;\${1 &lt; 2 &amp;&amp; &quot;yes&quot;}&lt;/b&gt;\`;\n</pre>`,
    ),
  );
  assert.deepEqual(linksIn(file, "crumbs").slice(0, 2), [
    ["/repos/kernel", "kernel"],
    ["/repos/kernel/tree/main/core", "core"],
  ]);
  assert.equal(dataOf(file).path, "core/sdk.ts");
});

test("a markdown file is shown as it reads, and with ?plain as its source", async () => {
  using board = repoWith([{ message: "Start", files: { "todo/first.md": "# First\n" } }]);
  const mind = localMind({ folders: { "/repos/task-board": board.folder }, member: null });
  const git = gitApp({ public: ["/repos/task-board"], ...look });
  const read = async (path) =>
    (await git.fetch(new Request(`https://git.example.com${path}`), mind)).text();

  const shown = await read("/repos/task-board/tree/main/todo/first.md");
  assert.ok(shown.includes(`<pre data-markdown data-dir="todo"># First\n</pre>`));
  // the action is an icon on the row of the file's name, and its name is there for a reader
  assert.ok(
    /<p class="crumbs">.*<span class="acts"><a href="\/repos\/task-board\/tree\/main\/todo\/first\.md\?plain" title="Its source" aria-label="Its source"><svg [^>]*aria-hidden="true">.*<\/svg><\/a><\/span><\/p>/.test(shown),
  );
  assert.ok(!shown.includes(`<p class="note">`));

  const source = await read("/repos/task-board/tree/main/todo/first.md?plain");
  assert.ok(source.includes(`<pre data-file="todo/first.md"># First\n</pre>`));
  assert.ok(source.includes(`<a href="/repos/task-board/tree/main/todo/first.md" title="As it reads" aria-label="As it reads"><svg `));
});

test("the commits are listed newest first, each with its oid, the first line of its message, who and when, and no address", async () => {
  using kernel = repoWith([
    { message: "Start\n\nThe first files.", files: { "a.ts": "1\n" } },
    { message: "Add b", files: { "b.ts": "2\n" } },
  ]);
  const mind = localMind({ folders: { "/repos/kernel": kernel.folder }, member: null });
  const git = gitApp({ public: ["/repos/kernel"], ...look });

  const answer = await git.fetch(new Request("https://git.example.com/repos/kernel/commits"), mind);
  const html = await answer.text();

  const [start, addB] = kernel.commits;
  assert.deepEqual(linksIn(html, "commits"), [
    [`/repos/kernel/commit/${addB}`, addB.slice(0, 7)],
    [`/repos/kernel/commit/${start}`, start.slice(0, 7)],
  ]);
  assert.ok(html.includes(`</a> Add b <span class="by">Ada Author, 2026-11-24 10:02 UTC</span>`));
  assert.ok(html.includes(`</a> Start <span class="by">Ada Author, 2026-11-24 10:01 UTC</span>`));
  assert.ok(!html.includes("ada@example.com"));
});

test("a commit's page names each file it changed, each under a name that folds it, and carries both texts of each; the second reader costs the repo nothing", async () => {
  using kernel = repoWith([
    { message: "Start", files: { "kept.ts": "same\n", "changed.ts": "old\n", "gone.ts": "bye\n" } },
    {
      message: "Change three files\n\nOne changed, one came, one went.",
      files: { "changed.ts": "new\n", "came.ts": "hello\n", "gone.ts": null },
    },
  ]);
  const mind = localMind({ folders: { "/repos/kernel": kernel.folder }, member: null });
  const git = gitApp({ public: ["/repos/kernel"], ...look });
  const [start, change] = kernel.commits;
  const read = () =>
    git.fetch(new Request(`https://git.example.com/repos/kernel/commit/${change}`), mind);

  const html = await (await read()).text();

  assert.ok(html.includes("<h1>Change three files</h1>"));
  assert.ok(html.includes(`<pre class="message">One changed, one came, one went.</pre>`));
  assert.ok(html.includes(`parent <a href="/repos/kernel/commit/${start}">`));
  assert.ok(html.includes("3 files changed"));
  assert.deepEqual(linksIn(html, "changes"), [
    ["#diff-0", "came.ts"],
    ["#diff-1", "changed.ts"],
    ["#diff-2", "gone.ts"],
  ]);
  // each file is open, under its name: a click on the name folds it, with no script. Its diff
  // is there as plain text, for a reader whose script has not drawn it
  assert.ok(
    html.includes(
      `<details class="changed" id="diff-1" open><summary><span class="status modified">M</span> changed.ts</summary><div class="diff" data-diff="1"><pre class="plain"><span class="part">@@ -1,1 +1,1 @@</span>\n<span class="went">-old</span>\n<span class="came">+new</span></pre></div></details>`,
    ),
  );
  assert.deepEqual(dataOf(html), {
    repo: "/repos/kernel",
    me: null,
    diffs: [
      { path: "came.ts", before: "", after: "hello\n" },
      { path: "changed.ts", before: "old\n", after: "new\n" },
      { path: "gone.ts", before: "bye\n", after: "" },
    ],
  });

  // a commit never changes: the app kept what it worked out, and reads the repo no more
  const asked = [...mind.asked];
  assert.equal(await (await read()).text(), html);
  assert.deepEqual(mind.asked, asked);
  assert.deepEqual(Object.keys(mind.kv), [`git-app:commit:/repos/kernel:${change}`]);
});

test("a commit is found by the start of its oid, and a commit larger than the page shows names its files with no diff", async () => {
  using kernel = repoWith([
    { message: "Start", files: { "small.ts": "1\n" } },
    { message: "A large file", files: { "large.txt": "x".repeat(700_000), "small.ts": "2\n" } },
  ]);
  const mind = localMind({ folders: { "/repos/kernel": kernel.folder }, member: null });
  const git = gitApp({ public: ["/repos/kernel"], ...look });

  const short = kernel.commits[1].slice(0, 7);
  const answer = await git.fetch(
    new Request(`https://git.example.com/repos/kernel/commit/${short}`),
    mind,
  );
  const html = await answer.text();

  assert.deepEqual(linksIn(html, "changes"), [
    ["#diff-0", "large.txt"],
    ["#diff-1", "small.ts"],
  ]);
  assert.ok(html.includes("Not shown: the change is larger than this page shows."));
  assert.deepEqual(dataOf(html).diffs, [null, { path: "small.ts", before: "1\n", after: "2\n" }]);

  // the short oid's second reader asks the log which commit it is, and no files
  await git.fetch(new Request(`https://git.example.com/repos/kernel/commit/${short}`), mind);
  assert.deepEqual(mind.asked, [
    "log /repos/kernel",
    "files /repos/kernel",
    "files /repos/kernel",
    "log /repos/kernel",
  ]);
});

test("a repo that is not public is for members: anyone else gets the mind's sign-in, and the repo is not read", async () => {
  using config = repoWith([{ message: "Start", files: { "worker.ts": "export default {};\n" } }]);
  const folders = { "/repos/config": config.folder };
  const git = gitApp({ public: [], ...look });
  const read = (mind) => git.fetch(new Request("https://git.example.com/repos/config"), mind);

  const anyone = localMind({ folders, member: null });
  const refused = await read(anyone);
  assert.equal(refused.status, 401);
  assert.equal(await refused.text(), "Sign in\n");
  assert.deepEqual(anyone.asked, []);

  const member = localMind({ folders, member: "ada@example.com" });
  const html = await (await read(member)).text();
  assert.deepEqual(linksIn(html, "entries"), [["/repos/config/tree/main/worker.ts", "worker.ts"]]);
  assert.ok(html.includes(`<span class="tag">private</span>`));
});

test("the app's own address lists the repos: the public ones to anyone, with no question to the mind, and every repo to a member", async () => {
  using kernel = repoWith([{ message: "Start", files: { "a.ts": "1\n" } }]);
  const folders = { "/repos/kernel": kernel.folder, "/repos/config": kernel.folder };
  const git = gitApp({ public: ["/repos/kernel"], ...look });
  const read = async (mind) =>
    (await git.fetch(new Request("https://git.example.com/"), mind)).text();

  const anyone = localMind({ folders, member: null });
  const forAnyone = await read(anyone);
  assert.deepEqual(linksIn(forAnyone, "repos"), [["/repos/kernel", "kernel"]]);
  assert.ok(forAnyone.includes("git clone https://git.example.com/repos/kernel"));
  assert.ok(forAnyone.includes(`<a href="/sign-in">Sign in</a>`));
  assert.deepEqual(anyone.asked, []);

  const forMember = await read(localMind({ folders, member: "ada@example.com" }));
  assert.deepEqual(linksIn(forMember, "repos"), [
    ["/repos/config", "config"],
    ["/repos/kernel", "kernel"],
  ]);
  assert.ok(forMember.includes(`<b>config</b></a> <span class="tag">private</span>`));
  assert.ok(!forMember.includes("/sign-in"));
});

test("what is not there is not found: a path, another branch, a commit, a repo, and a file that is not text says so", async () => {
  using kernel = repoWith([
    { message: "Start", files: { "a.ts": "1\n", "logo.png": Buffer.from([137, 80, 78, 71, 0, 1]) } },
  ]);
  const mind = localMind({ folders: { "/repos/kernel": kernel.folder }, member: "ada@example.com" });
  const git = gitApp({ public: ["/repos/kernel"], ...look });
  const read = async (path) => {
    const answer = await git.fetch(new Request(`https://git.example.com${path}`), mind);
    return { status: answer.status, html: await answer.text() };
  };

  const noPath = await read("/repos/kernel/tree/main/nope.ts");
  assert.equal(noPath.status, 404);
  assert.ok(noPath.html.includes("kernel has no file or folder nope.ts on main."));

  const noBranch = await read("/repos/kernel/tree/next/a.ts");
  assert.equal(noBranch.status, 404);
  assert.ok(noBranch.html.includes("Only main is shown here."));

  const noCommit = await read(`/repos/kernel/commit/${"0".repeat(40)}`);
  assert.equal(noCommit.status, 404);

  const noRepo = await read("/repos/missing");
  assert.equal(noRepo.status, 404);
  assert.ok(noRepo.html.includes("The mind has no repo missing."));

  const image = await read("/repos/kernel/tree/main/logo.png");
  assert.equal(image.status, 200);
  assert.ok(image.html.includes("This file is not text, so it is not shown."));
});

// ── the fixtures ──

/** What worker.ts hands the app: the mind's icon. */
test("a member's file page has the pencil, to GitHub's address for an edit; anyone else's has none", async () => {
  using kernel = repoWith([{ message: "Start", files: { "core/my sdk.ts": "export const one = 1;\n" } }]);
  const folders = { "/repos/kernel": kernel.folder };
  const git = gitApp({ public: ["/repos/kernel"], ...look });
  const read = async (member) =>
    (await git.fetch(new Request("https://git.example.com/repos/kernel/tree/main/core/my%20sdk.ts"), localMind({ folders, member }))).text();

  const members = await read("ada@example.com");
  assert.ok(members.includes(`<a class="pencil" href="/repos/kernel/edit/main/core/my%20sdk.ts" title="Edit this file" aria-label="Edit this file"><svg `));
  // on the row of the file's name, with no row of its own
  assert.ok(/<p class="crumbs">.*<span class="acts"><a class="pencil" [^>]*>.*<\/a><\/span><\/p>/.test(members));
  assert.ok(!(await read(null)).includes(`class="acts"`));
  assert.ok(!(await read(null)).includes("/edit/main/"));
});

test("the edit page is a member's: the file's text in a box, and what the commit needs; anyone else gets the sign-in, on a public repo too", async () => {
  using kernel = repoWith([
    { message: "Start", files: { "core/sdk.ts": "\nconst html = `</textarea><b>${1 < 2}</b>`;\n", "README.md": "# kernel\n" } },
    { on: "ada/edit-sdk-ts", message: "Taken", files: { "x.txt": "x\n" } },
  ]);
  const folders = { "/repos/kernel": kernel.folder };
  const git = gitApp({ public: ["/repos/kernel"], ...look });
  const address = "https://git.example.com/repos/kernel/edit/main/core/sdk.ts";

  const stranger = localMind({ folders, member: null });
  const refused = await git.fetch(new Request(address), stranger);
  assert.equal(refused.status, 401);
  assert.deepEqual(stranger.asked, [], "the repo was not read");

  const answer = await git.fetch(new Request(address), localMind({ folders, member: "ada@example.com" }));
  const html = await answer.text();
  assert.equal(answer.status, 200);
  // the text is text, and its first line break is kept: a textarea drops one after its tag
  assert.ok(html.includes(`aria-label="core/sdk.ts">\n\nconst html = \`&lt;/textarea&gt;&lt;b&gt;\${1 &lt; 2}&lt;/b&gt;\`;\n</textarea>`));
  assert.ok(html.includes(`<div id="commit">`));
  assert.ok(html.includes(`Edit <a href="/repos/kernel/tree/main/core/sdk.ts">core/sdk.ts</a>`));
  // the branch's name is one that no branch has; the author is the member, for their own page
  assert.deepEqual(dataOf(html), {
    repo: "/repos/kernel",
    me: "ada",
    edit: {
      path: "core/sdk.ts",
      commitOid: kernel.commits[0],
      crlf: false,
      branch: "ada/edit-sdk-ts-2",
      author: { name: "ada", email: "ada@example.com" },
    },
  });
});

test("the edit page keeps a file's CR LF line endings, and offers no edit of what a box would change whole", async () => {
  using site = repoWith([
    {
      message: "Start",
      files: {
        "dos.txt": "one\r\ntwo\r\n",
        "mixed.txt": "one\r\ntwo\n",
        "logo.png": Buffer.from([0x89, 0x50, 0x4e, 0x47, 0, 1, 2, 3]),
        "big.txt": "x".repeat(1_000_001),
      },
    },
  ]);
  const mind = localMind({ folders: { "/repos/site": site.folder }, member: "ada@example.com" });
  const git = gitApp({ public: [], ...look });
  const read = async (path) => {
    const answer = await git.fetch(new Request(`https://git.example.com/repos/site/edit/${path}`), mind);
    return { status: answer.status, html: await answer.text() };
  };

  const dos = await read("main/dos.txt");
  assert.ok(dos.html.includes(`>\none\ntwo\n</textarea>`));
  assert.equal(dataOf(dos.html).edit.crlf, true);

  for (const [path, words] of [
    ["main/mixed.txt", "This file mixes line endings"],
    ["main/logo.png", "This file is not text"],
    ["main/big.txt", "This file is too large"],
  ]) {
    const { status, html } = await read(path);
    assert.equal(status, 200, path);
    assert.ok(html.includes(words), path);
    assert.ok(!html.includes("<textarea"), path);
    assert.equal(dataOf(html).edit, undefined, path);
  }
  assert.equal((await read("main/gone.txt")).status, 404);
  assert.equal((await read("main")).status, 404);
  assert.equal((await read("a-branch/dos.txt")).status, 404);
});

const look = { icon: () => new Response("the icon") };