-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
555 lines (504 loc) 路 19.4 KB
/
Copy pathcli_test.go
File metadata and controls
555 lines (504 loc) 路 19.4 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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package main
import (
"bytes"
"encoding/json"
"strings"
"testing"
"github.com/tanem/mt5-pnl-cli/internal/secrets"
"github.com/tanem/mt5-pnl-cli/internal/snaptest"
)
// Two accounts, four deals (same data as the aggregate tests; week of
// 2026-01-05 plus one deal the following week).
const fixtureJSON = `{
"schema_version": "1.0",
"generated_at": "2026-06-13T00:00:00Z",
"accounts": [
{"login": 111, "label": "Trend EA", "currency": "USD", "balance": 1000.0,
"equity": 1010.5, "last_success_at": "2026-06-13T00:00:00Z", "last_error": null},
{"login": 222, "label": "Scalper EA", "currency": "USD", "balance": 500.0,
"equity": 500.0, "last_success_at": null, "last_error": "login failed"}
],
"closed_deals": [
{"account": 111, "time": 1767607200, "profit": 10.0, "swap": -0.5, "commission": -0.5, "fee": 0.0,
"ticket": 1, "order": 1, "position_id": 1, "time_msc": 0, "type": 0, "entry": 1, "reason": 0,
"magic": 0, "volume": 0.1, "price": 1.0, "symbol": "EURUSD", "comment": "", "external_id": ""},
{"account": 111, "time": 1767693600, "profit": -4.0, "swap": 0.0, "commission": 0.0, "fee": 0.0,
"ticket": 2, "order": 2, "position_id": 2, "time_msc": 0, "type": 1, "entry": 1, "reason": 0,
"magic": 0, "volume": 0.1, "price": 1.0, "symbol": "EURUSD", "comment": "", "external_id": ""},
{"account": 222, "time": 1767693600, "profit": 0.7, "swap": 0.0, "commission": -0.7, "fee": 0.0,
"ticket": 3, "order": 3, "position_id": 3, "time_msc": 0, "type": 0, "entry": 1, "reason": 0,
"magic": 0, "volume": 0.1, "price": 1.0, "symbol": "XAUUSD", "comment": "", "external_id": ""},
{"account": 111, "time": 1768212000, "profit": 5.0, "swap": 0.0, "commission": 0.0, "fee": 0.0,
"ticket": 4, "order": 4, "position_id": 4, "time_msc": 0, "type": 0, "entry": 1, "reason": 0,
"magic": 0, "volume": 0.1, "price": 1.0, "symbol": "EURUSD", "comment": "", "external_id": ""}
],
"open_positions": [],
"cash_flows": []
}`
func runCLI(t *testing.T, passphrase string, args ...string) (stdout, stderr string, code int) {
t.Helper()
var out, errBuf bytes.Buffer
code = run(args, &out, &errBuf, func() (string, error) { return passphrase, nil })
return out.String(), errBuf.String(), code
}
func fixture(t *testing.T) string {
t.Helper()
return snaptest.Write(t, fixtureJSON, "test-pass")
}
func TestPnLTableCommand(t *testing.T) {
path := fixture(t)
out, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d, stderr: %s", code, errOut)
}
for _, want := range []string{"Trend EA", "Scalper EA", "ALL", "2026-01-05", "2026-01-12", "Net P&L"} {
if !strings.Contains(out, want) {
t.Errorf("stdout missing %q:\n%s", want, out)
}
}
if errOut != "" {
t.Errorf("expected silent stderr with huge --stale-after, got %q", errOut)
}
}
func TestPnLJSONCommand(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31", "--format", "json")
if code != 0 {
t.Fatalf("exit %d", code)
}
var got struct {
Rows []map[string]any `json:"rows"`
Summary struct {
TotalPnL float64 `json:"total_pnl"`
TotalTrades int `json:"total_trades"`
} `json:"summary"`
}
if err := json.Unmarshal([]byte(out), &got); err != nil {
t.Fatalf("invalid JSON: %v\n%s", err, out)
}
if got.Summary.TotalPnL != 10.0 || got.Summary.TotalTrades != 4 {
t.Errorf("summary = %+v", got.Summary)
}
}
func TestPnLAccountsFilter(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--accounts", "scalper ea")
if code != 0 {
t.Fatalf("exit %d", code)
}
if strings.Contains(out, "Trend EA") {
t.Errorf("filter leaked other account:\n%s", out)
}
}
func TestPnLUnknownAccountLabel(t *testing.T) {
path := fixture(t)
_, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--accounts", "Nope")
if code != 1 || !strings.Contains(errOut, "Trend EA") {
t.Errorf("exit %d, stderr %q; want 1 + valid labels listed", code, errOut)
}
}
func TestPnLStalenessWarning(t *testing.T) {
path := fixture(t)
// --stale-after 1ns makes any snapshot stale, so the test never depends
// on the wall clock's distance from the fixture's generated_at.
_, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--stale-after", "1ns")
if code != 0 {
t.Fatalf("exit %d", code)
}
if !strings.Contains(errOut, "mt5-pnl-exporter export") {
t.Errorf("want staleness warning on stderr, got %q", errOut)
}
}
func TestPnLInvalidBy(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "pnl", "--by", "fortnight")
if code != 1 || !strings.Contains(errOut, "--by") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestPnLInvalidColor(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "pnl", "--color", "bogus")
if code != 1 || !strings.Contains(errOut, "--color") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestPnLWrongPassphrase(t *testing.T) {
path := fixture(t)
_, errOut, code := runCLI(t, "wrong", "pnl", "--snapshot", path)
if code != 1 || !strings.Contains(errOut, "wrong passphrase") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestPnLPassphraseMissing(t *testing.T) {
path := fixture(t)
var out, errBuf bytes.Buffer
code := run([]string{"pnl", "--snapshot", path}, &out, &errBuf,
func() (string, error) { return "", secrets.ErrNotFound })
if code != 1 || !strings.Contains(errBuf.String(), "set-passphrase") {
t.Errorf("exit %d, stderr %q", code, errBuf.String())
}
}
func TestPnLNoSnapshotPath(t *testing.T) {
t.Setenv("MT5_PNL_SNAPSHOT", "")
_, errOut, code := runCLI(t, "test-pass", "pnl")
if code != 1 || !strings.Contains(errOut, "MT5_PNL_SNAPSHOT") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestPnLEnvFallback(t *testing.T) {
path := fixture(t)
t.Setenv("MT5_PNL_SNAPSHOT", path)
_, errOut, code := runCLI(t, "test-pass",
"pnl", "--from", "2026-01-01", "--to", "2026-01-31", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d, stderr %q", code, errOut)
}
}
func TestUnsupportedSchema(t *testing.T) {
body := strings.Replace(fixtureJSON, `"schema_version": "1.0"`, `"schema_version": "2.0"`, 1)
path := snaptest.Write(t, body, "test-pass")
_, errOut, code := runCLI(t, "test-pass", "pnl", "--snapshot", path)
if code != 1 || !strings.Contains(errOut, "unsupported snapshot schema") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestAccountsCommand(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass",
"accounts", "--snapshot", path, "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d", code)
}
for _, want := range []string{"111", "Trend EA", "login failed", "Snapshot generated: 2026-06-13T00:00:00Z"} {
if !strings.Contains(out, want) {
t.Errorf("stdout missing %q:\n%s", want, out)
}
}
}
func TestAccountsMissingSnapshotFile(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "accounts", "--snapshot", "/nonexistent/snap.age")
if code != 1 || errOut == "" {
t.Errorf("exit %d, stderr %q; want 1 + error", code, errOut)
}
}
func TestVersionCommand(t *testing.T) {
out, _, code := runCLI(t, "", "version")
if code != 0 || !strings.Contains(out, "mt5-pnl-cli") || !strings.Contains(out, "schema 1.0") {
t.Errorf("exit %d, out %q", code, out)
}
}
func TestVersionFlag(t *testing.T) {
out, _, code := runCLI(t, "", "--version")
if code != 0 || !strings.Contains(out, "mt5-pnl-cli") || !strings.Contains(out, "schema 1.0") {
t.Errorf("exit %d, out %q", code, out)
}
}
func TestUnknownCommand(t *testing.T) {
_, errOut, code := runCLI(t, "", "bogus")
if code != 1 || !strings.Contains(errOut, "Usage") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestNoCommand(t *testing.T) {
_, errOut, code := runCLI(t, "")
if code != 1 || !strings.Contains(errOut, "Usage") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestPnLHelpToStdout(t *testing.T) {
out, errOut, code := runCLI(t, "", "pnl", "-h")
if code != 0 {
t.Fatalf("exit %d", code)
}
if !strings.Contains(out, "Usage: mt5-pnl-cli pnl") {
t.Errorf("help should be on stdout, got %q", out)
}
if errOut != "" {
t.Errorf("help should not write stderr, got %q", errOut)
}
}
func TestPnLParseErrorToStderr(t *testing.T) {
out, errOut, code := runCLI(t, "", "pnl", "--nope")
if code != 1 {
t.Fatalf("exit %d", code)
}
if out != "" {
t.Errorf("parse error should not write stdout, got %q", out)
}
if !strings.Contains(errOut, "not defined") {
t.Errorf("want flag error on stderr, got %q", errOut)
}
}
func TestAccountsHelpToStdout(t *testing.T) {
out, _, code := runCLI(t, "", "accounts", "--help")
if code != 0 || !strings.Contains(out, "Usage: mt5-pnl-cli accounts") {
t.Errorf("exit %d, out %q", code, out)
}
}
func TestTopLevelHelpMentionsFormat(t *testing.T) {
out, _, code := runCLI(t, "", "help")
if code != 0 {
t.Fatalf("exit %d", code)
}
if !strings.Contains(out, "--format") {
t.Errorf("top-level help should mention --format:\n%s", out)
}
}
const mixedFixtureJSON = `{
"schema_version": "1.0",
"generated_at": "2026-06-13T00:00:00Z",
"accounts": [
{"login": 111, "label": "USD Acct", "currency": "USD", "balance": 1000.0,
"equity": 1000.0, "last_success_at": "2026-06-13T00:00:00Z", "last_error": null},
{"login": 333, "label": "EUR Acct", "currency": "EUR", "balance": 800.0,
"equity": 800.0, "last_success_at": "2026-06-13T00:00:00Z", "last_error": null}
],
"closed_deals": [
{"account": 111, "time": 1767607200, "profit": 10.0, "swap": 0.0, "commission": 0.0, "fee": 0.0,
"ticket": 1, "order": 1, "position_id": 1, "time_msc": 0, "type": 0, "entry": 1, "reason": 0,
"magic": 0, "volume": 0.1, "price": 1.0, "symbol": "EURUSD", "comment": "", "external_id": ""},
{"account": 333, "time": 1767607200, "profit": 7.0, "swap": 0.0, "commission": 0.0, "fee": 0.0,
"ticket": 2, "order": 2, "position_id": 2, "time_msc": 0, "type": 0, "entry": 1, "reason": 0,
"magic": 0, "volume": 0.1, "price": 1.0, "symbol": "EURUSD", "comment": "", "external_id": ""}
],
"open_positions": [],
"cash_flows": []
}`
func mixedFixture(t *testing.T) string {
t.Helper()
return snaptest.Write(t, mixedFixtureJSON, "test-pass")
}
func TestPnLBySymbol(t *testing.T) {
path := fixture(t)
out, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "symbol", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d, stderr: %s", code, errOut)
}
if !strings.Contains(out, "SYMBOL") || !strings.Contains(out, "EURUSD") {
t.Errorf("by-symbol output should list symbols:\n%s", out)
}
if strings.Contains(out, "ACCOUNT") {
t.Errorf("by-symbol output should not have an ACCOUNT column:\n%s", out)
}
}
func TestPnLByMagicJSONGroupBy(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "magic", "--format", "json", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d", code)
}
if !strings.Contains(out, `"group_by": "magic"`) {
t.Errorf("by-magic JSON should carry group_by magic:\n%s", out)
}
}
func TestPnLBySymbolMixedCurrencyRefuses(t *testing.T) {
path := mixedFixture(t)
_, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "symbol", "--stale-after", "876000h")
if code != 1 {
t.Fatalf("exit %d, want 1; stderr: %s", code, errOut)
}
if !strings.Contains(errOut, "narrow --accounts") {
t.Errorf("refusal should guide narrowing accounts:\n%s", errOut)
}
}
func TestPnLFormatCSV(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "month", "--format", "csv", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d", code)
}
if !strings.HasPrefix(out, "group,group_by,account_login,account_label,pnl,") {
t.Errorf("want CSV header first, got:\n%s", out)
}
}
func TestPnLInvalidFormat(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "pnl", "--format", "yaml")
if code != 1 || !strings.Contains(errOut, "invalid --format") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestAccountsFormatCSV(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass", "accounts", "--snapshot", path,
"--format", "csv", "--stale-after", "876000h")
if code != 0 || !strings.HasPrefix(out, "login,label,currency,") {
t.Errorf("exit %d, want accounts CSV header, got:\n%s", code, out)
}
}
func TestPnLMixedCurrencyTableSuppresses(t *testing.T) {
path := snaptest.Write(t, mixedFixtureJSON, "test-pass")
out, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "month", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d, stderr %s", code, errOut)
}
if !strings.Contains(errOut, "multiple currencies") {
t.Errorf("want mixed-currency warning on stderr, got %q", errOut)
}
if !strings.Contains(out, "n/a") {
t.Errorf("want suppressed combined total (n/a) in table, got:\n%s", out)
}
}
func TestPnLMixedCurrencyJSONNull(t *testing.T) {
path := snaptest.Write(t, mixedFixtureJSON, "test-pass")
out, _, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "month", "--format", "json", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d", code)
}
if !strings.Contains(out, `"total_pnl": null`) {
t.Errorf("want null total under mixed currency, got:\n%s", out)
}
}
func TestPnLMixedCurrencyCSVOmitsCombined(t *testing.T) {
path := snaptest.Write(t, mixedFixtureJSON, "test-pass")
out, errOut, code := runCLI(t, "test-pass",
"pnl", "--snapshot", path, "--from", "2026-01-01", "--to", "2026-01-31",
"--by", "month", "--format", "csv", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d, stderr %s", code, errOut)
}
if strings.Contains(out, "ALL") {
t.Errorf("mixed CSV should omit the combined ALL row:\n%s", out)
}
if !strings.Contains(out, "USD Acct") || !strings.Contains(out, "EUR Acct") {
t.Errorf("mixed CSV should keep per-account rows:\n%s", out)
}
if !strings.Contains(errOut, "multiple currencies") {
t.Errorf("want mixed-currency warning on stderr, got %q", errOut)
}
}
func TestAccountsInvalidFormat(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "accounts", "--format", "yaml")
if code != 1 || !strings.Contains(errOut, "invalid --format") {
t.Errorf("exit %d, stderr %q", code, errOut)
}
}
func TestAccountsJSONCommand(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass",
"accounts", "--snapshot", path, "--format", "json", "--stale-after", "876000h")
if code != 0 {
t.Fatalf("exit %d", code)
}
var accts []map[string]any
if err := json.Unmarshal([]byte(out), &accts); err != nil {
t.Fatalf("invalid JSON: %v\n%s", err, out)
}
if len(accts) != 2 {
t.Fatalf("got %d accounts, want 2:\n%s", len(accts), out)
}
if !strings.Contains(out, "Trend EA") {
t.Errorf("JSON missing account label:\n%s", out)
}
}
func TestPnLFlagParseError(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "pnl", "--nope")
if code != 1 || errOut == "" {
t.Errorf("exit %d, stderr %q; want 1 + flag error", code, errOut)
}
}
func TestAccountsFlagParseError(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "accounts", "--nope")
if code != 1 || errOut == "" {
t.Errorf("exit %d, stderr %q; want 1 + flag error", code, errOut)
}
}
func TestPnLInvalidRange(t *testing.T) {
_, errOut, code := runCLI(t, "test-pass", "pnl", "--from", "not-a-date")
if code != 1 || !strings.Contains(errOut, "--from") {
t.Errorf("exit %d, stderr %q; want 1 + --from guidance", code, errOut)
}
}
func TestSetPassphraseRequiresTerminal(t *testing.T) {
// CI and other non-interactive runs provide a non-TTY stdin, so the
// interactive guard fires before any keychain access (which CI cannot
// reach anyway). cmdSetPassphrase reads os.Stdin directly, so stdin
// cannot be injected via runCLI; a rare interactive local run where
// stdin is a TTY would block this test on password input.
_, errOut, code := runCLI(t, "", "set-passphrase")
if code != 1 || !strings.Contains(errOut, "interactive terminal") {
t.Errorf("exit %d, stderr %q; want 1 + terminal guard", code, errOut)
}
}
func TestHelpCommand(t *testing.T) {
out, errOut, code := runCLI(t, "", "--help")
if code != 0 || !strings.Contains(out, "Usage") || !strings.Contains(out, "Examples:") || errOut != "" {
t.Errorf("exit %d, stdout %q, stderr %q; want 0 + usage + examples on stdout, empty stderr", code, out, errOut)
}
}
func TestPnLHelpShowsExamples(t *testing.T) {
out, _, code := runCLI(t, "", "pnl", "-h")
if code != 0 || !strings.Contains(out, "Examples:") {
t.Errorf("exit %d; pnl help should show Examples:\n%s", code, out)
}
if !strings.Contains(out, "mt5-pnl-cli pnl --from") {
t.Errorf("pnl help should show a worked example:\n%s", out)
}
}
func TestAccountsHelpShowsExamples(t *testing.T) {
out, _, code := runCLI(t, "", "accounts", "--help")
if code != 0 || !strings.Contains(out, "Examples:") {
t.Errorf("exit %d; accounts help should show Examples:\n%s", code, out)
}
if !strings.Contains(out, "mt5-pnl-cli accounts --format") {
t.Errorf("accounts help should show a worked example:\n%s", out)
}
}
func TestPnLQuietSuppressesWarnings(t *testing.T) {
path := fixture(t)
// 1ns threshold makes the snapshot stale regardless of the wall clock.
_, errOut, code := runCLI(t, "test-pass", "pnl", "--snapshot", path,
"--from", "2026-01-01", "--to", "2026-01-31", "--stale-after", "1ns")
if code != 0 || !strings.Contains(errOut, "warning") {
t.Fatalf("precondition: expected a staleness warning, exit %d stderr %q", code, errOut)
}
// With --quiet stderr is clean.
_, errOut, code = runCLI(t, "test-pass", "pnl", "--snapshot", path,
"--from", "2026-01-01", "--to", "2026-01-31", "--stale-after", "1ns", "--quiet")
if code != 0 || errOut != "" {
t.Errorf("--quiet should silence warnings; exit %d stderr %q", code, errOut)
}
}
func TestPnLQuietStillPrintsErrors(t *testing.T) {
// The snapshot-open error fires AFTER warnW is set to io.Discard, so a
// non-empty stderr here proves --quiet silences warnings, not errors
// (under --quiet, warnings are discarded, so stderr can only be the error).
_, errOut, code := runCLI(t, "test-pass", "pnl", "--snapshot", "/nonexistent/snap.age", "--quiet")
if code != 1 || errOut == "" {
t.Errorf("error must still print under --quiet; exit %d stderr %q", code, errOut)
}
}
func TestPnLColorAlwaysForcesAnsi(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass", "pnl", "--snapshot", path,
"--from", "2026-01-01", "--to", "2026-01-31", "--stale-after", "876000h", "--color=always")
if code != 0 || !strings.Contains(out, "\x1b[") {
t.Errorf("--color=always should emit ANSI; exit %d out %q", code, out)
}
}
func TestPnLDefaultNoColorToBuffer(t *testing.T) {
path := fixture(t)
out, _, code := runCLI(t, "test-pass", "pnl", "--snapshot", path,
"--from", "2026-01-01", "--to", "2026-01-31", "--stale-after", "876000h")
if code != 0 || strings.Contains(out, "\x1b[") {
t.Errorf("default (non-TTY buffer) should be uncoloured; exit %d out %q", code, out)
}
}