testLineageSaltsSeparateIdenticalCode
A lineage salt gives identical code separate identities.
import { analyzeProject } from "#codebase/tcmts/core/analyze"; import { dir, n } from "#codebase/tcmts/core/test/hash"; import assert from "node:assert/strict"; import fs from "node:fs"; import path from "node:path"; export const testLineageSaltsSeparateIdenticalCode = () => { const file = path.join(dir, `salted${n++}.ts`); fs.writeFileSync( file, `export const cacheA = new Map<string, number>();\nexport const cacheB = new Map<string, number>();\n`, ); const run = (saltFor?: (f: string, name: string) => string | undefined) => Object.fromEntries( analyzeProject([file], { cacheDir: dir, resolveName: () => "", saltFor }).defs.map((d) => [ d.nameHint, d.hash, ]), ); const unsalted = run(); assert.equal(unsalted.cacheA, unsalted.cacheB, "without salts, identical code is one definition"); const salted = run((_f, name) => `lib.${name}`); assert.notEqual(salted.cacheA, salted.cacheB, "two `new Map()`s are two maps"); assert.deepEqual( run((_f, name) => `lib.${name}`), salted, "the same salts reproduce the same hashes", ); }