-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathstatus_update_test.go
More file actions
355 lines (328 loc) · 10.9 KB
/
Copy pathstatus_update_test.go
File metadata and controls
355 lines (328 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"encoding/json"
"strings"
"testing"
)
func TestStatusVersionSuffix(t *testing.T) {
tests := []struct {
name string
update *StatusUpdateInfo
expected string
}{
{
name: "nil update shows nothing",
update: nil,
expected: "",
},
{
name: "update available with release URL",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.46.0",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0",
},
expected: " (update available: v0.46.0 — https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0)",
},
{
name: "update available without release URL",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.46.0",
},
expected: " (update available: v0.46.0)",
},
{
name: "up to date after successful check",
update: &StatusUpdateInfo{
Available: false,
LatestVersion: "v0.45.0",
},
expected: " (latest)",
},
{
name: "check error stays quiet",
update: &StatusUpdateInfo{
Available: false,
CheckError: "Get \"https://api.github.com\": dial tcp: no route to host",
},
expected: "",
},
{
name: "check error with stale availability stays quiet",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.46.0",
CheckError: "rate limited",
},
expected: "",
},
{
name: "check not completed yet shows nothing",
update: &StatusUpdateInfo{},
expected: "",
},
{
// Spec 079 US2 (FR-009): channels with a safe one-line command
// surface it right on the Version line.
name: "update available with channel command",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.48.0",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0",
InstallChannel: "homebrew",
UpdateCommand: "brew upgrade mcpproxy",
},
expected: " (update available: v0.48.0 — https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0 — Run: brew upgrade mcpproxy)",
},
{
// No-command channel gets the channel-appropriate guidance line
// instead of a possibly-wrong command (FR-009).
name: "update available with guidance-only channel",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.48.0",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0",
InstallChannel: "dmg",
},
expected: " (update available: v0.48.0 — https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0 — Download the latest DMG from the releases page)",
},
{
// A prerelease target on a command channel: the daemon suppresses
// the command (package managers serve stable only), and the
// generic release-page guidance renders instead of nothing.
name: "prerelease-suppressed command channel falls back to guidance",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.48.0-rc.1",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0-rc.1",
InstallChannel: "homebrew",
},
expected: " (update available: v0.48.0-rc.1 — https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0-rc.1 — Download the latest release from the releases page)",
},
{
// Older daemons omit install_channel: render exactly as before.
name: "update available without channel info keeps legacy format",
update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.48.0",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0",
},
expected: " (update available: v0.48.0 — https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.48.0)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := statusVersionSuffix(tt.update)
if result != tt.expected {
t.Errorf("statusVersionSuffix() = %q, want %q", result, tt.expected)
}
})
}
}
func TestStatusTableShowsUpdateAvailability(t *testing.T) {
t.Run("update available", func(t *testing.T) {
info := &StatusInfo{
State: "Running",
Edition: "personal",
ListenAddr: "127.0.0.1:8080",
APIKey: "a1b2****a1b2",
WebUIURL: "http://127.0.0.1:8080/ui/?apikey=test",
Version: "v0.45.0",
Update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.46.0",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0",
},
}
output := captureStdout(t, func() { printStatusTable(info) })
if !strings.Contains(output, "v0.45.0 (update available: v0.46.0 — https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0)") {
t.Errorf("expected update availability on the Version line, output:\n%s", output)
}
})
t.Run("up to date", func(t *testing.T) {
info := &StatusInfo{
State: "Running",
Edition: "personal",
ListenAddr: "127.0.0.1:8080",
APIKey: "a1b2****a1b2",
WebUIURL: "http://127.0.0.1:8080/ui/?apikey=test",
Version: "v0.46.0",
Update: &StatusUpdateInfo{
Available: false,
LatestVersion: "v0.46.0",
},
}
output := captureStdout(t, func() { printStatusTable(info) })
if !strings.Contains(output, "v0.46.0 (latest)") {
t.Errorf("expected '(latest)' on the Version line, output:\n%s", output)
}
})
t.Run("check error shows plain version", func(t *testing.T) {
info := &StatusInfo{
State: "Running",
Edition: "personal",
ListenAddr: "127.0.0.1:8080",
APIKey: "a1b2****a1b2",
WebUIURL: "http://127.0.0.1:8080/ui/?apikey=test",
Version: "v0.45.0",
Update: &StatusUpdateInfo{
CheckError: "dial tcp: no route to host",
},
}
output := captureStdout(t, func() { printStatusTable(info) })
if !strings.Contains(output, "Version:") || !strings.Contains(output, "v0.45.0") {
t.Errorf("expected plain version line, output:\n%s", output)
}
if strings.Contains(output, "update available") || strings.Contains(output, "(latest)") {
t.Errorf("expected no update annotation on check error, output:\n%s", output)
}
if strings.Contains(output, "no route to host") {
t.Errorf("check error must not be surfaced in human output:\n%s", output)
}
})
}
func TestExtractStatusUpdate(t *testing.T) {
t.Run("full update object", func(t *testing.T) {
infoData := map[string]interface{}{
"version": "v0.45.0",
"update": map[string]interface{}{
"available": true,
"latest_version": "v0.46.0-rc.1",
"release_url": "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0-rc.1",
"checked_at": "2026-07-02T10:00:00Z",
"is_prerelease": true,
},
}
u := extractStatusUpdate(infoData)
if u == nil {
t.Fatal("expected non-nil update info")
}
if !u.Available {
t.Error("expected Available=true")
}
if u.LatestVersion != "v0.46.0-rc.1" {
t.Errorf("expected LatestVersion v0.46.0-rc.1, got %q", u.LatestVersion)
}
if u.ReleaseURL != "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0-rc.1" {
t.Errorf("unexpected ReleaseURL %q", u.ReleaseURL)
}
if u.CheckedAt != "2026-07-02T10:00:00Z" {
t.Errorf("expected CheckedAt to be preserved, got %q", u.CheckedAt)
}
if !u.IsPrerelease {
t.Error("expected IsPrerelease=true")
}
})
t.Run("install channel and update command parsed", func(t *testing.T) {
infoData := map[string]interface{}{
"update": map[string]interface{}{
"available": true,
"latest_version": "v0.48.0",
"install_channel": "homebrew",
"update_command": "brew upgrade mcpproxy",
},
}
u := extractStatusUpdate(infoData)
if u == nil {
t.Fatal("expected non-nil update info")
}
if u.InstallChannel != "homebrew" {
t.Errorf("expected InstallChannel homebrew, got %q", u.InstallChannel)
}
if u.UpdateCommand != "brew upgrade mcpproxy" {
t.Errorf("expected UpdateCommand, got %q", u.UpdateCommand)
}
})
t.Run("check error preserved for machine output", func(t *testing.T) {
infoData := map[string]interface{}{
"update": map[string]interface{}{
"available": false,
"check_error": "rate limited",
},
}
u := extractStatusUpdate(infoData)
if u == nil {
t.Fatal("expected non-nil update info")
}
if u.CheckError != "rate limited" {
t.Errorf("expected CheckError 'rate limited', got %q", u.CheckError)
}
})
t.Run("missing update object", func(t *testing.T) {
infoData := map[string]interface{}{"version": "v0.45.0"}
if u := extractStatusUpdate(infoData); u != nil {
t.Errorf("expected nil update info, got %+v", u)
}
})
}
func TestStatusJSONIncludesUpdate(t *testing.T) {
info := &StatusInfo{
State: "Running",
ListenAddr: "127.0.0.1:8080",
APIKey: "a1b2****a1b2",
WebUIURL: "http://127.0.0.1:8080/ui/?apikey=test",
Version: "v0.45.0",
Update: &StatusUpdateInfo{
Available: true,
LatestVersion: "v0.46.0",
ReleaseURL: "https://github.com/smart-mcp-proxy/mcpproxy-go/releases/tag/v0.46.0",
CheckedAt: "2026-07-02T10:00:00Z",
IsPrerelease: true,
},
}
output := captureStdout(t, func() {
if err := printStatusJSON(info); err != nil {
t.Errorf("printStatusJSON failed: %v", err)
}
})
var result StatusInfo
if err := json.Unmarshal([]byte(output), &result); err != nil {
t.Fatalf("invalid JSON: %v\nOutput: %s", err, output)
}
if result.Update == nil {
t.Fatal("expected 'update' field in JSON output")
}
if !result.Update.Available {
t.Error("expected update.available=true in JSON output")
}
if result.Update.LatestVersion != "v0.46.0" {
t.Errorf("expected update.latest_version v0.46.0, got %q", result.Update.LatestVersion)
}
// Field names in the wire format must match the /api/v1/info update
// object (snake_case contract, FR-021).
var raw map[string]interface{}
if err := json.Unmarshal([]byte(output), &raw); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
rawUpdate, ok := raw["update"].(map[string]interface{})
if !ok {
t.Fatal("expected raw JSON key 'update'")
}
if v, ok := rawUpdate["checked_at"].(string); !ok || v != "2026-07-02T10:00:00Z" {
t.Errorf("expected raw JSON key 'update.checked_at', got %v", rawUpdate["checked_at"])
}
if v, ok := rawUpdate["is_prerelease"].(bool); !ok || !v {
t.Errorf("expected raw JSON key 'update.is_prerelease'=true, got %v", rawUpdate["is_prerelease"])
}
}
func TestStatusJSONOmitsUpdateWhenAbsent(t *testing.T) {
info := &StatusInfo{
State: "Not running",
ListenAddr: "127.0.0.1:8080 (configured)",
APIKey: "a1b2****a1b2",
WebUIURL: "http://127.0.0.1:8080/ui/?apikey=test",
}
output := captureStdout(t, func() {
if err := printStatusJSON(info); err != nil {
t.Errorf("printStatusJSON failed: %v", err)
}
})
var raw map[string]interface{}
if err := json.Unmarshal([]byte(output), &raw); err != nil {
t.Fatalf("invalid JSON: %v", err)
}
if _, ok := raw["update"]; ok {
t.Error("expected 'update' to be omitted when no update info collected")
}
}