testAddToScratch
Definitions added to a scratch file go after what's in it, their imports with its imports, without repeating any it has.
import { addToScratch } from "#codebase/tcmts/cli/main"; import assert from "node:assert/strict"; export const testAddToScratch = () => { const existing = `// tcm: namespace lib\nimport { a } from "#codebase/lib";\n\nexport const b = (): number => a + 1;\n`; const text = `import { a } from "#codebase/lib";\nimport fs from "node:fs";\n\nexport const c = (): string => fs.readFileSync("x", "utf8") + a;\n`; assert.equal( addToScratch("s.ts", existing, text), `// tcm: namespace lib\nimport { a } from "#codebase/lib";\nimport fs from "node:fs";\n\nexport const b = (): number => a + 1;\n\nexport const c = (): string => fs.readFileSync("x", "utf8") + a;\n`, ); // a name the file imports already, in an import of several, isn't imported again; the others are, into the import // from their module it has const grouped = `import { a, d } from "#codebase/lib";\nimport type { T } from "#codebase/lib";\n\nexport const e = (t: T): number => a + d + t.n;\n`; assert.equal( addToScratch("s.ts", existing, grouped), `// tcm: namespace lib\nimport { a, d } from "#codebase/lib";\nimport type { T } from "#codebase/lib";\n\nexport const b = (): number => a + 1;\n\nexport const e = (t: T): number => a + d + t.n;\n`, ); assert.equal( addToScratch( "s.ts", "export const x = 1;\n", `import fs from "node:fs";\n\nexport const y = fs;\n`, ), `import fs from "node:fs";\n\nexport const x = 1;\n\nexport const y = fs;\n`, ); }