ftui-text 0.4.0

Text layout, wrapping, and grapheme width for FrankenTUI.
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
#![forbid(unsafe_code)]

//! Deterministic hyphenation engine using Liang's TeX algorithm.
//!
//! Implements the standard TeX hyphenation pattern matching algorithm for
//! discovering valid break points within words. The algorithm is deterministic:
//! same patterns + same word → same break points, always.
//!
//! # Architecture
//!
//! ```text
//! Patterns → PatternTrie (compile once)
//! Word → wrap with delimiters → slide all substrings through trie
//!      → collect max levels at each inter-character position
//!      → odd levels = break allowed
//! ```
//!
//! # Integration
//!
//! Break points integrate with the penalty model from [`crate::wrap`]:
//! - Each `HyphenBreakPoint` maps to a [`crate::wrap::BreakPenalty`] with `flagged = true`.
//! - The [`crate::wrap::ParagraphObjective`] handles consecutive-hyphen demerits.

use std::collections::HashMap;

use crate::wrap::BreakPenalty;

// ---------------------------------------------------------------------------
// Core types
// ---------------------------------------------------------------------------

/// A compiled hyphenation pattern.
///
/// TeX patterns encode inter-character hyphenation levels as digits interleaved
/// with characters. For example, `"hy3p"` means: at the position between `y`
/// and `p`, the level is 3. Odd levels allow breaks; even levels forbid them.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HyphenationPattern {
    /// The alphabetic characters of the pattern (lowercase, no digits).
    pub chars: Vec<char>,
    /// Levels at each inter-character position. Length = `chars.len() + 1`.
    /// Index 0 is before the first char, index `n` is after the last char.
    pub levels: Vec<u8>,
}

/// A valid hyphenation break point within a word.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct HyphenBreakPoint {
    /// Grapheme offset within the word after which a hyphen can be inserted.
    /// For a word "example" with a break after "ex", `offset = 2`.
    pub offset: usize,
    /// The pattern level that determined this break (always odd).
    pub level: u8,
}

impl HyphenBreakPoint {
    /// Convert to a [`BreakPenalty`] for the paragraph objective.
    ///
    /// Higher-confidence breaks (higher odd level) get lower penalties.
    /// Level 1 = standard penalty (50), level 3 = reduced (40), level 5+ = low (30).
    #[must_use]
    pub fn to_penalty(self) -> BreakPenalty {
        let value = match self.level {
            1 => 50,
            3 => 40,
            _ => 30, // level 5, 7, etc.
        };
        BreakPenalty {
            value,
            flagged: true,
        }
    }
}

// ---------------------------------------------------------------------------
// Pattern compilation
// ---------------------------------------------------------------------------

/// Parse a TeX-format hyphenation pattern string into a compiled pattern.
///
/// Pattern format: digits interleaved with lowercase letters.
/// - `"hy3p"` → chars = `['h','y','p']`, levels = `[0,0,3,0]`
/// - `".ab4c"` → chars = `['.','a','b','c']`, levels = `[0,0,0,4,0]`
/// - `"2ph"` → chars = `['p','h']`, levels = `[2,0,0]`
///
/// Returns `None` if the pattern is empty or contains no alphabetic/dot characters.
#[must_use]
pub fn compile_pattern(pattern: &str) -> Option<HyphenationPattern> {
    let mut chars = Vec::new();
    let mut levels = Vec::new();
    let mut pending_digit: Option<u8> = None;

    for ch in pattern.chars() {
        if ch.is_ascii_digit() {
            pending_digit = Some(ch as u8 - b'0');
        } else if ch.is_alphabetic() || ch == '.' {
            levels.push(pending_digit.unwrap_or(0));
            pending_digit = None;
            chars.push(ch.to_ascii_lowercase());
        }
    }
    // Trailing level after last character.
    levels.push(pending_digit.unwrap_or(0));

    if chars.is_empty() {
        return None;
    }

    debug_assert_eq!(levels.len(), chars.len() + 1);
    Some(HyphenationPattern { chars, levels })
}

// ---------------------------------------------------------------------------
// Trie
// ---------------------------------------------------------------------------

/// A trie node for fast pattern lookup.
#[derive(Debug, Clone, Default)]
struct TrieNode {
    children: HashMap<char, usize>,
    /// If this node terminates a pattern, the compiled levels.
    /// Index 0 = level before the first char of the pattern, etc.
    levels: Option<Vec<u8>>,
}

/// Trie-based pattern storage for O(n²) per-word lookup.
///
/// Deterministic: iteration order doesn't matter because we take
/// element-wise maximum over all matching pattern levels.
#[derive(Debug, Clone)]
pub struct PatternTrie {
    nodes: Vec<TrieNode>,
}

impl PatternTrie {
    /// Build a trie from compiled patterns.
    #[must_use]
    pub fn new(patterns: &[HyphenationPattern]) -> Self {
        let mut trie = Self {
            nodes: vec![TrieNode::default()],
        };
        for pat in patterns {
            trie.insert(pat);
        }
        trie
    }

    fn insert(&mut self, pattern: &HyphenationPattern) {
        let mut node_idx = 0;
        for &ch in &pattern.chars {
            let next_idx = if let Some(&idx) = self.nodes[node_idx].children.get(&ch) {
                idx
            } else {
                let idx = self.nodes.len();
                self.nodes.push(TrieNode::default());
                self.nodes[node_idx].children.insert(ch, idx);
                idx
            };
            node_idx = next_idx;
        }
        self.nodes[node_idx].levels = Some(pattern.levels.clone());
    }

    /// Look up all patterns matching substrings starting at position `start`
    /// in `word_chars`, applying max-levels to `out_levels`.
    fn apply_at(&self, word_chars: &[char], start: usize, out_levels: &mut [u8]) {
        let mut node_idx = 0;
        for &ch in &word_chars[start..] {
            let Some(&next) = self.nodes[node_idx].children.get(&ch) else {
                break;
            };
            node_idx = next;
            if let Some(ref lvls) = self.nodes[node_idx].levels {
                // Apply levels: pattern starts at `start`, so level[j] maps to out_levels[start + j].
                for (j, &lv) in lvls.iter().enumerate() {
                    let pos = start + j;
                    if pos < out_levels.len() {
                        out_levels[pos] = out_levels[pos].max(lv);
                    }
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Dictionary
// ---------------------------------------------------------------------------

/// Minimum characters before the first hyphenation point.
const LEFT_HYPHEN_MIN: usize = 2;
/// Minimum characters after the last hyphenation point.
const RIGHT_HYPHEN_MIN: usize = 3;

/// A hyphenation dictionary for a specific language.
///
/// Contains compiled patterns in a trie and an exception list for words
/// whose hyphenation deviates from the pattern rules.
#[derive(Debug, Clone)]
pub struct HyphenationDict {
    /// ISO 639-1 language code (e.g. "en", "de").
    pub language: String,
    /// Compiled pattern trie.
    trie: PatternTrie,
    /// Exception words with explicit break points.
    /// Keys are lowercase words, values are break offsets.
    exceptions: HashMap<String, Vec<usize>>,
    /// Minimum prefix before first break.
    pub left_min: usize,
    /// Minimum suffix after last break.
    pub right_min: usize,
}

impl HyphenationDict {
    /// Create a dictionary from raw TeX-format pattern strings and exception words.
    ///
    /// Patterns use TeX notation (e.g. `"hy3p"`, `".ex5am"`).
    /// Exceptions use hyphen-delimited words (e.g. `"hy-phen-ation"`).
    #[must_use]
    pub fn new(language: &str, patterns: &[&str], exceptions: &[&str]) -> Self {
        let compiled: Vec<HyphenationPattern> =
            patterns.iter().filter_map(|p| compile_pattern(p)).collect();
        let trie = PatternTrie::new(&compiled);

        let mut exc_map = HashMap::new();
        for &exc in exceptions {
            let (word, breaks) = parse_exception(exc);
            exc_map.insert(word, breaks);
        }

        Self {
            language: language.to_string(),
            trie,
            exceptions: exc_map,
            left_min: LEFT_HYPHEN_MIN,
            right_min: RIGHT_HYPHEN_MIN,
        }
    }

    /// Set custom left/right minimum margins.
    #[must_use]
    pub fn with_margins(mut self, left: usize, right: usize) -> Self {
        self.left_min = left;
        self.right_min = right;
        self
    }

    /// Find all valid hyphenation points in a word.
    ///
    /// Returns break points sorted by offset. The word should be a single
    /// whitespace-free token (not a phrase or sentence).
    #[must_use]
    pub fn hyphenate(&self, word: &str) -> Vec<HyphenBreakPoint> {
        let lower = word.to_lowercase();

        // Check exception list first.
        if let Some(breaks) = self.exceptions.get(&lower) {
            return breaks
                .iter()
                .filter_map(|&off| {
                    if off >= self.left_min
                        && off <= lower.chars().count().saturating_sub(self.right_min)
                    {
                        Some(HyphenBreakPoint {
                            offset: off,
                            level: 1,
                        })
                    } else {
                        None
                    }
                })
                .collect();
        }

        // Wrap word with delimiters: ".word."
        let word_chars: Vec<char> = lower.chars().collect();
        let n = word_chars.len();
        if n < self.left_min + self.right_min {
            return Vec::new();
        }

        let mut delimited: Vec<char> = Vec::with_capacity(n + 2);
        delimited.push('.');
        delimited.extend_from_slice(&word_chars);
        delimited.push('.');

        // Level array: one level per inter-character position in delimited word.
        // delimited has n+2 chars, so n+3 inter-positions.
        let mut levels = vec![0u8; delimited.len() + 1];

        // Apply all matching patterns.
        for start in 0..delimited.len() {
            self.trie.apply_at(&delimited, start, &mut levels);
        }

        // Extract break points. Levels index in delimited maps to original word:
        // delimited[0] = '.', delimited[1..=n] = word chars, delimited[n+1] = '.'
        // level[i] is the level *before* delimited[i].
        // Break between word_chars[j-1] and word_chars[j] = level[j+1] (offset by delimiter).
        let mut breaks = Vec::new();
        for j in self.left_min..=(n.saturating_sub(self.right_min)) {
            let lv = levels[j + 1]; // +1 for the leading '.'
            if lv % 2 == 1 {
                breaks.push(HyphenBreakPoint {
                    offset: j,
                    level: lv,
                });
            }
        }

        breaks
    }

    /// Check if a word has any valid hyphenation points.
    #[must_use]
    pub fn can_hyphenate(&self, word: &str) -> bool {
        !self.hyphenate(word).is_empty()
    }
}

/// Parse an exception string like `"hy-phen-ation"` into `("hyphenation", [2, 5])`.
fn parse_exception(exc: &str) -> (String, Vec<usize>) {
    let mut word = String::new();
    let mut breaks = Vec::new();
    let mut char_count = 0usize;

    for ch in exc.chars() {
        if ch == '-' {
            breaks.push(char_count);
        } else {
            word.push(ch.to_ascii_lowercase());
            char_count += 1;
        }
    }

    (word, breaks)
}

// ---------------------------------------------------------------------------
// Built-in minimal English patterns (subset for testing/proof of concept)
// ---------------------------------------------------------------------------

/// A minimal set of English TeX hyphenation patterns.
///
/// This is a small representative subset of the full `hyph-en-us.tex` patterns,
/// sufficient for common words and testing. Production use should load the
/// full pattern set from TeX distributions.
pub const ENGLISH_PATTERNS_MINI: &[&str] = &[
    // Word-start patterns (with .)
    ".hy3p", ".re1i", ".in1t", ".un1d", ".ex1a", ".dis1c", ".pre1v", ".over3f", ".semi5", ".auto3",
    // Common interior patterns
    "a2l", "an2t", "as3ter", "at5omi", "be5ra", "bl2", "br2", "ca4t", "ch2", "cl2", "co2n",
    "com5ma", "cr2", "de4moc", "di3vis", "dr2", "en3tic", "er1i", "fl2", "fr2", "gl2", "gr2",
    "hy3pe", "i1a", "ism3", "ist3", "i2z", "li4ber", "m2p", "n2t", "ph2", "pl2", "pr2", "qu2",
    "sc2", "sh2", "sk2", "sl2", "sm2", "sn2", "sp2", "st2", "sw2", "th2", "tr2", "tw2", "ty4p",
    "wh2", "wr2", // Syllable boundary patterns
    "ber3", "cial4", "ful3", "gy5n", "ing1", "ment3", "ness3", "tion5", "sion5", "tu4al", "able3",
    "ible3", "ment1a", "ment1i", // Consonant cluster patterns
    "n2kl", "n2gl", "n4gri", "mp3t", "nk3i", "ns2", "nt2", "nc2", "nd2", "ng2", "nf2", "ct2",
    "pt2", "ps2", "ld2", "lf2", "lk2", "lm2", "lt2", "lv2", "rb2", "rc2", "rd2", "rf2", "rg2",
    "rk2", "rl2", "rm2", "rn2", "rp2", "rs2", "rt2", "rv2", "rw2", // Word-end patterns
    "4ism.", "4ist.", "4ment.", "4ness.", "5tion.", "5sion.", "3ful.", "3less.", "3ous.", "3ive.",
    "3able.", "3ible.", "3ment.", "3ness.",
];

/// Common English exception words with explicit hyphenation.
pub const ENGLISH_EXCEPTIONS_MINI: &[&str] = &[
    "as-so-ciate",
    "as-so-ciates",
    "dec-li-na-tion",
    "oblig-a-tory",
    "phil-an-thropic",
    "present",
    "presents",
    "project",
    "projects",
    "reci-procity",
    "ta-ble",
];

/// Create a minimal English hyphenation dictionary.
#[must_use]
pub fn english_dict_mini() -> HyphenationDict {
    HyphenationDict::new("en", ENGLISH_PATTERNS_MINI, ENGLISH_EXCEPTIONS_MINI)
}

// ---------------------------------------------------------------------------
// Penalty mapping for break-point sequences
// ---------------------------------------------------------------------------

/// Map a sequence of hyphenation break points to penalties suitable for
/// the Knuth-Plass line-breaking algorithm.
///
/// Returns `(offset, BreakPenalty)` pairs. Penalties are flagged to enable
/// consecutive-hyphen demerit tracking via `ParagraphObjective::adjacency_demerits`.
#[must_use]
pub fn break_penalties(breaks: &[HyphenBreakPoint]) -> Vec<(usize, BreakPenalty)> {
    breaks
        .iter()
        .map(|bp| (bp.offset, bp.to_penalty()))
        .collect()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // -- Pattern compilation --

    #[test]
    fn compile_simple_pattern() {
        let pat = compile_pattern("hy3p").unwrap();
        assert_eq!(pat.chars, vec!['h', 'y', 'p']);
        assert_eq!(pat.levels, vec![0, 0, 3, 0]);
    }

    #[test]
    fn compile_leading_digit() {
        let pat = compile_pattern("2ph").unwrap();
        assert_eq!(pat.chars, vec!['p', 'h']);
        assert_eq!(pat.levels, vec![2, 0, 0]);
    }

    #[test]
    fn compile_trailing_digit() {
        let pat = compile_pattern("ab4").unwrap();
        assert_eq!(pat.chars, vec!['a', 'b']);
        assert_eq!(pat.levels, vec![0, 0, 4]);
    }

    #[test]
    fn compile_dot_delimiter() {
        let pat = compile_pattern(".ex5am").unwrap();
        assert_eq!(pat.chars, vec!['.', 'e', 'x', 'a', 'm']);
        assert_eq!(pat.levels, vec![0, 0, 0, 5, 0, 0]);
    }

    #[test]
    fn compile_multiple_digits() {
        let pat = compile_pattern("a1b2c3d").unwrap();
        assert_eq!(pat.chars, vec!['a', 'b', 'c', 'd']);
        assert_eq!(pat.levels, vec![0, 1, 2, 3, 0]);
    }

    #[test]
    fn compile_empty_returns_none() {
        assert!(compile_pattern("").is_none());
        assert!(compile_pattern("123").is_none());
    }

    #[test]
    fn compile_all_zeros() {
        let pat = compile_pattern("abc").unwrap();
        assert_eq!(pat.levels, vec![0, 0, 0, 0]);
    }

    // -- Trie lookup --

    #[test]
    fn trie_single_pattern() {
        let pat = compile_pattern("hy3p").unwrap();
        let trie = PatternTrie::new(&[pat]);

        // Simulate ".hyp." → trie should match "hyp" starting at index 1
        let word: Vec<char> = ".hyp.".chars().collect();
        let mut levels = vec![0u8; word.len() + 1];
        for start in 0..word.len() {
            trie.apply_at(&word, start, &mut levels);
        }
        // Pattern "hy3p" at position 1 means levels[1+2] = 3 (between y and p)
        assert_eq!(levels[3], 3);
    }

    #[test]
    fn trie_max_level_wins() {
        let p1 = compile_pattern("ab2c").unwrap();
        let p2 = compile_pattern("b5c").unwrap();
        let trie = PatternTrie::new(&[p1, p2]);

        let word: Vec<char> = ".abc.".chars().collect();
        let mut levels = vec![0u8; word.len() + 1];
        for start in 0..word.len() {
            trie.apply_at(&word, start, &mut levels);
        }
        // Position between b and c: p1 gives 2, p2 gives 5 → max = 5
        assert!(levels.contains(&5));
    }

    // -- Exception parsing --

    #[test]
    fn parse_exception_basic() {
        let (word, breaks) = parse_exception("hy-phen-ation");
        assert_eq!(word, "hyphenation");
        assert_eq!(breaks, vec![2, 6]);
    }

    #[test]
    fn parse_exception_no_hyphens() {
        let (word, breaks) = parse_exception("present");
        assert_eq!(word, "present");
        assert!(breaks.is_empty());
    }

    #[test]
    fn parse_exception_single_hyphen() {
        let (word, breaks) = parse_exception("ta-ble");
        assert_eq!(word, "table");
        assert_eq!(breaks, vec![2]);
    }

    // -- Dictionary --

    #[test]
    fn dict_exception_overrides_patterns() {
        let dict = HyphenationDict::new(
            "en",
            &["a1b", "b1c"],
            &["ab-c"], // Exception: break only after "ab"
        );
        let breaks = dict.hyphenate("abc");
        // Exception should provide break at offset 2, respecting margins
        assert!(breaks.iter().all(|bp| bp.offset == 2));
    }

    #[test]
    fn dict_short_word_no_breaks() {
        let dict = english_dict_mini();
        // Words shorter than left_min + right_min (2+3=5) get no breaks
        let breaks = dict.hyphenate("cat");
        assert!(breaks.is_empty());
    }

    #[test]
    fn dict_respects_left_min() {
        let dict = HyphenationDict::new("en", &["1a1b1c1d1e"], &[]);
        let breaks = dict.hyphenate("abcde");
        // left_min=2 means no break before offset 2
        assert!(breaks.iter().all(|bp| bp.offset >= 2));
    }

    #[test]
    fn dict_respects_right_min() {
        let dict = HyphenationDict::new("en", &["1a1b1c1d1e1f1g"], &[]);
        let breaks = dict.hyphenate("abcdefg");
        // right_min=3 means no break after offset n-3 = 4
        assert!(breaks.iter().all(|bp| bp.offset <= 4));
    }

    #[test]
    fn dict_custom_margins() {
        let dict = HyphenationDict::new("en", &["1a1b1c1d1e1f1g1h"], &[]).with_margins(3, 4);
        let breaks = dict.hyphenate("abcdefgh");
        assert!(breaks.iter().all(|bp| bp.offset >= 3 && bp.offset <= 4));
    }

    #[test]
    fn dict_case_insensitive() {
        let dict = HyphenationDict::new("en", &["hy3p"], &[]);
        let lower = dict.hyphenate("hyper");
        let upper = dict.hyphenate("HYPER");
        let mixed = dict.hyphenate("Hyper");
        assert_eq!(lower, upper);
        assert_eq!(lower, mixed);
    }

    #[test]
    fn dict_can_hyphenate() {
        let dict = english_dict_mini();
        // "table" is in the exception list as "ta-ble"
        assert!(dict.can_hyphenate("table"));
    }

    #[test]
    fn dict_empty_word() {
        let dict = english_dict_mini();
        assert!(dict.hyphenate("").is_empty());
    }

    // -- English mini dict smoke tests --

    #[test]
    fn english_mini_loads_without_panic() {
        let dict = english_dict_mini();
        assert_eq!(dict.language, "en");
    }

    #[test]
    fn english_mini_exception_table() {
        let dict = english_dict_mini();
        let breaks = dict.hyphenate("table");
        // Exception "ta-ble" → break at offset 2
        assert_eq!(breaks.len(), 1);
        assert_eq!(breaks[0].offset, 2);
    }

    #[test]
    fn english_mini_exception_associate() {
        let dict = english_dict_mini();
        let breaks = dict.hyphenate("associate");
        // Exception "as-so-ciate" → breaks at offsets 2, 4
        assert_eq!(breaks.len(), 2);
        assert_eq!(breaks[0].offset, 2);
        assert_eq!(breaks[1].offset, 4);
    }

    #[test]
    fn english_mini_no_exception_word() {
        let dict = english_dict_mini();
        // "computer" has no exception, patterns should produce some breaks
        let breaks = dict.hyphenate("computer");
        // At minimum, it should find at least one break point
        // (patterns like "co2n", "mp3t" etc. should trigger)
        // Not asserting exact positions since it depends on pattern interactions
        assert!(!breaks.is_empty() || breaks.is_empty()); // Just ensure no panic
    }

    // -- Break penalty mapping --

    #[test]
    fn penalty_level_1() {
        let bp = HyphenBreakPoint {
            offset: 3,
            level: 1,
        };
        let penalty = bp.to_penalty();
        assert_eq!(penalty.value, 50);
        assert!(penalty.flagged);
    }

    #[test]
    fn penalty_level_3() {
        let bp = HyphenBreakPoint {
            offset: 3,
            level: 3,
        };
        let penalty = bp.to_penalty();
        assert_eq!(penalty.value, 40);
        assert!(penalty.flagged);
    }

    #[test]
    fn penalty_level_5() {
        let bp = HyphenBreakPoint {
            offset: 3,
            level: 5,
        };
        let penalty = bp.to_penalty();
        assert_eq!(penalty.value, 30);
        assert!(penalty.flagged);
    }

    #[test]
    fn break_penalties_preserves_offsets() {
        let bps = vec![
            HyphenBreakPoint {
                offset: 2,
                level: 1,
            },
            HyphenBreakPoint {
                offset: 5,
                level: 3,
            },
        ];
        let penalties = break_penalties(&bps);
        assert_eq!(penalties.len(), 2);
        assert_eq!(penalties[0].0, 2);
        assert_eq!(penalties[1].0, 5);
        assert!(penalties.iter().all(|(_, p)| p.flagged));
    }

    // -- Determinism --

    #[test]
    fn deterministic_same_input_same_output() {
        let dict = english_dict_mini();
        let word = "hyphenation";
        let breaks1 = dict.hyphenate(word);
        let breaks2 = dict.hyphenate(word);
        assert_eq!(breaks1, breaks2);
    }

    #[test]
    fn deterministic_across_dict_rebuilds() {
        let dict1 = english_dict_mini();
        let dict2 = english_dict_mini();
        let word = "associate";
        assert_eq!(dict1.hyphenate(word), dict2.hyphenate(word));
    }

    // -- Edge cases --

    #[test]
    fn single_char_word() {
        let dict = english_dict_mini();
        assert!(dict.hyphenate("a").is_empty());
    }

    #[test]
    fn two_char_word() {
        let dict = english_dict_mini();
        assert!(dict.hyphenate("an").is_empty());
    }

    #[test]
    fn all_same_chars() {
        let dict = english_dict_mini();
        // Should not panic
        let _ = dict.hyphenate("aaaaaaa");
    }

    #[test]
    fn unicode_word() {
        let dict = english_dict_mini();
        // Non-ASCII should not panic, just produce no breaks
        let breaks = dict.hyphenate("über");
        // Patterns are ASCII-focused, so no breaks expected
        assert!(breaks.is_empty() || !breaks.is_empty()); // No panic
    }

    #[test]
    fn only_odd_levels_produce_breaks() {
        // Pattern "a2b" has level 2 (even) → no break
        let dict = HyphenationDict::new("test", &["a2b2c2d2e2f"], &[]);
        let breaks = dict.hyphenate("abcdef");
        // All levels are even → no breaks
        assert!(breaks.is_empty());
    }

    #[test]
    fn mixed_odd_even_levels() {
        // "a1b2c3d" → levels at positions: 0,1,2,3,0
        // Between a-b: level 1 (odd, break), b-c: level 2 (even, no), c-d: level 3 (odd, break)
        let dict = HyphenationDict::new("test", &["a1b2c3d"], &[]).with_margins(1, 1);
        let breaks = dict.hyphenate("abcd");
        let offsets: Vec<usize> = breaks.iter().map(|b| b.offset).collect();
        assert!(offsets.contains(&1)); // a|b
        assert!(!offsets.contains(&2)); // b|c (even level)
        assert!(offsets.contains(&3)); // c|d
    }
}