// fixtures/generate-random-decks.mjs — random // decks from Slides' own model, as Yjs updates, for internal/slidestest's // TestFontTheGateCoversEveryFaceADeckUses (Docs' generate-random-docs.mjs, // adopted to a model that is not one ProseMirror document). // // holds .base for each deck the test made: the deck's log as the // updates wire frames it (frames.mjs). For each, this writes .bin, one // update that, applied on top, replaces the deck with a random one. The // test posts it through the ordinary write path (POST /d/{sqid}/updates), // so the deck passes the server's own check. // // Nothing here is a hand-kept list. The vocabularies are the model's: // block types, text-bearing types, templates, layouts, regions and sizes // from ops-model.mjs (the core's Model, mirrored), the marks from the // text schema (schema.mjs), and the themes and font stacks from the font // manifest the server sends a page (fontgate.go), which the test writes // to . Text goes in through the model's own writers: ops // with Markdown for blocks and notes (applyOps, checked like any batch), // writeText for table cells, which no op carries. So a block type, a // template, a mark or a theme added to the model is generated without // anybody remembering to (Docs' review 5: code blocks were missing from // a hand list, and the gate missed their family). // // Steps, items, talk and arrive are set by number, never drawn, so a seed // draws the decks it drew before them. Talk is never "parked": the viewer // hides a parked slide (spec 2026-11-18 §3), and the tests open them all. // // It refuses to finish unless every block type, template, layout, theme, // arrive, items value and mark appeared, every mark in a table cell, and // every shape of text (none, empty, trailing, leading and between // empties, words) in every type the editor binds and in a cell on the // editor's first slide, counted on the decks as written: each update // decoded back through view.mjs, the way the server will read it (Docs' // review 6). Seeded (seeded.mjs), so a failure reproduces. import * as Y from 'yjs' import { readFileSync, readdirSync, writeFileSync } from 'node:fs' import { join } from 'node:path' import { applyOps, view } from './ops.mjs' import { BLOCK_TYPES, TEXT_TYPES, TEMPLATES, LAYOUTS, REGIONS, SIZES, ITEMS, ARRIVALS } from './ops-model.mjs' import { schema, writeText } from './schema.mjs' import { parseFrames } from './frames.mjs' import { roots, getArray, orderedMaps } from './ydoc.mjs' import { checkDoc } from './check.mjs' import { FAMILY_KEYS } from './font-families.mjs' import { BOUND_TYPES } from './stage.mjs' import { seeded } from './seeded.mjs' const [seed = '1', dir, manifestPath] = process.argv.slice(2) if (!dir || !manifestPath) { console.error('usage: node generate-random-decks.mjs ') process.exit(64) } const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')) const { rand, pick, chance } = seeded(seed) const ALPHABET = 'abcdefghijklmnopqrstuvwxyz234567' const id = () => Array.from({ length: 12 }, () => ALPHABET[Math.floor(rand() * 32)]).join('') // The marks are the schema's; the Markdown that carries each one is the // op vocabulary's (markdown.mjs), and the cell writer takes the run's own // shape (schema.mjs runAttributes). const MARKS = Object.keys(schema.marks) const MARKDOWN = { b: t => `**${t}**`, i: t => `*${t}*`, c: t => '`' + t + '`', h: t => `==${t}==`, href: t => `[${t}](https://example.com/${t})` } for (const m of MARKS) if (!MARKDOWN[m]) throw new Error(`generate-random-decks: no Markdown for the schema's mark ${m}`) const THEMES = Object.keys(manifest.themes) // Font overrides: the theme's own (none), each family's own stack, a // system face, a stack that names a family after a system face, one // that fails the allowlist, and the stacks every theme uses. const STACKS = [ '', ...FAMILY_KEYS.map(k => manifest.families[k].css), 'system-ui, sans-serif', 'Georgia, serif', 'ui-monospace, monospace', ...FAMILY_KEYS.map(k => `Arial, '${manifest.families[k].css}', sans-serif`), 'url(x), serif', ...new Set(Object.values(manifest.themes).flat()) ] const WORDS = ['alpha', 'Bravo', 'charlie', 'Delta', 'office', 'affidavit', 'AVATAR', 'quietly', 'Wolverhampton', 'fluffy', 'fine', 'naïve'] const words = () => Array.from({ length: 1 + Math.floor(rand() * 4) }, () => pick(WORDS)).join(' ') // markdownText is a paragraph or two, each mark on a span with some // chance; force names marks that must appear. function markdownText (force = []) { const parts = [] for (let p = 0; p < 1 + Math.floor(rand() * 2); p++) { const spans = [] for (let s = 0; s < 1 + Math.floor(rand() * 4); s++) { let t = words() const m = force.length ? force.shift() : chance(0.4) ? pick(MARKS) : null if (m) t = MARKDOWN[m](t) spans.push(t) } parts.push(spans.join(' ')) } return parts.join('\n\n') } // cellRuns is a cell's paragraphs in the view's run shape, for writeText. function cellRuns (force) { const run = { t: words() } const m = force || (chance(0.5) ? pick(MARKS) : null) if (m === 'href') run.href = 'https://example.com/cell' else if (m) run[m] = true return [[{ t: pick(WORDS) + ' ' }, run]] } function propsFor (type) { switch (type) { case 'heading': return { level: 1 + Math.floor(rand() * 3) } case 'text': return { size: pick(SIZES), align: pick(['start', 'center', 'end']) } case 'list': return { style: pick(['bullet', 'number']), size: pick(SIZES), indent: Math.floor(rand() * 2) } case 'quote': return { cite: chance(0.5) ? words() : '' } case 'code': return { language: pick(['go', 'js', '']), size: pick(SIZES) } case 'table': return { rows: 1 + Math.floor(rand() * 2), cols: 1 + Math.floor(rand() * 3), header: chance(0.5) } case 'shape': return { kind: pick(['rect', 'ellipse', 'line', 'arrow']), fill: '#ddd' } case 'spacer': return { size: pick(SIZES) } case 'columns': return { count: 2 } case 'image': return { alt: 'nothing here', fit: pick(['cover', 'contain']) } default: return {} } } // The shapes a text can have that the renderer and the editor could // draw differently (Astra's finding 3 on #27: trailing empty paragraphs // in a heading): no paragraphs at all, one empty one, empties before, // between and after words. Every text-bearing type the editor binds // (stage.mjs's BOUND_TYPES) and every table cell gets every shape // across the set, on the slide the editor opens on. const SHAPES = ['none', 'empty', 'trailing', 'leading', 'between', 'words'] function runs () { const out = [] for (let k = 0; k < 1 + Math.floor(rand() * 3); k++) { const r = { t: (k ? ' ' : '') + words() } if (chance(0.5)) { const m = pick(MARKS) if (m === 'href') r.href = 'https://example.com/' + k else r[m] = true } out.push(r) } return out } function shaped (shape) { switch (shape) { case 'none': return [] case 'empty': return [[]] case 'trailing': return [runs(), [], []] case 'leading': return [[], runs()] case 'between': return [runs(), [], runs()] default: return [runs(), runs()] } } // A deck: deck i covers its share of the vocabularies in turn, so a // handful of decks covers them all, and the rest of each is random. Its // first slide — the one the editor opens on — carries one block of every // type the editor binds and a table, each text in a shape, so every // shape meets every type there across the set. function buildDeck (doc, i) { const r = roots(doc) const ops = [] for (const [, m] of orderedMaps(r.slides)) ops.push({ op: 'remove_slide', id: m.get('id') }) const fonts = chance(0.6) ? null : { heading: STACKS[(i * 3) % STACKS.length], body: STACKS[(i * 5 + 1) % STACKS.length] } ops.push({ op: 'set_meta', title: `Random ${i}`, theme: THEMES[i % THEMES.length], fonts, arrive: ARRIVALS[i % ARRIVALS.length] }) const cells = [] const shapedTexts = [] const slides = 1 + Math.floor(rand() * 3) for (let s = 0; s < slides; s++) { const sid = id() const layout = (i + s) % 4 === 3 ? 'canvas' : pick(LAYOUTS) ops.push({ op: 'add_slide', id: sid, layout, template: TEMPLATES[(i * 2 + s) % TEMPLATES.length] }) if (s > 0 && (i + s) % 2 === 0) ops.push({ op: 'set_slide', id: sid, talk: 'full' }) if (s === 0) { ;[...BOUND_TYPES, 'table'].forEach((type, k) => { const bid = id() const op = { op: 'add_block', slide: sid, id: bid, type, props: propsFor(type) } if (type === 'table') op.props = { rows: 2, cols: 2, header: chance(0.5) } if (layout === 'canvas') op.box = { x: 0.02 + 0.15 * k, y: 0.05, w: 0.14, h: 0.6 } else op.region = pick(REGIONS) ops.push(op) if (type === 'list') ops.push({ op: 'set_block', slide: sid, id: bid, items: ITEMS[i % ITEMS.length] }) if (type === 'table') cells.push({ sid, bid, n: 4, shape: i + k }) else shapedTexts.push({ sid, bid, shape: SHAPES[(i + k) % SHAPES.length] }) }) } if (chance(0.5)) ops.push({ op: 'set_notes', id: sid, markdown: markdownText() }) const blocks = 1 + Math.floor(rand() * 5) for (let b = 0; b < blocks; b++) { const type = b === 0 ? BLOCK_TYPES[(i + s) % BLOCK_TYPES.length] : pick(BLOCK_TYPES) const bid = id() const op = { op: 'add_block', slide: sid, id: bid, type, props: propsFor(type) } 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 } else op.region = pick(REGIONS) if (TEXT_TYPES.includes(type) && type !== 'shape') { op.markdown = type === 'code' ? words() + '\n' + words() : markdownText(b === 0 ? [MARKS[(i + s) % MARKS.length]] : []) } else if (type === 'shape') { op.markdown = words() } ops.push(op) if (b > 0) ops.push({ op: 'set_block', slide: sid, id: bid, step: b / 2 }) if (type === 'table') cells.push({ sid, bid, n: op.props.rows * op.props.cols }) } } applyOps(doc, ops, 'generate') // The cells, which no op writes: the schema's own writer. doc.transact(() => { for (const { sid, bid, n, shape } of cells) { const slide = orderedMaps(r.slides).map(([, m]) => m).find(m => m.get('id') === sid) const block = orderedMaps(getArray(slide, 'blocks')).map(([, m]) => m).find(m => m.get('id') === bid) const frags = getArray(block, 'cells').toArray() for (let k = 0; k < Math.min(n, frags.length); k++) { writeText(frags[k], shape === undefined ? cellRuns(k === 0 ? MARKS[(i + k) % MARKS.length] : null) : shaped(SHAPES[(shape + k) % SHAPES.length])) } } for (const { sid, bid, shape } of shapedTexts) { const slide = orderedMaps(r.slides).map(([, m]) => m).find(m => m.get('id') === sid) const block = orderedMaps(getArray(slide, 'blocks')).map(([, m]) => m).find(m => m.get('id') === bid) writeText(block.get('text'), shaped(shape)) } }, 'generate') } // Coverage, counted on what is written. 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() } // shapeOf names a text's shape as SHAPES does, read back off the view. function shapeOf (text) { if (!Array.isArray(text) || text.length === 0) return 'none' const empty = text.map(p => !p.some(r => r.t !== '')) if (empty.every(Boolean)) return 'empty' if (empty[empty.length - 1]) return 'trailing' if (empty[0]) return 'leading' if (empty.some(Boolean)) return 'between' return 'words' } function record (doc) { const v = view(doc) seen.themes.add(v.meta.theme) if (v.meta.arrive) seen.arrivals.add(v.meta.arrive) const markOf = run => MARKS.filter(m => (m === 'href' ? run.href != null : run[m])) const first = v.slides[0] for (const b of (first && first.blocks) || []) { if (BOUND_TYPES.includes(b.type)) seen.shapes.add(b.type + ':' + shapeOf(b.text)) if (b.type === 'table') for (const cell of (b.props && b.props.cells) || []) seen.shapes.add('cell:' + shapeOf(cell)) } for (const s of v.slides) { seen.layouts.add(s.layout) seen.templates.add(s.template) for (const b of s.blocks) { seen.types.add(b.type) if (b.items) seen.items.add(b.items) for (const p of b.text || []) for (const run of p) for (const m of markOf(run)) seen.marks.add(m) for (const cell of (b.props && b.props.cells) || []) { if (!Array.isArray(cell)) continue for (const p of cell) for (const run of p) for (const m of markOf(run)) seen.cellMarks.add(m) } } } } const bases = readdirSync(dir).filter(f => f.endsWith('.base')).sort((a, b) => parseInt(a) - parseInt(b)) if (!bases.length) { console.error(`generate-random-decks: no .base files in ${dir}`) process.exit(2) } for (const name of bases) { const doc = new Y.Doc() for (const f of parseFrames(new Uint8Array(readFileSync(join(dir, name))))) Y.applyUpdate(doc, f) const before = Y.encodeStateVector(doc) buildDeck(doc, parseInt(name)) checkDoc(doc) record(doc) writeFileSync(join(dir, name.replace(/\.base$/, '.bin')), Y.encodeStateAsUpdate(doc, before)) } const missing = [] const want = { types: BLOCK_TYPES, templates: TEMPLATES, layouts: LAYOUTS, themes: THEMES, arrivals: ARRIVALS, items: ITEMS, marks: MARKS, cellMarks: MARKS, shapes: [...BOUND_TYPES, 'cell'].flatMap(t => SHAPES.map(sh => t + ':' + sh)) } for (const [k, all] of Object.entries(want)) { const gone = all.filter(x => !seen[k].has(x)) if (gone.length) missing.push(`${k} [${gone}]`) } if (missing.length) { console.error(`generate-random-decks: ${bases.length} decks as written never contain ${missing.join(', ')}`) process.exit(2) } console.log(bases.length)