From 124427c9d25f4c8bfe521e190128ca5265cefe35 Mon Sep 17 00:00:00 2001 From: Vivswan Shah <58091053+Vivswan@users.noreply.github.com> Date: Mon, 26 Oct 2026 16:33:31 -0400 Subject: [PATCH] refactor(e2e): parse the fuzz summary table with the GFM table grammar The fuzzer's summaryTableProblems found the step summary's table by hand (the first run of lines opening with a pipe), counted each row's unescaped pipes with a backslash-run state machine, checked the delimiter row against a regex, and compared the pipe counts. The mdast GFM table stack already in the repository (mdast-util-from-markdown with micromark-extension-gfm-table and mdast-util-gfm-table, as generated-regions.ts uses it) reads the table as GitHub renders it: it recognizes the table only when the header and delimiter row agree, treats an escaped pipe as cell text and a pipe after an escaped backslash as a delimiter, and keeps each row's real cell count. What remains compares each row's cell count to the header's and reports a missing table, which a malformed delimiter row now also becomes. Differential check, the old function against the new over engine-rendered summaries: 700 generated through writeSummary, writeMultiSummary, writeSnapshotDirSummary, and writeRenderSummary with cells drawn from the hostile names plus raw pipes, lone and trailing backslashes, code spans, and blank cells; 200 real summaries from generated fuzz scenarios run through the runner with the oracle's predicted section count; 62 real summaries from a sample of the curated corpus. All 962 agree on the verdict and on the problem kinds. Of 75 hand-made malformed inputs and 10501 mutations (a delimiter row dropped or emptied, an escape broken, a row dropped, a cell appended, the table removed or indented, a leading pipe stripped, a raw pipe inserted, a blank line inserted, the text truncated), the verdicts agree on 8580 mutations, and every split is a shape the engine cannot write: an indented table, a row without its leading or trailing pipe, text glued to the table, or a multi-target summary with its first table destroyed. The fuzz passes this function single-run summaries alone. The diagnostics change wording: a malformed delimiter row reads "summary carries no markdown table" rather than naming the separator row, the row-count message counts data rows, and the cell-count message numbers the data row. No engine-produced summary changes verdict. --- test/e2e/fuzz.ts | 55 +++++++++++++++++------------------------------- 1 file changed, 19 insertions(+), 36 deletions(-) diff --git a/test/e2e/fuzz.ts b/test/e2e/fuzz.ts index 29cfe4243..538cc3cb4 100644 --- a/test/e2e/fuzz.ts +++ b/test/e2e/fuzz.ts @@ -12,6 +12,9 @@ */ import { parseArgs } from "node:util"; +import { fromMarkdown } from "mdast-util-from-markdown"; +import { gfmTableFromMarkdown } from "mdast-util-gfm-table"; +import { gfmTable } from "micromark-extension-gfm-table"; import { canonicalDocument } from "../../src/engine/canonical.js"; import { describeRemoval } from "../../src/engine/layers.js"; import { MAX_RETRIES } from "../../src/github/api.js"; @@ -292,51 +295,31 @@ const REQUIRED_CLASSES: Record = { }; /** - * A pipe is a delimiter only after an EVEN run of backslashes: the engine escapes cells with - * backslash-then-pipe (markdownCell, src/report/markdown.ts); under HOSTILE_NAMES a broken escape changes a row's separator count. + * The engine escapes a cell's pipes and backslashes (markdownCell, src/report/markdown.ts), so under HOSTILE_NAMES a + * broken escape surfaces as a row whose cell count differs from the header's; the GFM grammar reads the table as + * GitHub renders it, and a malformed delimiter row leaves no table at all. */ function summaryTableProblems(summary: string, expectedRows: number): string[] { - const lines = summary.split("\n").map((line) => line.trim()); - const start = lines.findIndex((line) => line.startsWith("|")); - if (start === -1) { - // A completed run renders the header and separator even with zero data rows, so absence is an error at any expectedRows. + const tree = fromMarkdown(summary, { + extensions: [gfmTable()], + mdastExtensions: [gfmTableFromMarkdown()], + }); + const table = tree.children.find((node) => node.type === "table"); + if (table === undefined) { + // A completed run renders the header and delimiter row even with zero data rows, so absence is an error at any expectedRows. return ["summary carries no markdown table"]; } - const table: string[] = []; - for (let i = start; i < lines.length && lines[i]?.startsWith("|"); i++) { - table.push(lines[i] as string); - } - const separators = (row: string): number => { - let count = 0; - let backslashes = 0; - for (const ch of row) { - if (ch === "\\") { - backslashes++; - continue; - } - if (ch === "|" && backslashes % 2 === 0) { - count++; - } - backslashes = 0; - } - return count; - }; + const [width = 0, ...rowWidths] = table.children.map((row) => row.children.length); const problems: string[] = []; - if (table.length < 2 + expectedRows) { + if (rowWidths.length < expectedRows) { problems.push( - `summary table has ${table.length} row(s), expected a header + separator + at least ${expectedRows} data row(s)`, + `summary table has ${rowWidths.length} data row(s), expected at least ${expectedRows}`, ); } - // The loose "any of -:| whitespace" form would accept an empty separator cell. - const separatorCells = (table[1] ?? "").split("|").slice(1, -1); - if (separatorCells.length === 0 || !separatorCells.every((cell) => /^\s*:?-+:?\s*$/.test(cell))) { - problems.push(`summary table separator row is malformed: "${table[1] ?? "(absent)"}"`); - } - const headerCount = separators(table[0] ?? ""); - for (const [index, row] of table.entries()) { - if (separators(row) !== headerCount) { + for (const [index, cells] of rowWidths.entries()) { + if (cells !== width) { problems.push( - `summary table row ${index} has ${separators(row)} cell separator(s), the header has ${headerCount} - a broken pipe escape?`, + `summary table data row ${index + 1} has ${cells} cell(s), the header has ${width} - a broken pipe escape?`, ); break; }