@@ -3862,6 +3862,168 @@ describe("repos tools", () => {
38623862
38633863 expect ( result . content [ 0 ] . text ) . toBe ( JSON . stringify ( expectedResponse , null , 2 ) ) ;
38643864 } ) ;
3865+
3866+ it ( "should include changed files when includeChangedFiles is true" , async ( ) => {
3867+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3868+
3869+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3870+ if ( ! call ) throw new Error ( "repo_get_pull_request_by_id tool not registered" ) ;
3871+ const [ , , , handler ] = call ;
3872+
3873+ const mockPR = {
3874+ pullRequestId : 123 ,
3875+ title : "Test PR" ,
3876+ repository : { project : { id : "project123" , name : "testproject" } } ,
3877+ } ;
3878+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
3879+
3880+ const mockChangeEntries = [
3881+ { changeTrackingId : 1 , item : { path : "/src/file1.ts" } , changeType : 2 } ,
3882+ { changeTrackingId : 2 , item : { path : "/src/file2.ts" } , changeType : 1 } ,
3883+ ] ;
3884+ mockGitApi . getPullRequestIterations . mockResolvedValue ( [ { id : 1 } , { id : 2 } ] ) ;
3885+ mockGitApi . getPullRequestIterationChanges . mockResolvedValue ( { changeEntries : mockChangeEntries } ) ;
3886+
3887+ const params = {
3888+ repositoryId : "repo123" ,
3889+ pullRequestId : 123 ,
3890+ includeChangedFiles : true ,
3891+ } ;
3892+
3893+ const result = await handler ( params ) ;
3894+
3895+ expect ( mockGitApi . getPullRequestIterations ) . toHaveBeenCalledWith ( "repo123" , 123 , undefined ) ;
3896+ expect ( mockGitApi . getPullRequestIterationChanges ) . toHaveBeenCalledWith ( "repo123" , 123 , 2 , undefined ) ;
3897+
3898+ const resultData = JSON . parse ( result . content [ 0 ] . text ) ;
3899+ expect ( resultData . changedFilesSummary ) . toEqual ( {
3900+ changeEntries : mockChangeEntries ,
3901+ fileCount : 2 ,
3902+ } ) ;
3903+ } ) ;
3904+
3905+ it ( "should not fetch changed files when includeChangedFiles is false" , async ( ) => {
3906+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3907+
3908+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3909+ const [ , , , handler ] = call ;
3910+
3911+ const mockPR = { pullRequestId : 123 , title : "Test PR" } ;
3912+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
3913+
3914+ const result = await handler ( { repositoryId : "repo123" , pullRequestId : 123 , includeChangedFiles : false } ) ;
3915+
3916+ expect ( mockGitApi . getPullRequestIterations ) . not . toHaveBeenCalled ( ) ;
3917+ expect ( result . content [ 0 ] . text ) . toBe ( JSON . stringify ( mockPR , null , 2 ) ) ;
3918+ } ) ;
3919+
3920+ it ( "should not fetch changed files when includeChangedFiles is not specified" , async ( ) => {
3921+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3922+
3923+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3924+ const [ , , , handler ] = call ;
3925+
3926+ const mockPR = { pullRequestId : 123 , title : "Test PR" } ;
3927+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
3928+
3929+ const result = await handler ( { repositoryId : "repo123" , pullRequestId : 123 } ) ;
3930+
3931+ expect ( mockGitApi . getPullRequestIterations ) . not . toHaveBeenCalled ( ) ;
3932+ expect ( result . content [ 0 ] . text ) . toBe ( JSON . stringify ( mockPR , null , 2 ) ) ;
3933+ } ) ;
3934+
3935+ it ( "should handle empty iterations when includeChangedFiles is true" , async ( ) => {
3936+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3937+
3938+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3939+ const [ , , , handler ] = call ;
3940+
3941+ const mockPR = { pullRequestId : 123 , title : "Test PR" } ;
3942+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
3943+ mockGitApi . getPullRequestIterations . mockResolvedValue ( [ ] ) ;
3944+
3945+ const result = await handler ( { repositoryId : "repo123" , pullRequestId : 123 , includeChangedFiles : true } ) ;
3946+
3947+ const resultData = JSON . parse ( result . content [ 0 ] . text ) ;
3948+ expect ( resultData . changedFilesSummary ) . toEqual ( { changeEntries : [ ] , fileCount : 0 } ) ;
3949+ expect ( mockGitApi . getPullRequestIterationChanges ) . not . toHaveBeenCalled ( ) ;
3950+ } ) ;
3951+
3952+ it ( "should handle getPullRequestIterationChanges API error gracefully" , async ( ) => {
3953+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3954+
3955+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3956+ const [ , , , handler ] = call ;
3957+
3958+ const mockPR = { pullRequestId : 123 , title : "Test PR" } ;
3959+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
3960+ mockGitApi . getPullRequestIterations . mockResolvedValue ( [ { id : 1 } ] ) ;
3961+ mockGitApi . getPullRequestIterationChanges . mockRejectedValue ( new Error ( "API Error: Changes not accessible" ) ) ;
3962+
3963+ const consoleSpy = jest . spyOn ( console , "warn" ) . mockImplementation ( ) ;
3964+
3965+ const result = await handler ( { repositoryId : "repo123" , pullRequestId : 123 , includeChangedFiles : true } ) ;
3966+
3967+ expect ( consoleSpy ) . toHaveBeenCalledWith ( "Error fetching PR changed files: API Error: Changes not accessible" ) ;
3968+
3969+ const resultData = JSON . parse ( result . content [ 0 ] . text ) ;
3970+ expect ( resultData . pullRequestId ) . toBe ( 123 ) ;
3971+ expect ( resultData . changedFilesSummary ) . toEqual ( { } ) ;
3972+
3973+ consoleSpy . mockRestore ( ) ;
3974+ } ) ;
3975+
3976+ it ( "should handle iteration with null id when includeChangedFiles is true" , async ( ) => {
3977+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3978+
3979+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3980+ const [ , , , handler ] = call ;
3981+
3982+ const mockPR = { pullRequestId : 123 , title : "Test PR" } ;
3983+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
3984+ mockGitApi . getPullRequestIterations . mockResolvedValue ( [ { id : null } ] ) ;
3985+
3986+ const result = await handler ( { repositoryId : "repo123" , pullRequestId : 123 , includeChangedFiles : true } ) ;
3987+
3988+ const resultData = JSON . parse ( result . content [ 0 ] . text ) ;
3989+ expect ( resultData . changedFilesSummary ) . toEqual ( { changeEntries : [ ] , fileCount : 0 } ) ;
3990+ expect ( mockGitApi . getPullRequestIterationChanges ) . not . toHaveBeenCalled ( ) ;
3991+ } ) ;
3992+
3993+ it ( "should work with both includeLabels and includeChangedFiles enabled" , async ( ) => {
3994+ configureRepoTools ( server , tokenProvider , connectionProvider , userAgentProvider ) ;
3995+
3996+ const call = ( server . tool as jest . Mock ) . mock . calls . find ( ( [ toolName ] ) => toolName === REPO_TOOLS . get_pull_request_by_id ) ;
3997+ const [ , , , handler ] = call ;
3998+
3999+ const mockPR = {
4000+ pullRequestId : 123 ,
4001+ title : "Test PR" ,
4002+ repository : { project : { id : "project123" , name : "testproject" } } ,
4003+ } ;
4004+ mockGitApi . getPullRequest . mockResolvedValue ( mockPR ) ;
4005+
4006+ const mockLabels = [ { name : "bug" , id : "label1" } ] ;
4007+ mockGitApi . getPullRequestLabels . mockResolvedValue ( mockLabels ) ;
4008+
4009+ const mockChangeEntries = [ { changeTrackingId : 1 , item : { path : "/src/app.ts" } , changeType : 2 } ] ;
4010+ mockGitApi . getPullRequestIterations . mockResolvedValue ( [ { id : 1 } ] ) ;
4011+ mockGitApi . getPullRequestIterationChanges . mockResolvedValue ( { changeEntries : mockChangeEntries } ) ;
4012+
4013+ const result = await handler ( {
4014+ repositoryId : "repo123" ,
4015+ pullRequestId : 123 ,
4016+ includeLabels : true ,
4017+ includeChangedFiles : true ,
4018+ } ) ;
4019+
4020+ expect ( mockGitApi . getPullRequestLabels ) . toHaveBeenCalled ( ) ;
4021+ expect ( mockGitApi . getPullRequestIterations ) . toHaveBeenCalled ( ) ;
4022+
4023+ const resultData = JSON . parse ( result . content [ 0 ] . text ) ;
4024+ expect ( resultData . labelSummary ) . toEqual ( { labels : [ "bug" ] , labelCount : 1 } ) ;
4025+ expect ( resultData . changedFilesSummary ) . toEqual ( { changeEntries : mockChangeEntries , fileCount : 1 } ) ;
4026+ } ) ;
38654027 } ) ;
38664028
38674029 describe ( "repo_get_pull_request_changes" , ( ) => {
0 commit comments