repartee 0.9.1

A modern terminal IRC client built with Ratatui and Tokio
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
mod backlog;
mod dcc;
mod image;
mod input;
mod irc;
mod maintenance;
mod mentions;
mod scripting;
mod session;
mod shell;
mod web;
mod who;

use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Instant;

use chrono::Utc;
use color_eyre::eyre::Result;
use crossterm::event::{self, Event};
use ratatui::layout::Rect;
use tokio::sync::mpsc;
use tokio::time::{Duration, interval};

use crate::config::{self, AppConfig};
use crate::constants;
use crate::irc::{IrcEvent, IrcHandle};
use crate::state::AppState;
use crate::state::buffer::{
    ActivityLevel, Buffer, BufferType, Message, MessageType, make_buffer_id,
};
use crate::state::connection::{Connection, ConnectionStatus};
use crate::theme::{self, ThemeFile};
use crate::ui;
use crate::ui::layout::UiRegions;

use ratatui_image::picker::ProtocolType;

/// Maximum number of lines queued from a multiline paste.
const MAX_PASTE_LINES: usize = 1000;

/// Maximum alias recursion depth (prevents infinite loops from circular aliases).
const MAX_ALIAS_DEPTH: u8 = 10;

/// Detect the outer terminal via tmux client queries.
///
/// Queries `#{client_termtype}` and `#{client_termname}` to identify the real
/// terminal hosting the tmux session (e.g. iTerm2, Ghostty, Kitty). Falls back
/// to Alacritty heuristic (generic xterm + empty termname).
fn detect_via_tmux() -> Option<(&'static str, Option<ProtocolType>, String)> {
    let termtype = tmux_query_raw("#{client_termtype}");
    let termname = tmux_query_raw("#{client_termname}");
    tracing::debug!(
        client_termtype = ?termtype,
        client_termname = ?termname,
        "tmux outer terminal queries"
    );

    if let Some(ref tt) = termtype
        && let Some((name, proto)) = match_terminal(tt)
    {
        return Some((name, Some(proto), format!("tmux:client_termtype={tt}")));
    }
    if let Some(ref tn) = termname
        && let Some((name, proto)) = match_terminal(tn)
    {
        return Some((name, Some(proto), format!("tmux:client_termname={tn}")));
    }

    // Alacritty: generic termtype like "xterm-256color" + empty termname.
    // No image protocol support — use halfblocks.
    let tt_generic = termtype.as_deref().unwrap_or("").starts_with("xterm");
    let tn_empty = termname.as_deref().unwrap_or("").is_empty();
    if tt_generic && tn_empty {
        return Some((
            "alacritty",
            Some(ProtocolType::Halfblocks),
            "tmux:generic-xterm+empty-termname".into(),
        ));
    }

    None
}

fn detect_outer_terminal(
    in_tmux: bool,
    env_override: Option<&std::collections::HashMap<String, String>>,
) -> (&'static str, Option<ProtocolType>, String) {
    let get_env = |key: &str| -> Option<String> {
        env_override.map_or_else(
            || std::env::var(key).ok().filter(|s| !s.is_empty()),
            |vars| vars.get(key).cloned(),
        )
    };

    tracing::debug!(
        TMUX = ?get_env("TMUX"),
        TERM = ?get_env("TERM"),
        TERM_PROGRAM = ?get_env("TERM_PROGRAM"),
        TERM_PROGRAM_VERSION = ?get_env("TERM_PROGRAM_VERSION"),
        LC_TERMINAL = ?get_env("LC_TERMINAL"),
        LC_TERMINAL_VERSION = ?get_env("LC_TERMINAL_VERSION"),
        ITERM_SESSION_ID = ?get_env("ITERM_SESSION_ID"),
        KITTY_PID = ?get_env("KITTY_PID"),
        GHOSTTY_RESOURCES_DIR = ?get_env("GHOSTTY_RESOURCES_DIR"),
        WT_SESSION = ?get_env("WT_SESSION"),
        COLORTERM = ?get_env("COLORTERM"),
        in_tmux,
        env_from_shim = env_override.is_some(),
        "outer terminal env vars"
    );

    if in_tmux && let Some(result) = detect_via_tmux() {
        return result;
    }

    let lc_terminal = get_env("LC_TERMINAL").unwrap_or_default();

    if !lc_terminal.is_empty() {
        if lc_terminal.eq_ignore_ascii_case("iterm2")
            || lc_terminal.to_ascii_lowercase().contains("iterm")
        {
            return (
                "iterm2",
                Some(ProtocolType::Iterm2),
                format!("env:LC_TERMINAL={lc_terminal}"),
            );
        }
        if lc_terminal.eq_ignore_ascii_case("ghostty") {
            return (
                "ghostty",
                Some(ProtocolType::Kitty),
                format!("env:LC_TERMINAL={lc_terminal}"),
            );
        }
        if lc_terminal.eq_ignore_ascii_case("subterm") {
            return (
                "subterm",
                Some(ProtocolType::Kitty),
                format!("env:LC_TERMINAL={lc_terminal}"),
            );
        }
    }

    if get_env("ITERM_SESSION_ID").is_some() {
        return (
            "iterm2",
            Some(ProtocolType::Iterm2),
            "env:ITERM_SESSION_ID".into(),
        );
    }

    if let Some(grd) = get_env("GHOSTTY_RESOURCES_DIR")
        && grd.len() > 1
    {
        return (
            "ghostty",
            Some(ProtocolType::Kitty),
            format!("env:GHOSTTY_RESOURCES_DIR={grd}"),
        );
    }

    if get_env("KITTY_PID").is_some() {
        return ("kitty", Some(ProtocolType::Kitty), "env:KITTY_PID".into());
    }
    if get_env("WEZTERM_EXECUTABLE").is_some() {
        return (
            "wezterm",
            Some(ProtocolType::Iterm2),
            "env:WEZTERM_EXECUTABLE".into(),
        );
    }
    if get_env("WT_SESSION").is_some() {
        return (
            "windows-terminal",
            Some(ProtocolType::Sixel),
            "env:WT_SESSION".into(),
        );
    }

    if !in_tmux {
        let tp = get_env("TERM_PROGRAM").unwrap_or_default();
        if !tp.is_empty()
            && tp != "tmux"
            && let Some((name, proto)) = match_terminal(&tp)
        {
            return (name, Some(proto), format!("env:TERM_PROGRAM={tp}"));
        }
    }

    let term = get_env("TERM").unwrap_or_default();
    if let Some((name, proto)) = match_terminal(&term) {
        return (name, Some(proto), format!("env:TERM={term}"));
    }

    ("unknown", None, "auto:unknown".into())
}

/// Match a terminal identifier string to a terminal name and image protocol.
fn match_terminal(name: &str) -> Option<(&'static str, ProtocolType)> {
    let contains = |needle: &str| -> bool {
        name.as_bytes()
            .windows(needle.len())
            .any(|w| w.eq_ignore_ascii_case(needle.as_bytes()))
    };

    if contains("iterm") {
        return Some(("iterm2", ProtocolType::Iterm2));
    }
    if contains("ghostty") {
        return Some(("ghostty", ProtocolType::Kitty));
    }
    if contains("kitty") {
        return Some(("kitty", ProtocolType::Kitty));
    }
    if contains("subterm") {
        return Some(("subterm", ProtocolType::Kitty));
    }
    if contains("wezterm") {
        return Some(("wezterm", ProtocolType::Iterm2));
    }
    if contains("rio") {
        return Some(("rio", ProtocolType::Kitty));
    }
    if contains("foot") {
        return Some(("foot", ProtocolType::Sixel));
    }
    if contains("contour") {
        return Some(("contour", ProtocolType::Sixel));
    }
    if contains("konsole") {
        return Some(("konsole", ProtocolType::Sixel));
    }
    if contains("mintty") {
        return Some(("mintty", ProtocolType::Sixel));
    }
    if contains("mlterm") {
        return Some(("mlterm", ProtocolType::Sixel));
    }

    None
}

/// Resolve the image protocol to use.
fn resolve_image_protocol(
    config_protocol: &str,
    picker: &ratatui_image::picker::Picker,
    outer_terminal: &str,
    outer_proto: Option<ProtocolType>,
    outer_source: String,
    env_is_authoritative: bool,
) -> (Option<ProtocolType>, String) {
    match config_protocol {
        "kitty" => return (Some(ProtocolType::Kitty), "config:kitty".into()),
        "iterm2" => return (Some(ProtocolType::Iterm2), "config:iterm2".into()),
        "sixel" => return (Some(ProtocolType::Sixel), "config:sixel".into()),
        "halfblocks" => return (Some(ProtocolType::Halfblocks), "config:halfblocks".into()),
        _ => {}
    }

    if (outer_source.starts_with("tmux:") || env_is_authoritative)
        && let Some(proto) = outer_proto
    {
        return (Some(proto), outer_source);
    }

    if outer_terminal == "iterm2" {
        return (
            Some(ProtocolType::Iterm2),
            format!("iterm2-override:{outer_source}"),
        );
    }

    if picker.protocol_type() != ProtocolType::Halfblocks {
        return (None, format!("io-query:{:?}", picker.protocol_type()));
    }

    if let Some(proto) = outer_proto {
        return (Some(proto), outer_source);
    }

    (None, "auto:halfblocks".into())
}

/// Run a tmux `display-message -p` query and return the trimmed stdout.
pub fn tmux_query_raw(format_str: &str) -> Option<String> {
    let output = std::process::Command::new("tmux")
        .args(["display-message", "-p", format_str])
        .stdin(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .output()
        .ok()?;

    let name = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if name.is_empty() { None } else { Some(name) }
}

/// Expand an alias template with positional args and context variables.
fn expand_alias_template(
    template: &str,
    args: &[String],
    channel: &str,
    nick: &str,
    server: &str,
) -> String {
    let mut body = template.to_string();

    if !body.contains('$') {
        body.push_str(" $*");
    }

    body = body.replace("${C}", channel);
    body = body.replace("$C", channel);
    body = body.replace("${N}", nick);
    body = body.replace("$N", nick);
    body = body.replace("${S}", server);
    body = body.replace("$S", server);
    body = body.replace("${T}", channel);
    body = body.replace("$T", channel);

    for i in (0..=9).rev() {
        let range_var = format!("${i}-");
        if body.contains(&range_var) {
            let val = if i < args.len() {
                args[i..].join(" ")
            } else {
                String::new()
            };
            body = body.replace(&range_var, &val);
        }
    }

    let all_args = args.join(" ");
    body = body.replace("$*", &all_args);
    body = body.replace("$-", &all_args);

    for i in (0..=9).rev() {
        let var = format!("${i}");
        let val = args.get(i).map_or("", String::as_str);
        body = body.replace(&var, val);
    }

    body.trim().to_string()
}

#[expect(
    clippy::struct_excessive_bools,
    reason = "App is the root state container"
)]
pub struct App {
    pub state: AppState,
    pub config: AppConfig,
    pub theme: ThemeFile,
    pub input: ui::input::InputState,
    pub should_quit: bool,
    pub(crate) script_snapshot_dirty: bool,
    pub splash_visible: usize,
    pub splash_done: bool,
    pub scroll_offset: usize,
    pub ui_regions: Option<UiRegions>,
    pub irc_handles: HashMap<String, IrcHandle>,
    pub(crate) forwarder_handles: HashMap<String, tokio::task::JoinHandle<()>>,
    pub irc_tx: mpsc::Sender<IrcEvent>,
    pub(crate) irc_rx: mpsc::Receiver<IrcEvent>,
    pub(crate) last_esc_time: Option<Instant>,
    pub buffer_list_scroll: usize,
    pub buffer_list_total: usize,
    pub nick_list_scroll: usize,
    pub nick_list_total: usize,
    pub lag_pings: HashMap<String, Instant>,
    pub(crate) batch_trackers: HashMap<String, crate::irc::batch::BatchTracker>,
    pub storage: Option<crate::storage::Storage>,
    pub(crate) last_event_purge: Instant,
    pub(crate) last_mention_purge: Instant,
    pub quit_message: Option<String>,
    pub image_preview: crate::image_preview::PreviewStatus,
    pub image_clear_rect: Option<Rect>,
    pub(crate) preview_rx: mpsc::Receiver<crate::image_preview::ImagePreviewEvent>,
    pub(crate) preview_tx: mpsc::Sender<crate::image_preview::ImagePreviewEvent>,
    pub http_client: reqwest::Client,
    pub picker: ratatui_image::picker::Picker,
    pub in_tmux: bool,
    pub needs_full_redraw: bool,
    pub outer_terminal: String,
    pub color_support: crate::nick_color::ColorSupport,
    pub image_proto_source: String,
    pub shim_term_env: Option<std::collections::HashMap<String, String>>,
    pub(crate) channel_query_queues: HashMap<String, VecDeque<String>>,
    pub(crate) channel_query_in_flight: HashMap<String, HashSet<String>>,
    pub(crate) channel_query_sent_at: HashMap<String, Instant>,
    pub(crate) paste_queue: VecDeque<String>,
    pub script_manager: Option<crate::scripting::engine::ScriptManager>,
    pub script_api: Option<crate::scripting::engine::ScriptAPI>,
    pub script_state: Arc<std::sync::RwLock<crate::scripting::engine::ScriptStateSnapshot>>,
    pub(crate) script_action_rx: mpsc::Receiver<crate::scripting::ScriptAction>,
    pub script_commands: HashMap<String, (String, String)>,
    pub script_config: HashMap<(String, String), String>,
    pub(crate) active_timers: HashMap<u64, tokio::task::JoinHandle<()>>,
    pub(crate) script_action_tx: mpsc::Sender<crate::scripting::ScriptAction>,
    pub wrap_indent: usize,
    pub cached_config_toml: Option<toml::Value>,
    pub terminal: Option<ui::Tui>,
    pub detached: bool,
    pub should_detach: bool,
    pub(crate) socket_listener: Option<tokio::net::UnixListener>,
    pub(crate) socket_output_tx:
        Option<tokio::sync::mpsc::Sender<crate::session::protocol::MainMessage>>,
    pub(crate) shim_event_rx:
        Option<tokio::sync::mpsc::Receiver<crate::session::protocol::ShimMessage>>,
    pub is_socket_attached: bool,
    pub(crate) term_reader_stop: Arc<AtomicBool>,
    pub(crate) term_rx: Option<tokio::sync::mpsc::Receiver<crossterm::event::Event>>,
    pub(crate) shim_output_handle: Option<tokio::task::JoinHandle<()>>,
    pub(crate) shim_input_handle: Option<tokio::task::JoinHandle<()>>,
    pub cached_term_cols: u16,
    pub cached_term_rows: u16,
    pub dcc: crate::dcc::DccManager,
    pub(crate) dcc_rx: mpsc::Receiver<crate::dcc::DccEvent>,
    pub shell_mgr: crate::shell::ShellManager,
    pub(crate) shell_rx: mpsc::Receiver<crate::shell::ShellEvent>,
    pub shell_input_active: bool,
    pub(crate) last_shell_web_broadcast: Instant,
    pub(crate) shell_broadcast_pending: Option<String>,
    pub spellchecker: Option<crate::spellcheck::SpellChecker>,
    pub(crate) dict_rx: mpsc::Receiver<crate::spellcheck::DictEvent>,
    pub dict_tx: mpsc::Sender<crate::spellcheck::DictEvent>,
    pub web_broadcaster: std::sync::Arc<crate::web::broadcast::WebBroadcaster>,
    pub(crate) web_cmd_rx: mpsc::Receiver<(crate::web::protocol::WebCommand, String)>,
    pub(crate) web_cmd_tx: mpsc::Sender<(crate::web::protocol::WebCommand, String)>,
    pub(crate) web_server_handle: Option<tokio::task::JoinHandle<()>>,
    pub(crate) web_sessions:
        Option<std::sync::Arc<tokio::sync::Mutex<crate::web::auth::SessionStore>>>,
    pub(crate) web_rate_limiter:
        Option<std::sync::Arc<tokio::sync::Mutex<crate::web::auth::RateLimiter>>>,
    pub(crate) web_state_snapshot:
        Option<std::sync::Arc<std::sync::RwLock<crate::web::server::WebStateSnapshot>>>,
    pub(crate) web_active_buffers: HashMap<String, String>,
    pub web_restart_pending: bool,
    /// Tracks the current local date for emitting "day changed" markers.
    pub(crate) last_day: chrono::NaiveDate,
}

impl App {
    /// Connection ID for the app-level default Status buffer.
    pub const DEFAULT_CONN_ID: &'static str = "_default";

    #[allow(clippy::too_many_lines)]
    pub fn new() -> Result<Self> {
        constants::ensure_config_dir();
        let mut config = config::load_config(&constants::config_path())?;

        let env_vars = config::load_env(&constants::env_path())?;
        config::apply_credentials(&mut config.servers, &env_vars);
        config::apply_web_credentials(&mut config.web, &env_vars);
        let theme_path = constants::theme_dir().join(format!("{}.theme", config.general.theme));
        let theme = theme::load_theme(&theme_path)?;

        let mut state = AppState::new();
        state.flood_protection = config.general.flood_protection;
        state.ignores.clone_from(&config.ignores);
        state.scrollback_limit = config.display.scrollback_lines;
        state.nick_color_sat = config.display.nick_color_saturation;
        state.nick_color_lit = config.display.nick_color_lightness;
        let (irc_tx, irc_rx) = mpsc::channel(4096);

        let storage = if config.logging.enabled {
            match crate::storage::Storage::init(&config.logging) {
                Ok(s) => {
                    state.log_tx = Some(s.log_tx.clone());
                    state
                        .log_exclude_types
                        .clone_from(&config.logging.exclude_types);
                    Some(s)
                }
                Err(e) => {
                    tracing::error!("failed to initialize storage: {e}");
                    None
                }
            }
        } else {
            None
        };

        // RPE2E manager — needs storage to be up. The keyring shares the
        // same SQLite connection owned by Storage.
        if config.e2e.enabled
            && let Some(storage_ref) = storage.as_ref()
        {
            let keyring = crate::e2e::keyring::Keyring::new_encrypted(storage_ref.db.clone())?;
            match crate::e2e::E2eManager::load_or_init_with_config(keyring, &config.e2e) {
                Ok(mgr) => {
                    let fp = mgr.fingerprint();
                    tracing::info!(
                        "e2e: manager initialized, fingerprint={}",
                        crate::e2e::crypto::fingerprint::fingerprint_hex(&fp)
                    );
                    state.e2e_manager = Some(std::sync::Arc::new(mgr));
                }
                Err(e) => tracing::error!("e2e: manager init failed: {e}"),
            }
        }

        let (preview_tx, preview_rx) = mpsc::channel(64);

        let in_tmux = std::env::var("TMUX").is_ok_and(|s| !s.is_empty());
        let picker_result = ratatui_image::picker::Picker::from_query_stdio();
        tracing::debug!(
            result = ?picker_result.as_ref().map(ratatui_image::picker::Picker::protocol_type),
            capabilities = ?picker_result.as_ref().ok().map(|p| p.capabilities().clone()),
            font_size = ?picker_result.as_ref().ok().map(ratatui_image::picker::Picker::font_size),
            "ratatui-image from_query_stdio result"
        );
        let mut picker =
            picker_result.unwrap_or_else(|_| ratatui_image::picker::Picker::halfblocks());

        let (outer_terminal, outer_proto, outer_source) = detect_outer_terminal(in_tmux, None);
        tracing::info!(
            outer_terminal = %outer_terminal,
            outer_proto = ?outer_proto,
            outer_source = %outer_source,
            "outer terminal detected"
        );

        let (resolved_proto, source) = resolve_image_protocol(
            &config.image_preview.protocol,
            &picker,
            outer_terminal,
            outer_proto,
            outer_source,
            false,
        );
        if let Some(proto) = resolved_proto {
            tracing::debug!(
                from = ?picker.protocol_type(),
                to = ?proto,
                "overriding picker protocol"
            );
            picker.set_protocol_type(proto);
        }
        tracing::info!(
            protocol = ?picker.protocol_type(),
            source = %source,
            "image preview protocol selected"
        );

        let http_client = reqwest::Client::new();

        let (dict_tx, dict_rx) = mpsc::channel(64);
        let (web_tx, web_rx) = mpsc::channel(256);

        let (mut dcc, dcc_rx) = crate::dcc::DccManager::new();
        dcc.timeout_secs = config.dcc.timeout;
        if !config.dcc.own_ip.is_empty() {
            dcc.own_ip = config.dcc.own_ip.parse().ok();
        }
        dcc.port_range = crate::dcc::chat::parse_port_range(&config.dcc.port_range);
        dcc.autoaccept_lowports = config.dcc.autoaccept_lowports;
        dcc.autochat_masks.clone_from(&config.dcc.autochat_masks);
        dcc.max_connections = config.dcc.max_connections;

        let (shell_mgr, shell_rx) = crate::shell::ShellManager::new();

        let (script_action_tx, script_action_rx) = mpsc::channel(1024);
        let script_state = Arc::new(std::sync::RwLock::new(state.script_snapshot()));
        let next_timer_id = Arc::new(std::sync::atomic::AtomicU64::new(1));
        let script_api = Self::build_script_api(
            script_action_tx.clone(),
            Arc::clone(&script_state),
            Arc::clone(&next_timer_id),
        );
        let mut script_manager =
            crate::scripting::engine::ScriptManager::new(constants::scripts_dir());
        match crate::scripting::lua::LuaEngine::new() {
            Ok(lua_engine) => {
                script_manager.register_engine(Box::new(lua_engine));
                tracing::info!("Lua scripting engine registered");
            }
            Err(e) => {
                tracing::error!("failed to initialize Lua engine: {e}");
            }
        }

        let color_support = crate::nick_color::detect_color_support(outer_terminal);
        tracing::info!(%outer_terminal, ?color_support, "terminal color support detected");

        let mut app = Self {
            state,
            config,
            theme,
            input: ui::input::InputState::new(),
            should_quit: false,
            script_snapshot_dirty: true,
            splash_visible: 0,
            splash_done: false,
            scroll_offset: 0,
            ui_regions: None,
            irc_handles: HashMap::new(),
            forwarder_handles: HashMap::new(),
            irc_tx,
            irc_rx,
            last_esc_time: None,
            buffer_list_scroll: 0,
            buffer_list_total: 0,
            nick_list_scroll: 0,
            nick_list_total: 0,
            lag_pings: HashMap::new(),
            batch_trackers: HashMap::new(),
            storage,
            last_event_purge: Instant::now(),
            last_mention_purge: Instant::now(),
            quit_message: None,
            image_preview: crate::image_preview::PreviewStatus::default(),
            image_clear_rect: None,
            preview_rx,
            preview_tx,
            http_client,
            picker,
            in_tmux,
            needs_full_redraw: false,
            outer_terminal: outer_terminal.to_string(),
            color_support,
            image_proto_source: source,
            shim_term_env: None,
            channel_query_queues: HashMap::new(),
            channel_query_in_flight: HashMap::new(),
            channel_query_sent_at: HashMap::new(),
            paste_queue: VecDeque::new(),
            script_manager: Some(script_manager),
            script_api: Some(script_api),
            script_state,
            script_action_rx,
            script_commands: HashMap::new(),
            script_config: HashMap::new(),
            active_timers: HashMap::new(),
            script_action_tx,
            wrap_indent: 0,
            cached_config_toml: None,
            terminal: None,
            detached: false,
            should_detach: false,
            socket_listener: None,
            socket_output_tx: None,
            shim_event_rx: None,
            is_socket_attached: false,
            term_reader_stop: Arc::new(AtomicBool::new(false)),
            term_rx: None,
            shim_output_handle: None,
            shim_input_handle: None,
            cached_term_cols: 80,
            cached_term_rows: 24,
            dcc,
            dcc_rx,
            shell_mgr,
            shell_rx,
            shell_input_active: false,
            last_shell_web_broadcast: Instant::now(),
            shell_broadcast_pending: None,
            spellchecker: None,
            dict_rx,
            dict_tx,
            web_broadcaster: std::sync::Arc::new(crate::web::broadcast::WebBroadcaster::new(2048)),
            web_cmd_rx: web_rx,
            web_cmd_tx: web_tx,
            web_server_handle: None,
            web_sessions: None,
            web_rate_limiter: None,
            web_state_snapshot: None,
            web_active_buffers: HashMap::new(),
            web_restart_pending: false,
            last_day: chrono::Local::now().date_naive(),
        };
        app.recompute_wrap_indent();

        if app.config.spellcheck.enabled {
            app.init_spellchecker();
        }

        Ok(app)
    }

    /// Recompute the cached wrap-indent width used by `chat_view`.
    pub fn recompute_wrap_indent(&mut self) {
        let ts_sample = chrono::Local::now()
            .format(&self.config.general.timestamp_format)
            .to_string();
        let ts_format = self
            .theme
            .abstracts
            .get("timestamp")
            .cloned()
            .unwrap_or_else(|| "$*".to_string());
        let ts_resolved = crate::theme::resolve_abstractions(&ts_format, &self.theme.abstracts, 0);
        let ts_spans = crate::theme::parse_format_string(&ts_resolved, &[&ts_sample]);
        let ts_visual_width: usize = ts_spans.iter().map(|s| s.text.chars().count()).sum();
        self.wrap_indent = ts_visual_width + 1 + self.config.display.nick_column_width as usize + 1;
    }

    /// Animated splash screen.
    async fn run_splash(&mut self) -> Result<()> {
        const LINE_DELAY_MS: u64 = 50;
        const HOLD_MS: u64 = 2500;

        let Some(terminal) = self.terminal.as_mut() else {
            self.splash_done = true;
            return Ok(());
        };
        let total_lines = include_str!("../../logo.txt").lines().count();

        let mut line_tick = interval(Duration::from_millis(LINE_DELAY_MS));

        while self.splash_visible < total_lines {
            terminal.draw(|frame| ui::splash::render(frame, self.splash_visible))?;

            tokio::select! {
                _ = line_tick.tick() => {
                    self.splash_visible += 1;
                }
                ev = tokio::task::spawn_blocking(|| {
                    if event::poll(std::time::Duration::from_millis(1)).unwrap_or(false) {
                        event::read().ok()
                    } else {
                        None
                    }
                }) => {
                    if let Ok(Some(Event::Key(_))) = ev {
                        self.splash_done = true;
                        return Ok(());
                    }
                }
            }
        }

        let terminal = self.terminal.as_mut().unwrap();
        terminal.draw(|frame| ui::splash::render(frame, total_lines))?;
        let hold_start = Instant::now();
        while hold_start.elapsed() < Duration::from_millis(HOLD_MS) {
            let remaining = Duration::from_millis(HOLD_MS).saturating_sub(hold_start.elapsed());
            if remaining.is_zero() {
                break;
            }
            if let Ok(Some(Event::Key(_))) = tokio::task::spawn_blocking(move || {
                if event::poll(remaining.min(Duration::from_millis(100))).unwrap_or(false) {
                    event::read().ok()
                } else {
                    None
                }
            })
            .await
            {
                break;
            }
        }

        self.splash_done = true;
        Ok(())
    }

    /// Spawn the blocking terminal event reader thread for local terminal mode.
    fn start_term_reader(&mut self) {
        let (term_tx, term_rx) = mpsc::channel(4096);
        let stop = Arc::clone(&self.term_reader_stop);
        self.term_reader_stop.store(false, Ordering::Relaxed);
        std::thread::spawn(move || {
            while !stop.load(Ordering::Relaxed) {
                if event::poll(std::time::Duration::from_millis(100)).unwrap_or(false) {
                    match event::read() {
                        Ok(ev) => {
                            if term_tx.blocking_send(ev).is_err() {
                                break;
                            }
                        }
                        Err(_) => break,
                    }
                }
            }
        });
        self.term_rx = Some(term_rx);
    }

    /// Stop the local terminal reader thread.
    fn stop_term_reader(&mut self) {
        self.term_reader_stop.store(true, Ordering::Relaxed);
        self.term_rx = None;
    }

    /// Get terminal size from cached dimensions.
    pub const fn terminal_size(&self) -> (u16, u16) {
        (self.cached_term_cols, self.cached_term_rows)
    }

    fn create_default_status(state: &mut AppState) {
        let buf_id = make_buffer_id(Self::DEFAULT_CONN_ID, "Status");
        state.add_connection(Connection {
            id: Self::DEFAULT_CONN_ID.to_string(),
            label: "Status".to_string(),
            status: ConnectionStatus::Disconnected,
            nick: String::new(),
            user_modes: String::new(),
            isupport: HashMap::new(),
            isupport_parsed: crate::irc::isupport::Isupport::new(),
            error: None,
            lag: None,
            lag_pending: false,
            reconnect_attempts: 0,
            reconnect_delay_secs: 0,
            next_reconnect: None,
            should_reconnect: false,
            joined_channels: Vec::new(),
            origin_config: config::ServerConfig {
                label: String::new(),
                address: String::new(),
                port: 0,
                tls: false,
                tls_verify: true,
                autoconnect: false,
                channels: vec![],
                nick: None,
                username: None,
                realname: None,
                password: None,
                sasl_user: None,
                sasl_pass: None,
                bind_ip: None,
                encoding: None,
                auto_reconnect: Some(false),
                reconnect_delay: None,
                reconnect_max_retries: None,
                autosendcmd: None,
                sasl_mechanism: None,
                client_cert_path: None,
            },
            local_ip: None,
            enabled_caps: HashSet::new(),
            who_token_counter: 0,
            silent_who_channels: HashSet::new(),
        });
        state.add_buffer(Buffer {
            id: buf_id.clone(),
            connection_id: Self::DEFAULT_CONN_ID.to_string(),
            buffer_type: BufferType::Server,
            name: "Status".to_string(),
            messages: VecDeque::new(),
            activity: ActivityLevel::None,
            unread_count: 0,
            last_read: Utc::now(),
            topic: None,
            topic_set_by: None,
            users: HashMap::new(),
            modes: None,
            mode_params: None,
            list_modes: HashMap::new(),
            last_speakers: Vec::new(),
            peer_handle: None,
        });
        state.set_active_buffer(&buf_id);

        let id = state.next_message_id();
        state.add_message(
            &buf_id,
            Message {
                id,
                timestamp: Utc::now(),
                message_type: MessageType::Event,
                nick: None,
                nick_mode: None,
                text: format!(
                    "Welcome to {}! Use /connect <server> to connect.",
                    crate::constants::APP_NAME
                ),
                highlight: false,
                event_key: None,
                event_params: None,
                log_msg_id: None,
                log_ref_id: None,
                tags: None,
            },
        );
    }

    /// Recreate the default Status buffer if no real buffers remain.
    pub fn ensure_default_status(&mut self) {
        let has_real_buffers = self
            .state
            .buffers
            .values()
            .any(|b| b.connection_id != Self::DEFAULT_CONN_ID);
        if !has_real_buffers {
            Self::create_default_status(&mut self.state);
        }
    }

    #[allow(clippy::too_many_lines)]
    pub async fn run(&mut self) -> Result<()> {
        crate::session::cleanup_stale_sockets();

        if self.terminal.is_some() && !self.is_socket_attached {
            self.run_splash().await?;
        }

        let autoconnect_ids: Vec<String> = self
            .config
            .servers
            .iter()
            .filter(|(_, cfg)| cfg.autoconnect)
            .map(|(id, _)| id.clone())
            .collect();

        for server_id in &autoconnect_ids {
            let _ = self.connect_server_async(server_id).await;
        }

        if self.state.buffers.is_empty() {
            Self::create_default_status(&mut self.state);
        }

        self.autoload_scripts();

        if self.config.display.mentions_buffer {
            self.create_mentions_buffer();
        }

        if self.terminal.is_some() && !self.is_socket_attached {
            self.start_term_reader();
        }

        if let Err(e) = self.start_socket_listener() {
            tracing::warn!("failed to start session socket: {e}");
        }

        self.start_web_server().await;

        let mut sigterm =
            tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())?;
        let mut sigint = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::interrupt())?;
        let mut sighup = tokio::signal::unix::signal(tokio::signal::unix::SignalKind::hangup())?;

        let mut tick = interval(Duration::from_secs(1));
        let mut paste_tick = interval(Duration::from_millis(500));
        let shell_broadcast_sleep = tokio::time::sleep(std::time::Duration::from_secs(86400));
        tokio::pin!(shell_broadcast_sleep);

        while !self.should_quit {
            if self.should_detach {
                self.perform_detach();
            }

            if self.web_restart_pending {
                self.web_restart_pending = false;
                self.stop_web_server();
                self.start_web_server().await;
            }

            if let Some(mut terminal) = self.terminal.take() {
                if self.needs_full_redraw {
                    let _ = terminal.clear();
                    self.needs_full_redraw = false;
                }
                match terminal.draw(|frame| ui::layout::draw(frame, self)) {
                    Ok(_) => {
                        self.terminal = Some(terminal);
                    }
                    Err(e) => {
                        tracing::warn!("terminal draw failed, triggering detach: {e}");
                        self.should_detach = true;
                    }
                }
            }

            if self.terminal.is_some() {
                self.write_tmux_direct_image();
            }

            tokio::select! {
                ev = async {
                    match self.term_rx.as_mut() {
                        Some(rx) => rx.recv().await,
                        None => std::future::pending().await,
                    }
                } => match ev {
                    Some(ev) => {
                        self.handle_event(ev);
                        if let Some(mut rx) = self.term_rx.take() {
                            while let Ok(ev) = rx.try_recv() {
                                self.handle_event(ev);
                            }
                            self.term_rx = Some(rx);
                        }
                        self.update_script_snapshot();
                        self.drain_pending_web_events();
                    }
                    None => {
                        self.term_rx = None;
                    }
                },
                shim_ev = async {
                    match self.shim_event_rx.as_mut() {
                        Some(rx) => rx.recv().await,
                        None => std::future::pending().await,
                    }
                } => match shim_ev {
                    Some(crate::session::protocol::ShimMessage::TermEvent(ev)) => {
                        self.handle_event(ev);
                        if let Some(mut rx) = self.shim_event_rx.take() {
                            while let Ok(msg) = rx.try_recv() {
                                if let crate::session::protocol::ShimMessage::TermEvent(ev) = msg {
                                    self.handle_event(ev);
                                }
                            }
                            self.shim_event_rx = Some(rx);
                        }
                        self.update_script_snapshot();
                        self.drain_pending_web_events();
                    }
                    Some(crate::session::protocol::ShimMessage::Resize { cols, rows }) => {
                        self.cached_term_cols = cols;
                        self.cached_term_rows = rows;
                        if let Some(ref mut terminal) = self.terminal {
                            let _ = terminal.resize(ratatui::layout::Rect::new(0, 0, cols, rows));
                            self.needs_full_redraw = true;
                        }
                        self.resize_all_shells();
                    }
                    Some(crate::session::protocol::ShimMessage::Detach) => {
                        self.should_detach = true;
                    }
                    None => {
                        tracing::info!("shim disconnected, returning to detached mode");
                        self.terminal = None;
                        self.socket_output_tx = None;
                        self.shim_event_rx = None;
                        self.is_socket_attached = false;
                        self.shim_term_env = None;
                        if let Some(h) = self.shim_output_handle.take() { h.abort(); }
                        if let Some(h) = self.shim_input_handle.take() { h.abort(); }
                        self.detached = true;
                    }
                },
                stream = async {
                    match self.socket_listener.as_ref() {
                        Some(listener) => {
                            match listener.accept().await {
                                Ok((stream, _)) => Some(stream),
                                Err(e) => {
                                    tracing::warn!("socket accept error: {e}");
                                    None
                                }
                            }
                        }
                        None => std::future::pending().await,
                    }
                } => {
                    if let Some(stream) = stream
                        && let Err(e) = self.handle_shim_connect(stream).await
                    {
                        tracing::warn!("failed to handle shim connection: {e}");
                    }
                },
                irc_ev = self.irc_rx.recv() => {
                    if let Some(event) = irc_ev {
                        self.handle_irc_event(event);
                        self.script_snapshot_dirty = true;
                        self.update_script_snapshot();
                        self.drain_pending_web_events();
                    }
                },
                preview_ev = self.preview_rx.recv() => {
                    if let Some(ev) = preview_ev {
                        self.handle_preview_event(ev);
                    }
                },
                dcc_ev = self.dcc_rx.recv() => {
                    if let Some(ev) = dcc_ev {
                        self.handle_dcc_event(ev);
                        self.drain_pending_web_events();
                    }
                },
                shell_ev = self.shell_rx.recv() => {
                    if let Some(ev) = shell_ev {
                        self.handle_shell_event(ev);
                        if self.shell_broadcast_pending.is_some() {
                            shell_broadcast_sleep.as_mut().reset(
                                tokio::time::Instant::now() + std::time::Duration::from_millis(150)
                            );
                        }
                    }
                },
                dict_ev = self.dict_rx.recv() => {
                    if let Some(ev) = dict_ev {
                        self.handle_dict_event(ev);
                    }
                },
                web_cmd = self.web_cmd_rx.recv() => {
                    if let Some((cmd, session_id)) = web_cmd {
                        tracing::debug!(?cmd, %session_id, "web command received");
                        self.handle_web_command(cmd, &session_id);
                        self.drain_pending_web_events();
                    }
                },
                () = &mut shell_broadcast_sleep, if self.shell_broadcast_pending.is_some() => {
                    if let Some(shell_id) = self.shell_broadcast_pending.take() {
                        if self.shell_mgr.is_web_session(&shell_id) {
                            self.force_broadcast_web_shell_screen(&shell_id);
                        } else {
                            self.force_broadcast_shell_screen(&shell_id);
                        }
                    }
                    shell_broadcast_sleep.as_mut().reset(tokio::time::Instant::now() + std::time::Duration::from_secs(86400));
                },
                _ = tick.tick() => {
                    self.handle_netsplit_tick();
                    self.purge_expired_batches();
                    self.check_reconnects();
                    self.measure_lag();
                    self.check_day_changed();
                    if self.script_manager.is_some() && !self.script_commands.is_empty() {
                        self.update_script_snapshot();
                    }
                    self.check_stale_who_batches();
                    if let Some(ref sessions) = self.web_sessions
                        && let Ok(mut s) = sessions.try_lock() { s.purge_expired(); }
                    if let Some(ref limiter) = self.web_rate_limiter
                        && let Ok(mut l) = limiter.try_lock() { l.purge_expired(); }
                    if let Some(ref snapshot) = self.web_state_snapshot
                        && let Ok(mut snap) = snapshot.write()
                    {
                        let mention_count = self.storage.as_ref().and_then(|s| {
                            s.db.try_lock().ok().and_then(|db| {
                                crate::storage::query::get_unread_mention_count(&db).ok()
                            })
                        }).unwrap_or(0);
                        let init = crate::web::snapshot::build_sync_init(&self.state, mention_count, &self.config.web.timestamp_format);
                        if let crate::web::protocol::WebEvent::SyncInit { buffers, connections, mention_count, active_buffer_id, timestamp_format, .. } = init {
                            snap.buffers = buffers;
                            snap.connections = connections;
                            snap.mention_count = mention_count;
                            snap.active_buffer_id = active_buffer_id;
                            snap.timestamp_format = timestamp_format;
                        }
                    }
                    let expired = self.dcc.purge_expired();
                    for (_id, nick) in expired {
                        crate::commands::helpers::add_local_event(
                            self,
                            &format!("DCC CHAT request from {nick} timed out"),
                        );
                    }
                    self.maybe_purge_old_events();
                    self.maybe_purge_old_mentions();
                    self.drain_pending_web_events();
                },
                _ = paste_tick.tick() => {
                    self.drain_paste_queue();
                    self.drain_pending_web_events();
                },
                action = self.script_action_rx.recv() => {
                    if let Some(action) = action {
                        self.handle_script_action(action);
                        while let Ok(action) = self.script_action_rx.try_recv() {
                            self.handle_script_action(action);
                        }
                        self.script_snapshot_dirty = true;
                        self.update_script_snapshot();
                        self.drain_pending_web_events();
                    }
                },
                _ = sigterm.recv() => {
                    self.should_quit = true;
                },
                _ = sigint.recv() => {
                    if self.detached {
                        self.should_quit = true;
                    }
                },
                _ = sighup.recv() => {
                    if !self.detached {
                        tracing::info!("SIGHUP received, auto-detaching");
                        self.should_detach = true;
                    }
                },
            }
        }

        for (_, handle) in self.active_timers.drain() {
            handle.abort();
        }

        for (_, handle) in self.forwarder_handles.drain() {
            handle.abort();
        }

        self.notify_shim_quit();
        self.stop_term_reader();

        self.shell_mgr.kill_all();

        let default_quit = crate::constants::default_quit_message();
        let quit_msg = self.quit_message.as_deref().unwrap_or(&default_quit);
        for handle in self.irc_handles.values() {
            let _ = handle.sender.send_quit(quit_msg);
        }
        for _ in 0..10 {
            tokio::task::yield_now().await;
        }
        tokio::time::sleep(std::time::Duration::from_millis(500)).await;
        for handle in self.irc_handles.values_mut() {
            if let Some(oh) = handle.outgoing_handle.take() {
                oh.abort();
            }
        }

        if let Some(storage) = self.storage.take() {
            storage.shutdown().await;
        }

        Self::remove_own_socket();

        Ok(())
    }
}

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

    // ── match_terminal tests ──

    #[test]
    fn match_terminal_iterm2_termtype() {
        let (name, proto) = match_terminal("iTerm2 3.6.8").unwrap();
        assert_eq!(name, "iterm2");
        assert_eq!(proto, ProtocolType::Iterm2);
    }

    #[test]
    fn match_terminal_ghostty() {
        let (name, proto) = match_terminal("ghostty 1.3.0").unwrap();
        assert_eq!(name, "ghostty");
        assert_eq!(proto, ProtocolType::Kitty);
        let (name, proto) = match_terminal("xterm-ghostty").unwrap();
        assert_eq!(name, "ghostty");
        assert_eq!(proto, ProtocolType::Kitty);
    }

    #[test]
    fn match_terminal_kitty() {
        let (name, proto) = match_terminal("xterm-kitty").unwrap();
        assert_eq!(name, "kitty");
        assert_eq!(proto, ProtocolType::Kitty);
    }

    #[test]
    fn match_terminal_subterm() {
        let (name, proto) = match_terminal("Subterm 1.0").unwrap();
        assert_eq!(name, "subterm");
        assert_eq!(proto, ProtocolType::Kitty);
    }

    #[test]
    fn match_terminal_wezterm() {
        let (name, proto) = match_terminal("WezTerm 20240203").unwrap();
        assert_eq!(name, "wezterm");
        assert_eq!(proto, ProtocolType::Iterm2);
    }

    #[test]
    fn match_terminal_foot() {
        let (name, proto) = match_terminal("foot").unwrap();
        assert_eq!(name, "foot");
        assert_eq!(proto, ProtocolType::Sixel);
    }

    #[test]
    fn match_terminal_konsole() {
        let (name, proto) = match_terminal("konsole").unwrap();
        assert_eq!(name, "konsole");
        assert_eq!(proto, ProtocolType::Sixel);
    }

    #[test]
    fn match_terminal_unknown() {
        assert!(match_terminal("some-random-terminal").is_none());
    }

    // ── resolve_image_protocol tests ──

    #[test]
    fn resolve_config_override_kitty() {
        let picker = ratatui_image::picker::Picker::halfblocks();
        let (proto, source) =
            resolve_image_protocol("kitty", &picker, "unknown", None, String::new(), false);
        assert_eq!(proto, Some(ProtocolType::Kitty));
        assert_eq!(source, "config:kitty");
    }

    #[test]
    fn resolve_config_override_iterm2() {
        let picker = ratatui_image::picker::Picker::halfblocks();
        let (proto, source) =
            resolve_image_protocol("iterm2", &picker, "unknown", None, String::new(), false);
        assert_eq!(proto, Some(ProtocolType::Iterm2));
        assert_eq!(source, "config:iterm2");
    }

    #[test]
    fn resolve_tmux_overrides_io_detection() {
        let mut picker = ratatui_image::picker::Picker::halfblocks();
        picker.set_protocol_type(ProtocolType::Kitty);
        let (proto, source) = resolve_image_protocol(
            "auto",
            &picker,
            "ghostty",
            Some(ProtocolType::Kitty),
            "tmux:client_termtype=ghostty 1.3.0".into(),
            false,
        );
        assert_eq!(proto, Some(ProtocolType::Kitty));
        assert!(source.starts_with("tmux:"));
    }

    #[test]
    fn resolve_tmux_iterm2_overrides_kitty_io() {
        let mut picker = ratatui_image::picker::Picker::halfblocks();
        picker.set_protocol_type(ProtocolType::Kitty);
        let (proto, source) = resolve_image_protocol(
            "auto",
            &picker,
            "iterm2",
            Some(ProtocolType::Iterm2),
            "tmux:client_termtype=iTerm2 3.6.8".into(),
            false,
        );
        assert_eq!(proto, Some(ProtocolType::Iterm2));
        assert!(source.starts_with("tmux:"));
    }

    #[test]
    fn resolve_direct_trusts_io_detection() {
        let mut picker = ratatui_image::picker::Picker::halfblocks();
        picker.set_protocol_type(ProtocolType::Kitty);
        let (proto, source) = resolve_image_protocol(
            "auto",
            &picker,
            "ghostty",
            Some(ProtocolType::Kitty),
            "env:LC_TERMINAL=Ghostty".into(),
            false,
        );
        assert_eq!(proto, None);
        assert!(source.starts_with("io-query:"));
    }

    #[test]
    fn resolve_env_iterm2_override_over_kitty_io() {
        let mut picker = ratatui_image::picker::Picker::halfblocks();
        picker.set_protocol_type(ProtocolType::Kitty);
        let (proto, _source) = resolve_image_protocol(
            "auto",
            &picker,
            "iterm2",
            Some(ProtocolType::Iterm2),
            "env:ITERM_SESSION_ID".into(),
            false,
        );
        assert_eq!(proto, Some(ProtocolType::Iterm2));
    }

    #[test]
    fn resolve_shim_env_overrides_io_detection() {
        let mut picker = ratatui_image::picker::Picker::halfblocks();
        picker.set_protocol_type(ProtocolType::Iterm2);
        let (proto, source) = resolve_image_protocol(
            "auto",
            &picker,
            "subterm",
            Some(ProtocolType::Kitty),
            "env:LC_TERMINAL=subterm".into(),
            true,
        );
        assert_eq!(proto, Some(ProtocolType::Kitty));
        assert_eq!(source, "env:LC_TERMINAL=subterm");
    }
}