double-o 0.6.0

Context-efficient command runner for AI coding agents
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
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
use assert_cmd::Command;
use predicates::prelude::*;
use std::os::unix::fs::PermissionsExt;
use tempfile::TempDir;

fn oo() -> Command {
    Command::cargo_bin("oo").unwrap()
}

#[test]
fn test_echo_passthrough() {
    oo().args(["echo", "hello"])
        .assert()
        .success()
        .stdout("hello\n");
}

#[test]
fn test_multiword_echo() {
    oo().args(["echo", "hello", "world"])
        .assert()
        .success()
        .stdout("hello world\n");
}

#[test]
fn test_false_failure() {
    oo().args(["false"])
        .assert()
        .failure()
        .stdout(predicate::str::starts_with("\u{2717}")); // ✗
}

#[test]
fn test_exit_code_preserved() {
    oo().args(["sh", "-c", "exit 42"]).assert().code(42);
}

#[test]
fn test_version() {
    oo().arg("version")
        .assert()
        .success()
        .stdout(predicate::str::contains(env!("CARGO_PKG_VERSION")));
}

#[test]
fn test_no_args_shows_help() {
    oo().assert()
        .success()
        .stdout(predicate::str::contains("Usage"));
}

#[test]
fn test_large_output_gets_indicator() {
    // `git log` is categorized as Data, so large output gets indexed (●).
    // Create a temp git repo to generate logs that exceed 4KB threshold.
    let dir = TempDir::new().unwrap();
    Command::new("git")
        .args(["init"])
        .current_dir(dir.path())
        .output()
        .unwrap();

    // Create multiple commits with content to exceed 4KB
    for i in 0..100 {
        std::fs::write(dir.path().join("file.txt"), format!("content {}\n", i)).unwrap();
        Command::new("git")
            .args(["add", "."])
            .current_dir(dir.path())
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", &format!("commit {}", i)])
            .env("GIT_AUTHOR_NAME", "Test")
            .env("GIT_AUTHOR_EMAIL", "test@example.com")
            .env("GIT_COMMITTER_NAME", "Test")
            .env("GIT_COMMITTER_EMAIL", "test@example.com")
            .current_dir(dir.path())
            .output()
            .unwrap();
    }

    // Run `oo git log` in the repo — should get ● (Large/indexed) indicator
    oo().args(["git", "log"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::starts_with("\u{25CF}")); // ●
}

#[test]
fn test_stderr_included_in_failure() {
    oo().args(["sh", "-c", "echo failure_msg >&2; exit 1"])
        .assert()
        .failure()
        .stdout(predicate::str::contains("failure_msg"));
}

#[test]
fn test_forget_runs() {
    oo().arg("forget")
        .assert()
        .success()
        .stdout(predicate::str::contains("Cleared session data"));
}

#[test]
fn test_help_no_args_shows_usage() {
    oo().arg("help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Usage:"));
}

#[test]
fn test_help_includes_help_cmd_in_usage() {
    // Verify the help command itself appears in the no-args usage output
    oo().assert()
        .success()
        .stdout(predicate::str::contains("help [cmd]"));
}

/// The no-args / `oo help` output must list all 7 reserved subcommands,
/// each with a one-line description, plus the tool description line.
fn assert_full_subcommand_list(
    assertion: assert_cmd::assert::Assert,
) -> assert_cmd::assert::Assert {
    let mut assertion = assertion.success();
    for (name, description) in [
        ("recall", "Search session output"),
        ("forget", "Clear session data"),
        ("learn", "Learn output compression patterns"),
        ("help [cmd]", "cheat-sheet for cmd via cheat.sh"),
        ("init", "Set up hooks for agent frameworks"),
        ("patterns", "List output compression patterns"),
        ("version", "Show version"),
    ] {
        assertion = assertion.stdout(predicate::str::contains(name));
        assertion = assertion.stdout(predicate::str::contains(description));
    }
    assertion = assertion.stdout(predicate::str::contains(
        "Context-efficient command runner for AI coding agents",
    ));
    // The internal _learn_bg command must never leak into help output.
    assertion.stdout(predicate::str::contains("_learn_bg").not())
}

#[test]
fn test_no_args_lists_all_subcommands() {
    assert_full_subcommand_list(oo().assert());
}

#[test]
fn test_help_no_args_lists_all_subcommands() {
    assert_full_subcommand_list(oo().arg("help").assert());
}

#[test]
fn test_help_empty_arg() {
    Command::cargo_bin("oo")
        .unwrap()
        .args(&["help", ""])
        .assert()
        .failure();
}

// ---------------------------------------------------------------------------
// oo init
// ---------------------------------------------------------------------------

#[test]
fn test_init_creates_hooks_json() {
    let dir = TempDir::new().unwrap();
    oo().arg("init").current_dir(dir.path()).assert().success();

    let hooks_path = dir.path().join(".claude").join("hooks.json");
    assert!(
        hooks_path.exists(),
        ".claude/hooks.json must exist after oo init"
    );
}

#[test]
fn test_init_hooks_json_is_valid_json() {
    let dir = TempDir::new().unwrap();
    oo().arg("init").current_dir(dir.path()).assert().success();

    let content = std::fs::read_to_string(dir.path().join(".claude").join("hooks.json")).unwrap();
    let parsed: serde_json::Value =
        serde_json::from_str(&content).expect("hooks.json must be valid JSON");
    assert!(parsed.get("hooks").is_some());
}

#[test]
fn test_init_prints_agents_snippet() {
    let dir = TempDir::new().unwrap();
    oo().arg("init")
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Prefix all shell commands with `oo`",
        ));
}

#[test]
fn test_init_second_run_does_not_overwrite() {
    let dir = TempDir::new().unwrap();

    // First run creates the file.
    oo().arg("init").current_dir(dir.path()).assert().success();

    // Overwrite with sentinel content.
    let hooks_path = dir.path().join(".claude").join("hooks.json");
    std::fs::write(&hooks_path, r#"{"hooks":[],"sentinel":true}"#).unwrap();

    // Second run must not clobber the file.
    oo().arg("init").current_dir(dir.path()).assert().success();

    let after = std::fs::read_to_string(&hooks_path).unwrap();
    assert!(
        after.contains("\"sentinel\":true"),
        "pre-existing hooks.json must not be overwritten on second oo init"
    );
}

#[test]
fn test_init_second_run_succeeds_without_error() {
    let dir = TempDir::new().unwrap();
    oo().arg("init").current_dir(dir.path()).assert().success();
    // Second invocation must exit 0 — idempotent.
    oo().arg("init").current_dir(dir.path()).assert().success();
}

// ---------------------------------------------------------------------------
// oo init --format generic
// ---------------------------------------------------------------------------

#[test]
fn test_init_format_generic_exits_success() {
    let dir = TempDir::new().unwrap();
    oo().args(["init", "--format", "generic"])
        .current_dir(dir.path())
        .assert()
        .success();
}

#[test]
fn test_init_format_generic_prints_agents_snippet() {
    let dir = TempDir::new().unwrap();
    oo().args(["init", "--format", "generic"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Prefix all shell commands with `oo`",
        ));
}

#[test]
fn test_init_format_generic_prints_setup_section() {
    let dir = TempDir::new().unwrap();
    oo().args(["init", "--format", "generic"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("## Setup"));
}

#[test]
fn test_init_format_generic_prints_shell_commands_instructions() {
    let dir = TempDir::new().unwrap();
    oo().args(["init", "--format", "generic"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("oo recall"))
        .stdout(predicate::str::contains("oo help"))
        .stdout(predicate::str::contains("oo learn"));
}

#[test]
fn test_init_format_generic_prints_alias_section() {
    let dir = TempDir::new().unwrap();
    oo().args(["init", "--format", "generic"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("alias o='oo'"));
}

#[test]
fn test_init_format_generic_does_not_create_hooks_json() {
    let dir = TempDir::new().unwrap();
    oo().args(["init", "--format", "generic"])
        .current_dir(dir.path())
        .assert()
        .success();
    // Generic format must NOT create any files.
    let hooks_path = dir.path().join(".claude").join("hooks.json");
    assert!(
        !hooks_path.exists(),
        "oo init --format generic must not create .claude/hooks.json"
    );
}

// ---------------------------------------------------------------------------
// oo init --format claude
// ---------------------------------------------------------------------------

#[test]
fn test_init_format_claude_creates_hooks_json() {
    let dir = TempDir::new().unwrap();
    oo().arg("init").current_dir(dir.path()).assert().success();
    let hooks_path = dir.path().join(".claude").join("hooks.json");
    assert!(
        hooks_path.exists(),
        ".claude/hooks.json must exist after oo init --format claude"
    );
}

#[test]
fn test_init_format_claude_prints_agents_snippet() {
    let dir = TempDir::new().unwrap();
    oo().arg("init")
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Prefix all shell commands with `oo`",
        ));
}

// ---------------------------------------------------------------------------
// recall command
// ---------------------------------------------------------------------------

#[test]
fn test_recall_no_args() {
    // `oo recall` with no query should fail with a helpful error
    oo().arg("recall")
        .assert()
        .failure()
        .stderr(predicate::str::contains("recall requires a query"));
}

#[test]
fn test_recall_no_results() {
    // A query that matches nothing should exit 0 and mention no results
    oo().args(["recall", "xyzzy_nonexistent_query_12345"])
        .assert()
        .success()
        .stdout(predicate::str::contains("No results found"));
}

// ---------------------------------------------------------------------------
// recall — snippet (bounded default) and --full integration tests
// ---------------------------------------------------------------------------
//
// These tests use OO_DATA_DIR to isolate the store so they don't touch the
// developer's real ~/.local/share/.oo/oo.db (or vice versa).
// ---------------------------------------------------------------------------

/// Seed a large indexed blob and return (temp_dir, blob_content).
///
/// The blob is > 4 KB so it hits the Large tier (● indexed indicator).
fn index_large_blob() -> (TempDir, String) {
    let dir = TempDir::new().unwrap();
    let blob: String = (0..200)
        .map(|i| {
            format!(
                "sentinel_line_{i:04} data_padding_abcdef_{}\n",
                "x".repeat(50)
            )
        })
        .collect();
    // ~ 200 * 85 = 17 KB — well above 4 KB threshold

    // OO_DATA_DIR isolates the store for this test run
    // `oo <cmd>` where cmd produces >4KB output → Large tier → indexed
    let blob_file = dir.path().join("blob.txt");
    std::fs::write(&blob_file, &blob).unwrap();

    // Use `cat` to produce large output (cat is Data category → Large tier)
    let mut cmd = oo();
    cmd.args(["cat", blob_file.to_str().unwrap()]);
    cmd.env("OO_DATA_DIR", dir.path());
    cmd.assert().success();

    (dir, blob)
}

#[test]
fn test_recall_default_is_bounded() {
    // Index a large blob, recall it — default output must be bounded
    let (dir, blob) = index_large_blob();

    // Recall with a token that appears in the blob
    let mut cmd = oo();
    cmd.args(["recall", "sentinel_line_0100"]);
    cmd.env("OO_DATA_DIR", dir.path());
    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success(), "recall must exit 0");

    // The stdout must be bounded: well under the full blob size
    let stdout_len = stdout.len();
    let blob_len = blob.len();
    assert!(
        stdout_len < blob_len,
        "default recall stdout ({stdout_len} B) must be < full blob ({blob_len} B)"
    );

    // The output must not be empty (results were found)
    assert!(
        !stdout.trim().is_empty(),
        "default recall must produce output"
    );

    // The output should NOT contain the tail of the blob (last 100 chars)
    let blob_tail = &blob[blob.len().saturating_sub(100)..];
    assert!(
        !stdout.contains(blob_tail),
        "default recall must NOT contain the tail of the blob (bounded display)"
    );
}

#[test]
fn test_recall_full_shows_complete_blob() {
    // --full must show the complete stored content
    let (dir, _blob) = index_large_blob();

    let mut cmd = oo();
    cmd.args(["recall", "--full", "sentinel_line_0100"]);
    cmd.env("OO_DATA_DIR", dir.path());
    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success(), "recall must exit 0");

    // The full blob content must appear (at least the last sentinel line)
    assert!(
        stdout.contains("sentinel_line_0199"),
        "--full recall must contain the last sentinel line of the blob"
    );
}

#[test]
fn test_recall_full_flag_position_independent() {
    // Both `oo recall --full <q>` and `oo recall <q> --full` must work
    let (dir, _blob) = index_large_blob();

    // Flag before query
    let mut cmd1 = oo();
    cmd1.args(["recall", "--full", "sentinel_line_0100"]);
    cmd1.env("OO_DATA_DIR", dir.path());
    let out1 = cmd1.output().unwrap();
    assert!(out1.status.success(), "flag-before must be --full mode");

    // Flag after query
    let mut cmd2 = oo();
    cmd2.args(["recall", "sentinel_line_0100", "--full"]);
    cmd2.env("OO_DATA_DIR", dir.path());
    let out2 = cmd2.output().unwrap();
    assert!(out2.status.success(), "flag-after must be --full mode");

    // Both must contain the last sentinel (proves --full mode)
    let stdout1 = String::from_utf8_lossy(&out1.stdout);
    let stdout2 = String::from_utf8_lossy(&out2.stdout);
    assert!(
        stdout1.contains("sentinel_line_0199"),
        "flag-before must be --full mode"
    );
    assert!(
        stdout2.contains("sentinel_line_0199"),
        "flag-after must be --full mode"
    );
}

#[test]
fn test_recall_full_alone_gives_empty_query_error() {
    // `oo recall --full` alone → empty query → error exit 1
    oo().args(["recall", "--full"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("recall requires a query"));
}

#[test]
fn test_recall_default_displays_fts_snippet_not_content_prefix() {
    // Guard for the whole FTS5 snippet feature: the default (non-`--full`)
    // recall output must contain the matched sentinel token — proving the
    // store-provided FTS5 snippet (centered on the best match) is what gets
    // displayed. If the wiring is removed and `cmd_recall` falls back to
    // `bounded_display(&r.content)` (the first ~512 chars of the full blob),
    // the sentinel token — placed well beyond the first 512 chars — will be
    // absent and this test will fail.
    let dir = TempDir::new().unwrap();
    // Build a blob whose first 512 chars contain NO occurrence of the matched
    // token: 150 filler tokens (1350 chars) before the sentinel.
    let filler: String = (0..500).map(|i| format!("filler{i:04}_ ")).collect(); // 500*11 = 5500 chars of filler
    let blob: String = format!("{filler}sentinel_beta_9999 {filler}"); // ~11000 chars total (well above 4 KB → Bounded/Large tier)
    let blob_file = dir.path().join("blob.txt");
    std::fs::write(&blob_file, &blob).unwrap();

    let mut cmd = oo();
    cmd.args(["cat", blob_file.to_str().unwrap()]);
    cmd.env("OO_DATA_DIR", dir.path());
    cmd.assert().success();

    // Default (bounded) recall for a token that appears only in the middle of
    // the blob — never in the first 512 chars.
    let mut cmd = oo();
    cmd.args(["recall", "sentinel_beta_9999"]);
    cmd.env("OO_DATA_DIR", dir.path());
    let output = cmd.output().unwrap();
    let stdout = String::from_utf8_lossy(&output.stdout);
    assert!(output.status.success(), "recall must exit 0");

    // The matched sentinel must be shown: an FTS5 snippet centered on the
    // match contains it; `bounded_display(&content)` (first ~512 chars, all
    // filler) does not. The blob is ~11 000 chars (well above 4 KB) so `oo cat`
    // indexes the full content and the FTS5 branch is exercised.
    assert!(
        stdout.contains("sentinel_beta_9999"),
        "default recall must display the FTS5 snippet centered on the matched token\ngot: {stdout}"
    );

    // Sanity: the output is bounded (not the full blob).
    let stdout_len = stdout.len();
    let blob_len = blob.len();
    assert!(
        stdout_len < blob_len,
        "default recall stdout ({stdout_len} B) must be < full blob ({blob_len} B)"
    );
}

// ---------------------------------------------------------------------------
// learn command
// ---------------------------------------------------------------------------

#[test]
fn test_learn_no_args() {
    // `oo learn` with no command should fail with a helpful error
    oo().arg("learn")
        .assert()
        .failure()
        .stderr(predicate::str::contains("learn requires a command"));
}

#[test]
fn test_learn_no_api_key() {
    // `oo learn echo hello` spawns a background child that checks the API key.
    // The foreground process always exits 0 (it just runs the command and detaches).
    // What we can verify: stderr mentions the learning intent for the command.
    oo().args(["learn", "echo", "hello"])
        .env_remove("ANTHROPIC_API_KEY")
        .env_remove("OPENAI_API_KEY")
        .assert()
        .success()
        .stderr(predicate::str::contains("[learning pattern for"));
}

// ---------------------------------------------------------------------------
// run command edge cases
// ---------------------------------------------------------------------------

#[test]
fn test_run_command_not_found() {
    // A command that doesn't exist should result in a failure indicator or error
    oo().args(["nonexistent_binary_xyz_abc_123"])
        .assert()
        .failure();
}

#[test]
fn test_run_multiword_command() {
    // Multiple args are passed through correctly
    oo().args(["echo", "hello", "world"])
        .assert()
        .success()
        .stdout(predicate::str::contains("hello world"));
}

#[test]
fn test_passthrough_git_version() {
    // `git --version` produces small output (< 4096 bytes) and exits 0.
    // oo must pass it through verbatim — no ✓ or ● prefix.
    // This tests the passthrough classification tier end-to-end.
    oo().args(["git", "--version"])
        .assert()
        .success()
        .stdout(predicate::str::starts_with("git version"));
}

#[test]
fn test_failure_stderr_output() {
    // `ls /nonexistent_path_xyz` should exit non-zero and show ✗ with stderr content
    oo().args(["ls", "/nonexistent_path_xyz_abc_12345"])
        .assert()
        .failure()
        .stdout(predicate::str::starts_with("\u{2717}")); // ✗
}

// ---------------------------------------------------------------------------
// parse_action dispatch (CLI integration)
// ---------------------------------------------------------------------------

#[test]
fn test_dispatch_version() {
    // `oo version` must exit 0
    oo().arg("version").assert().success();
}

#[test]
fn test_dispatch_forget() {
    // `oo forget` must exit 0 and clear session data
    oo().arg("forget")
        .assert()
        .success()
        .stdout(predicate::str::contains("Cleared session data"));
}

#[test]
fn test_dispatch_run() {
    // `oo echo hi` dispatches to run — exits 0 with command output
    oo().args(["echo", "hi"])
        .assert()
        .success()
        .stdout(predicate::str::contains("hi"));
}

#[test]
fn test_dispatch_recall_query_joined() {
    // `oo recall hello world` — multi-word query joined, exits 0
    oo().args(["recall", "hello", "world"]).assert().success();
}

#[test]
fn test_dispatch_help() {
    // `oo help` — shows usage
    oo().arg("help")
        .assert()
        .success()
        .stdout(predicate::str::contains("Usage"));
}

#[test]
fn test_dispatch_init() {
    // `oo init` — runs the init command (tested more fully in init block above)
    let dir = TempDir::new().unwrap();
    oo().arg("init").current_dir(dir.path()).assert().success();
}

// ---------------------------------------------------------------------------
// oo patterns subcommand
// ---------------------------------------------------------------------------

#[test]
fn test_patterns_no_learned_patterns() {
    // When no learned patterns exist, built-in patterns are still shown
    let dir = TempDir::new().unwrap();
    oo().arg("patterns")
        .env("HOME", dir.path())
        .env("XDG_CONFIG_HOME", dir.path().join(".config"))
        .assert()
        .success()
        .stdout(predicate::str::contains("Built-in"))
        .stdout(predicate::str::contains("User"));
}

#[test]
fn test_patterns_with_learned_pattern() {
    // When a valid pattern file exists, list it.
    // Use double_o::learn::patterns_dir() to resolve the platform-correct path
    // (macOS: ~/Library/Application Support/oo/patterns, Linux: ~/.config/oo/patterns).
    let dir = TempDir::new().unwrap();
    // Build the platform-appropriate path under our temp HOME by temporarily
    // setting HOME and querying dirs::config_dir equivalent logic.
    // We know patterns_dir() = dirs::config_dir()/oo/patterns, so replicate that.
    #[cfg(target_os = "macos")]
    let patterns_dir = dir
        .path()
        .join("Library")
        .join("Application Support")
        .join("oo")
        .join("patterns");
    #[cfg(not(target_os = "macos"))]
    let patterns_dir = dir.path().join(".config").join("oo").join("patterns");

    std::fs::create_dir_all(&patterns_dir).unwrap();
    std::fs::write(
        patterns_dir.join("pytest.toml"),
        "command_match = \"^pytest\"\n[success]\npattern = '(?P<n>\\d+) passed'\nsummary = \"{n} passed\"\n",
    )
    .unwrap();
    oo().arg("patterns")
        .env("HOME", dir.path())
        .env("XDG_CONFIG_HOME", dir.path().join(".config"))
        .assert()
        .success()
        .stdout(predicate::str::contains("^pytest"));
}

#[test]
fn test_learn_provider_logged_to_stderr() {
    // Part 1: provider name must appear in stderr before background spawn
    oo().args(["learn", "echo", "hello"])
        .env_remove("ANTHROPIC_API_KEY")
        .env_remove("OPENAI_API_KEY")
        .env_remove("CEREBRAS_API_KEY")
        .assert()
        .success()
        .stderr(predicate::str::contains("anthropic"));
}

// ---------------------------------------------------------------------------
// version / format tests
// ---------------------------------------------------------------------------

#[test]
fn test_version_shows_oo_prefix() {
    // The plain "oo" logo must appear in the version output
    oo().arg("version")
        .assert()
        .success()
        .stdout(predicate::str::starts_with("oo "));
}

#[test]
fn test_version_shows_version_number() {
    // Must contain the version from Cargo.toml
    let version = env!("CARGO_PKG_VERSION");
    oo().arg("version")
        .assert()
        .success()
        .stdout(predicate::str::contains(version));
}

// ---------------------------------------------------------------------------
// help command integration
// ---------------------------------------------------------------------------

/// Network-dependent: looks up cheat.sh for `ls`.
/// Marked ignore — run manually when network is available.
#[test]
#[ignore = "requires network access to cheat.sh"]
fn test_help_with_valid_command() {
    oo().args(["help", "ls"])
        .assert()
        .success()
        .stdout(predicate::str::is_empty().not());
}

// ---------------------------------------------------------------------------
// Large Content/Unknown passthrough repros (issue #148)
//
// These are the exact live repros from the ticket: pre-fix, `oo cat` on a
// 110KB file emitted exactly 110,000 bytes and a 30,000-echo `sh -c` emitted
// exactly 150,000 bytes verbatim into the agent's context. Each test
// self-isolates its store via OO_DATA_DIR (fresh temp dir per test) so the
// recall assertions only see the row this test indexed.
//
// The recall assertions deliberately check RETRIEVABILITY ONLY — a distinctive
// marker token embedded in the tail of the original output must return a
// NON-EMPTY recall result whose stored content contains the token. They must
// NOT assert on the shape/boundedness of `oo recall`'s printed output; that is
// issue #147's job.
// ---------------------------------------------------------------------------

/// A single, unambiguous FTS5 token — guaranteed to be a one-token query that
/// the store's per-row phrase matching can resolve (no punctuation, no spaces).
const RECALL_MARKER: &str = "oo148tailmarker7f3a9c";

/// Build a 110,000-byte file whose contents are all distinct 22-character
/// lines (positions 0..5000), each containing the RECALL_MARKER token. The
/// file is large enough to trigger the >4KB threshold, and the marker is
/// guaranteed to be a single FTS5 token in the indexed row. The file's tail
/// (last 5000 lines) also contains the marker, so it survives the
/// byte-based head+tail truncation (tail budget = 40% of 4096 ≈ 1638 bytes ≈
/// ~74 lines of 22-char lines, well within the last 5000 lines).
fn write_repro_file(dir: &std::path::Path) -> std::path::PathBuf {
    let mut content = String::new();
    for i in 0..5000 {
        content.push_str(&format!("line{i:06} {RECALL_MARKER}\n"));
    }
    let path = dir.join("big.txt");
    std::fs::write(&path, &content).unwrap();
    path
}

/// Isolate the store under a fresh temp data dir. Returns the TempDir (kept
/// alive for the duration of the test) and the data-dir path to pass to
/// OO_DATA_DIR on every oo invocation in the test.
fn isolated_store() -> (TempDir, std::path::PathBuf) {
    let dir = TempDir::new().unwrap();
    let data_dir = dir.path().join("oo-data");
    std::fs::create_dir_all(&data_dir).unwrap();
    (dir, data_dir)
}

/// Run `oo recall <marker>` against the isolated store and assert the result is
/// non-empty AND contains the marker token. Returns the recall stdout for the
/// caller to inspect if needed.
fn assert_recall_contains(data_dir: &std::path::Path, marker: &str) {
    let out = oo()
        .args(["recall", marker])
        .env("OO_DATA_DIR", data_dir)
        .assert()
        .success()
        .to_string();
    assert!(
        !out.contains("No results found"),
        "recall on the tail marker must return a non-empty result; got: {out:?}"
    );
    assert!(
        out.contains(marker),
        "recall result must contain the tail marker token; got: {out:?}"
    );
}

#[test]
fn test_content_arm_large_output_bounded_and_recallable() {
    // Repro A (Content arm): `oo cat` on a 110,000-byte file. Pre-fix this
    // emitted exactly 110,000 bytes verbatim. Post-fix: stdout must be bounded
    // far under the full size, the full content must be indexed, and a recall
    // seeded with the tail marker must return a non-empty result containing it.
    let file = TempDir::new().unwrap();
    let big = write_repro_file(file.path());

    let (_guard, data_dir) = isolated_store();
    oo().args(["cat", big.to_str().unwrap()])
        .env("OO_DATA_DIR", &data_dir)
        .assert()
        .success()
        // The pre-fix byte-exact 110,000-byte dump is the bug: bound well under it.
        // The fix caps display at 4096 bytes + one marker line; 20KB leaves a wide
        // margin over that while still being provably not the verbatim 110KB dump.
        .stdout(predicate::function(|out: &str| out.len() < 20_000))
        .stdout(predicate::str::contains(RECALL_MARKER));

    // The full content must have been indexed — recall on the tail marker
    // returns a NON-EMPTY result whose stored content contains the token.
    assert_recall_contains(&data_dir, RECALL_MARKER);
}

#[test]
fn test_unknown_arm_large_output_bounded_and_recallable() {
    // Repro B (Unknown arm): `oo sh -c '<30000 lines> + <marker line>'`.
    // Pre-fix this emitted ~150,000 bytes verbatim (30,000 x "line\n").
    // The shell loop runs first, then the marker line is appended, so the
    // marker sits in the TAIL of the output — the slice that must survive the
    // byte-based head+tail truncation to be displayed, and the whole output
    // (including the marker) is what gets indexed for recall.
    let (_guard, data_dir) = isolated_store();
    oo().args([
        "sh",
        "-c",
        &format!("for i in $(seq 1 30000); do echo line; done; echo {RECALL_MARKER}"),
    ])
    .env("OO_DATA_DIR", &data_dir)
    .assert()
    .success()
    .stdout(predicate::function(|out: &str| out.len() < 20_000))
    .stdout(predicate::str::contains(RECALL_MARKER));

    assert_recall_contains(&data_dir, RECALL_MARKER);
}

// ---------------------------------------------------------------------------
// cargo-dist configuration verification
// ---------------------------------------------------------------------------

#[test]
fn test_cargo_toml_has_shell_installer_enabled() {
    // Verify that Cargo.toml configures shell installer for cargo-dist
    let cargo_toml = include_str!("../Cargo.toml");
    assert!(
        cargo_toml.contains(r#"installers = ["shell"]"#),
        "Cargo.toml must have shell installer enabled: installers = [\"shell\"]"
    );
}

#[test]
fn test_cargo_toml_has_install_path_cargo_home() {
    // Verify that Cargo.toml configures install-path as CARGO_HOME
    let cargo_toml = include_str!("../Cargo.toml");
    assert!(
        cargo_toml.contains(r#"install-path = "CARGO_HOME""#),
        "Cargo.toml must have install-path = \"CARGO_HOME\""
    );
}

#[test]
fn test_cargo_toml_has_cargo_dist_version() {
    // Verify that Cargo.toml specifies a cargo-dist version
    let cargo_toml = include_str!("../Cargo.toml");
    assert!(
        cargo_toml.contains("cargo-dist-version ="),
        "Cargo.toml must specify cargo-dist-version for dist config"
    );
}

// ---------------------------------------------------------------------------
// project patterns (.oo/patterns)
// ---------------------------------------------------------------------------

#[test]
fn test_project_patterns_listed_when_present() {
    let dir = TempDir::new().unwrap();
    std::fs::create_dir_all(dir.path().join(".git")).unwrap();
    let pat_dir = dir.path().join(".oo").join("patterns");
    std::fs::create_dir_all(&pat_dir).unwrap();
    std::fs::write(
        pat_dir.join("mytest.toml"),
        "command_match = \"^mytest\"\n[success]\npattern = '(?P<n>\\d+) ok'\nsummary = \"{n} ok\"\n",
    )
    .unwrap();

    oo().arg("patterns")
        .current_dir(dir.path())
        .env("HOME", dir.path())
        .env("XDG_CONFIG_HOME", dir.path().join(".config"))
        .assert()
        .success()
        .stdout(predicate::str::contains("Project"))
        .stdout(predicate::str::contains("^mytest"));
}

#[test]
fn test_project_patterns_override_builtins() {
    // Create a project pattern for "echo" with a success pattern that matches
    // the output, proving project patterns are loaded and take effect.
    let dir = TempDir::new().unwrap();
    std::fs::create_dir_all(dir.path().join(".git")).unwrap();
    let pat_dir = dir.path().join(".oo").join("patterns");
    std::fs::create_dir_all(&pat_dir).unwrap();

    // A pattern that matches "echo" and summarises any output as "proj-match"
    std::fs::write(
        pat_dir.join("echo.toml"),
        "command_match = \"^echo\\\\b\"\n[success]\npattern = '(?s)(?P<all>.+)'\nsummary = \"proj-match\"\n",
    )
    .unwrap();

    // Generate enough output to exceed SMALL_THRESHOLD so the pattern is consulted.
    // SMALL_THRESHOLD is 4096 bytes; we need > 4096 bytes of output.
    let big_arg = "x".repeat(5000);
    oo().args(["echo", &big_arg])
        .current_dir(dir.path())
        .env("HOME", dir.path())
        .env("XDG_CONFIG_HOME", dir.path().join(".config"))
        .assert()
        .success()
        .stdout(predicate::str::contains("proj-match"));
}

// ---------------------------------------------------------------------------
// Savings suffix (issue #150)
// ---------------------------------------------------------------------------

/// Create a temp bin dir containing an executable script named `name` whose
/// body is `script_body`, and return the PATH with that dir prepended.
fn make_fake_bin(name: &str, script_body: &str) -> (TempDir, String) {
    let dir = TempDir::new().unwrap();
    let bin_dir = dir.path().join("bin");
    std::fs::create_dir_all(&bin_dir).unwrap();
    let bin = bin_dir.join(name);
    std::fs::write(&bin, script_body).unwrap();
    std::fs::set_permissions(&bin, std::fs::Permissions::from_mode(0o755)).unwrap();
    let path = format!(
        "{}:{}",
        bin_dir.display(),
        std::env::var("PATH").unwrap_or_default()
    );
    (dir, path)
}

/// Quiet Success: a Status-category command with >4 KB output and no matching
/// builtin success pattern produces `✓ {label}` (empty summary). The
/// `[saved …]` suffix must appear on the indicator line.
#[test]
fn test_quiet_success_shows_savings() {
    let (keep, path) = make_fake_bin(
        "go",
        "#!/bin/sh\nfor i in $(seq 1 500); do echo 'building component $i ...'; done\nexit 0\n",
    );
    let _ = keep;
    oo().args(["go", "build"])
        .env("PATH", path)
        .assert()
        .success()
        .stdout(predicate::str::starts_with("\u{2713}"))
        .stdout(predicate::str::contains("[saved "))
        // The suffix must be on the indicator line, not on subsequent lines.
        .stdout(predicate::function(|out: &str| {
            let first_line = out.lines().next().unwrap_or("");
            first_line.starts_with('\u{2713}') && first_line.contains("[saved ")
        }));
}

/// Pattern-summarised Success: >4 KB output matching a builtin pattern with a
/// non-empty summary → `✓ {label} ({summary}) [saved …]`.
#[test]
fn test_summary_success_shows_savings() {
    let (keep, path) = make_fake_bin(
        "cargo",
        "#!/bin/sh\nfor i in $(seq 1 500); do echo \"running test $i ...\"; done\necho \"test result: ok. 42 passed; 0 failed; finished in 1.23s\"\nexit 0\n",
    );
    let _ = keep;
    oo().args(["cargo", "test"])
        .env("PATH", path)
        .assert()
        .success()
        .stdout(predicate::str::starts_with("\u{2713}"))
        .stdout(predicate::str::contains("42 passed"))
        .stdout(predicate::str::contains("[saved "))
        .stdout(predicate::function(|out: &str| {
            let first_line = out.lines().next().unwrap_or("");
            first_line.starts_with('\u{2713}') && first_line.contains("[saved ")
        }));
}

/// Filtered Failure: >4 KB output, non-zero exit. The `[saved …]` suffix must
/// appear on the `✗` indicator line; the filtered output lines that follow
/// must NOT contain the suffix.
#[test]
fn test_failure_shows_savings() {
    let (keep, path) = make_fake_bin(
        "pytest",
        "#!/bin/sh\nfor i in $(seq 1 500); do echo \"ERROR: test_failing_$i failed with assertion error\"; done\nexit 1\n",
    );
    let _ = keep;
    oo().args(["pytest", "-x"])
        .env("PATH", path)
        .assert()
        .failure()
        .stdout(predicate::str::starts_with("\u{2717}"))
        .stdout(predicate::str::contains("[saved "))
        .stdout(predicate::function(|out: &str| {
            let first_line = out.lines().next().unwrap_or("");
            first_line.starts_with('\u{2717}') && first_line.contains("[saved ")
        }));
}

/// REGRESSION LOCK — pre-existing failure formatting: the `✗ {label}`
/// indicator line is followed by a deliberate BLANK LINE before the error
/// body (the `\n` in the payload plus `println!`'s own newline). This must
/// survive both with and without a savings suffix present.
#[test]
fn test_failure_indicator_blank_line_with_suffix() {
    let (keep, path) = make_fake_bin(
        "pytest",
        "#!/bin/sh\nfor i in $(seq 1 500); do echo \"ERROR: test_failing_$i failed with assertion error\"; done\nexit 1\n",
    );
    let _ = keep;
    oo().args(["pytest", "-x"])
        .env("PATH", path)
        .assert()
        .failure()
        .stdout(predicate::function(|out: &str| {
            let mut lines = out.lines();
            let indicator = lines.next().unwrap_or("");
            let blank = lines.next().unwrap_or("");
            indicator.starts_with('\u{2717}') && indicator.contains("[saved ") && blank.is_empty()
        }));
}

/// Same regression as above for the small-failure case where the savings are
/// below `MIN_SAVINGS` and no suffix is printed: the blank line still holds.
#[test]
fn test_failure_indicator_blank_line_without_suffix() {
    oo().args(["false"])
        .assert()
        .failure()
        .stdout(predicate::function(|out: &str| {
            let mut lines = out.lines();
            let indicator = lines.next().unwrap_or("");
            let blank = lines.next().unwrap_or("");
            indicator.starts_with('\u{2717}') && !indicator.contains("[saved ") && blank.is_empty()
        }));
}

/// Large arm regression lock: the `● {label} (indexed … → use `oo recall` to
/// query)` string is unchanged — no `[saved …]` suffix (it already reports
/// its size; no double-reporting).
#[test]
fn test_large_arm_wording_unchanged_no_savings() {
    let dir = TempDir::new().unwrap();
    Command::new("git")
        .args(["init"])
        .current_dir(dir.path())
        .output()
        .unwrap();
    for i in 0..100 {
        std::fs::write(dir.path().join("file.txt"), format!("content {}\n", i)).unwrap();
        Command::new("git")
            .args(["add", "."])
            .current_dir(dir.path())
            .output()
            .unwrap();
        Command::new("git")
            .args(["commit", "-m", &format!("commit {}", i)])
            .env("GIT_AUTHOR_NAME", "Test")
            .env("GIT_AUTHOR_EMAIL", "test@example.com")
            .env("GIT_COMMITTER_NAME", "Test")
            .env("GIT_COMMITTER_EMAIL", "test@example.com")
            .current_dir(dir.path())
            .output()
            .unwrap();
    }

    oo().args(["git", "log"])
        .current_dir(dir.path())
        .assert()
        .success()
        .stdout(predicate::str::starts_with("\u{25CF}"))
        .stdout(predicate::str::contains("indexed"))
        .stdout(predicate::str::contains("use `oo recall` to query"))
        .stdout(predicate::str::contains("[saved ").not());
}

/// Bounded arm regression lock: the `● {label} (output truncated: …)` line is
/// unchanged — no `[saved …]` suffix (its framing line already carries the
/// total size; no double-reporting).
#[test]
fn test_bounded_arm_wording_unchanged_no_savings() {
    let dir = TempDir::new().unwrap();
    let big_file = dir.path().join("big.txt");
    let content = std::iter::repeat("x\n").take(5000).collect::<String>();
    std::fs::write(&big_file, &content).unwrap();

    oo().args(["cat", big_file.to_str().unwrap()])
        .assert()
        .success()
        .stdout(predicate::str::starts_with("\u{25CF}"))
        .stdout(predicate::str::contains("output truncated:"))
        .stdout(predicate::str::contains("use `oo recall` to query"))
        .stdout(predicate::str::contains("[saved ").not());
}

/// Passthrough arm: small output (< 4 KB) passes through verbatim with no
/// indicator line and no savings figure.
#[test]
fn test_passthrough_no_savings() {
    oo().args(["echo", "hello"])
        .assert()
        .success()
        .stdout("hello\n");
}

// ---------------------------------------------------------------------------
// clap --help / -h surface (issue #163, ws-clap-help)
//
// The doc-comment on the `args` field of the Cli derive in src/main.rs
// controls both surfaces — clap auto-wires -h from the same doc-comment.
// We assert on the two subcommands that were actually missing from the
// old doc-comment (init, patterns), plus one description phrase per
// subcommand, plus the tool description. `help`/`version` bare-word
// assertions are avoided — they match the Options section lines
// (`-h, --help` / `-V, --version`) and would be vacuous on this surface.
// ---------------------------------------------------------------------------

/// All 7 reserved subcommand names appear in `oo --help` output with a
/// one-line description alongside each. `init` and `patterns` are the two
/// names missing from the old doc-comment — those assertions are the ones
/// that would fail on the pre-fix binary.
#[test]
fn test_clap_help_lists_all_subcommands() {
    oo().arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "Context-efficient command runner for AI coding agents",
        ))
        .stdout(predicate::str::contains("recall ("))
        .stdout(predicate::str::contains("forget ("))
        .stdout(predicate::str::contains("learn ("))
        .stdout(predicate::str::contains("help ("))
        .stdout(predicate::str::contains("init ("))
        .stdout(predicate::str::contains("patterns ("))
        .stdout(predicate::str::contains("version ("));
}

/// Same assertions on the short-flag form — clap emits both from the same
/// doc-comment, so -h must not silently drift from --help.
#[test]
fn test_clap_short_help_lists_all_subcommands() {
    oo().arg("-h")
        .assert()
        .success()
        .stdout(predicate::str::contains("Context-efficient command runner"))
        .stdout(predicate::str::contains("recall ("))
        .stdout(predicate::str::contains("forget ("))
        .stdout(predicate::str::contains("learn ("))
        .stdout(predicate::str::contains("help ("))
        .stdout(predicate::str::contains("init ("))
        .stdout(predicate::str::contains("patterns ("))
        .stdout(predicate::str::contains("version ("));
}