| 1 | // fixtures/generate-random-docs.mjs <seed> <count> <outdir> — random |
| 2 | // documents from the editor's own schema (fixtures/schema.mjs), as Yjs |
| 3 | // updates, for internal/docs' TestFontTheGateCoversEveryFaceTheDocumentUses. |
| 4 | // |
| 5 | // Every node type and every mark comes from the schema, not from a list |
| 6 | // written here: the generator walks each node type's content expression |
| 7 | // (ContentMatch) and puts any mark the parent allows on its text. So a |
| 8 | // node or mark added to the schema is generated without anyone |
| 9 | // remembering to (branch review 5: code blocks were missing from the |
| 10 | // hand list, and the gate missed their family). It refuses to finish |
| 11 | // unless every node type and mark appeared at least once across the set. |
| 12 | // Seeded, so a failure reproduces. |
| 13 | import * as Y from 'yjs' |
| 14 | import { prosemirrorToYXmlFragment, yXmlFragmentToProseMirrorRootNode } from 'y-prosemirror' |
| 15 | import { writeFileSync, mkdirSync } from 'node:fs' |
| 16 | import { Fragment } from 'prosemirror-model' |
| 17 | import { schema } from './schema.mjs' |
| 18 | |
| 19 | const [seed = '1', count = '30', outdir = '/tmp/docs-random-docs'] = process.argv.slice(2) |
| 20 | |
| 21 | let state = Number(seed) >>> 0 || 1 |
| 22 | const rand = () => { |
| 23 | state = (state + 0x6d2b79f5) >>> 0 |
| 24 | let t = state |
| 25 | t = Math.imul(t ^ (t >>> 15), t | 1) |
| 26 | t ^= t + Math.imul(t ^ (t >>> 7), t | 61) |
| 27 | return ((t ^ (t >>> 14)) >>> 0) / 4294967296 |
| 28 | } |
| 29 | const pick = (a) => a[Math.floor(rand() * a.length)] |
| 30 | const chance = (p) => rand() < p |
| 31 | |
| 32 | |
| 33 | // Attributes a node or mark needs and has no default for. |
| 34 | const REQUIRED = { image: { src: 'data:image/gif;base64,R0lGODlhAQABAAAAACw=' }, link: { href: 'https://example.com/' } } |
| 35 | function attrsFor(type) { |
| 36 | const out = { ...(REQUIRED[type.name] || {}) } |
| 37 | if (type.name === 'heading') out.level = 1 + Math.floor(rand() * 6) |
| 38 | return out |
| 39 | } |
| 40 | |
| 41 | const WORDS = ['alpha', 'Bravo', 'charlie', 'Delta', 'office', 'affidavit', 'AVATAR', 'quietly', 'Wolverhampton', 'fluffy'] |
| 42 | function words() { |
| 43 | if (chance(0.15)) return ' '.repeat(1 + Math.floor(rand() * 3)) |
| 44 | return Array.from({ length: 1 + Math.floor(rand() * 5) }, () => pick(WORDS)).join(' ') + (chance(0.5) ? ' ' : '') |
| 45 | } |
| 46 | |
| 47 | // marksFor is a random set of marks the parent allows; Mark.addToSet |
| 48 | // applies the schema's own exclusions, so marks that cannot coexist do |
| 49 | // not. |
| 50 | function marksFor(parent) { |
| 51 | let set = [] |
| 52 | for (const type of Object.values(schema.marks)) { |
| 53 | if (!parent.allowsMarkType(type) || !chance(0.12)) continue |
| 54 | set = type.create(attrsFor(type)).addToSet(set) |
| 55 | } |
| 56 | return set |
| 57 | } |
| 58 | |
| 59 | // fill builds random content for a node type by walking its content |
| 60 | // expression: at each point, end if allowed and the dice say so, else |
| 61 | // take a random edge the expression offers. createAndFill completes |
| 62 | // whatever the expression still requires. |
| 63 | function fill(type, depth) { |
| 64 | const children = [] |
| 65 | let match = type.contentMatch |
| 66 | for (let i = 0; i < 8; i++) { |
| 67 | if (match.validEnd && (chance(0.3) || depth > 3 || i > 5)) break |
| 68 | const edges = [] |
| 69 | for (let e = 0; e < match.edgeCount; e++) edges.push(match.edge(e)) |
| 70 | if (!edges.length) break |
| 71 | // Deep down, prefer what ends the recursion. |
| 72 | const shallow = edges.filter((e) => e.type.isInline || e.type.isLeaf) |
| 73 | const { type: child, next } = pick(depth > 2 && shallow.length ? shallow : edges) |
| 74 | if (child.isText) children.push(schema.text(words(), marksFor(type))) |
| 75 | else if (child.isLeaf) children.push(child.create(attrsFor(child), null, child.isInline ? marksFor(type) : undefined)) |
| 76 | else children.push(fill(child, depth + 1)) |
| 77 | match = next |
| 78 | } |
| 79 | return type.createAndFill(attrsFor(type), Fragment.from(children)) |
| 80 | } |
| 81 | |
| 82 | mkdirSync(outdir, { recursive: true }) |
| 83 | |
| 84 | // Coverage is counted on what is actually written: each update decoded |
| 85 | // back through the schema, the way the browser will receive it (branch |
| 86 | // review 6: marks on inline leaves are lost in the Yjs conversion, and a |
| 87 | // mark counted when it was attempted could be absent from every file). |
| 88 | // Failed attempts count for nothing. If the requested documents leave a |
| 89 | // node or mark out, more are generated, up to a limit. |
| 90 | const emittedNodes = new Set() |
| 91 | const emittedMarks = new Set() |
| 92 | function record(update) { |
| 93 | const ydoc = new Y.Doc() |
| 94 | Y.applyUpdate(ydoc, update) |
| 95 | const root = yXmlFragmentToProseMirrorRootNode(ydoc.getXmlFragment('prosemirror'), schema) |
| 96 | emittedNodes.add(root.type.name) |
| 97 | root.descendants((n) => { |
| 98 | emittedNodes.add(n.type.name) |
| 99 | for (const m of n.marks) emittedMarks.add(m.type.name) |
| 100 | }) |
| 101 | } |
| 102 | const covered = () => Object.keys(schema.nodes).every((k) => emittedNodes.has(k)) && Object.keys(schema.marks).every((k) => emittedMarks.has(k)) |
| 103 | |
| 104 | const n = Number(count) |
| 105 | let written = 0 |
| 106 | while (written < n || (!covered() && written < n * 5)) { |
| 107 | let doc = null |
| 108 | for (let tries = 0; tries < 20 && !doc; tries++) { |
| 109 | doc = fill(schema.nodes.doc, 0) |
| 110 | try { |
| 111 | doc && doc.check() |
| 112 | } catch { |
| 113 | doc = null |
| 114 | } |
| 115 | } |
| 116 | if (!doc) throw new Error('generate-random-docs: could not build a valid document') |
| 117 | const ydoc = new Y.Doc() |
| 118 | ydoc.clientID = 1 |
| 119 | prosemirrorToYXmlFragment(doc, ydoc.getXmlFragment('prosemirror')) |
| 120 | const update = Y.encodeStateAsUpdate(ydoc) |
| 121 | writeFileSync(`${outdir}/${written}.bin`, update) |
| 122 | record(update) |
| 123 | written++ |
| 124 | } |
| 125 | const missingNodes = Object.keys(schema.nodes).filter((k) => !emittedNodes.has(k)) |
| 126 | const missingMarks = Object.keys(schema.marks).filter((k) => !emittedMarks.has(k)) |
| 127 | if (missingNodes.length || missingMarks.length) { |
| 128 | console.error(`generate-random-docs: ${written} documents as written never contain nodes [${missingNodes}] or marks [${missingMarks}]`) |
| 129 | process.exit(2) |
| 130 | } |
| 131 | console.log(written) |
| 132 | |