import { readdir, readFile } from 'node:fs/promises' import { join } from 'node:path' import { EQUINE_DRAFT_DIR, EQUINE_LEGACY_IMPORT_DIR, EQUINE_SOURCE_PATHS, renderArtifacts, renderDraftArtifacts, sha256Hex, type AssetRecord, type DraftResult, } from '../src' import { REPO_ROOT, runDraft } from './helpers' const legacyDir = join(REPO_ROOT, EQUINE_LEGACY_IMPORT_DIR) const draftDir = join(REPO_ROOT, EQUINE_DRAFT_DIR) let legacy: Record let draftFiles: Record let draft: DraftResult /** Recursive file list, relative to `dir`, with forward slashes. */ async function listFiles(dir: string): Promise { const entries = await readdir(dir, { recursive: true, withFileTypes: true }) return entries .filter((entry) => entry.isFile()) .map((entry) => join(entry.parentPath, entry.name).slice(dir.length + 1)) } beforeAll(async () => { const run = await runDraft() draft = run.draft legacy = await renderArtifacts(run.result) draftFiles = await renderDraftArtifacts(run.draft, run.result.anomalies) }) describe('committed legacy-import folder (golden)', () => { it.each([ 'document.json', 'legacy-id-map.json', 'assets.json', 'IMPORT-REPORT.md', ])('%s equals a fresh run (rerun pnpm import:equine)', async (file) => { expect(await readFile(join(legacyDir, file), 'utf8')).toBe(legacy[file]) }) it('carries no SQL: the draft folder holds the only seed', () => { expect(Object.keys(legacy).some((file) => file.endsWith('.sql'))).toBe( false, ) }) it('copies the SVG assets byte for byte with the recorded sha256', async () => { const assets = JSON.parse(legacy['assets.json']!) as AssetRecord[] const sources = [EQUINE_SOURCE_PATHS.mainSvg, EQUINE_SOURCE_PATHS.headSvg] for (const [index, asset] of assets.entries()) { const copy = await readFile(join(legacyDir, 'assets', asset.file)) const original = await readFile(join(REPO_ROOT, sources[index]!)) expect(copy.equals(original)).toBe(true) expect(sha256Hex(copy)).toBe(asset.sha256) expect(copy.byteLength).toBe(asset.byte_size) } }) }) describe('committed draft folder (golden)', () => { it('equals a fresh run for every produced file (rerun pnpm import:equine)', async () => { for (const [file, content] of Object.entries(draftFiles)) { expect(await readFile(join(draftDir, file), 'utf8'), file).toBe(content) } }) it('holds no file the run does not produce', async () => { const expected = [ ...Object.keys(draftFiles), ...draft.assets.map((asset) => `assets/${asset.file}`), ] expect((await listFiles(draftDir)).sort()).toEqual(expected.sort()) }) it('holds the cleaned drawing with the recorded sha256', async () => { const [asset] = draft.assets const bytes = await readFile(join(draftDir, 'assets', asset!.file)) expect(bytes.toString('utf8')).toBe(draft.cleanedSvg) expect(sha256Hex(bytes)).toBe(asset!.sha256) expect(bytes.byteLength).toBe(asset!.byte_size) }) })