testCycleMateImportsDontClash

tcmts.cli.test.e2e.cycles.testCycleMateImportsDontClash · #3b7wpn7x · term

Editing one member of a cycle whose other member imports a name the file uses for something else: the other is written importing it by another name (Move_1), rather than the save being refused

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 testCycleMateImportsDontClash = () => {
  const dir = tempDir("cycle import clash");
  const tcm = (...args: string[]) => okIn(dir, ...args);
  tcm("init");
  const write = (code: string) => fs.writeFileSync(path.join(dir, "scratch.ts"), code);
  write(`// tcm: namespace shapes\nexport type Move = { dx: number };\n`);
  tcm("add");
  write(`// tcm: namespace other\nexport type Move = { name: string };\n`);
  tcm("add");
  write(
    `// tcm: namespace lib\nimport type { Move } from "#codebase/other";\nexport const a = (n: number): string => (n > 0 ? b(n - 1) : "a");\nexport const b = (n: number, m?: Move): string => (n > 0 ? a(n - 1) : m?.name ?? "b");\n`,
  );
  tcm("add");
  // lib.a, edited alone, with a Move of its own
  write(
    `// tcm: namespace lib\nimport type { Move } from "#codebase/shapes";\nexport const a = (n: number, m?: Move): string => (n > 0 ? b(n - 1) : String(m?.dx ?? "a"));\n`,
  );
  const saved = tcm("update");
  assert.match(saved, /~ update {4}lib\.a/, saved);
  assert.doesNotMatch(saved, /imported from .* here, and from .* there/);
  assert.match(tcm("view", "lib.b"), /m\?: Move/, "lib.b keeps its own Move");
  assert.equal(tcm("run", "lib.a", "0").trim(), "a");
}

uses