diff --git a/CLAUDE.md b/CLAUDE.md index 0b72406..6db238e 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -137,6 +137,13 @@ original plan and any signed-in notes stay out of the repository (see `.gitignor `test/document.test.ts` checks they match. Every text the reader keeps goes through it, table cells and captions included, and so does every text a renderer prints. Never add a visible character to it, or a mark that turns text from right to left. +- **The PDF is read a second way only for table cells cut across pages** (`readSplitRows`), and + only after the plain reading failed. A cell left unfinished goes on at the top of the next page, + and of every page after until it is finished: a page that holds none of it is refused, so no + later row is read before it. Every line must be the exact expected text. Never loosen it to + compare words without their order, and never let it take up a cell again on the page it was left + on. A line two cells both hold may be read as either's; the ways tried are counted + (`SPLIT_ROW_TURNS`), and running out refuses the document. - **A failed check says where, never what.** Verification details hold block IDs, page numbers, counts and character codes (`charClass`: a letter or digit only as its kind). Never put a piece of code text into a report, an error message or a log line. diff --git a/README.md b/README.md index 506e212..0eb6e57 100644 --- a/README.md +++ b/README.md @@ -545,6 +545,14 @@ fit between the margins. One taller than a page is also made smaller, in proport it is on one page (at most 8.96 inches of the 9.2 between the margins). Printed at full height it was cut across two pages and the picture count failed. +**A table row taller than a page.** A row with so much text that it does not fit on one page is +cut across pages with its cells side by side: the first part of each cell on one page, the rest of +each on the next. All of the text is there, but page by page it does not come in the order of the +text, where one cell is whole before the next begins, and the PDF check used to refuse such a +document. It now reads such a row cell by cell across the page break. Nothing else passes that +way: a changed word, text left out, cells in the wrong order, or the rest of a cell printed +anywhere but at the top of the next page are refused as before. + **A numbered item inside a numbered item.** Its marker, such as "2.1.", is wider than the column the documents gave a marker, so the PDF broke it in two with the final period on a line of its own. When such an item began on the last line of a page, the period went to the next page and the PDF diff --git a/docs/adr/0003-documents.md b/docs/adr/0003-documents.md index 315811a..de58520 100644 --- a/docs/adr/0003-documents.md +++ b/docs/adr/0003-documents.md @@ -112,10 +112,43 @@ The marker's column now takes the width of its marker, never less than before always follows it. An item whose marker fitted prints exactly where it did. Word was never affected: its items are a hanging indent with a tab. -Still open, found while testing this: a single table row taller than a whole page. Its cells -are split across pages side by side, so the PDF reads part of the first cell, then part of the -second, where the text has the whole first cell first. The check refuses it. No such row has been -met on a live page. +## A table row taller than a page + +Added 1 December 2026. Found while testing the list markers. A row that is taller than a whole page +has to be cut, and it is cut with its cells side by side: one page prints the first part of the +first cell and then the first part of the second, and the next page prints the rest of each. Every +character of every cell is printed, in order. But the text has the first cell whole before the +second begins, so the PDF, read page by page, did not read back as the text, and the check refused +a document that was right. A cell that spans several rows and runs over a page reads the same way. + +The row is printed as before. Shrinking it to fit a page would make a long cell unreadable, and +printing its cells one under the other would not be the table the page shows. What changed is the +reading (`readSplitRows` in `src/document/verify.ts`), and only as a second reading, after the +plain one has failed, so every document that passed before is judged exactly as it was: + +- A table cell may be left unfinished where the next line begins the same table's next cell. +- What is left of it must go on at the top of the next page, before anything else of that page, + and so on every page after until it is finished. A page that holds none of it while it is + unfinished is refused, so no later row is ever read before the rest of an earlier one. (Cells are + set from the top, so the rest of a cut cell is always the first thing of its next page.) It is + never taken up again on the page it was left on, so a table on one page is read only in its own + order. +- Every printed line must be the very text expected at one of those places. Nothing may be left + over at the end, of the pages or of the text. +- The last lines of two cells may stand on one baseline and come back as one line; that is the end + of the one, a space, and the rest of the other. +- Two cells of a row can hold the same line, and such a line can be read as either's. The reader + follows each way, the plainest first, until one reads every page to the end. Every way obeys the + rules above, so this accepts nothing more; it only keeps a correct document from being refused + for the way a line was first read. The ways tried are counted (`SPLIT_ROW_TURNS`), and a document + that runs out of them is refused. + +The report then carries `cellsAcrossPages`, a count. The check refuses the same PDF for a text +with a word changed, with text added to a cell, or with the cells the other way round, and +`test/fidelity.electron.cjs` holds it to that. + +Rejected: comparing the words of a row without regard to order. That would pass a row whose +cells were printed in the wrong columns. ## Pagination diff --git a/docs/architecture.md b/docs/architecture.md index 4547bfb..94cc6ae 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -121,7 +121,11 @@ The hard rules are tested by name. Search the tests for `rule_R` to see each one than a page is printed smaller, in proportion, so that all of it is on one page (`pictureScale`); Word follows. The verifier then reads the PDF text and the Word XML back and compares them with the tree, setting aside only header rows repeated at the top of a - continuation page, and counts the pictures in both. + continuation page, and counts the pictures in both. A table row taller than a page is cut with + its cells side by side, so a page holds parts of several cells: when the plain reading fails, + the PDF is read once more with a cell allowed to be left unfinished and gone on with at the + top of each following page until it is finished, and nothing else allowed (`readSplitRows`; + the report says `cellsAcrossPages`). - **What a failed check says.** The report names where the documents first differ: the page of the PDF, the block (`blockAt`), and the two characters, a letter or a digit only as its kind and anything else as its code point (`charClass`). `explainFailure` turns that into the diff --git a/src/document/verify.ts b/src/document/verify.ts index 469e820..e2ea3a4 100644 --- a/src/document/verify.ts +++ b/src/document/verify.ts @@ -428,6 +428,169 @@ function repeatedHead(lines: string[], heads: string[], expectedRest: string): n return 0; } +/** + * What a document prints as one text, with where each table cell's own text lies in it. Nothing + * when the pieces do not add up to `expected` exactly, which no ordinary text does. + */ +export function cellRegions(tree: SectionTree, includeAdoptionTable: boolean, expected: string) { + const regions: { start: number; end: number; table?: string }[] = []; + let text = ''; + const add = (piece: string, table?: string) => { + const normal = normalizeForCompare(piece); + if (!normal) return; + if (text) text += ' '; + regions.push({ start: text.length, end: text.length + normal.length, table }); + text += normal; + }; + add(tree.meta.headerLine); + for (const b of exportBlocks(tree, includeAdoptionTable)) { + if (b.type === 'gap') add('· · ·'); + else if (b.type === 'table') { + if (b.caption) add(b.caption); + for (const row of [...b.header, ...b.rows]) for (const c of row) add(c.text, b.id); + } else for (const line of flattenBlock(b)) add(line); + } + return text === expected ? regions : undefined; +} + +/** How many times the second reading may go back to a line it could have read another way. */ +const SPLIT_ROW_TURNS = 20_000; + +/** + * Read the pages again, allowing for one thing the plain reading cannot follow: a table row + * taller than a page. Such a row is cut across pages with its cells side by side, so a page + * prints the first part of one cell and then the first part of the next, and the following page + * prints the rest of each. Every character of every cell is there, in order, but not in the order + * of the text, where a cell is whole before the next begins. The same holds for a cell that spans + * several rows and runs over a page. + * + * So a cell of a table may be left unfinished where the next line begins the table's next cell. + * What is left of it must then go on at the top of the next page, before anything else of that + * page, and so on every page after until it is finished: a page that holds none of it, while it + * is unfinished, is refused, so no later row is ever read before the rest of an earlier one. + * Nothing else is allowed: every printed line must be the very text that is expected at one of + * those places, a cell is never taken up again on the page it was left on, and at the end + * nothing may be left over on either side. + * + * Two cells can hold the same line, and such a line can then be read as either's. Each way is + * followed, the plainest first, until one reads every page to the end; each obeys every rule + * above, so this accepts nothing more, and only stops a correct document being refused for the + * way it was first read. Resolves to how many cells were read across a page break, or to nothing + * when the pages do not read as the text this way either. + */ +export function readSplitRows( + pages: string[][], + heads: string[], + expected: string, + regions: { start: number; end: number; table?: string }[], +): number | undefined { + /** What is left of a cell: the page it was left on or last set aside on, and the last page it had a line of. */ + interface Left { + start: number; + end: number; + page: number; + took: number; + } + /** Where the reading stands: the line in hand (or none, and the next to fetch), and what was read. */ + interface Standing { + page: number; + index: number; + line: string; + pos: number; + left: Left[]; + split: number; + } + /** Where `line` ends if it is the text expected at `at`, within `limit`. A line break may stand for a space. */ + const fits = (line: string, at: number, limit: number) => { + const from = at > 0 && expected[at] === ' ' ? at + 1 : at; + return from + line.length <= limit && expected.startsWith(line, from) ? from + line.length : -1; + }; + /** The unfinished table cell `pos` is in, and the cell after it when that is of the same table. */ + const cellAt = (pos: number) => { + let low = 0, + high = regions.length - 1; + while (low < high) { + const mid = (low + high + 1) >> 1; + if (regions[mid]!.start <= pos) low = mid; + else high = mid - 1; + } + const cell = regions[low]; + const following = regions[low + 1]; + return cell?.table && cell.start <= pos && pos < cell.end && following?.table === cell.table + ? { cell, following } + : undefined; + }; + /** Every way to read on from `s`, the plainest first; none when it cannot be; a number at the end. */ + const ways = (s: Standing): Standing[] | number => { + let { page, index, line } = s; + while (!line) { + const lines = pages[page]; + if (!lines) return s.pos === expected.length && !s.left.length && s.split ? s.split : []; + if (index < lines.length) line = lines[index++]!; + else { + page++; + const next = pages[page]; + index = next ? repeatedHead(next, heads, expected.slice(s.pos)) : 0; + } + } + const here = { ...s, page, index, line }; + // What an earlier page left of a cell comes first on a later one. + const which = s.left.findIndex((rest) => rest.page < page); + if (which >= 0) { + const rest = s.left[which]!; + const without = s.left.filter((other) => other !== rest); + const instead = (now: Left) => s.left.map((other) => (other === rest ? now : other)); + const out: Standing[] = []; + const end = fits(line, rest.start, rest.end); + if (end >= 0) { + const more = end < rest.end; + out.push({ + ...here, + line: '', + left: more ? instead({ ...rest, start: end, took: page }) : without, + }); + } else { + // The last line of two cells can stand on one baseline, and is then read as one line: + // the end of this cell, a space, and what belongs to the next. + const from = expected[rest.start] === ' ' ? rest.start + 1 : rest.start; + const tail = expected.slice(from, rest.end); + if (tail && line.startsWith(tail + ' ')) + out.push({ ...here, line: line.slice(tail.length + 1), left: without }); + } + // Its part of this page is over, and it is not taken up again here. A cell that has had no + // line of this page is never passed over: nothing is read before the rest of it. + if (rest.took === page) out.push({ ...here, left: instead({ ...rest, page }) }); + return out; + } + const out: Standing[] = []; + const end = fits(line, s.pos, expected.length); + if (end >= 0) out.push({ ...here, line: '', pos: end }); + // Inside a table cell, the line may begin the table's next cell instead. + const at = cellAt(s.pos); + const begun = at ? fits(line, at.following.start, expected.length) : -1; + if (at && begun >= 0) + out.push({ + ...here, + line: '', + pos: begun, + split: s.split + 1, + left: [...s.left, { start: s.pos, end: at.cell.end, page, took: page }], + }); + return out; + }; + const others: Standing[] = []; + let turns = 0; + let standing: Standing | undefined = { page: 0, index: 0, line: '', pos: 0, left: [], split: 0 }; + while (standing) { + const next = ways(standing); + if (typeof next === 'number') return next; + for (let i = next.length - 1; i > 0; i--) others.push(next[i]!); + if (!next.length && ++turns > SPLIT_ROW_TURNS) return undefined; + standing = next[0] ?? others.pop(); + } + return undefined; +} + export async function verifyPdf(bytes: Buffer, tree: SectionTree, includeAdoptionTable = false) { const pdfjs = await import('pdfjs-dist/legacy/build/pdf.mjs'); const task = pdfjs.getDocument({ data: new Uint8Array(bytes), useSystemFonts: false, verbosity: 0 }); @@ -486,7 +649,15 @@ export async function verifyPdf(bytes: Buffer, tree: SectionTree, includeAdoptio actual += printed; } } - const result = checkText(actual, expected); + let result: ReturnType = checkText(actual, expected); + // The plain reading failed. A table row taller than a page is printed in another order than + // the text, every character of it all the same: read the pages once more with that allowed. + let cellsAcrossPages: number | undefined; + if (!result.ok) { + const regions = cellRegions(tree, includeAdoptionTable, expected); + cellsAcrossPages = regions && readSplitRows(pages, heads, expected, regions); + if (cellsAcrossPages) result = { ok: true }; + } const images = pictures(tree, includeAdoptionTable); const imagesOk = painted === images; const footerOk = !footerPage; @@ -502,6 +673,8 @@ export async function verifyPdf(bytes: Buffer, tree: SectionTree, includeAdoptio footerOk, ...(footerPage && { footerPage }), ...((images || !imagesOk) && { images, imagesPrinted: painted }), + // Cells of a table row taller than a page, read across the page break that cuts them. + ...(cellsAcrossPages && { cellsAcrossPages }), }, pageCount: pdf.numPages, }; diff --git a/test/document.test.ts b/test/document.test.ts index f2d1647..7e770ed 100644 --- a/test/document.test.ts +++ b/test/document.test.ts @@ -6,6 +6,8 @@ import { z } from 'zod'; import JSZip from 'jszip'; import { describeVerification } from '../src/document/review'; import { + cellRegions, + readSplitRows, UNPRINTED, VerificationError, explainFailure, @@ -316,6 +318,161 @@ test('a failed check says which document, which block, which page and which char ); assert.ok(!JSON.stringify(check.details).includes('Zzz')); }); +test('a table row cut across pages reads cell by cell, and only that', async () => { + const { tree } = await reference; + const doc: SectionTree = { + ...tree, + blocks: [ + tree.blocks[0]!, + { + id: 't', + depth: 0, + type: 'table', + caption: 'Table 1 Samples', + header: [[{ text: 'Left' }, { text: 'Right' }]], + rows: [ + [{ text: 'a1 a2 a3 a4 a5 a6' }, { text: 'b1 b2 b3 b4 b5 b6' }], + [{ text: 'c1' }, { text: 'd1' }], + ], + }, + { id: 'p', depth: 0, type: 'paragraph', variant: 'body', text: 'After.', spans: [] }, + ], + }; + const first = tree.blocks[0]!; + const heading = first.type === 'heading' ? first.marker + ' ' + first.text : ''; + const before = [tree.meta.headerLine, heading, 'Table 1 Samples', 'Left Right'].map( + normalizeForCompare, + ); + const expected = [...before, 'a1 a2 a3 a4 a5 a6 b1 b2 b3 b4 b5 b6', 'c1 d1', 'After.'].join(' '); + const regions = cellRegions(doc, false, expected)!; + assert.ok(regions, 'the cells add up to the text'); + const heads = ['Left Right']; + const read = (...pages: string[][]) => readSplitRows(pages, heads, expected, regions); + + // The row cut over two pages: part of each cell on each, the header rows repeated. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3'], ['Left Right', 'a4 a5 a6', 'b4 b5 b6', 'c1 d1', 'After.']), + 1, + ); + // Over three pages. + assert.equal( + read( + [...before, 'a1 a2', 'b1 b2'], + ['Left Right', 'a3 a4', 'b3 b4'], + ['Left Right', 'a5 a6', 'b5 b6', 'c1 d1', 'After.'], + ), + 1, + ); + // The last lines of both cells on one baseline, read as one line. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3'], ['Left Right', 'a4 a5 a6 b4 b5 b6', 'c1 d1', 'After.']), + 1, + ); + // One cell ends on the first page; only the other runs on. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3 b4 b5 b6'], ['Left Right', 'a4 a5 a6', 'c1 d1', 'After.']), + 1, + ); + + // Refused: text read in order needs no second reading, so this one has nothing to say. + assert.equal( + read([...before, 'a1 a2 a3 a4 a5 a6', 'b1 b2 b3 b4 b5 b6', 'c1 d1', 'After.']), + undefined, + ); + // A cell left unfinished and taken up again on the same page. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3 b4 b5 b6', 'a4 a5 a6', 'c1 d1', 'After.']), + undefined, + ); + // The rest of a cell never printed. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3'], ['Left Right', 'b4 b5 b6', 'c1 d1', 'After.']), + undefined, + ); + // The rest printed after something else of the later page. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3 b4 b5 b6'], ['Left Right', 'c1 d1', 'a4 a5 a6', 'After.']), + undefined, + ); + // The rest printed a page late, after the next row: no row is read before the rest of an earlier one. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3 b4 b5 b6'], ['Left Right', 'c1 d1'], ['a4 a5 a6', 'After.']), + undefined, + ); + // Nor is a page of the other cell alone read while the first is unfinished. + assert.equal( + read( + [...before, 'a1 a2 a3', 'b1 b2'], + ['Left Right', 'b3 b4'], + ['Left Right', 'a4 a5 a6', 'b5 b6', 'c1 d1', 'After.'], + ), + undefined, + ); + // A word that is not the text's. + assert.equal( + read([...before, 'a1 a2 a3', 'b1 b2 b3'], ['Left Right', 'a4 a5 a7', 'b4 b5 b6', 'c1 d1', 'After.']), + undefined, + ); + // The cells the other way round on the page. + assert.equal( + read([...before, 'b1 b2 b3', 'a1 a2 a3'], ['Left Right', 'b4 b5 b6', 'a4 a5 a6', 'c1 d1', 'After.']), + undefined, + ); + // Text outside a table is never read this way. + assert.equal( + read([ + ...before.slice(0, 1), + before[2]!, + before[1]!, + ...before.slice(3), + 'a1 a2 a3 a4 a5 a6 b1 b2 b3 b4 b5 b6', + 'c1 d1', + 'After.', + ]), + undefined, + ); + // Two cells that hold the same line: a line can be read as either cell's, and the first way + // tried must not decide. Regions are given directly; the pieces are joined by single spaces. + const laid = (...pieces: (string | [string])[]) => { + let text = ''; + const places: { start: number; end: number; table?: string }[] = []; + for (const piece of pieces) { + const [words, table] = typeof piece === 'string' ? [piece, undefined] : [piece[0], 't']; + if (text) text += ' '; + places.push({ start: text.length, end: text.length + words.length, table }); + text += words; + } + return (...pages: string[][]) => readSplitRows(pages, [], text, places); + }; + const same = laid('Head', ['a1 same same same a4'], ['b1 same b3 b4'], 'After.'); + // The third "same" of the second page is the second cell's, though the first cell could take it. + assert.equal( + same(['Head', 'a1', 'b1'], ['same', 'same', 'same', 'b3'], ['same', 'a4', 'b4', 'After.']), + 1, + ); + assert.equal(same(['Head', 'a1', 'b1'], ['same', 'same', 'same', 'b3'], ['same a4 b4', 'After.']), 1); + // A line that could go on with its cell begins the next one instead. + const begins = laid('Head', ['a1 same a3'], ['same b2'], 'After.'); + assert.equal(begins(['Head', 'a1', 'same'], ['same', 'a3', 'b2', 'After.']), 1); + // Reading a line another way never lets a word go missing or change places. + assert.equal( + same(['Head', 'a1', 'b1'], ['same', 'same', 'b3'], ['same', 'a4', 'b4', 'After.']), + undefined, + ); + assert.equal( + same(['Head', 'a1', 'b1'], ['same', 'same', 'same', 'b3'], ['a4', 'same', 'b4', 'After.']), + undefined, + ); + // Many cells of the same lines end in an answer, and soon: the ways tried are counted. + const many = Array.from({ length: 14 }, (): [string] => [Array(12).fill('same').join(' ')]); + const flood = laid('Head', ...many, 'After.'); + const page = () => Array(40).fill('same'); + const started = Date.now(); + assert.equal(flood(['Head', ...page()], page(), page(), [...page(), 'Never.']), undefined); + assert.ok(Date.now() - started < 5000, 'the search is bounded'); + // Text that does not add up from its cells gives no regions to read by. + assert.equal(cellRegions(doc, false, expected + ' more'), undefined); +}); test('DOCX roundtrip uses literal markers, source cells and exact runs, detects residual text', async () => { const { tree } = await reference; const extra: Block[] = [ diff --git a/test/fidelity.electron.cjs b/test/fidelity.electron.cjs index d87d14f..b519c37 100644 --- a/test/fidelity.electron.cjs +++ b/test/fidelity.electron.cjs @@ -13,7 +13,7 @@ const { DOMParser } = require('@xmldom/xmldom'); const { ElectronViewDriver, pacedDriver } = require('../dist/browser/driver.js'); const { Pacing } = require('../dist/browser/pacing.js'); const { getSiteAdapter } = require('../dist/sites/registry.js'); -const { writeVerifiedArtifacts, treeToHtml, printPage } = require('../dist/document'); +const { writeVerifiedArtifacts, treeToHtml, printPage, verifyPdf } = require('../dist/document'); const { BlockedError, PageTimeout, PICTURE_LIMIT } = require('../dist/sites/adapter.js'); const { Store } = require('../dist/db/store'); const { Library } = require('../dist/main/library'); @@ -2141,6 +2141,125 @@ check( }, ); +// ───────── A table row taller than a page ───────── + +check( + 'tables: a row taller than a page, cut across pages with its cells side by side, still verifies, and nothing else does', + async () => { + const words = 'sample placeholder duct fan damper grille filter intake exhaust plenum louver'.split( + ' ', + ); + let next = 0; + const text = (n) => Array.from({ length: n }, () => words[next++ % words.length]).join(' ') + '.'; + const cell = (value, extra = {}) => ({ text: value, ...extra }); + const tableOf = (header, rows) => ({ + id: id('t'), + depth: 0, + type: 'table', + caption: 'Table 401.3 Sample Provisions', + header: [header.map((h) => cell(h))], + rows, + }); + const docOf = (table) => { + const blocks = [ + heading401(), + para('Sample provisions are listed below.'), + table, + para('Sample text after the table.'), + ]; + const rawLines = [ + '401.1 Long Samples', + blocks[1].text, + table.caption, + ...[...table.header, ...table.rows].map((row) => row.map((c) => c.text).join(' ')), + blocks[3].text, + ]; + return { tree: { schemaVersion: 1, meta: longMeta(), blocks }, rawLines }; + }; + const made = async (table) => { + const doc = docOf(table); + const result = await writeVerifiedArtifacts({ + ...doc, + accountLabel: 'Fixture account', + appVersion: 'test', + boundaryConfirmed: true, + outputDir: path.join(work, 'out', 'tall-row-' + Date.now() + '-' + next), + }); + return { ...doc, result, bytes: fs.readFileSync(result.pdfPath) }; + }; + // The same words in both cells: every line of the one is a line of the other. + const twice = text(1100); + const cases = { + 'two long cells of the same words': tableOf( + ['Sample item', 'Sample provision'], + [[cell(twice), cell(twice)]], + ), + 'two long cells': tableOf( + ['Sample item', 'Sample provision'], + [[cell(text(700)), cell(text(700))]], + ), + 'a long cell beside a short one': tableOf( + ['Sample item', 'Sample class'], + [ + [cell(text(900)), cell('Sample class one.')], + [cell('Sample row two.'), cell('Sample class two.')], + ], + ), + 'three long cells over three pages': tableOf( + ['Sample item', 'Sample provision', 'Sample note'], + [ + [cell('Sample first row.'), cell('Short sample.'), cell('None.')], + [cell(text(520)), cell(text(470)), cell(text(505))], + [cell('Sample last row.'), cell('Short sample.'), cell('None.')], + ], + ), + 'a long cell that spans three rows': tableOf( + ['Sample material', 'Sample class', 'Sample method'], + [ + [cell(text(600), { rowspan: 3 }), cell('1'), cell(text(40))], + [cell('2'), cell(text(40))], + [cell('3'), cell(text(40))], + [cell('Sample last material.'), cell('4'), cell('Sample method four.')], + ], + ), + }; + let kept; + for (const [name, table] of Object.entries(cases)) { + const doc = await made(table); + const { V2, V3 } = doc.result.report.checks; + assert.equal(V2.ok && V3.ok, true, name); + assert.ok(doc.result.report.stats.pdfPages >= 3, name + ': the row runs over pages'); + // It was read the second way, and the report says so with a count, no text. + assert.ok(V2.details.cellsAcrossPages >= 1, name + ': ' + JSON.stringify(V2.details)); + if (name === 'two long cells') kept = doc; + } + + // The same PDF is refused for any other text. A word changed deep in the second cell: + const changed = structuredClone(kept.tree); + const second = changed.blocks[2].rows[0][1]; + second.text = second.text.replace(/damper(?=[^.]*\.$)/, 'dampers'); + assert.notEqual(second.text, kept.tree.blocks[2].rows[0][1].text); + assert.equal((await verifyPdf(kept.bytes, changed)).ok, false, 'a changed word is not read past'); + // A word missing from the end of the first cell: + const longer = structuredClone(kept.tree); + longer.blocks[2].rows[0][0].text += ' Sample addition.'; + assert.equal((await verifyPdf(kept.bytes, longer)).ok, false, 'text that is not printed is missed'); + // The two cells the other way round: + const swapped = structuredClone(kept.tree); + swapped.blocks[2].rows[0].reverse(); + assert.equal((await verifyPdf(kept.bytes, swapped)).ok, false, 'cells in another order are refused'); + // And a table on one page reads only in its own order: a cell left unfinished is taken up + // again only on a later page, never on the page it was left on. + const small = await made( + tableOf(['Sample item', 'Sample provision'], [[cell(text(60)), cell(text(60))]]), + ); + assert.equal(small.result.report.checks.V2.details.cellsAcrossPages, undefined); + const turned = structuredClone(small.tree); + turned.blocks[2].rows[0].reverse(); + assert.equal((await verifyPdf(small.bytes, turned)).ok, false, 'a one-page table is read in order'); + }, +); + // ───────── Stops that come late, pages that do not answer, many pictures ───────── check(