testLibraryMirrors

tcmts.cli.test.e2e.libraries.testLibraryMirrors · #zs2iig5h · term

a library comes from any of its sources, tried in turn: a mirror answers when the author's codebase is gone, and any codebase that has a library can serve it by hash

import { codebase } from "#codebase/tcmts/cli/test/e2e/releases";
import assert from "node:assert/strict";
import fs from "node:fs";

export const testLibraryMirrors = () => {
  const alice = codebase("alice mirrors");
  alice.save(
    `// tcm: namespace geo\nexport const manifest = { name: "geo" };\nexport const area = (side: number) => side * side;\n`,
    "add",
  );
  alice.tcm("release", "geo@1.0.0");
  alice.save(`// tcm: namespace geo\nexport const cube = (side: number) => side ** 3;\n`, "add");
  alice.tcm("release", "geo@1.1.0");
  // a mirror: a codebase that pulled hers, releases and all
  const mirror = codebase("mirror");
  mirror.tcm("remote", "add", "origin", alice.dir);
  mirror.tcm("pull");

  const me = codebase("me mirrors");
  me.save(`// tcm: namespace app\nexport const greeting = "hi";\n`, "add");
  me.tcm("lib", "add", alice.dir, "geo@1.0.0");
  assert.match(
    me.tcm("lib", "sources", "geo", "--add", mirror.dir),
    /app\.lib\.geo comes from .*alice mirrors.*, then .*mirror/,
  );
  assert.match(me.tcm("lib"), /geo@1\.0\.0 {2}from .*alice mirrors.*, .*mirror/);

  // her codebase gone, the mirror answers, and what it sends is checked like any
  fs.renameSync(alice.dir, `${alice.dir} gone`);
  const up = me.tcm("lib", "upgrade", "geo");
  assert.equal(up.match(/tried .*alice mirrors \w+: no tcm codebase here/g)?.length, 1);
  assert.match(up, /geo@1\.0\.0 → geo@1\.1\.0: additions \(from .*mirror \w+\)/);
  assert.match(up, /upgraded app\.lib\.geo to geo@1\.1\.0/);

  // sources come and go, but not the last
  me.tcm("lib", "sources", "geo", "--rm", alice.dir);
  assert.match(
    me.run("lib", "sources", "geo", "--rm", mirror.dir).out,
    /is where app\.lib\.geo comes from; add another first/,
  );
  assert.match(
    me.run("lib", "sources", "geo", "--rm", "nowhere").out,
    /isn't fetched from nowhere/,
  );

  // any codebase that has a library serves it by hash: here, one that only uses it
  const hash = /"namespace": "(\w+)"/.exec(me.tcm("view", "app.lib.libraries"))![1]!;
  const other = codebase("other mirrors");
  other.save(`// tcm: namespace app\nexport const x = 1;\n`, "add");
  assert.match(
    other.tcm("lib", "add", me.dir, `#${hash}`, "--as", "geo"),
    /added app\.lib\.geo: #\w{8} from /,
  );
  // (and a release only where releases are)
  assert.match(
    other.run("lib", "add", me.dir, "geo", "--at", "app.again").out,
    /no source has geo:\n {2}.*has no releases of geo/,
  );
}

uses