deciduous 0.15.0

Decision graph tooling for AI-assisted development. Track every goal, decision, and outcome. Survive context loss. Query your reasoning.
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
//! Integration tests for the deciduous CLI
//!
//! These tests exercise the full CLI workflow using a temporary database.
//! They verify that commands work end-to-end without mocking.

use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;

/// Helper to run deciduous CLI with a specific database path
fn run_deciduous(args: &[&str], db_path: &PathBuf) -> std::process::Output {
    Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .args(args)
        .env("DECIDUOUS_DB_PATH", db_path)
        .output()
        .expect("Failed to execute deciduous")
}

/// Helper to get stdout as string
fn stdout(output: &std::process::Output) -> String {
    String::from_utf8_lossy(&output.stdout).to_string()
}

/// Helper to get stderr as string
fn stderr(output: &std::process::Output) -> String {
    String::from_utf8_lossy(&output.stderr).to_string()
}

// =============================================================================
// Basic Command Tests
// =============================================================================

#[test]
fn test_help_command() {
    let output = Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .arg("--help")
        .output()
        .expect("Failed to execute");

    assert!(output.status.success());
    let out = stdout(&output);
    assert!(out.contains("deciduous"));
    assert!(out.contains("Decision graph"));
}

#[test]
fn test_version_command() {
    let output = Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .arg("--version")
        .output()
        .expect("Failed to execute");

    assert!(output.status.success());
    let out = stdout(&output);
    assert!(out.contains("deciduous"));
}

// =============================================================================
// Shell Completion Tests
// =============================================================================

#[test]
fn test_completion_zsh() {
    let output = Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .args(["completion", "zsh"])
        .output()
        .expect("Failed to execute");

    assert!(
        output.status.success(),
        "completion zsh failed: {}",
        stderr(&output)
    );
    let out = stdout(&output);
    assert!(
        out.contains("#compdef deciduous"),
        "zsh completion should contain #compdef"
    );
}

#[test]
fn test_completion_bash() {
    let output = Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .args(["completion", "bash"])
        .output()
        .expect("Failed to execute");

    assert!(
        output.status.success(),
        "completion bash failed: {}",
        stderr(&output)
    );
    let out = stdout(&output);
    assert!(
        out.contains("_deciduous"),
        "bash completion should contain _deciduous function"
    );
}

#[test]
fn test_completion_fish() {
    let output = Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .args(["completion", "fish"])
        .output()
        .expect("Failed to execute");

    assert!(
        output.status.success(),
        "completion fish failed: {}",
        stderr(&output)
    );
    let out = stdout(&output);
    assert!(
        out.contains("complete -c deciduous"),
        "fish completion should contain complete command"
    );
}

#[test]
fn test_completion_help() {
    let output = Command::new(env!("CARGO_BIN_EXE_deciduous"))
        .args(["completion", "--help"])
        .output()
        .expect("Failed to execute");

    assert!(output.status.success());
    let out = stdout(&output);
    assert!(out.contains("bash"));
    assert!(out.contains("zsh"));
    assert!(out.contains("fish"));
}

// =============================================================================
// Node CRUD Tests
// =============================================================================

#[test]
fn test_add_and_list_nodes() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Add a goal node
    let output = run_deciduous(&["add", "goal", "Test Goal", "-c", "90"], &db_path);
    assert!(
        output.status.success(),
        "add goal failed: {}",
        stderr(&output)
    );
    assert!(stdout(&output).contains("Created node"));

    // Add an action node
    let output = run_deciduous(&["add", "action", "Test Action", "-c", "85"], &db_path);
    assert!(
        output.status.success(),
        "add action failed: {}",
        stderr(&output)
    );

    // List nodes
    let output = run_deciduous(&["nodes"], &db_path);
    assert!(output.status.success(), "nodes failed: {}", stderr(&output));
    let out = stdout(&output);
    assert!(out.contains("Test Goal"));
    assert!(out.contains("Test Action"));
    assert!(out.contains("goal"));
    assert!(out.contains("action"));
}

#[test]
fn test_add_node_with_all_metadata() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Add node with all optional fields
    let output = run_deciduous(
        &[
            "add",
            "goal",
            "Full Metadata Goal",
            "-c",
            "95",
            "-p",
            "User asked: implement feature X",
            "-f",
            "src/main.rs,src/lib.rs",
            "-b",
            "feature-branch",
        ],
        &db_path,
    );
    assert!(
        output.status.success(),
        "add with metadata failed: {}",
        stderr(&output)
    );

    // Verify node was created
    let output = run_deciduous(&["nodes"], &db_path);
    assert!(stdout(&output).contains("Full Metadata Goal"));
}

#[test]
fn test_add_all_node_types() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    let node_types = [
        "goal",
        "decision",
        "option",
        "action",
        "outcome",
        "observation",
    ];

    for node_type in &node_types {
        let title = format!("Test {}", node_type);
        let output = run_deciduous(&["add", node_type, &title, "-c", "80"], &db_path);
        assert!(
            output.status.success(),
            "add {} failed: {}",
            node_type,
            stderr(&output)
        );
    }

    // List and verify all types present
    let output = run_deciduous(&["nodes"], &db_path);
    let out = stdout(&output);
    for node_type in &node_types {
        assert!(out.contains(node_type), "Missing node type: {}", node_type);
    }
}

// =============================================================================
// Edge Tests
// =============================================================================

#[test]
fn test_link_nodes() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create two nodes
    run_deciduous(&["add", "goal", "Goal", "-c", "90"], &db_path);
    run_deciduous(&["add", "action", "Action", "-c", "85"], &db_path);

    // Link them
    let output = run_deciduous(&["link", "1", "2", "-r", "Goal leads to action"], &db_path);
    assert!(output.status.success(), "link failed: {}", stderr(&output));
    assert!(stdout(&output).contains("Created edge"));

    // Verify edge exists
    let output = run_deciduous(&["edges"], &db_path);
    assert!(output.status.success());
    let out = stdout(&output);
    assert!(out.contains("1"));
    assert!(out.contains("2"));
    assert!(out.contains("leads_to"));
}

#[test]
fn test_link_with_edge_types() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create decision and options
    run_deciduous(&["add", "decision", "Choose framework"], &db_path);
    run_deciduous(&["add", "option", "React"], &db_path);
    run_deciduous(&["add", "option", "Vue"], &db_path);

    // Link with chosen/rejected types
    let output = run_deciduous(
        &["link", "1", "2", "-t", "chosen", "-r", "Better ecosystem"],
        &db_path,
    );
    assert!(output.status.success());

    let output = run_deciduous(
        &[
            "link",
            "1",
            "3",
            "-t",
            "rejected",
            "-r",
            "Smaller community",
        ],
        &db_path,
    );
    assert!(output.status.success());

    // Verify edges
    let output = run_deciduous(&["edges"], &db_path);
    let out = stdout(&output);
    assert!(out.contains("chosen"));
    assert!(out.contains("rejected"));
}

// =============================================================================
// Status Update Tests
// =============================================================================

#[test]
fn test_update_node_status() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create a node
    run_deciduous(&["add", "action", "My Action"], &db_path);

    // Update status
    let output = run_deciduous(&["status", "1", "completed"], &db_path);
    assert!(
        output.status.success(),
        "status update failed: {}",
        stderr(&output)
    );

    // Verify status changed
    let output = run_deciduous(&["nodes"], &db_path);
    assert!(stdout(&output).contains("completed"));
}

// =============================================================================
// Graph Export Tests
// =============================================================================

#[test]
fn test_graph_json_export() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create some nodes and edges
    run_deciduous(&["add", "goal", "Export Test Goal"], &db_path);
    run_deciduous(&["add", "action", "Export Test Action"], &db_path);
    run_deciduous(&["link", "1", "2", "-r", "test"], &db_path);

    // Export graph as JSON
    let output = run_deciduous(&["graph"], &db_path);
    assert!(
        output.status.success(),
        "graph export failed: {}",
        stderr(&output)
    );

    let out = stdout(&output);

    // Verify it's valid JSON with expected structure
    let json: serde_json::Value = serde_json::from_str(&out).expect("Output should be valid JSON");

    assert!(json.get("nodes").is_some(), "JSON should have nodes");
    assert!(json.get("edges").is_some(), "JSON should have edges");

    let nodes = json["nodes"].as_array().unwrap();
    assert_eq!(nodes.len(), 2);
}

#[test]
fn test_dot_export() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create graph
    run_deciduous(&["add", "goal", "DOT Test"], &db_path);
    run_deciduous(&["add", "action", "DOT Action"], &db_path);
    run_deciduous(&["link", "1", "2"], &db_path);

    // Export as DOT
    let output = run_deciduous(&["dot"], &db_path);
    assert!(
        output.status.success(),
        "dot export failed: {}",
        stderr(&output)
    );

    let out = stdout(&output);
    assert!(out.contains("digraph"));
    assert!(out.contains("DOT Test"));
    assert!(out.contains("->"));
}

// =============================================================================
// Filter Tests
// =============================================================================

#[test]
fn test_filter_nodes_by_type() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create mixed nodes
    run_deciduous(&["add", "goal", "Goal 1"], &db_path);
    run_deciduous(&["add", "goal", "Goal 2"], &db_path);
    run_deciduous(&["add", "action", "Action 1"], &db_path);

    // Filter by type
    let output = run_deciduous(&["nodes", "-t", "goal"], &db_path);
    assert!(output.status.success());

    let out = stdout(&output);
    assert!(out.contains("Goal 1"));
    assert!(out.contains("Goal 2"));
    assert!(!out.contains("Action 1"));
}

// =============================================================================
// Command Log Tests
// =============================================================================

#[test]
fn test_command_log() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Run some commands
    run_deciduous(&["add", "goal", "Logged Goal"], &db_path);
    run_deciduous(&["add", "action", "Logged Action"], &db_path);

    // Check command log
    let output = run_deciduous(&["commands"], &db_path);
    assert!(
        output.status.success(),
        "commands failed: {}",
        stderr(&output)
    );

    let out = stdout(&output);
    // Command log should show something
    assert!(!out.is_empty());
}

// =============================================================================
// Error Handling Tests
// =============================================================================

#[test]
fn test_link_nonexistent_nodes() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Try to link nodes that don't exist
    let output = run_deciduous(&["link", "999", "998"], &db_path);

    // Should fail gracefully
    assert!(
        !output.status.success()
            || stderr(&output).contains("Error")
            || stderr(&output).contains("not found")
    );
}

#[test]
fn test_invalid_node_type() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Try to add invalid node type - the CLI accepts it but warns
    // This tests that the CLI handles it gracefully (doesn't crash)
    let output = run_deciduous(&["add", "invalid_type", "Test"], &db_path);

    // CLI should complete (may succeed with warning or fail gracefully)
    // Main thing is it shouldn't panic
    let _out = stdout(&output);
    let _err = stderr(&output);
    // Just verify it ran without panic - actual behavior varies
}

// =============================================================================
// Node Delete Tests
// =============================================================================

#[test]
fn test_delete_node() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create a node
    run_deciduous(&["add", "goal", "Delete Me", "-c", "90"], &db_path);

    // Verify it exists
    let output = run_deciduous(&["nodes"], &db_path);
    assert!(stdout(&output).contains("Delete Me"));

    // Delete it
    let output = run_deciduous(&["delete", "1"], &db_path);
    assert!(
        output.status.success(),
        "delete failed: {}",
        stderr(&output)
    );

    // Verify it's gone
    let output = run_deciduous(&["nodes"], &db_path);
    assert!(!stdout(&output).contains("Delete Me"));
}

#[test]
fn test_delete_node_cascades_edges() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create two nodes and link them
    run_deciduous(&["add", "goal", "Parent"], &db_path);
    run_deciduous(&["add", "action", "Child"], &db_path);
    run_deciduous(&["link", "1", "2", "-r", "parent-child"], &db_path);

    // Verify edge exists
    let output = run_deciduous(&["edges"], &db_path);
    assert!(stdout(&output).contains("leads_to"));

    // Delete parent node (edges are automatically cleaned up)
    let output = run_deciduous(&["delete", "1"], &db_path);
    assert!(
        output.status.success(),
        "delete failed: {}",
        stderr(&output)
    );

    // Verify edge is gone too (deleting a node removes its edges)
    let output = run_deciduous(&["edges"], &db_path);
    let out = stdout(&output);
    assert!(
        !out.contains("parent-child"),
        "Edge should be removed after node delete"
    );
}

// =============================================================================
// Unlink Tests
// =============================================================================

#[test]
fn test_unlink_nodes() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create and link nodes
    run_deciduous(&["add", "goal", "Goal"], &db_path);
    run_deciduous(&["add", "action", "Action"], &db_path);
    run_deciduous(&["link", "1", "2", "-r", "test link"], &db_path);

    // Verify edge exists
    let output = run_deciduous(&["edges"], &db_path);
    assert!(stdout(&output).contains("1"));

    // Unlink (requires FROM and TO)
    let output = run_deciduous(&["unlink", "1", "2"], &db_path);
    assert!(
        output.status.success(),
        "unlink failed: {}",
        stderr(&output)
    );
}

// =============================================================================
// Show/Detail Tests
// =============================================================================

#[test]
fn test_show_node_detail() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create node with metadata
    run_deciduous(
        &[
            "add",
            "goal",
            "Detailed Goal",
            "-c",
            "95",
            "-p",
            "User prompt here",
        ],
        &db_path,
    );

    // Show detail
    let output = run_deciduous(&["show", "1"], &db_path);
    assert!(output.status.success(), "show failed: {}", stderr(&output));
    let out = stdout(&output);
    assert!(out.contains("Detailed Goal"));
}

#[test]
fn test_show_node_json() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    run_deciduous(&["add", "goal", "JSON Goal", "-c", "90"], &db_path);

    let output = run_deciduous(&["show", "1", "--json"], &db_path);
    assert!(
        output.status.success(),
        "show --json failed: {}",
        stderr(&output)
    );
    let out = stdout(&output);
    let json: serde_json::Value =
        serde_json::from_str(&out).expect("show --json should output valid JSON");
    assert_eq!(json["title"], "JSON Goal");
}

// =============================================================================
// Prompt Tests
// =============================================================================

#[test]
fn test_update_prompt() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    run_deciduous(&["add", "goal", "Prompt Goal"], &db_path);

    // Update prompt
    let output = run_deciduous(&["prompt", "1", "Updated prompt text"], &db_path);
    assert!(
        output.status.success(),
        "prompt update failed: {}",
        stderr(&output)
    );

    // Verify via show
    let output = run_deciduous(&["show", "1", "--json"], &db_path);
    let out = stdout(&output);
    assert!(out.contains("Updated prompt text"));
}

// =============================================================================
// Status Transitions Tests
// =============================================================================

#[test]
fn test_all_status_values() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    run_deciduous(&["add", "goal", "Status Test"], &db_path);

    let statuses = ["active", "completed", "superseded", "abandoned", "rejected"];
    for status in &statuses {
        let output = run_deciduous(&["status", "1", status], &db_path);
        assert!(
            output.status.success(),
            "status {} failed: {}",
            status,
            stderr(&output)
        );
    }
}

// =============================================================================
// Graph Traversal Tests
// =============================================================================

#[test]
fn test_complex_graph_structure() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Build a proper decision tree:
    // goal -> option1, option2
    // option1 -> decision
    // decision -> action
    // action -> outcome
    run_deciduous(&["add", "goal", "Root Goal", "-c", "90"], &db_path);
    run_deciduous(&["add", "option", "Option A", "-c", "80"], &db_path);
    run_deciduous(&["add", "option", "Option B", "-c", "75"], &db_path);
    run_deciduous(&["add", "decision", "Choose A", "-c", "95"], &db_path);
    run_deciduous(&["add", "action", "Implement A", "-c", "85"], &db_path);
    run_deciduous(&["add", "outcome", "A works", "-c", "90"], &db_path);
    run_deciduous(&["add", "observation", "Noticed X", "-c", "70"], &db_path);

    // Link the full chain
    run_deciduous(&["link", "1", "2", "-r", "possible approach"], &db_path);
    run_deciduous(&["link", "1", "3", "-r", "possible approach"], &db_path);
    run_deciduous(
        &["link", "2", "4", "-t", "chosen", "-r", "better fit"],
        &db_path,
    );
    run_deciduous(
        &["link", "3", "4", "-t", "rejected", "-r", "too complex"],
        &db_path,
    );
    run_deciduous(&["link", "4", "5", "-r", "implementation"], &db_path);
    run_deciduous(&["link", "5", "6", "-r", "result"], &db_path);
    run_deciduous(&["link", "7", "1", "-r", "context"], &db_path);

    // Verify full graph
    let output = run_deciduous(&["graph"], &db_path);
    assert!(output.status.success());
    let json: serde_json::Value =
        serde_json::from_str(&stdout(&output)).expect("Graph should be valid JSON");
    assert_eq!(json["nodes"].as_array().unwrap().len(), 7);
    assert_eq!(json["edges"].as_array().unwrap().len(), 7);

    // Verify DOT export includes all nodes
    let output = run_deciduous(&["dot"], &db_path);
    assert!(output.status.success());
    let dot = stdout(&output);
    assert!(dot.contains("Root Goal"));
    assert!(dot.contains("Option A"));
    assert!(dot.contains("Choose A"));
    assert!(dot.contains("Implement A"));
    assert!(dot.contains("A works"));
    assert!(dot.contains("Noticed X"));
}

#[test]
fn test_dot_with_root_filter() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create two separate chains
    run_deciduous(&["add", "goal", "Chain 1 Root"], &db_path);
    run_deciduous(&["add", "action", "Chain 1 Action"], &db_path);
    run_deciduous(&["link", "1", "2"], &db_path);

    run_deciduous(&["add", "goal", "Chain 2 Root"], &db_path);
    run_deciduous(&["add", "action", "Chain 2 Action"], &db_path);
    run_deciduous(&["link", "3", "4"], &db_path);

    // Filter DOT to only chain 1
    let output = run_deciduous(&["dot", "-r", "1"], &db_path);
    assert!(output.status.success());
    let dot = stdout(&output);
    assert!(dot.contains("Chain 1 Root"));
    assert!(dot.contains("Chain 1 Action"));
    assert!(!dot.contains("Chain 2 Root"));
}

#[test]
fn test_dot_with_node_range() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    run_deciduous(&["add", "goal", "Node 1"], &db_path);
    run_deciduous(&["add", "action", "Node 2"], &db_path);
    run_deciduous(&["add", "outcome", "Node 3"], &db_path);

    // Filter to nodes 1-2 only
    let output = run_deciduous(&["dot", "-n", "1-2"], &db_path);
    assert!(output.status.success());
    let dot = stdout(&output);
    assert!(dot.contains("Node 1"));
    assert!(dot.contains("Node 2"));
    assert!(!dot.contains("Node 3"));
}

// =============================================================================
// Backup Tests
// =============================================================================

#[test]
fn test_backup() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create some data
    run_deciduous(&["add", "goal", "Backup Test"], &db_path);

    // Run backup
    let output = run_deciduous(&["backup"], &db_path);
    assert!(
        output.status.success(),
        "backup failed: {}",
        stderr(&output)
    );
}

// =============================================================================
// Empty Database Edge Cases
// =============================================================================

#[test]
fn test_empty_db_nodes() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // List nodes on empty database
    let output = run_deciduous(&["nodes"], &db_path);
    assert!(
        output.status.success(),
        "nodes on empty db failed: {}",
        stderr(&output)
    );
}

#[test]
fn test_empty_db_edges() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    let output = run_deciduous(&["edges"], &db_path);
    assert!(
        output.status.success(),
        "edges on empty db failed: {}",
        stderr(&output)
    );
}

#[test]
fn test_empty_db_graph() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    let output = run_deciduous(&["graph"], &db_path);
    assert!(
        output.status.success(),
        "graph on empty db failed: {}",
        stderr(&output)
    );
    let json: serde_json::Value =
        serde_json::from_str(&stdout(&output)).expect("Empty graph should be valid JSON");
    assert_eq!(json["nodes"].as_array().unwrap().len(), 0);
    assert_eq!(json["edges"].as_array().unwrap().len(), 0);
}

// =============================================================================
// Branch Filter Tests
// =============================================================================

#[test]
fn test_filter_nodes_by_branch() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create nodes on different branches
    run_deciduous(&["add", "goal", "Main Goal", "-b", "main"], &db_path);
    run_deciduous(
        &["add", "goal", "Feature Goal", "-b", "feature-x"],
        &db_path,
    );

    // Filter by branch
    let output = run_deciduous(&["nodes", "-b", "main"], &db_path);
    assert!(output.status.success());
    let out = stdout(&output);
    assert!(out.contains("Main Goal"));
    assert!(!out.contains("Feature Goal"));
}

// =============================================================================
// Writeup Tests
// =============================================================================

#[test]
fn test_writeup_basic() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    run_deciduous(&["add", "goal", "PR Goal", "-c", "90"], &db_path);
    run_deciduous(&["add", "action", "PR Action", "-c", "85"], &db_path);
    run_deciduous(&["link", "1", "2", "-r", "implementation"], &db_path);

    let output = run_deciduous(&["writeup", "-t", "Test PR", "-n", "1-2"], &db_path);
    assert!(
        output.status.success(),
        "writeup failed: {}",
        stderr(&output)
    );
    let out = stdout(&output);
    assert!(out.contains("Test PR"));
}

// =============================================================================
// Multiple Edge Types Tests
// =============================================================================

#[test]
fn test_all_edge_types() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create nodes
    for i in 0..6 {
        run_deciduous(&["add", "goal", &format!("Node {}", i)], &db_path);
    }

    let edge_types = [
        "leads_to", "chosen", "rejected", "blocks", "enables", "requires",
    ];
    for (i, edge_type) in edge_types.iter().enumerate() {
        let from = format!("{}", i + 1);
        let to = format!("{}", ((i + 1) % 6) + 1);
        let output = run_deciduous(
            &[
                "link",
                &from,
                &to,
                "-t",
                edge_type,
                "-r",
                &format!("test {}", edge_type),
            ],
            &db_path,
        );
        assert!(
            output.status.success(),
            "link with type {} failed: {}",
            edge_type,
            stderr(&output)
        );
    }

    // Verify all edges created
    let output = run_deciduous(&["edges"], &db_path);
    let out = stdout(&output);
    for edge_type in &edge_types {
        assert!(out.contains(edge_type), "Missing edge type: {}", edge_type);
    }
}

// =============================================================================
// Revisit Node Tests
// =============================================================================

#[test]
fn test_revisit_node_type() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    let output = run_deciduous(
        &["add", "revisit", "Reconsider approach", "-c", "80"],
        &db_path,
    );
    assert!(
        output.status.success(),
        "add revisit failed: {}",
        stderr(&output)
    );
    assert!(stdout(&output).contains("Created node"));

    let output = run_deciduous(&["nodes"], &db_path);
    assert!(stdout(&output).contains("revisit"));
}

// =============================================================================
// Supersede Tests
// =============================================================================

#[test]
fn test_supersede_node() {
    let temp_dir = TempDir::new().expect("Failed to create temp dir");
    let db_path = temp_dir.path().join("test.db");

    // Create original decision and its replacement
    run_deciduous(&["add", "decision", "Old Approach"], &db_path);
    run_deciduous(&["add", "decision", "New Approach"], &db_path);

    // Supersede old with new
    let output = run_deciduous(&["status", "1", "superseded"], &db_path);
    assert!(
        output.status.success(),
        "supersede failed: {}",
        stderr(&output)
    );

    // Verify status changed
    let output = run_deciduous(&["show", "1", "--json"], &db_path);
    let out = stdout(&output);
    assert!(out.contains("superseded"));
}