alix 0.8.0

A learning tool built for understanding, not just remembering.
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
//! Read-only duplicate identity-token detection across a folder of decks: it
//! never writes; resolution (re-minting the loser's token) happens later at
//! session-open.

use std::{
    collections::{HashMap, HashSet},
    hash::Hasher,
    path::{Path, PathBuf},
};

use twox_hash::XxHash64;

use crate::parser;

/// An authored `id:` value claimed by more than one block; the keeper keeps
/// its progress, each loser is re-minted at its deck's next review-open.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CardDupe {
    /// The `id:` value the deck files hold, which is the only spelling ever
    /// written back: a composed review-unit id never reaches a deck file.
    pub base: String,
    /// The keeper: (deck file, 1-based line of the authored block).
    pub keeper: (PathBuf, usize),
    /// The losing blocks: (deck file, 1-based line of the authored block).
    pub losers: Vec<(PathBuf, usize)>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DuplicateMap {
    /// (kept deck, excluded deck, shared token) per excluded copy. Never
    /// auto-fixed: the tool can't know which copy is pristine, so removing the
    /// copy's `id:` line is manual.
    pub excluded_decks: Vec<(PathBuf, PathBuf, String)>,
    /// Excludes cards from an already-excluded deck: a whole-file copy is one
    /// deck-level finding, not one per card.
    pub card_dupes: Vec<CardDupe>,
    /// What each scanned deck hashed to while these findings were derived, so
    /// a repair this scan authorized refuses a file that moved under it.
    pub digests: HashMap<PathBuf, u64>,
}

pub fn digest(text: &str) -> u64 {
    let mut hasher = XxHash64::default();
    hasher.write(text.as_bytes());
    hasher.finish()
}

pub fn scan_dir(dir: &Path) -> DuplicateMap {
    scan(&crate::workspace::deck_files(dir))
}

/// scan_dir on a token-extracting line scan instead of full parses: the
/// review-open hot path. It diverges from the parser in BOTH directions: a
/// file the parser refuses outright still yields its tokens here, so a caller
/// that writes must confirm a finding against `scan_dir` before it does.
pub fn scan_dir_fast(dir: &Path) -> DuplicateMap {
    build_map(&parse_fast(dir))
}

/// True when the line scan sees one card token claimed twice, counted BEFORE
/// deck-level exclusion so an over-claimed deck token cannot hide one.
pub fn any_repeated_card_token_fast(dir: &Path) -> bool {
    let mut seen: HashSet<&str> = HashSet::new();
    let parsed = parse_fast(dir);
    parsed
        .iter()
        .flat_map(|p| p.cards.iter())
        .any(|card| !seen.insert(card.id.as_str()))
}

fn parse_fast(dir: &Path) -> Vec<Parsed> {
    let member_dir = crate::workspace::member_dir(dir);
    let mut paths: Vec<PathBuf> = std::fs::read_dir(member_dir)
        .map(|entries| {
            entries
                .filter_map(|r| r.ok().map(|e| e.path()))
                .filter(|p| p.is_file() && p.extension().is_some_and(|e| e == "md"))
                .filter(|p| {
                    !p.file_name().and_then(|n| n.to_str()).is_some_and(|n| {
                        crate::workspace::is_conventional_non_deck(n)
                            || crate::workspace::is_conflict_name(n)
                    })
                })
                .collect()
        })
        .unwrap_or_default();
    paths.sort();
    let mut parsed = Vec::new();
    for path in paths {
        let Ok(text) = std::fs::read_to_string(&path) else {
            continue;
        };
        let (deck_token, cards) = extract_ids(&text);
        parsed.push(Parsed {
            path,
            deck_token,
            digest: digest(&text),
            cards,
        });
    }
    parsed
}

/// (deck token, one claim per authored block) via a fence-aware line scan
/// mirroring the parser's directive placement rules. A line scan sees only
/// authored spellings, so each claim's composed id is its base.
fn extract_ids(text: &str) -> (Option<String>, Vec<ScannedCard>) {
    let mut deck_token = None;
    let mut cards = Vec::new();
    let mut fence: Option<char> = None;
    let mut in_frontmatter = false;
    let mut heading_line = 0usize;
    let mut prev_pipe = false;
    for (i, raw) in text.lines().enumerate() {
        let n = i + 1;
        let line = raw.strip_suffix('\r').unwrap_or(raw);
        if i == 0 && line.trim_end() == "---" {
            in_frontmatter = true;
            continue;
        }
        if in_frontmatter {
            if line.trim_end().trim_end_matches([' ', '\t']) == "---" {
                in_frontmatter = false;
            } else if deck_token.is_none()
                && let Some(rest) = line.trim().strip_prefix("id:")
            {
                let v = rest.trim().trim_matches('"');
                if matches!(
                    crate::token::parse_id(v),
                    Some((crate::token::Kind::Deck, ..))
                ) {
                    deck_token = Some(v.to_string());
                }
            }
            continue;
        }
        let marker = line.chars().next();
        if (line.starts_with("```") || line.starts_with("~~~")) && fence.is_none() {
            fence = marker;
            continue;
        }
        if let Some(f) = fence {
            if line.starts_with(f) && line.trim_end().chars().all(|c| c == f) {
                fence = None;
            }
            continue;
        }
        if let Some((depth, _)) = crate::parser::heading_depth(line) {
            if crate::parser::is_card_depth(depth) {
                heading_line = n;
            } else {
                // A section closes the card chain: an id below it belongs
                // to no card, so no heading may stay open to claim it.
                heading_line = 0;
            }
        }
        // A table opens a block too: its first pipe line anchors the
        // container id comment that follows the table.
        if line.starts_with('|') && !prev_pipe {
            heading_line = n;
        }
        prev_pipe = line.starts_with('|');
        let candidate = if line.trim().starts_with("<!--") {
            line.trim()
        } else {
            continue;
        };
        if let Some(inner) = candidate
            .strip_prefix("<!--")
            .and_then(|s| s.strip_suffix("-->"))
            && let Some(rest) = inner.trim().strip_prefix("id:")
        {
            let v = rest.trim();
            if matches!(
                crate::token::parse_prefixed_card_id(v),
                Some((_, None, false, None))
            ) && heading_line > 0
            {
                let entry = ScannedCard {
                    id: v.to_string(),
                    base: v.to_string(),
                    block_line: heading_line,
                };
                if !cards.contains(&entry) {
                    cards.push(entry);
                }
            }
        }
    }
    (deck_token, cards)
}

/// One review unit's identity claim: the composed id that has to be unique,
/// the authored value the deck file holds, and the block that value closes.
#[derive(PartialEq, Eq)]
struct ScannedCard {
    id: String,
    base: String,
    block_line: usize,
}

struct Parsed {
    path: PathBuf,
    deck_token: Option<String>,
    digest: u64,
    /// One entry per composed id, so a card's regions and a table's rows
    /// each contribute their own claim over one shared block line.
    cards: Vec<ScannedCard>,
}

/// Skips unreadable/unparseable decks silently; `doctor` reports those
/// separately.
pub fn scan(deck_paths: &[PathBuf]) -> DuplicateMap {
    let mut parsed: Vec<Parsed> = Vec::new();
    for path in deck_paths {
        let Ok(text) = std::fs::read_to_string(path) else {
            continue;
        };
        let subject = path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("deck.md");
        let Ok(deck) = parser::parse(subject, &text) else {
            continue;
        };
        let mut cards: Vec<ScannedCard> = Vec::new();
        for card in &deck.cards {
            // The composed id decides uniqueness, since sibling table rows
            // share their container token by design and are never duplicates.
            // The base decides what a repair writes.
            if let (Some(id), Some(base)) = (card.id(), card.token.as_deref()) {
                let entry = ScannedCard {
                    id,
                    base: base.to_string(),
                    block_line: card.block_line,
                };
                if !cards.contains(&entry) {
                    cards.push(entry);
                }
            }
        }
        parsed.push(Parsed {
            path: path.clone(),
            deck_token: deck.deck_token.clone(),
            digest: digest(&text),
            cards,
        });
    }

    build_map(&parsed)
}

fn build_map(parsed: &[Parsed]) -> DuplicateMap {
    let (excluded_decks, excluded) = deck_dupes(parsed);
    let card_dupes = card_dupes(parsed, &excluded);
    DuplicateMap {
        excluded_decks,
        card_dupes,
        digests: parsed.iter().map(|p| (p.path.clone(), p.digest)).collect(),
    }
}

/// The deck-token duplicates, plus the indices of the losing (excluded) decks.
fn deck_dupes(parsed: &[Parsed]) -> (Vec<(PathBuf, PathBuf, String)>, Vec<usize>) {
    let mut groups: HashMap<&str, Vec<usize>> = HashMap::new();
    for (i, p) in parsed.iter().enumerate() {
        if let Some(tok) = p.deck_token.as_deref() {
            groups.entry(tok).or_default().push(i);
        }
    }
    let mut excluded_decks = Vec::new();
    let mut excluded = Vec::new();
    for (tok, idxs) in groups {
        if idxs.len() < 2 {
            continue;
        }
        let keeper = idxs[keeper_index(
            &idxs
                .iter()
                .map(|&i| parsed[i].path.as_path())
                .collect::<Vec<_>>(),
        )];
        for &i in &idxs {
            if i != keeper {
                excluded.push(i);
                excluded_decks.push((
                    parsed[keeper].path.clone(),
                    parsed[i].path.clone(),
                    tok.to_string(),
                ));
            }
        }
    }
    // HashMap iteration is unordered: sort for a deterministic report.
    excluded_decks.sort();
    (excluded_decks, excluded)
}

/// Authored-identity duplicates across the non-excluded decks. One authored
/// block is ONE claim however many review units it expands to, so a block's
/// sibling regions, table rows, and reversed half co-own their base
/// legally while two different blocks never do. Grouping on the composed id
/// instead would both miss a pair whose shapes differ (a plain card and a
/// cloze copy of it) and let one block be elected keeper of one unit and
/// loser of another, which no single `id:` line can satisfy.
fn card_dupes(parsed: &[Parsed], excluded: &[usize]) -> Vec<CardDupe> {
    // base -> the authored blocks claiming it, in scan order (deck order,
    // then line).
    let mut sites: HashMap<&str, Vec<(PathBuf, usize)>> = HashMap::new();
    for (i, p) in parsed.iter().enumerate() {
        if excluded.contains(&i) {
            continue;
        }
        for card in &p.cards {
            let claim = (p.path.clone(), card.block_line);
            let claims = sites.entry(card.base.as_str()).or_default();
            if !claims.contains(&claim) {
                claims.push(claim);
            }
        }
    }
    let mut out = Vec::new();
    for (base, sites) in sites {
        if sites.len() < 2 {
            continue;
        }
        let keeper = keeper_index(&sites.iter().map(|(p, _)| p.as_path()).collect::<Vec<_>>());
        let losers = sites
            .iter()
            .enumerate()
            .filter(|(i, _)| *i != keeper)
            .map(|(_, site)| site.clone())
            .collect();
        out.push(CardDupe {
            base: base.to_string(),
            keeper: sites[keeper].clone(),
            losers,
        });
    }
    // Deterministic report order.
    out.sort_by(|a, b| a.base.cmp(&b.base));
    out
}

/// The index of the keeper, by [`beats`]: an undecorated stem beats a
/// decorated copy, else the earliest in scan order.
fn keeper_index(paths: &[&Path]) -> usize {
    let mut best = 0;
    for i in 1..paths.len() {
        if beats(paths[i], paths[best]) {
            best = i;
        }
    }
    best
}

/// True only if `current`'s stem is `challenger`'s stem plus a suffix starting
/// non-alphanumeric; an alphanumeric continuation (`deck1` vs `deck10`) is
/// unrelated, not a decoration.
fn beats(challenger: &Path, current: &Path) -> bool {
    let c = stem(challenger);
    let cur = stem(current);
    // `starts_with` guarantees `c.len()` is a char boundary of `cur`, and
    // `c != cur` guarantees a next character exists.
    c != cur
        && cur.starts_with(c.as_str())
        && cur[c.len()..]
            .chars()
            .next()
            .is_some_and(|ch| !ch.is_alphanumeric())
}

/// The file name without its `.md` extension: the unit `beats` compares.
fn stem(path: &Path) -> String {
    path.file_name()
        .and_then(|n| n.to_str())
        .map(|n| n.strip_suffix(".md").unwrap_or(n).to_string())
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write(dir: &Path, name: &str, token: &str, card_token: &str) -> PathBuf {
        let path = dir.join(name);
        std::fs::write(
            &path,
            format!(
                "---\nformat-version: 1\nid: \"{token}\"\n---\n## q\na\n<!-- id: {card_token} -->\n"
            ),
        )
        .unwrap();
        path
    }

    /// Existing ids must be found at every card depth: a scan that only
    /// knows `## ` would miss a sub-card's id and mint a second one for the
    /// same proposition.
    #[test]
    fn existing_ids_are_found_at_every_card_depth() {
        let text = "## a\n1\n<!-- id: card-a1 -->\n### b\n2\n<!-- id: card-b1 -->\n#### c\n3\n<!-- id: card-c1 -->\n##### d\n4\n<!-- id: card-d1 -->\n###### e\n5\n<!-- id: card-e1 -->\n";
        let (_, ids) = extract_ids(text);
        let found: Vec<&str> = ids.iter().map(|card| card.id.as_str()).collect();
        for want in ["card-a1", "card-b1", "card-c1", "card-d1", "card-e1"] {
            assert!(found.contains(&want), "{want} missing from {found:?}");
        }
    }

    #[test]
    fn the_fast_scan_matches_the_full_scan_for_a_copied_depth_six_card() {
        let dir = tempfile::tempdir().unwrap();
        let chain = |deck: &str, leaf: &str| {
            format!(
                "---\nformat-version: 1\nid: \"{deck}\"\n---\n\
                 ## a\n1\n### b\n2\n#### c\n3\n##### d\n4\n\
                 ###### e\n{leaf}\n<!-- id: card-4jkya9q3m8z0tw5v9y2b4n6d8f -->\n"
            )
        };
        std::fs::write(
            dir.path().join("a.md"),
            chain("deck-9w2c7x4k1m8q3z5t0v6b2n4d8f", "first"),
        )
        .unwrap();
        std::fs::write(
            dir.path().join("b.md"),
            chain("deck-6v3c7x4k1m8q3z5t0b2n4d8f9w", "copy"),
        )
        .unwrap();

        let full = scan_dir(dir.path());
        let fast = scan_dir_fast(dir.path());

        assert_eq!(1, full.card_dupes.len(), "the parser sees the copied card");
        assert_eq!(
            (&full.excluded_decks, &full.card_dupes),
            (&fast.excluded_decks, &fast.card_dupes),
            "review-open must not miss a copied card merely because its legal front is depth six"
        );
    }

    #[test]
    fn the_fast_scan_matches_the_full_scan_across_placements_and_fences() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("a.md"),
            "---\nformat-version: 1\nid: \"deck-dtok1\"\n\n---\n# T\n\n## q1\nanswer\n<!-- id: card-shared1 -->\n\n## q2\nanswer\n<!-- id: card-trailingtok -->\n",
        )
        .unwrap();
        std::fs::write(
            dir.path().join("b.md"),
            "---\nformat-version: 1\nid: \"deck-dtok2\"\n---\n## q3\nbelow front\n<!-- id: card-shared1 -->\n\n## q4\n```\n## fenced <!-- id: card-fencedtok -->\n<!-- id: card-alsofenced -->\n```\n<!-- id: card-realtok -->\n",
        )
        .unwrap();
        std::fs::write(dir.path().join("notes.md"), "just prose, no cards\n").unwrap();
        std::fs::write(
            dir.path().join("c.md.bak"),
            "## x\n<!-- id: card-shared1 -->\n",
        )
        .unwrap();

        let full = scan_dir(dir.path());
        let fast = scan_dir_fast(dir.path());
        assert_eq!(
            (&full.excluded_decks, &full.card_dupes),
            (&fast.excluded_decks, &fast.card_dupes),
            "the two scans must agree on findings; a card-less file digested by only one is not one"
        );
        assert_eq!(1, fast.card_dupes.len());
        assert_eq!("card-shared1", fast.card_dupes[0].base);
    }

    /// The fast scan's known over-claim, pinned so nothing writes on its word.
    #[test]
    fn the_fast_scan_claims_tokens_from_a_deck_the_parser_refuses() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("a.md"),
            "---\nformat-version: 1\nid: \"deck-dtok1\"\n---\n## q1\nanswer\n<!-- id: card-shared1 -->\n",
        )
        .unwrap();
        // The closing fence carries a no-break space, so the parser never
        // closes the frontmatter and refuses the whole file.
        std::fs::write(
            dir.path().join("b.md"),
            "---\nformat-version: 1\nid: \"deck-dtok2\"\n---\u{a0}\n## q2\nanswer\n<!-- id: card-shared1 -->\n",
        )
        .unwrap();

        let full = scan_dir(dir.path());
        let fast = scan_dir_fast(dir.path());

        assert!(
            full.card_dupes.is_empty(),
            "the parser refuses b.md, so it claims no token: {:?}",
            full.card_dupes
        );
        assert_eq!(
            1,
            fast.card_dupes.len(),
            "the line scan cannot know the file was refused, so it over-claims; \
             `resolve_duplicates_at_open` is what must not act on this"
        );
    }

    fn table_deck(deck_id: &str, container: &str) -> String {
        format!(
            "---\nformat-version: 1\nid: \"{deck_id}\"\n---\n| word | meaning |\n|---|---|\n| one | eins | <!-- r:4k2x9w -->\n| two | zwei | <!-- r:7m3p5q -->\n<!-- cards -->\n<!-- id: {container} -->\n"
        )
    }

    #[test]
    fn sibling_rows_in_one_table_are_not_duplicate_card_tokens() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("table.md"),
            table_deck(
                "deck-9w2c7x4k1m8q3z5t0v6b2n4d8f",
                "card-4jkya9q3m8z0tw5v9y2b4n6d8f",
            ),
        )
        .unwrap();

        let map = scan_dir(dir.path());

        assert!(map.card_dupes.is_empty(), "{:#?}", map.card_dupes);
        assert_eq!(
            map,
            scan_dir_fast(dir.path()),
            "the deck must parse, or emptiness here is vacuous"
        );
    }

    #[test]
    fn the_fast_scan_detects_a_copied_table_container() {
        let dir = tempfile::tempdir().unwrap();
        let container = "card-4jkya9q3m8z0tw5v9y2b4n6d8f";
        std::fs::write(
            dir.path().join("a.md"),
            table_deck("deck-9w2c7x4k1m8q3z5t0v6b2n4d8f", container),
        )
        .unwrap();
        std::fs::write(
            dir.path().join("b.md"),
            table_deck("deck-6v3c7x4k1m8q3z5t0b2n4d8f9w", container),
        )
        .unwrap();

        let map = scan_dir_fast(dir.path());

        assert_eq!(1, map.card_dupes.len(), "{map:#?}");
        assert_eq!(container, map.card_dupes[0].base);
        assert_eq!(
            5, map.card_dupes[0].keeper.1,
            "the container anchors to the table's HEADER line, not a later row"
        );
        assert_eq!(vec![5], {
            let mut lines: Vec<usize> = map.card_dupes[0].losers.iter().map(|l| l.1).collect();
            lines.sort_unstable();
            lines
        });
    }

    #[test]
    fn a_duplicate_deck_token_excludes_the_decorated_copy() {
        let dir = tempfile::tempdir().unwrap();
        // Plain lexicographic order would keep `deck (1).md` (space sorts before `.`);
        // the keep-rule must prevent that exact inversion.
        let base = write(dir.path(), "deck.md", "deck-dsame", "card-cbase");
        let copy1 = write(dir.path(), "deck (1).md", "deck-dsame", "card-ccopy1");
        let copy2 = write(dir.path(), "deck copy.md", "deck-dsame", "card-ccopy2");

        let map = scan(&[copy1.clone(), copy2.clone(), base.clone()]);

        assert_eq!(
            vec![
                (base.clone(), copy1, "deck-dsame".to_string()),
                (base, copy2, "deck-dsame".to_string()),
            ],
            map.excluded_decks
        );
        assert!(map.card_dupes.is_empty());
    }

    #[test]
    fn unrelated_duplicate_deck_names_fall_back_to_scan_order() {
        let dir = tempfile::tempdir().unwrap();
        // Non-alphabetical scan order pins that `zebra.md` wins by being scanned
        // first, not by sorting first.
        let zebra = write(dir.path(), "zebra.md", "deck-dsame", "card-czebra");
        let apple = write(dir.path(), "apple.md", "deck-dsame", "card-capple");

        let map = scan(&[zebra.clone(), apple.clone()]);
        assert_eq!(
            vec![(zebra, apple, "deck-dsame".to_string())],
            map.excluded_decks
        );
    }

    #[test]
    fn the_duplicate_map_is_returned_by_the_lib_scan() {
        let dir = tempfile::tempdir().unwrap();
        let a = dir.path().join("notes.md");
        std::fs::write(
            &a,
            "---\nformat-version: 1\nid: \"deck-dtoka\"\n---\n## q\na\n<!-- id: card-cshared -->\n",
        )
        .unwrap();
        let b = dir.path().join("notes copy.md");
        std::fs::write(
            &b,
            "---\nformat-version: 1\nid: \"deck-dtokb\"\n---\n## q\nb\n<!-- id: card-cshared -->\n",
        )
        .unwrap();

        let map = scan(&[a.clone(), b.clone()]);
        assert!(
            map.excluded_decks.is_empty(),
            "decks differ, not deck-dupes"
        );
        assert_eq!(1, map.card_dupes.len());
        let dupe = &map.card_dupes[0];
        assert_eq!("card-cshared", dupe.base);
        assert_eq!((a, 5), dupe.keeper);
        assert_eq!(vec![(b, 5)], dupe.losers);
    }

    #[test]
    fn scan_dir_enumerates_and_skips_unparseable_decks() {
        let dir = tempfile::tempdir().unwrap();
        write(dir.path(), "one.md", "deck-d1", "card-c1");
        std::fs::write(dir.path().join("broken.md"), "## q with no answer\n").unwrap();
        let map = scan_dir(dir.path());
        assert!(map.excluded_decks.is_empty());
        assert!(map.card_dupes.is_empty());
    }

    #[test]
    fn two_decorated_copies_without_an_original_fall_back_to_scan_order() {
        let dir = tempfile::tempdir().unwrap();
        // Neither copy's stem is a prefix of the other's, so the keep-rule has
        // nothing to prefer; scan order decides.
        let paren = write(dir.path(), "deck (1).md", "deck-dsame", "card-cparen");
        let copy = write(dir.path(), "deck copy.md", "deck-dsame", "card-ccopy");

        let map = scan(&[paren.clone(), copy.clone()]);
        assert_eq!(
            vec![(paren, copy, "deck-dsame".to_string())],
            map.excluded_decks
        );
    }

    #[test]
    fn case_differing_stems_are_unrelated_decks() {
        let dir = tempfile::tempdir().unwrap();
        // The keep-rule's prefix check is case-sensitive, so differing-case stems
        // count as unrelated names.
        let upper = write(dir.path(), "Deck.md", "deck-dsame", "card-cupper");
        let lower = write(dir.path(), "deck.md", "deck-dsame", "card-clower");

        let map = scan(&[upper.clone(), lower.clone()]);
        assert_eq!(
            vec![(upper, lower, "deck-dsame".to_string())],
            map.excluded_decks
        );
    }

    #[test]
    fn an_alphanumeric_continuation_is_not_a_decoration() {
        let dir = tempfile::tempdir().unwrap();
        let ten = write(dir.path(), "deck10.md", "deck-dsame", "card-cten");
        let one = write(dir.path(), "deck1.md", "deck-dsame", "card-cone");

        let map = scan(&[ten.clone(), one.clone()]);
        assert_eq!(
            vec![(ten, one, "deck-dsame".to_string())],
            map.excluded_decks
        );
    }

    #[test]
    fn fast_scan_ignores_non_decks_by_extension_and_reserved_name() {
        let dir = tempfile::tempdir().unwrap();
        let shared = "card-9w2c7x4k1m8q3z5t0v6b2n4d8f";
        let extension_only = "card-4jkya9q3m8z0tw5v9y2b4n6d8f";
        let conventional = "card-6v3c7x4k1m8q3z5t0b2n4d8f9w";
        let conflict = "card-8v3c7x4k1m9q2z5t0b6n4d8f9w";
        write(dir.path(), "a.md", "deck-a", shared);
        write(dir.path(), "b.md", "deck-b", shared);
        write(dir.path(), "notes.txt", "deck-c", extension_only);
        write(dir.path(), "extension-peer.md", "deck-d", extension_only);
        write(dir.path(), "README.md", "deck-e", conventional);
        write(dir.path(), "readme-peer.md", "deck-f", conventional);
        write(
            dir.path(),
            "copy.sync-conflict-device.md",
            "deck-g",
            conflict,
        );
        write(dir.path(), "copy-peer.md", "deck-h", conflict);

        let map = scan_dir_fast(dir.path());
        let tokens: Vec<&str> = map
            .card_dupes
            .iter()
            .map(|dupe| dupe.base.as_str())
            .collect();

        assert_eq!(vec![shared], tokens);
    }

    #[test]
    fn fast_id_extraction_obeys_frontmatter_fence_heading_and_token_boundaries() {
        let deck = "deck-9w2c7x4k1m8q3z5t0v6b2n4d8f";
        let before_heading = "card-4jkya9q3m8z0tw5v9y2b4n6d8f";
        let fenced = "card-6v3c7x4k1m8q3z5t0b2n4d8f9w";
        let prose = "card-8v3c7x4k1m9q2z5t0b6n4d8f9w";
        let accepted = "card-7v3c7x4k1m9q2z5t0b6n4d8f9w";
        let text = format!(
            "---\nformat-version: 1\nid: \"{deck}\"\n---\n\
             <!-- id: {before_heading} -->\n\
             ## real\n\
             ```\n\
             ```not a closing fence\n\
             <!-- id: {fenced} -->\n\
             ```\n\
             prose <!-- id: {prose} -->\n\
             <!-- id: card-invalid! -->\n\
             <!-- id: {accepted} -->\n"
        );

        let (found_deck, cards) = extract_ids(&text);
        assert_eq!(Some(deck.to_string()), found_deck);
        assert_eq!(
            vec![(accepted.to_string(), 6)],
            cards
                .iter()
                .map(|card| (card.id.clone(), card.block_line))
                .collect::<Vec<_>>()
        );
    }

    #[test]
    fn the_raw_gate_covers_every_composed_identity_shape() {
        let shared = "card-4jkya9q3m8z0tw5v9y2b4n6d8f";
        let cases = [
            ("plain", format!("## q\na\n<!-- id: {shared} -->\n")),
            (
                "span blanks",
                format!(
                    "## q\nalpha and beta\n<!-- blank: span hidden=\"alpha\" b:a1b2c3 -->\n<!-- blank: span hidden=\"beta\" b:d4e5f6 -->\n<!-- id: {shared} -->\n"
                ),
            ),
            (
                "reversed half",
                format!("## q\na\n<!-- direction: both -->\n<!-- id: {shared} -->\n"),
            ),
            (
                "table row stamp",
                format!(
                    "| front | back |\n|---|---|\n| q | a | <!-- r:4k2x9w -->\n<!-- cards -->\n<!-- id: {shared} -->\n"
                ),
            ),
        ];
        for (shape, body) in cases {
            let dir = tempfile::tempdir().unwrap();
            for (name, deck_id) in [
                ("a.md", "deck-9w2c7x4k1m8q3z5t0v6b2n4d8f"),
                ("b.md", "deck-6v3c7x4k1m8q3z5t0b2n4d8f9w"),
            ] {
                std::fs::write(
                    dir.path().join(name),
                    format!("---\nformat-version: 1\nid: \"{deck_id}\"\n---\n{body}"),
                )
                .unwrap();
            }
            assert!(
                !scan_dir(dir.path()).card_dupes.is_empty(),
                "the {shape} fixture must contain a real full-parser duplicate"
            );
            assert!(
                any_repeated_card_token_fast(dir.path()),
                "the raw gate missed the {shape} duplicate"
            );
        }

        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("same-file.md"),
            format!(
                "---\nformat-version: 1\nid: \"deck-9w2c7x4k1m8q3z5t0v6b2n4d8f\"\n---\n## first\na\n<!-- id: {shared} -->\n## second\nb\n<!-- id: {shared} -->\n"
            ),
        )
        .unwrap();
        assert!(!scan_dir(dir.path()).card_dupes.is_empty());
        assert!(any_repeated_card_token_fast(dir.path()));
    }
}