9
9
"path/filepath"
10
10
"testing"
11
11
12
+ "github.com/stacklok/toolhive/pkg/logger"
12
13
"github.com/stacklok/toolhive/pkg/transport/ssecommon"
13
14
)
14
15
@@ -62,7 +63,7 @@ func setupTestConfig(t *testing.T, testName string) (string, string, ConfigFile)
62
63
}
63
64
64
65
// getMCPServers reads the config file and returns the mcpServers map
65
- func getMCPServers (t * testing.T , configPath string ) map [string ]interface {} {
66
+ func getMCPServers (t * testing.T , configPath string , editor ConfigEditor ) map [string ]interface {} {
66
67
t .Helper ()
67
68
68
69
// Read the config file
@@ -71,27 +72,48 @@ func getMCPServers(t *testing.T, configPath string) map[string]interface{} {
71
72
t .Fatalf ("Failed to read updated config file: %v" , err )
72
73
}
73
74
74
- // Check if the servers were updated correctly
75
- mcpServers , ok := updatedConfig .Contents ["mcpServers" ].(map [string ]interface {})
76
- if ! ok {
77
- t .Fatalf ("mcpServers is not a map" )
75
+ _ , vsOk := editor .(* VSCodeConfigEditor )
76
+ if vsOk {
77
+ updatedConfig .Editor = editor
78
+ mcpMap , ok := updatedConfig .Contents ["mcp" ].(map [string ]interface {})
79
+ if ! ok {
80
+ t .Fatalf ("mcp is not a map" )
81
+ }
82
+
83
+ // Get servers child object
84
+ mcpServers , ok := mcpMap ["servers" ]
85
+ if ! ok {
86
+ t .Fatalf ("mcpServers is not a map" )
87
+ }
88
+ return mcpServers .(map [string ]interface {})
89
+ }
90
+
91
+ _ , standardOk := editor .(* StandardConfigEditor )
92
+ if standardOk {
93
+ mcpServers , ok := updatedConfig .Contents ["mcpServers" ].(map [string ]interface {})
94
+ if ! ok {
95
+ t .Fatalf ("mcpServers is not a map" )
96
+ }
97
+ return mcpServers
78
98
}
79
99
80
- return mcpServers
100
+ return nil
81
101
}
82
102
83
103
// testUpdateExistingServer tests updating an existing server
84
104
func testUpdateExistingServer (t * testing.T , config ConfigFile , configPath string ) {
85
105
t .Helper ()
86
106
// Test updating an existing server with lock
87
107
expectedURL := "http://localhost:54321" + ssecommon .HTTPSSEEndpoint + "#test-container"
88
- err := config .SaveWithLock ("existing-server" , expectedURL , & StandardConfigEditor {})
108
+ editor := & StandardConfigEditor {}
109
+
110
+ err := config .SaveWithLock ("existing-server" , expectedURL , editor )
89
111
if err != nil {
90
112
t .Fatalf ("Failed to update MCP server config: %v" , err )
91
113
}
92
114
93
115
// Get the updated servers
94
- mcpServers := getMCPServers (t , configPath )
116
+ mcpServers := getMCPServers (t , configPath , editor )
95
117
96
118
// Check existing server
97
119
existingServer , ok := mcpServers ["existing-server" ].(map [string ]interface {})
@@ -112,13 +134,14 @@ func testAddNewServer(t *testing.T, config ConfigFile, configPath string) {
112
134
t .Helper ()
113
135
// Test adding a new server with lock
114
136
expectedURL := "http://localhost:9876" + ssecommon .HTTPSSEEndpoint + "#new-container"
115
- err := config .SaveWithLock ("new-server" , expectedURL , & StandardConfigEditor {})
137
+ editor := & StandardConfigEditor {}
138
+ err := config .SaveWithLock ("new-server" , expectedURL , editor )
116
139
if err != nil {
117
140
t .Fatalf ("Failed to add new MCP server config: %v" , err )
118
141
}
119
142
120
143
// Get the updated servers
121
- mcpServers := getMCPServers (t , configPath )
144
+ mcpServers := getMCPServers (t , configPath , editor )
122
145
123
146
// Check new server
124
147
newServer , ok := mcpServers ["new-server" ].(map [string ]interface {})
@@ -134,11 +157,53 @@ func testAddNewServer(t *testing.T, config ConfigFile, configPath string) {
134
157
}
135
158
}
136
159
160
+ // testAddNewServer tests adding a new server
161
+ func testRemovingServer (t * testing.T , config ConfigFile , configPath string , editor ConfigEditor ) {
162
+ t .Helper ()
163
+ // Test adding a new server with lock
164
+ expectedURL := "http://localhost:9876" + ssecommon .HTTPSSEEndpoint + "#new-container"
165
+ err := config .SaveWithLock ("new-server" , expectedURL , editor )
166
+ if err != nil {
167
+ t .Fatalf ("Failed to add new MCP server config: %v" , err )
168
+ }
169
+
170
+ // Get the updated servers
171
+ mcpServers := getMCPServers (t , configPath , editor )
172
+
173
+ // Check new server
174
+ newServer , ok := mcpServers ["new-server" ].(map [string ]interface {})
175
+ if ! ok {
176
+ t .Fatalf ("new-server is not a map" )
177
+ }
178
+ newURL , ok := newServer ["url" ].(string )
179
+ if ! ok {
180
+ t .Fatalf ("url is not a string" )
181
+ }
182
+ if newURL != expectedURL {
183
+ t .Fatalf ("Unexpected URL for new-server: %s, expected: %s" , newURL , expectedURL )
184
+ }
185
+
186
+ // Remove the server
187
+ err = config .DeleteConfigWithLock ("new-server" , editor )
188
+ if err != nil {
189
+ t .Fatalf ("Failed to remove MCP server config: %v" , err )
190
+ }
191
+
192
+ mcpServersNew := getMCPServers (t , configPath , editor )
193
+
194
+ // Check that the server was removed
195
+ _ , ok = mcpServersNew ["new-server" ]
196
+ if ok {
197
+ t .Fatalf ("new-server is still in the config" )
198
+ }
199
+ }
200
+
137
201
// testPreserveExistingConfig tests that existing configurations are preserved
138
202
func testPreserveExistingConfig (t * testing.T , configPath string ) {
139
203
t .Helper ()
140
204
// Get the updated servers
141
- mcpServers := getMCPServers (t , configPath )
205
+ editor := & StandardConfigEditor {}
206
+ mcpServers := getMCPServers (t , configPath , editor )
142
207
143
208
// Check postgres server (should be unchanged)
144
209
postgresServer , ok := mcpServers ["postgres" ].(map [string ]interface {})
@@ -213,6 +278,41 @@ func TestUpdateMCPServerConfig(t *testing.T) {
213
278
214
279
testPreserveExistingConfig (t , configPath )
215
280
})
281
+
282
+ }
283
+
284
+ func TestRemoveMCPServerConfig (t * testing.T ) {
285
+ t .Parallel ()
286
+
287
+ logger .Initialize ()
288
+
289
+ t .Run ("RemoveExistingServerStandardEditor" , func (t * testing.T ) {
290
+ t .Parallel ()
291
+
292
+ // Setup test environment for this subtest
293
+ tempDir , configPath , config := setupTestConfig (t , "remove" )
294
+ t .Cleanup (func () {
295
+ if err := os .RemoveAll (tempDir ); err != nil {
296
+ t .Logf ("Failed to remove temp dir: %v" , err )
297
+ }
298
+ })
299
+
300
+ testRemovingServer (t , config , configPath , & StandardConfigEditor {})
301
+ })
302
+
303
+ t .Run ("RemoveExistingServerVSCodeEditor" , func (t * testing.T ) {
304
+ t .Parallel ()
305
+
306
+ // Setup test environment for this subtest
307
+ tempDir , configPath , config := setupTestConfig (t , "remove" )
308
+ t .Cleanup (func () {
309
+ if err := os .RemoveAll (tempDir ); err != nil {
310
+ t .Logf ("Failed to remove temp dir: %v" , err )
311
+ }
312
+ })
313
+
314
+ testRemovingServer (t , config , configPath , & VSCodeConfigEditor {})
315
+ })
216
316
}
217
317
218
318
func TestGenerateMCPServerURL (t * testing.T ) {
0 commit comments