claude_storage 1.0.0

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
//! 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::abc` 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 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", "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 sessions::2. Got: {combined}"
  );
}

/// Pairwise: `.list type::uuid 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", "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 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 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", "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 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 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", "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 sessions::0 should succeed. stderr: {stderr}"
  );
}

/// Test `.list 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", "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 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::all` 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::notvalid` 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 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 sessions::1` shows "(1 session)" (singular) when project has 1 session.
///
/// 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", "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 sessions::1 should succeed; stderr: {}",
    String::from_utf8_lossy( &output.stderr )
  );
  assert!(
    stdout.contains( "(1 session)" ),
    "with 1 session, project label must use singular '(1 session)'; got:\n{stdout}"
  );
  assert!(
    !stdout.contains( "(1 sessions)" ),
    "with 1 session, project label must NOT use plural '(1 sessions)'; got:\n{stdout}"
  );
}

/// Test `.list sessions::1` shows "(2 sessions)" (plural) when project has 2 sessions.
///
/// 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", "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 sessions::1 should succeed; stderr: {}",
    String::from_utf8_lossy( &output.stderr )
  );
  assert!(
    stdout.contains( "(2 sessions)" ),
    "with 2 sessions, project label must use plural '(2 sessions)'; got:\n{stdout}"
  );
}