claude_storage 1.3.1

CLI tool for exploring Claude Code filesystem storage
Documentation
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
//! List Command Parameter Validation Tests
//!
//! ## Purpose
//!
//! Validates parameter bounds and combinations for the `.list` command.
//! Covers Finding #015 (verbosity range) and session filter empty-string validation.
//!
//! ## Coverage
//!
//! - Verbosity range validation (0-5): values outside range rejected (Finding #015)
//! - Agent Boolean parameter: only 0 or 1 accepted (framework validation)
//! - Session filter: non-empty string required when provided
//! - Sessions Boolean: only 0 or 1 accepted (framework validation)
//! - Pairwise combinations: valid and invalid parameter combinations
//!
//! ## Testing Strategy
//!
//! All tests use `CLAUDE_STORAGE_ROOT` + `TempDir` for full isolation.
//! No test touches the real `~/.claude/` storage directory.
//!
//! ## Related Requirements
//!
//! REQ-010: List Command specification (spec.md)

mod common;

/// Test `.list ```verbosity::```-1` fails — negative verbosity rejected (Finding #015)
///
/// ## Root Cause
///
/// `list_routine` retrieved verbosity with `get_integer("verbosity").unwrap_or(1)`
/// but never validated the 0-5 range constraint. Values like -1 or 10 were silently
/// accepted, unlike `status_routine` and `show_routine` which include explicit range
/// validation after the `get_integer` call.
///
/// ## Why Not Caught
///
/// The `.list` command had no parameter validation tests. Existing list tests only
/// verified functionality with valid parameters. No tests covered out-of-range values
/// or boundary conditions for the verbosity parameter.
///
/// ## Fix Applied
///
/// Added explicit range check `if !(0..=5).contains(&verbosity)` in `list_routine`
/// immediately after `get_integer("verbosity").unwrap_or(1)`, matching the pattern
/// from `status_routine` (lines 88-99). Returns `"Invalid verbosity: N. Valid range: 0-5"`.
///
/// ## Prevention
///
/// Every command with a `verbosity` parameter must validate the 0-5 range at routine
/// entry. When adding new commands, audit existing commands for inconsistent validation.
/// Parameters with defaults still require validation — `unwrap_or(1)` does not prevent
/// a user from passing `verbosity::-1` or `verbosity::10`.
///
/// ## Pitfall
///
/// `get_integer("verbosity").unwrap_or(1)` only substitutes the default when the
/// parameter is absent. When the user explicitly provides an out-of-range value, the
/// function returns that value — range validation is always the caller's responsibility.
///
/// Related: Finding #015, spec.md REQ-010
// test_kind: validation(finding-015)
#[ test ]
fn test_list_verbosity_negative()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::-1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with verbosity::-1. Got: {combined}"
  );

  assert!(
    combined.to_lowercase().contains( "verbosity" ) ||
    combined.to_lowercase().contains( "invalid" ),
    "Error should mention verbosity or invalid. Got: {combined}"
  );
}

/// Test `.list ```verbosity::```6` fails — above-range verbosity rejected (Finding #015)
///
/// Same root cause and fix as `test_list_verbosity_negative`. Tests the upper
/// boundary: `verbosity::5` is the maximum, `verbosity::6` must be rejected.
///
/// Related: Finding #015
// test_kind: validation(finding-015)
#[ test ]
fn test_list_verbosity_out_of_range()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::6" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with verbosity::6. Got: {combined}"
  );

  assert!(
    combined.to_lowercase().contains( "verbosity" ) ||
    combined.to_lowercase().contains( "invalid" ),
    "Error should mention verbosity or invalid. Got: {combined}"
  );
}

/// Test `.list ```verbosity::```0` succeeds — minimum boundary accepted (Finding #015)
///
/// Verifies that the lower boundary of the valid range (0) is accepted after
/// Fix(issue-015). `verbosity::0` suppresses most output but is a valid value.
///
/// Related: Finding #015
// test_kind: validation(finding-015)
#[ test ]
fn test_list_verbosity_zero()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-proj-v0", "sess-v0-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::0" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list verbosity::0 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```verbosity::```3` succeeds — mid-range value accepted (Finding #015)
///
/// Related: Finding #015
// test_kind: validation(finding-015)
#[ test ]
fn test_list_verbosity_mid_range()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-proj-v3", "sess-v3-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::3" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list verbosity::3 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```verbosity::```5` succeeds — maximum boundary accepted (Finding #015)
///
/// Related: Finding #015
// test_kind: validation(finding-015)
#[ test ]
fn test_list_verbosity_max()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-proj-v5", "sess-v5-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::5" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list verbosity::5 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```agent::```2` fails — Boolean parameter rejects non-Boolean value
///
/// `agent` is declared as `Boolean` type in the CLI YAML specification. The
/// framework validates Boolean parameters and only accepts 0 or 1. Any other
/// value (including 2) must be rejected.
#[ test ]
fn test_list_agent_invalid()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "agent::2" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with invalid Boolean agent::2. Got: {combined}"
  );
}

/// Test `.list ```agent::```0` succeeds — Boolean false value
#[ test ]
fn test_list_agent_zero()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "agent::0" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list agent::0 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```agent::```1` succeeds — Boolean true value
#[ test ]
fn test_list_agent_one()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-proj-ag", "sess-ag-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "agent::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list agent::1 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```session::ab```c` succeeds — non-empty session substring filter
#[ test ]
fn test_list_session_filter()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-proj-sf", "sess-sf-abc", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "session::abc" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list session::abc should succeed. stderr: {stderr}"
  );
}

/// Test `.list session::` fails — empty session filter rejected by framework
///
/// The framework rejects `session::` (empty value for a String parameter) with a
/// parse error: "Expected value for named argument 'session' but found end of
/// instruction". No application-level validation is needed; the framework enforces
/// that String parameters must have a non-empty value.
///
/// ## Pitfall
///
/// Don't assume String parameters accept empty values. The unilang framework
/// treats `param::` (no value after `::`) as a parse error, not as `Some("")`.
/// This means application code never sees the empty-string case for String params.
// test_kind: validation(session-empty-filter)
#[ test ]
fn test_list_session_filter_empty()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "session::" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with empty session filter. Got: {combined}"
  );

  assert!(
    combined.contains( "session" ),
    "Error should mention session parameter. Got: {combined}"
  );
}

/// Test `.list ```show_sessions::```2` fails — Boolean parameter rejects non-Boolean value
///
/// `sessions` is declared as `Boolean` type. Framework validation rejects
/// any value other than 0 or 1.
#[ test ]
fn test_list_sessions_invalid()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "show_sessions::2" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with invalid Boolean show_sessions::2. Got: {combined}"
  );
}

/// Pairwise: `.list ```type::uuid``` ```show_sessions::```1` succeeds
#[ test ]
fn test_list_type_uuid_with_sessions()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-pair-uuid", "sess-uuid-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "type::uuid", "show_sessions::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list type::uuid show_sessions::1 should succeed. stderr: {stderr}"
  );
}

/// Pairwise: `.list ```type::path``` ```verbosity::```2` succeeds
#[ test ]
fn test_list_type_path_with_verbosity()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-pair-path", "sess-path-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "type::path", "verbosity::2" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list type::path verbosity::2 should succeed. stderr: {stderr}"
  );
}

/// Pairwise: `.list ```show_sessions::0``` ```verbosity::```-1` fails — invalid verbosity
///
/// When multiple parameters are provided with one invalid value, the command
/// must fail. Verbosity range validation happens before storage access.
// test_kind: validation(finding-015)
#[ test ]
fn test_list_sessions_with_invalid_verbosity()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "show_sessions::0", "verbosity::-1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with verbosity::-1 even alongside valid show_sessions::0. Got: {combined}"
  );
}

/// Test `.list` with no parameters succeeds (base case, covers agent absent)
#[ test ]
fn test_list_no_params()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-base-proj", "sess-base-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list with no params should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```show_sessions::```0` succeeds — explicit sessions-off
#[ test ]
fn test_list_sessions_zero()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-s0-proj", "sess-s0-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "show_sessions::0" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list show_sessions::0 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```show_sessions::```1` succeeds — explicit sessions-on
#[ test ]
fn test_list_sessions_one()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-s1-proj", "sess-s1-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "show_sessions::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list show_sessions::1 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```min_entries::```0` succeeds — zero `min_entries` is valid lower bound
#[ test ]
fn test_list_min_entries_zero()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-me-proj", "sess-me-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "min_entries::0" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list min_entries::0 should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```path::```/tmp` succeeds — substring path filter
#[ test ]
fn test_list_path_filter()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "path::/tmp" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list path::/tmp should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```type::al```l` succeeds — explicit all-types filter
#[ test ]
fn test_list_type_all()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-tall-proj", "sess-tall-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "type::all" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list type::all should succeed. stderr: {stderr}"
  );
}

/// Test `.list ```type::notvali```d` fails — type parameter validates against allowed values
///
/// `list_routine` validates `type` against "uuid", "path", and "all". Any other
/// value returns `"Invalid type: X. Valid values: uuid, path, all"`.
#[ test ]
fn test_list_type_invalid()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();

  let output = common::clg_cmd()
    .args( [ ".list", "type::notvalid" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );
  let stdout = String::from_utf8_lossy( &output.stdout );
  let combined = format!( "{stderr}{stdout}" );

  assert!(
    !output.status.success(),
    "Should fail with invalid type. Got: {combined}"
  );

  assert!(
    combined.to_lowercase().contains( "type" ) ||
    combined.to_lowercase().contains( "invalid" ),
    "Error should mention type or invalid. Got: {combined}"
  );
}

/// Test `.list ```verbosity::```1` succeeds — default value accepted explicitly
#[ test ]
fn test_list_verbosity_default_explicit()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "list-vdef-proj", "sess-vdef-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stderr = String::from_utf8_lossy( &output.stderr );

  assert!(
    output.status.success(),
    ".list verbosity::1 (default) should succeed. stderr: {stderr}"
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// issue-025 regression: "Found 1 projects:" uses wrong plural — must be
// "Found 1 project:" (singular).
//
// Root Cause: list_routine always formats the count noun as "projects"
// regardless of count. English grammar requires singular when count == 1.
//
// Why Not Caught: No existing test asserted the exact singular/plural form of
// the "Found N projects:" header — only that the command succeeds.
//
// Fix Applied: Derive noun ("project" vs "projects") from the count, use
// it in the header format string.
//
// Prevention: Assert exact-string header form, not just command success.
//
// Pitfall: "Found 0 projects:" stays plural — zero takes plural in English.
// ─────────────────────────────────────────────────────────────────────────────

/// Test `.list` outputs singular "Found 1 project:" when exactly 1 project exists.
///
/// bug_reproducer(issue-025)
// test_kind: bug_reproducer(issue-025)
#[ test ]
fn test_list_singular_noun_one_project()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "only-proj", "sess-sing-001", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  assert!(
    output.status.success(),
    ".list with 1 project should succeed; stderr: {}",
    String::from_utf8_lossy( &output.stderr )
  );
  assert!(
    stdout.contains( "Found 1 project:" ),
    "with 1 project, header must use singular 'project'; got:\n{stdout}"
  );
  assert!(
    !stdout.contains( "Found 1 projects:" ),
    "with 1 project, header must NOT use plural 'projects'; got:\n{stdout}"
  );
}

/// Test `.list` outputs plural "Found 2 projects:" when 2 projects exist.
///
/// bug_reproducer(issue-025)
// test_kind: bug_reproducer(issue-025)
#[ test ]
fn test_list_plural_noun_multiple_projects()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "proj-a", "sess-plur-a", 2 );
  common::write_test_session( storage.path(), "proj-b", "sess-plur-b", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "verbosity::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  assert!(
    output.status.success(),
    ".list with 2 projects should succeed; stderr: {}",
    String::from_utf8_lossy( &output.stderr )
  );
  assert!(
    stdout.contains( "Found 2 projects:" ),
    "with 2 projects, header must use plural 'projects'; got:\n{stdout}"
  );
}

// ─────────────────────────────────────────────────────────────────────────────
// issue-027 regression: ".list show_sessions::1" with 1 session shows "(1 sessions)"
// — wrong plural in per-project session count label (verbosity 1).
//
// Root Cause: list_routine verbosity==1 branch used hardcoded plural "sessions"
// in the `"{:?} ({} sessions)"` format string regardless of session_count value.
// English requires singular "session" when count == 1.
//
// Why Not Caught: The issue-025 fix addressed the "Found N X:" header noun but
// missed the per-project session count label on the same verbosity level. These
// are two separate format strings — fixing one left the other broken.
//
// Fix Applied: Derive noun ("session" vs "sessions") from session_count before
// the writeln! call, matching the pattern used for the "Found N X:" header fix.
//
// Prevention: When fixing plural nouns in a routine, audit ALL format strings
// that embed counts, not just the most visible one.
//
// Pitfall: Multiple format strings in the same routine can have the same bug.
// A targeted fix for one occurrence may miss siblings with identical patterns.
// ─────────────────────────────────────────────────────────────────────────────

/// Test `.list ```show_sessions::```1` shows "(1 conversation)" (singular) when project has 1 conversation.
///
/// bug_reproducer(issue-027)
// test_kind: bug_reproducer(issue-027)
#[ test ]
fn test_list_session_count_singular_when_one_session()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  common::write_test_session( storage.path(), "sing-proj", "sess-sing-only", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "show_sessions::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  assert!(
    output.status.success(),
    ".list show_sessions::1 should succeed; stderr: {}",
    String::from_utf8_lossy( &output.stderr )
  );
  assert!(
    stdout.contains( "(1 conversation)" ),
    "with 1 conversation, project label must use singular '(1 conversation)'; got:\n{stdout}"
  );
  assert!(
    !stdout.contains( "(1 conversations)" ),
    "with 1 conversation, project label must NOT use plural '(1 conversations)'; got:\n{stdout}"
  );
}

/// Test `.list ```show_sessions::```1` shows "(2 conversations)" (plural) when project has 2 conversations.
///
/// Regression guard for issue-027: plural form must remain correct for counts > 1.
// test_kind: regression_guard(issue-027)
#[ test ]
fn test_list_session_count_plural_when_multiple_sessions()
{
  use tempfile::TempDir;
  let storage = TempDir::new().unwrap();
  // Two sessions in the same project
  common::write_test_session( storage.path(), "plur-proj", "sess-plur-x", 2 );
  common::write_test_session( storage.path(), "plur-proj", "sess-plur-y", 2 );

  let output = common::clg_cmd()
    .args( [ ".list", "show_sessions::1" ] )
    .env( "CLAUDE_STORAGE_ROOT", storage.path() )
    .output()
    .expect( "Failed to execute .list" );

  let stdout = String::from_utf8_lossy( &output.stdout );
  assert!(
    output.status.success(),
    ".list show_sessions::1 should succeed; stderr: {}",
    String::from_utf8_lossy( &output.stderr )
  );
  assert!(
    stdout.contains( "(2 conversations)" ),
    "with 2 conversations, project label must use plural '(2 conversations)'; got:\n{stdout}"
  );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T01: `.list type::conversation project::<id>` outputs conversation IDs
// ────────────────────────────────────────────────────────────────────────────
/// IT-T01: `.list ```type::conversatio```n` outputs one conversation ID per line when `project::` is given.
///
/// Verifies the conversation type filter lists IDs and exits 0.
#[ test ]
fn it_list_type_conversation_outputs_ids()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-conv" );
  let encoded = common::write_path_project_session( &storage_root, &project, "sess-conv-001", 2 );

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list type::conversation must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout );
  assert!( !s.is_empty(), "output must be non-empty when project has sessions; got:\n{s}" );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T02: `.list type::conversation` without `project::` returns error
// ────────────────────────────────────────────────────────────────────────────
/// IT-T02: `.list ```type::conversatio```n` without `project::` must return a clear error.
///
/// Conversations require a project scope; without it the command must fail.
#[ test ]
fn it_list_type_conversation_requires_project()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .output()
    .unwrap();

  assert!(
    !out.status.success(),
    ".list type::conversation without project:: must exit non-0; got: {:?}",
    out.status.code(),
  );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T03: `.list type::conversation count::1 project::<id>` outputs bare integer
// ────────────────────────────────────────────────────────────────────────────
/// IT-T03: `count::1` mode outputs only the count as a bare integer.
///
/// Useful for scripting: `clg .list ```type::conversation``` ```count::1``` ```project::abc12```3` → `2`
#[ test ]
fn it_list_count_mode_outputs_integer()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-count" );
  let encoded = common::write_path_project_session( &storage_root, &project, "sess-count-001", 2 );
  common::write_path_project_session( &storage_root, &project, "sess-count-002", 2 );

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( "count::1" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list count::1 must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout ).trim().to_string();
  assert!(
    s.parse::< usize >().is_ok(),
    "count mode must output a bare integer; got: '{s}'",
  );
  assert_eq!( s, "2", "expected 2 conversations for 2 sessions; got: '{s}'" );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T09 (CC-L01): `.list type::conversation project::<id>` line count = N sessions
// ────────────────────────────────────────────────────────────────────────────
/// IT-T09: `.list ```type::conversatio```n` outputs exactly N lines for N conversations.
///
/// IT-T01 only checks non-empty. This test verifies the line count precisely
/// matches the session count (1:1 identity mapping — no duplicates, no missing).
#[ test ]
fn it_list_type_conversation_exact_line_count()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-cc-l01" );
  let encoded = common::write_path_project_session( &storage_root, &project, "sess-l01-a", 2 );
  common::write_path_project_session( &storage_root, &project, "sess-l01-b", 2 );

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list type::conversation must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout );
  let lines : Vec< &str > = s.lines().filter( | l | !l.is_empty() ).collect();
  assert_eq!(
    lines.len(),
    2,
    "2 sessions → 2 conversation ID lines; got {} line(s):\n{s}",
    lines.len(),
  );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T10 (CC-L02): `.list type::conversation project::<id>` with 0 sessions → empty
// ────────────────────────────────────────────────────────────────────────────
/// IT-T10: `.list ```type::conversatio```n` on an empty project outputs nothing.
///
/// Project dir exists in storage but has no JSONL files.
/// Must exit 0 and produce empty output — not an error.
#[ test ]
fn it_list_type_conversation_empty_project()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-cc-l02-empty" );

  // Create project directory without any session files
  let encoded = claude_storage_core::encode_path( &project )
    .expect( "encode project path" );
  let dir = storage_root.join( "projects" ).join( &encoded );
  std::fs::create_dir_all( &dir ).unwrap();

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list type::conversation on empty project must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout ).trim().to_string();
  assert!(
    s.is_empty(),
    "empty project must produce empty output; got: '{s}'"
  );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T11 (CC-L03): `.list type::conversation count::0 project::<id>` → full list
// ────────────────────────────────────────────────────────────────────────────
/// IT-T11: `count::0` means "not count mode" — outputs full list, not bare integer.
///
/// `count::0` (Boolean false) must behave identically to omitting `count::`.
/// The output must be a multi-line list of conversation IDs, not a bare integer.
#[ test ]
fn it_list_count_zero_means_full_list()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-cc-l03" );
  let encoded = common::write_path_project_session( &storage_root, &project, "sess-l03-a", 2 );
  common::write_path_project_session( &storage_root, &project, "sess-l03-b", 2 );

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( "count::0" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list type::conversation count::0 must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout ).trim().to_string();
  // Output must NOT be a bare integer
  assert!(
    s.parse::< usize >().is_err(),
    "count::0 must produce list output (not bare integer); got: '{s}'"
  );
  // Must have 2 lines — one per conversation
  let lines : Vec< &str > = s.lines().filter( | l | !l.is_empty() ).collect();
  assert_eq!(
    lines.len(),
    2,
    "count::0 full list must have 2 lines for 2 sessions; got {} line(s):\n{s}",
    lines.len(),
  );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T12 (CC-L04): `.list type::conversation count::1` with 0 sessions → "0"
// ────────────────────────────────────────────────────────────────────────────
/// IT-T12: `count::1` on an empty project outputs "0".
///
/// Count mode on a project with zero conversations must output "0" (not crash).
#[ test ]
fn it_list_count_mode_zero_sessions()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-cc-l04-empty" );

  // Create project directory without any session files
  let encoded = claude_storage_core::encode_path( &project )
    .expect( "encode project path" );
  let dir = storage_root.join( "projects" ).join( &encoded );
  std::fs::create_dir_all( &dir ).unwrap();

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( "count::1" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list type::conversation count::1 on empty project must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout ).trim().to_string();
  assert_eq!( s, "0", "count::1 on empty project must output '0'; got: '{s}'" );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T13 (CC-L05): `.list type::conversation count::1` with 1 session → "1"
// ────────────────────────────────────────────────────────────────────────────
/// IT-T13: `count::1` with exactly 1 conversation outputs "1".
///
/// Singular count edge case — complementary to IT-T03 which uses 2 sessions.
#[ test ]
fn it_list_count_mode_one_session()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );
  let project = root.path().join( "proj-cc-l05" );
  let encoded = common::write_path_project_session( &storage_root, &project, "sess-l05-only", 2 );

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( "count::1" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    out.status.success(),
    ".list type::conversation count::1 must exit 0; stderr: {}",
    String::from_utf8_lossy( &out.stderr ),
  );
  let s = String::from_utf8_lossy( &out.stdout ).trim().to_string();
  assert_eq!( s, "1", "1 session → 1 conversation in count mode; got: '{s}'" );
}

// ────────────────────────────────────────────────────────────────────────────
// IT-T14 (CC-L06): `.list type::conversation project::<nonexistent>` → error
// ────────────────────────────────────────────────────────────────────────────
/// IT-T14: `.list ```type::conversatio```n` with a nonexistent project must fail.
///
/// A valid encoded project ID whose storage directory does not exist is an error.
/// Must exit non-0 — silently returning empty output would hide a user mistake.
#[ test ]
fn it_list_type_conversation_nonexistent_project_fails()
{
  use tempfile::TempDir;
  let root = TempDir::new().unwrap();
  let storage_root = root.path().join( ".claude" );

  // Encode a path but intentionally do NOT create the project dir in storage
  let fake_project = root.path().join( "nonexistent-proj-l06" );
  let encoded = claude_storage_core::encode_path( &fake_project )
    .expect( "encode project path" );

  let out = common::clg_cmd()
    .env( "HOME", root.path().to_str().unwrap() )
    .env( "CLAUDE_STORAGE_ROOT", storage_root.to_str().unwrap() )
    .arg( ".list" )
    .arg( "type::conversation" )
    .arg( format!( "project::{encoded}" ) )
    .output()
    .unwrap();

  assert!(
    !out.status.success(),
    ".list type::conversation with nonexistent project must exit non-0; stdout: {}; stderr: {}",
    String::from_utf8_lossy( &out.stdout ),
    String::from_utf8_lossy( &out.stderr ),
  );
}