testMirrorCycleEdit

tcmts.cli.test.e2e.mirror.testMirrorCycleEdit · #3eqbgako · term

a cycle edited through a mirror changes only what was edited: names re-hashed together read as they did

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 testMirrorCycleEdit = () => {
  const root = tempDir("mirror");
  const proj = path.join(root, "cycle");
  const cbDir = path.join(root, "cycle-cb");
  fs.mkdirSync(path.join(proj, "src"), { recursive: true });
  fs.writeFileSync(path.join(proj, "package.json"), JSON.stringify({ type: "module" }));
  // a class and a function in another file that take each other: one cycle, hashed together
  const counter = `import { log } from "./log.ts";\n\nexport class Counter {\n  n = 0;\n  bump(): number {\n    this.n++;\n    log(this);\n    return this.n;\n  }\n}\n`;
  fs.writeFileSync(path.join(proj, "src", "counter.ts"), counter);
  fs.writeFileSync(path.join(proj, "src", "log.ts"), `import type { Counter } from "./counter.ts";\n\nexport function log(c: Counter): void {\n  console.log("at", c.n);\n}\n`);
  fs.mkdirSync(cbDir);
  const run = (...args: string[]) => okIn(cbDir, ...args);
  run("init");
  fs.rmSync(path.join(cbDir, "scratch.ts"));
  run("import", proj, "--into", "app", "--mirror");
  assert.match(run("edit", "app.log.log"), /with app\.counter\.Counter/);
  const scratch = path.join(cbDir, "scratch.ts");
  fs.writeFileSync(scratch, fs.readFileSync(scratch, "utf8").replace(`console.log("at", c.n);`, `console.log("count", c.n);`));
  assert.match(run("update"), /mirror: wrote src\/log\.ts/);
  assert.equal(fs.readFileSync(path.join(proj, "src", "counter.ts"), "utf8"), counter, "Counter's file reads as it did");
  assert.equal(fs.readFileSync(path.join(proj, "src", "log.ts"), "utf8"), `import type { Counter } from "./counter.ts";\n\nexport function log(c: Counter): void {\n  console.log("count", c.n);\n}\n`);
  assert.match(run("mirror", "--check"), /the files hash to what the codebase holds/);
}

uses