testImportLibrary

tcmts.cli.test.e2e.import.testImportLibrary · #vk777dpx · term

import a library that uses enums, namespaces, parameter properties, namespace imports, module state, ts directives, statements setting up constants and namespaces of types beside an interface

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

export const testImportLibrary = () => {
  const dir = tempDir("import");
  const lib = tempDir("import lib");
  const tcm = (...args: string[]) => okIn(dir, ...args);
  tcm("init");
  writeLibrary(lib);
  const imported = tcm("import", path.join(lib, "src"), "--into", "lib", "--exclude", "testing");
  assert.match(imported, /\+ new/);
  assert.doesNotMatch(
    imported,
    /warning/,
    "module resolution and emit options are tcm's business in import mode",
  );

  // ambient unique symbols with the same text stay distinct
  assert.notEqual(hashOf(dir, "lib.symbols.BrandA"), hashOf(dir, "lib.symbols.BrandB"));
  // a `let` and the function assigning it share a module, so the assignment works
  assert.equal(componentOf(dir, "lib.config.setConfig"), componentOf(dir, "lib.config.config"));
  assert.notEqual(
    componentOf(dir, "lib.config.getConfig"),
    componentOf(dir, "lib.config.config"),
    "readers just import it",
  );

  assert.match(tcm("verify"), /verified \d+ definitions: rendering and re-hashing is the identity/);

  fs.writeFileSync(
    path.join(dir, "scratch.ts"),
    `// tcm: namespace app
import { Shape, Kind, unwrap } from "#codebase/lib/shapes";
import { setConfig, getConfig, callCount, pick } from "#codebase/lib/config";
import { code, codeOf, make, limits, box, unbox, Pair, firstOf } from "#codebase/lib/tables";
import { platform } from "#codebase/lib/host";
import { boxed } from "#codebase/lib/merged";

export function main() {
  const s = new Shape(Kind.Square, 3);
  setConfig({ verbose: true });
  console.log(s.area(), s.kind, unwrap({ value: 42 }) + 1, getConfig().verbose, callCount(), pick("p"));
  console.log("tables", code("c"), codeOf("z"), make(4).describe(), Object.isFrozen(limits), unbox(box(7)) + 1, firstOf(Pair(5, 6)));
  console.log("host", platform() === process.platform, boxed(9).value);
}
`,
  );
  tcm("add");
  const ran = tcm("run", "app.main");
  assert.match(ran, /9 square 43 true 1 p/);
  assert.match(
    ran,
    /tables 2 255 n=4 true 8 5/,
    "the statements setting up each constant ran with it",
  );
  assert.match(
    ran,
    /host true 9/,
    "a declared global is the host's; a local value beside an imported type is the local",
  );
}

uses