@@ -2,6 +2,7 @@ import express from 'express';
22import request from 'supertest' ;
33import { PassThrough } from 'stream' ;
44import { plantumlRouter } from '../../../../src/server/routes/plantuml' ;
5+ import { logger } from '../../../../src/utils/logger' ;
56
67const createPlantumlModule = ( svg : string ) => ( {
78 generate : jest . fn ( ( ) => {
@@ -16,6 +17,19 @@ const createPlantumlModule = (svg: string) => ({
1617 } ) ,
1718} ) ;
1819
20+ const createFailingPlantumlModule = ( error : Error ) => ( {
21+ generate : jest . fn ( ( ) => {
22+ const inputStream = new PassThrough ( ) ;
23+ const outputStream = new PassThrough ( ) ;
24+
25+ inputStream . on ( 'finish' , ( ) => {
26+ outputStream . emit ( 'error' , error ) ;
27+ } ) ;
28+
29+ return { in : inputStream , out : outputStream } ;
30+ } ) ,
31+ } ) ;
32+
1933describe ( 'plantuml.ts' , ( ) => {
2034 let app : express . Application ;
2135 let plantumlModule : ReturnType < typeof createPlantumlModule > ;
@@ -59,5 +73,25 @@ describe('plantuml.ts', () => {
5973 expect ( svgBody ) . toBe ( '<svg>diagram</svg>' ) ;
6074 expect ( plantumlModule . generate ) . toHaveBeenCalledWith ( { format : 'svg' } ) ;
6175 } ) ;
76+
77+ it ( 'should return 500 when SVG generation fails' , async ( ) => {
78+ const error = new Error ( 'generation failed' ) ;
79+ const errorSpy = jest . spyOn ( logger , 'error' ) . mockImplementation ( ( ) => { } ) ;
80+
81+ plantumlModule = createFailingPlantumlModule ( error ) ;
82+ app = express ( ) ;
83+ app . use ( express . json ( ) ) ;
84+ app . use ( '/api/plantuml' , plantumlRouter ( plantumlModule ) ) ;
85+
86+ const response = await request ( app )
87+ . post ( '/api/plantuml/svg' )
88+ . send ( { diagram : '@startuml\nA --> B\n@enduml' } ) ;
89+
90+ expect ( response . statusCode ) . toBe ( 500 ) ;
91+ expect ( response . text ) . toBe ( 'Error generating PlantUML diagram.' ) ;
92+ expect ( errorSpy ) . toHaveBeenCalledWith ( 'Error generating PlantUML diagram:' , error ) ;
93+
94+ errorSpy . mockRestore ( ) ;
95+ } ) ;
6296 } ) ;
6397} ) ;
0 commit comments