|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// The protocol upgrade guide renders every entry's `surface` inside a code |
| 4 | +// span, and a surface may carry backticks of its own: most semantic entries |
| 5 | +// mark their inline code in it. CommonMark closes a span at the next backtick |
| 6 | +// string of the opening length, so the span must be fenced longer than any run |
| 7 | +// inside it — or the line renders as alternating code and prose fragments. A |
| 8 | +// conversion's surface sits in a GFM table cell as well, where an unescaped |
| 9 | +// pipe ends the cell, inside a code span or not. Three pins, kept apart: |
| 10 | +// |
| 11 | +// - the helper: `codeSpan` fences by that rule and leaves a text with no |
| 12 | +// backtick and no leading or trailing space byte-identical; `readCodeSpan` |
| 13 | +// reads a span the way a renderer does, and reads the OLD single-backtick |
| 14 | +// wrapping of a marked surface as broken (the lit control — a reader that |
| 15 | +// cannot see a break would prove nothing); |
| 16 | +// - the real entry set: every conversion and semantic surface the registries |
| 17 | +// carry round-trips through the pair, so a new entry cannot bring the defect |
| 18 | +// back whatever backticks it carries; |
| 19 | +// - the generated guide: the REAL generator's bytes, read back span by span and |
| 20 | +// cell by cell, give every entry's surface verbatim — pinned on what ships, |
| 21 | +// not on the helper. |
| 22 | + |
| 23 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 24 | +import { spawnSync } from 'node:child_process'; |
| 25 | +import fs from 'node:fs'; |
| 26 | +import os from 'node:os'; |
| 27 | +import path from 'node:path'; |
| 28 | +import { fileURLToPath } from 'node:url'; |
| 29 | + |
| 30 | +import { ALL_CONVERSIONS, CONVERSIONS_BY_MAJOR } from '../src/conversions/registry'; |
| 31 | +import { MIGRATION_SUPPORT_FLOOR, MIGRATIONS_BY_MAJOR } from '../src/migrations/registry'; |
| 32 | +import type { SemanticMigration } from '../src/migrations/types'; |
| 33 | +import { codeSpan, readCodeSpan } from './lib/markdown-code-span'; |
| 34 | + |
| 35 | +const HERE = path.dirname(fileURLToPath(import.meta.url)); |
| 36 | +const PKG = path.resolve(HERE, '..'); |
| 37 | +const TSX = path.join(PKG, 'node_modules', '.bin', 'tsx'); |
| 38 | +const SPAWN_TIMEOUT_MS = 120_000; |
| 39 | + |
| 40 | +/** Every semantic entry with the major its hop arrives at. */ |
| 41 | +function semanticEntries(): { major: number; entry: SemanticMigration }[] { |
| 42 | + const out: { major: number; entry: SemanticMigration }[] = []; |
| 43 | + for (const [major, step] of Object.entries(MIGRATIONS_BY_MAJOR)) { |
| 44 | + for (const entry of step.semantic) out.push({ major: Number(major), entry }); |
| 45 | + } |
| 46 | + return out; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * A GFM table row's cells, read the way the spec reads them: every `|` ends a |
| 51 | + * cell, inside a code span or not, except one escaped as `\|` (GFM §4.10, |
| 52 | + * example 200). The pieces outside the row's leading and trailing pipe are |
| 53 | + * dropped; each cell is trimmed. |
| 54 | + */ |
| 55 | +function gfmCells(row: string): string[] { |
| 56 | + const cells: string[] = []; |
| 57 | + let cell = ''; |
| 58 | + for (let i = 0; i < row.length; i++) { |
| 59 | + if (row[i] === '\\' && row[i + 1] === '|') { |
| 60 | + cell += '\\|'; |
| 61 | + i++; |
| 62 | + } else if (row[i] === '|') { |
| 63 | + cells.push(cell); |
| 64 | + cell = ''; |
| 65 | + } else { |
| 66 | + cell += row[i]; |
| 67 | + } |
| 68 | + } |
| 69 | + cells.push(cell); |
| 70 | + return cells.slice(1, -1).map((c) => c.trim()); |
| 71 | +} |
| 72 | + |
| 73 | +describe('codeSpan — one code span for any one-line text', () => { |
| 74 | + it('a text with no backtick and no leading or trailing space is wrapped in single backticks, byte for byte as before', () => { |
| 75 | + expect(codeSpan('flow.node.type')).toBe('`flow.node.type`'); |
| 76 | + expect(codeSpan('data.query.aggregations[].distinct')).toBe('`data.query.aggregations[].distinct`'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('a text with its own backticks is fenced one backtick longer than its longest run', () => { |
| 80 | + expect(codeSpan('x `a` y')).toBe('``x `a` y``'); |
| 81 | + expect(codeSpan('a ``b`` c')).toBe('```a ``b`` c```'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('a text that starts or ends with a backtick or a space is padded, which the reader strips again', () => { |
| 85 | + expect(codeSpan('`a` on b')).toBe('`` `a` on b ``'); |
| 86 | + expect(codeSpan('on `b`')).toBe('`` on `b` ``'); |
| 87 | + expect(codeSpan(' lead')).toBe('` lead `'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('a blank or multi-line text is refused, naming the subject, never rendered as a span that says nothing or breaks its row', () => { |
| 91 | + expect(() => codeSpan('')).toThrow(/^codeSpan: .*blank/); |
| 92 | + expect(() => codeSpan(' ')).toThrow(/blank/); |
| 93 | + expect(() => codeSpan('a\nb')).toThrow(/one line/); |
| 94 | + expect(() => codeSpan('', 'entry some-id')).toThrow(/^entry some-id: /); |
| 95 | + }); |
| 96 | + |
| 97 | + it('round-trips through the reader', () => { |
| 98 | + for (const text of ['flow.node.type', 'x `a` y', '`a` on b', 'on `b`', 'a ``b`` c', ' lead', 'trail ', '`', '``']) { |
| 99 | + const span = codeSpan(text); |
| 100 | + expect(readCodeSpan(span, 0), span).toEqual({ content: text, end: span.length }); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
| 104 | + |
| 105 | +describe('readCodeSpan — CommonMark’s reading of a span', () => { |
| 106 | + it('closes at the next backtick string of the opening length, and strips one space from each end', () => { |
| 107 | + expect(readCodeSpan('`a` b', 0)).toEqual({ content: 'a', end: 3 }); |
| 108 | + expect(readCodeSpan('`` `a` b `` c', 0)).toEqual({ content: '`a` b', end: 11 }); |
| 109 | + expect(readCodeSpan('x `a`', 2)).toEqual({ content: 'a', end: 5 }); |
| 110 | + }); |
| 111 | + |
| 112 | + it('a line ending inside the span reads as a space; a span of spaces only keeps them', () => { |
| 113 | + expect(readCodeSpan('`a\nb`', 0)).toEqual({ content: 'a b', end: 5 }); |
| 114 | + expect(readCodeSpan('`a\r\nb`', 0)).toEqual({ content: 'a b', end: 6 }); |
| 115 | + expect(readCodeSpan('` `', 0)).toEqual({ content: ' ', end: 4 }); |
| 116 | + expect(readCodeSpan('` \t `', 0)).toEqual({ content: '\t', end: 5 }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('an opener with no closer of its length is not a span', () => { |
| 120 | + expect(readCodeSpan('``a` b', 0)).toBeNull(); |
| 121 | + expect(readCodeSpan('`a', 0)).toBeNull(); |
| 122 | + }); |
| 123 | + |
| 124 | + it('a position that is not the start of a backtick string is not a span', () => { |
| 125 | + expect(readCodeSpan('a `b`', 0)).toBeNull(); |
| 126 | + expect(readCodeSpan('``a``', 1)).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('the lit control: the old single-backtick wrapping of a real marked surface reads as broken', () => { |
| 130 | + // A surface with a single-backtick string inside: the old wrapper closed at |
| 131 | + // it, or opened a double-backtick span that never closes. (A run of two or |
| 132 | + // more inside a single-backtick span is literal and would not break it.) |
| 133 | + const marked = semanticEntries().filter(({ entry }) => /(?:^|[^`])`(?:[^`]|$)/.test(entry.surface)); |
| 134 | + expect(marked.length).toBeGreaterThan(0); |
| 135 | + for (const { entry } of marked) { |
| 136 | + const old = `\`${entry.surface}\``; |
| 137 | + expect(readCodeSpan(old, 0)?.content, entry.id).not.toBe(entry.surface); |
| 138 | + } |
| 139 | + }); |
| 140 | +}); |
| 141 | + |
| 142 | +describe('the real entry set — every surface the registries carry renders as one unbroken span', () => { |
| 143 | + it('every semantic surface round-trips, whatever backticks it carries', () => { |
| 144 | + const entries = semanticEntries(); |
| 145 | + expect(entries.length).toBeGreaterThan(0); |
| 146 | + for (const { entry } of entries) { |
| 147 | + const span = codeSpan(entry.surface); |
| 148 | + expect(readCodeSpan(span, 0)?.content, entry.id).toBe(entry.surface); |
| 149 | + } |
| 150 | + }); |
| 151 | + |
| 152 | + it('every conversion surface round-trips, whatever backticks it carries, and never escapes a pipe itself', () => { |
| 153 | + expect(ALL_CONVERSIONS.length).toBeGreaterThan(0); |
| 154 | + for (const c of ALL_CONVERSIONS) { |
| 155 | + const span = codeSpan(c.surface); |
| 156 | + expect(readCodeSpan(span, 0)?.content, c.id).toBe(c.surface); |
| 157 | + // A backslash right before a pipe is where GFM renderers part ways once |
| 158 | + // the row escapes it again (`\\|` is a literal backslash and a cell |
| 159 | + // boundary to one, a backslash and an escaped pipe to another), so a |
| 160 | + // conversion surface never carries that pair — loudly. |
| 161 | + expect(c.surface, c.id).not.toMatch(/\\\|/); |
| 162 | + } |
| 163 | + }); |
| 164 | +}); |
| 165 | + |
| 166 | +describe('the generated guide — read back span by span, every entry’s surface is verbatim', () => { |
| 167 | + let tmp: string; |
| 168 | + let sections: Map<number, string[]>; |
| 169 | + |
| 170 | + beforeAll(() => { |
| 171 | + tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'upgrade-guide-surface-')); |
| 172 | + const file = path.join(tmp, 'guide.md'); |
| 173 | + const r = spawnSync(TSX, [path.join(HERE, 'build-upgrade-guide.ts'), '--out', file], { |
| 174 | + cwd: PKG, |
| 175 | + encoding: 'utf8', |
| 176 | + timeout: SPAWN_TIMEOUT_MS, |
| 177 | + stdio: ['ignore', 'pipe', 'pipe'], |
| 178 | + }); |
| 179 | + expect(r.status, `${r.stdout ?? ''}${r.stderr ?? ''}`).toBe(0); |
| 180 | + const text = fs.readFileSync(file, 'utf8'); |
| 181 | + sections = new Map(); |
| 182 | + for (const part of text.split(/^(?=## Protocol \d+ → \d+$)/m).slice(1)) { |
| 183 | + sections.set(Number(/→ (\d+)/.exec(part)![1]), part.split('\n')); |
| 184 | + } |
| 185 | + }); |
| 186 | + |
| 187 | + afterAll(() => { |
| 188 | + fs.rmSync(tmp, { recursive: true, force: true }); |
| 189 | + }); |
| 190 | + |
| 191 | + /** |
| 192 | + * Whether the guide renders a hop for `major`: it starts one above the chain's |
| 193 | + * support floor, so a registry major at or below the floor has no section, |
| 194 | + * and one above it must. |
| 195 | + */ |
| 196 | + function rendered(major: number): boolean { |
| 197 | + const has = sections.has(major); |
| 198 | + if (!has) expect(major, `protocol ${major} has no section`).toBeLessThanOrEqual(MIGRATION_SUPPORT_FLOOR); |
| 199 | + return has; |
| 200 | + } |
| 201 | + |
| 202 | + /** The one line of a rendered `major`'s section that starts with `prefix`. */ |
| 203 | + function lineOf(major: number, prefix: string): string { |
| 204 | + const hits = sections.get(major)!.filter((line) => line.startsWith(prefix)); |
| 205 | + expect(hits, prefix).toHaveLength(1); |
| 206 | + return hits[0]!; |
| 207 | + } |
| 208 | + |
| 209 | + it('each semantic entry’s line carries its surface as one span, followed by the replacement arrow', () => { |
| 210 | + const entries = semanticEntries(); |
| 211 | + expect(entries.length).toBeGreaterThan(0); |
| 212 | + for (const { major, entry } of entries) { |
| 213 | + if (!rendered(major)) continue; |
| 214 | + const prefix = `- **\`${entry.id}\`** — `; |
| 215 | + const line = lineOf(major, prefix); |
| 216 | + const span = readCodeSpan(line, prefix.length); |
| 217 | + expect(span?.content, entry.id).toBe(entry.surface); |
| 218 | + expect(line.slice(span!.end).startsWith(' → '), entry.id).toBe(true); |
| 219 | + } |
| 220 | + }); |
| 221 | + |
| 222 | + it('each conversion’s table row is four cells, the surface one span filling its own cell', () => { |
| 223 | + for (const [key, list] of Object.entries(CONVERSIONS_BY_MAJOR)) { |
| 224 | + const major = Number(key); |
| 225 | + if (!rendered(major)) continue; |
| 226 | + for (const c of list) { |
| 227 | + const line = lineOf(major, `| \`${c.id}\` | `); |
| 228 | + const cells = gfmCells(line); |
| 229 | + expect(cells, c.id).toHaveLength(4); |
| 230 | + const span = readCodeSpan(cells[1]!, 0); |
| 231 | + expect(span?.end, c.id).toBe(cells[1]!.length); |
| 232 | + expect(span?.content.replace(/\\\|/g, '|'), c.id).toBe(c.surface); |
| 233 | + } |
| 234 | + } |
| 235 | + }); |
| 236 | + |
| 237 | + it('the lit control: the old single-backtick row of a surface that carries a pipe splits into extra cells', () => { |
| 238 | + // A literal row first, so the control outlives the two pipe-carrying |
| 239 | + // conversions in today's registry (their retirement kits age out one |
| 240 | + // major on, ADR-0087); then every real one the registry carries. |
| 241 | + expect(gfmCells("| `a` | `x ('m' | 's')` | s | live |")).toHaveLength(5); |
| 242 | + for (const c of ALL_CONVERSIONS.filter((c) => c.surface.includes('|'))) { |
| 243 | + const old = `| \`${c.id}\` | \`${c.surface}\` | ${c.summary} | live |`; |
| 244 | + expect(gfmCells(old).length, c.id).toBeGreaterThan(4); |
| 245 | + } |
| 246 | + }); |
| 247 | +}); |
0 commit comments