testKeptConstantsFollowARename

tcmts.cli.test.e2e.effects.testKeptConstantsFollowARename · #jmvv7wl6 · term

A kept constant renamed (its text changes, its hash doesn't) keeps its value, in its module under its new name.

import { okIn, tempDir } from "#codebase/tcmts/cli/test/e2e/harness";
import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";

export const testKeptConstantsFollowARename = () => {
  const dir = tempDir("kept rename");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.writeFileSync(
    path.join(dir, "scratch.ts"),
    '// tcm: namespace lib\nexport const t = Array.from({ length: 4 }, (_, i) => i * 3);\nexport const main = (): number => (console.log(t.join(",")), 0);\n',
  );
  ok("add");
  assert.match(ok("run", "lib.main"), /0,3,6,9/);
  ok("mv", "lib.t", "lib.aMuchLongerName");
  assert.match(ok("run", "lib.main"), /0,3,6,9/);
  const modules = fs
    .readdirSync(path.join(dir, ".tcm", "cache"))
    .filter((f) => /^[a-z2-7]{52}\.ts$/.test(f))
    .map((f) => fs.readFileSync(path.join(dir, ".tcm", "cache", f), "utf8"))
    .join("\n");
  assert.match(
    modules,
    /const aMuchLongerName = __tcm_kept\(\(\) => \(Array\.from[^\n]*\), \[0,3,6,9\]\)/,
  );
}

uses