testAddImportsWhatItUses

tcmts.cli.test.e2e.editing.testAddImportsWhatItUses · #tjghdtuw · term

tcm add imports what the code uses and doesn't import, as tcm patch does: a default import the codebase's code makes (os), and a name a module the code imports from already exports (join, from node:path), though the codebase has a page by that 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 testAddImportsWhatItUses = () => {
  const dir = tempDir("add imports");
  const ok = (...args: string[]) => okIn(dir, ...args);
  ok("init");
  fs.rmSync(path.join(dir, "scratch.ts"), { force: true });
  const write = (f: string, text: string) => fs.writeFileSync(path.join(dir, f), text);
  write("join.md", "# Join\n\nJoining paths.\n");
  ok("add", "join.md", "--into", "docs");
  write(
    "a.ts",
    `// tcm: namespace lib\nimport os from "node:os";\nexport const home = (): string => os.homedir();\n`,
  );
  ok("add", "a.ts");
  write(
    "b.ts",
    `// tcm: namespace lib\nimport { basename } from "node:path";\n` +
      `export const where = (): string => basename(join(os.tmpdir(), "x"));\n`,
  );
  const out = ok("add", "b.ts");
  assert.match(out, /import os from "node:os";/, out);
  assert.match(out, /import \{ join \} from "node:path";/, out);
  assert.match(out, /\+ new +lib\.where/, out);
  assert.equal(ok("run", "lib.where").trim(), "x");
}

uses