// fixtures/generate-random-docs.mjs — random // documents from the editor's own schema (fixtures/schema.mjs), as Yjs // updates, for internal/docs' TestFontTheGateCoversEveryFaceTheDocumentUses. // // Every node type and every mark comes from the schema, not from a list // written here: the generator walks each node type's content expression // (ContentMatch) and puts any mark the parent allows on its text. So a // node or mark added to the schema is generated without anyone // remembering to (branch review 5: code blocks were missing from the // hand list, and the gate missed their family). It refuses to finish // unless every node type and mark appeared at least once across the set. // Seeded, so a failure reproduces. import * as Y from 'yjs' import { prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror' import { writeFileSync, mkdirSync } from 'node:fs' import { Fragment } from 'prosemirror-model' import { schema } from './schema.mjs' const [seed = '1', count = '30', outdir = '/tmp/docs-random-docs'] = process.argv.slice(2) let state = Number(seed) >>> 0 || 1 const rand = () => { state = (state + 0x6d2b79f5) >>> 0 let t = state t = Math.imul(t ^ (t >>> 15), t | 1) t ^= t + Math.imul(t ^ (t >>> 7), t | 61) return ((t ^ (t >>> 14)) >>> 0) / 4294967296 } const pick = (a) => a[Math.floor(rand() * a.length)] const chance = (p) => rand() < p // Attributes a node or mark needs and has no default for. const REQUIRED = { image: { src: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=' }, link: { href: 'https://example.com/' } } function attrsFor(type) { const out = { ...(REQUIRED[type.name] || {}) } if (type.name === 'heading') out.level = 1 + Math.floor(rand() * 6) return out } const WORDS = ['alpha', 'Bravo', 'charlie', 'Delta', 'office', 'affidavit', 'AVATAR', 'quietly', 'Wolverhampton', 'fluffy'] function words() { if (chance(0.15)) return ' '.repeat(1 + Math.floor(rand() * 3)) return Array.from({ length: 1 + Math.floor(rand() * 5) }, () => pick(WORDS)).join(' ') + (chance(0.5) ? ' ' : '') } // marksFor is a random set of marks the parent allows; Mark.addToSet // applies the schema's own exclusions, so marks that cannot coexist do // not. function marksFor(parent) { let set = [] for (const type of Object.values(schema.marks)) { if (!parent.allowsMarkType(type) || !chance(0.12)) continue set = type.create(attrsFor(type)).addToSet(set) } return set } // fill builds random content for a node type by walking its content // expression: at each point, end if allowed and the dice say so, else // take a random edge the expression offers. createAndFill completes // whatever the expression still requires. function fill(type, depth) { const children = [] let match = type.contentMatch for (let i = 0; i < 8; i++) { if (match.validEnd && (chance(0.3) || depth > 3 || i > 5)) break const edges = [] for (let e = 0; e < match.edgeCount; e++) edges.push(match.edge(e)) if (!edges.length) break // Deep down, prefer what ends the recursion. const shallow = edges.filter((e) => e.type.isInline || e.type.isLeaf) const { type: child, next } = pick(depth > 2 && shallow.length ? shallow : edges) if (child.isText) children.push(schema.text(words(), marksFor(type))) else if (child.isLeaf) children.push(child.create(attrsFor(child), null, child.isInline ? marksFor(type) : undefined)) else children.push(fill(child, depth + 1)) match = next } return type.createAndFill(attrsFor(type), Fragment.from(children)) } mkdirSync(outdir, { recursive: true }) // Coverage is counted on what is actually written: each update decoded // back through the schema, the way the browser will receive it (branch // review 6: marks on inline leaves are lost in the Yjs conversion, and a // mark counted when it was attempted could be absent from every file). // Failed attempts count for nothing. If the requested documents leave a // node or mark out, more are generated, up to a limit. const emittedNodes = new Set() const emittedMarks = new Set() function record(update) { const ydoc = new Y.Doc() Y.applyUpdate(ydoc, update) const root = yXmlFragmentToProseMirrorRootNode(ydoc.getXmlFragment('prosemirror'), schema) emittedNodes.add(root.type.name) root.descendants((n) => { emittedNodes.add(n.type.name) for (const m of n.marks) emittedMarks.add(m.type.name) }) } const covered = () => Object.keys(schema.nodes).every((k) => emittedNodes.has(k)) && Object.keys(schema.marks).every((k) => emittedMarks.has(k)) const n = Number(count) let written = 0 while (written < n || (!covered() && written < n * 5)) { let doc = null for (let tries = 0; tries < 20 && !doc; tries++) { doc = fill(schema.nodes.doc, 0) try { doc && doc.check() } catch { doc = null } } if (!doc) throw new Error('generate-random-docs: could not build a valid document') const ydoc = new Y.Doc() ydoc.clientID = 1 prosemirrorToYXmlFragment(doc, ydoc.getXmlFragment('prosemirror')) const update = Y.encodeStateAsUpdate(ydoc) writeFileSync(`${outdir}/${written}.bin`, update) record(update) written++ } const missingNodes = Object.keys(schema.nodes).filter((k) => !emittedNodes.has(k)) const missingMarks = Object.keys(schema.marks).filter((k) => !emittedMarks.has(k)) if (missingNodes.length || missingMarks.length) { console.error(`generate-random-docs: ${written} documents as written never contain nodes [${missingNodes}] or marks [${missingMarks}]`) process.exit(2) } console.log(written)