1- import { describe , expect , it , vi } from "vitest" ;
2- import { existsSync , mkdtempSync , readFileSync } from "node:fs" ;
1+ import { describe , expect , it , onTestFinished , vi } from "vitest" ;
2+ import { existsSync , mkdtempSync , readFileSync , rmSync } from "node:fs" ;
33import { tmpdir } from "node:os" ;
44import { join } from "node:path" ;
55import type { Page } from "puppeteer-core" ;
@@ -10,6 +10,12 @@ import {
1010 pngHeight ,
1111} from "./screenshotCapture.js" ;
1212
13+ function tempDir ( prefix : string ) : string {
14+ const dir = mkdtempSync ( join ( tmpdir ( ) , prefix ) ) ;
15+ onTestFinished ( ( ) => rmSync ( dir , { recursive : true , force : true } ) ) ;
16+ return dir ;
17+ }
18+
1319// A real 1920x800 PNG header, so the produced-height guard sees something valid.
1420function pngBuffer ( height : number , width = 1920 ) : Buffer {
1521 const buf = Buffer . alloc ( 24 ) ;
@@ -38,7 +44,7 @@ function fakePage(
3844
3945describe ( "captureFullPagePlate — the scroll shot's plate" , ( ) => {
4046 it ( "writes one full-page png and returns its relative path" , async ( ) => {
41- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
47+ const dir = tempDir ( "hf-plate-" ) ;
4248 const { page, screenshot } = fakePage ( { docHeight : 10962 , plateHeight : 10962 } ) ;
4349
4450 const out = await captureFullPagePlate ( page , dir ) ;
@@ -49,7 +55,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
4955 } ) ;
5056
5157 it ( "stays 1x: it never touches the viewport's deviceScaleFactor" , async ( ) => {
52- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
58+ const dir = tempDir ( "hf-plate-" ) ;
5359 const setViewport = vi . fn ( async ( ) => undefined ) ;
5460 const { page } = fakePage ( { } , { setViewport } ) ;
5561
@@ -60,7 +66,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
6066 } ) ;
6167
6268 it ( "skips a page taller than Chrome can capture, instead of writing a clipped plate" , async ( ) => {
63- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
69+ const dir = tempDir ( "hf-plate-" ) ;
6470 const { page, screenshot } = fakePage ( { docHeight : MAX_PLATE_HEIGHT_PX + 1 } ) ;
6571
6672 const out = await captureFullPagePlate ( page , dir ) ;
@@ -71,7 +77,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
7177 } ) ;
7278
7379 it ( "neutralises sticky/fixed chrome for the shot and restores it afterwards" , async ( ) => {
74- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
80+ const dir = tempDir ( "hf-plate-" ) ;
7581 const { page, evaluate, screenshot } = fakePage ( ) ;
7682
7783 await captureFullPagePlate ( page , dir ) ;
@@ -96,7 +102,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
96102 } ) ;
97103
98104 it ( "restores the page even when the screenshot throws" , async ( ) => {
99- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
105+ const dir = tempDir ( "hf-plate-" ) ;
100106 const screenshot = vi . fn ( async ( _opts ?: unknown ) => {
101107 throw new Error ( "capture failed" ) ;
102108 } ) ;
@@ -111,7 +117,7 @@ describe("captureFullPagePlate — the scroll shot's plate", () => {
111117
112118describe ( "captureScrollScreenshots — capture budget" , ( ) => {
113119 it ( "does not begin page work when the post-navigation budget is exhausted" , async ( ) => {
114- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-scroll-budget-" ) ) ;
120+ const dir = tempDir ( "hf-scroll-budget-" ) ;
115121 const evaluate = vi . fn ( async ( ) => 1080 ) ;
116122 const screenshot = vi . fn ( async ( ) => pngBuffer ( 1080 ) ) ;
117123 const page = { evaluate, screenshot } as unknown as Page ;
@@ -126,7 +132,7 @@ describe("captureScrollScreenshots — capture budget", () => {
126132 it ( "re-checks the budget after settling and before each viewport screenshot" , async ( ) => {
127133 vi . useFakeTimers ( ) ;
128134 vi . setSystemTime ( 0 ) ;
129- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-scroll-expiring-budget-" ) ) ;
135+ const dir = tempDir ( "hf-scroll-expiring-budget-" ) ;
130136 const evaluate = vi . fn ( async ( expression : unknown ) => {
131137 const source = String ( expression ) ;
132138 if ( source . includes ( "Math.max(document.body.scrollHeight" ) ) return 1080 ;
@@ -155,7 +161,7 @@ describe("captureFullPagePlate — capture budget", () => {
155161 it ( "re-checks the budget immediately before the full-page screenshot" , async ( ) => {
156162 vi . useFakeTimers ( ) ;
157163 vi . setSystemTime ( 0 ) ;
158- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-expiring-budget-" ) ) ;
164+ const dir = tempDir ( "hf-plate-expiring-budget-" ) ;
159165 const screenshot = vi . fn ( async ( ) => pngBuffer ( 8000 ) ) ;
160166 const evaluate = vi . fn ( async ( expression : unknown ) => {
161167 if ( String ( expression ) . includes ( "scrollHeight" ) ) {
@@ -181,7 +187,7 @@ describe("captureFullPagePlate — capture budget", () => {
181187
182188describe ( "captureFullPagePlate — guards against a silently clipped plate" , ( ) => {
183189 it ( "measures the height itself, after lazy content has grown the page" , async ( ) => {
184- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
190+ const dir = tempDir ( "hf-plate-" ) ;
185191 // A page that measured 9000 before scrolling but is 20000 once lazy images land: the
186192 // pre-scroll number would have passed the guard and emitted a clipped plate.
187193 const { page, screenshot } = fakePage ( { docHeight : 20000 } ) ;
@@ -191,7 +197,7 @@ describe("captureFullPagePlate — guards against a silently clipped plate", ()
191197 } ) ;
192198
193199 it ( "discards a plate Chrome clipped, even when the measurement passed" , async ( ) => {
194- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
200+ const dir = tempDir ( "hf-plate-" ) ;
195201 // Measurement said 16000, but the capture itself triggered more loading and came back
196202 // over the cap. Emitting it would be undetectable downstream.
197203 const { page } = fakePage ( { docHeight : 16000 , plateHeight : MAX_PLATE_HEIGHT_PX + 500 } ) ;
@@ -201,7 +207,7 @@ describe("captureFullPagePlate — guards against a silently clipped plate", ()
201207 } ) ;
202208
203209 it ( "survives a restore that throws — the real error is what propagates" , async ( ) => {
204- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
210+ const dir = tempDir ( "hf-plate-" ) ;
205211 let call = 0 ;
206212 const evaluate = vi . fn ( async ( script ?: unknown ) => {
207213 call ++ ;
@@ -233,7 +239,7 @@ describe("pngHeight", () => {
233239
234240describe ( "captureFullPagePlate — the guard sees the post-neutralisation page (Magi's case)" , ( ) => {
235241 it ( "skips when the initial height is under the cap but the final height is over it" , async ( ) => {
236- const dir = mkdtempSync ( join ( tmpdir ( ) , "hf-plate-" ) ) ;
242+ const dir = tempDir ( "hf-plate-" ) ;
237243 // Pre-traversal the page measured 9000. Lazy content and un-fixing the sticky header push
238244 // it over the cap by the time the plate would be shot. Probing before either step would
239245 // have passed the guard and emitted a clipped plate.
0 commit comments