kanban-domain 0.7.0

Domain models and business logic for the kanban project management tool
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
use crate::data_store::DataStore;
use crate::dependencies::{RelatesKind, Severity};
use crate::KanbanResult;
use kanban_core::Edge as _;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use super::{Command, CommandContext};
use crate::Card;

/// Per-kind dependency commands.
///
/// Each variant has a single relation kind baked into its type and
/// carries the kind-specific metadata (severity on Blocks, kind on
/// Relates) directly. No runtime kind discriminator: replay sees
/// the same metadata the forward saw.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "action", rename_all = "snake_case")]
pub enum DependencyCommand {
    AddSpawns(AddSpawns),
    AddBlocks(AddBlocks),
    AddRelates(AddRelates),
    RemoveSpawns(RemoveSpawns),
    RemoveBlocks(RemoveBlocks),
    RemoveRelates(RemoveRelates),
    /// Atomic create-card-and-link-as-subcard. Genuinely different
    /// from the edge commands — touches the board (card counter), the
    /// card store (new card), and the graph (parent edge). Its
    /// inverse is `DeleteCard` (polymorphic over live/archived, also
    /// strips incident edges).
    CreateSubcard(CreateSubcardCommand),
}

impl DependencyCommand {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        match self {
            DependencyCommand::AddSpawns(c) => c.execute(context),
            DependencyCommand::AddBlocks(c) => c.execute(context),
            DependencyCommand::AddRelates(c) => c.execute(context),
            DependencyCommand::RemoveSpawns(c) => c.execute(context),
            DependencyCommand::RemoveBlocks(c) => c.execute(context),
            DependencyCommand::RemoveRelates(c) => c.execute(context),
            DependencyCommand::CreateSubcard(c) => c.execute(context),
        }
    }

    pub fn description(&self) -> String {
        match self {
            DependencyCommand::AddSpawns(c) => c.description(),
            DependencyCommand::AddBlocks(c) => c.description(),
            DependencyCommand::AddRelates(c) => c.description(),
            DependencyCommand::RemoveSpawns(c) => c.description(),
            DependencyCommand::RemoveBlocks(c) => c.description(),
            DependencyCommand::RemoveRelates(c) => c.description(),
            DependencyCommand::CreateSubcard(c) => c.description(),
        }
    }

    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        match self {
            DependencyCommand::AddSpawns(c) => c.capture_inverse(store),
            DependencyCommand::AddBlocks(c) => c.capture_inverse(store),
            DependencyCommand::AddRelates(c) => c.capture_inverse(store),
            DependencyCommand::RemoveSpawns(c) => c.capture_inverse(store),
            DependencyCommand::RemoveBlocks(c) => c.capture_inverse(store),
            DependencyCommand::RemoveRelates(c) => c.capture_inverse(store),
            DependencyCommand::CreateSubcard(c) => c.capture_inverse(store),
        }
    }
}

// ────────────────────────────────────────────────────────────────────
// Add* commands: per-kind, carry the kind-specific metadata.
// ────────────────────────────────────────────────────────────────────

/// Add a parent->child Spawns edge. `source` is the parent,
/// `target` is the child.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddSpawns {
    pub source: Uuid,
    pub target: Uuid,
    /// When `true`, insert the edge already in the archived state.
    /// Used by cascade-undo (`DeleteCard` / `DeleteCardEdges`) to
    /// preserve the archive state of incident edges across delete/undo
    /// cycles. User-initiated `attach_child(ren)` paths leave this
    /// `false` (default) so edges land active.
    ///
    /// `#[serde(default)]` lets legacy command-log entries (pre-fix)
    /// deserialise with `false`, matching their original semantics.
    #[serde(default)]
    pub as_archived: bool,
}

impl AddSpawns {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let (source, target, as_archived) = (self.source, self.target, self.as_archived);
        context.store.modify_graph(Box::new(move |graph| {
            if as_archived {
                graph.add_archived_spawns(source, target)
            } else {
                graph.set_parent(target, source)
            }
        }))
    }

    pub fn description(&self) -> String {
        format!("Set parent: {} is parent of {}", self.source, self.target)
    }

    /// Inverse: per-kind [`RemoveSpawns`] with `tolerate_missing =
    /// true` so undo replay succeeds even if intervening state has
    /// already removed the edge. Per-kind tolerance keeps the inverse
    /// in the same edge kind as the forward — a `[AddSpawns(a,b),
    /// AddBlocks(a,b)]` batch now undoes each edge independently
    /// instead of having the first inverse wipe both kinds.
    pub fn capture_inverse(&self, _store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        Ok(vec![Command::Dependency(DependencyCommand::RemoveSpawns(
            RemoveSpawns {
                source: self.source,
                target: self.target,
                tolerate_missing: true,
            },
        ))])
    }
}

/// Add a blocker->blocked Blocks edge with a severity.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddBlocks {
    pub source: Uuid,
    pub target: Uuid,
    #[serde(default)]
    pub severity: Severity,
    /// See [`AddSpawns::as_archived`] for the cascade-undo rationale.
    #[serde(default)]
    pub as_archived: bool,
}

impl AddBlocks {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let (source, target, severity, as_archived) =
            (self.source, self.target, self.severity, self.as_archived);
        context.store.modify_graph(Box::new(move |graph| {
            if as_archived {
                graph.add_archived_blocks(source, target, severity)
            } else {
                graph.set_block_with_severity(source, target, severity)
            }
        }))
    }

    pub fn description(&self) -> String {
        format!(
            "Add blocks dependency ({:?}): {} blocks {}",
            self.severity, self.source, self.target
        )
    }

    /// Inverse: per-kind [`RemoveBlocks`] with `tolerate_missing =
    /// true`. See [`AddSpawns::capture_inverse`] for the rationale.
    pub fn capture_inverse(&self, _store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        Ok(vec![Command::Dependency(DependencyCommand::RemoveBlocks(
            RemoveBlocks {
                source: self.source,
                target: self.target,
                tolerate_missing: true,
            },
        ))])
    }
}

/// Add an undirected RelatesTo edge with a sub-kind.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AddRelates {
    pub source: Uuid,
    pub target: Uuid,
    #[serde(default)]
    pub kind: RelatesKind,
    /// See [`AddSpawns::as_archived`] for the cascade-undo rationale.
    #[serde(default)]
    pub as_archived: bool,
}

impl AddRelates {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let (source, target, kind, as_archived) =
            (self.source, self.target, self.kind, self.as_archived);
        context.store.modify_graph(Box::new(move |graph| {
            if as_archived {
                graph.add_archived_relates(source, target, kind)
            } else {
                graph.relate_with_kind(source, target, kind)
            }
        }))
    }

    pub fn description(&self) -> String {
        format!(
            "Add relates-to dependency ({:?}): {} <-> {}",
            self.kind, self.source, self.target
        )
    }

    /// Inverse: per-kind [`RemoveRelates`] with `tolerate_missing =
    /// true`. See [`AddSpawns::capture_inverse`] for the rationale.
    pub fn capture_inverse(&self, _store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        Ok(vec![Command::Dependency(DependencyCommand::RemoveRelates(
            RemoveRelates {
                source: self.source,
                target: self.target,
                tolerate_missing: true,
            },
        ))])
    }
}

// ────────────────────────────────────────────────────────────────────
// Remove* commands: per-kind. `tolerate_missing` decouples the
// undo-replay tolerance from kind-agnosticism. Edges are identified
// by (kind, source, target); the kind comes from the variant, so
// metadata fields stay scoped to add commands.
// ────────────────────────────────────────────────────────────────────

/// Remove a parent->child Spawns edge.
///
/// `tolerate_missing` controls behavior when the edge is absent at
/// execute time:
/// - `false` (default, user-initiated paths): returns
///   [`DependencyError::EdgeNotFound`] so the surface can render
///   "no such edge to remove" to the user.
/// - `true` (inverse-replay paths): swallows `EdgeNotFound` and
///   returns `Ok(())`. The undo invariant requires inverses to
///   succeed even if intervening state has already removed the edge.
///
/// The flag decouples *tolerance* (a replay concern) from
/// *kind-agnosticism* (a separate dimension). Each per-kind remove
/// stays in its own kind and chooses its tolerance at construction.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveSpawns {
    pub source: Uuid,
    pub target: Uuid,
    #[serde(default)]
    pub tolerate_missing: bool,
}

impl RemoveSpawns {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let (source, target, tolerate) = (self.source, self.target, self.tolerate_missing);
        context.store.modify_graph(Box::new(move |graph| {
            match graph.remove_parent(target, source) {
                Ok(()) => Ok(()),
                Err(e) if tolerate && e.is_edge_not_found() => Ok(()),
                Err(e) => Err(e),
            }
        }))
    }

    pub fn description(&self) -> String {
        format!(
            "Remove parent: {} is no longer parent of {}",
            self.source, self.target
        )
    }

    /// Inverse: re-add the parent edge (as active — user-initiated
    /// removes only fire against active edges, so the original state
    /// was active).
    pub fn capture_inverse(&self, _store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        Ok(vec![Command::Dependency(DependencyCommand::AddSpawns(
            AddSpawns {
                source: self.source,
                target: self.target,
                as_archived: false,
            },
        ))])
    }
}

/// Remove a blocker->blocked Blocks edge. See [`RemoveSpawns`] for the
/// `tolerate_missing` flag semantics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveBlocks {
    pub source: Uuid,
    pub target: Uuid,
    #[serde(default)]
    pub tolerate_missing: bool,
}

impl RemoveBlocks {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let (source, target, tolerate) = (self.source, self.target, self.tolerate_missing);
        context
            .store
            .modify_graph(Box::new(move |graph| match graph.unblock(source, target) {
                Ok(()) => Ok(()),
                Err(e) if tolerate && e.is_edge_not_found() => Ok(()),
                Err(e) => Err(e),
            }))
    }

    pub fn description(&self) -> String {
        format!(
            "Remove blocks dependency: {} no longer blocks {}",
            self.source, self.target
        )
    }

    /// Inverse: re-add the blocks edge. We don't know the original
    /// severity at remove time; the capture function walks the
    /// pre-remove graph to record it.
    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let graph = store.get_graph()?;
        let severity = graph
            .blocks_edges()
            .iter()
            .find(|e| e.source() == self.source && e.target() == self.target)
            .map(|e| e.severity)
            .unwrap_or_default();
        Ok(vec![Command::Dependency(DependencyCommand::AddBlocks(
            AddBlocks {
                source: self.source,
                target: self.target,
                severity,
                as_archived: false,
            },
        ))])
    }
}

/// Remove an undirected RelatesTo edge. See [`RemoveSpawns`] for the
/// `tolerate_missing` flag semantics.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoveRelates {
    pub source: Uuid,
    pub target: Uuid,
    #[serde(default)]
    pub tolerate_missing: bool,
}

impl RemoveRelates {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        let (source, target, tolerate) = (self.source, self.target, self.tolerate_missing);
        context.store.modify_graph(Box::new(move |graph| {
            match graph.dissociate(source, target) {
                Ok(()) => Ok(()),
                Err(e) if tolerate && e.is_edge_not_found() => Ok(()),
                Err(e) => Err(e),
            }
        }))
    }

    pub fn description(&self) -> String {
        format!(
            "Remove relates-to dependency: {} <-> {}",
            self.source, self.target
        )
    }

    /// Inverse: re-add the relates edge. Same as RemoveBlocks: we
    /// capture the kind from the pre-remove graph.
    pub fn capture_inverse(&self, store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        let graph = store.get_graph()?;
        let (a, b) = (self.source, self.target);
        let kind = graph
            .relates_edges()
            .iter()
            .find(|e| (e.source() == a && e.target() == b) || (e.source() == b && e.target() == a))
            .map(|e| e.kind)
            .unwrap_or_default();
        Ok(vec![Command::Dependency(DependencyCommand::AddRelates(
            AddRelates {
                source: self.source,
                target: self.target,
                kind,
                as_archived: false,
            },
        ))])
    }
}

// ────────────────────────────────────────────────────────────────────
// Inverse-replay helper.
// ────────────────────────────────────────────────────────────────────

/// Build inverse-replay `Add*` commands for every edge in `graph`
/// that matches `predicate`. Each per-kind sub-graph contributes its
/// matching edges with metadata (severity / kind) and archive state
/// preserved. Archived edges restore as archived; active edges restore
/// as active. Without this distinction, cascade-undo silently revived
/// archived incident edges to active state — losing the soft-delete
/// history that `archive_node` had recorded.
///
/// Used by the cascade capture-inverse sites that need to restore
/// edges of every kind touching one or more nodes:
/// - [`super::cascade_commands::DeleteCardEdges::capture_inverse`]
/// - [`super::card_commands::DeleteCard::capture_inverse`]
pub(super) fn edges_to_undo_commands<P>(
    graph: &crate::DependencyGraph,
    predicate: P,
) -> Vec<Command>
where
    P: Fn(Uuid, Uuid) -> bool,
{
    use kanban_core::Edge as _;
    let mut out = Vec::new();
    for e in graph.spawns_edges() {
        if predicate(e.source(), e.target()) {
            out.push(Command::Dependency(DependencyCommand::AddSpawns(
                AddSpawns {
                    source: e.source(),
                    target: e.target(),
                    as_archived: !e.is_active(),
                },
            )));
        }
    }
    for e in graph.blocks_edges() {
        if predicate(e.source(), e.target()) {
            out.push(Command::Dependency(DependencyCommand::AddBlocks(
                AddBlocks {
                    source: e.source(),
                    target: e.target(),
                    severity: e.severity,
                    as_archived: !e.is_active(),
                },
            )));
        }
    }
    for e in graph.relates_edges() {
        if predicate(e.source(), e.target()) {
            out.push(Command::Dependency(DependencyCommand::AddRelates(
                AddRelates {
                    source: e.source(),
                    target: e.target(),
                    kind: e.kind,
                    as_archived: !e.is_active(),
                },
            )));
        }
    }
    out
}

/// Create a new card as a subcard of a parent card
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateSubcardCommand {
    /// Stable id for the new subcard, baked in at construction so undo
    /// (KAN-191) can target a DeleteCard at the right id without needing
    /// to read post-execute state.
    #[serde(default = "Uuid::new_v4")]
    pub id: Uuid,
    pub parent_id: Uuid,
    pub board_id: Uuid,
    pub column_id: Uuid,
    pub title: String,
    pub description: Option<String>,
    pub position: i32,
}

impl CreateSubcardCommand {
    pub fn execute(&self, context: &CommandContext) -> KanbanResult<()> {
        context.get_card(self.parent_id)?;
        let mut board = context.get_board(self.board_id)?;
        let mut card = Card::new(
            &mut board,
            self.column_id,
            self.title.clone(),
            self.position,
        );
        card.id = self.id;

        if let Some(desc) = &self.description {
            card.description = Some(desc.clone());
        }

        let card_id = card.id;
        let parent_id = self.parent_id;
        context.store.upsert_board(board)?;
        context.store.upsert_card(card)?;

        context
            .store
            .modify_graph(Box::new(move |graph| graph.set_parent(card_id, parent_id)))
    }

    pub fn description(&self) -> String {
        format!(
            "Create subcard '{}' under parent {}",
            self.title, self.parent_id
        )
    }

    /// Inverse: delete the new card. `DeleteCard` is polymorphic over
    /// live / archived and strips incident graph edges, so the parent
    /// edge added by the forward is cleaned up in the same step. The
    /// board's `card_counter` stays bumped; redo reproduces the same
    /// id and number.
    pub fn capture_inverse(&self, _store: &dyn DataStore) -> KanbanResult<Vec<Command>> {
        Ok(vec![Command::Card(
            super::card_commands::CardCommand::Delete(super::card_commands::DeleteCard {
                card_id: self.id,
            }),
        )])
    }
}

#[cfg(test)]
mod tests {
    use super::super::test_helpers::TestContext;
    use super::*;
    use crate::DataStore;

    #[test]
    fn test_add_spawns_executes() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let parent_id = Uuid::new_v4();
        let child_id = Uuid::new_v4();
        assert!(AddSpawns {
            source: parent_id,
            target: child_id,
            as_archived: false,
        }
        .execute(&context)
        .is_ok());
        let graph = tc.store.get_graph().unwrap();
        assert_eq!(graph.children(parent_id), vec![child_id]);
    }

    #[test]
    fn test_add_spawns_prevents_cycle() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        assert!(AddSpawns {
            source: a,
            target: b,
            as_archived: false,
        }
        .execute(&context)
        .is_ok());
        assert!(AddSpawns {
            source: b,
            target: a,
            as_archived: false,
        }
        .execute(&context)
        .is_err());
    }

    #[test]
    fn test_add_blocks_preserves_severity() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let blocker = Uuid::new_v4();
        let blocked = Uuid::new_v4();
        AddBlocks {
            source: blocker,
            target: blocked,
            severity: Severity::High,
            as_archived: false,
        }
        .execute(&context)
        .unwrap();
        let graph = tc.store.get_graph().unwrap();
        assert_eq!(graph.blocks_edges()[0].severity, Severity::High);
    }

    #[test]
    fn test_add_relates_preserves_kind() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        AddRelates {
            source: a,
            target: b,
            kind: RelatesKind::Duplicates,
            as_archived: false,
        }
        .execute(&context)
        .unwrap();
        let graph = tc.store.get_graph().unwrap();
        assert_eq!(graph.relates_edges()[0].kind, RelatesKind::Duplicates);
    }

    #[test]
    fn test_remove_spawns_executes() {
        let tc = TestContext::new();
        let parent_id = Uuid::new_v4();
        let child_id = Uuid::new_v4();
        {
            let mut graph = tc.store.get_graph().unwrap();
            graph.set_parent(child_id, parent_id).unwrap();
            tc.store.set_graph(graph).unwrap();
        }
        let context = tc.as_command_context();
        assert!(RemoveSpawns {
            source: parent_id,
            target: child_id,
            tolerate_missing: false,
        }
        .execute(&context)
        .is_ok());
        let graph = tc.store.get_graph().unwrap();
        assert_eq!(graph.children(parent_id).len(), 0);
    }

    #[test]
    fn test_remove_blocks_inverse_captures_severity_from_pre_remove_graph() {
        let tc = TestContext::new();
        let blocker = Uuid::new_v4();
        let blocked = Uuid::new_v4();
        {
            let mut graph = tc.store.get_graph().unwrap();
            graph
                .set_block_with_severity(blocker, blocked, Severity::Critical)
                .unwrap();
            tc.store.set_graph(graph).unwrap();
        }
        let cmd = RemoveBlocks {
            source: blocker,
            target: blocked,
            tolerate_missing: false,
        };
        let inverse = cmd.capture_inverse(&tc.store).unwrap();
        match &inverse[0] {
            Command::Dependency(DependencyCommand::AddBlocks(a)) => {
                assert_eq!(a.severity, Severity::Critical);
            }
            other => panic!("expected AddBlocks inverse, got {other:?}"),
        }
    }

    #[test]
    fn test_remove_relates_inverse_captures_kind_from_pre_remove_graph() {
        let tc = TestContext::new();
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        {
            let mut graph = tc.store.get_graph().unwrap();
            graph
                .relate_with_kind(a, b, RelatesKind::Duplicates)
                .unwrap();
            tc.store.set_graph(graph).unwrap();
        }
        let cmd = RemoveRelates {
            source: a,
            target: b,
            tolerate_missing: false,
        };
        let inverse = cmd.capture_inverse(&tc.store).unwrap();
        match &inverse[0] {
            Command::Dependency(DependencyCommand::AddRelates(a)) => {
                assert_eq!(a.kind, RelatesKind::Duplicates);
            }
            other => panic!("expected AddRelates inverse, got {other:?}"),
        }
    }

    /// Inverse round-trip across every Severity variant. A single-variant
    /// test (Critical above) doesn't catch the case where the impl
    /// silently casts or defaults a non-Default variant. Parameterised
    /// over Low / Medium / High / Critical — if any variant gets dropped
    /// the test fails on that variant.
    #[test]
    fn test_remove_blocks_inverse_preserves_severity_across_all_variants() {
        for severity in [
            Severity::Low,
            Severity::Medium,
            Severity::High,
            Severity::Critical,
        ] {
            let tc = TestContext::new();
            let blocker = Uuid::new_v4();
            let blocked = Uuid::new_v4();
            {
                let mut graph = tc.store.get_graph().unwrap();
                graph
                    .set_block_with_severity(blocker, blocked, severity)
                    .unwrap();
                tc.store.set_graph(graph).unwrap();
            }
            let cmd = RemoveBlocks {
                source: blocker,
                target: blocked,
                tolerate_missing: false,
            };
            let inverse = cmd.capture_inverse(&tc.store).unwrap();
            match &inverse[0] {
                Command::Dependency(DependencyCommand::AddBlocks(a)) => {
                    assert_eq!(
                        a.severity, severity,
                        "severity {severity:?} must round-trip through capture_inverse"
                    );
                }
                other => panic!("expected AddBlocks inverse for {severity:?}, got {other:?}"),
            }
        }
    }

    /// Same shape for RelatesKind. The undirected sub-graph means
    /// orientation matching also matters — see the dedicated test
    /// below.
    #[test]
    fn test_remove_relates_inverse_preserves_kind_across_all_variants() {
        for kind in [
            RelatesKind::General,
            RelatesKind::Duplicates,
            RelatesKind::MentionedIn,
        ] {
            let tc = TestContext::new();
            let a = Uuid::new_v4();
            let b = Uuid::new_v4();
            {
                let mut graph = tc.store.get_graph().unwrap();
                graph.relate_with_kind(a, b, kind).unwrap();
                tc.store.set_graph(graph).unwrap();
            }
            let cmd = RemoveRelates {
                source: a,
                target: b,
                tolerate_missing: false,
            };
            let inverse = cmd.capture_inverse(&tc.store).unwrap();
            match &inverse[0] {
                Command::Dependency(DependencyCommand::AddRelates(a)) => {
                    assert_eq!(
                        a.kind, kind,
                        "kind {kind:?} must round-trip through capture_inverse"
                    );
                }
                other => panic!("expected AddRelates inverse for {kind:?}, got {other:?}"),
            }
        }
    }

    /// `as_archived: true` on `AddSpawns` inserts the edge already in
    /// the archived state. Used by cascade-undo to preserve the
    /// archive state of incident edges across DeleteCard / undo
    /// cycles. Without this branch, restoring an archived incident
    /// edge as active would silently lose soft-delete history.
    #[test]
    fn test_add_spawns_with_as_archived_true_inserts_archived_edge() {
        use kanban_core::Edge as _;
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let parent = Uuid::new_v4();
        let child = Uuid::new_v4();
        AddSpawns {
            source: parent,
            target: child,
            as_archived: true,
        }
        .execute(&context)
        .unwrap();
        let graph = tc.store.get_graph().unwrap();
        // Active accessors must not see the edge.
        assert!(graph.children(parent).is_empty(), "active children empty");
        // Archived history must contain it.
        let edges = graph.spawns_edges();
        assert_eq!(edges.len(), 1, "edge present in history");
        assert!(!edges[0].is_active(), "edge is archived");
    }

    #[test]
    fn test_add_blocks_with_as_archived_true_inserts_archived_edge_with_severity() {
        use kanban_core::Edge as _;
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let blocker = Uuid::new_v4();
        let blocked = Uuid::new_v4();
        AddBlocks {
            source: blocker,
            target: blocked,
            severity: Severity::Critical,
            as_archived: true,
        }
        .execute(&context)
        .unwrap();
        let graph = tc.store.get_graph().unwrap();
        assert!(graph.blocked(blocker).is_empty(), "active blocked empty");
        let edges = graph.blocks_edges();
        assert_eq!(edges.len(), 1);
        assert!(!edges[0].is_active(), "edge archived");
        assert_eq!(
            edges[0].severity,
            Severity::Critical,
            "severity preserved through archived insert"
        );
    }

    #[test]
    fn test_add_relates_with_as_archived_true_inserts_archived_edge_with_kind() {
        use kanban_core::Edge as _;
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        AddRelates {
            source: a,
            target: b,
            kind: RelatesKind::Duplicates,
            as_archived: true,
        }
        .execute(&context)
        .unwrap();
        let graph = tc.store.get_graph().unwrap();
        assert!(graph.related(a).is_empty(), "active related empty");
        let edges = graph.relates_edges();
        assert_eq!(edges.len(), 1);
        assert!(!edges[0].is_active(), "edge archived");
        assert_eq!(
            edges[0].kind,
            RelatesKind::Duplicates,
            "kind preserved through archived insert"
        );
    }

    /// Backwards-compat: legacy command-log entries that pre-date the
    /// `as_archived` field deserialise with `as_archived = false`
    /// (active), matching the original semantics. `#[serde(default)]`
    /// keeps replay equivalence for all old logs.
    #[test]
    fn test_add_spawns_legacy_json_without_as_archived_defaults_to_false() {
        let source = Uuid::nil();
        let target = Uuid::from_u128(0x42);
        let legacy: DependencyCommand = serde_json::from_value(serde_json::json!({
            "action": "add_spawns",
            "source": source,
            "target": target,
        }))
        .expect("legacy add_spawns without as_archived must deserialise");
        match legacy {
            DependencyCommand::AddSpawns(a) => {
                assert!(
                    !a.as_archived,
                    "default must be active for backwards-compat"
                );
            }
            other => panic!("expected AddSpawns, got {other:?}"),
        }
    }

    /// RelatesEdge is undirected: if the user added with (a, b) but
    /// removes with (b, a), `RemoveRelates::capture_inverse` must still
    /// find the original edge and recover its kind. The impl uses
    /// either-orientation matching for exactly this reason — pinning
    /// the property protects it from a silent regression to
    /// directional-only matching.
    #[test]
    fn test_remove_relates_inverse_finds_edge_in_reversed_orientation() {
        let tc = TestContext::new();
        let a = Uuid::new_v4();
        let b = Uuid::new_v4();
        {
            let mut graph = tc.store.get_graph().unwrap();
            // Edge added in (a, b) orientation
            graph
                .relate_with_kind(a, b, RelatesKind::MentionedIn)
                .unwrap();
            tc.store.set_graph(graph).unwrap();
        }
        // Remove issued in (b, a) orientation
        let cmd = RemoveRelates {
            source: b,
            target: a,
            tolerate_missing: false,
        };
        let inverse = cmd.capture_inverse(&tc.store).unwrap();
        match &inverse[0] {
            Command::Dependency(DependencyCommand::AddRelates(restored)) => {
                assert_eq!(
                    restored.kind,
                    RelatesKind::MentionedIn,
                    "reversed-orientation remove must still recover the original kind"
                );
            }
            other => panic!("expected AddRelates inverse, got {other:?}"),
        }
    }

    /// Per-kind inverse: an AddSpawns undoes via a tolerant RemoveSpawns,
    /// not a kind-agnostic Remove. A `[AddSpawns(a,b), AddBlocks(a,b)]`
    /// batch's reverse-order undo now leaves each forward independently
    /// undone instead of having the first inverse wipe both kinds.
    #[test]
    fn test_add_spawns_inverse_is_tolerant_remove_spawns() {
        let tc = TestContext::new();
        let cmd = AddSpawns {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            as_archived: false,
        };
        let inverse = cmd.capture_inverse(&tc.store).unwrap();
        assert_eq!(inverse.len(), 1);
        match &inverse[0] {
            Command::Dependency(DependencyCommand::RemoveSpawns(r)) => {
                assert!(r.tolerate_missing, "undo inverse must tolerate missing");
            }
            other => panic!("expected RemoveSpawns inverse, got {other:?}"),
        }
    }

    #[test]
    fn test_add_blocks_inverse_is_tolerant_remove_blocks() {
        let tc = TestContext::new();
        let cmd = AddBlocks {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            severity: Severity::High,
            as_archived: false,
        };
        let inverse = cmd.capture_inverse(&tc.store).unwrap();
        assert_eq!(inverse.len(), 1);
        match &inverse[0] {
            Command::Dependency(DependencyCommand::RemoveBlocks(r)) => {
                assert!(r.tolerate_missing, "undo inverse must tolerate missing");
            }
            other => panic!("expected RemoveBlocks inverse, got {other:?}"),
        }
    }

    #[test]
    fn test_add_relates_inverse_is_tolerant_remove_relates() {
        let tc = TestContext::new();
        let cmd = AddRelates {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            kind: RelatesKind::Duplicates,
            as_archived: false,
        };
        let inverse = cmd.capture_inverse(&tc.store).unwrap();
        assert_eq!(inverse.len(), 1);
        match &inverse[0] {
            Command::Dependency(DependencyCommand::RemoveRelates(r)) => {
                assert!(r.tolerate_missing, "undo inverse must tolerate missing");
            }
            other => panic!("expected RemoveRelates inverse, got {other:?}"),
        }
    }

    /// `tolerate_missing = true` swallows EdgeNotFound; the
    /// user-initiated path (default `false`) propagates the error so
    /// the surface can render "no such edge to remove". This decouples
    /// replay tolerance from kind-agnosticism.
    #[test]
    fn test_remove_spawns_tolerant_succeeds_on_missing_edge() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let result = RemoveSpawns {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            tolerate_missing: true,
        }
        .execute(&context);
        assert!(result.is_ok(), "tolerant remove must swallow EdgeNotFound");
    }

    #[test]
    fn test_remove_spawns_strict_errors_on_missing_edge() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let result = RemoveSpawns {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            tolerate_missing: false,
        }
        .execute(&context);
        assert!(
            result.unwrap_err().is_edge_not_found(),
            "strict remove must propagate EdgeNotFound"
        );
    }

    #[test]
    fn test_remove_blocks_tolerant_succeeds_on_missing_edge() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let result = RemoveBlocks {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            tolerate_missing: true,
        }
        .execute(&context);
        assert!(result.is_ok());
    }

    #[test]
    fn test_remove_relates_tolerant_succeeds_on_missing_edge() {
        let tc = TestContext::new();
        let context = tc.as_command_context();
        let result = RemoveRelates {
            source: Uuid::new_v4(),
            target: Uuid::new_v4(),
            tolerate_missing: true,
        }
        .execute(&context);
        assert!(result.is_ok());
    }

    /// Pin the on-disk JSON shape of `DependencyCommand` variants so a
    /// future SQLite command-log wiring (the schema exists today but
    /// the writer is dormant) treats the collapsed variant names as a
    /// backwards-compatibility contract. Any rename or reshape will
    /// fail this test loudly rather than silently breaking replay.
    #[test]
    fn test_dependency_command_serialization_shape_is_stable() {
        let source = Uuid::nil();
        let target = Uuid::from_u128(0x42);

        // Per-kind add variants
        let add_spawns = DependencyCommand::AddSpawns(AddSpawns {
            source,
            target,
            as_archived: false,
        });
        let json = serde_json::to_value(&add_spawns).unwrap();
        assert_eq!(json["action"], "add_spawns");

        let add_blocks = DependencyCommand::AddBlocks(AddBlocks {
            source,
            target,
            severity: Severity::High,
            as_archived: false,
        });
        let json = serde_json::to_value(&add_blocks).unwrap();
        assert_eq!(json["action"], "add_blocks");
        assert_eq!(json["severity"], "High");

        let add_relates = DependencyCommand::AddRelates(AddRelates {
            source,
            target,
            kind: RelatesKind::Duplicates,
            as_archived: false,
        });
        let json = serde_json::to_value(&add_relates).unwrap();
        assert_eq!(json["action"], "add_relates");
        assert_eq!(json["kind"], "Duplicates");

        // Per-kind remove variants
        let remove_blocks = DependencyCommand::RemoveBlocks(RemoveBlocks {
            source,
            target,
            tolerate_missing: false,
        });
        let json = serde_json::to_value(&remove_blocks).unwrap();
        assert_eq!(json["action"], "remove_blocks");
        assert_eq!(json["tolerate_missing"], false);

        // Backwards-compat: pre-tolerance JSON (no `tolerate_missing`
        // field) deserialises with `tolerate_missing = false` via
        // `#[serde(default)]`. Old command-log entries stay valid.
        let legacy: DependencyCommand = serde_json::from_value(serde_json::json!({
            "action": "remove_spawns",
            "source": source,
            "target": target
        }))
        .expect("legacy remove_spawns without tolerate_missing must deserialise");
        match legacy {
            DependencyCommand::RemoveSpawns(r) => {
                assert!(!r.tolerate_missing, "default must be strict");
            }
            other => panic!("expected RemoveSpawns, got {other:?}"),
        }

        // Round-trip
        let round: DependencyCommand =
            serde_json::from_value(serde_json::to_value(&add_blocks).unwrap()).unwrap();
        assert!(matches!(
            round,
            DependencyCommand::AddBlocks(AddBlocks {
                severity: Severity::High,
                ..
            })
        ));
    }

    #[test]
    fn test_create_subcard_command() {
        use crate::Board;

        let tc = TestContext::new();
        let column_id = Uuid::new_v4();

        let mut board = Board::new("Test Board", None::<String>);
        board.card_prefix = Some("TEST".to_string());
        let board_id = board.id;
        let parent = crate::Card::new(&mut board, column_id, "Parent", 0);
        let parent_id = parent.id;
        tc.store.upsert_board(board).unwrap();
        tc.store.upsert_card(parent).unwrap();

        let context = tc.as_command_context();
        let cmd = CreateSubcardCommand {
            id: Uuid::new_v4(),
            parent_id,
            board_id,
            column_id,
            title: "Test Subcard".to_string(),
            description: Some("Test description".to_string()),
            position: 0,
        };

        assert!(cmd.execute(&context).is_ok());

        let cards = tc.store.list_all_cards().unwrap();
        assert_eq!(cards.len(), 2);
        let card = cards.iter().find(|c| c.title == "Test Subcard").unwrap();
        assert_eq!(card.description, Some("Test description".to_string()));
        assert_eq!(card.column_id, column_id);

        let graph = tc.store.get_graph().unwrap();
        assert_eq!(graph.children(parent_id).len(), 1);
        assert!(graph.children(parent_id).contains(&card.id));
    }

    #[test]
    fn test_create_subcard_with_nonexistent_parent_returns_not_found() {
        let tc = TestContext::new();
        let board = crate::Board::new("B", Some("TST"));
        let col = crate::Column::new(board.id, "Col", 0);
        let board_id = board.id;
        let column_id = col.id;
        tc.store.upsert_board(board).unwrap();
        tc.store.upsert_column(col).unwrap();

        let context = tc.as_command_context();
        let cmd = CreateSubcardCommand {
            id: Uuid::new_v4(),
            parent_id: Uuid::new_v4(),
            board_id,
            column_id,
            title: "Subcard".to_string(),
            description: None,
            position: 0,
        };
        let result = cmd.execute(&context);
        assert!(result.is_err(), "Expected error for nonexistent parent");
        assert!(result.unwrap_err().is_not_found());
    }
}