@@ -526,7 +526,7 @@ describe("configureWorkItemTools", () => {
526526 if ( ! call ) throw new Error ( "wit_link_work_item_to_pull_request tool not registered" ) ;
527527 const [ , , , handler ] = call ;
528528
529- ( mockWorkItemTrackingApi . updateWorkItem as jest . Mock ) . mockResolvedValue ( [
529+ ( mockWorkItemTrackingApi . updateWorkItem as jest . Mock ) . mockResolvedValue ( [
530530 _mockWorkItem ,
531531 ] ) ;
532532
@@ -575,6 +575,71 @@ describe("configureWorkItemTools", () => {
575575 )
576576 ) ;
577577 } ) ;
578+
579+ it ( "should handle errors from updateWorkItem and return a descriptive error" , async ( ) => {
580+ configureWorkItemTools ( server , tokenProvider , connectionProvider ) ;
581+ const call = ( server . tool as jest . Mock ) . mock . calls . find (
582+ ( [ toolName ] ) => toolName === "wit_link_work_item_to_pull_request"
583+ ) ;
584+
585+ if ( ! call ) throw new Error ( "wit_link_work_item_to_pull_request tool not registered" ) ;
586+
587+ const [ , , , handler ] = call ;
588+ ( mockWorkItemTrackingApi . updateWorkItem as jest . Mock ) . mockRejectedValue ( new Error ( "API failure" ) ) ;
589+
590+ const params = {
591+ project : "Contoso" ,
592+ repositoryId : 12345 ,
593+ pullRequestId : 67890 ,
594+ workItemId : 131489 ,
595+ } ;
596+ const result = await handler ( params ) ;
597+
598+ expect ( result . isError ) . toBe ( true ) ;
599+ expect ( result . content [ 0 ] . text ) . toContain ( "API failure" ) ;
600+ } ) ;
601+
602+ it ( "should encode special characters in project and repositoryId for vstfsUrl" , async ( ) => {
603+ configureWorkItemTools ( server , tokenProvider , connectionProvider ) ;
604+ const call = ( server . tool as jest . Mock ) . mock . calls . find (
605+ ( [ toolName ] ) => toolName === "wit_link_work_item_to_pull_request"
606+ ) ;
607+ if ( ! call ) throw new Error ( "wit_link_work_item_to_pull_request tool not registered" ) ;
608+
609+ const [ , , , handler ] = call ;
610+ ( mockWorkItemTrackingApi . updateWorkItem as jest . Mock ) . mockResolvedValue ( [
611+ _mockWorkItem ,
612+ ] ) ;
613+
614+ const params = {
615+ project : "Contoso Project" ,
616+ repositoryId : "repo/with/slash" ,
617+ pullRequestId : 67890 ,
618+ workItemId : 131489 ,
619+ } ;
620+ const artifactPathValue = `${ params . project } /${ params . repositoryId } /${ params . pullRequestId } ` ;
621+ const vstfsUrl = `vstfs:///Git/PullRequestId/${ encodeURIComponent ( artifactPathValue ) } ` ;
622+ const document = [
623+ {
624+ op : "add" ,
625+ path : "/relations/-" ,
626+ value : {
627+ rel : "ArtifactLink" ,
628+ url : vstfsUrl ,
629+ attributes : {
630+ name : "Pull Request" ,
631+ } ,
632+ } ,
633+ } ,
634+ ] ;
635+ await handler ( params ) ;
636+ expect ( mockWorkItemTrackingApi . updateWorkItem ) . toHaveBeenCalledWith (
637+ { } ,
638+ document ,
639+ params . workItemId ,
640+ params . project ,
641+ ) ;
642+ } ) ;
578643 } ) ;
579644
580645 describe ( "get_work_items_for_iteration tool" , ( ) => {
0 commit comments