| 1 | // fixtures/generate-random-decks.mjs <seed> <dir> <manifest.json> — random |
| 2 | // decks from Slides' own model, as Yjs updates, for internal/slidestest's |
| 3 | // TestFontTheGateCoversEveryFaceADeckUses (Docs' generate-random-docs.mjs, |
| 4 | // adopted to a model that is not one ProseMirror document). |
| 5 | // |
| 6 | // <dir> holds <n>.base for each deck the test made: the deck's log as the |
| 7 | // updates wire frames it (frames.mjs). For each, this writes <n>.bin, one |
| 8 | // update that, applied on top, replaces the deck with a random one. The |
| 9 | // test posts it through the ordinary write path (POST /d/{sqid}/updates), |
| 10 | // so the deck passes the server's own check. |
| 11 | // |
| 12 | // Nothing here is a hand-kept list. The vocabularies are the model's: |
| 13 | // block types, text-bearing types, templates, layouts, regions and sizes |
| 14 | // from ops-model.mjs (the core's Model, mirrored), the marks from the |
| 15 | // text schema (schema.mjs), and the themes and font stacks from the font |
| 16 | // manifest the server sends a page (fontgate.go), which the test writes |
| 17 | // to <manifest.json>. Text goes in through the model's own writers: ops |
| 18 | // with Markdown for blocks and notes (applyOps, checked like any batch), |
| 19 | // writeText for table cells, which no op carries. So a block type, a |
| 20 | // template, a mark or a theme added to the model is generated without |
| 21 | // anybody remembering to (Docs' review 5: code blocks were missing from |
| 22 | // a hand list, and the gate missed their family). |
| 23 | // |
| 24 | // Steps, items, talk and arrive are set by number, never drawn, so a seed |
| 25 | // draws the decks it drew before them. Talk is never "parked": the viewer |
| 26 | // hides a parked slide (spec 2026-11-18 §3), and the tests open them all. |
| 27 | // |
| 28 | // It refuses to finish unless every block type, template, layout, theme, |
| 29 | // arrive, items value and mark appeared, every mark in a table cell, and |
| 30 | // every shape of text (none, empty, trailing, leading and between |
| 31 | // empties, words) in every type the editor binds and in a cell on the |
| 32 | // editor's first slide, counted on the decks as written: each update |
| 33 | // decoded back through view.mjs, the way the server will read it (Docs' |
| 34 | // review 6). Seeded (seeded.mjs), so a failure reproduces. |
| 35 | import * as Y from 'yjs' |
| 36 | import { readFileSync, readdirSync, writeFileSync } from 'node:fs' |
| 37 | import { join } from 'node:path' |
| 38 | import { applyOps, view } from './ops.mjs' |
| 39 | import { BLOCK_TYPES, TEXT_TYPES, TEMPLATES, LAYOUTS, REGIONS, SIZES, ITEMS, ARRIVALS } from './ops-model.mjs' |
| 40 | import { schema, writeText } from './schema.mjs' |
| 41 | import { parseFrames } from './frames.mjs' |
| 42 | import { roots, getArray, orderedMaps } from './ydoc.mjs' |
| 43 | import { checkDoc } from './check.mjs' |
| 44 | import { FAMILY_KEYS } from './font-families.mjs' |
| 45 | import { BOUND_TYPES } from './stage.mjs' |
| 46 | import { seeded } from './seeded.mjs' |
| 47 | |
| 48 | const [seed = '1', dir, manifestPath] = process.argv.slice(2) |
| 49 | if (!dir || !manifestPath) { |
| 50 | console.error('usage: node generate-random-decks.mjs <seed> <dir> <manifest.json>') |
| 51 | process.exit(64) |
| 52 | } |
| 53 | const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) |
| 54 | |
| 55 | const { rand, pick, chance } = seeded(seed) |
| 56 | const ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567' |
| 57 | const id = () => Array.from({ length: 12 }, () => ALPHABET[Math.floor(rand() * 32)]).join('') |
| 58 | |
| 59 | // The marks are the schema's; the Markdown that carries each one is the |
| 60 | // op vocabulary's (markdown.mjs), and the cell writer takes the run's own |
| 61 | // shape (schema.mjs runAttributes). |
| 62 | const MARKS = Object.keys(schema.marks) |
| 63 | const MARKDOWN = { b: t => `**${t}**`, i: t => `*${t}*`, c: t => '`' + t + '`', h: t => `==${t}==`, href: t => `[${t}](https://example.com/${t})` } |
| 64 | for (const m of MARKS) if (!MARKDOWN[m]) throw new Error(`generate-random-decks: no Markdown for the schema's mark ${m}`) |
| 65 | |
| 66 | const THEMES = Object.keys(manifest.themes) |
| 67 | // Font overrides: the theme's own (none), each family's own stack, a |
| 68 | // system face, a stack that names a family after a system face, one |
| 69 | // that fails the allowlist, and the stacks every theme uses. |
| 70 | const STACKS = [ |
| 71 | '', |
| 72 | ...FAMILY_KEYS.map(k => manifest.families[k].css), |
| 73 | 'system-ui, sans-serif', |
| 74 | 'Georgia, serif', |
| 75 | 'ui-monospace, monospace', |
| 76 | ...FAMILY_KEYS.map(k => `Arial, '${manifest.families[k].css}', sans-serif`), |
| 77 | 'url(x), serif', |
| 78 | ...new Set(Object.values(manifest.themes).flat()) |
| 79 | ] |
| 80 | |
| 81 | const WORDS = ['alpha', 'Bravo', 'charlie', 'Delta', 'office', 'affidavit', 'AVATAR', 'quietly', 'Wolverhampton', 'fluffy', 'fine', 'naïve'] |
| 82 | const words = () => Array.from({ length: 1 + Math.floor(rand() * 4) }, () => pick(WORDS)).join(' ') |
| 83 | |
| 84 | // markdownText is a paragraph or two, each mark on a span with some |
| 85 | // chance; force names marks that must appear. |
| 86 | function markdownText (force = []) { |
| 87 | const parts = [] |
| 88 | for (let p = 0; p < 1 + Math.floor(rand() * 2); p++) { |
| 89 | const spans = [] |
| 90 | for (let s = 0; s < 1 + Math.floor(rand() * 4); s++) { |
| 91 | let t = words() |
| 92 | const m = force.length ? force.shift() : chance(0.4) ? pick(MARKS) : null |
| 93 | if (m) t = MARKDOWN[m](t) |
| 94 | spans.push(t) |
| 95 | } |
| 96 | parts.push(spans.join(' ')) |
| 97 | } |
| 98 | return parts.join('\n\n') |
| 99 | } |
| 100 | |
| 101 | // cellRuns is a cell's paragraphs in the view's run shape, for writeText. |
| 102 | function cellRuns (force) { |
| 103 | const run = { t: words() } |
| 104 | const m = force || (chance(0.5) ? pick(MARKS) : null) |
| 105 | if (m === 'href') run.href = 'https://example.com/cell' |
| 106 | else if (m) run[m] = true |
| 107 | return [[{ t: pick(WORDS) + ' ' }, run]] |
| 108 | } |
| 109 | |
| 110 | function propsFor (type) { |
| 111 | switch (type) { |
| 112 | case 'heading': return { level: 1 + Math.floor(rand() * 3) } |
| 113 | case 'text': return { size: pick(SIZES), align: pick(['start', 'center', 'end']) } |
| 114 | case 'list': return { style: pick(['bullet', 'number']), size: pick(SIZES), indent: Math.floor(rand() * 2) } |
| 115 | case 'quote': return { cite: chance(0.5) ? words() : '' } |
| 116 | case 'code': return { language: pick(['go', 'js', '']), size: pick(SIZES) } |
| 117 | case 'table': return { rows: 1 + Math.floor(rand() * 2), cols: 1 + Math.floor(rand() * 3), header: chance(0.5) } |
| 118 | case 'shape': return { kind: pick(['rect', 'ellipse', 'line', 'arrow']), fill: '#ddd' } |
| 119 | case 'spacer': return { size: pick(SIZES) } |
| 120 | case 'columns': return { count: 2 } |
| 121 | case 'image': return { alt: 'nothing here', fit: pick(['cover', 'contain']) } |
| 122 | default: return {} |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // The shapes a text can have that the renderer and the editor could |
| 127 | // draw differently (Astra's finding 3 on #27: trailing empty paragraphs |
| 128 | // in a heading): no paragraphs at all, one empty one, empties before, |
| 129 | // between and after words. Every text-bearing type the editor binds |
| 130 | // (stage.mjs's BOUND_TYPES) and every table cell gets every shape |
| 131 | // across the set, on the slide the editor opens on. |
| 132 | const SHAPES = ['none', 'empty', 'trailing', 'leading', 'between', 'words'] |
| 133 | function runs () { |
| 134 | const out = [] |
| 135 | for (let k = 0; k < 1 + Math.floor(rand() * 3); k++) { |
| 136 | const r = { t: (k ? ' ' : '') + words() } |
| 137 | if (chance(0.5)) { |
| 138 | const m = pick(MARKS) |
| 139 | if (m === 'href') r.href = 'https://example.com/' + k |
| 140 | else r[m] = true |
| 141 | } |
| 142 | out.push(r) |
| 143 | } |
| 144 | return out |
| 145 | } |
| 146 | function shaped (shape) { |
| 147 | switch (shape) { |
| 148 | case 'none': return [] |
| 149 | case 'empty': return [[]] |
| 150 | case 'trailing': return [runs(), [], []] |
| 151 | case 'leading': return [[], runs()] |
| 152 | case 'between': return [runs(), [], runs()] |
| 153 | default: return [runs(), runs()] |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | // A deck: deck i covers its share of the vocabularies in turn, so a |
| 158 | // handful of decks covers them all, and the rest of each is random. Its |
| 159 | // first slide — the one the editor opens on — carries one block of every |
| 160 | // type the editor binds and a table, each text in a shape, so every |
| 161 | // shape meets every type there across the set. |
| 162 | function buildDeck (doc, i) { |
| 163 | const r = roots(doc) |
| 164 | const ops = [] |
| 165 | for (const [, m] of orderedMaps(r.slides)) ops.push({ op: 'remove_slide', id: m.get('id') }) |
| 166 | const fonts = chance(0.6) ? null : { heading: STACKS[(i * 3) % STACKS.length], body: STACKS[(i * 5 + 1) % STACKS.length] } |
| 167 | ops.push({ op: 'set_meta', title: `Random ${i}`, theme: THEMES[i % THEMES.length], fonts, arrive: ARRIVALS[i % ARRIVALS.length] }) |
| 168 | const cells = [] |
| 169 | const shapedTexts = [] |
| 170 | const slides = 1 + Math.floor(rand() * 3) |
| 171 | for (let s = 0; s < slides; s++) { |
| 172 | const sid = id() |
| 173 | const layout = (i + s) % 4 === 3 ? 'canvas' : pick(LAYOUTS) |
| 174 | ops.push({ op: 'add_slide', id: sid, layout, template: TEMPLATES[(i * 2 + s) % TEMPLATES.length] }) |
| 175 | if (s > 0 && (i + s) % 2 === 0) ops.push({ op: 'set_slide', id: sid, talk: 'full' }) |
| 176 | if (s === 0) { |
| 177 | ;[...BOUND_TYPES, 'table'].forEach((type, k) => { |
| 178 | const bid = id() |
| 179 | const op = { op: 'add_block', slide: sid, id: bid, type, props: propsFor(type) } |
| 180 | if (type === 'table') op.props = { rows: 2, cols: 2, header: chance(0.5) } |
| 181 | if (layout === 'canvas') op.box = { x: 0.02 + 0.15 * k, y: 0.05, w: 0.14, h: 0.6 } |
| 182 | else op.region = pick(REGIONS) |
| 183 | ops.push(op) |
| 184 | if (type === 'list') ops.push({ op: 'set_block', slide: sid, id: bid, items: ITEMS[i % ITEMS.length] }) |
| 185 | if (type === 'table') cells.push({ sid, bid, n: 4, shape: i + k }) |
| 186 | else shapedTexts.push({ sid, bid, shape: SHAPES[(i + k) % SHAPES.length] }) |
| 187 | }) |
| 188 | } |
| 189 | if (chance(0.5)) ops.push({ op: 'set_notes', id: sid, markdown: markdownText() }) |
| 190 | const blocks = 1 + Math.floor(rand() * 5) |
| 191 | for (let b = 0; b < blocks; b++) { |
| 192 | const type = b === 0 ? BLOCK_TYPES[(i + s) % BLOCK_TYPES.length] : pick(BLOCK_TYPES) |
| 193 | const bid = id() |
| 194 | const op = { op: 'add_block', slide: sid, id: bid, type, props: propsFor(type) } |
| 195 | if (layout === 'canvas') op.box = { x: rand() * 0.5, y: rand() * 0.5, w: 0.3 + rand() * 0.4, h: 0.2 + rand() * 0.3 } |
| 196 | else op.region = pick(REGIONS) |
| 197 | if (TEXT_TYPES.includes(type) && type !== 'shape') { |
| 198 | op.markdown = type === 'code' ? words() + '\n' + words() : markdownText(b === 0 ? [MARKS[(i + s) % MARKS.length]] : []) |
| 199 | } else if (type === 'shape') { |
| 200 | op.markdown = words() |
| 201 | } |
| 202 | ops.push(op) |
| 203 | if (b > 0) ops.push({ op: 'set_block', slide: sid, id: bid, step: b / 2 }) |
| 204 | if (type === 'table') cells.push({ sid, bid, n: op.props.rows * op.props.cols }) |
| 205 | } |
| 206 | } |
| 207 | applyOps(doc, ops, 'generate') |
| 208 | // The cells, which no op writes: the schema's own writer. |
| 209 | doc.transact(() => { |
| 210 | for (const { sid, bid, n, shape } of cells) { |
| 211 | const slide = orderedMaps(r.slides).map(([, m]) => m).find(m => m.get('id') === sid) |
| 212 | const block = orderedMaps(getArray(slide, 'blocks')).map(([, m]) => m).find(m => m.get('id') === bid) |
| 213 | const frags = getArray(block, 'cells').toArray() |
| 214 | for (let k = 0; k < Math.min(n, frags.length); k++) { |
| 215 | writeText(frags[k], shape === undefined ? cellRuns(k === 0 ? MARKS[(i + k) % MARKS.length] : null) : shaped(SHAPES[(shape + k) % SHAPES.length])) |
| 216 | } |
| 217 | } |
| 218 | for (const { sid, bid, shape } of shapedTexts) { |
| 219 | const slide = orderedMaps(r.slides).map(([, m]) => m).find(m => m.get('id') === sid) |
| 220 | const block = orderedMaps(getArray(slide, 'blocks')).map(([, m]) => m).find(m => m.get('id') === bid) |
| 221 | writeText(block.get('text'), shaped(shape)) |
| 222 | } |
| 223 | }, 'generate') |
| 224 | } |
| 225 | |
| 226 | // Coverage, counted on what is written. |
| 227 | const seen = { types: new Set(), templates: new Set(), layouts: new Set(), themes: new Set(), arrivals: new Set(), items: new Set(), marks: new Set(), cellMarks: new Set(), shapes: new Set() } |
| 228 | // shapeOf names a text's shape as SHAPES does, read back off the view. |
| 229 | function shapeOf (text) { |
| 230 | if (!Array.isArray(text) || text.length === 0) return 'none' |
| 231 | const empty = text.map(p => !p.some(r => r.t !== '')) |
| 232 | if (empty.every(Boolean)) return 'empty' |
| 233 | if (empty[empty.length - 1]) return 'trailing' |
| 234 | if (empty[0]) return 'leading' |
| 235 | if (empty.some(Boolean)) return 'between' |
| 236 | return 'words' |
| 237 | } |
| 238 | function record (doc) { |
| 239 | const v = view(doc) |
| 240 | seen.themes.add(v.meta.theme) |
| 241 | if (v.meta.arrive) seen.arrivals.add(v.meta.arrive) |
| 242 | const markOf = run => MARKS.filter(m => (m === 'href' ? run.href != null : run[m])) |
| 243 | const first = v.slides[0] |
| 244 | for (const b of (first && first.blocks) || []) { |
| 245 | if (BOUND_TYPES.includes(b.type)) seen.shapes.add(b.type + ':' + shapeOf(b.text)) |
| 246 | if (b.type === 'table') for (const cell of (b.props && b.props.cells) || []) seen.shapes.add('cell:' + shapeOf(cell)) |
| 247 | } |
| 248 | for (const s of v.slides) { |
| 249 | seen.layouts.add(s.layout) |
| 250 | seen.templates.add(s.template) |
| 251 | for (const b of s.blocks) { |
| 252 | seen.types.add(b.type) |
| 253 | if (b.items) seen.items.add(b.items) |
| 254 | for (const p of b.text || []) for (const run of p) for (const m of markOf(run)) seen.marks.add(m) |
| 255 | for (const cell of (b.props && b.props.cells) || []) { |
| 256 | if (!Array.isArray(cell)) continue |
| 257 | for (const p of cell) for (const run of p) for (const m of markOf(run)) seen.cellMarks.add(m) |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | const bases = readdirSync(dir).filter(f => f.endsWith('.base')).sort((a, b) => parseInt(a) - parseInt(b)) |
| 264 | if (!bases.length) { |
| 265 | console.error(`generate-random-decks: no <n>.base files in ${dir}`) |
| 266 | process.exit(2) |
| 267 | } |
| 268 | for (const name of bases) { |
| 269 | const doc = new Y.Doc() |
| 270 | for (const f of parseFrames(new Uint8Array(readFileSync(join(dir, name))))) Y.applyUpdate(doc, f) |
| 271 | const before = Y.encodeStateVector(doc) |
| 272 | buildDeck(doc, parseInt(name)) |
| 273 | checkDoc(doc) |
| 274 | record(doc) |
| 275 | writeFileSync(join(dir, name.replace(/\.base$/, '.bin')), Y.encodeStateAsUpdate(doc, before)) |
| 276 | } |
| 277 | |
| 278 | const missing = [] |
| 279 | const want = { |
| 280 | types: BLOCK_TYPES, |
| 281 | templates: TEMPLATES, |
| 282 | layouts: LAYOUTS, |
| 283 | themes: THEMES, |
| 284 | arrivals: ARRIVALS, |
| 285 | items: ITEMS, |
| 286 | marks: MARKS, |
| 287 | cellMarks: MARKS, |
| 288 | shapes: [...BOUND_TYPES, 'cell'].flatMap(t => SHAPES.map(sh => t + ':' + sh)) |
| 289 | } |
| 290 | for (const [k, all] of Object.entries(want)) { |
| 291 | const gone = all.filter(x => !seen[k].has(x)) |
| 292 | if (gone.length) missing.push(`${k} [${gone}]`) |
| 293 | } |
| 294 | if (missing.length) { |
| 295 | console.error(`generate-random-decks: ${bases.length} decks as written never contain ${missing.join(', ')}`) |
| 296 | process.exit(2) |
| 297 | } |
| 298 | console.log(bases.length) |
| 299 | |