@@ -220,4 +220,75 @@ describe('MermaidChart', () => {
220220 ) . rejects . toThrow ( AICreditsLimitExceededError ) ;
221221 } ) ;
222222 } ) ;
223+
224+ describe ( '#regenerateDiagram' , ( ) => {
225+ beforeEach ( async ( ) => {
226+ await client . setAccessToken ( 'test-access-token' ) ;
227+ } ) ;
228+
229+ it ( 'should POST to the regenerate endpoint with the request body and return response.data' , async ( ) => {
230+ const jsonResponse = {
231+ result : 'ok' as const ,
232+ code : '```mermaid\nflowchart TD\n A --> B --> C\n```' ,
233+ solved : true ,
234+ } ;
235+
236+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
237+ const postSpy = vi . spyOn ( ( client as any ) . axios , 'post' ) . mockResolvedValue ( {
238+ data : jsonResponse ,
239+ } ) ;
240+
241+ const requestBody = {
242+ code : 'flowchart TD\n A --> B' ,
243+ sourceFiles : [ 'const x = 1;' , 'function foo() {}' ] ,
244+ } ;
245+
246+ const result = await client . regenerateDiagram ( requestBody ) ;
247+
248+ expect ( postSpy ) . toHaveBeenCalledWith ( URLS . rest . openai . regenerate , requestBody ) ;
249+ expect ( result ) . toEqual ( jsonResponse ) ;
250+ } ) ;
251+
252+ it ( 'should include creditUsage in response when present' , async ( ) => {
253+ const jsonResponse = {
254+ result : 'ok' as const ,
255+ code : '```mermaid\nflowchart TD\n A --> B\n```' ,
256+ solved : true ,
257+ creditUsage : {
258+ creditsToDeduct : 1 ,
259+ baseCost : 1 ,
260+ reason : 'regeneration' ,
261+ } ,
262+ } ;
263+
264+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
265+ vi . spyOn ( ( client as any ) . axios , 'post' ) . mockResolvedValue ( {
266+ data : jsonResponse ,
267+ } ) ;
268+
269+ const result = await client . regenerateDiagram ( {
270+ code : 'flowchart TD\n A --> B' ,
271+ sourceFiles : [ 'const x = 1;' ] ,
272+ } ) ;
273+
274+ expect ( result . creditUsage ) . toEqual ( jsonResponse . creditUsage ) ;
275+ } ) ;
276+
277+ it ( 'should throw AICreditsLimitExceededError on 402' , async ( ) => {
278+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
279+ vi . spyOn ( ( client as any ) . axios , 'post' ) . mockRejectedValue ( {
280+ response : {
281+ status : 402 ,
282+ data : 'AI credits limit exceeded' ,
283+ } ,
284+ } ) ;
285+
286+ await expect (
287+ client . regenerateDiagram ( {
288+ code : 'flowchart TD\n A --> B' ,
289+ sourceFiles : [ ] ,
290+ } ) ,
291+ ) . rejects . toThrow ( AICreditsLimitExceededError ) ;
292+ } ) ;
293+ } ) ;
223294} ) ;
0 commit comments