|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + furnitureThreshold, |
| 7 | + isPageNumber, |
| 8 | + normalizeFurnitureText, |
| 9 | + type PdfPageLines, |
| 10 | + suppressFurniture, |
| 11 | +} from '@/lib/file-parsers/pdf-furniture' |
| 12 | +import type { PdfLine } from '@/lib/file-parsers/pdf-lines' |
| 13 | + |
| 14 | +const PAGE_HEIGHT = 792 |
| 15 | + |
| 16 | +function line(text: string, y: number): PdfLine { |
| 17 | + return { text, y, height: 11 } |
| 18 | +} |
| 19 | + |
| 20 | +/** A page with a top header, a bottom footer, and two body lines. */ |
| 21 | +function page(index: number, options: { header?: string; footer?: string } = {}): PdfPageLines { |
| 22 | + const lines: PdfLine[] = [] |
| 23 | + if (options.header) lines.push(line(options.header, 729)) |
| 24 | + lines.push( |
| 25 | + line(`Body line one of page ${index}`, 600), |
| 26 | + line(`Body line two of page ${index}`, 585) |
| 27 | + ) |
| 28 | + if (options.footer) lines.push(line(options.footer, 55)) |
| 29 | + return { lines, pageHeight: PAGE_HEIGHT } |
| 30 | +} |
| 31 | + |
| 32 | +function texts(pages: PdfLine[][]): string[][] { |
| 33 | + return pages.map((lines) => lines.map((entry) => entry.text)) |
| 34 | +} |
| 35 | + |
| 36 | +describe('suppressFurniture', () => { |
| 37 | + it('drops a header repeated on enough pages but keeps its first occurrence', () => { |
| 38 | + const pages = [1, 2, 3, 4].map((i) => page(i, { header: 'ACME Corp — Internal Use Only' })) |
| 39 | + |
| 40 | + const result = texts(suppressFurniture(pages)) |
| 41 | + |
| 42 | + expect(result[0]).toContain('ACME Corp — Internal Use Only') |
| 43 | + for (const remaining of result.slice(1)) { |
| 44 | + expect(remaining).not.toContain('ACME Corp — Internal Use Only') |
| 45 | + expect(remaining).toHaveLength(2) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + it('never applies the frequency rule to a single page', () => { |
| 50 | + const result = texts(suppressFurniture([page(1, { header: 'Draft' })])) |
| 51 | + |
| 52 | + expect(result[0]).toContain('Draft') |
| 53 | + }) |
| 54 | + |
| 55 | + it('treats two matching pages as furniture when the document has exactly two pages', () => { |
| 56 | + const pages = [1, 2].map((i) => |
| 57 | + page(i, { footer: `Confidential draft, do not distribute — Page ${i} of 2` }) |
| 58 | + ) |
| 59 | + |
| 60 | + const result = texts(suppressFurniture(pages)) |
| 61 | + |
| 62 | + expect(result[0]).toContain('Confidential draft, do not distribute — Page 1 of 2') |
| 63 | + expect(result[1]).not.toContain('Confidential draft, do not distribute — Page 2 of 2') |
| 64 | + }) |
| 65 | + |
| 66 | + it('requires the fraction threshold on longer documents without a streak', () => { |
| 67 | + const pages = Array.from({ length: 10 }, (_, i) => |
| 68 | + page(i, { footer: i % 4 === 0 ? 'Sporadic note' : undefined }) |
| 69 | + ) |
| 70 | + |
| 71 | + const result = texts(suppressFurniture(pages)) |
| 72 | + |
| 73 | + expect(result.flat().filter((text) => text === 'Sporadic note')).toHaveLength(3) |
| 74 | + }) |
| 75 | + |
| 76 | + it('detects a footer by a three-page streak even when its title changes per chapter', () => { |
| 77 | + const pages = Array.from({ length: 12 }, (_, i) => { |
| 78 | + const chapter = i < 6 ? 'Chapter 1 Filing Information' : 'Chapter 2 Filing Status' |
| 79 | + const pageNumber = i + 6 |
| 80 | + const footer = |
| 81 | + i % 2 === 0 |
| 82 | + ? `${pageNumber} ${chapter} Publication 17 (2025)` |
| 83 | + : `Publication 17 (2025) ${chapter} ${pageNumber}` |
| 84 | + return page(i, { footer }) |
| 85 | + }) |
| 86 | + |
| 87 | + const result = texts(suppressFurniture(pages)) |
| 88 | + const footers = result.flat().filter((text) => text.includes('Publication 17')) |
| 89 | + |
| 90 | + expect(footers).toEqual([ |
| 91 | + '6 Chapter 1 Filing Information Publication 17 (2025)', |
| 92 | + '12 Chapter 2 Filing Status Publication 17 (2025)', |
| 93 | + ]) |
| 94 | + }) |
| 95 | + |
| 96 | + it('drops page numbers in the bands regardless of repetition', () => { |
| 97 | + const pages: PdfPageLines[] = [ |
| 98 | + { lines: [line('Page 1 of 3', 55), line('Body', 600)], pageHeight: PAGE_HEIGHT }, |
| 99 | + { lines: [line('2', 55), line('Body', 600), line('iv', 729)], pageHeight: PAGE_HEIGHT }, |
| 100 | + { lines: [line('- 3 -', 55), line('Body', 600), line('925', 60)], pageHeight: PAGE_HEIGHT }, |
| 101 | + ] |
| 102 | + |
| 103 | + expect(texts(suppressFurniture(pages))).toEqual([['Body'], ['Body'], ['Body', '925']]) |
| 104 | + }) |
| 105 | + |
| 106 | + it('ignores band text longer than the furniture cap, such as a repeated table header', () => { |
| 107 | + const header = `SKU Product Unit price Lead time Notes ${'Column '.repeat(14)}`.trim() |
| 108 | + expect(header.length).toBeGreaterThan(120) |
| 109 | + const pages = [1, 2, 3, 4].map((i) => page(i, { header })) |
| 110 | + |
| 111 | + const result = texts(suppressFurniture(pages)) |
| 112 | + |
| 113 | + for (const kept of result) expect(kept).toContain(header) |
| 114 | + }) |
| 115 | + |
| 116 | + it('merges same-baseline fragments into one key before matching', () => { |
| 117 | + const pages = [1, 2, 3, 4].map((i) => ({ |
| 118 | + lines: [ |
| 119 | + line('Body', 600), |
| 120 | + line(`${i}`, 31.3), |
| 121 | + line('Chapter 1', 31.3), |
| 122 | + line('Publication 17 (2025)', 32.5), |
| 123 | + ], |
| 124 | + pageHeight: PAGE_HEIGHT, |
| 125 | + })) |
| 126 | + |
| 127 | + const result = texts(suppressFurniture(pages)) |
| 128 | + |
| 129 | + expect(result[0]).toEqual(['Body', '1', 'Chapter 1', 'Publication 17 (2025)']) |
| 130 | + expect(result[3]).toEqual(['Body']) |
| 131 | + }) |
| 132 | + |
| 133 | + it('leaves body text alone even when it repeats', () => { |
| 134 | + const pages = [1, 2, 3, 4].map(() => ({ |
| 135 | + lines: [line('Repeated body sentence.', 400)], |
| 136 | + pageHeight: PAGE_HEIGHT, |
| 137 | + })) |
| 138 | + |
| 139 | + for (const kept of texts(suppressFurniture(pages))) { |
| 140 | + expect(kept).toEqual(['Repeated body sentence.']) |
| 141 | + } |
| 142 | + }) |
| 143 | + |
| 144 | + it('skips pages without a page height or line geometry', () => { |
| 145 | + const pages: PdfPageLines[] = [1, 2, 3].map(() => ({ |
| 146 | + lines: [{ text: 'Header', height: 0 }, line('Header', 729)], |
| 147 | + })) |
| 148 | + |
| 149 | + for (const kept of texts(suppressFurniture(pages))) expect(kept).toEqual(['Header', 'Header']) |
| 150 | + }) |
| 151 | +}) |
| 152 | + |
| 153 | +describe('normalizeFurnitureText', () => { |
| 154 | + it('keys mirrored facing-page footers identically', () => { |
| 155 | + expect(normalizeFurnitureText('6 Chapter 1 Filing Information Publication 17 (2025)')).toBe( |
| 156 | + normalizeFurnitureText('Publication 17 (2025) Chapter 1 Filing Information 17') |
| 157 | + ) |
| 158 | + }) |
| 159 | + |
| 160 | + it('collapses case, digits, and edge punctuation', () => { |
| 161 | + expect(normalizeFurnitureText(' Confidential DRAFT — Page 12 of 40. ')).toBe( |
| 162 | + '# # confidential draft of page' |
| 163 | + ) |
| 164 | + }) |
| 165 | +}) |
| 166 | + |
| 167 | +describe('isPageNumber', () => { |
| 168 | + it('matches the page-number shapes', () => { |
| 169 | + expect(isPageNumber('Page 3', 10)).toBe(true) |
| 170 | + expect(isPageNumber('page 3 of 10', 10)).toBe(true) |
| 171 | + expect(isPageNumber('3 / 10', 10)).toBe(true) |
| 172 | + expect(isPageNumber('7', 10)).toBe(true) |
| 173 | + expect(isPageNumber('xiv', 10)).toBe(true) |
| 174 | + expect(isPageNumber('— 12 —', 20)).toBe(true) |
| 175 | + }) |
| 176 | + |
| 177 | + it('rejects bare numbers beyond the page count and ordinary text', () => { |
| 178 | + expect(isPageNumber('925', 142)).toBe(false) |
| 179 | + expect(isPageNumber('2120', 142)).toBe(false) |
| 180 | + expect(isPageNumber('Chapter 1', 10)).toBe(false) |
| 181 | + expect(isPageNumber('civilian', 10)).toBe(false) |
| 182 | + }) |
| 183 | +}) |
| 184 | + |
| 185 | +describe('furnitureThreshold', () => { |
| 186 | + it('scales with the page count', () => { |
| 187 | + expect(furnitureThreshold(1)).toBeUndefined() |
| 188 | + expect(furnitureThreshold(2)).toBe(2) |
| 189 | + expect(furnitureThreshold(3)).toBe(3) |
| 190 | + expect(furnitureThreshold(10)).toBe(5) |
| 191 | + expect(furnitureThreshold(142)).toBe(71) |
| 192 | + }) |
| 193 | +}) |
0 commit comments