// Reproducible replacement for return #162's step 3 (its out-sha256 recomputation // from files it never published). Uses the SERVED tailfmt.normalize and the exact // capture rule of the SERVED embed.js (capturedIn, lines 246-248; outHash = sha(normalize(captured))). 'use strict'; const fs = require('fs'); const crypto = require('crypto'); const T = require('./research/qc/tailfmt'); const STDERR_MARK = '───── stderr ─────'; const SRC = 'research/verify-ladder-big.js'; const src = fs.readFileSync(SRC, 'utf8').replace(/\r\n/g, '\n'); const stdout = fs.readFileSync('out-big.txt', 'utf8').replace(/\r\n/g, '\n'); const stderr = fs.readFileSync('err-big.txt', 'utf8').replace(/\r\n/g, '\n'); const captured = String(stdout).replace(/\n*$/, '\n') + STDERR_MARK + '\n' + stderr; const norm = T.normalize(captured); const outHash = T.sha(norm); const field = (key) => { const m = src.split('\n').find((l) => l.startsWith('// ' + key + ':')); return m ? m.slice(m.indexOf(':') + 1).trim() : null; }; const recOut = field('out-sha256'); const recCode = field('code-sha256'); const recBodyLines = Number(field('body-lines')); const codeNow = T.sha(T.headText(src)); // The body is the `body-lines` lines that follow the header's closing rule — the same // rule the parser in tailfmt.js uses, so this does not have to guess where it ends. const rule = '// ' + '='.repeat(76); const lines = src.split('\n'); const banner = lines.findIndex((l) => l.startsWith('// OUTPUT — EMBEDDED')); const headerClose = lines.indexOf(rule, banner); const pasted = lines.slice(headerClose + 1, headerClose + 1 + recBodyLines) .map((l) => (l === '//' ? '' : l.startsWith('// ') ? l.slice(3) : l)) .join('\n').replace(/[ \t]+$/gm, '').replace(/\n+$/, ''); const body = String(captured).replace(/\n+$/, '') .split('\n').map((l) => (l ? '// ' + l.replace(/\s+$/, '') : '//')); const line = (k, v) => console.log(k.padEnd(34), v); console.log('files (this run)'); line(' sha256(out-big.txt)', crypto.createHash('sha256').update(fs.readFileSync('out-big.txt')).digest('hex')); line(' sha256(err-big.txt)', crypto.createHash('sha256').update(fs.readFileSync('err-big.txt')).digest('hex')); console.log('fingerprint, recomputed from those two files'); line(' code-sha256', codeNow + (codeNow === recCode ? ' MATCH (recorded)' : ' DIFFERS from ' + recCode)); line(' out-sha256', outHash + (outHash === recOut ? ' MATCH (recorded)' : ' DIFFERS from ' + recOut)); line(' body-lines', body.length + ' recorded ' + recBodyLines + (body.length === recBodyLines ? ' MATCH' : ' DIFFERS')); const pastedNorm = T.normalize(pasted + '\n').replace(/\n+$/, ''); line(' normalized run output == normalize(pasted block)', pastedNorm === norm.replace(/\n+$/, '') ? 'IDENTICAL' : 'DIFFERS'); line(' raw pasted block == raw run output', pasted === captured.replace(/\n+$/, '') ? 'IDENTICAL' : 'DIFFERS (the elapsed minutes; that is what normalize exists to scrub)'); if (pastedNorm !== norm.replace(/\n+$/, '')) { const a = norm.replace(/\n+$/, '').split('\n'), b = pastedNorm.split('\n'); let shown = 0; for (let i = 0; i < Math.max(a.length, b.length) && shown < 8; i++) { if (a[i] !== b[i]) { console.log(` line ${i + 1}:\n mine: ${JSON.stringify(a[i])}\n recorded: ${JSON.stringify(b[i])}`); shown++; } } }