iterate.com / git

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

git / tests / branch-commit.test.mjsmain at 517348b

// tests/branch-commit.test.mjs — the commit that a member's page makes on a new branch
// (../public/branch-commit.js), pushed to git's own http-backend over a repo that git made.
// Run it from the repo's root with `node --test "tests/*.test.mjs"`.
import assert from "node:assert/strict";
import { chmodSync } from "node:fs";
import { join } from "node:path";
import { test } from "node:test";
import git from "isomorphic-git";
import http from "isomorphic-git/http/node";
import { commitOnBranch, isBranchName } from "../public/branch-commit.js";
import { repoWith } from "./fixtures.mjs";
import { gitServer } from "./git-http.mjs";

const author = { name: "ada@example.com", email: "ada@example.com" };

async function served(history) {
  const site = repoWith(history);
  const server = await gitServer({ "/repos/site": site.folder });
  return {
    site,
    url: `${server.origin}/repos/site`,
    seen: server.seen,
    async [Symbol.asyncDispose]() {
      await server.close();
      site[Symbol.dispose]();
    },
  };
}

test("an edit becomes one commit on a new branch: the file's new text, main as its parent, every other entry as it was", async () => {
  await using at = await served([
    { message: "Start", files: { "README.md": "# site\n", "src/deep/hello.ts": "export const hello = 1;\n", "run.sh": "#!/bin/sh\n" } },
  ]);
  const { site, url } = at;
  // a file that runs keeps its mode through an edit of another file
  chmodSync(join(site.folder, "run.sh"), 0o755);
  const tip = site.more({ message: "It runs", files: {} });
  assert.match(site.git("ls-tree", "main", "run.sh"), /^100755 /);

  const { commitOid } = await commitOnBranch({
    git, http, url, author,
    parent: tip,
    branch: "ada/fix-hello",
    path: "src/deep/hello.ts",
    content: "export const hello = 2;\n",
    message: "Update src/deep/hello.ts\n\nIt was one.",
  });

  assert.equal(site.git("rev-parse", "ada/fix-hello"), commitOid);
  assert.equal(site.git("rev-parse", "main"), tip, "main is where it was");
  assert.equal(site.git("log", "-1", "--format=%P|%an|%ae|%B", "ada/fix-hello").trim(), `${tip}|ada@example.com|ada@example.com|Update src/deep/hello.ts\n\nIt was one.`);
  assert.equal(site.git("show", "ada/fix-hello:src/deep/hello.ts"), "export const hello = 2;");
  assert.equal(site.git("diff", "--name-status", "main", "ada/fix-hello"), "M\tsrc/deep/hello.ts");
  assert.match(site.git("ls-tree", "ada/fix-hello", "run.sh"), /^100755 /);
  assert.equal(site.git("fsck", "--strict", "--no-dangling"), "");
});

test("a file at the root is edited too", async () => {
  await using at = await served([{ message: "Start", files: { "README.md": "# site\n", "a/b.txt": "b\n" } }]);
  const { site, url } = at;
  await commitOnBranch({ git, http, url, author, parent: site.commits[0], branch: "readme", path: "README.md", content: "# The site\n", message: "Update README.md" });
  assert.equal(site.git("show", "readme:README.md"), "# The site");
  assert.equal(site.git("diff", "--name-status", "main", "readme"), "M\tREADME.md");
});

test("nothing is pushed when main has moved, the branch is there, the name is bad, the file is gone or the text is the same", async () => {
  await using at = await served([
    { message: "Start", files: { "README.md": "# site\n" } },
    { on: "taken", message: "A branch", files: { "x.txt": "x\n" } },
    { message: "Main moves", files: { "more.txt": "more\n" } },
  ]);
  const { site, url, seen } = at;
  const [first, , tip] = site.commits;
  const edit = { git, http, url, author, parent: tip, branch: "new", path: "README.md", content: "# changed\n", message: "Update README.md" };

  await assert.rejects(commitOnBranch({ ...edit, parent: first }), /main has moved since this page was loaded/);
  await assert.rejects(commitOnBranch({ ...edit, branch: "taken" }), /The branch taken is there already/);
  await assert.rejects(commitOnBranch({ ...edit, branch: "main" }), /no name for a branch/);
  await assert.rejects(commitOnBranch({ ...edit, branch: "a b" }), /no name for a branch/);
  await assert.rejects(commitOnBranch({ ...edit, path: "gone.md" }), /main has no file gone.md/);
  await assert.rejects(commitOnBranch({ ...edit, path: "nowhere/gone.md" }), /main has no file nowhere\/gone.md/);
  await assert.rejects(commitOnBranch({ ...edit, content: "# site\n" }), /Nothing is changed/);

  assert.ok(!seen.some((request) => request.endsWith("git-receive-pack")), "no push was sent");
  assert.equal(site.git("for-each-ref", "--format=%(refname:short)", "refs/heads/"), "main\ntaken");
});

test("a branch's name is one that the app's addresses and git both take", () => {
  for (const name of ["fix", "ada/fix-hello", "v1.2_x", "a/b/c"]) assert.ok(isBranchName(name), name);
  for (const name of ["main", "HEAD", "", "a b", "/a", "a/", "a//b", ".a", "a..b", "a.lock", "a.", "-", "a~1", "a:b", "a\\b", "x".repeat(101)])
    assert.ok(!isBranchName(name), name);
});