nika 0.35.4

Semantic YAML workflow engine for AI tasks - DAG execution, MCP integration, multi-provider LLM support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
//! Matrix Decrypt Widget - Text reveal effect with emoji chaos
//!
//! Creates a "decryption" effect where text is revealed progressively
//! from emoji/kanji chaos, synchronized with streaming tokens.
//!
//! ```text
//! Frame 0: 🏴‍☠️自🐉ニ🦋カ🪐海🦄賊🌌🐔🦈🔮🐙✨🌊🧚🛰️💎🦁
//! Frame 3: l🏴‍☠️t 自bl🐉ck ニ= B🦋ock:カ:bor🪐ere海d()
//! Frame 6: let bl🐙ck = Block::bor✨ered().title("Wel🌊ome");
//! Frame 10: let block = Block::bordered().title("Welcome");
//! ```

use std::borrow::Cow;

use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use ratatui::{
    buffer::Buffer,
    layout::Rect,
    style::{Color, Style},
    text::{Line, Span},
    widgets::Widget,
};

use crate::tui::theme::solarized;

// ═══════════════════════════════════════════════════════════════════════════════
// THEMED EMOJI COLLECTIONS
// ═══════════════════════════════════════════════════════════════════════════════

/// Pirate theme emojis (for fetch: verb - ocean/network)
const PIRATE_EMOJIS: &[&str] = &[
    "🏴‍☠️",
    "🐙",
    "🐳",
    "🌊",
    "🦈",
    "",
    "🗡️",
    "💀",
    "🦜",
    "🏝️",
    "🧭",
    "",
    "🦑",
    "🐚",
    "🪸",
];

/// Cosmic theme emojis (for infer: verb - thinking/AI)
const COSMIC_EMOJIS: &[&str] = &[
    "🪐", "🌌", "🛰️", "🌙", "", "🔮", "🌟", "☄️", "🚀", "👽", "🛸", "", "💫", "🌠", "🔭",
];

/// Unicorn theme emojis (for magical/creative outputs)
const UNICORN_EMOJIS: &[&str] = &[
    "🦄", "🌈", "🧚", "🦋", "", "💖", "🌸", "🎀", "💫", "🍭", "🎠", "💜", "🩷", "🪷", "🦩",
];

/// Animal emojis - Nika mascots (for agent: verb)
const ANIMAL_EMOJIS: &[&str] = &[
    "🐔",
    "🐤",
    "🦙",
    "🐭",
    "🐉",
    "🦖",
    "🦀",
    "🦞",
    "🦁",
    "🐦‍🔥",
    "🦆",
    "🦦",
    "🐒",
    "🦊",
    "🦚",
    "🪽",
    "🐿️",
    "🦝",
    "🐸",
    "🦎",
];

/// Food & drink emojis (fun sprinkles)
const FOOD_EMOJIS: &[&str] = &[
    "🧀", "🍕", "🍺", "🍸", "🍻", "🥂", "🍣", "🌮", "🥦", "🌵", "🌴", "🧃", "🍜", "🥖", "🥐", "🌶️",
    "🍩", "🧁", "🍰", "🎂",
];

/// Special/rare emojis (high impact)
const SPECIAL_EMOJIS: &[&str] = &[
    "💎", "🦾", "", "👺", "🫀", "🗿", "⛩️", "🍀", "🎅", "🎨", "🔥", "💀", "👾", "🤖", "🧠", "🎯",
    "🎪", "🎭", "🏆", "🎖️",
];

/// Tech emojis (for invoke: verb - MCP tools)
const TECH_EMOJIS: &[&str] = &[
    "🔌", "💾", "📡", "🖥️", "⚙️", "🔧", "🛠️", "📟", "💿", "🔋", "📶", "🖨️", "⌨️", "🖱️", "💽",
];

/// Nature emojis (for exec: verb - shell/system)
const NATURE_EMOJIS: &[&str] = &[
    "🌵", "🌴", "🥦", "🍀", "🌿", "🍃", "🌱", "🌲", "🌳", "🍂", "🍁", "🌾", "🪴", "🎋", "🎍",
];

/// Half-width Katakana - Classic Matrix style (monospace in terminals!)
const MATRIX_KATAKANA: &[char] = &[
    // Half-width katakana - consistent single-cell width
    '', '', '', '', '', '', '', '', '', '', '', '', '', '', 'ソ', '', '', '', '',
    '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '',
    '', '', '', '', '', '', '',
];

/// Full-width Kanji for flavor (double-width)
const KANJI_CHARS: &[char] = &[
    // 自由 (jiyū = Freedom) + ニカ (Nika)
    '', '', '', '', // 海賊 (kaizoku = Pirate)
    '', '', // Matrix-style kanji
    '', '', '', '', '', '', '', '', '', '',
];

/// Nerd/geek reference strings (cycling text)
const NERD_REFS: &[&str] = &[
    "42",    // Hitchhiker's Guide
    "EOF",   // End of file
    "NULL",  // Null pointer
    "sudo",  // Unix
    "git",   // Version control
    "0x",    // Hex prefix
    "fn",    // Rust function
    "async", // Async programming
    "OK",    // HTTP 200
    "ACK",   // Network acknowledgment
    "SYN",   // TCP handshake
    "NaN",   // Not a Number
    "π",     // Pi
    "",     // Infinity
    "λ",     // Lambda
    "Ω",     // Omega
];

/// Cycling text words
const CYCLING_WORDS: &[&str] = &[
    "Nika", "Freedom", "Liberté", "自由", "LOVE", "PEACE", "CODE", "RUST", "ASYNC", "MCP", "DAG",
    "YAML", "ARR", "AHOY", "MAGIC",
];

// ═══════════════════════════════════════════════════════════════════════════════
// VERB TYPE FOR COLOR MATCHING
// ═══════════════════════════════════════════════════════════════════════════════

/// Verb type for color theming
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DecryptVerb {
    /// LLM inference - violet/cosmic theme
    #[default]
    Infer,
    /// Shell execution - green/nature theme
    Exec,
    /// HTTP fetch - cyan/pirate theme
    Fetch,
    /// MCP invoke - blue/tech theme
    Invoke,
    /// Agent loop - yellow/animal theme
    Agent,
    /// Chat message - neutral
    Chat,
}

impl DecryptVerb {
    /// Get the primary color for this verb
    pub fn color(&self) -> Color {
        match self {
            DecryptVerb::Infer => solarized::VIOLET,
            DecryptVerb::Exec => solarized::GREEN,
            DecryptVerb::Fetch => solarized::CYAN,
            DecryptVerb::Invoke => solarized::BLUE,
            DecryptVerb::Agent => solarized::YELLOW,
            DecryptVerb::Chat => solarized::BASE0,
        }
    }

    /// Get the themed emojis for this verb
    pub fn emojis(&self) -> &'static [&'static str] {
        match self {
            DecryptVerb::Infer => COSMIC_EMOJIS,
            DecryptVerb::Exec => NATURE_EMOJIS,
            DecryptVerb::Fetch => PIRATE_EMOJIS,
            DecryptVerb::Invoke => TECH_EMOJIS,
            DecryptVerb::Agent => ANIMAL_EMOJIS,
            DecryptVerb::Chat => SPECIAL_EMOJIS,
        }
    }

    /// Get the icon for this verb
    pub fn icon(&self) -> &'static str {
        match self {
            DecryptVerb::Infer => "",
            DecryptVerb::Exec => "📟",
            DecryptVerb::Fetch => "🛰️",
            DecryptVerb::Invoke => "🔌",
            DecryptVerb::Agent => "🐔",
            DecryptVerb::Chat => "💬",
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// MATRIX DECRYPT STATE
// ═══════════════════════════════════════════════════════════════════════════════

/// State for a single character being decrypted
#[derive(Clone)]
struct CharState {
    /// The final revealed character
    target: char,
    /// Current display (may be emoji or random char)
    current: DecryptGlyph,
    /// Reveal progress (0.0 = chaos, 1.0 = revealed)
    progress: f32,
    /// Per-character random seed
    seed: u64,
}

#[derive(Clone)]
enum DecryptGlyph {
    Char(char),
    Emoji(&'static str),
    Kanji(char),
    Text(&'static str),
}

impl DecryptGlyph {
    /// Returns glyph as string. Uses Cow to avoid allocation for static strings.
    fn as_str(&self) -> Cow<'static, str> {
        match self {
            DecryptGlyph::Char(c) => Cow::Owned(c.to_string()),
            DecryptGlyph::Emoji(e) => Cow::Borrowed(*e),
            DecryptGlyph::Kanji(k) => Cow::Owned(k.to_string()),
            DecryptGlyph::Text(t) => Cow::Borrowed(*t),
        }
    }

    fn display_width(&self) -> usize {
        match self {
            DecryptGlyph::Char(_) => 1,
            DecryptGlyph::Emoji(_) => 2,
            DecryptGlyph::Kanji(_) => 2,
            DecryptGlyph::Text(t) => t.chars().count(),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// MATRIX DECRYPT WIDGET
// ═══════════════════════════════════════════════════════════════════════════════

/// Matrix Decrypt effect widget
///
/// Reveals text progressively from emoji/kanji chaos, synchronized with streaming.
pub struct MatrixDecrypt<'a> {
    /// The text to reveal
    text: &'a str,
    /// Current reveal progress (0.0 = all chaos, 1.0 = all revealed)
    progress: f32,
    /// Animation frame for flickering effect
    frame: u8,
    /// Verb type for theming
    verb: DecryptVerb,
    /// Seed for reproducible randomness
    seed: u64,
    /// Whether to include rare emojis
    with_rare_emojis: bool,
    /// Whether to include kanji
    with_kanji: bool,
    /// Reveal speed multiplier
    speed: f32,
}

impl<'a> Default for MatrixDecrypt<'a> {
    fn default() -> Self {
        Self {
            text: "",
            progress: 0.0,
            frame: 0,
            verb: DecryptVerb::default(),
            seed: 42,
            with_rare_emojis: true,
            with_kanji: true,
            speed: 1.0,
        }
    }
}

impl<'a> MatrixDecrypt<'a> {
    pub fn new(text: &'a str) -> Self {
        Self {
            text,
            ..Default::default()
        }
    }

    /// Set the text to reveal
    pub fn text(mut self, text: &'a str) -> Self {
        self.text = text;
        self
    }

    /// Set reveal progress (0.0 = chaos, 1.0 = fully revealed)
    pub fn progress(mut self, progress: f32) -> Self {
        self.progress = progress.clamp(0.0, 1.0);
        self
    }

    /// Set animation frame for flickering
    pub fn frame(mut self, frame: u8) -> Self {
        self.frame = frame;
        self
    }

    /// Set verb type for color theming
    pub fn verb(mut self, verb: DecryptVerb) -> Self {
        self.verb = verb;
        self
    }

    /// Set random seed
    pub fn seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Enable/disable rare emojis
    pub fn with_rare_emojis(mut self, enable: bool) -> Self {
        self.with_rare_emojis = enable;
        self
    }

    /// Enable/disable kanji characters
    pub fn with_kanji(mut self, enable: bool) -> Self {
        self.with_kanji = enable;
        self
    }

    /// Set reveal speed multiplier
    pub fn speed(mut self, speed: f32) -> Self {
        self.speed = speed.max(0.1);
        self
    }

    /// Generate a random chaos glyph based on verb theme
    fn random_glyph(&self, rng: &mut SmallRng) -> DecryptGlyph {
        let roll: f32 = rng.gen();

        // TRUE Matrix-style: katakana-dominant, minimal emojis
        if roll < 0.35 {
            // 35% - ASCII characters (monospace)
            let chars: &[char] = &[
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', '_', '<', '>', '[', ']', '{', '}', '/', '*', '#', '@', '^',
            ];
            let idx = rng.gen_range(0..chars.len());
            DecryptGlyph::Char(chars[idx])
        } else if roll < 0.80 {
            // 45% - Half-width katakana (THE Matrix look!)
            let idx = rng.gen_range(0..MATRIX_KATAKANA.len());
            DecryptGlyph::Char(MATRIX_KATAKANA[idx])
        } else if roll < 0.90 && self.with_kanji {
            // 10% - Full-width Kanji (Nika flavor)
            let idx = rng.gen_range(0..KANJI_CHARS.len());
            DecryptGlyph::Kanji(KANJI_CHARS[idx])
        } else if roll < 0.98 {
            // 8% - Nerd references
            let idx = rng.gen_range(0..NERD_REFS.len());
            DecryptGlyph::Text(NERD_REFS[idx])
        } else if self.with_rare_emojis {
            // 2% - Nika mascots ONLY (very rare!)
            const NIKA_MASCOTS: &[&str] = &["🐔", "", "📟", "🛰️", "🔌", "🦋"];
            let idx = rng.gen_range(0..NIKA_MASCOTS.len());
            DecryptGlyph::Emoji(NIKA_MASCOTS[idx])
        } else {
            // Fallback to katakana
            let idx = rng.gen_range(0..MATRIX_KATAKANA.len());
            DecryptGlyph::Char(MATRIX_KATAKANA[idx])
        }
    }

    /// Calculate if a character should be revealed based on position and progress
    fn should_reveal(&self, char_index: usize, total_chars: usize, rng: &mut SmallRng) -> bool {
        if total_chars == 0 {
            return true;
        }

        // Base reveal threshold based on position (left reveals first)
        let position_factor = char_index as f32 / total_chars as f32;

        // Add some randomness for organic feel
        let random_offset: f32 = rng.gen_range(-0.15..0.15);

        // Adjusted threshold
        let threshold = (position_factor + random_offset).clamp(0.0, 1.0);

        // Reveal if progress exceeds threshold
        self.progress * self.speed > threshold
    }

    /// Build the display line with reveal effect
    fn build_line(&self) -> Line<'a> {
        let mut rng = SmallRng::seed_from_u64(self.seed.wrapping_add(self.frame as u64));
        let chars: Vec<char> = self.text.chars().collect();
        let total = chars.len();

        let mut spans = Vec::new();
        let base_color = self.verb.color();

        for (i, &ch) in chars.iter().enumerate() {
            // Per-character seed for consistent chaos pattern
            let char_seed = self.seed.wrapping_add(i as u64 * 31337);
            let mut char_rng = SmallRng::seed_from_u64(char_seed.wrapping_add(self.frame as u64));

            if self.should_reveal(i, total, &mut rng) {
                // Revealed - show actual character
                let style = Style::default().fg(base_color);
                spans.push(Span::styled(ch.to_string(), style));
            } else {
                // Not revealed - show chaos glyph
                let glyph = self.random_glyph(&mut char_rng);

                // Chaos color (dimmer, different hue)
                let chaos_color = self.chaos_color(&mut char_rng);
                // 100% opacity - no DIM modifier for vibrant chaos
                let style = Style::default().fg(chaos_color);

                spans.push(Span::styled(glyph.as_str(), style));
            }
        }

        Line::from(spans)
    }

    /// Get a chaos color (varies by frame for flickering)
    fn chaos_color(&self, rng: &mut SmallRng) -> Color {
        let colors = [
            solarized::CYAN,
            solarized::GREEN,
            solarized::YELLOW,
            solarized::ORANGE,
            solarized::MAGENTA,
            solarized::VIOLET,
            solarized::BLUE,
            solarized::RED,
        ];
        colors[rng.gen_range(0..colors.len())]
    }
}

impl Widget for MatrixDecrypt<'_> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        if area.width == 0 || area.height == 0 || self.text.is_empty() {
            return;
        }

        let line = self.build_line();

        // Render line at top of area
        let x = area.x;
        let y = area.y;

        buf.set_line(x, y, &line, area.width);
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// STREAMING DECRYPT STATE
// ═══════════════════════════════════════════════════════════════════════════════

/// Stateful wrapper for streaming text with decrypt effect
#[derive(Debug, Clone)]
pub struct StreamingDecrypt {
    /// Full text received so far
    text: String,
    /// Current reveal progress per character (0.0 to 1.0)
    char_progress: Vec<f32>,
    /// Birth frame for each character (for initial chaos delay)
    char_birth_frames: Vec<u8>,
    /// Animation frame counter
    frame: u8,
    /// Verb type for theming
    verb: DecryptVerb,
    /// Random seed
    seed: u64,
    /// Auto-advance reveal speed (progress per frame)
    reveal_speed: f32,
    /// Wave factor: how much position affects reveal delay (0.0 to 1.0)
    /// Higher = later characters reveal much slower
    wave_factor: f32,
    /// Initial chaos frames: how long new chars stay in chaos before revealing
    initial_chaos_frames: u8,
    /// Whether fully revealed
    is_complete: bool,
}

impl Default for StreamingDecrypt {
    fn default() -> Self {
        Self {
            text: String::new(),
            char_progress: Vec::new(),
            char_birth_frames: Vec::new(),
            frame: 0,
            verb: DecryptVerb::default(),
            seed: 42,
            reveal_speed: 0.025, // Slower reveal (~40 frames = 667ms at 60fps)
            wave_factor: 0.15,   // Cascade effect - later chars reveal slower
            initial_chaos_frames: 8, // ~130ms of visible chaos before reveal
            is_complete: false,
        }
    }
}

impl StreamingDecrypt {
    pub fn new() -> Self {
        Self::default()
    }

    /// Set verb type for theming
    pub fn with_verb(mut self, verb: DecryptVerb) -> Self {
        self.verb = verb;
        self
    }

    /// Set reveal speed (progress per frame, 0.0 to 1.0)
    pub fn with_reveal_speed(mut self, speed: f32) -> Self {
        self.reveal_speed = speed.clamp(0.005, 1.0);
        self
    }

    /// Set wave factor (cascade effect strength, 0.0 to 1.0)
    /// Higher values = later characters reveal much slower
    pub fn with_wave_factor(mut self, factor: f32) -> Self {
        self.wave_factor = factor.clamp(0.0, 0.5);
        self
    }

    /// Set initial chaos frames (delay before revealing starts)
    pub fn with_initial_chaos(mut self, frames: u8) -> Self {
        self.initial_chaos_frames = frames;
        self
    }

    /// Set random seed
    pub fn with_seed(mut self, seed: u64) -> Self {
        self.seed = seed;
        self
    }

    /// Append new streaming text (from LLM token)
    pub fn push_text(&mut self, new_text: &str) {
        for ch in new_text.chars() {
            self.text.push(ch);
            // New characters start at 0% revealed
            self.char_progress.push(0.0);
            // Record birth frame for initial chaos delay
            self.char_birth_frames.push(self.frame);
        }
    }

    /// Advance animation frame and reveal progress
    /// Implements cascade effect and initial chaos delay
    pub fn tick(&mut self) {
        self.frame = self.frame.wrapping_add(1);

        let total_chars = self.char_progress.len();
        if total_chars == 0 {
            return;
        }

        // Advance reveal progress for each character with cascade effect
        let mut all_revealed = true;
        for (i, progress) in self.char_progress.iter_mut().enumerate() {
            if *progress >= 1.0 {
                continue;
            }

            // Check initial chaos delay
            let birth_frame = self.char_birth_frames.get(i).copied().unwrap_or(0);
            let frames_alive = self.frame.wrapping_sub(birth_frame);
            if frames_alive < self.initial_chaos_frames {
                // Stay in chaos - don't advance progress
                all_revealed = false;
                continue;
            }

            // Calculate cascade effect: later chars have reduced speed
            let position_factor = i as f32 / total_chars.max(1) as f32;
            let speed_multiplier = 1.0 - (position_factor * self.wave_factor);
            let effective_speed = self.reveal_speed * speed_multiplier;

            *progress = (*progress + effective_speed).min(1.0);
            if *progress < 1.0 {
                all_revealed = false;
            }
        }

        self.is_complete = all_revealed && !self.text.is_empty();
    }

    /// Force complete reveal (skip animation)
    pub fn reveal_all(&mut self) {
        for progress in &mut self.char_progress {
            *progress = 1.0;
        }
        self.is_complete = true;
    }

    /// Check if fully revealed
    pub fn is_complete(&self) -> bool {
        self.is_complete
    }

    /// Get current text
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Get current verb
    pub fn verb(&self) -> DecryptVerb {
        self.verb
    }

    /// Clear state for new message
    pub fn clear(&mut self) {
        self.text.clear();
        self.char_progress.clear();
        self.char_birth_frames.clear();
        self.frame = 0;
        self.is_complete = false;
    }

    /// Calculate overall progress (0.0 to 1.0)
    pub fn overall_progress(&self) -> f32 {
        if self.char_progress.is_empty() {
            return 1.0;
        }
        self.char_progress.iter().sum::<f32>() / self.char_progress.len() as f32
    }

    /// Create a widget for rendering current state
    pub fn widget(&self) -> MatrixDecrypt<'_> {
        MatrixDecrypt::new(&self.text)
            .progress(self.overall_progress())
            .frame(self.frame)
            .verb(self.verb)
            .seed(self.seed)
    }

    /// Render directly to buffer
    pub fn render(&self, area: Rect, buf: &mut Buffer) {
        self.widget().render(area, buf);
    }

    /// Build Line for use in List context (ChatView)
    /// Returns a Line with the matrix decrypt effect applied
    pub fn build_line(&self) -> Line<'static> {
        use rand::rngs::SmallRng;
        use rand::SeedableRng;

        let chars: Vec<char> = self.text.chars().collect();

        let mut spans = Vec::new();
        let base_color = self.verb.color();

        for (i, &ch) in chars.iter().enumerate() {
            // Per-character seed for consistent chaos pattern
            let char_seed = self.seed.wrapping_add(i as u64 * 31337);
            let mut char_rng = SmallRng::seed_from_u64(char_seed.wrapping_add(self.frame as u64));

            // Get progress for this character
            let progress = self.char_progress.get(i).copied().unwrap_or(1.0);

            if progress >= 1.0 {
                // Revealed - show actual character
                let style = Style::default().fg(base_color);
                spans.push(Span::styled(ch.to_string(), style));
            } else {
                // Not revealed - show chaos glyph
                let glyph = Self::random_glyph_static(&mut char_rng);

                // Chaos color (varies by frame for flickering)
                let colors = [
                    solarized::CYAN,
                    solarized::GREEN,
                    solarized::YELLOW,
                    solarized::ORANGE,
                    solarized::MAGENTA,
                    solarized::VIOLET,
                    solarized::BLUE,
                    solarized::RED,
                ];
                let chaos_color = colors[char_rng.gen_range(0..colors.len())];
                // 100% opacity - no DIM modifier for vibrant chaos
                let style = Style::default().fg(chaos_color);

                spans.push(Span::styled(glyph, style));
            }
        }

        Line::from(spans)
    }

    /// Build multiple Lines for multi-line text (splits on newlines)
    pub fn build_lines(&self) -> Vec<Line<'static>> {
        // Split text into lines and build each one
        let lines_text: Vec<&str> = self.text.lines().collect();
        let mut result = Vec::new();
        let mut char_offset = 0;

        for line_text in lines_text {
            let line_len = line_text.chars().count();

            // Build line with the subset of char_progress for this line
            let line = self.build_line_segment(line_text, char_offset);
            result.push(line);

            char_offset += line_len + 1; // +1 for newline
        }

        result
    }

    /// Build multiple Lines with word wrapping
    ///
    /// Wraps text at the specified max_width to prevent overflow.
    /// Maintains character progress indices across wrapped lines.
    pub fn build_lines_wrapped(&self, max_width: usize) -> Vec<Line<'static>> {
        if max_width == 0 {
            return self.build_lines();
        }

        let mut result = Vec::new();
        let mut global_char_offset = 0;

        // Process each original line (split by \n)
        for original_line in self.text.lines() {
            let original_len = original_line.chars().count();

            // Wrap this line to max_width
            let wrapped = Self::wrap_line(original_line, max_width);

            let mut line_char_offset = global_char_offset;
            for wrapped_segment in wrapped {
                let segment_len = wrapped_segment.chars().count();
                let line = self.build_line_segment(&wrapped_segment, line_char_offset);
                result.push(line);
                line_char_offset += segment_len;
            }

            global_char_offset += original_len + 1; // +1 for newline
        }

        if result.is_empty() {
            result.push(Line::from(vec![]));
        }

        result
    }

    /// Simple word wrapping helper
    fn wrap_line(text: &str, max_width: usize) -> Vec<String> {
        if max_width == 0 || text.is_empty() {
            return vec![text.to_string()];
        }

        let mut result = Vec::new();
        let mut current_line = String::new();
        let mut current_width = 0;

        for word in text.split_whitespace() {
            let word_width = word.chars().count();

            if word_width > max_width {
                // Word is too long, must split it
                if !current_line.is_empty() {
                    result.push(current_line);
                    current_line = String::new();
                    current_width = 0;
                }
                // Split long word into chunks
                let mut chars = word.chars().peekable();
                while chars.peek().is_some() {
                    let chunk: String = chars.by_ref().take(max_width).collect();
                    if !chunk.is_empty() {
                        result.push(chunk);
                    }
                }
            } else if current_width == 0 {
                // First word on line
                current_line = word.to_string();
                current_width = word_width;
            } else if current_width + 1 + word_width <= max_width {
                // Word fits with space
                current_line.push(' ');
                current_line.push_str(word);
                current_width += 1 + word_width;
            } else {
                // Word doesn't fit, start new line
                result.push(current_line);
                current_line = word.to_string();
                current_width = word_width;
            }
        }

        if !current_line.is_empty() {
            result.push(current_line);
        }

        if result.is_empty() {
            result.push(String::new());
        }

        result
    }

    /// Build a Line for a specific segment of text
    fn build_line_segment(&self, text: &str, char_offset: usize) -> Line<'static> {
        use rand::rngs::SmallRng;
        use rand::SeedableRng;

        let chars: Vec<char> = text.chars().collect();

        let mut spans = Vec::new();
        let base_color = self.verb.color();

        for (i, &ch) in chars.iter().enumerate() {
            let global_idx = char_offset + i;
            let char_seed = self.seed.wrapping_add(global_idx as u64 * 31337);
            let mut char_rng = SmallRng::seed_from_u64(char_seed.wrapping_add(self.frame as u64));

            // Get progress for this character
            let progress = self.char_progress.get(global_idx).copied().unwrap_or(1.0);

            if progress >= 1.0 {
                let style = Style::default().fg(base_color);
                spans.push(Span::styled(ch.to_string(), style));
            } else {
                let glyph = Self::random_glyph_static(&mut char_rng);
                let colors = [
                    solarized::CYAN,
                    solarized::GREEN,
                    solarized::YELLOW,
                    solarized::ORANGE,
                    solarized::MAGENTA,
                    solarized::VIOLET,
                    solarized::BLUE,
                    solarized::RED,
                ];
                let chaos_color = colors[char_rng.gen_range(0..colors.len())];
                // 100% opacity - no DIM modifier for vibrant chaos
                let style = Style::default().fg(chaos_color);
                spans.push(Span::styled(glyph, style));
            }
        }

        Line::from(spans)
    }

    /// Generate a random chaos glyph (static version for build_line)
    /// TRUE Matrix-style - katakana-dominant, Nika mascots only
    fn random_glyph_static(rng: &mut SmallRng) -> String {
        let roll: f32 = rng.gen();

        if roll < 0.35 {
            // 35% - ASCII characters (monospace)
            let chars: &[char] = &[
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
                'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
                '6', '7', '8', '9', '_', '<', '>', '[', ']', '{', '}', '/', '*', '#', '@', '^',
            ];
            let idx = rng.gen_range(0..chars.len());
            chars[idx].to_string()
        } else if roll < 0.80 {
            // 45% - Half-width katakana (THE Matrix look!)
            let idx = rng.gen_range(0..MATRIX_KATAKANA.len());
            MATRIX_KATAKANA[idx].to_string()
        } else if roll < 0.90 {
            // 10% - Full-width Kanji (Nika flavor)
            let idx = rng.gen_range(0..KANJI_CHARS.len());
            KANJI_CHARS[idx].to_string()
        } else if roll < 0.98 {
            // 8% - Nerd references
            let idx = rng.gen_range(0..NERD_REFS.len());
            NERD_REFS[idx].to_string()
        } else {
            // 2% - Nika mascots ONLY (very rare!)
            const NIKA_MASCOTS: &[&str] = &["🐔", "", "📟", "🛰️", "🔌", "🦋"];
            let idx = rng.gen_range(0..NIKA_MASCOTS.len());
            NIKA_MASCOTS[idx].to_string()
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// MULTI-LINE STREAMING DECRYPT
// ═══════════════════════════════════════════════════════════════════════════════

/// Multi-line streaming decrypt for longer outputs
#[derive(Clone, Default)]
pub struct MultiLineDecrypt {
    /// Lines of text
    lines: Vec<StreamingDecrypt>,
    /// Current line being written to
    current_line: usize,
    /// Verb type for theming
    verb: DecryptVerb,
    /// Reveal speed
    reveal_speed: f32,
    /// Seed
    seed: u64,
}

impl MultiLineDecrypt {
    pub fn new() -> Self {
        Self {
            lines: vec![StreamingDecrypt::default()],
            current_line: 0,
            verb: DecryptVerb::default(),
            reveal_speed: 0.05,
            seed: 42,
        }
    }

    pub fn with_verb(mut self, verb: DecryptVerb) -> Self {
        self.verb = verb;
        for line in &mut self.lines {
            line.verb = verb;
        }
        self
    }

    pub fn with_reveal_speed(mut self, speed: f32) -> Self {
        self.reveal_speed = speed;
        for line in &mut self.lines {
            line.reveal_speed = speed;
        }
        self
    }

    /// Push streaming text, handling newlines
    pub fn push_text(&mut self, text: &str) {
        for ch in text.chars() {
            if ch == '\n' {
                // Start new line
                self.current_line += 1;
                let new_line = StreamingDecrypt::new()
                    .with_verb(self.verb)
                    .with_reveal_speed(self.reveal_speed)
                    .with_seed(self.seed.wrapping_add(self.current_line as u64));
                self.lines.push(new_line);
            } else {
                // Add to current line
                if self.current_line >= self.lines.len() {
                    let new_line = StreamingDecrypt::new()
                        .with_verb(self.verb)
                        .with_reveal_speed(self.reveal_speed)
                        .with_seed(self.seed.wrapping_add(self.current_line as u64));
                    self.lines.push(new_line);
                }
                self.lines[self.current_line].push_text(&ch.to_string());
            }
        }
    }

    /// Advance animation for all lines
    pub fn tick(&mut self) {
        for line in &mut self.lines {
            line.tick();
        }
    }

    /// Reveal all lines immediately
    pub fn reveal_all(&mut self) {
        for line in &mut self.lines {
            line.reveal_all();
        }
    }

    /// Check if all lines are revealed
    pub fn is_complete(&self) -> bool {
        self.lines.iter().all(|l| l.is_complete())
    }

    /// Clear all content
    pub fn clear(&mut self) {
        self.lines.clear();
        self.lines.push(
            StreamingDecrypt::new()
                .with_verb(self.verb)
                .with_reveal_speed(self.reveal_speed)
                .with_seed(self.seed),
        );
        self.current_line = 0;
    }

    /// Get line count
    pub fn line_count(&self) -> usize {
        self.lines.len()
    }

    /// Render to buffer with scroll offset
    pub fn render(&self, area: Rect, buf: &mut Buffer, scroll_offset: usize) {
        for (i, line) in self.lines.iter().enumerate().skip(scroll_offset) {
            let y = area.y + (i - scroll_offset) as u16;
            if y >= area.y + area.height {
                break;
            }
            let line_area = Rect::new(area.x, y, area.width, 1);
            line.render(line_area, buf);
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TESTS
// ═══════════════════════════════════════════════════════════════════════════════

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::backend::TestBackend;
    use ratatui::Terminal;

    #[test]
    fn test_decrypt_verb_colors() {
        assert_eq!(DecryptVerb::Infer.color(), solarized::VIOLET);
        assert_eq!(DecryptVerb::Exec.color(), solarized::GREEN);
        assert_eq!(DecryptVerb::Fetch.color(), solarized::CYAN);
        assert_eq!(DecryptVerb::Invoke.color(), solarized::BLUE);
        assert_eq!(DecryptVerb::Agent.color(), solarized::YELLOW);
    }

    #[test]
    fn test_decrypt_verb_emojis() {
        assert!(!DecryptVerb::Infer.emojis().is_empty());
        assert!(!DecryptVerb::Agent.emojis().is_empty());
        assert!(DecryptVerb::Infer.emojis().contains(&"🪐"));
        assert!(DecryptVerb::Agent.emojis().contains(&"🐔"));
    }

    #[test]
    fn test_decrypt_verb_icons() {
        assert_eq!(DecryptVerb::Infer.icon(), "");
        assert_eq!(DecryptVerb::Agent.icon(), "🐔");
    }

    #[test]
    fn test_matrix_decrypt_renders() {
        let backend = TestBackend::new(40, 5);
        let mut terminal = Terminal::new(backend).unwrap();

        terminal
            .draw(|frame| {
                let decrypt = MatrixDecrypt::new("Hello, World!")
                    .progress(0.5)
                    .frame(0)
                    .verb(DecryptVerb::Infer);
                frame.render_widget(decrypt, frame.area());
            })
            .unwrap();
    }

    #[test]
    fn test_matrix_decrypt_full_reveal() {
        let backend = TestBackend::new(40, 5);
        let mut terminal = Terminal::new(backend).unwrap();

        terminal
            .draw(|frame| {
                let decrypt = MatrixDecrypt::new("Fully revealed")
                    .progress(1.0)
                    .verb(DecryptVerb::Exec);
                frame.render_widget(decrypt, frame.area());
            })
            .unwrap();
    }

    #[test]
    fn test_matrix_decrypt_chaos() {
        let backend = TestBackend::new(40, 5);
        let mut terminal = Terminal::new(backend).unwrap();

        terminal
            .draw(|frame| {
                let decrypt = MatrixDecrypt::new("All chaos")
                    .progress(0.0)
                    .verb(DecryptVerb::Fetch);
                frame.render_widget(decrypt, frame.area());
            })
            .unwrap();
    }

    #[test]
    fn test_streaming_decrypt_push() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Hello");
        assert_eq!(stream.text(), "Hello");
        assert_eq!(stream.char_progress.len(), 5);

        stream.push_text(" World");
        assert_eq!(stream.text(), "Hello World");
        assert_eq!(stream.char_progress.len(), 11);
    }

    #[test]
    fn test_streaming_decrypt_tick() {
        // Need to skip initial chaos frames before reveal starts
        let mut stream = StreamingDecrypt::new()
            .with_reveal_speed(0.5)
            .with_initial_chaos(0) // Disable chaos delay for this test
            .with_wave_factor(0.0); // Disable cascade for predictable test
        stream.push_text("Hi");

        assert!(!stream.is_complete());

        stream.tick();
        assert_eq!(stream.char_progress[0], 0.5);

        stream.tick();
        assert_eq!(stream.char_progress[0], 1.0);
        assert!(stream.is_complete());
    }

    #[test]
    fn test_streaming_decrypt_initial_chaos_delay() {
        let mut stream = StreamingDecrypt::new()
            .with_reveal_speed(1.0) // Would reveal instantly without chaos delay
            .with_initial_chaos(5) // 5 frames of chaos (frames 1-4, then reveal at 5)
            .with_wave_factor(0.0);
        stream.push_text("A");

        // First 4 ticks should NOT advance progress (chaos phase)
        // Note: initial_chaos_frames=5 means frames_alive must be >= 5 to reveal
        // tick increments frame first, so: tick 1->frame=1, tick 4->frame=4 still in chaos
        for i in 0..4 {
            stream.tick();
            assert_eq!(
                stream.char_progress[0],
                0.0,
                "Should stay at 0 during chaos at tick {}",
                i + 1
            );
        }

        // 5th tick: frame=5, frames_alive=5, 5 >= 5 so reveal starts
        stream.tick();
        assert_eq!(stream.char_progress[0], 1.0, "Should reveal after chaos");
        assert!(stream.is_complete());
    }

    #[test]
    fn test_streaming_decrypt_cascade_effect() {
        let mut stream = StreamingDecrypt::new()
            .with_reveal_speed(0.1)
            .with_initial_chaos(0) // No delay
            .with_wave_factor(0.5); // Strong cascade
        stream.push_text("ABC");

        // After one tick, first char should have more progress than last
        stream.tick();
        let first_progress = stream.char_progress[0];
        let last_progress = stream.char_progress[2];
        assert!(
            first_progress > last_progress,
            "First char ({}) should reveal faster than last ({})",
            first_progress,
            last_progress
        );
    }

    #[test]
    fn test_streaming_decrypt_reveal_all() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Test text");

        assert!(!stream.is_complete());

        stream.reveal_all();
        assert!(stream.is_complete());
        assert!(stream.char_progress.iter().all(|&p| p == 1.0));
    }

    #[test]
    fn test_streaming_decrypt_clear() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Some text");
        stream.tick();

        stream.clear();
        assert!(stream.text().is_empty());
        assert!(stream.char_progress.is_empty());
        assert!(!stream.is_complete());
    }

    #[test]
    fn test_streaming_decrypt_verb() {
        let stream = StreamingDecrypt::new().with_verb(DecryptVerb::Agent);
        assert_eq!(stream.verb, DecryptVerb::Agent);
    }

    #[test]
    fn test_multi_line_decrypt() {
        let mut multi = MultiLineDecrypt::new();
        multi.push_text("Line 1\nLine 2\nLine 3");

        assert_eq!(multi.line_count(), 3);
        assert_eq!(multi.lines[0].text(), "Line 1");
        assert_eq!(multi.lines[1].text(), "Line 2");
        assert_eq!(multi.lines[2].text(), "Line 3");
    }

    #[test]
    fn test_multi_line_tick() {
        let mut multi = MultiLineDecrypt::new().with_reveal_speed(1.0); // Fast reveal
        multi.push_text("A\nB");

        // Need multiple ticks due to initial chaos delay (default 8 frames)
        // Tick enough times to pass chaos delay + reveal
        for _ in 0..15 {
            multi.tick();
        }
        assert!(multi.is_complete());
    }

    #[test]
    fn test_multi_line_reveal_all() {
        let mut multi = MultiLineDecrypt::new();
        multi.push_text("Line 1\nLine 2");

        assert!(!multi.is_complete());
        multi.reveal_all();
        assert!(multi.is_complete());
    }

    #[test]
    fn test_kanji_chars_included() {
        assert!(KANJI_CHARS.contains(&''));
        assert!(KANJI_CHARS.contains(&''));
        assert!(KANJI_CHARS.contains(&''));
        assert!(KANJI_CHARS.contains(&''));
        assert!(KANJI_CHARS.contains(&''));
        assert!(KANJI_CHARS.contains(&''));
    }

    #[test]
    fn test_nerd_refs_included() {
        assert!(NERD_REFS.contains(&"42"));
        assert!(NERD_REFS.contains(&"NULL"));
        assert!(NERD_REFS.contains(&"sudo"));
    }

    #[test]
    fn test_cycling_words_included() {
        assert!(CYCLING_WORDS.contains(&"Nika"));
        assert!(CYCLING_WORDS.contains(&"Freedom"));
        assert!(CYCLING_WORDS.contains(&"自由"));
    }

    #[test]
    fn test_emoji_themes_non_empty() {
        assert!(!PIRATE_EMOJIS.is_empty());
        assert!(!COSMIC_EMOJIS.is_empty());
        assert!(!UNICORN_EMOJIS.is_empty());
        assert!(!ANIMAL_EMOJIS.is_empty());
        assert!(!FOOD_EMOJIS.is_empty());
        assert!(!SPECIAL_EMOJIS.is_empty());
        assert!(!TECH_EMOJIS.is_empty());
        assert!(!NATURE_EMOJIS.is_empty());
    }

    #[test]
    fn test_decrypt_glyph_width() {
        assert_eq!(DecryptGlyph::Char('a').display_width(), 1);
        assert_eq!(DecryptGlyph::Emoji("🐔").display_width(), 2);
        assert_eq!(DecryptGlyph::Kanji('').display_width(), 2);
        assert_eq!(DecryptGlyph::Text("42").display_width(), 2);
    }

    #[test]
    fn test_overall_progress() {
        let mut stream = StreamingDecrypt::new();
        assert_eq!(stream.overall_progress(), 1.0); // Empty = complete

        stream.push_text("AB");
        assert_eq!(stream.overall_progress(), 0.0);

        stream.char_progress[0] = 1.0;
        assert_eq!(stream.overall_progress(), 0.5);

        stream.char_progress[1] = 1.0;
        assert_eq!(stream.overall_progress(), 1.0);
    }

    #[test]
    fn test_build_line_returns_line() {
        let mut stream = StreamingDecrypt::new().with_verb(DecryptVerb::Agent);
        stream.push_text("Hello");
        stream.reveal_all(); // Fully reveal

        let line = stream.build_line();
        // Should have 5 spans (one per character)
        assert_eq!(line.spans.len(), 5);
    }

    #[test]
    fn test_build_lines_multiline() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Line 1\nLine 2\nLine 3");
        stream.reveal_all();

        let lines = stream.build_lines();
        assert_eq!(lines.len(), 3);
    }

    #[test]
    fn test_build_line_chaos_when_not_revealed() {
        let stream = StreamingDecrypt::new().with_verb(DecryptVerb::Fetch);
        // Don't push any text or tick - should be empty
        let line = stream.build_line();
        assert!(line.spans.is_empty());
    }

    #[test]
    fn test_build_lines_wrapped_short_text() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Hello World");
        stream.reveal_all();

        // Width is large enough - no wrapping needed
        let lines = stream.build_lines_wrapped(50);
        assert_eq!(lines.len(), 1);
    }

    #[test]
    fn test_build_lines_wrapped_wraps_long_text() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Hello World Test");
        stream.reveal_all();

        // Width of 10 should wrap "Hello World Test" into multiple lines
        let lines = stream.build_lines_wrapped(10);
        assert!(lines.len() >= 2);
    }

    #[test]
    fn test_build_lines_wrapped_zero_width_fallback() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("Hello\nWorld");
        stream.reveal_all();

        // Width 0 should fall back to build_lines
        let lines = stream.build_lines_wrapped(0);
        assert_eq!(lines.len(), 2);
    }

    #[test]
    fn test_build_lines_wrapped_multiline_with_wrap() {
        let mut stream = StreamingDecrypt::new();
        stream.push_text("This is a long first line\nShort second");
        stream.reveal_all();

        // Width of 15 should wrap the first line
        let lines = stream.build_lines_wrapped(15);
        assert!(lines.len() >= 3); // First line wraps + second line
    }

    #[test]
    fn test_wrap_line_simple() {
        let result = StreamingDecrypt::wrap_line("Hello World", 20);
        assert_eq!(result, vec!["Hello World"]);
    }

    #[test]
    fn test_wrap_line_breaks_at_space() {
        let result = StreamingDecrypt::wrap_line("Hello World Test", 10);
        assert_eq!(result.len(), 2);
        assert_eq!(result[0], "Hello");
        assert_eq!(result[1], "World Test");
    }

    #[test]
    fn test_wrap_line_long_word_split() {
        let result = StreamingDecrypt::wrap_line("Supercalifragilistic", 5);
        assert!(result.len() >= 4); // Should split the long word
    }

    #[test]
    fn test_wrap_line_empty() {
        let result = StreamingDecrypt::wrap_line("", 10);
        assert_eq!(result, vec![""]);
    }
}