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
pub mod sample;

use std::{
    collections::HashSet,
    hash::{Hash, Hasher},
};

use crate::card::Card;

// One correct option plus three distractors.
pub const NUM_OPTIONS: usize = 4;

#[cfg(feature = "full")]
pub(crate) const NOTE_POSITION_INSTRUCTION: &str = "Never refer to an option by number, letter, \
    or screen position; options are shuffled, so name the claim or mistaken premise itself.";

pub(crate) fn note_names_position(note: &str) -> bool {
    let mut tokens = note
        .split(|character: char| !(character.is_alphanumeric() || character == '#'))
        .filter(|token| !token.is_empty());

    let option = |token: &str| {
        ["option", "options", "choice", "choices"]
            .iter()
            .any(|candidate| token.eq_ignore_ascii_case(candidate))
    };
    let numbered_position = |token: &str| {
        let token = token.strip_prefix('#').unwrap_or(token);
        token.parse::<usize>().is_ok_and(|value| value > 0)
            || ["st", "nd", "rd", "th"].iter().any(|suffix| {
                token
                    .strip_suffix(suffix)
                    .is_some_and(|number| number.parse::<usize>().is_ok_and(|value| value > 0))
            })
            || ["first", "second", "third", "fourth", "fifth", "last"]
                .iter()
                .any(|candidate| token.eq_ignore_ascii_case(candidate))
    };
    let label = |token: &str| {
        ["a", "b", "c", "d", "e"]
            .iter()
            .any(|candidate| token.eq_ignore_ascii_case(candidate))
    };

    let Some(mut previous) = tokens.next() else {
        return false;
    };
    for current in tokens {
        if (option(previous) && (numbered_position(current) || label(current)))
            || (numbered_position(previous) && option(current))
        {
            return true;
        }
        previous = current;
    }
    false
}

#[derive(Debug)]
pub struct ChoiceQuestion {
    pub options: Vec<String>,
    pub correct: usize,
    pub multiple: bool,
    /// Ascending option indices of every correct answer; `[correct]` on a
    /// single-answer question.
    pub correct_set: Vec<usize>,
}

// The whole answer as one option, so a quotation stays (dropping it would
// offer a truncated answer as the correct pick) but loses its marker.
fn answer_text(card: &Card) -> String {
    crate::render::readable_answer_lines(card, crate::card::AnswerSpace::Authored).join("\n")
}

fn authored_correct_options(card: &Card) -> &[String] {
    &card.back
}

fn content(text: &str, card: &Card) -> String {
    crate::inline::strip_inline_with(text.trim(), &card.definitions)
}

// Stable across polls of one appearance, but a fresh study session receives a
// new session seed so a remembered option position does not carry between runs.
pub fn seed_for(card_id: &str, session_seed: u64, appearance: u32) -> u64 {
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    card_id.hash(&mut hasher);
    session_seed.hash(&mut hasher);
    appearance.hash(&mut hasher);
    hasher.finish()
}

fn distinct_distractors(card: &Card, ai_distractors: &[String]) -> Vec<String> {
    let needed = NUM_OPTIONS - 1;
    // Seed with the answer so no AI distractor can duplicate it.
    let mut seen: HashSet<String> = HashSet::new();
    seen.insert(content(&answer_text(card), card));
    let mut chosen: Vec<String> = Vec::new();
    for option in ai_distractors {
        if chosen.len() == needed {
            break;
        }
        let trimmed = option.trim();
        let content = content(trimmed, card);
        if !content.is_empty() && seen.insert(content) {
            chosen.push(trimmed.to_string());
        }
    }
    chosen
}

pub fn build(card: &Card, seed: u64, ai_distractors: &[String]) -> Option<ChoiceQuestion> {
    let mut options = distinct_distractors(card, ai_distractors);
    if options.len() < NUM_OPTIONS - 1 {
        return None;
    }
    let correct_text = answer_text(card);
    options.push(correct_text.clone());
    let mut rng = Rng::new(seed);
    shuffle(&mut options, &mut rng);
    let correct = options.iter().position(|t| *t == correct_text)?;
    Some(ChoiceQuestion {
        options,
        correct,
        multiple: false,
        correct_set: vec![correct],
    })
}

pub fn build_authored(
    card: &Card,
    seed: u64,
    authored_distractors: &[String],
) -> Option<ChoiceQuestion> {
    let correct_text = authored_correct_options(card).join("\n");
    let mut seen = HashSet::new();
    seen.insert(content(&correct_text, card));
    let mut options = Vec::new();
    for distractor in authored_distractors {
        let trimmed = distractor.trim();
        let content = content(trimmed, card);
        if !content.is_empty() && seen.insert(content) {
            options.push(trimmed.to_string());
        }
    }
    if options.is_empty() {
        return None;
    }
    options.push(correct_text.clone());
    let mut rng = Rng::new(seed);
    shuffle(&mut options, &mut rng);
    let correct = options.iter().position(|option| *option == correct_text)?;
    Some(ChoiceQuestion {
        options,
        correct,
        multiple: false,
        correct_set: vec![correct],
    })
}

pub fn build_authored_multi(
    card: &Card,
    seed: u64,
    authored_distractors: &[String],
) -> Option<ChoiceQuestion> {
    let mut seen = HashSet::new();
    let mut correct_texts = Vec::new();
    for line in authored_correct_options(card) {
        let trimmed = line.trim();
        let content = content(trimmed, card);
        if !content.is_empty() && seen.insert(content) {
            correct_texts.push(trimmed.to_string());
        }
    }
    let mut options = correct_texts.clone();
    let correct_count = options.len();
    for distractor in authored_distractors {
        let trimmed = distractor.trim();
        let content = content(trimmed, card);
        if !content.is_empty() && seen.insert(content) {
            options.push(trimmed.to_string());
        }
    }
    if correct_count == 0 || options.len() == correct_count {
        return None;
    }
    let mut rng = Rng::new(seed);
    shuffle(&mut options, &mut rng);
    let correct_set: Vec<usize> = options
        .iter()
        .enumerate()
        .filter(|(_, option)| correct_texts.contains(option))
        .map(|(index, _)| index)
        .collect();
    let correct = *correct_set.first()?;
    Some(ChoiceQuestion {
        options,
        correct,
        multiple: true,
        correct_set,
    })
}

pub fn can_build(card: &Card, ai_distractors: &[String]) -> bool {
    // Image-region cards are deliberately excluded, even against stale
    // cached distractors the eligibility gate never minted.
    if card.region.is_some() && !card.is_text_blank_card() {
        return false;
    }
    distinct_distractors(card, ai_distractors).len() == NUM_OPTIONS - 1
}

/// A table row card's distractor pool: its own column, i.e. sibling rows of
/// the same container in the same direction (a reversed sibling's answer IS
/// the front column).
pub fn column_pool(card: &Card, deck_cards: &[Card]) -> Vec<String> {
    let (Some(token), Some(row)) = (card.token.as_deref(), card.row.as_deref()) else {
        return Vec::new();
    };
    deck_cards
        .iter()
        .filter(|sibling| {
            sibling.token.as_deref() == Some(token)
                && sibling.reversed == card.reversed
                && sibling.row.as_deref().is_some_and(|r| r != row)
        })
        .map(answer_text)
        .collect()
}

pub fn build_sampled(card: &Card, seed: u64, deck_cards: &[Card]) -> Option<ChoiceQuestion> {
    if card.sampling == Some(false) {
        return None;
    }
    let pool = column_pool(card, deck_cards);
    let sampled = sample::sample_distractors(&answer_text(card), &pool, seed, NUM_OPTIONS - 1)?;
    build(card, seed, &sampled)
}

pub fn can_sample(card: &Card, deck_cards: &[Card]) -> bool {
    if card.region.is_some() && !card.is_text_blank_card() {
        return false;
    }
    build_sampled(card, 0, deck_cards).is_some()
}

pub fn recognition_question(
    card: &Card,
    seed: u64,
    ai_distractors: Option<&[String]>,
) -> Option<ChoiceQuestion> {
    if card.back.len() != 1 {
        return None;
    }
    let ai = ai_distractors.filter(|d| d.len() >= NUM_OPTIONS - 1)?;
    build(card, seed, ai)
}

// SplitMix64: good enough for shuffling options, and avoids a dependency.
struct Rng(u64);

impl Rng {
    fn new(seed: u64) -> Self {
        Self(seed)
    }

    fn next_u64(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    fn below(&mut self, n: usize) -> usize {
        (self.next_u64() % n as u64) as usize
    }
}

fn shuffle<T>(items: &mut [T], rng: &mut Rng) {
    for i in (1..items.len()).rev() {
        items.swap(i, rng.below(i + 1));
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use super::*;

    fn card(line: usize, back: &str) -> Card {
        Card::plain(
            Arc::from("deck.txt"),
            format!("front {line}"),
            back.split('\n').map(String::from).collect(),
            Vec::new(),
            line,
        )
    }

    fn ai(distractors: &[&str]) -> Vec<String> {
        distractors.iter().map(|s| s.to_string()).collect()
    }

    fn multi_card(back: &[&str]) -> Card {
        let mut c = card(1, &back.join("\n"));
        c.multiple_choice = true;
        c
    }

    fn authored_card(source: &str) -> Card {
        crate::parser::parse_str("deck.md", source)
            .unwrap()
            .remove(0)
    }

    #[test]
    fn a_multi_question_offers_every_correct_line_and_every_distractor() {
        let c = multi_card(&["2", "4"]);
        let q = build_authored_multi(&c, 7, &ai(&["3", "5"])).unwrap();
        assert_eq!(4, q.options.len());
        assert!(q.multiple);
        assert_eq!(2, q.correct_set.len());
        for &i in &q.correct_set {
            assert!(
                ["2", "4"].contains(&q.options[i].as_str()),
                "index {i} must be correct"
            );
        }
        assert_eq!(
            q.correct, q.correct_set[0],
            "correct mirrors the first correct index"
        );
    }

    #[test]
    fn an_atomic_option_keeps_a_supporting_quotation_but_not_its_marker() {
        let c = card(1, "the claim\n> supporting quotation");
        let q = build(&c, 7, &ai(&["one", "two", "three"])).unwrap();

        assert_eq!(
            q.options[q.correct], "the claim\nsupporting quotation",
            "one option is the WHOLE answer, so the quotation stays; dropping it \
             would offer a truncated answer as the correct pick"
        );
        for option in &q.options {
            assert!(
                !option.contains('>'),
                "no option shows authoring syntax: {option:?}"
            );
        }
    }

    #[test]
    fn a_multi_question_correct_set_is_ascending_and_seed_stable() {
        let c = multi_card(&["alpha", "beta", "gamma"]);
        let d = ai(&["delta", "epsilon"]);
        let q1 = build_authored_multi(&c, 42, &d).unwrap();
        let q2 = build_authored_multi(&c, 42, &d).unwrap();
        assert_eq!(q1.options, q2.options, "same seed, same order");
        assert_eq!(q1.correct_set, q2.correct_set);
        assert!(
            q1.correct_set.windows(2).all(|w| w[0] < w[1]),
            "ascending: {:?}",
            q1.correct_set
        );
    }

    #[test]
    fn a_multi_question_without_a_distractor_does_not_build() {
        let c = multi_card(&["2", "4"]);
        assert!(build_authored_multi(&c, 7, &[]).is_none());
        assert!(
            build_authored_multi(&c, 7, &ai(&["2"])).is_none(),
            "a distractor duplicating a correct option deduplicates away"
        );
    }

    #[test]
    fn a_multi_question_deduplicates_correct_lines_by_content() {
        let c = multi_card(&["**2**", "2", "4"]);
        let q = build_authored_multi(&c, 7, &ai(&["3"])).unwrap();
        assert_eq!(
            3,
            q.options.len(),
            "styled duplicate collapses: {:?}",
            q.options
        );
        assert_eq!(2, q.correct_set.len());
    }

    #[test]
    fn position_dependent_choice_note_references_are_recognized_narrowly() {
        for unsafe_note in [
            "Option 2 reverses the relation.",
            "Choice #3 omits the guard.",
            "The second option is too broad.",
            "Option B confuses identity with sampling.",
        ] {
            assert!(note_names_position(unsafe_note), "{unsafe_note}");
        }
        for safe_note in [
            "The length-limit claim invents a grammar rule.",
            "There are 2 independent reasons.",
            "Option parsing happens before sampling.",
            "Scoping was a choice, not a parser limitation.",
        ] {
            assert!(!note_names_position(safe_note), "{safe_note}");
        }
    }

    fn table_card(token: &str, row: &str, back: &str, reversed: bool) -> Card {
        let mut c = card(1, back);
        c.token = Some(Arc::from(token));
        c.row = Some(Arc::from(row));
        c.reversed = reversed;
        c
    }

    #[test]
    fn the_column_pool_is_same_direction_sibling_rows_of_one_container() {
        let deck = vec![
            table_card("card-t0", "aaaaaa", "alpha", false),
            table_card("card-t0", "bbbbbb", "beta", false),
            table_card("card-t0", "cccccc", "gamma", false),
            table_card("card-t0", "aaaaaa", "front-a", true),
            table_card("card-t0", "bbbbbb", "front-b", true),
            table_card("card-t1", "dddddd", "other-table", false),
            card(9, "rowless"),
        ];
        assert_eq!(
            vec!["beta", "gamma"],
            column_pool(&deck[0], &deck),
            "same container, same direction, other rows only"
        );
        assert_eq!(
            vec!["front-b"],
            column_pool(&deck[3], &deck),
            "the reversed pool is the front column"
        );
        assert!(
            column_pool(&deck[6], &deck).is_empty(),
            "a rowless card never samples"
        );
        assert_eq!(
            vec!["other-table"],
            column_pool(&table_card("card-t1", "eeeeee", "x", false), &deck),
            "pools never cross containers"
        );
    }

    #[test]
    fn build_sampled_needs_three_distinct_column_values() {
        let deck = vec![
            table_card("card-t0", "aaaaaa", "alpha", false),
            table_card("card-t0", "bbbbbb", "beta", false),
            table_card("card-t0", "cccccc", "beta", false),
            table_card("card-t0", "dddddd", "gamma", false),
        ];
        assert!(
            !can_sample(&deck[0], &deck),
            "beta twice leaves a two-value pool"
        );
        assert!(build_sampled(&deck[0], 7, &deck).is_none());

        let mut deck = deck;
        deck.push(table_card("card-t0", "eeeeee", "delta", false));
        assert!(can_sample(&deck[0], &deck));
        let question = build_sampled(&deck[0], 7, &deck).expect("a full pick");
        assert_eq!(NUM_OPTIONS, question.options.len());
        assert_eq!("alpha", question.options[question.correct]);
    }

    #[test]
    fn question_has_four_options_with_correct_exactly_once() {
        let c = card(1, "alpha");
        let d = ai(&["beta", "gamma", "delta", "epsilon", "zeta"]);
        let q = build(&c, 42, &d).unwrap();
        assert_eq!(NUM_OPTIONS, q.options.len());
        assert_eq!(1, q.options.iter().filter(|o| *o == "alpha").count());
        assert_eq!("alpha", q.options[q.correct]);
    }

    #[test]
    fn authored_build_uses_all_options_no_padding() {
        let mut c = card(1, "Paris");
        c.authored_distractors = vec!["London".into(), "Berlin".into()];
        let q = build_authored(&c, 1, &c.authored_distractors).unwrap();
        assert_eq!(3, q.options.len());
        assert_eq!("Paris", q.options[q.correct]);
        assert_eq!(
            1,
            q.options.iter().filter(|option| *option == "Paris").count()
        );
    }

    #[test]
    fn a_quoted_correct_option_stays_distinct_from_the_same_unquoted_distractor() {
        let c = authored_card(
            "## scope\n- [x] > not guaranteed\n- [ ] not guaranteed\n<!-- choices: single -->\n",
        );
        let q = build_authored(&c, 1, &c.authored_distractors)
            .expect("the invoked authored choice must remain buildable");

        assert_eq!(2, q.options.len());
        assert_eq!("> not guaranteed", q.options[q.correct]);
        assert!(q.options.iter().any(|option| option == "not guaranteed"));
    }

    #[test]
    fn checked_and_unchecked_quote_markers_project_symmetrically() {
        let c = authored_card(
            "## attribution\n- [x] > asserted claim\n- [ ] > attributed claim\n<!-- choices: single -->\n",
        );
        let q = build_authored(&c, 1, &c.authored_distractors).unwrap();

        assert_eq!("> asserted claim", q.options[q.correct]);
        assert!(
            q.options
                .iter()
                .any(|option| option == "> attributed claim")
        );
    }

    #[test]
    fn every_checked_quoted_option_remains_in_the_multiple_choice_correct_set() {
        let c = authored_card(
            "## all truths\n- [x] ordinary correct\n- [x] > attributed correct\n- [ ] ordinary wrong\n<!-- choices: multiple -->\n",
        );
        let q = build_authored_multi(&c, 1, &c.authored_distractors).unwrap();
        let mut correct: Vec<&str> = q
            .correct_set
            .iter()
            .map(|index| q.options[*index].as_str())
            .collect();
        correct.sort_unstable();

        assert_eq!(vec!["> attributed correct", "ordinary correct"], correct);
    }

    #[test]
    fn authored_build_needs_at_least_one_distractor() {
        let c = card(1, "Paris");
        assert!(build_authored(&c, 1, &[]).is_none());
    }

    #[test]
    fn fewer_than_three_distractors_yields_none() {
        let c = card(1, "alpha");
        assert!(build(&c, 42, &ai(&["beta", "gamma"])).is_none());
    }

    #[test]
    fn duplicate_distractors_count_once() {
        let c = card(1, "alpha");
        assert!(build(&c, 42, &ai(&["beta", "beta", "alpha"])).is_none());
    }

    #[test]
    fn ai_distractors_deduplicate_by_content_but_keep_source() {
        let c = card(1, "$x$");
        let q = build(&c, 42, &ai(&["x", "$y$", "z", "w"])).unwrap();
        assert_eq!("$x$", q.options[q.correct]);
        assert!(!q.options.iter().any(|option| option == "x"));
        assert!(q.options.iter().any(|option| option == "$y$"));
    }

    #[test]
    fn authored_distractors_deduplicate_by_content_but_keep_source() {
        let mut c = card(1, "$x^2$");
        c.authored_distractors = vec![
            "x^2".into(),
            "$x^3$".into(),
            "**four**".into(),
            "four".into(),
        ];
        let q = build_authored(&c, 1, &c.authored_distractors).unwrap();
        assert_eq!("$x^2$", q.options[q.correct]);
        assert!(!q.options.iter().any(|option| option == "x^2"));
        assert!(q.options.iter().any(|option| option == "$x^3$"));
        assert_eq!(
            1,
            q.options
                .iter()
                .filter(|option| content(option, &c) == "four")
                .count()
        );
    }

    #[test]
    fn an_ai_distractor_equal_to_the_answer_is_dropped() {
        let c = card(1, "alpha");
        let d = ai(&["alpha", "beta", "gamma", "delta"]);
        for seed in 0..10 {
            let q = build(&c, seed, &d).unwrap();
            assert_eq!(1, q.options.iter().filter(|o| *o == "alpha").count());
        }
    }

    #[test]
    fn same_seed_same_question() {
        let c = card(1, "alpha");
        let d = ai(&["beta", "gamma", "delta"]);
        let a = build(&c, 7, &d).unwrap();
        let b = build(&c, 7, &d).unwrap();
        assert_eq!(a.options, b.options);
        assert_eq!(a.correct, b.correct);
    }

    #[test]
    fn different_seeds_vary_the_options() {
        let c = card(1, "alpha");
        let d = ai(&["beta", "gamma", "delta"]);
        let orders: HashSet<Vec<String>> = (0..10)
            .map(|seed| build(&c, seed, &d).unwrap().options)
            .collect();
        assert!(orders.len() > 1, "options never varied across seeds");
    }

    #[test]
    fn multi_line_answers_become_one_option() {
        let c = card(1, "line a\nline b");
        let d = ai(&["x", "y", "z"]);
        let q = build(&c, 1, &d).unwrap();
        assert_eq!("line a\nline b", q.options[q.correct]);
    }

    #[test]
    fn recognition_question_needs_atomic_answer_and_full_ai_distractors() {
        let c = card(1, "alpha");
        let d = ai(&["w1", "w2", "w3"]);
        let q = recognition_question(&c, 1, Some(&d)).unwrap();
        assert_eq!(NUM_OPTIONS, q.options.len());
        assert_eq!("alpha", q.options[q.correct]);
    }

    #[test]
    fn recognition_question_rejects_too_few_ai_distractors() {
        let c = card(1, "alpha");
        assert!(recognition_question(&c, 1, Some(&ai(&["w1", "w2"]))).is_none());
        assert!(recognition_question(&c, 1, None).is_none());
    }

    #[test]
    fn recognition_question_rejects_multi_line_answers() {
        let c = card(1, "line a\nline b");
        let d = ai(&["w1", "w2", "w3"]);
        assert!(recognition_question(&c, 1, Some(&d)).is_none());
    }

    #[test]
    fn same_appearance_seed_is_stable_but_later_appearances_vary_the_order() {
        let c = card(1, "alpha");
        let d = ai(&["beta", "gamma", "delta"]);
        let id = "q42";

        let first = build(&c, seed_for(id, 100, 1), &d).unwrap();
        let first_again = build(&c, seed_for(id, 100, 1), &d).unwrap();
        assert_eq!(
            first.options, first_again.options,
            "the same appearance must not reshuffle mid-poll"
        );

        // Allow for the rare same-permutation collision across a couple of
        // seeds by checking a handful of later appearances.
        let later_orders_differ = (2..12)
            .map(|appearance| build(&c, seed_for(id, 100, appearance), &d).unwrap())
            .any(|q| q.options != first.options);
        assert!(
            later_orders_differ,
            "no later appearance ever varied the order"
        );
    }

    #[test]
    fn an_image_region_card_takes_no_choice_question_from_any_source() {
        let text = "## q\n![](a.png)\n<!-- blank: rect x=1 y=1 width=2 height=2 hidden=\"answer\" b:a1b2c3 -->\n\n---\nback\n<!-- id: card-qregionregionregionregionx -->\n";
        let cards = crate::parser::parse_str("t.md", text).unwrap();
        let region = cards
            .iter()
            .find(|c| c.region.is_some())
            .expect("the blank produced a region card");
        assert!(
            !can_build(region, &ai(&["p", "q", "r"])),
            "cached distractors never build an image-region choice"
        );
        assert!(!can_sample(region, &cards));
    }

    #[test]
    fn a_text_span_card_builds_a_choice_from_cached_distractors() {
        let text = "## q\nthe answer is answer\n<!-- blank: span hidden=\"answer\" b:a1b2c3 -->\n<!-- id: card-qspanspanspanspanspanspanx -->\n";
        let cards = crate::parser::parse_str("t.md", text).unwrap();
        let span = cards
            .iter()
            .find(|c| c.region.is_some())
            .expect("the span produced a blank card");
        assert!(
            can_build(span, &ai(&["p", "q", "r"])),
            "a text span builds choices like the cloze it replaced"
        );
    }

    #[test]
    fn numeric_option_positions_are_one_based() {
        assert!(!note_names_position("Option 0 is impossible."));
        assert!(!note_names_position("The 0th option is impossible."));
        assert!(note_names_position("The 1st option is too broad."));
    }
}