-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
504 lines (497 loc) · 18.9 KB
/
Copy pathmain_test.go
File metadata and controls
504 lines (497 loc) · 18.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestValidateEnvelope_OK(t *testing.T) {
raw, _ := os.ReadFile("testdata/envelope_ok.json")
if err := validateEnvelope(raw); err != nil {
t.Fatalf("want nil, got %v", err)
}
}
func TestValidateEnvelope_Malformed(t *testing.T) {
raw, _ := os.ReadFile("testdata/envelope_malformed.json")
if err := validateEnvelope(raw); err == nil {
t.Fatal("want error for malformed JSON")
}
}
func TestValidateEnvelope_MissingMemoryField(t *testing.T) {
raw, _ := os.ReadFile("testdata/envelope_missing_memory.json")
if err := validateEnvelope(raw); err == nil {
t.Fatal("want error for missing VisiblePhysicalBytes")
}
}
func TestHandleSnapshot_MethodNotAllowed(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/api/snapshot", nil)
rr := httptest.NewRecorder()
handleSnapshot(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("want 405 got %d", rr.Code)
}
}
func TestHandleSnapshot_ConcurrencyLimit429(t *testing.T) {
// fill snapshotSem
snapshotSem <- struct{}{}
snapshotSem <- struct{}{}
defer func() { <-snapshotSem; <-snapshotSem }()
req := httptest.NewRequest(http.MethodGet, "/api/snapshot", nil)
rr := httptest.NewRecorder()
handleSnapshot(rr, req)
if rr.Code != http.StatusTooManyRequests {
t.Fatalf("want 429 got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleWslShutdown_RequiresToken(t *testing.T) {
capabilityToken = "test-token-123"
req := httptest.NewRequest(http.MethodPost, "/api/wsl/shutdown", nil)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handleWslShutdown(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 without token, got %d", rr.Code)
}
}
func TestHandleWslShutdown_RequiresConfirm(t *testing.T) {
capabilityToken = "test-token-123"
req := httptest.NewRequest(http.MethodPost, "/api/wsl/shutdown", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
// empty body -> should 400
handleWslShutdown(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("want 400 without confirm, got %d", rr.Code)
}
}
func TestHandleWslShutdown_OriginRejected(t *testing.T) {
capabilityToken = "test-token-123"
req := httptest.NewRequest(http.MethodPost, "/api/wsl/shutdown", nil)
req.Header.Set("Origin", "https://evil.example")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleWslShutdown(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 for evil origin, got %d", rr.Code)
}
}
func TestHandleReclaimStandby_RequiresToken(t *testing.T) {
capabilityToken = "test-token-123"
req := httptest.NewRequest(http.MethodPost, "/api/reclaim/standby", nil)
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
handleReclaimStandby(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 without token, got %d", rr.Code)
}
}
func TestHandleReclaimStandby_OriginRejected(t *testing.T) {
capabilityToken = "test-token-123"
req := httptest.NewRequest(http.MethodPost, "/api/reclaim/standby", nil)
req.Header.Set("Origin", "https://evil.example")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleReclaimStandby(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 for evil origin, got %d", rr.Code)
}
}
func TestHandleReclaimStandby_RequiresConfirm(t *testing.T) {
capabilityToken = "test-token-123"
req := httptest.NewRequest(http.MethodPost, "/api/reclaim/standby", nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleReclaimStandby(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("want 400 without confirm, got %d", rr.Code)
}
}
func TestHandleReclaimStandby_MethodNotAllowed(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/reclaim/standby", nil)
rr := httptest.NewRecorder()
handleReclaimStandby(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("want 405 got %d", rr.Code)
}
}
func TestHandleReclaimStandby_RateLimit(t *testing.T) {
capabilityToken = "test-token-123"
reclaimSem <- struct{}{}
defer func() { <-reclaimSem }()
body := strings.NewReader(`{"confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/reclaim/standby", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleReclaimStandby(rr, req)
if rr.Code != http.StatusTooManyRequests {
t.Fatalf("want 429 got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleWslConfig_RequiresToken(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"memory":"4GB","confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/wsl/config", body)
req.Header.Set("Content-Type", "application/json")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleWslConfig(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 without token, got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleWslConfig_OriginRejected(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"memory":"4GB","confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/wsl/config", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Header.Set("Origin", "https://evil.example")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleWslConfig(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 for evil origin, got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleWslConfig_RequiresConfirm(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"memory":"4GB"}`)
req := httptest.NewRequest(http.MethodPost, "/api/wsl/config", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleWslConfig(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("want 400 without confirm, got %d body %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "Confirmation required") {
t.Fatalf("want Confirmation required, got %s", rr.Body.String())
}
}
func TestHandleWslConfig_InvalidMemory(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"memory":"not-a-size","confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/wsl/config", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleWslConfig(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("want 400 for invalid memory, got %d body %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "Invalid memory value") {
t.Fatalf("want Invalid memory value, got %s", rr.Body.String())
}
}
func TestHandleWslConfig_RateLimit(t *testing.T) {
capabilityToken = "test-token-123"
wslConfigSem <- struct{}{}
defer func() { <-wslConfigSem }()
body := strings.NewReader(`{"memory":"4GB","confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/wsl/config", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleWslConfig(rr, req)
if rr.Code != http.StatusTooManyRequests {
t.Fatalf("want 429 got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleWslConfig_MethodNotAllowed(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/wsl/config", nil)
rr := httptest.NewRecorder()
handleWslConfig(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("want 405 got %d", rr.Code)
}
}
func TestHandleRuntimeRestart_RequiresToken(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"host":"Teams","pid":12345,"confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/runtime/restart", body)
req.Header.Set("Content-Type", "application/json")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleRuntimeRestart(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 without token, got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleRuntimeRestart_OriginRejected(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"host":"Teams","pid":12345,"confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/runtime/restart", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Header.Set("Origin", "https://evil.example")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleRuntimeRestart(rr, req)
if rr.Code != http.StatusForbidden {
t.Fatalf("want 403 for evil origin, got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleRuntimeRestart_RequiresConfirm(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"host":"Teams","pid":12345}`)
req := httptest.NewRequest(http.MethodPost, "/api/runtime/restart", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleRuntimeRestart(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("want 400 without confirm, got %d body %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "Confirmation required") {
t.Fatalf("want Confirmation required, got %s", rr.Body.String())
}
}
func TestHandleRuntimeRestart_MethodNotAllowed(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/api/runtime/restart", nil)
rr := httptest.NewRecorder()
handleRuntimeRestart(rr, req)
if rr.Code != http.StatusMethodNotAllowed {
t.Fatalf("want 405 got %d", rr.Code)
}
}
func TestHandleRuntimeRestart_RateLimit(t *testing.T) {
capabilityToken = "test-token-123"
runtimeRestartSem <- struct{}{}
defer func() { <-runtimeRestartSem }()
body := strings.NewReader(`{"host":"Teams","pid":12345,"confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/runtime/restart", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleRuntimeRestart(rr, req)
if rr.Code != http.StatusTooManyRequests {
t.Fatalf("want 429 got %d body %s", rr.Code, rr.Body.String())
}
}
func TestHandleRuntimeRestart_InvalidPid(t *testing.T) {
capabilityToken = "test-token-123"
body := strings.NewReader(`{"host":"Teams","pid":0,"confirm":true}`)
req := httptest.NewRequest(http.MethodPost, "/api/runtime/restart", body)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-SysView-Token", "test-token-123")
req.Host = "localhost:22880"
rr := httptest.NewRecorder()
handleRuntimeRestart(rr, req)
if rr.Code != http.StatusBadRequest {
t.Fatalf("want 400 for invalid pid, got %d body %s", rr.Code, rr.Body.String())
}
if !strings.Contains(rr.Body.String(), "Host and PID required") {
t.Fatalf("want Host and PID required, got %s", rr.Body.String())
}
body2 := strings.NewReader(`{"host":"Teams","pid":2,"confirm":true}`)
req2 := httptest.NewRequest(http.MethodPost, "/api/runtime/restart", body2)
req2.Header.Set("Content-Type", "application/json")
req2.Header.Set("X-SysView-Token", "test-token-123")
req2.Host = "localhost:22880"
rr2 := httptest.NewRecorder()
handleRuntimeRestart(rr2, req2)
if rr2.Code != http.StatusBadRequest {
t.Fatalf("want 400 for pid 2, got %d body %s", rr2.Code, rr2.Body.String())
}
}
func abs(x int64) int64 {
if x < 0 {
return -x
}
return x
}
func TestMemoryInvariants_Tolerance(t *testing.T) {
// visible 61.58GB, InUse 54.69, Standby 6.81, Free 0, Modified 0 -> sum ~61.5 vs 61.58 delta <10MB should pass
raw, _ := os.ReadFile("testdata/envelope_ok.json")
var env map[string]json.RawMessage
json.Unmarshal(raw, &env)
var data struct {
Memory struct {
VisiblePhysicalBytes int64 `json:"VisiblePhysicalBytes"`
InUseBytes int64 `json:"InUseBytes"`
StandbyBytes int64 `json:"StandbyBytes"`
ModifiedBytes int64 `json:"ModifiedBytes"`
FreeBytes int64 `json:"FreeBytes"`
HardwareReservedBytes int64 `json:"HardwareReservedBytes"`
TotalPhysicalBytes int64 `json:"TotalPhysicalBytes"`
} `json:"Memory"`
}
var d map[string]json.RawMessage
json.Unmarshal(env["data"], &d)
json.Unmarshal(d["Memory"], &data.Memory)
sum := data.Memory.InUseBytes + data.Memory.StandbyBytes + data.Memory.ModifiedBytes + data.Memory.FreeBytes
if abs(sum-data.Memory.VisiblePhysicalBytes) > 10*1024*1024 {
t.Fatalf("invariant should hold: sum %d vs visible %d", sum, data.Memory.VisiblePhysicalBytes)
}
}
func TestMemoryUnavailable_RendersUnavailable(t *testing.T) {
raw, _ := os.ReadFile("testdata/envelope_provider_unavailable.json")
if err := validateEnvelope(raw); err != nil {
// provider unavailable still validates shape but frontend must show Unavailable — checked in JS test
}
// Go side: handler should have returned 200 with providers.memory=unavailable — assert fixture has that field
var env map[string]any
json.Unmarshal(raw, &env)
prov := env["providers"].(map[string]any)
if prov["memory"] != "unavailable" {
t.Fatal("fixture must have memory=unavailable")
}
}
func TestHeadlessHelp(t *testing.T) {
cmd := exec.Command("go", "run", ".", "--help")
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("go run --help failed: %v output: %s", err, string(out))
}
s := string(out)
for _, want := range []string{"headless", "once", "output", "pretty", "redact"} {
if !strings.Contains(s, want) {
t.Fatalf("--help missing %q in output: %s", want, s)
}
}
// also ensure help mentions headless mode description
if !strings.Contains(s, "headless mode") && !strings.Contains(s, "headless") {
t.Fatalf("--help should mention headless mode, got: %s", s)
}
}
func TestHeadlessWritesFile(t *testing.T) {
dir := t.TempDir()
outFile := filepath.Join(dir, "snapshot.json")
// Build an envelope containing secrets that must be redacted
raw := `{"capturedAt":"2026-09-01T00:00:00Z","schemaVersion":3,"providers":{"memory":"ok"},"errors":[],"data":{"Memory":{"VisiblePhysicalBytes":100,"AvailableBytes":50,"InUseBytes":50},"AllProcesses":[{"PID":1,"Name":"foo","CommandLine":"secret --token abc"}],"WebViewProcesses":[{"PID":2,"Name":"edge","CommandLine":"secret2"}],"Startup":[{"Name":"app","Command":"C:\\secret\\run.exe --key 123"}],"Network":{"TcpConnections":[]}}}`
if err := validateEnvelope([]byte(raw)); err != nil {
t.Fatalf("validateEnvelope: %v", err)
}
redacted, err := applyRedaction([]byte(raw))
if err != nil {
t.Fatalf("applyRedaction: %v", err)
}
if !json.Valid(redacted) {
t.Fatal("redacted output is not valid JSON")
}
// Verify redaction
var env map[string]any
if err := json.Unmarshal(redacted, &env); err != nil {
t.Fatalf("unmarshal redacted: %v", err)
}
data := env["data"].(map[string]any)
if arr, ok := data["AllProcesses"].([]any); ok && len(arr) > 0 {
if m, ok := arr[0].(map[string]any); ok {
if m["CommandLine"] != "[redacted]" {
t.Fatalf("AllProcesses CommandLine not redacted: %v", m["CommandLine"])
}
}
} else {
t.Fatal("AllProcesses missing")
}
if arr, ok := data["WebViewProcesses"].([]any); ok && len(arr) > 0 {
if m, ok := arr[0].(map[string]any); ok {
if m["CommandLine"] != "[redacted]" {
t.Fatalf("WebViewProcesses CommandLine not redacted: %v", m["CommandLine"])
}
}
}
if arr, ok := data["Startup"].([]any); ok && len(arr) > 0 {
if m, ok := arr[0].(map[string]any); ok {
if m["Command"] != "[redacted]" {
t.Fatalf("Startup Command not redacted: %v", m["Command"])
}
}
}
// Verify schemaVersion preserved
var top map[string]json.RawMessage
json.Unmarshal(redacted, &top)
var sv struct {
SchemaVersion int `json:"schemaVersion"`
}
json.Unmarshal(redacted, &sv)
if sv.SchemaVersion != 3 {
t.Fatalf("schemaVersion want 3 got %d", sv.SchemaVersion)
}
// Mimic runHeadless file write path (without os.Exit)
if err := os.WriteFile(outFile, redacted, 0644); err != nil {
t.Fatalf("WriteFile: %v", err)
}
got, err := os.ReadFile(outFile)
if err != nil {
t.Fatalf("ReadFile: %v", err)
}
if !json.Valid(got) {
t.Fatal("written file is not valid JSON")
}
if err := validateEnvelope(got); err != nil {
t.Fatalf("validateEnvelope on written file: %v", err)
}
// Pretty variant
var v any
json.Unmarshal(redacted, &v)
pretty, _ := json.MarshalIndent(v, "", " ")
if !strings.Contains(string(pretty), "\n") {
t.Fatal("pretty JSON should contain newlines")
}
prettyFile := filepath.Join(dir, "pretty.json")
if err := os.WriteFile(prettyFile, pretty, 0644); err != nil {
t.Fatalf("WriteFile pretty: %v", err)
}
if !json.Valid(pretty) {
t.Fatal("pretty file not valid JSON")
}
}
func TestHeadlessRedactToggleAndExitCodes(t *testing.T) {
// Redact disabled should preserve secrets
raw := `{"capturedAt":"2026-09-01T00:00:00Z","schemaVersion":3,"providers":{"memory":"ok"},"errors":[],"data":{"Memory":{"VisiblePhysicalBytes":100,"AvailableBytes":50,"InUseBytes":50},"AllProcesses":[{"PID":1,"Name":"foo","CommandLine":"keep me"}],"Startup":[{"Name":"app","Command":"keep this"}]}}`
// redact=false means raw unchanged (runHeadless would skip applyRedaction)
var env map[string]any
json.Unmarshal([]byte(raw), &env)
data := env["data"].(map[string]any)
if arr, ok := data["AllProcesses"].([]any); ok {
if m := arr[0].(map[string]any); m["CommandLine"] != "keep me" {
t.Fatalf("redact=false should preserve CommandLine")
}
}
// redact=true path must redact
redacted, err := applyRedaction([]byte(raw))
if err != nil {
t.Fatalf("applyRedaction: %v", err)
}
json.Unmarshal(redacted, &env)
data = env["data"].(map[string]any)
if arr, ok := data["AllProcesses"].([]any); ok {
if m := arr[0].(map[string]any); m["CommandLine"] != "[redacted]" {
t.Fatalf("redact=true should redact, got %v", m["CommandLine"])
}
}
// Exit code semantics: validateEnvelope valid -> exit 0, invalid -> non-zero
if err := validateEnvelope([]byte(raw)); err != nil {
t.Fatalf("valid envelope should not error: %v", err)
}
if err := validateEnvelope([]byte(`{bad`)); err == nil {
t.Fatal("malformed JSON should error (would be exit 1)")
}
if err := validateEnvelope([]byte(`{"capturedAt":"x","schemaVersion":3,"providers":{},"errors":[],"data":{"Memory":{}}}`)); err == nil {
t.Fatal("missing Memory fields should error (would be exit 1)")
}
// Help exit code already checked in TestHeadlessHelp (0)
// Unknown flag should be non-zero exit
cmd := exec.Command("go", "run", ".", "--unknown-flag-xyz")
if err := cmd.Run(); err == nil {
t.Fatal("unknown flag should exit non-zero")
}
}