// testkit.mjs — what the fidelity gates share: the corpus from Go's // golden file, a seeded generator, and an editor without a DOM. The // gates drive EditorState transactions directly, with the same plugins // the page will run; no view is needed, so none is made. Not bundled. import { readFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { EditorState } from 'prosemirror-state' import { history } from 'prosemirror-history' import { createSchema } from './schema.mjs' import { mount, fidelityPlugin } from './fidelity.mjs' const here = dirname(fileURLToPath(import.meta.url)) export const goldenPath = process.env.MAPA_GOLDEN || join(here, '..', 'internal', 'mapa', 'markup', 'testdata', 'golden.json') export const localCorpus = Boolean(process.env.MAPA_GOLDEN) // corpus is every golden body, read a line at a time (parity.test.mjs). export function corpus () { const bytes = readFileSync(goldenPath) const bodies = [] for (let start = 0; start < bytes.length;) { let end = bytes.indexOf(10, start) if (end < 0) end = bytes.length const line = bytes.toString('utf8', start, end).replace(/,$/u, '') start = end + 1 if (line !== '[' && line !== ']' && line !== '') { const { name, body } = JSON.parse(line) bodies.push({ name, body }) } } return bodies } // random is mulberry32, seeded from a string, so each body has its own // fixed sequence. export function random (seedText) { let seed = 2166136261 for (const char of seedText) seed = Math.imul(seed ^ char.codePointAt(0), 16777619) const next = () => { seed = (seed + 0x6d2b79f5) | 0 let value = Math.imul(seed ^ (seed >>> 15), 1 | seed) value = (value + Math.imul(value ^ (value >>> 7), 61 | value)) ^ value return ((value ^ (value >>> 14)) >>> 0) / 4294967296 } next.int = n => Math.floor(next() * n) next.pick = list => list[next.int(list.length)] return next } export const schema = createSchema() // editor is a mounted body in an EditorState with history and the // fidelity plugin, behind the small part of EditorView's interface the // fidelity functions use (state and dispatch). export function editor (body, options = {}) { const { doc, fidelity } = mount(schema, body) const state = EditorState.create({ doc, plugins: [ history(), fidelityPlugin(fidelity, { composing: options.composing ?? (() => false) }), ...(options.plugins?.(fidelity) ?? []) ] }) const view = { state, dispatch (tr) { this.state = this.state.apply(tr) }, // run is a prosemirror command against this view. run (command) { return command(this.state, tr => this.dispatch(tr)) } } return { view, fidelity } }