@@ -97,7 +97,7 @@ describe("Pdf utility functions", () => {
9797 } ) ;
9898
9999 describe ( "loadPdf" , ( ) => {
100- it ( "should return true, null, and 0 if the file is not a PDF " , async ( ) => {
100+ it ( "should return true, null, and 0 if the file is null " , async ( ) => {
101101 const result = await loadPdf ( null ) ;
102102
103103 expect ( result ) . toEqual ( [ true , null , 0 ] ) ;
@@ -115,6 +115,19 @@ describe("Pdf utility functions", () => {
115115 expect ( file . content ) . not . toHaveBeenCalled ( ) ;
116116 } ) ;
117117
118+ it ( "should return true, null, and 0 if the file is not a PDF without basing on file extension" , async ( ) => {
119+ const file = {
120+ name : "uuid1234" ,
121+ content : jest . fn ( ) . mockResolvedValue ( new ArrayBuffer ( 0 ) ) ,
122+ } ;
123+
124+ const result = await loadPdf ( file as any ) ;
125+
126+ expect ( result ) . toEqual ( [ true , null , 0 ] ) ;
127+ expect ( file . content ) . not . toHaveBeenCalled ( ) ;
128+ } ) ;
129+
130+
118131 it ( "should return true, null, and 0 if there is an error while loading the PDF" , async ( ) => {
119132 const file = {
120133 name : "document.pdf" ,
@@ -143,5 +156,24 @@ describe("Pdf utility functions", () => {
143156 expect ( loadMock ) . toHaveBeenCalledTimes ( 1 ) ;
144157 expect ( loadMock ) . toHaveBeenCalledWith ( f . arrayBuffer ( ) ) ;
145158 } ) ;
159+
160+ it ( "should return false, PDFDocument object, and the number of pages if the PDF is loaded successfully without basing on file extension" , async ( ) => {
161+ const file = readFileSync ( "test/data/layout-parser-paper-fast.pdf" ) ;
162+ const f = {
163+ name : "uuid1234" ,
164+ arrayBuffer : ( ) => file . buffer ,
165+ } ;
166+
167+ jest . clearAllMocks ( ) ; // Reset Mocks Between Tests
168+ const loadMock = jest . spyOn ( PDFDocument , "load" ) ;
169+
170+ const [ error , _ , pages ] = await loadPdf ( f as any ) ;
171+
172+ expect ( error ) . toBeFalsy ( ) ;
173+ expect ( pages ) . toEqual ( 2 ) ;
174+ expect ( loadMock ) . toHaveBeenCalledTimes ( 1 ) ;
175+ expect ( loadMock ) . toHaveBeenCalledWith ( f . arrayBuffer ( ) ) ;
176+ } ) ;
177+
146178 } ) ;
147179} ) ;
0 commit comments