11import { describe , expect , it } from 'vitest'
22import { extractOcrImageFiles , MAX_OCR_EXTRACTED_IMAGES } from '@/lib/internal/mistral/images'
33
4- const PNG = Buffer . from ( 'synthetic png bytes' )
5- const dataUri = ( bytes : Buffer ) => `data:image/png ;base64,${ bytes . toString ( 'base64' ) } `
4+ const JPEG = Buffer . concat ( [ Buffer . from ( [ 0xff , 0xd8 , 0xff ] ) , Buffer . from ( 'synthetic jpeg body' ) ] )
5+ const dataUri = ( bytes : Buffer ) => `data:image/jpeg ;base64,${ bytes . toString ( 'base64' ) } `
66
77function image ( id : string , imageBase64 : string | undefined ) {
88 return {
@@ -16,28 +16,28 @@ function image(id: string, imageBase64: string | undefined) {
1616}
1717
1818describe ( 'extractOcrImageFiles' , ( ) => {
19- it ( 'moves image bytes into files and leaves no base64 in the page output' , ( ) => {
19+ it ( 'moves image bytes into files named as storage will name them, with no base64 left in the page output' , ( ) => {
2020 const { output, files } = extractOcrImageFiles ( {
2121 pages : [
2222 {
2323 index : 2 ,
24- markdown : '' ,
25- images : [ image ( 'img-0.png ' , dataUri ( PNG ) ) ] ,
24+ markdown : '' ,
25+ images : [ image ( 'img-0.jpeg ' , dataUri ( JPEG ) ) ] ,
2626 } ,
2727 ] ,
2828 } )
29- expect ( files ) . toEqual ( [ { buffer : PNG , name : 'page-2-img-0.png ' , mimeType : 'image/png ' } ] )
30- expect ( JSON . stringify ( output ) ) . not . toContain ( PNG . toString ( 'base64' ) )
29+ expect ( files ) . toEqual ( [ { buffer : JPEG , name : 'page-2-img-0.jpg ' , mimeType : 'image/jpeg ' } ] )
30+ expect ( JSON . stringify ( output ) ) . not . toContain ( JPEG . toString ( 'base64' ) )
3131 expect ( output . pages ) . toEqual ( [
3232 expect . objectContaining ( {
33- images : [ expect . objectContaining ( { id : 'img-0.png ' , file_name : 'page-2-img-0.png ' } ) ] ,
33+ images : [ expect . objectContaining ( { id : 'img-0.jpeg ' , file_name : 'page-2-img-0.jpg ' } ) ] ,
3434 } ) ,
3535 ] )
3636 } )
3737
3838 it ( 'stores at most the image cap and keeps the bounding boxes of the rest without a file' , ( ) => {
3939 const images = Array . from ( { length : MAX_OCR_EXTRACTED_IMAGES + 1 } , ( _ , i ) =>
40- image ( `img-${ i } .jpeg` , PNG . toString ( 'base64' ) )
40+ image ( `img-${ i } .jpeg` , JPEG . toString ( 'base64' ) )
4141 )
4242 const { output, files } = extractOcrImageFiles ( { pages : [ { index : 0 , images } ] } )
4343 expect ( files ) . toHaveLength ( MAX_OCR_EXTRACTED_IMAGES )
@@ -51,10 +51,13 @@ describe('extractOcrImageFiles', () => {
5151 const huge = Buffer . alloc ( 21 * 1024 * 1024 )
5252 const { output, files } = extractOcrImageFiles ( {
5353 pages : [
54- { index : 0 , images : [ image ( 'img-0.png' , dataUri ( huge ) ) , image ( 'img-1.png' , dataUri ( PNG ) ) ] } ,
54+ {
55+ index : 0 ,
56+ images : [ image ( 'img-0.png' , dataUri ( huge ) ) , image ( 'img-1.jpg' , dataUri ( JPEG ) ) ] ,
57+ } ,
5558 ] ,
5659 } )
57- expect ( files . map ( ( file ) => file . name ) ) . toEqual ( [ 'page-0-img-1.png ' ] )
60+ expect ( files . map ( ( file ) => file . name ) ) . toEqual ( [ 'page-0-img-1.jpg ' ] )
5861 expect ( JSON . stringify ( output ) ) . not . toContain ( 'image_base64' )
5962 } )
6063} )
0 commit comments