@@ -130,4 +130,95 @@ describe('github_comment file comments', () => {
130130 it ( 'no longer exposes the deprecated position parameter' , ( ) => {
131131 expect ( commentTool . params . position ) . toBeUndefined ( )
132132 } )
133+
134+ it ( 'leaves an untouched block on the reviews endpoint when commentType is unset' , ( ) => {
135+ const params : CreateCommentParams = {
136+ owner : 'octo' ,
137+ repo : 'demo' ,
138+ pullNumber : 7 ,
139+ body : 'Nice' ,
140+ apiKey : 'ghp_test' ,
141+ }
142+ const url = commentTool . request . url as ( params : CreateCommentParams ) => string
143+ const method = commentTool . request . method as ( params : CreateCommentParams ) => string
144+
145+ expect ( url ( params ) ) . toBe ( 'https://api.github.com/repos/octo/demo/pulls/7/reviews' )
146+ expect ( method ( params ) ) . toBe ( 'POST' )
147+ expect ( commentTool . request . body ?.( params ) ) . toEqual ( { body : 'Nice' , event : 'COMMENT' } )
148+ } )
149+
150+ it ( 'never looks the pull request up for a general PR comment carrying a path' , ( ) => {
151+ const params : CreateCommentParams = {
152+ owner : 'octo' ,
153+ repo : 'demo' ,
154+ pullNumber : 7 ,
155+ body : 'Nice' ,
156+ path : 'src/main.ts' ,
157+ commentType : 'pr_comment' ,
158+ apiKey : 'ghp_test' ,
159+ }
160+ const url = commentTool . request . url as ( params : CreateCommentParams ) => string
161+ const method = commentTool . request . method as ( params : CreateCommentParams ) => string
162+
163+ expect ( url ( params ) ) . toBe ( 'https://api.github.com/repos/octo/demo/pulls/7/comments' )
164+ expect ( method ( params ) ) . toBe ( 'POST' )
165+ expect ( commentTool . request . body ?.( params ) ) . toEqual ( { body : 'Nice' , event : 'COMMENT' } )
166+ } )
167+
168+ it ( 'posts a file comment left without a path to the reviews endpoint' , ( ) => {
169+ const { path, ...params } = FILE_COMMENT_PARAMS
170+ const url = commentTool . request . url as ( params : CreateCommentParams ) => string
171+ const method = commentTool . request . method as ( params : CreateCommentParams ) => string
172+
173+ expect ( url ( params ) ) . toBe ( 'https://api.github.com/repos/octo/demo/pulls/7/reviews' )
174+ expect ( method ( params ) ) . toBe ( 'POST' )
175+ } )
176+
177+ it ( 'does not fetch the pull request for a file comment left without a path' , async ( ) => {
178+ const { path, ...params } = FILE_COMMENT_PARAMS
179+
180+ const result = await commentTool . transformResponse ! ( createdCommentResponse ( ) , params )
181+
182+ expect ( fetchMock ) . not . toHaveBeenCalled ( )
183+ expect ( result . success ) . toBe ( true )
184+ } )
185+
186+ it ( 'coerces a line number typed into the short input to an integer' , ( ) => {
187+ const params = {
188+ ...FILE_COMMENT_PARAMS ,
189+ commitId : 'b' . repeat ( 40 ) ,
190+ line : '42' as unknown as number ,
191+ }
192+
193+ expect ( commentTool . request . body ?.( params ) ) . toEqual ( {
194+ body : 'Looks good' ,
195+ commit_id : 'b' . repeat ( 40 ) ,
196+ path : 'src/main.ts' ,
197+ line : 42 ,
198+ side : 'RIGHT' ,
199+ } )
200+ } )
201+
202+ it ( 'coerces the line on the resolved-commit path as well' , async ( ) => {
203+ fetchMock . mockResolvedValueOnce ( createdCommentResponse ( ) )
204+ const params = { ...FILE_COMMENT_PARAMS , line : '42' as unknown as number }
205+
206+ await commentTool . transformResponse ! ( pullRequestResponse ( ) , params )
207+
208+ expect ( JSON . parse ( fetchMock . mock . calls [ 0 ] [ 1 ] . body ) . line ) . toBe ( 42 )
209+ } )
210+
211+ it ( 'omits a blank or unparseable line rather than sending NaN' , ( ) => {
212+ for ( const line of [ '' , ' ' , 'abc' , undefined , null ] ) {
213+ const params = {
214+ ...FILE_COMMENT_PARAMS ,
215+ commitId : 'b' . repeat ( 40 ) ,
216+ line : line as unknown as number ,
217+ }
218+ const body = commentTool . request . body ?.( params ) as Record < string , unknown >
219+
220+ expect ( body ) . toHaveProperty ( 'line' )
221+ expect ( body . line ) . toBeUndefined ( )
222+ }
223+ } )
133224} )
0 commit comments