testEditAddsWhereItChecks
A constant added to a scratch file (tcm edit) whose code uses it, as its value: written before what uses it, so
the file typechecks, and the file's import of it goes.
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 testEditAddsWhereItChecks = () => { const dir = tempDir("edit adds where it checks"); const ok = (...args: string[]) => okIn(dir, ...args); ok("init"); fs.rmSync(path.join(dir, "scratch.ts"), { force: true }); fs.writeFileSync( path.join(dir, "lib.ts"), `// tcm: namespace lib\nexport type Body = { n: number };\nexport const BASE: Body = { n: 1 };\nexport const USER = BASE.n + 1;\n`, ); ok("add", "lib.ts"); ok("edit", "lib.Body", "lib.USER"); const scratch = path.join(dir, "scratch.ts"); fs.writeFileSync( scratch, fs .readFileSync(scratch, "utf8") .replace("n: number }", "n: number; m?: number }") .replace("BASE.n + 1", "BASE.n + 2"), ); ok("edit", "lib.BASE"); const text = fs.readFileSync(scratch, "utf8"); assert.doesNotMatch(text, /import \{[^}]*\bBASE\b/, text); assert.ok(text.indexOf("export const BASE") < text.indexOf("export const USER"), text); ok("check"); const out = ok("update"); assert.match(out, /~ update +lib\.Body/, out); assert.equal(ok("run", "lib.USER").trim(), "3"); }