@@ -5,75 +5,82 @@ import path from 'path';
55
66const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
77const FIXTURES = path . join ( __dirname , 'fixtures' ) ;
8- if ( ! fs . existsSync ( FIXTURES ) ) fs . mkdirSync ( FIXTURES , { recursive : true } ) ;
98
10- // Single-line fixture
11- const singleLineDoc = await PDFDocument . create ( ) ;
12- const singleLineFont = await singleLineDoc . embedFont ( StandardFonts . Helvetica ) ;
13- const singleLinePage = singleLineDoc . addPage ( [ 612 , 792 ] ) ; // dimension of US Letter page
14- singleLinePage . drawText (
15- 'known phrase from fixture' ,
16- { x : 72 , y : 720 , size : 12 , font : singleLineFont }
17- ) ;
18- fs . writeFileSync (
19- path . join ( FIXTURES , 'single-line.pdf' ) ,
20- await singleLineDoc . save ( )
21- ) ;
9+ /**
10+ * Vitest globalSetup: regenerate the PDF test fixtures (deterministically, via
11+ * pdf-lib) before the suite runs. The fixtures are gitignored — edit this
12+ * generator, never the binaries. Runs for every invocation (test, coverage,
13+ * watch, IDE), so the suite is self-contained from a clean checkout.
14+ */
15+ export default async function setup ( ) {
16+ fs . mkdirSync ( FIXTURES , { recursive : true } ) ;
2217
23- // Multi -line fixture
24- const multiLineDoc = await PDFDocument . create ( ) ;
25- const multiLineFont = await multiLineDoc . embedFont ( StandardFonts . Helvetica ) ;
26- const multiLinePage = multiLineDoc . addPage ( [ 612 , 792 ] ) ;
27- multiLinePage . drawText (
28- 'first line of text\nsecond line of text\nthird line of text ',
29- { x : 72 , y : 720 , size : 12 , font : multiLineFont , lineHeight : 20 }
30- ) ;
31- fs . writeFileSync (
32- path . join ( FIXTURES , 'multi -line.pdf' ) ,
33- await multiLineDoc . save ( )
34- ) ;
18+ // Single -line fixture
19+ const singleLineDoc = await PDFDocument . create ( ) ;
20+ const singleLineFont = await singleLineDoc . embedFont ( StandardFonts . Helvetica ) ;
21+ const singleLinePage = singleLineDoc . addPage ( [ 612 , 792 ] ) ; // dimension of US Letter page
22+ singleLinePage . drawText (
23+ 'known phrase from fixture ',
24+ { x : 72 , y : 720 , size : 12 , font : singleLineFont }
25+ ) ;
26+ fs . writeFileSync (
27+ path . join ( FIXTURES , 'single -line.pdf' ) ,
28+ await singleLineDoc . save ( )
29+ ) ;
3530
36- // Multi-page fixture
37- const multiPageDoc = await PDFDocument . create ( ) ;
38- const multiPageFont = await multiPageDoc . embedFont ( StandardFonts . Helvetica ) ;
39- const page1 = multiPageDoc . addPage ( [ 612 , 792 ] ) ;
40- const page2 = multiPageDoc . addPage ( [ 612 , 792 ] ) ;
41- page1 . drawText (
42- 'content on page one' ,
43- { x : 72 , y : 720 , size : 12 , font : multiPageFont }
44- ) ;
45- page2 . drawText (
46- 'content on page two' ,
47- { x : 72 , y : 720 , size : 12 , font : multiPageFont }
48- ) ;
49- fs . writeFileSync (
50- path . join ( FIXTURES , 'multi-page.pdf' ) ,
51- await multiPageDoc . save ( )
52- ) ;
31+ // Multi-line fixture
32+ const multiLineDoc = await PDFDocument . create ( ) ;
33+ const multiLineFont = await multiLineDoc . embedFont ( StandardFonts . Helvetica ) ;
34+ const multiLinePage = multiLineDoc . addPage ( [ 612 , 792 ] ) ;
35+ multiLinePage . drawText (
36+ 'first line of text\nsecond line of text\nthird line of text' ,
37+ { x : 72 , y : 720 , size : 12 , font : multiLineFont , lineHeight : 20 }
38+ ) ;
39+ fs . writeFileSync (
40+ path . join ( FIXTURES , 'multi-line.pdf' ) ,
41+ await multiLineDoc . save ( )
42+ ) ;
5343
54- // Multi-column fixture
55- const multiColumnDoc = await PDFDocument . create ( ) ;
56- const multiColumnFont = await multiColumnDoc . embedFont ( StandardFonts . Helvetica ) ;
57- const multiColumnPage = multiColumnDoc . addPage ( [ 612 , 792 ] ) ;
58- multiColumnPage . drawText (
59- 'left column line one\nleft column line two' ,
60- { x : 72 , y : 720 , size : 12 , font : multiColumnFont , lineHeight : 20 }
61- ) ;
62- multiColumnPage . drawText (
63- 'right column line one\nright column line two' ,
64- { x : 320 , y : 720 , size : 12 , font : multiColumnFont , lineHeight : 20 }
65- ) ;
66- fs . writeFileSync (
67- path . join ( FIXTURES , 'multi-column.pdf' ) ,
68- await multiColumnDoc . save ( )
69- ) ;
44+ // Multi-page fixture
45+ const multiPageDoc = await PDFDocument . create ( ) ;
46+ const multiPageFont = await multiPageDoc . embedFont ( StandardFonts . Helvetica ) ;
47+ const page1 = multiPageDoc . addPage ( [ 612 , 792 ] ) ;
48+ const page2 = multiPageDoc . addPage ( [ 612 , 792 ] ) ;
49+ page1 . drawText (
50+ 'content on page one' ,
51+ { x : 72 , y : 720 , size : 12 , font : multiPageFont }
52+ ) ;
53+ page2 . drawText (
54+ 'content on page two' ,
55+ { x : 72 , y : 720 , size : 12 , font : multiPageFont }
56+ ) ;
57+ fs . writeFileSync (
58+ path . join ( FIXTURES , 'multi-page.pdf' ) ,
59+ await multiPageDoc . save ( )
60+ ) ;
7061
71- // Scanned fixture — empty page, no text layer
72- const scannedDoc = await PDFDocument . create ( ) ;
73- scannedDoc . addPage ( [ 612 , 792 ] ) ;
74- fs . writeFileSync (
75- path . join ( FIXTURES , 'scanned.pdf' ) ,
76- await scannedDoc . save ( )
77- ) ;
62+ // Multi-column fixture
63+ const multiColumnDoc = await PDFDocument . create ( ) ;
64+ const multiColumnFont = await multiColumnDoc . embedFont ( StandardFonts . Helvetica ) ;
65+ const multiColumnPage = multiColumnDoc . addPage ( [ 612 , 792 ] ) ;
66+ multiColumnPage . drawText (
67+ 'left column line one\nleft column line two' ,
68+ { x : 72 , y : 720 , size : 12 , font : multiColumnFont , lineHeight : 20 }
69+ ) ;
70+ multiColumnPage . drawText (
71+ 'right column line one\nright column line two' ,
72+ { x : 320 , y : 720 , size : 12 , font : multiColumnFont , lineHeight : 20 }
73+ ) ;
74+ fs . writeFileSync (
75+ path . join ( FIXTURES , 'multi-column.pdf' ) ,
76+ await multiColumnDoc . save ( )
77+ ) ;
7878
79- console . log ( 'PDF fixtures generated.' ) ;
79+ // Scanned fixture — empty page, no text layer
80+ const scannedDoc = await PDFDocument . create ( ) ;
81+ scannedDoc . addPage ( [ 612 , 792 ] ) ;
82+ fs . writeFileSync (
83+ path . join ( FIXTURES , 'scanned.pdf' ) ,
84+ await scannedDoc . save ( )
85+ ) ;
86+ }
0 commit comments