33import { beforeAll , beforeEach , describe , expect , it , vi } from "vitest" ;
44import { createSolidPngBuffer } from "../../test/helpers/image-fixtures.js" ;
55
6- const { infoMock, warnMock } = vi . hoisted ( ( ) => ( {
6+ const { infoMock, resizeToJpegMock , warnMock } = vi . hoisted ( ( ) => ( {
77 infoMock : vi . fn ( ) ,
8+ resizeToJpegMock : vi . fn ( ) ,
89 warnMock : vi . fn ( ) ,
910} ) ) ;
1011
@@ -24,6 +25,16 @@ vi.mock("../logging/subsystem.js", () => {
2425 return { createSubsystemLogger : ( ) => makeLogger ( ) } ;
2526} ) ;
2627
28+ vi . mock ( "../media/media-services.js" , ( ) => ( {
29+ buildImageResizeSideGrid : ( ) => [ 2000 ] ,
30+ getImageMetadata : vi . fn ( ) ,
31+ IMAGE_REDUCE_QUALITY_STEPS : [ 85 ] ,
32+ isImageProcessorUnavailableError : ( ) => false ,
33+ MAX_IMAGE_INPUT_PIXELS : 25_000_000 ,
34+ readImageMetadataFromHeader : ( ) => ( { width : 2001 , height : 8 } ) ,
35+ resizeToJpeg : resizeToJpegMock ,
36+ } ) ) ;
37+
2738import { sanitizeContentBlocksImages } from "./tool-images.js" ;
2839
2940async function createLargePng ( ) : Promise < Buffer > {
@@ -39,6 +50,8 @@ describe("tool-images log context", () => {
3950
4051 beforeEach ( ( ) => {
4152 infoMock . mockClear ( ) ;
53+ resizeToJpegMock . mockReset ( ) ;
54+ resizeToJpegMock . mockResolvedValue ( Buffer . alloc ( 100 , 2 ) ) ;
4255 warnMock . mockClear ( ) ;
4356 } ) ;
4457
@@ -50,4 +63,31 @@ describe("tool-images log context", () => {
5063 const messages = infoMock . mock . calls . map ( ( call ) => String ( call [ 0 ] ?? "" ) ) ;
5164 expect ( messages . join ( "\n" ) ) . toContain ( "sample-diagram.png" ) ;
5265 } ) ;
66+
67+ it . each ( [
68+ { sourceBytes : 200 , outputBytes : 150 , pct : 25 , label : "-25%" } ,
69+ { sourceBytes : 200 , outputBytes : 200 , pct : 0 , label : "0%" } ,
70+ { sourceBytes : 200 , outputBytes : 250 , pct : - 25 , label : "+25%" } ,
71+ { sourceBytes : 3 , outputBytes : 2 , pct : 33.3 , label : "-33.3%" } ,
72+ ] ) ( "logs the signed byte delta for $label" , async ( { sourceBytes, outputBytes, pct, label } ) => {
73+ resizeToJpegMock . mockResolvedValue ( Buffer . alloc ( outputBytes , 2 ) ) ;
74+ const blocks = [
75+ {
76+ type : "image" as const ,
77+ data : Buffer . alloc ( sourceBytes , 1 ) . toString ( "base64" ) ,
78+ mimeType : "image/png" ,
79+ } ,
80+ ] ;
81+ await sanitizeContentBlocksImages ( blocks , "read:/tmp/images/sample-diagram.png" ) ;
82+ const [ message , context ] = infoMock . mock . calls . at ( - 1 ) ?? [ ] ;
83+ expect ( message ) . toContain ( `sample-diagram.png 2001x8px` ) ;
84+ expect ( message ) . toContain ( `(${ label } )` ) ;
85+ expect ( message ) . not . toContain ( "(--" ) ;
86+ expect ( context ) . toMatchObject ( {
87+ fileName : "sample-diagram.png" ,
88+ sourceBytes,
89+ outputBytes,
90+ byteReductionPct : pct ,
91+ } ) ;
92+ } ) ;
5393} ) ;
0 commit comments