testCodeFilesSavedAsWritten

tcmts.cli.test.e2e.files.testCodeFilesSavedAsWritten · #dtfl7nq5 · term

Code kept as written (an example's ui.tsx, a file in a marked directory) is edited like any file: tcm edit writes it out, and tcm update <file> --into <namespace> saves it back as written, not as definitions.

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 testCodeFilesSavedAsWritten = () => {
  const root = tempDir("code files");
  const proj = path.join(root, "proj");
  const cbDir = path.join(root, "cb");
  for (const d of ["src", "ext"]) fs.mkdirSync(path.join(proj, d), { recursive: true });
  fs.mkdirSync(cbDir);
  fs.writeFileSync(path.join(proj, "package.json"), JSON.stringify({ type: "module" }));
  fs.writeFileSync(
    path.join(proj, "src", "math.ts"),
    "export const square = (x: number): number => x * x;\n",
  );
  fs.writeFileSync(path.join(proj, "ext", ".tcm-files"), "");
  fs.writeFileSync(path.join(proj, "ext", "main.ts"), "export const one = 1;\n");
  assert.equal(spawnSync("git", ["-C", proj, "init", "-q"]).status, 0);
  const ok = (...args: string[]) => okIn(cbDir, ...args);
  ok("init");
  fs.rmSync(path.join(cbDir, "scratch.ts"));
  ok("import", proj, "--into", "app", "--mirror");
  assert.match(ok("ls", "app"), /app\.ext\.main +#\w+ +: ts/);
  ok("edit", "app.ext.main");
  assert.equal(fs.readFileSync(path.join(cbDir, "main.ts"), "utf8"), "export const one = 1;\n");
  fs.writeFileSync(path.join(cbDir, "main.ts"), "export const one = 2;\n");
  assert.match(ok("update", "main.ts", "--into", "app.ext"), /update +app\.ext\.main : ts/);
  assert.match(ok("view", "app.ext.main"), /export const one = 2;/);
  // a file, not code: nothing named for what's in it
  assert.doesNotMatch(ok("ls", "app"), /app\.ext\.one/);
  // a new one, added from a marked directory (not a mirror's), is a file too
  const marked = path.join(root, "more");
  fs.mkdirSync(marked);
  fs.writeFileSync(path.join(marked, ".tcm-files"), "");
  fs.writeFileSync(path.join(marked, "extra.ts"), "export const two = 2;\n");
  assert.match(
    ok("add", path.join(marked, "extra.ts"), "--into", "app.ext"),
    /new +app\.ext\.extra : ts/,
  );
  assert.doesNotMatch(ok("ls", "app"), /app\.ext\.two/);
}

uses