testMirroredCodeImportsAPage

tcmts.cli.test.e2e.files.testMirroredCodeImportsAPage · #yclnqwv6 · term

Code importing a page is mirrored with the import tcm writes (#codebase/…), and the files still hash to it.

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

export const testMirroredCodeImportsAPage = () => {
  const root = tempDir("files mirror import");
  const proj = path.join(root, "proj");
  // inside the project, which maps `#codebase` to it (as this repository does)
  const cbDir = path.join(proj, "codebase");
  fs.mkdirSync(path.join(proj, "docs"), { recursive: true });
  fs.mkdirSync(cbDir);
  fs.writeFileSync(path.join(proj, ".gitignore"), "codebase/\n");
  fs.writeFileSync(path.join(proj, "package.json"), JSON.stringify({ type: "module", imports: { "#codebase": "./codebase/.tcm/codebase/index.ts", "#codebase/*": "./codebase/.tcm/codebase/*.ts" } }));
  fs.writeFileSync(path.join(proj, "math.ts"), "export const square = (x: number): number => x * x;\n");
  fs.writeFileSync(path.join(proj, "docs", "getting-started.md"), "# Getting started\n\nHello.\n");
  assert.equal(spawnSync("git", ["-C", proj, "init", "-q"]).status, 0);
  const ok = (...args: string[]) => okIn(cbDir, ...args);
  ok("init");
  ok("import", "..", "--into", "app", "--mirror");
  fs.writeFileSync(path.join(cbDir, "scratch.ts"), `// tcm: namespace app.site\nimport { "getting-started" as gettingStarted } from "#codebase/app/docs";\n\nexport const intro = (): string => gettingStarted.split("\\n")[0]!;\n`);
  ok("add");
  assert.match(fs.readFileSync(path.join(proj, "site.ts"), "utf8"), /import \{ "getting-started" as \w+ \} from "#codebase\/app\/docs";/);
  assert.match(ok("mirror", "--check"), /the files hash to what the codebase holds/);
  assert.match(ok("run", "app.site.intro"), /^# Getting started$/m);
}

uses