testBuildsAreNamed

tcmts.cli.builds.test.testBuildsAreNamed · #dcvmjox4 · term

Builds are named known-good, previous, and (older ones) the history entry they were made from; each resolves to its tcm.js, as a path does, and a name that's none of them says what's kept.

import { buildsDir, keptBuilds, resolveBuild } from "#codebase/tcmts/cli/builds";
import { fakeBuild } from "#codebase/tcmts/cli/builds/test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testBuildsAreNamed = () => {
  const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "tcm builds ")));
  try {
    const good = fakeBuild(root, "known-good", "aaaaaaaa2222", 1);
    fakeBuild(root, "known-good-previous", "bbbbbbbb3333", 5);
    fakeBuild(root, "builds/cccccccc", "cccccccc4444", 30);
    fakeBuild(root, "builds/dddddddd", "dddddddd5555", 10);
    // not a build: no tcm.js in it
    fs.mkdirSync(path.join(root, "builds", "junk"), { recursive: true });
    const kept = keptBuilds(root);
    assert.deepEqual(
      kept.map((b) => [b.name, b.head?.slice(0, 8), b.how]),
      [
        ["known-good", "aaaaaaaa", "verified"],
        ["previous", "bbbbbbbb", "verified"],
        ["dddddddd", "dddddddd", "verified"],
        ["cccccccc", "cccccccc", "verified"],
      ],
    );
    assert.match(kept[0]!.when!, /^\d{4}-\d\d-\d\d \d\d:\d\d$/);
    const bin = (where: string) =>
      path.join(root, where, "node_modules", "tcm-ts", "bin", "tcm.js");
    assert.equal(resolveBuild("known-good", root), bin("known-good"));
    assert.equal(resolveBuild("previous", root), bin("known-good-previous"));
    assert.equal(resolveBuild("#cccc", root), bin("builds/cccccccc"));
    assert.equal(resolveBuild("dddddddd5", root), bin("builds/dddddddd"));
    assert.equal(resolveBuild("bbbbbbbb", root), bin("known-good-previous"));
    // a path: a tcm.js, or a build's directory
    assert.equal(resolveBuild(bin("known-good"), undefined), bin("known-good"));
    assert.equal(resolveBuild("known-good", undefined, root), bin("known-good"));
    assert.equal(resolveBuild(good, root, "/"), bin("known-good"));
    assert.throws(
      () => resolveBuild("eeee", root, root),
      /no build eeee: kept are known-good, previous, dddddddd, cccccccc \(`tcm builds` lists them/,
    );
    assert.throws(() => resolveBuild("builds/junk", root, root), /isn't a tcm build/);
    assert.throws(
      () => resolveBuild("previous", undefined, root),
      /no builds are kept here: TCM_BUILDS/,
    );
    // where they're kept: TCM_BUILDS, or the directory holding the build that's running (an older one's too)
    assert.equal(buildsDir({ TCM_BUILDS: root }, undefined), root);
    assert.equal(buildsDir({}, bin("known-good")), root);
    assert.equal(buildsDir({}, bin("builds/cccccccc")), root);
    fs.rmSync(path.join(good, "VERIFIED"));
    fs.rmSync(path.join(good, "verified.json"));
    assert.equal(buildsDir({}, bin("known-good")), undefined);
    assert.equal(buildsDir({}, path.join(root, "tcm.js")), undefined);
  } finally {
    fs.rmSync(root, { recursive: true, force: true });
  }
}

uses