@@ -6,25 +6,9 @@ import {createLayoutExtraction} from './layout-extraction.mjs';
66
77const pause = ms => new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
88
9- test ( 'coalesces same-entry requests, sorts and deduplicates pages, and splits by one-indexed page' , async ( ) => {
10- const calls = [ ] ; const service = createLayoutExtraction ( { extractor :async ( bytes , pages , options ) => { calls . push ( { bytes, pages :[ ...pages ] , options} ) ; return pages . flatMap ( page => [ { page, text :`page-${ page } ` } ] ) ; } } ) ;
11- const entry = { id :'fixture' , bytes :Buffer . from ( '%PDF' ) } ;
12- const values = await Promise . all ( [ service . extractPage ( entry , 3 ) , service . extractPage ( entry , 1 ) , service . extractPage ( entry , 3 ) , service . extractPage ( entry , 2 ) , service . extractPage ( entry , 2 ) ] ) ;
13- assert . deepEqual ( calls , [ { bytes :entry . bytes , pages :[ 1 , 2 , 3 ] , options :{ frame :'display' } } ] ) ; assert . deepEqual ( values . map ( items => items [ 0 ] . page ) , [ 3 , 1 , 3 , 2 , 2 ] ) ; assert . deepEqual ( service . stats ( entry ) , { pages :3 , bytes :jsonSize ( [ { page :1 , text :'page-1' } ] ) + jsonSize ( [ { page :2 , text :'page-2' } ] ) + jsonSize ( [ { page :3 , text :'page-3' } ] ) , pendingPages :0 } ) ;
14- const before = calls . length ; assert . deepEqual ( await service . extractPage ( entry , 1 ) , [ { page :1 , text :'page-1' } ] ) ; assert . equal ( calls . length , before ) ;
15- } ) ;
16-
179test ( 'cleanup cancels pending and in-flight waiters and ignores late native results' , async ( ) => {
1810 let release ; let calls = 0 ; const service = createLayoutExtraction ( { extractor :async ( _bytes , pages ) => { calls ++ ; return new Promise ( resolve => { release = ( ) => resolve ( pages . map ( page => ( { page} ) ) ) ; } ) ; } } ) ; const entry = { id :'cleanup-inflight' , bytes :Buffer . from ( '%PDF' ) } ;
1911 const pendingEntry = { id :'cleanup-pending' , bytes :Buffer . from ( '%PDF' ) } ; const pending = service . extractPage ( pendingEntry , 1 ) ; assert . equal ( service . cleanup ( pendingEntry ) , true ) ; await assert . rejects ( pending , / c l e a n e d u p / ) ;
2012 const waiting = service . extractPage ( entry , 1 ) ; await pause ( 8 ) ; const oldRelease = release ; assert . equal ( typeof oldRelease , 'function' ) ; assert . equal ( service . cleanup ( entry ) , true ) ; await assert . rejects ( waiting , / c l e a n e d u p / ) ; oldRelease ( ) ; await pause ( 2 ) ; assert . deepEqual ( service . stats ( entry ) , { pages :0 , bytes :0 , pendingPages :0 } ) ;
2113 const next = service . extractPage ( entry , 1 ) ; await pause ( 8 ) ; assert . equal ( calls , 2 ) ; const newRelease = release ; assert . equal ( typeof newRelease , 'function' ) ; newRelease ( ) ; assert . deepEqual ( await next , [ { page :1 } ] ) ;
2214} ) ;
23-
24- test ( 'limits cache by exact JSON bytes and page count with LRU eviction' , async ( ) => {
25- const service = createLayoutExtraction ( { maxPages :2 , maxBytes :100 , extractor :async ( _bytes , pages ) => pages . map ( page => ( { page, text :'x' . repeat ( page * 20 ) } ) ) } ) ;
26- const entry = { id :'cache' , bytes :Buffer . from ( '%PDF' ) } ; await service . extractPage ( entry , 1 ) ; await service . extractPage ( entry , 2 ) ; assert . deepEqual ( service . stats ( entry ) , { pages :1 , bytes :jsonSize ( [ { page :2 , text :'x' . repeat ( 40 ) } ] ) , pendingPages :0 } ) ;
27- await service . extractPage ( entry , 3 ) ; const current = service . stats ( entry ) ; assert . ok ( current . pages <= 2 ) ; assert . ok ( current . bytes <= 100 ) ; assert . deepEqual ( await service . extractPage ( entry , 1 ) , [ { page :1 , text :'x' . repeat ( 20 ) } ] ) ;
28- } ) ;
29-
30- function jsonSize ( value ) { return Buffer . byteLength ( JSON . stringify ( value ) , 'utf8' ) ; }
0 commit comments