miwa-cli 0.7.0

doc / req / test の3層の仕様乖離(ドリフト)を検知する CLI
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
//! `check`: compare the current graph against the lock and report findings.

#[derive(
    Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, serde::Serialize, serde::Deserialize,
)]
pub enum FindingKind {
    Drift,
    New,
    Removed,
    Broken,
    Orphan,
    NoTest,
    Invalid,
    Index,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)]
pub struct Finding {
    pub kind: FindingKind,
    pub target_kind: crate::model::TargetKind,
    pub target: String,
    pub reason: crate::model::ReasonCode,
    pub detail: String,
}

/// Findings are sorted by `(kind, target_kind, target, reason)`.
pub fn check(graph: &crate::model::Graph, lock: &crate::lock::Lock) -> Vec<Finding> {
    use crate::model::{EdgeKind, NodeKind, ReasonCode, TargetKind};

    let mut findings = Vec::new();

    for (edge_id, edge) in &graph.edges {
        let (from_hash, to_hash) = crate::model::edge_hashes(graph, edge);
        match lock.edges.get(edge_id) {
            Some(entry) => {
                let from_changed = entry.from != from_hash;
                let to_changed = entry.to != to_hash;
                if from_changed || to_changed {
                    let reason = match (from_changed, to_changed, edge.kind) {
                        (true, true, _) => ReasonCode::BothChanged,
                        (true, false, EdgeKind::DocReq) => ReasonCode::DocChanged,
                        (true, false, EdgeKind::TestReq) => ReasonCode::TestChanged,
                        (true, false, EdgeKind::Desc) => ReasonCode::DescriptionChanged,
                        (false, true, EdgeKind::Desc) => ReasonCode::BodyChanged,
                        (false, true, _) => ReasonCode::ReqChanged,
                        (false, false, _) => {
                            unreachable!("guarded by from_changed || to_changed")
                        }
                    };
                    findings.push(Finding {
                        kind: FindingKind::Drift,
                        target_kind: TargetKind::Edge,
                        target: edge_id.clone(),
                        reason,
                        detail: format!("edge {edge_id} drifted from the agreed state"),
                    });
                }
            }
            None => {
                findings.push(Finding {
                    kind: FindingKind::New,
                    target_kind: TargetKind::Edge,
                    target: edge_id.clone(),
                    reason: ReasonCode::NewEdge,
                    detail: format!("edge {edge_id} is not recorded in the lock"),
                });
            }
        }
    }

    for edge_id in lock.edges.keys() {
        if !graph.edges.contains_key(edge_id) {
            findings.push(Finding {
                kind: FindingKind::Removed,
                target_kind: TargetKind::Edge,
                target: edge_id.clone(),
                reason: ReasonCode::RemovedEdge,
                detail: format!("edge {edge_id} no longer exists in the source"),
            });
        }
    }

    // Precompute, in a single pass over the edges, which docs/reqs already
    // have an incoming/outgoing edge, so the orphan/no-test checks below can
    // look each node up in O(1) instead of re-scanning all edges per node.
    let mut docs_with_req: std::collections::HashSet<&crate::model::NodeKey> =
        std::collections::HashSet::new();
    let mut reqs_with_doc: std::collections::HashSet<&crate::model::NodeKey> =
        std::collections::HashSet::new();
    let mut reqs_with_test: std::collections::HashSet<&crate::model::NodeKey> =
        std::collections::HashSet::new();
    for edge in graph.edges.values() {
        match edge.kind {
            EdgeKind::DocReq => {
                docs_with_req.insert(&edge.from);
                reqs_with_doc.insert(&edge.to);
            }
            EdgeKind::TestReq => {
                reqs_with_test.insert(&edge.to);
            }
            EdgeKind::Desc => {}
        }
    }

    for node in graph.nodes.values() {
        match node.key.kind {
            NodeKind::Req => {
                if !reqs_with_doc.contains(&node.key) {
                    findings.push(Finding {
                        kind: FindingKind::Orphan,
                        target_kind: TargetKind::Req,
                        target: node.key.id.clone(),
                        reason: ReasonCode::ReqWithoutDoc,
                        detail: format!("{} is not linked from any doc", node.key.id),
                    });
                }
                if !reqs_with_test.contains(&node.key) {
                    findings.push(Finding {
                        kind: FindingKind::NoTest,
                        target_kind: TargetKind::Req,
                        target: node.key.id.clone(),
                        reason: ReasonCode::ReqWithoutTest,
                        detail: format!("{} has no verifying test", node.key.id),
                    });
                }
            }
            NodeKind::Doc => {
                if !docs_with_req.contains(&node.key) {
                    findings.push(Finding {
                        kind: FindingKind::Orphan,
                        target_kind: TargetKind::Doc,
                        target: node.key.id.clone(),
                        reason: ReasonCode::DocWithoutReq,
                        detail: format!("{} does not link to any req", node.key.id),
                    });
                }
            }
            NodeKind::Test => {}
        }
    }

    for invalid in &graph.invalid {
        findings.push(Finding {
            kind: FindingKind::Invalid,
            target_kind: invalid.target_kind,
            target: invalid.target.clone(),
            reason: invalid.reason,
            detail: invalid.detail.clone(),
        });
    }

    for broken in &graph.broken {
        let target_kind = match broken.from.kind {
            NodeKind::Doc => TargetKind::Doc,
            NodeKind::Test => TargetKind::Test,
            NodeKind::Req => TargetKind::Req,
        };
        findings.push(Finding {
            kind: FindingKind::Broken,
            target_kind,
            target: broken.from.id.clone(),
            reason: broken.reason,
            detail: broken.detail.clone(),
        });
    }

    for issue in &graph.index_issues {
        findings.push(Finding {
            kind: FindingKind::Index,
            target_kind: TargetKind::Index,
            target: issue.target.clone(),
            reason: issue.reason,
            detail: issue.detail.clone(),
        });
    }

    findings.sort();
    findings
}

/// `current - base`, keyed by `(kind, target_kind, target, reason)`.
pub fn diff_findings(current: &[Finding], base: &[Finding]) -> Vec<Finding> {
    let base_keys: std::collections::HashSet<_> = base
        .iter()
        .map(|f| (f.kind, f.target_kind, f.target.clone(), f.reason))
        .collect();
    current
        .iter()
        .filter(|f| !base_keys.contains(&(f.kind, f.target_kind, f.target.clone(), f.reason)))
        .cloned()
        .collect()
}

/// Returns 2 if `gate` and `findings` is non-empty, else 0.
pub fn exit_code(findings: &[Finding], gate: bool) -> i32 {
    if gate && !findings.is_empty() {
        2
    } else {
        0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::lock::{Lock, LockEntry};
    use crate::model::{Edge, EdgeKind, Graph, Node, NodeKey, NodeKind, ReasonCode};

    fn node(kind: NodeKind, id: &str, hash: &str) -> Node {
        Node {
            key: NodeKey {
                kind,
                id: id.to_string(),
            },
            path: format!("{id}.md"),
            content: String::new(),
            hash: hash.to_string(),
            description: None,
        }
    }

    fn lock_entry(from: &str, to: &str) -> LockEntry {
        LockEntry {
            from: from.to_string(),
            to: to.to_string(),
            by: "human".to_string(),
            reason: None,
            score: None,
            at: "2026-01-01T00:00:00Z".to_string(),
        }
    }

    fn doc_req_graph(doc_hash: &str, req_hash: &str) -> Graph {
        let mut graph = Graph::default();
        let doc_key = NodeKey {
            kind: NodeKind::Doc,
            id: "DOC-A".to_string(),
        };
        let req_key = NodeKey {
            kind: NodeKind::Req,
            id: "FR-001".to_string(),
        };
        graph
            .nodes
            .insert(doc_key.clone(), node(NodeKind::Doc, "DOC-A", doc_hash));
        graph
            .nodes
            .insert(req_key.clone(), node(NodeKind::Req, "FR-001", req_hash));
        let edge_id = "doc-req:DOC-A:FR-001".to_string();
        graph.edges.insert(
            edge_id.clone(),
            Edge {
                id: edge_id,
                kind: EdgeKind::DocReq,
                from: doc_key,
                to: req_key,
            },
        );
        graph
    }

    fn test_req_graph(test_hash: &str, req_hash: &str) -> Graph {
        let mut graph = Graph::default();
        let test_key = NodeKey {
            kind: NodeKind::Test,
            id: "src/foo::bar".to_string(),
        };
        let req_key = NodeKey {
            kind: NodeKind::Req,
            id: "FR-001".to_string(),
        };
        graph.nodes.insert(
            test_key.clone(),
            node(NodeKind::Test, "src/foo::bar", test_hash),
        );
        graph
            .nodes
            .insert(req_key.clone(), node(NodeKind::Req, "FR-001", req_hash));
        let edge_id = "test-req:src/foo::bar:FR-001".to_string();
        graph.edges.insert(
            edge_id.clone(),
            Edge {
                id: edge_id,
                kind: EdgeKind::TestReq,
                from: test_key,
                to: req_key,
            },
        );
        graph
    }

    // [FR-024]
    #[test]
    fn testだけ変わった場合はtest_changedとしてdriftを報告する() {
        let graph = test_req_graph("sha256:test-new", "sha256:req-same");
        let mut lock = Lock::default();
        lock.edges.insert(
            "test-req:src/foo::bar:FR-001".to_string(),
            lock_entry("sha256:test-old", "sha256:req-same"),
        );
        let findings = check(&graph, &lock);
        assert!(findings.iter().any(|f| {
            f.kind == FindingKind::Drift
                && f.reason == ReasonCode::TestChanged
                && f.target == "test-req:src/foo::bar:FR-001"
        }));
    }

    // [FR-024]
    #[test]
    fn docだけ変わった場合はdoc_changedとしてdriftを報告する() {
        let graph = doc_req_graph("sha256:doc-new", "sha256:req-same");
        let mut lock = Lock::default();
        lock.edges.insert(
            "doc-req:DOC-A:FR-001".to_string(),
            lock_entry("sha256:doc-old", "sha256:req-same"),
        );
        let findings = check(&graph, &lock);
        assert!(findings.iter().any(|f| f.kind == FindingKind::Drift
            && f.reason == ReasonCode::DocChanged
            && f.target == "doc-req:DOC-A:FR-001"));
    }

    // [FR-024]
    #[test]
    fn reqだけ変わった場合はreq_changedとしてdriftを報告する() {
        let graph = doc_req_graph("sha256:doc-same", "sha256:req-new");
        let mut lock = Lock::default();
        lock.edges.insert(
            "doc-req:DOC-A:FR-001".to_string(),
            lock_entry("sha256:doc-same", "sha256:req-old"),
        );
        let findings = check(&graph, &lock);
        assert!(findings
            .iter()
            .any(|f| f.kind == FindingKind::Drift && f.reason == ReasonCode::ReqChanged));
    }

    // [FR-024]
    #[test]
    fn 両側が変わった場合はboth_changedとして1件だけ報告する() {
        let graph = doc_req_graph("sha256:doc-new", "sha256:req-new");
        let mut lock = Lock::default();
        lock.edges.insert(
            "doc-req:DOC-A:FR-001".to_string(),
            lock_entry("sha256:doc-old", "sha256:req-old"),
        );
        let findings = check(&graph, &lock);
        let drifts: Vec<_> = findings
            .iter()
            .filter(|f| f.kind == FindingKind::Drift && f.target == "doc-req:DOC-A:FR-001")
            .collect();
        assert_eq!(
            drifts.len(),
            1,
            "expected exactly one drift finding: {drifts:?}"
        );
        assert_eq!(drifts[0].reason, ReasonCode::BothChanged);
    }

    // [FR-025]
    #[test]
    fn ロックにないエッジはnewとして報告する() {
        let graph = doc_req_graph("sha256:doc", "sha256:req");
        let lock = Lock::default();
        let findings = check(&graph, &lock);
        assert!(findings.iter().any(|f| f.kind == FindingKind::New
            && f.target == "doc-req:DOC-A:FR-001"
            && f.reason == ReasonCode::NewEdge));
    }

    // [FR-025]
    #[test]
    fn ソースから消えたエッジはremovedとして報告する() {
        let mut graph = Graph::default();
        graph.nodes.insert(
            NodeKey {
                kind: NodeKind::Req,
                id: "FR-001".to_string(),
            },
            node(NodeKind::Req, "FR-001", "sha256:req"),
        );
        let mut lock = Lock::default();
        lock.edges.insert(
            "doc-req:DOC-A:FR-001".to_string(),
            lock_entry("sha256:doc-old", "sha256:req-old"),
        );
        let findings = check(&graph, &lock);
        assert!(findings.iter().any(|f| {
            f.kind == FindingKind::Removed
                && f.target == "doc-req:DOC-A:FR-001"
                && f.reason == ReasonCode::RemovedEdge
        }));
    }

    // [FR-026]
    #[test]
    fn どのdocからもリンクされないreqはorphanになる() {
        let mut graph = Graph::default();
        graph.nodes.insert(
            NodeKey {
                kind: NodeKind::Req,
                id: "FR-001".to_string(),
            },
            node(NodeKind::Req, "FR-001", "sha256:req"),
        );
        let findings = check(&graph, &Lock::default());
        assert!(findings.iter().any(|f| {
            f.kind == FindingKind::Orphan
                && f.reason == ReasonCode::ReqWithoutDoc
                && f.target == "FR-001"
        }));
    }

    // [FR-026]
    #[test]
    fn どのreqにもリンクしないdocはorphanになる() {
        let mut graph = Graph::default();
        graph.nodes.insert(
            NodeKey {
                kind: NodeKind::Doc,
                id: "DOC-A".to_string(),
            },
            node(NodeKind::Doc, "DOC-A", "sha256:doc"),
        );
        let findings = check(&graph, &Lock::default());
        assert!(findings.iter().any(|f| {
            f.kind == FindingKind::Orphan
                && f.reason == ReasonCode::DocWithoutReq
                && f.target == "DOC-A"
        }));
    }

    // [FR-027]
    #[test]
    fn テストで検証されていないreqはno_testになる() {
        let graph = doc_req_graph("sha256:doc", "sha256:req");
        let findings = check(&graph, &Lock::default());
        assert!(findings.iter().any(|f| {
            f.kind == FindingKind::NoTest
                && f.reason == ReasonCode::ReqWithoutTest
                && f.target == "FR-001"
        }));
    }

    // [FR-030]
    #[test]
    fn exit_codeはgate指定かつfindingがあれば2を返す() {
        let graph = doc_req_graph("sha256:doc", "sha256:req");
        let findings = check(&graph, &Lock::default());
        assert!(!findings.is_empty());
        assert_eq!(exit_code(&findings, true), 2);
    }

    // [FR-030]
    #[test]
    fn exit_codeはgateなしなら常に0を返す() {
        let graph = doc_req_graph("sha256:doc", "sha256:req");
        let findings = check(&graph, &Lock::default());
        assert!(!findings.is_empty());
        assert_eq!(exit_code(&findings, false), 0);
        assert_eq!(exit_code(&[], false), 0);
    }

    // [FR-030]
    #[test]
    fn exit_codeはfindingが0件でgateがあっても0を返す() {
        assert_eq!(exit_code(&[], true), 0);
    }

    // [FR-031]
    #[test]
    fn diff_findingsは種別_対象種別_対象_理由が一致するものを除外する() {
        let base_finding = Finding {
            kind: FindingKind::Drift,
            target_kind: crate::model::TargetKind::Edge,
            target: "doc-req:DOC-A:FR-001".to_string(),
            reason: ReasonCode::DocChanged,
            detail: "旧い説明文".to_string(),
        };
        let mut current_same = base_finding.clone();
        current_same.detail = "新しい説明文".to_string(); // 説明文の違いは同一性に影響しない

        let new_finding = Finding {
            kind: FindingKind::New,
            target_kind: crate::model::TargetKind::Edge,
            target: "doc-req:DOC-B:FR-002".to_string(),
            reason: ReasonCode::NewEdge,
            detail: String::new(),
        };

        let current = vec![current_same, new_finding.clone()];
        let base = vec![base_finding];
        let diff = diff_findings(&current, &base);
        assert_eq!(diff, vec![new_finding]);
    }

    fn desc_node(kind: NodeKind, id: &str, description: Option<&str>, hash: &str) -> Node {
        Node {
            description: description.map(str::to_string),
            ..node(kind, id, hash)
        }
    }

    fn add_desc_edge(graph: &mut Graph, kind: NodeKind, id: &str) {
        let key = NodeKey {
            kind,
            id: id.to_string(),
        };
        let edge_id = crate::model::desc_edge_id(&key);
        graph.edges.insert(
            edge_id.clone(),
            Edge {
                id: edge_id,
                kind: EdgeKind::Desc,
                from: key.clone(),
                to: key,
            },
        );
    }

    /// A single Doc node `DOC-A` with a `desc:doc:DOC-A` edge.
    fn desc_graph(description: Option<&str>, body_hash: &str) -> Graph {
        let mut graph = Graph::default();
        let n = desc_node(NodeKind::Doc, "DOC-A", description, body_hash);
        graph.nodes.insert(n.key.clone(), n);
        add_desc_edge(&mut graph, NodeKind::Doc, "DOC-A");
        graph
    }

    fn desc_lock(description: &str, body_hash: &str) -> Lock {
        let mut lock = Lock::default();
        lock.edges.insert(
            "desc:doc:DOC-A".to_string(),
            lock_entry(&crate::hash::content_hash(description), body_hash),
        );
        lock
    }

    fn desc_drifts(findings: &[Finding]) -> Vec<&Finding> {
        findings
            .iter()
            .filter(|f| f.kind == FindingKind::Drift && f.target == "desc:doc:DOC-A")
            .collect()
    }

    // [FR-024]
    #[test]
    fn descは要約だけ変わるとdescription_changedのdriftになる() {
        let graph = desc_graph(Some("新しい要約"), "sha256:body");
        let findings = check(&graph, &desc_lock("古い要約", "sha256:body"));
        let drifts = desc_drifts(&findings);
        assert_eq!(drifts.len(), 1, "{findings:?}");
        assert_eq!(drifts[0].reason, ReasonCode::DescriptionChanged);
    }

    // [FR-024]
    #[test]
    fn descは本文だけ変わるとbody_changedのdriftになる() {
        let graph = desc_graph(Some("要約"), "sha256:body-new");
        let findings = check(&graph, &desc_lock("要約", "sha256:body-old"));
        let drifts = desc_drifts(&findings);
        assert_eq!(drifts.len(), 1, "{findings:?}");
        assert_eq!(drifts[0].reason, ReasonCode::BodyChanged);
    }

    // [FR-088]
    #[test]
    fn descは要約と本文の両方が変わるとboth_changedのdriftが1件になる() {
        let graph = desc_graph(Some("新しい要約"), "sha256:body-new");
        let findings = check(&graph, &desc_lock("古い要約", "sha256:body-old"));
        let drifts = desc_drifts(&findings);
        assert_eq!(drifts.len(), 1, "{findings:?}");
        assert_eq!(drifts[0].reason, ReasonCode::BothChanged);
    }

    // [FR-088]
    #[test]
    fn descは変化がなければdriftを報告しない() {
        let graph = desc_graph(Some("要約"), "sha256:body");
        let findings = check(&graph, &desc_lock("要約", "sha256:body"));
        assert!(
            findings.iter().all(|f| f.target != "desc:doc:DOC-A"),
            "{findings:?}"
        );
    }

    // [FR-088]
    #[test]
    fn ロックにないdescエッジはnewとして報告する() {
        let graph = desc_graph(Some("要約"), "sha256:body");
        let findings = check(&graph, &Lock::default());
        assert!(findings.iter().any(|f| f.kind == FindingKind::New
            && f.target == "desc:doc:DOC-A"
            && f.reason == ReasonCode::NewEdge));
    }

    // [FR-088]
    #[test]
    fn ソースから消えたdescエッジはremovedとして報告する() {
        let findings = check(&Graph::default(), &desc_lock("要約", "sha256:body"));
        assert!(findings.iter().any(|f| f.kind == FindingKind::Removed
            && f.target == "desc:doc:DOC-A"
            && f.reason == ReasonCode::RemovedEdge));
    }

    // [FR-088]
    #[test]
    fn 要約を消すとロックの要約が非空ならdescription_changedになる() {
        let graph = desc_graph(None, "sha256:body");
        let findings = check(&graph, &desc_lock("要約", "sha256:body"));
        let drifts = desc_drifts(&findings);
        assert_eq!(drifts.len(), 1, "{findings:?}");
        assert_eq!(drifts[0].reason, ReasonCode::DescriptionChanged);
    }

    // [FR-088]
    #[test]
    fn ロックの要約が空で要約も無ければdriftを報告しない() {
        let graph = desc_graph(None, "sha256:body");
        let findings = check(&graph, &desc_lock("", "sha256:body"));
        assert!(desc_drifts(&findings).is_empty(), "{findings:?}");
    }

    fn lock_of(graph: &Graph) -> Lock {
        let mut lock = Lock::default();
        for (id, edge) in &graph.edges {
            let (from, to) = crate::model::edge_hashes(graph, edge);
            lock.edges.insert(id.clone(), lock_entry(&from, &to));
        }
        lock
    }

    /// DOC-A links FR-001 and FR-002; FR-001 is also tested; desc edges for
    /// DOC-A and FR-001 (the req is under an index root).
    fn full_graph(doc_hash: &str, req_hash: &str) -> Graph {
        let mut graph = Graph::default();
        for n in [
            desc_node(NodeKind::Doc, "DOC-A", Some("doc 要約"), doc_hash),
            desc_node(NodeKind::Req, "FR-001", Some("req 要約"), req_hash),
            desc_node(NodeKind::Req, "FR-002", None, "sha256:r2"),
            node(NodeKind::Test, "src/foo::bar", "sha256:t"),
        ] {
            graph.nodes.insert(n.key.clone(), n);
        }
        for (kind, from_kind, from, to) in [
            (EdgeKind::DocReq, NodeKind::Doc, "DOC-A", "FR-001"),
            (EdgeKind::DocReq, NodeKind::Doc, "DOC-A", "FR-002"),
            (EdgeKind::TestReq, NodeKind::Test, "src/foo::bar", "FR-001"),
        ] {
            let id = crate::model::edge_id(kind, from, to);
            graph.edges.insert(
                id.clone(),
                Edge {
                    id,
                    kind,
                    from: NodeKey {
                        kind: from_kind,
                        id: from.to_string(),
                    },
                    to: NodeKey {
                        kind: NodeKind::Req,
                        id: to.to_string(),
                    },
                },
            );
        }
        add_desc_edge(&mut graph, NodeKind::Doc, "DOC-A");
        add_desc_edge(&mut graph, NodeKind::Req, "FR-001");
        graph
    }

    fn drift_reasons(findings: &[Finding]) -> Vec<(String, ReasonCode)> {
        findings
            .iter()
            .filter(|f| f.kind == FindingKind::Drift)
            .map(|f| (f.target.clone(), f.reason))
            .collect()
    }

    // [FR-088]
    #[test]
    fn doc本文を編集するとdoc_reqは本数分のdoc_changedとdescのbody_changedが別々に出る() {
        let lock = lock_of(&full_graph("sha256:d-old", "sha256:r1"));
        let findings = check(&full_graph("sha256:d-new", "sha256:r1"), &lock);
        assert_eq!(
            drift_reasons(&findings),
            vec![
                ("desc:doc:DOC-A".to_string(), ReasonCode::BodyChanged),
                ("doc-req:DOC-A:FR-001".to_string(), ReasonCode::DocChanged),
                ("doc-req:DOC-A:FR-002".to_string(), ReasonCode::DocChanged),
            ]
        );
    }

    // [FR-088]
    #[test]
    fn 索引配下のreq本文を編集するとreq_changedとdescのbody_changedが出る() {
        let lock = lock_of(&full_graph("sha256:d", "sha256:r1-old"));
        let findings = check(&full_graph("sha256:d", "sha256:r1-new"), &lock);
        assert_eq!(
            drift_reasons(&findings),
            vec![
                ("desc:req:FR-001".to_string(), ReasonCode::BodyChanged),
                ("doc-req:DOC-A:FR-001".to_string(), ReasonCode::ReqChanged),
                (
                    "test-req:src/foo::bar:FR-001".to_string(),
                    ReasonCode::ReqChanged
                ),
            ]
        );
    }

    // [FR-087]
    #[test]
    fn 要約だけを変えてもdoc_reqとtest_reqにfindingは出ない() {
        let old = full_graph("sha256:d", "sha256:r1");
        let lock = lock_of(&old);
        let mut new = old.clone();
        for n in new.nodes.values_mut() {
            if n.description.is_some() {
                n.description = Some("変更後の要約".to_string());
            }
        }
        let findings = check(&new, &lock);
        assert_eq!(
            drift_reasons(&findings),
            vec![
                ("desc:doc:DOC-A".to_string(), ReasonCode::DescriptionChanged),
                (
                    "desc:req:FR-001".to_string(),
                    ReasonCode::DescriptionChanged
                ),
            ]
        );
    }

    // [FR-086]
    #[test]
    fn descしか持たないreqはorphanとno_testのままになる() {
        let mut graph = Graph::default();
        let n = desc_node(NodeKind::Req, "FR-001", Some("要約"), "sha256:r");
        graph.nodes.insert(n.key.clone(), n);
        add_desc_edge(&mut graph, NodeKind::Req, "FR-001");
        let findings = check(&graph, &lock_of(&graph));
        assert!(findings.iter().any(|f| f.kind == FindingKind::Orphan
            && f.reason == ReasonCode::ReqWithoutDoc
            && f.target == "FR-001"));
        assert!(findings.iter().any(|f| f.kind == FindingKind::NoTest
            && f.reason == ReasonCode::ReqWithoutTest
            && f.target == "FR-001"));
    }

    // [FR-086]
    #[test]
    fn descしか持たないdocはorphanのままになる() {
        let graph = desc_graph(Some("要約"), "sha256:body");
        let findings = check(&graph, &lock_of(&graph));
        assert!(findings.iter().any(|f| f.kind == FindingKind::Orphan
            && f.reason == ReasonCode::DocWithoutReq
            && f.target == "DOC-A"));
    }
}