// parity.test.mjs — gate L4 (spec §7): markup.mjs gives Go's Parse and // Spans on every corpus body, and the schema's toDOM gives Go's // RenderPage HTML, byte for byte. Go writes the golden file // (make golden); MAPA_GOLDEN points this at another one, which is how // make corpus-local runs it over Markdown read in place. import { test } from 'node:test' import assert from 'node:assert/strict' import { readFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { parse, plain, spans, Code } from './markup.mjs' import { createSchema } from './schema.mjs' import { fromBlocks, fromMarkdown } from './fromMarkdown.mjs' import { toHTML } from './domhtml.mjs' import { guess } from './resolutions.mjs' import { useDates } from './dates.mjs' const here = dirname(fileURLToPath(import.meta.url)) const goldenPath = process.env.MAPA_GOLDEN || join(here, '..', 'internal', 'mapa', 'markup', 'testdata', 'golden.json') // The golden file holds one body per line, so a large local corpus is // read a line at a time rather than as one string. const goldenBytes = readFileSync(goldenPath) function * goldenBodies () { for (let start = 0; start < goldenBytes.length;) { let end = goldenBytes.indexOf(10, start) if (end < 0) end = goldenBytes.length const line = goldenBytes.toString('utf8', start, end).replace(/,$/u, '') start = end + 1 if (line !== '[' && line !== ']' && line !== '') yield JSON.parse(line) } } const goldenCount = [...goldenBodies()].length // The schema resolves links from the body being drawn: the answers Go's // resolver gave, as the editor's data island would carry them. let answers = new Map() let images = new Map() const key = (target, anchor) => `${target}\u0000${anchor}` const schema = createSchema((target, anchor) => { const answer = answers.get(key(target, anchor)) if (!answer) throw new Error(`Go resolved no link [[${target}#^${anchor}]]`) return answer }, id => { const answer = images.get(id) if (!answer) throw new Error(`Go resolved no image ${id}`) return answer }) function blockFields (block) { const { kind, level, ordered, marker, start, end, source, column } = block const fields = { kind, level, ordered, marker, start, end, source } if (column) fields.column = column return fields } function spanFields (block) { if (block.kind === Code) return [] return spans(plain(block)) } // mismatches runs check on every golden body and names the ones that // differ, so a failure says which body to look at. function mismatches (check) { const failed = [] for (const body of goldenBodies()) { try { check(body) } catch (error) { failed.push(`${body.name}: ${error.message}`) } } return failed } function report (failed) { assert.equal(failed.length, 0, `${failed.length} of ${goldenCount} bodies differ:\n` + failed.slice(0, 5).join('\n\n')) } test('golden file has bodies', () => { assert.ok(goldenCount > 0, `${goldenPath} holds no bodies`) }) test('markup.mjs parses every body into Go\'s blocks, byte offsets and all', () => { report(mismatches(body => { const blocks = parse(body.body) assert.deepEqual(blocks.map(blockFields), body.blocks.map(blockFields)) })) }) test('markup.mjs strips and spans every block as Go does', () => { report(mismatches(body => { const blocks = parse(body.body) assert.deepEqual(blocks.map(plain), body.blocks.map(block => block.plain)) assert.deepEqual(blocks.map(spanFields), body.blocks.map(block => block.spans)) })) }) test('the schema\'s toDOM draws every body as Go renders it', () => { report(mismatches(body => { answers = new Map(body.links.map(link => [key(link.target, link.anchor), link])) images = new Map((body.images ?? []).map(image => [image.id, image])) const doc = fromBlocks(schema, parse(body.body)) doc.check() assert.equal(toHTML(schema, doc), body.html) })) }) // The four outputs spec §5 pins, held without Go as well. test('emphasis draws the four pinned outputs', () => { answers = new Map() const pinned = { '*a **b** c*': '
a b c
', '***x***': 'x
', '**a** ***b*** **c**': 'a ' + 'b c
', '**a *b* c**': '**a b c**
' } for (const [source, html] of Object.entries(pinned)) { assert.equal(toHTML(schema, fromMarkdown(schema, source)), html, source) if (process.env.MAPA_GOLDEN) continue // a local corpus need not hold them const drawn = [...goldenBodies()].find(body => body.body === source) assert.ok(drawn, `the corpus lacks ${source}`) assert.equal(drawn.html, html, `Go draws ${source}`) } }) test('a body ends in the editor\'s empty paragraph', () => { answers = new Map() const empty = 'a
') }) // L4 in other locales (build note 85): Go's golden_locales.json draws // every dated corpus body in en-IE, en-US, de and fr. The editor draws // each with its date chips labelled by itself, through guess and // dates.mjs, from that locale's island data (locale.Client), never from // Go's answer, and must give Go's HTML byte for byte. const localesPath = join(here, '..', 'internal', 'mapa', 'markup', 'testdata', 'golden_locales.json') const dates = JSON.parse(readFileSync(join(here, '..', 'internal', 'mapa', 'locale', 'dates.json'), 'utf8')) test('the editor labels date chips as Go does, in every locale L4 holds', () => { const localeBodies = JSON.parse(readFileSync(localesPath, 'utf8')) const codes = new Set(localeBodies.map(body => body.name.split(' ')[0])) assert.deepEqual([...codes], ['en-IE', 'en-US', 'de', 'fr']) const failed = [] try { for (const body of localeBodies) { const code = body.name.split(' ')[0] useDates({ code, ...dates.locales[code] }) answers = new Map(body.links.map(link => { if (!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/u.test(link.target)) return [key(link.target, link.anchor), link] const ours = guess(link.target, 2026) if (ours.day !== link.day && !ours.broken) failed.push(`${body.name}: ${link.target} is ${ours.day}, Go ${link.day}`) return [key(link.target, link.anchor), ours] })) try { assert.equal(toHTML(schema, fromBlocks(schema, parse(body.body))), body.html) } catch (error) { failed.push(`${body.name}: ${error.message}`) } } } finally { useDates(null) } assert.deepEqual(failed.slice(0, 5), []) })