path-cli 0.12.0

CLI for deriving, querying, and visualizing Toolpath provenance (binary: path)
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
use assert_cmd::Command;
use predicates::prelude::*;
use std::path::PathBuf;

fn examples_dir() -> PathBuf {
    PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .join("..")
        .join("..")
        .join("examples")
}

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

// ── Git fixture ──────────────────────────────────────────────────────

/// Creates a temporary git repo with a known commit history for testing.
///
/// Layout (all on branch `main`):
///   commit 1: "initial commit"  — creates main.rs with "fn main() {}"
///   commit 2: "fix the bug"     — changes main.rs to "fn main() { fixed() }"
///
/// Returns (temp_dir, branch_name). Temp dir must be kept alive for the
/// repo to remain on disk.
fn git_fixture() -> (tempfile::TempDir, String) {
    let dir = tempfile::tempdir().unwrap();
    let repo = git2::Repository::init(dir.path()).unwrap();

    let mut config = repo.config().unwrap();
    config.set_str("user.name", "Alice Dev").unwrap();
    config.set_str("user.email", "alice@example.com").unwrap();

    // Commit 1
    let mut index = repo.index().unwrap();
    std::fs::write(dir.path().join("main.rs"), "fn main() {}").unwrap();
    index.add_path(std::path::Path::new("main.rs")).unwrap();
    index.write().unwrap();
    let tree1 = repo.find_tree(index.write_tree().unwrap()).unwrap();
    let sig = repo.signature().unwrap();
    let oid1 = repo
        .commit(Some("HEAD"), &sig, &sig, "initial commit", &tree1, &[])
        .unwrap();
    let commit1 = repo.find_commit(oid1).unwrap();

    // Commit 2
    std::fs::write(dir.path().join("main.rs"), "fn main() { fixed() }").unwrap();
    index.add_path(std::path::Path::new("main.rs")).unwrap();
    index.write().unwrap();
    let tree2 = repo.find_tree(index.write_tree().unwrap()).unwrap();
    repo.commit(Some("HEAD"), &sig, &sig, "fix the bug", &tree2, &[&commit1])
        .unwrap();

    // Determine the branch name (main or master depending on git config)
    let head = repo.head().unwrap();
    let branch = head.shorthand().unwrap().to_string();

    (dir, branch)
}

// ── Validate ─────────────────────────────────────────────────────────

#[test]
fn validate_valid_step() {
    cmd()
        .args(["p", "validate"])
        .arg("--input")
        .arg(examples_dir().join("step-01-minimal.json"))
        .assert()
        .success()
        .stdout(predicate::str::contains("Valid"));
}

#[test]
fn validate_invalid_json() {
    let dir = std::env::temp_dir();
    let tmp_file = dir.join("toolpath-integration-invalid.json");
    std::fs::write(&tmp_file, "{ not valid json }").unwrap();

    cmd()
        .args(["p", "validate"])
        .arg("--input")
        .arg(&tmp_file)
        .assert()
        .failure();

    let _ = std::fs::remove_file(&tmp_file);
}

// ── Derive git ───────────────────────────────────────────────────────

#[test]
fn derive_git_produces_path() {
    let (dir, branch) = git_fixture();

    cmd()
        .args(["p", "derive"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .assert()
        .success()
        .stdout(predicate::str::contains("\"graph\":"))
        .stdout(predicate::str::contains("\"paths\":"))
        .stdout(predicate::str::contains("\"head\":"))
        .stdout(predicate::str::contains("\"steps\""));
}

#[test]
fn derive_git_has_correct_actor() {
    let (dir, branch) = git_fixture();

    let output = cmd()
        .args(["p", "derive"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .arg("--pretty")
        .output()
        .unwrap();
    assert!(output.status.success());

    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    let path = &json["paths"][0];

    // Actor is derived from git author email username (alice@example.com → alice)
    let step = &path["steps"][0];
    assert_eq!(step["step"]["actor"], "human:alice");

    // Actor metadata in path.meta.actors
    let actors = &path["meta"]["actors"];
    let alice = &actors["human:alice"];
    assert_eq!(alice["name"], "Alice Dev");
    assert_eq!(alice["identities"][0]["id"], "alice@example.com");
}

#[test]
fn derive_git_has_change_with_diff() {
    let (dir, branch) = git_fixture();

    let output = cmd()
        .args(["p", "derive"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .arg("--pretty")
        .output()
        .unwrap();
    assert!(output.status.success());

    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    let step = &json["paths"][0]["steps"][0];

    // The step should have a change for main.rs with a raw diff
    let change = &step["change"]["main.rs"];
    let raw = change["raw"].as_str().unwrap();
    assert!(
        raw.contains("-fn main() {}"),
        "diff should show old content"
    );
    assert!(
        raw.contains("+fn main() { fixed() }"),
        "diff should show new content"
    );
}

#[test]
fn derive_git_has_intent_from_commit_message() {
    let (dir, branch) = git_fixture();

    let output = cmd()
        .args(["p", "derive"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .arg("--pretty")
        .output()
        .unwrap();
    assert!(output.status.success());

    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    let step = &json["paths"][0]["steps"][0];

    // meta.intent is the commit message
    assert_eq!(step["meta"]["intent"], "fix the bug");
}

#[test]
fn derive_git_has_base_uri() {
    let (dir, branch) = git_fixture();

    let output = cmd()
        .args(["p", "derive"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .arg("--pretty")
        .output()
        .unwrap();
    assert!(output.status.success());

    let json: serde_json::Value = serde_json::from_slice(&output.stdout).unwrap();
    let base = &json["paths"][0]["path"]["base"];

    // base.uri should be a file:// URL pointing to the repo
    let uri = base["uri"].as_str().unwrap();
    assert!(
        uri.starts_with("file://"),
        "Expected file:// URI, got {}",
        uri
    );

    // base.ref should be a commit hash (40 hex chars)
    let git_ref = base["ref"].as_str().unwrap();
    assert_eq!(git_ref.len(), 40);
    assert!(git_ref.chars().all(|c| c.is_ascii_hexdigit()));
}

// ── Derive → validate roundtrip ─────────────────────────────────────

#[test]
fn derive_git_validate_roundtrip() {
    let (dir, branch) = git_fixture();
    let tmp_file = std::env::temp_dir().join("toolpath-integration-roundtrip.json");

    let derive_output = cmd()
        .args(["p", "derive"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .output()
        .unwrap();
    assert!(derive_output.status.success());
    std::fs::write(&tmp_file, &derive_output.stdout).unwrap();

    cmd()
        .args(["p", "validate"])
        .arg("--input")
        .arg(&tmp_file)
        .assert()
        .success()
        .stdout(predicate::str::contains("Valid"));

    let _ = std::fs::remove_file(&tmp_file);
}

// ── Render ───────────────────────────────────────────────────────────

#[test]
fn render_dot_from_stdin() {
    let input = std::fs::read_to_string(examples_dir().join("path-01-pr.path.json")).unwrap();

    cmd()
        .args(["p", "render"])
        .arg("dot")
        .write_stdin(input)
        .assert()
        .success()
        .stdout(predicate::str::contains("digraph"));
}

// ── Query ────────────────────────────────────────────────────────────

#[test]
fn query_dead_ends() {
    cmd()
        .arg("query")
        .arg("dead-ends")
        .arg("--input")
        .arg(examples_dir().join("path-01-pr.path.json"))
        .assert()
        .success()
        .stdout(predicate::str::contains("step-002a"));
}

#[test]
fn query_ancestors() {
    cmd()
        .arg("query")
        .arg("ancestors")
        .arg("--input")
        .arg(examples_dir().join("path-01-pr.path.json"))
        .arg("--step-id")
        .arg("step-004")
        .assert()
        .success()
        .stdout(predicate::str::contains("step-001"))
        .stdout(predicate::str::contains("step-004"));
}

// ── Merge ────────────────────────────────────────────────────────────

#[test]
fn merge_produces_graph() {
    cmd()
        .args(["p", "merge"])
        .arg(examples_dir().join("path-01-pr.path.json"))
        .arg(examples_dir().join("path-02-local-session.path.json"))
        .assert()
        .success()
        .stdout(predicate::str::contains("\"graph\":"))
        .stdout(predicate::str::contains("\"paths\":"));
}

// ── .path.jsonl input ────────────────────────────────────────────────

#[test]
fn validate_accepts_path_jsonl() {
    cmd()
        .args(["p", "validate"])
        .arg("--input")
        .arg(examples_dir().join("path-02-local-session.path.jsonl"))
        .assert()
        .success()
        .stdout(predicate::str::contains("Valid: Graph"));
}

#[test]
fn validate_rejects_truncated_jsonl() {
    let mut f = tempfile::Builder::new()
        .suffix(".path.jsonl")
        .tempfile()
        .unwrap();
    // No PathOpen, just garbage.
    use std::io::Write;
    writeln!(f, r#"{{"Step":"garbage"}}"#).unwrap();
    f.flush().unwrap();

    cmd()
        .args(["p", "validate"])
        .arg("--input")
        .arg(f.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Invalid"));
}

#[test]
fn render_md_accepts_path_jsonl() {
    cmd()
        .args(["p", "render"])
        .arg("md")
        .arg("--input")
        .arg(examples_dir().join("path-03-signed-pr.path.jsonl"))
        .assert()
        .success()
        .stdout(predicate::str::is_empty().not());
}

#[test]
fn query_dead_ends_accepts_path_jsonl() {
    cmd()
        .arg("query")
        .arg("dead-ends")
        .arg("--input")
        .arg(examples_dir().join("path-04-exploration.path.jsonl"))
        .assert()
        .success();
}

#[test]
fn merge_accepts_path_jsonl() {
    cmd()
        .args(["p", "merge"])
        .arg(examples_dir().join("path-01-pr.path.jsonl"))
        .arg(examples_dir().join("path-02-local-session.path.jsonl"))
        .assert()
        .success()
        .stdout(predicate::str::contains("\"graph\":"))
        .stdout(predicate::str::contains("\"paths\":"));
}

// ── Auth ─────────────────────────────────────────────────────────────

#[test]
fn auth_help_lists_subcommands() {
    cmd()
        .arg("auth")
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("login"))
        .stdout(predicate::str::contains("logout"))
        .stdout(predicate::str::contains("status"))
        .stdout(predicate::str::contains("whoami"));
}

#[test]
fn auth_login_against_unreachable_url_errors() {
    // Port 1 is privileged and not bound to anything — connection refused.
    cmd()
        .arg("auth")
        .arg("login")
        .arg("--url")
        .arg("http://127.0.0.1:1")
        .arg("--code")
        .arg("BCDFGHJK")
        .assert()
        .failure()
        .stderr(predicate::str::contains("127.0.0.1"));
}

// ── Import / export / cache ─────────────────────────────────────────

#[test]
fn import_help_lists_sources_including_pathbase() {
    cmd()
        .args(["p", "import"])
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("git"))
        .stdout(predicate::str::contains("github"))
        .stdout(predicate::str::contains("claude"))
        .stdout(predicate::str::contains("pathbase"));
}

#[test]
fn export_help_lists_claude_and_pathbase() {
    cmd()
        .args(["p", "export"])
        .arg("--help")
        .assert()
        .success()
        .stdout(predicate::str::contains("claude"))
        .stdout(predicate::str::contains("pathbase"));
}

#[test]
fn import_git_no_cache_emits_stdout_json() {
    let (dir, branch) = git_fixture();

    cmd()
        .args(["p", "import"])
        .arg("git")
        .arg("--no-cache")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .assert()
        .success()
        .stdout(predicate::str::contains("\"graph\":"))
        .stdout(predicate::str::contains("\"paths\":"))
        .stdout(predicate::str::contains("\"steps\""));
}

#[test]
fn import_git_writes_cache_and_prints_path() {
    let (dir, branch) = git_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import"])
        .arg("git")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .assert()
        .success()
        .stdout(predicate::str::contains(".json"))
        .stderr(predicate::str::contains("Imported"));
}

#[test]
fn import_git_errors_on_existing_cache_without_force() {
    let (dir, branch) = git_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "git", "--branch"])
        .arg(&branch)
        .arg("--repo")
        .arg(dir.path())
        .assert()
        .success();

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "git", "--branch"])
        .arg(&branch)
        .arg("--repo")
        .arg(dir.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("already exists"));

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "git", "--force", "--branch"])
        .arg(&branch)
        .arg("--repo")
        .arg(dir.path())
        .assert()
        .success();
}

#[test]
fn cache_ls_on_empty_directory_prints_hint() {
    let cfg = tempfile::tempdir().unwrap();
    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "cache", "ls"])
        .assert()
        .success()
        .stderr(predicate::str::contains("No cached"));
}

#[test]
fn cache_ls_after_import_lists_entry() {
    let (dir, branch) = git_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "git", "--branch"])
        .arg(&branch)
        .arg("--repo")
        .arg(dir.path())
        .assert()
        .success();

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "cache", "ls"])
        .assert()
        .success()
        .stdout(predicate::str::contains("git-"));
}

#[test]
fn export_pathbase_repo_flag_requires_login() {
    // `export pathbase` without --repo falls through to the anonymous
    // endpoint; --repo is the explicitly-authenticated path, so it must
    // refuse without credentials.
    let cfg = tempfile::tempdir().unwrap();
    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "p",
            "export",
            "pathbase",
            "--repo",
            "alex/pathstash",
            "--url",
            "http://127.0.0.1:1",
            "--input",
        ])
        .arg(examples_dir().join("path-01-pr.path.json"))
        .assert()
        .failure()
        .stderr(predicate::str::contains("Not logged in"));
}

#[test]
fn import_pathbase_rejects_legacy_trace_id() {
    // The old `/traces/<id>` shape is gone; passing a bare token that
    // isn't an `<owner>/<repo>/<uuid>` triple should fail at parse time
    // with a clear message rather than blowing up downstream.
    let cfg = tempfile::tempdir().unwrap();
    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "pathbase", "trc_nonexistent"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("<owner>/<repo>/<uuid>"));
}

#[test]
fn import_git_no_cache_honors_global_pretty() {
    let (dir, branch) = git_fixture();

    let output = cmd()
        .arg("--pretty")
        .args(["p", "import"])
        .arg("git")
        .arg("--no-cache")
        .arg("--repo")
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .output()
        .unwrap();
    assert!(output.status.success());
    let stdout = String::from_utf8(output.stdout).unwrap();
    // Pretty JSON always has multi-line indentation; compact JSON never does.
    assert!(
        stdout.contains("\n  "),
        "expected pretty-printed JSON, got: {stdout}"
    );
}

#[test]
fn import_git_two_repos_on_same_branch_have_distinct_cache_ids() {
    let (dir_a, branch) = git_fixture();
    let (dir_b, _) = git_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "git", "--branch"])
        .arg(&branch)
        .arg("--repo")
        .arg(dir_a.path())
        .assert()
        .success();

    // Second import from a different repo on the same branch must NOT
    // trigger the "cache entry already exists" collision.
    cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "import", "git", "--branch"])
        .arg(&branch)
        .arg("--repo")
        .arg(dir_b.path())
        .assert()
        .success();

    let ls = cmd()
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["p", "cache", "ls"])
        .output()
        .unwrap();
    let stdout = String::from_utf8(ls.stdout).unwrap();
    let git_entries = stdout.lines().filter(|l| l.starts_with("git-")).count();
    assert_eq!(
        git_entries, 2,
        "expected two distinct git- cache entries, got:\n{stdout}"
    );
}

// ── `path p derive` regression guard ────────────────────────────────

#[test]
fn p_derive_is_first_class_and_warns_no_one() {
    // `path p derive` is the canonical home of the stdout-JSON derive
    // surface — it must produce the document on stdout AND keep stderr
    // clean. Regression guard: if we ever start printing a deprecation
    // notice through this path, this test breaks loudly.
    let (dir, branch) = git_fixture();
    cmd()
        .args(["p", "derive", "git", "--repo"])
        .arg(dir.path())
        .arg("--branch")
        .arg(&branch)
        .assert()
        .success()
        .stdout(predicate::str::contains("\"graph\":"))
        .stderr(predicate::str::is_empty());
}

#[test]
fn share_help_lists_unified_picker_flags() {
    cmd()
        .args(["share", "--help"])
        .assert()
        .success()
        .stdout(predicate::str::contains("--harness"))
        .stdout(predicate::str::contains("--session"))
        .stdout(predicate::str::contains("--project"))
        .stdout(predicate::str::contains("--anon"));
}

#[test]
fn share_explicit_args_uploads_via_anon() {
    use std::io::Write;
    use std::net::TcpListener;

    // Stand up a one-shot mock that returns a valid AnonUploadResponse.
    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        // Drain the request just enough to keep the OS happy.
        use std::io::Read;
        let mut buf = [0u8; 4096];
        let _ = stream.read(&mut buf);
        let body = r#"{"id":"fe94b6f9-b0af-4cdd-b9ca-3c9a2a697537","repo_id":"00000000-0000-0000-0000-000000000002","toolpath_id":"tp-1","document":{"graph":{"id":"g"},"paths":[]},"path_count":0,"url":"https://example.test/anon/abc-123","visibility":"unlisted","created_at":"2024-01-01T00:00:00Z","updated_at":"2024-01-01T00:00:00Z"}"#;
        let resp = format!(
            "HTTP/1.1 201 Created\r\nContent-Length: {}\r\nContent-Type: application/json\r\n\r\n{}",
            body.len(),
            body
        );
        let _ = stream.write_all(resp.as_bytes());
    });

    // Build a claude fixture so the explicit-args path has something to derive.
    let temp = tempfile::tempdir().unwrap();
    let project = temp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();
    let claude_dir = temp.path().join(".claude");
    // toolpath-claude maps '/', '_', and '.' to '-' when sanitizing project
    // paths into directory slugs — mirror that here so the fixture lands
    // where the resolver looks for it.
    let project_slug = project
        .to_string_lossy()
        .replace([std::path::MAIN_SEPARATOR, '_', '.'], "-");
    let project_dir = claude_dir.join("projects").join(&project_slug);
    std::fs::create_dir_all(&project_dir).unwrap();
    std::fs::write(
        project_dir.join("session-abc.jsonl"),
        format!(
            r#"{{"type":"user","uuid":"u-1","timestamp":"2024-01-01T00:00:00Z","cwd":"{cwd}","message":{{"role":"user","content":"hi"}}}}
{{"type":"assistant","uuid":"a-1","timestamp":"2024-01-01T00:00:01Z","message":{{"role":"assistant","content":"hello"}}}}
"#,
            cwd = project.display()
        ),
    )
    .unwrap();

    let cfg = tempfile::tempdir().unwrap();
    cmd()
        .env("HOME", temp.path())
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "share",
            "--harness",
            "claude",
            "--session",
            "session-abc",
            "--project",
        ])
        .arg(&project)
        .args(["--anon", "--no-cache", "--url"])
        .arg(format!("http://127.0.0.1:{port}"))
        .assert()
        .success()
        .stdout(predicate::str::contains(
            "https://example.test/anon/abc-123",
        ))
        .stderr(predicate::str::contains("Uploaded"));

    server.join().unwrap();
}

/// Helper for the cache tests. Spawns a one-shot mock anon-upload server
/// on a free port and returns (port, server-thread-handle, fixture-temp,
/// project-path, $HOME-path).
fn share_anon_fixture() -> (
    u16,
    std::thread::JoinHandle<()>,
    tempfile::TempDir,
    PathBuf,
    PathBuf,
) {
    use std::io::{Read, Write};
    use std::net::TcpListener;

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut buf = [0u8; 4096];
        let _ = stream.read(&mut buf);
        let body = r#"{"id":"fe94b6f9-b0af-4cdd-b9ca-3c9a2a697537","repo_id":"00000000-0000-0000-0000-000000000002","toolpath_id":"tp-1","document":{"graph":{"id":"g"},"paths":[]},"path_count":0,"url":"https://example.test/anon/abc","visibility":"unlisted","created_at":"2024-01-01T00:00:00Z","updated_at":"2024-01-01T00:00:00Z"}"#;
        let resp = format!(
            "HTTP/1.1 201 Created\r\nContent-Length: {}\r\nContent-Type: application/json\r\n\r\n{}",
            body.len(),
            body
        );
        let _ = stream.write_all(resp.as_bytes());
    });

    let temp = tempfile::tempdir().unwrap();
    let project = temp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();
    let claude_dir = temp.path().join(".claude");
    // toolpath-claude maps '/', '_', and '.' to '-' when sanitizing project
    // paths into directory slugs — mirror that here so the fixture lands
    // where the resolver looks for it.
    let project_slug = project
        .to_string_lossy()
        .replace([std::path::MAIN_SEPARATOR, '_', '.'], "-");
    let project_dir = claude_dir.join("projects").join(&project_slug);
    std::fs::create_dir_all(&project_dir).unwrap();
    std::fs::write(
        project_dir.join("session-abc.jsonl"),
        format!(
            r#"{{"type":"user","uuid":"u-1","timestamp":"2024-01-01T00:00:00Z","cwd":"{cwd}","message":{{"role":"user","content":"hi"}}}}
{{"type":"assistant","uuid":"a-1","timestamp":"2024-01-01T00:00:01Z","message":{{"role":"assistant","content":"hello"}}}}
"#,
            cwd = project.display()
        ),
    )
    .unwrap();

    let home = temp.path().to_path_buf();
    (port, server, temp, project, home)
}

/// Spawn a one-shot mock anon-upload server on a free port. Returns the
/// port and the join handle. Used by tests that need multiple sequential
/// uploads (the default fixture builds the claude session too, which we
/// don't want to redo between runs).
fn one_shot_anon_server() -> (u16, std::thread::JoinHandle<()>) {
    use std::io::{Read, Write};
    use std::net::TcpListener;

    let listener = TcpListener::bind("127.0.0.1:0").unwrap();
    let port = listener.local_addr().unwrap().port();
    let server = std::thread::spawn(move || {
        let (mut stream, _) = listener.accept().unwrap();
        let mut buf = [0u8; 4096];
        let _ = stream.read(&mut buf);
        let body = r#"{"id":"fe94b6f9-b0af-4cdd-b9ca-3c9a2a697537","repo_id":"00000000-0000-0000-0000-000000000002","toolpath_id":"tp-1","document":{"graph":{"id":"g"},"paths":[]},"path_count":0,"url":"https://example.test/anon/abc","visibility":"unlisted","created_at":"2024-01-01T00:00:00Z","updated_at":"2024-01-01T00:00:00Z"}"#;
        let resp = format!(
            "HTTP/1.1 201 Created\r\nContent-Length: {}\r\nContent-Type: application/json\r\n\r\n{}",
            body.len(),
            body
        );
        let _ = stream.write_all(resp.as_bytes());
    });
    (port, server)
}

/// `path share` re-run after a conversation has grown should overwrite
/// the cache file with the fresh derive — otherwise the cache and the
/// uploaded body would disagree (upload uses the in-memory fresh body,
/// cache file would be stale). Lock that contract in.
#[test]
fn share_rewrites_cache_when_session_has_grown() {
    let temp = tempfile::tempdir().unwrap();
    let project = temp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();
    let claude_dir = temp.path().join(".claude");
    let project_slug = project
        .to_string_lossy()
        .replace([std::path::MAIN_SEPARATOR, '_', '.'], "-");
    let project_dir = claude_dir.join("projects").join(&project_slug);
    std::fs::create_dir_all(&project_dir).unwrap();
    let session_file = project_dir.join("session-grow.jsonl");
    let cwd_str = project.display().to_string();
    let initial = format!(
        r#"{{"type":"user","uuid":"u-1","timestamp":"2024-01-01T00:00:00Z","cwd":"{cwd_str}","message":{{"role":"user","content":"first"}}}}
{{"type":"assistant","uuid":"a-1","timestamp":"2024-01-01T00:00:01Z","message":{{"role":"assistant","content":"reply-1"}}}}
"#
    );
    std::fs::write(&session_file, &initial).unwrap();

    let cfg = tempfile::tempdir().unwrap();
    let home = temp.path();

    // First share: cache picks up the 2-turn conversation.
    let (port1, server1) = one_shot_anon_server();
    cmd()
        .env("HOME", home)
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "share",
            "--harness",
            "claude",
            "--session",
            "session-grow",
            "--project",
        ])
        .arg(&project)
        .args(["--anon", "--url"])
        .arg(format!("http://127.0.0.1:{port1}"))
        .assert()
        .success();
    server1.join().unwrap();

    let docs = cfg.path().join("documents");
    let cache_files: Vec<_> = std::fs::read_dir(&docs)
        .unwrap()
        .filter_map(|e| e.ok())
        .collect();
    assert_eq!(
        cache_files.len(),
        1,
        "expected one cache entry after first share"
    );
    let cache_path = cache_files[0].path();
    let cache_v1 = std::fs::read_to_string(&cache_path).unwrap();
    assert!(
        cache_v1.contains("reply-1"),
        "v1 cache must contain reply-1"
    );
    assert!(
        !cache_v1.contains("reply-2"),
        "v1 cache must not contain reply-2 yet"
    );

    // Conversation continues: append two more turns to the session JSONL.
    let mut grown = initial.clone();
    grown.push_str(&format!(
        r#"{{"type":"user","uuid":"u-2","timestamp":"2024-01-02T00:00:00Z","cwd":"{cwd_str}","message":{{"role":"user","content":"second"}}}}
{{"type":"assistant","uuid":"a-2","timestamp":"2024-01-02T00:00:01Z","message":{{"role":"assistant","content":"reply-2"}}}}
"#
    ));
    std::fs::write(&session_file, &grown).unwrap();

    // Second share: must overwrite the cache file with the grown derive,
    // not silently keep the v1 contents while uploading v2.
    let (port2, server2) = one_shot_anon_server();
    cmd()
        .env("HOME", home)
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "share",
            "--harness",
            "claude",
            "--session",
            "session-grow",
            "--project",
        ])
        .arg(&project)
        .args(["--anon", "--url"])
        .arg(format!("http://127.0.0.1:{port2}"))
        .assert()
        .success();
    server2.join().unwrap();

    let cache_v2 = std::fs::read_to_string(&cache_path).unwrap();
    assert!(
        cache_v2.contains("reply-2"),
        "v2 cache should contain the new turn, got: {cache_v2}"
    );
    assert_ne!(
        cache_v1, cache_v2,
        "cache file must be rewritten when the session has grown"
    );
}

#[test]
fn share_writes_cache_by_default() {
    let (port, server, _temp, project, home) = share_anon_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("HOME", &home)
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "share",
            "--harness",
            "claude",
            "--session",
            "session-abc",
            "--project",
        ])
        .arg(&project)
        .args(["--anon", "--url"])
        .arg(format!("http://127.0.0.1:{port}"))
        .assert()
        .success();

    let docs = cfg.path().join("documents");
    let entries: Vec<_> = std::fs::read_dir(&docs)
        .unwrap()
        .filter_map(|e| e.ok())
        .collect();
    assert_eq!(
        entries.len(),
        1,
        "expected exactly one cache entry, got {entries:?}"
    );
    let name = entries[0].file_name().to_string_lossy().into_owned();
    assert!(
        name.starts_with("claude-"),
        "expected claude-* cache id, got {name}"
    );

    server.join().unwrap();
}

#[test]
fn share_no_cache_skips_write() {
    let (port, server, _temp, project, home) = share_anon_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("HOME", &home)
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "share",
            "--harness",
            "claude",
            "--session",
            "session-abc",
            "--project",
        ])
        .arg(&project)
        .args(["--anon", "--no-cache", "--url"])
        .arg(format!("http://127.0.0.1:{port}"))
        .assert()
        .success();

    let docs = cfg.path().join("documents");
    if docs.exists() {
        let entries: Vec<_> = std::fs::read_dir(&docs)
            .unwrap()
            .filter_map(|e| e.ok())
            .collect();
        assert!(
            entries.is_empty(),
            "expected no cache entries with --no-cache, got {entries:?}"
        );
    }

    server.join().unwrap();
}

#[test]
fn share_logged_out_anon_default() {
    // No --anon flag and no credentials file => share() falls through to the
    // anonymous endpoint and emits a "not logged in — uploading anonymously"
    // notice on stderr. This covers the logged-out branch in
    // cmd_export::run_pathbase_inner that the explicit --anon tests skip.
    let (port, server, _temp, project, home) = share_anon_fixture();
    let cfg = tempfile::tempdir().unwrap();

    cmd()
        .env("HOME", &home)
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args([
            "share",
            "--harness",
            "claude",
            "--session",
            "session-abc",
            "--project",
        ])
        .arg(&project)
        .args(["--no-cache", "--url"])
        .arg(format!("http://127.0.0.1:{port}"))
        .assert()
        .success()
        .stderr(predicate::str::contains("not logged in"))
        .stderr(predicate::str::contains("uploading anonymously"));

    server.join().unwrap();
}

#[test]
fn share_filters_by_project_with_no_matches_errors() {
    let cfg = tempfile::tempdir().unwrap();
    let home = tempfile::tempdir().unwrap();
    let nonexistent = home.path().join("never");

    cmd()
        .env("HOME", home.path())
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["share", "--project"])
        .arg(&nonexistent)
        .assert()
        .failure()
        .stderr(predicate::str::contains(
            "No agent sessions found in project",
        ));
}

#[test]
fn share_no_harness_non_tty_prints_recipe() {
    // Build a minimal claude fixture in a tempdir, point HOME at it, so
    // gather_sessions returns a non-empty Vec. Without this, an environment
    // with no agent harnesses configured (e.g. CI) would hit bail_no_sessions
    // before the fzf-unavailable recipe path. We want the recipe path here.
    let temp = tempfile::tempdir().unwrap();
    let project = temp.path().join("proj");
    std::fs::create_dir_all(&project).unwrap();
    let claude_dir = temp.path().join(".claude");
    let project_slug = project
        .to_string_lossy()
        .replace([std::path::MAIN_SEPARATOR, '_', '.'], "-");
    let project_dir = claude_dir.join("projects").join(&project_slug);
    std::fs::create_dir_all(&project_dir).unwrap();
    std::fs::write(
        project_dir.join("session-recipe.jsonl"),
        format!(
            r#"{{"type":"user","uuid":"u-1","timestamp":"2024-01-01T00:00:00Z","cwd":"{cwd}","message":{{"role":"user","content":"hi"}}}}
{{"type":"assistant","uuid":"a-1","timestamp":"2024-01-01T00:00:01Z","message":{{"role":"assistant","content":"hello"}}}}
"#,
            cwd = project.display()
        ),
    )
    .unwrap();

    let cfg = tempfile::tempdir().unwrap();
    cmd()
        .env("HOME", temp.path())
        .env("TOOLPATH_CONFIG_DIR", cfg.path())
        .args(["share"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("path import"))
        .stderr(predicate::str::contains("path export pathbase"));
}