oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1// parity.test.mjs — gate L4 (spec §7): markup.mjs gives Go's Parse and
2// Spans on every corpus body, and the schema's toDOM gives Go's
3// RenderPage HTML, byte for byte. Go writes the golden file
4// (make golden); MAPA_GOLDEN points this at another one, which is how
5// make corpus-local runs it over Markdown read in place.
6
7import { test } from 'node:test'
8import assert from 'node:assert/strict'
9import { readFileSync } from 'node:fs'
10import { dirname, join } from 'node:path'
11import { fileURLToPath } from 'node:url'
12import { parse, plain, spans, Code } from './markup.mjs'
13import { createSchema } from './schema.mjs'
14import { fromBlocks, fromMarkdown } from './fromMarkdown.mjs'
15import { toHTML } from './domhtml.mjs'
16import { guess } from './resolutions.mjs'
17import { useDates } from './dates.mjs'
18
19const here = dirname(fileURLToPath(import.meta.url))
20const goldenPath = process.env.MAPA_GOLDEN ||
21 join(here, '..', 'internal', 'mapa', 'markup', 'testdata', 'golden.json')
22// The golden file holds one body per line, so a large local corpus is
23// read a line at a time rather than as one string.
24const goldenBytes = readFileSync(goldenPath)
25
26function * goldenBodies () {
27 for (let start = 0; start < goldenBytes.length;) {
28 let end = goldenBytes.indexOf(10, start)
29 if (end < 0) end = goldenBytes.length
30 const line = goldenBytes.toString('utf8', start, end).replace(/,$/u, '')
31 start = end + 1
32 if (line !== '[' && line !== ']' && line !== '') yield JSON.parse(line)
33 }
34}
35
36const goldenCount = [...goldenBodies()].length
37
38// The schema resolves links from the body being drawn: the answers Go's
39// resolver gave, as the editor's data island would carry them.
40let answers = new Map()
41let images = new Map()
42const key = (target, anchor) => `${target}\u0000${anchor}`
43const schema = createSchema((target, anchor) => {
44 const answer = answers.get(key(target, anchor))
45 if (!answer) throw new Error(`Go resolved no link [[${target}#^${anchor}]]`)
46 return answer
47}, id => {
48 const answer = images.get(id)
49 if (!answer) throw new Error(`Go resolved no image ${id}`)
50 return answer
51})
52
53function blockFields (block) {
54 const { kind, level, ordered, marker, start, end, source, column } = block
55 const fields = { kind, level, ordered, marker, start, end, source }
56 if (column) fields.column = column
57 return fields
58}
59
60function spanFields (block) {
61 if (block.kind === Code) return []
62 return spans(plain(block))
63}
64
65// mismatches runs check on every golden body and names the ones that
66// differ, so a failure says which body to look at.
67function mismatches (check) {
68 const failed = []
69 for (const body of goldenBodies()) {
70 try {
71 check(body)
72 } catch (error) {
73 failed.push(`${body.name}: ${error.message}`)
74 }
75 }
76 return failed
77}
78
79function report (failed) {
80 assert.equal(failed.length, 0,
81 `${failed.length} of ${goldenCount} bodies differ:\n` +
82 failed.slice(0, 5).join('\n\n'))
83}
84
85test('golden file has bodies', () => {
86 assert.ok(goldenCount > 0, `${goldenPath} holds no bodies`)
87})
88
89test('markup.mjs parses every body into Go\'s blocks, byte offsets and all', () => {
90 report(mismatches(body => {
91 const blocks = parse(body.body)
92 assert.deepEqual(blocks.map(blockFields), body.blocks.map(blockFields))
93 }))
94})
95
96test('markup.mjs strips and spans every block as Go does', () => {
97 report(mismatches(body => {
98 const blocks = parse(body.body)
99 assert.deepEqual(blocks.map(plain), body.blocks.map(block => block.plain))
100 assert.deepEqual(blocks.map(spanFields),
101 body.blocks.map(block => block.spans))
102 }))
103})
104
105test('the schema\'s toDOM draws every body as Go renders it', () => {
106 report(mismatches(body => {
107 answers = new Map(body.links.map(link =>
108 [key(link.target, link.anchor), link]))
109 images = new Map((body.images ?? []).map(image => [image.id, image]))
110 const doc = fromBlocks(schema, parse(body.body))
111 doc.check()
112 assert.equal(toHTML(schema, doc), body.html)
113 }))
114})
115
116// The four outputs spec §5 pins, held without Go as well.
117test('emphasis draws the four pinned outputs', () => {
118 answers = new Map()
119 const pinned = {
120 '*a **b** c*': '<p><em>a <strong>b</strong> c</em></p>',
121 '***x***': '<p><em><strong>x</strong></em></p>',
122 '**a** ***b*** **c**': '<p><strong>a</strong> ' +
123 '<em><strong>b</strong></em> <strong>c</strong></p>',
124 '**a *b* c**': '<p>**a <em>b</em> c**</p>'
125 }
126 for (const [source, html] of Object.entries(pinned)) {
127 assert.equal(toHTML(schema, fromMarkdown(schema, source)), html, source)
128 if (process.env.MAPA_GOLDEN) continue // a local corpus need not hold them
129 const drawn = [...goldenBodies()].find(body => body.body === source)
130 assert.ok(drawn, `the corpus lacks ${source}`)
131 assert.equal(drawn.html, html, `Go draws ${source}`)
132 }
133})
134
135test('a body ends in the editor\'s empty paragraph', () => {
136 answers = new Map()
137 const empty = '<p><br class="ProseMirror-trailingBreak"></p>'
138 assert.equal(toHTML(schema, fromMarkdown(schema, '')), empty)
139 assert.equal(toHTML(schema, fromMarkdown(schema, '---')), `<hr>${empty}`)
140 assert.equal(toHTML(schema, fromMarkdown(schema, 'a')), '<p>a</p>')
141})
142
143// L4 in other locales (build note 85): Go's golden_locales.json draws
144// every dated corpus body in en-IE, en-US, de and fr. The editor draws
145// each with its date chips labelled by itself, through guess and
146// dates.mjs, from that locale's island data (locale.Client), never from
147// Go's answer, and must give Go's HTML byte for byte.
148const localesPath = join(here, '..', 'internal', 'mapa', 'markup', 'testdata', 'golden_locales.json')
149const dates = JSON.parse(readFileSync(join(here, '..', 'internal', 'mapa', 'locale', 'dates.json'), 'utf8'))
150
151test('the editor labels date chips as Go does, in every locale L4 holds', () => {
152 const localeBodies = JSON.parse(readFileSync(localesPath, 'utf8'))
153 const codes = new Set(localeBodies.map(body => body.name.split(' ')[0]))
154 assert.deepEqual([...codes], ['en-IE', 'en-US', 'de', 'fr'])
155 const failed = []
156 try {
157 for (const body of localeBodies) {
158 const code = body.name.split(' ')[0]
159 useDates({ code, ...dates.locales[code] })
160 answers = new Map(body.links.map(link => {
161 if (!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/u.test(link.target)) return [key(link.target, link.anchor), link]
162 const ours = guess(link.target, 2026)
163 if (ours.day !== link.day && !ours.broken) failed.push(`${body.name}: ${link.target} is ${ours.day}, Go ${link.day}`)
164 return [key(link.target, link.anchor), ours]
165 }))
166 try {
167 assert.equal(toHTML(schema, fromBlocks(schema, parse(body.body))), body.html)
168 } catch (error) {
169 failed.push(`${body.name}: ${error.message}`)
170 }
171 }
172 } finally {
173 useDates(null)
174 }
175 assert.deepEqual(failed.slice(0, 5), [])
176})
177