@@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
22import { MermaidChart } from './index.js' ;
33import { AICreditsLimitExceededError } from './errors.js' ;
44import type { AuthorizationData } from './types.js' ;
5+ import { URLS } from './urls.js' ;
56
67import { OAuth2Client } from '@badgateway/oauth2-client' ;
78
@@ -171,4 +172,52 @@ describe('MermaidChart', () => {
171172 ) . rejects . toThrow ( AICreditsLimitExceededError ) ;
172173 } ) ;
173174 } ) ;
175+
176+ describe ( '#suggestPrSummary' , ( ) => {
177+ beforeEach ( async ( ) => {
178+ await client . setAccessToken ( 'test-access-token' ) ;
179+ } ) ;
180+
181+ it ( 'should POST to the pr-summary endpoint with the request body and return response.data' , async ( ) => {
182+ const jsonResponse = {
183+ title : 'Add validation step to flowchart' ,
184+ description : '## What changed\n- Added node C' ,
185+ branchName : 'feature/flowchart-validation' ,
186+ commitMessage : 'Add validation node C' ,
187+ } ;
188+
189+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
190+ const postSpy = vi . spyOn ( ( client as any ) . axios , 'post' ) . mockResolvedValue ( {
191+ data : jsonResponse ,
192+ } ) ;
193+
194+ const requestBody = {
195+ originalDiagram : 'flowchart TD\n A --> B' ,
196+ editedDiagram : 'flowchart TD\n A --> B\n B --> C[Validate]' ,
197+ } ;
198+
199+ const result = await client . suggestPrSummary ( requestBody ) ;
200+
201+ expect ( postSpy ) . toHaveBeenCalledWith ( URLS . rest . openai . prSummary , requestBody ) ;
202+ expect ( result ) . toEqual ( jsonResponse ) ;
203+ } ) ;
204+
205+ it ( 'should throw AICreditsLimitExceededError on 402' , async ( ) => {
206+ // Mock the underlying axios call so the error mapping in suggestPrSummary is exercised.
207+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
208+ vi . spyOn ( ( client as any ) . axios , 'post' ) . mockRejectedValue ( {
209+ response : {
210+ status : 402 ,
211+ data : 'AI credits limit exceeded' ,
212+ } ,
213+ } ) ;
214+
215+ await expect (
216+ client . suggestPrSummary ( {
217+ originalDiagram : 'flowchart TD\n A --> B' ,
218+ editedDiagram : 'flowchart TD\n A --> B\n B --> C' ,
219+ } ) ,
220+ ) . rejects . toThrow ( AICreditsLimitExceededError ) ;
221+ } ) ;
222+ } ) ;
174223} ) ;
0 commit comments