-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_cat_test.go
More file actions
714 lines (615 loc) · 24.3 KB
/
Copy pathcmd_cat_test.go
File metadata and controls
714 lines (615 loc) · 24.3 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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package cli_test
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
testutils "github.com/jlrickert/cli-toolkit/sandbox"
"github.com/stretchr/testify/require"
)
type catStatsJSON struct {
Accessed string `json:"accessed"`
AccessCount int `json:"access_count"`
}
type catTestCase struct {
name string
args []string
setupFixture *string
cwd *string
expectedInStdout []string
expectedErr string
description string
}
func TestCatCommand_TableDrivenErrorHandling(t *testing.T) {
tests := []catTestCase{
{
name: "cat_invalid_node_id",
args: []string{"cat", "invalid"},
setupFixture: strPtr("joe"),
expectedErr: "invalid node ID",
description: "Error when node ID cannot be parsed",
},
{
name: "cat_missing_node_id",
args: []string{"cat"},
expectedErr: "at least 1 arg",
description: "Error when node ID is not provided",
},
{
name: "cat_nonexistent_alias",
args: []string{"cat", "0", "--keg", "nonexistent"},
setupFixture: strPtr("joe"),
expectedErr: "keg alias not found",
description: "Error when keg alias does not exist",
},
{
name: "cat_nonexistent_node",
args: []string{"cat", "12341234"},
setupFixture: strPtr("joe"),
expectedErr: "node 12341234 not found",
description: "Error when node does not exist",
},
{
name: "cat_conflicting_output_flags",
args: []string{"cat", "0", "--meta-only", "--stats-only"},
setupFixture: strPtr("joe"),
expectedErr: "only one output mode may be selected",
description: "Error when multiple output modes are selected",
},
{
name: "cat_conflicting_edit_and_output_flag",
args: []string{"cat", "0", "--edit", "--stats-only"},
setupFixture: strPtr("joe"),
expectedErr: "only one output mode may be selected",
description: "Error when edit mode conflicts with output flags",
},
}
for _, tt := range tests {
t.Run(tt.name, func(innerT *testing.T) {
innerT.Parallel()
var opts []testutils.Option
if tt.setupFixture != nil {
opts = append(opts, testutils.WithFixture(*tt.setupFixture, "~"))
}
sb := NewSandbox(innerT, opts...)
if tt.cwd != nil {
sb.Setwd(*tt.cwd)
}
h := NewProcess(innerT, false, tt.args...)
res := h.Run(sb.Context(), sb.Runtime())
if tt.expectedErr != "" {
require.Error(innerT, res.Err, "expected error - %s", tt.description)
stderr := string(res.Stderr)
require.Contains(innerT, stderr, tt.expectedErr,
"error message should contain %q, got stderr: %s and err: %v", tt.expectedErr, stderr, res.Err)
} else {
require.NoError(innerT, res.Err, "cat command should succeed - %s", tt.description)
stdout := string(res.Stdout)
for _, expected := range tt.expectedInStdout {
require.Contains(innerT, stdout, expected,
"expected output to contain %q, got:\n%s", expected, stdout)
}
// Verify frontmatter structure: starts with ---, has ---, then content
lines := strings.Split(stdout, "\n")
require.Greater(innerT, len(lines), 2, "output should have multiple lines")
require.Equal(innerT, "---", lines[0], "output should start with frontmatter delimiter")
// Find the closing delimiter
closingFound := false
for i := 1; i < len(lines); i++ {
if lines[i] == "---" {
closingFound = true
break
}
}
require.True(innerT, closingFound, "output should have closing frontmatter delimiter")
}
})
}
}
func TestCatCommand_WithJoeFixture(t *testing.T) {
tests := []catTestCase{
{
name: "cat_personal_keg_from_default_location",
args: []string{
"cat",
"0",
},
setupFixture: strPtr("joe"),
expectedInStdout: []string{
"---",
"# Sorry, planned but not yet available",
},
description: "Display node 0 from default personal keg",
},
{
name: "cat_default_keg_overrides_path_resolution",
args: []string{
"cat",
"0",
},
setupFixture: strPtr("joe"),
cwd: strPtr("~/repos/work/spy-things"),
expectedInStdout: []string{
"---",
"# Sorry, planned but not yet available",
},
description: "Display node 0 from default keg even when kegMap path matches",
},
{
name: "cat_explicit_alias_overrides_path_resolution",
args: []string{
"cat",
"0",
"--keg", "example",
},
setupFixture: strPtr("joe"),
cwd: strPtr("~/repos/work/spy-things"),
expectedInStdout: []string{
"---",
"title:",
"---",
},
description: "Explicit alias overrides path-based keg resolution",
},
{
name: "cat_personal_keg_explicit_alias",
args: []string{
"cat",
"0",
"--keg", "personal",
},
setupFixture: strPtr("joe"),
expectedInStdout: []string{
"---",
"# Sorry, planned but not yet available",
},
description: "Display node 0 from personal keg with explicit alias",
},
{
name: "cat_content_only",
args: []string{
"cat",
"0",
"--content-only",
"--keg", "personal",
},
setupFixture: strPtr("joe"),
expectedInStdout: []string{
"# Sorry, planned but not yet available",
},
description: "Display only content when --content-only is provided",
},
{
name: "cat_meta_only",
args: []string{
"cat",
"0",
"--meta-only",
"--keg", "personal",
},
setupFixture: strPtr("joe"),
expectedInStdout: []string{
"tags:",
"- planned",
},
description: "Display only metadata when --meta-only is provided",
},
{
name: "cat_stats_only",
args: []string{
"cat",
"0",
"--stats-only",
"--keg", "personal",
},
setupFixture: strPtr("joe"),
expectedInStdout: []string{
"hash:",
"updated:",
},
description: "Display only stats when --stats-only is provided",
},
}
for _, tt := range tests {
t.Run(tt.name, func(innerT *testing.T) {
innerT.Parallel()
var opts []testutils.Option
if tt.setupFixture != nil {
opts = append(opts, testutils.WithFixture(*tt.setupFixture, "~"))
}
sb := NewSandbox(innerT, opts...)
if tt.cwd != nil {
sb.Setwd(*tt.cwd)
}
h := NewProcess(innerT, false, tt.args...)
res := h.Run(sb.Context(), sb.Runtime())
if tt.expectedErr != "" {
require.Error(innerT, res.Err, "expected error - %s", tt.description)
stderr := string(res.Stderr)
require.Contains(innerT, stderr, tt.expectedErr,
"error message should contain %q, got stderr: %s and err: %v", tt.expectedErr, stderr, res.Err)
} else {
require.NoError(innerT, res.Err, "cat command should succeed - %s", tt.description)
stdout := string(res.Stdout)
for _, expected := range tt.expectedInStdout {
require.Contains(innerT, stdout, expected,
"expected output to contain %q, got:\n%s", expected, stdout)
}
if !strings.Contains(strings.Join(tt.args, " "), "--content-only") &&
!strings.Contains(strings.Join(tt.args, " "), "--meta-only") &&
!strings.Contains(strings.Join(tt.args, " "), "--stats-only") {
// Verify frontmatter structure
require.Contains(innerT, stdout, "---", "output should contain frontmatter delimiter")
}
}
})
}
}
func TestCatCommand_IntegrationWithInit(t *testing.T) {
t.Run("cat_node_after_init", func(innerT *testing.T) {
innerT.Parallel()
opts := []testutils.Option{
testutils.WithFixture("testuser", "~"),
}
sb := NewSandbox(innerT, opts...)
// First, initialize a user keg
initCmd := NewProcess(innerT, false,
"init",
"--user",
"--keg", "newstudy",
"--creator", "test-user",
)
initRes := initCmd.Run(sb.Context(), sb.Runtime())
require.NoError(innerT, initRes.Err, "init should succeed")
require.Contains(innerT, string(initRes.Stdout), "keg newstudy created")
// Now cat the node 0
catCmd := NewProcess(innerT, false, "cat", "0", "--keg", "newstudy")
catRes := catCmd.Run(sb.Context(), sb.Runtime())
require.NoError(innerT, catRes.Err, "cat should succeed")
stdout := string(catRes.Stdout)
require.Contains(innerT, stdout, "---", "output should contain frontmatter")
require.NotContains(innerT, stdout, "access_count:", "frontmatter should not inject stats")
require.Contains(innerT, stdout, "Sorry, planned but not yet available", "output should contain content")
})
}
func TestCatCommand_UserKeg(t *testing.T) {
t.Run("cat_from_user_keg_with_alias", func(innerT *testing.T) {
innerT.Parallel()
opts := []testutils.Option{
testutils.WithFixture("testuser", "~"),
}
sb := NewSandbox(innerT, opts...)
// First, initialize a user keg
initCmd := NewProcess(innerT, false,
"init",
"--user",
"--keg", "public",
"--creator", "test-user",
)
initRes := initCmd.Run(sb.Context(), sb.Runtime())
require.NoError(innerT, initRes.Err, "init should succeed")
require.Contains(innerT, string(initRes.Stdout), "keg public created")
// Now cat the node from that user keg
catCmd := NewProcess(innerT, false, "cat", "0", "--keg", "public")
catRes := catCmd.Run(sb.Context(), sb.Runtime())
require.NoError(innerT, catRes.Err, "cat should succeed")
stdout := string(catRes.Stdout)
require.Contains(innerT, stdout, "---", "output should contain frontmatter")
require.NotContains(innerT, stdout, "access_count:", "frontmatter should not inject stats")
require.Contains(innerT, stdout, "Sorry, planned but not yet available", "content should be present")
})
}
func TestCatCommand_BumpsAccessedAndAccessCount(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
statsPath := "~/kegs/personal/0/stats.json"
oldAccessed := "2001-01-01T00:00:00Z"
sb.MustWriteFile(statsPath, []byte(`{"accessed":"`+oldAccessed+`","access_count":7}`), 0o644)
h := NewProcess(t, false, "cat", "0", "--keg", "personal", "--content-only")
res := h.Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err, "cat should succeed and bump access metadata")
var afterOne catStatsJSON
require.NoError(t, json.Unmarshal(sb.MustReadFile(statsPath), &afterOne))
require.Equal(t, 8, afterOne.AccessCount, "access count should increment on read")
require.NotEmpty(t, afterOne.Accessed, "accessed should be set")
require.NotEqual(t, oldAccessed, afterOne.Accessed, "accessed should be bumped")
res = h.Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err, "second cat should also succeed")
var afterTwo catStatsJSON
require.NoError(t, json.Unmarshal(sb.MustReadFile(statsPath), &afterTwo))
require.Equal(t, 9, afterTwo.AccessCount, "access count should increment on every read")
}
func TestCatCommand_DefaultFrontmatterDoesNotInjectStats(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
statsPath := "~/kegs/personal/0/stats.json"
sb.MustWriteFile(statsPath, []byte(`{"accessed":"2025-01-01T00:00:00Z","access_count":123}`), 0o644)
res := NewProcess(t, false, "cat", "0", "--keg", "personal").Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
require.NotContains(t, out, "access_count:", "default frontmatter should come from meta only")
require.NotContains(t, out, "accessed:", "default frontmatter should come from meta only")
}
func TestCatCommand_EditFlagEditsNode(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
jail := sb.Runtime().GetJail()
require.NotEmpty(t, jail)
resolvedJail, err := filepath.EvalSymlinks(jail)
require.NoError(t, err)
require.NoError(t, sb.Runtime().SetJail(resolvedJail))
jail = resolvedJail
scriptPath := filepath.Join(jail, "cat-edit-node.sh")
script := `#!/bin/sh
cat > "$1" <<'EOF'
---
tags:
- edited-via-cat
summary: changed by cat edit
---
# Cat Edited
Body updated from cat --edit.
EOF
`
require.NoError(t, os.WriteFile(scriptPath, []byte(script), 0o755))
require.NoError(t, sb.Runtime().Set("EDITOR", "/bin/sh "+scriptPath))
sb.Runtime().Unset("VISUAL")
res := NewProcess(t, false, "cat", "0", "--keg", "personal", "--edit").RunWithIO(sb.Context(), sb.Runtime(), strings.NewReader(""))
require.NoError(t, res.Err)
require.Equal(t, "", strings.TrimSpace(string(res.Stdout)))
meta := string(sb.MustReadFile("~/kegs/personal/0/meta.yaml"))
content := string(sb.MustReadFile("~/kegs/personal/0/README.md"))
require.Contains(t, meta, "- edited-via-cat")
require.Contains(t, meta, "summary: changed by cat edit")
require.Contains(t, content, "# Cat Edited")
require.Contains(t, content, "Body updated from cat --edit.")
}
// TestCatCommand_MultiNode_YAMLStream verifies that requesting multiple nodes
// in default (frontmatter) mode produces a YAML multi-document stream where
// each document has an injected "id:" field and no "=== N ===" decoration.
func TestCatCommand_MultiNode_YAMLStream(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
// Create a second node so we have two to cat.
createRes := NewProcess(t, false, "create", "--keg", "personal", "--title", "Second node").Run(sb.Context(), sb.Runtime())
require.NoError(t, createRes.Err, "create should succeed")
res := NewProcess(t, false, "cat", "0", "1", "--keg", "personal").Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
// Must contain id fields for both nodes.
require.Contains(t, out, `id: "0"`, "first document should have id: 0")
require.Contains(t, out, `id: "1"`, "second document should have id: 1")
// Must NOT contain the old === decoration.
require.NotContains(t, out, "=== 0 ===", "should not use old header format")
require.NotContains(t, out, "=== 1 ===", "should not use old header format")
// The output must be a valid YAML stream: starts with "---".
require.True(t, strings.HasPrefix(out, "---\n"), "output should start with YAML document-start marker")
}
// TestCatCommand_MultiNode_ContentOnly verifies that --content-only with
// multiple nodes injects the node ID as a tiny YAML frontmatter before each
// content block.
func TestCatCommand_MultiNode_ContentOnly(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
// Create a second node so we have two to cat.
createRes := NewProcess(t, false, "create", "--keg", "personal", "--title", "Second node").Run(sb.Context(), sb.Runtime())
require.NoError(t, createRes.Err, "create should succeed")
res := NewProcess(t, false, "cat", "0", "1", "--keg", "personal", "--content-only").Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
// Each document must carry its ID.
require.Contains(t, out, `id: "0"`, "content-only multi-node should inject id for first node")
require.Contains(t, out, `id: "1"`, "content-only multi-node should inject id for second node")
// The output starts with a YAML document-start marker.
require.True(t, strings.HasPrefix(out, "---\n"), "output should start with ---")
}
// TestCatCommand_MultiNode_MetaOnly verifies that --meta-only with multiple
// nodes injects the node ID into each YAML document.
func TestCatCommand_MultiNode_MetaOnly(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
createRes := NewProcess(t, false, "create", "--keg", "personal", "--title", "Second node").Run(sb.Context(), sb.Runtime())
require.NoError(t, createRes.Err, "create should succeed")
res := NewProcess(t, false, "cat", "0", "1", "--keg", "personal", "--meta-only").Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
require.Contains(t, out, `id: "0"`, "meta-only multi-node should inject id for first node")
require.Contains(t, out, `id: "1"`, "meta-only multi-node should inject id for second node")
require.True(t, strings.HasPrefix(out, "---\n"), "output should start with ---")
}
// TestCatCommand_MultiNode_StatsOnly verifies that --stats-only with multiple
// nodes injects the node ID into each YAML document.
func TestCatCommand_MultiNode_StatsOnly(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
createRes := NewProcess(t, false, "create", "--keg", "personal", "--title", "Second node").Run(sb.Context(), sb.Runtime())
require.NoError(t, createRes.Err, "create should succeed")
res := NewProcess(t, false, "cat", "0", "1", "--keg", "personal", "--stats-only").Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
require.Contains(t, out, `id: "0"`, "stats-only multi-node should inject id for first node")
require.Contains(t, out, `id: "1"`, "stats-only multi-node should inject id for second node")
require.True(t, strings.HasPrefix(out, "---\n"), "output should start with ---")
}
// TestCatCommand_SingleNode_NoIDField verifies that a single-node cat does NOT
// inject an "id:" field (backward-compatibility guarantee).
func TestCatCommand_SingleNode_NoIDField(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
res := NewProcess(t, false, "cat", "0", "--keg", "personal").Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
require.NotContains(t, out, `id: "0"`, "single-node output should not have injected id field")
}
// TestCatCommand_TTY_SingleNode_DelegatesToEdit verifies that when stdout is a
// TTY and a single node is requested with no output-mode flags, cat delegates
// to the edit flow (editor is opened, no content printed to stdout).
func TestCatCommand_TTY_SingleNode_DelegatesToEdit(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
jail := sb.Runtime().GetJail()
require.NotEmpty(t, jail)
resolvedJail, err := filepath.EvalSymlinks(jail)
require.NoError(t, err)
require.NoError(t, sb.Runtime().SetJail(resolvedJail))
jail = resolvedJail
// Editor script that writes known content so we can verify edit ran.
scriptPath := filepath.Join(jail, "cat-tty-edit.sh")
script := `#!/bin/sh
cat > "$1" <<'EOF'
---
tags:
- tty-edited
---
# TTY Cat Edit
EOF
`
require.NoError(t, os.WriteFile(scriptPath, []byte(script), 0o755))
require.NoError(t, sb.Runtime().Set("EDITOR", "/bin/sh "+scriptPath))
sb.Runtime().Unset("VISUAL")
// isTTY=true: should delegate to editor, no stdout output.
res := NewProcess(t, true, "cat", "0", "--keg", "personal").
RunWithIO(sb.Context(), sb.Runtime(), strings.NewReader(""))
require.NoError(t, res.Err)
require.Equal(t, "", strings.TrimSpace(string(res.Stdout)),
"interactive TTY cat should not print to stdout")
// Verify editor was invoked by checking the node was modified.
content := string(sb.MustReadFile("~/kegs/personal/0/README.md"))
require.Contains(t, content, "# TTY Cat Edit",
"editor should have modified the node content")
meta := string(sb.MustReadFile("~/kegs/personal/0/meta.yaml"))
require.Contains(t, meta, "- tty-edited",
"editor should have modified the node metadata")
}
// TestCatCommand_NonTTY_SingleNode_PrintsToStdout verifies that when stdout is
// not a TTY, cat prints content to stdout as usual.
func TestCatCommand_NonTTY_SingleNode_PrintsToStdout(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
// isTTY=false: should print to stdout, not open editor.
res := NewProcess(t, false, "cat", "0", "--keg", "personal").
Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
require.Contains(t, out, "---", "non-TTY cat should print frontmatter")
require.Contains(t, out, "# Sorry, planned but not yet available",
"non-TTY cat should print node content to stdout")
}
// TestCatCommand_TTY_OutputModeFlags_OverrideTTY verifies that output-mode
// flags (--content-only, --stats-only, --meta-only) force stdout output even
// when the terminal is interactive.
func TestCatCommand_TTY_OutputModeFlags_OverrideTTY(t *testing.T) {
t.Parallel()
tests := []struct {
name string
flag string
expected string
}{
{
name: "content-only overrides TTY",
flag: "--content-only",
expected: "# Sorry, planned but not yet available",
},
{
name: "meta-only overrides TTY",
flag: "--meta-only",
expected: "tags:",
},
{
name: "stats-only overrides TTY",
flag: "--stats-only",
expected: "hash:",
},
}
for _, tt := range tests {
t.Run(tt.name, func(innerT *testing.T) {
innerT.Parallel()
sb := NewSandbox(innerT, testutils.WithFixture("joe", "~"))
// isTTY=true but output-mode flag set: should print to stdout.
res := NewProcess(innerT, true, "cat", "0", "--keg", "personal", tt.flag).
Run(sb.Context(), sb.Runtime())
require.NoError(innerT, res.Err)
out := string(res.Stdout)
require.Contains(innerT, out, tt.expected,
"output-mode flag should force stdout output even with TTY")
})
}
}
// TestCatCommand_TTY_MultipleNodes_PrintsToStdout verifies that requesting
// multiple nodes always prints to stdout even on an interactive terminal.
func TestCatCommand_TTY_MultipleNodes_PrintsToStdout(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
// Create a second node so we have two to cat.
createRes := NewProcess(t, false, "create", "--keg", "personal", "--title", "Second node").
Run(sb.Context(), sb.Runtime())
require.NoError(t, createRes.Err, "create should succeed")
// isTTY=true with multiple nodes: should still print to stdout.
res := NewProcess(t, true, "cat", "0", "1", "--keg", "personal").
Run(sb.Context(), sb.Runtime())
require.NoError(t, res.Err)
out := string(res.Stdout)
require.Contains(t, out, `id: "0"`, "multi-node output should have id for first node")
require.Contains(t, out, `id: "1"`, "multi-node output should have id for second node")
}
// TestCatCommand_TagAndQueryFlags verifies that --tag and --query are
// independently functional (not aliased to the same variable) and that
// using both simultaneously produces an error.
func TestCatCommand_TagAndQueryFlags(t *testing.T) {
t.Parallel()
t.Run("tag_flag_selects_nodes", func(innerT *testing.T) {
innerT.Parallel()
sb := NewSandbox(innerT, testutils.WithFixture("joe", "~"))
res := NewProcess(innerT, false, "cat", "--tag", "planned", "--keg", "personal", "--content-only").
Run(sb.Context(), sb.Runtime())
require.NoError(innerT, res.Err, "--tag should select nodes by tag expression")
require.Contains(innerT, string(res.Stdout), "Sorry, planned but not yet available",
"--tag should return content for matching nodes")
})
t.Run("query_flag_selects_nodes", func(innerT *testing.T) {
innerT.Parallel()
sb := NewSandbox(innerT, testutils.WithFixture("joe", "~"))
res := NewProcess(innerT, false, "cat", "--query", "planned", "--keg", "personal", "--content-only").
Run(sb.Context(), sb.Runtime())
require.NoError(innerT, res.Err, "--query should select nodes by tag expression")
require.Contains(innerT, string(res.Stdout), "Sorry, planned but not yet available",
"--query should return content for matching nodes")
})
t.Run("tag_and_query_mutually_exclusive", func(innerT *testing.T) {
innerT.Parallel()
sb := NewSandbox(innerT, testutils.WithFixture("joe", "~"))
res := NewProcess(innerT, false, "cat", "--tag", "planned", "--query", "planned", "--keg", "personal").
Run(sb.Context(), sb.Runtime())
require.Error(innerT, res.Err, "using both --tag and --query should error")
require.Contains(innerT, string(res.Stderr), "mutually exclusive",
"error should mention mutual exclusivity")
})
}
// TestCatCommand_TTY_BumpsAccessCount verifies that interactive TTY cat
// increments access_count via the edit flow's Touch call.
func TestCatCommand_TTY_BumpsAccessCount(t *testing.T) {
t.Parallel()
sb := NewSandbox(t, testutils.WithFixture("joe", "~"))
jail := sb.Runtime().GetJail()
require.NotEmpty(t, jail)
resolvedJail, err := filepath.EvalSymlinks(jail)
require.NoError(t, err)
require.NoError(t, sb.Runtime().SetJail(resolvedJail))
jail = resolvedJail
// Editor that immediately exits without changes.
scriptPath := filepath.Join(jail, "cat-tty-noop.sh")
script := "#!/bin/sh\ntrue\n"
require.NoError(t, os.WriteFile(scriptPath, []byte(script), 0o755))
require.NoError(t, sb.Runtime().Set("EDITOR", "/bin/sh "+scriptPath))
sb.Runtime().Unset("VISUAL")
statsPath := "~/kegs/personal/0/stats.json"
sb.MustWriteFile(statsPath, []byte(`{"accessed":"2001-01-01T00:00:00Z","access_count":5}`), 0o644)
res := NewProcess(t, true, "cat", "0", "--keg", "personal").
RunWithIO(sb.Context(), sb.Runtime(), strings.NewReader(""))
require.NoError(t, res.Err)
var stats catStatsJSON
require.NoError(t, json.Unmarshal(sb.MustReadFile(statsPath), &stats))
require.Equal(t, 6, stats.AccessCount,
"interactive TTY cat should bump access_count")
}