testPublishBuild

tcmts.cli.web.testPublishBuild · #fmb5dfsv · term

import { publishBuild } from "#codebase/tcmts/cli/web";
import type { BuildManifest } from "#codebase/tcmts/cli/web";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";

export const testPublishBuild = () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), "tcm-publish-"));
  try {
    const apps = path.join(dir, "apps");
    const stage = (name: string, made: Partial<BuildManifest>) => {
      const staged = path.join(dir, name);
      fs.mkdirSync(staged, { recursive: true });
      fs.writeFileSync(path.join(staged, "tcm-app.json"), JSON.stringify({ hash: "h", ...made }));
      fs.writeFileSync(path.join(staged, "index.html"), name);
      return staged;
    };
    const page = () => fs.readFileSync(path.join(apps, "h", "index.html"), "utf8");
    // the first put in place stays; a second, the same build, is dropped
    assert.equal(
      publishBuild(apps, "h", stage("a", { tcm: "1" }), path.join(dir, "aside"), () => false).tcm,
      "1",
    );
    const second = stage("b", { tcm: "1" });
    assert.equal(publishBuild(apps, "h", second, path.join(dir, "aside"), () => false).tcm, "1");
    assert.equal(page(), "a");
    assert.ok(!fs.existsSync(second), "dropped");
    // one replaced (another tcm's, or --force) is moved aside, then removed
    const seen: (string | undefined)[] = [];
    const third = stage("c", { tcm: "2" });
    const placed = publishBuild(apps, "h", third, path.join(dir, "aside"), (there) => {
      seen.push(there.tcm);
      return there.tcm !== "2";
    });
    assert.equal(placed.tcm, "2");
    assert.deepEqual(seen, ["1"]);
    assert.equal(page(), "c");
    assert.ok(!fs.existsSync(path.join(dir, "aside")));
    // one without a manifest (an older tcm's, half made) is replaced, whatever `replace` says
    fs.rmSync(path.join(apps, "h", "tcm-app.json"));
    publishBuild(apps, "h", stage("d", { tcm: "2" }), path.join(dir, "aside"), () => false);
    assert.equal(page(), "d");
    assert.throws(
      () => publishBuild(apps, "h", path.join(dir, "e"), path.join(dir, "aside"), () => false),
      /holds no build/,
    );
  } finally {
    fs.rmSync(dir, { recursive: true, force: true });
  }
}

uses