@@ -2,7 +2,7 @@ import { describe, it, expect, vi } from 'vitest';
22import {
33 ConfigureMcpUseCase ,
44 type ConfigureMcpDependencies ,
5- } from '../../../.. /usecases/cli/configureMcp.usecase.js' ;
5+ } from '@ /usecases/cli/configureMcp.usecase.js' ;
66
77function createFakeDeps (
88 overrides ?: Partial < ConfigureMcpDependencies > ,
@@ -31,9 +31,16 @@ describe('ConfigureMcpUseCase', () => {
3131 } ) ;
3232
3333 it ( 'should configure mcp when settings file does not exist' , ( ) => {
34+ const validWrittenSettings = JSON . stringify ( {
35+ mcpServers : {
36+ 'review-progress' : { command : 'node' , args : [ '/path/to/dist/mcpServer.js' ] } ,
37+ } ,
38+ } ) ;
3439 const deps = createFakeDeps ( {
3540 existsSync : vi . fn ( ( ) => false ) ,
36- readFileSync : vi . fn ( ( ) => { throw new Error ( 'ENOENT' ) ; } ) ,
41+ readFileSync : vi . fn ( )
42+ . mockImplementationOnce ( ( ) => { throw new Error ( 'ENOENT' ) ; } )
43+ . mockReturnValueOnce ( validWrittenSettings ) ,
3744 } ) ;
3845 const usecase = new ConfigureMcpUseCase ( deps ) ;
3946
@@ -69,16 +76,26 @@ describe('ConfigureMcpUseCase', () => {
6976 } ) ;
7077
7178 it ( 'should update mcp config when path has changed' , ( ) => {
72- const existingSettings = {
79+ const existingSettings = JSON . stringify ( {
7380 mcpServers : {
7481 'review-progress' : {
7582 command : 'node' ,
7683 args : [ '/old/path/mcpServer.js' ] ,
7784 } ,
7885 } ,
79- } ;
86+ } ) ;
87+ const updatedSettings = JSON . stringify ( {
88+ mcpServers : {
89+ 'review-progress' : {
90+ command : 'node' ,
91+ args : [ '/path/to/dist/mcpServer.js' ] ,
92+ } ,
93+ } ,
94+ } ) ;
8095 const deps = createFakeDeps ( {
81- readFileSync : vi . fn ( ( ) => JSON . stringify ( existingSettings ) ) ,
96+ readFileSync : vi . fn ( )
97+ . mockReturnValueOnce ( existingSettings )
98+ . mockReturnValueOnce ( updatedSettings ) ,
8299 } ) ;
83100 const usecase = new ConfigureMcpUseCase ( deps ) ;
84101
@@ -89,18 +106,27 @@ describe('ConfigureMcpUseCase', () => {
89106 } ) ;
90107
91108 it ( 'should preserve existing mcp servers when adding review-progress' , ( ) => {
92- const existingSettings = {
109+ const existingSettings = JSON . stringify ( {
93110 mcpServers : {
94111 'other-server' : { command : 'python' , args : [ 'server.py' ] } ,
95112 } ,
96- } ;
113+ } ) ;
114+ const updatedSettings = JSON . stringify ( {
115+ mcpServers : {
116+ 'other-server' : { command : 'python' , args : [ 'server.py' ] } ,
117+ 'review-progress' : { command : 'node' , args : [ '/path/to/dist/mcpServer.js' ] } ,
118+ } ,
119+ } ) ;
97120 const deps = createFakeDeps ( {
98- readFileSync : vi . fn ( ( ) => JSON . stringify ( existingSettings ) ) ,
121+ readFileSync : vi . fn ( )
122+ . mockReturnValueOnce ( existingSettings )
123+ . mockReturnValueOnce ( updatedSettings ) ,
99124 } ) ;
100125 const usecase = new ConfigureMcpUseCase ( deps ) ;
101126
102- usecase . execute ( ) ;
127+ const result = usecase . execute ( ) ;
103128
129+ expect ( result ) . toBe ( 'configured' ) ;
104130 const writtenContent = JSON . parse (
105131 ( deps . writeFileSync as ReturnType < typeof vi . fn > ) . mock . calls [ 0 ] [ 1 ] ,
106132 ) ;
@@ -109,8 +135,15 @@ describe('ConfigureMcpUseCase', () => {
109135 } ) ;
110136
111137 it ( 'should backup settings before modifying' , ( ) => {
138+ const validWrittenSettings = JSON . stringify ( {
139+ mcpServers : {
140+ 'review-progress' : { command : 'node' , args : [ '/path/to/dist/mcpServer.js' ] } ,
141+ } ,
142+ } ) ;
112143 const deps = createFakeDeps ( {
113- readFileSync : vi . fn ( ( ) => '{}' ) ,
144+ readFileSync : vi . fn ( )
145+ . mockReturnValueOnce ( '{}' )
146+ . mockReturnValueOnce ( validWrittenSettings ) ,
114147 } ) ;
115148 const usecase = new ConfigureMcpUseCase ( deps ) ;
116149
@@ -121,4 +154,35 @@ describe('ConfigureMcpUseCase', () => {
121154 '/home/user/.claude/settings.json.bak' ,
122155 ) ;
123156 } ) ;
157+
158+ it ( 'should return validation-failed when review-progress entry is missing after write' , ( ) => {
159+ const settingsWithoutReviewProgress = JSON . stringify ( {
160+ mcpServers : {
161+ 'some-other-server' : { command : 'python' , args : [ 'server.py' ] } ,
162+ } ,
163+ } ) ;
164+ const deps = createFakeDeps ( {
165+ readFileSync : vi . fn ( )
166+ . mockReturnValueOnce ( '{}' )
167+ . mockReturnValueOnce ( settingsWithoutReviewProgress ) ,
168+ } ) ;
169+ const usecase = new ConfigureMcpUseCase ( deps ) ;
170+
171+ const result = usecase . execute ( ) ;
172+
173+ expect ( result ) . toBe ( 'validation-failed' ) ;
174+ } ) ;
175+
176+ it ( 'should return validation-failed when written file is not valid' , ( ) => {
177+ const deps = createFakeDeps ( {
178+ readFileSync : vi . fn ( )
179+ . mockReturnValueOnce ( '{}' )
180+ . mockReturnValueOnce ( 'not json' ) ,
181+ } ) ;
182+ const usecase = new ConfigureMcpUseCase ( deps ) ;
183+
184+ const result = usecase . execute ( ) ;
185+
186+ expect ( result ) . toBe ( 'validation-failed' ) ;
187+ } ) ;
124188} ) ;
0 commit comments