-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
5833 lines (5406 loc) · 179 KB
/
Copy pathcli.rs
File metadata and controls
5833 lines (5406 loc) · 179 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
use assert_cmd::Command;
use predicates::prelude::*;
use predicates::str::contains;
#[test]
fn pack_command_prints_markdown_for_existing_task() {
let dir = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "Pack me"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["pack", &task_id, "--mode", "compact"])
.assert()
.success()
.stdout(contains("# Pack me"));
}
#[test]
fn event_command_appends_decision_visible_in_pack() {
let dir = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "T"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args([
"event",
&task_id,
"--type",
"decision",
"--text",
"Adopt Rust",
])
.assert()
.success();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("Adopt Rust"));
}
#[test]
fn close_command_marks_task_closed_in_pack() {
let dir = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "T"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["close", &task_id, "--reason", "shipped"])
.assert()
.success();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("status: closed"));
}
#[test]
fn close_warns_on_completeness_gap() {
let dir = assert_fs::TempDir::new().unwrap();
// Create a task WITH a goal so the NoGoal gap won't fire.
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "Gap me", "--goal", "ship it"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
// Close WITHOUT an outcome → ClosedNoOutcome gap. The close still succeeds
// and prints the gap to stderr.
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["close", &task_id, "--reason", "done"])
.assert()
.success()
.stderr(contains("closed without a recorded outcome"));
// And the task is actually closed.
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("status: closed"));
}
#[test]
fn doctor_exits_zero_on_fresh_install() {
let dir = assert_fs::TempDir::new().unwrap();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["doctor"])
.assert()
.success();
}
#[test]
fn doctor_json_output_is_parseable_and_lists_paths() {
let dir = assert_fs::TempDir::new().unwrap();
let output = Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["doctor", "--json"])
.output()
.unwrap();
let stdout = String::from_utf8(output.stdout).unwrap();
let v: serde_json::Value =
serde_json::from_str(&stdout).expect("doctor --json must be valid JSON");
assert!(v.get("data_dir").is_some());
assert!(v.get("events_dir").is_some());
assert!(v.get("state_dir").is_some());
assert!(v.get("known_projects").unwrap().is_array());
assert!(v.get("issues").unwrap().is_array());
}
fn write_pending(xdg: &std::path::Path, id: &str, text: &str, attempts: u32) {
let dir = xdg.join("task-journal").join("pending");
std::fs::create_dir_all(&dir).unwrap();
let body = serde_json::json!({
"text": text,
"error": "test injection",
"queued_at": "2026-05-07T00:00:00Z",
"attempts": attempts,
});
std::fs::write(
dir.join(format!("{id}.json")),
serde_json::to_string_pretty(&body).unwrap(),
)
.unwrap();
}
#[test]
fn pending_list_shows_queued_entries() {
let xdg = assert_fs::TempDir::new().unwrap();
let proj = assert_fs::TempDir::new().unwrap();
write_pending(xdg.path(), "tj-pending-1", "I think the cache is racy", 0);
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args(["pending", "list"])
.assert()
.success()
.stdout(contains("tj-pending-1"))
.stdout(contains("I think the cache is racy"));
}
#[test]
fn pending_retry_drains_with_mock_classifier() {
let xdg = assert_fs::TempDir::new().unwrap();
let proj = assert_fs::TempDir::new().unwrap();
// Seed: real task in JSONL so the classifier-mocked event has a
// legitimate task_id to attach to.
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args(["create", "Pending host"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
write_pending(
xdg.path(),
"tj-pending-2",
"Adopted Rust for the journal",
0,
);
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args([
"pending",
"retry",
"--mock-event-type",
"decision",
"--mock-task-id",
&task_id,
"--mock-confidence",
"0.92",
])
.assert()
.success()
.stdout(contains("1 drained"));
// pending file removed
let pending_file = xdg
.path()
.join("task-journal")
.join("pending")
.join("tj-pending-2.json");
assert!(!pending_file.exists(), "drained entry must be removed");
// event landed in JSONL — visible in pack
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("Adopted Rust for the journal"));
}
#[test]
fn pending_retry_marks_dead_after_max_attempts() {
let xdg = assert_fs::TempDir::new().unwrap();
let proj = assert_fs::TempDir::new().unwrap();
// Already at attempts=2; one more failure should rename to *.dead.json.
write_pending(xdg.path(), "tj-dying", "any text", 2);
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
// No --mock-* flags → retry fails → attempts becomes 3 → dead.
.args(["pending", "retry"])
.assert()
.success()
.stdout(contains("1 marked dead"));
let pending_dir = xdg.path().join("task-journal").join("pending");
let live = pending_dir.join("tj-dying.json");
let dead = pending_dir.join("tj-dying.dead.json");
assert!(!live.exists(), "live file must be gone after dead-rename");
assert!(dead.exists(), "dead file must exist: {dead:?}");
}
#[test]
fn export_sqlite_round_trips_through_pack() {
// Setup A: write a project + task in xdg_a/proj_a.
let xdg_a = assert_fs::TempDir::new().unwrap();
let proj_a = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg_a.path())
.current_dir(proj_a.path())
.args(["create", "Round-trip via sqlite export"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg_a.path())
.current_dir(proj_a.path())
.args([
"event",
&task_id,
"--type",
"decision",
"--text",
"Adopt sqlite export",
])
.assert()
.success();
// Export the SQLite snapshot to a buffer.
let snapshot = Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg_a.path())
.current_dir(proj_a.path())
.args(["export", "--format", "sqlite"])
.output()
.unwrap()
.stdout;
assert!(
snapshot.starts_with(b"SQLite format 3\0"),
"magic bytes missing"
);
// Setup B: a fresh xdg, no JSONL — only the snapshot in state/.
let xdg_b = assert_fs::TempDir::new().unwrap();
// Project hash derives from the proj path; we keep the same path so
// the hash matches what the snapshot was keyed under.
let project_hash = {
let out = Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg_a.path())
.current_dir(proj_a.path())
.args(["doctor", "--json"])
.output()
.unwrap()
.stdout;
let v: serde_json::Value = serde_json::from_slice(&out).unwrap();
v["state_dir"].as_str().unwrap().to_owned()
};
// We can't read the project_hash directly, but state_dir/<hash>.sqlite
// is the file we're after. Re-derive the destination for xdg_b by
// running doctor against xdg_b too — same proj path = same hash.
let _ = project_hash;
let dest_state_dir = xdg_b.path().join("task-journal").join("state");
std::fs::create_dir_all(&dest_state_dir).unwrap();
// Pull the source filename (first .sqlite under xdg_a/task-journal/state).
let src_state_dir = xdg_a.path().join("task-journal").join("state");
let src_file = std::fs::read_dir(&src_state_dir)
.unwrap()
.filter_map(|e| e.ok())
.map(|e| e.path())
.find(|p| p.extension().and_then(|s| s.to_str()) == Some("sqlite"))
.expect("source sqlite present");
let dest_file = dest_state_dir.join(src_file.file_name().unwrap());
std::fs::write(&dest_file, &snapshot).unwrap();
// Pack from the new XDG without a JSONL — assemble must read from the
// snapshot SQLite alone.
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg_b.path())
.current_dir(proj_a.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("Adopt sqlite export"));
}
#[test]
fn export_html_emits_self_contained_document() {
let xdg = assert_fs::TempDir::new().unwrap();
let proj = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args(["create", "HTML export test"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args([
"event",
&task_id,
"--type",
"decision",
"--text",
"Adopt Rust",
])
.assert()
.success();
let output = Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args(["export", "--format", "html", "--task", &task_id])
.output()
.unwrap();
let html = String::from_utf8(output.stdout).unwrap();
// Self-contained shape.
let lower = html.to_lowercase();
assert!(
lower.starts_with("<!doctype html>"),
"html missing doctype: {html}"
);
assert!(html.contains("HTML export test"), "task title missing");
assert!(html.contains("Adopt Rust"), "decision event missing");
// No external assets — no http/https URL anywhere.
assert!(!html.contains("http://"), "external http url leaked");
assert!(!html.contains("https://"), "external https url leaked");
}
#[test]
fn migrate_project_round_trips_data_to_new_path() {
let xdg = assert_fs::TempDir::new().unwrap();
let proj_a = assert_fs::TempDir::new().unwrap();
let proj_b = assert_fs::TempDir::new().unwrap();
// Distinct project roots so each hashes to itself, not to a shared ancestor
// carrying a `.git` (which collapses both to one hash on some hosts, WSL).
std::fs::create_dir(proj_a.path().join(".git")).unwrap();
std::fs::create_dir(proj_b.path().join(".git")).unwrap();
// Create a task with the cwd = proj_a.
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj_a.path())
.args(["create", "Migration round-trip"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
// Migrate the data to proj_b.
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.args([
"migrate-project",
"--from",
proj_a.path().to_str().unwrap(),
"--to",
proj_b.path().to_str().unwrap(),
])
.assert()
.success();
// Pack from proj_b finds the same task.
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj_b.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("Migration round-trip"));
}
#[test]
fn migrate_project_refuses_overwrite_without_force() {
let xdg = assert_fs::TempDir::new().unwrap();
let proj_a = assert_fs::TempDir::new().unwrap();
let proj_b = assert_fs::TempDir::new().unwrap();
// Distinct project roots so each hashes to itself, not a shared `.git`
// ancestor (which collapses both to one hash on some hosts, e.g. WSL).
std::fs::create_dir(proj_a.path().join(".git")).unwrap();
std::fs::create_dir(proj_b.path().join(".git")).unwrap();
// Both projects have data: create a task in each.
for proj in [&proj_a, &proj_b] {
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.current_dir(proj.path())
.args(["create", "Conflicting"])
.assert()
.success();
}
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", xdg.path())
.args([
"migrate-project",
"--from",
proj_a.path().to_str().unwrap(),
"--to",
proj_b.path().to_str().unwrap(),
])
.assert()
.failure()
.stderr(contains("destination already exists"));
}
#[test]
fn close_unknown_task_id_returns_error() {
let dir = assert_fs::TempDir::new().unwrap();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["close", "tj-doesnotexist", "--reason", "shipped"])
.assert()
.failure()
.stderr(contains("task not found: tj-doesnotexist"));
}
#[test]
fn search_all_projects_finds_match_in_other_project_hash() {
let dir = assert_fs::TempDir::new().unwrap();
let state = dir.path().join("task-journal").join("state");
std::fs::create_dir_all(&state).unwrap();
for hash in ["aaaa1111aaaa1111", "bbbb2222bbbb2222"] {
let db_path = state.join(format!("{hash}.sqlite"));
let conn = tj_core::db::open(&db_path).unwrap();
let mut e = tj_core::event::Event::new(
format!("tj-{}", &hash[..6]),
tj_core::event::EventType::Open,
tj_core::event::Author::User,
tj_core::event::Source::Cli,
format!("Marker {hash}"),
);
e.meta = serde_json::json!({"title": format!("Title {hash}")});
tj_core::db::upsert_task_from_event(&conn, &e, hash).unwrap();
tj_core::db::index_event(&conn, &e).unwrap();
}
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["search", "Marker", "--all-projects"])
.assert()
.success()
.stdout(contains("aaaa1111").and(contains("bbbb2222")));
}
#[test]
fn search_command_finds_task_by_event_text() {
let dir = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "OAuth thing"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args([
"event",
&task_id,
"--type",
"decision",
"--text",
"Adopt Rust + rmcp",
])
.assert()
.success();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["search", "rmcp"])
.assert()
.success()
.stdout(contains(&task_id));
}
#[test]
fn e2e_create_event_close_pack_search() {
let dir = assert_fs::TempDir::new().unwrap();
let env = || {
let mut cmd = Command::cargo_bin("task-journal").unwrap();
cmd.env("XDG_DATA_HOME", dir.path());
cmd
};
let task_id = String::from_utf8(
env()
.args(["create", "Build pack assembler"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
env()
.args([
"event",
&task_id,
"--type",
"hypothesis",
"--text",
"Use SQLite views",
])
.assert()
.success();
env()
.args([
"event",
&task_id,
"--type",
"decision",
"--text",
"Rust + rmcp",
])
.assert()
.success();
env()
.args([
"event",
&task_id,
"--type",
"rejection",
"--text",
"Node loses binary",
])
.assert()
.success();
env()
.args(["close", &task_id, "--reason", "shipped"])
.assert()
.success();
env()
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(
contains("Build pack assembler")
.and(contains("Rust + rmcp"))
.and(contains("Node loses binary"))
.and(contains("status: closed")),
);
env()
.args(["search", "rmcp"])
.assert()
.success()
.stdout(contains(&task_id));
}
#[test]
fn e2e_hook_simulation_classifies_and_packs_event() {
let dir = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "Stack choice for journal"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args([
"ingest-hook",
"--kind",
"Stop",
"--text",
"After review, we adopt Rust because of the single-binary distribution.",
"--mock-event-type",
"decision",
"--mock-task-id",
&task_id,
"--mock-confidence",
"0.92",
])
.assert()
.success();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(
contains("Stack choice for journal")
.and(contains("[decision]"))
.and(contains("single-binary"))
.and(contains("[?]").not()),
);
}
#[test]
fn event_correct_links_to_corrected_event() {
let dir = assert_fs::TempDir::new().unwrap();
let task_id = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["create", "Correct me"])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
let bad = String::from_utf8(
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args([
"event",
&task_id,
"--type",
"finding",
"--text",
"Migration done (wrong)",
])
.assert()
.success()
.get_output()
.stdout
.clone(),
)
.unwrap()
.trim()
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args([
"event-correct",
"--corrects",
&bad,
"--task",
&task_id,
"--text",
"Migration was NOT done; finding was wrong",
])
.assert()
.success();
Command::cargo_bin("task-journal")
.unwrap()
.env("XDG_DATA_HOME", dir.path())
.args(["pack", &task_id, "--mode", "full"])
.assert()
.success()
.stdout(contains("Migration was NOT done").and(contains("[correction]")));
}
#[test]
fn install_hooks_command_uses_no_fail_pattern() {
let dir = assert_fs::TempDir::new().unwrap();
Command::cargo_bin("task-journal")
.unwrap()
.env("HOME", dir.path())
.args(["install-hooks", "--scope", "user"])
.assert()
.success();
let s = std::fs::read_to_string(dir.path().join(".claude/settings.json")).unwrap();
assert!(
s.contains("|| true"),
"hook command must end with || true so a failed classifier doesn't break Claude Code: {s}"
);
}
#[test]
fn install_hooks_writes_to_settings_json() {
let dir = assert_fs::TempDir::new().unwrap();
Command::cargo_bin("task-journal")
.unwrap()
.env("HOME", dir.path())
.args(["install-hooks", "--scope", "user"])
.assert()
.success();
let settings_path = dir.path().join(".claude").join("settings.json");
assert!(settings_path.exists());
let content = std::fs::read_to_string(&settings_path).unwrap();
assert!(content.contains("task-journal ingest-hook")); // SessionStart resume
assert!(
content.contains("SessionStart"),
"install-hooks must wire SessionStart so resume-pack injection works"
);
// v0.14.x — self-tagging-first: the default wires the no-model UserPromptSubmit
// nudge, but NOT the per-message classifier hooks (those spawn `claude -p`);
// the classifier is opt-in via `--auto-capture`.
assert!(
content.contains("task-journal nudge"),
"default must wire the no-model UserPromptSubmit nudge"
);
assert!(
!content.contains("PostToolUse"),
"default must not wire the per-message classifier hooks"
);
assert!(
!content.contains("\"Stop\""),
"default must not wire the classifier Stop hook"
);
}
#[test]
fn install_hooks_auto_capture_wires_all_events() {
let dir = assert_fs::TempDir::new().unwrap();
Command::cargo_bin("task-journal")
.unwrap()
.env("HOME", dir.path())
.args(["install-hooks", "--scope", "user", "--auto-capture"])
.assert()
.success();
let content =
std::fs::read_to_string(dir.path().join(".claude").join("settings.json")).unwrap();
for ev in [
"SessionStart",
"UserPromptSubmit",
"PostToolUse",
"Stop",
"PreCompact",
"SessionEnd",
] {
assert!(content.contains(ev), "--auto-capture must wire {ev}");
}
}
#[test]
fn session_end_hook_is_clean_noop_without_journal() {
// SessionEnd(clear) with no journal yet must exit cleanly (it's the
// last-chance catch-up; nothing to catch when there's no project journal).
let dir = assert_fs::TempDir::new().unwrap();
let proj = assert_fs::TempDir::new().unwrap();
for reason in ["clear", "logout"] {
let payload = serde_json::json!({
"hook_event_name": "SessionEnd",
"reason": reason,
"session_id": "s-end",
"transcript_path": "/nonexistent/x.jsonl",
"cwd": proj.path().to_string_lossy(),
})
.to_string();
Command::cargo_bin("task-journal")
.unwrap()
.current_dir(proj.path())
.env("XDG_DATA_HOME", dir.path())
.args(["ingest-hook", "--backend", "hybrid"])
.write_stdin(payload)
.assert()
.success();
}
}
#[test]
fn install_hooks_merges_and_preserves_third_party_hooks() {
let dir = assert_fs::TempDir::new().unwrap();
let claude_dir = dir.path().join(".claude");
std::fs::create_dir_all(&claude_dir).unwrap();
// Pre-existing foreign hooks (another plugin) on the same events we touch.
std::fs::write(
claude_dir.join("settings.json"),
serde_json::json!({
"hooks": {
"UserPromptSubmit": [{ "matcher": "", "hooks": [
{ "type": "command", "command": "other-plugin do-thing" }
]}],
"SessionStart": [{ "matcher": "", "hooks": [
{ "type": "command", "command": "other-plugin start" }
]}]
}
})
.to_string(),
)
.unwrap();
let run = || {
Command::cargo_bin("task-journal")
.unwrap()
.env("HOME", dir.path())
.args(["install-hooks", "--scope", "user"])
.assert()
.success();
};
run();
run(); // idempotent: second install must not duplicate task-journal entries
let content = std::fs::read_to_string(claude_dir.join("settings.json")).unwrap();
// Foreign hooks survive.
assert!(
content.contains("other-plugin do-thing"),
"must preserve a third-party UserPromptSubmit hook: {content}"
);
assert!(
content.contains("other-plugin start"),
"must preserve a third-party SessionStart hook"
);
// Ours got added.
assert!(content.contains("task-journal nudge"));
assert!(content.contains("task-journal ingest-hook"));
// Idempotent — exactly one nudge, not two.
assert_eq!(
content.matches("task-journal nudge").count(),
1,
"re-install must not duplicate the nudge hook: {content}"
);
}
#[test]
fn install_hooks_is_idempotent_and_uninstall_works() {
let dir = assert_fs::TempDir::new().unwrap();
let claude_dir = dir.path().join(".claude");
std::fs::create_dir_all(&claude_dir).unwrap();
std::fs::write(
claude_dir.join("settings.json"),
serde_json::json!({"theme": "dark"}).to_string(),
)
.unwrap();
Command::cargo_bin("task-journal")
.unwrap()
.env("HOME", dir.path())
.args(["install-hooks", "--scope", "user"])
.assert()
.success();
Command::cargo_bin("task-journal")
.unwrap()
.env("HOME", dir.path())
.args(["install-hooks", "--scope", "user"])
.assert()
.success();
let after_install = std::fs::read_to_string(claude_dir.join("settings.json")).unwrap();
assert!(
after_install.contains("\"theme\":\"dark\"")
|| after_install.contains("\"theme\": \"dark\""),