catenary-mcp 1.5.0

A high-performance multiplexing bridge between MCP (Model Context Protocol) and LSP (Language Server Protocol). Enables LLMs to access IDE-grade code intelligence across multiple languages simultaneously with smart routing and UTF-8 accuracy.
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
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2026 Mark Wells <contact@markwells.dev>

//! Messages panel: renders a list of protocol messages with cursor, scroll
//! offset, tail attach/detach behavior, and horizontal scroll indicators.
//!
//! This is the core building block — later tickets add expansion (04),
//! multi-panel grid (05), scrollbar (06), and selection (07) on top of this.

use std::collections::{HashMap, HashSet};

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::{Color, Style};
use ratatui::symbols;
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Widget};
use unicode_width::UnicodeWidthStr;

use super::selection::VisualSelection;
use super::theme::{IconSet, Theme, format_message_plain, format_message_styled};
use crate::session::SessionMessage;

// ── Data types ──────────────────────────────────────────────────────────

/// Language server lifecycle state.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum LsState {
    /// Server has not been loaded yet.
    NotLoaded,
    /// Server is initializing.
    Initializing,
    /// Server is sending progress notifications.
    Progress,
    /// Server is healthy and running.
    Healthy,
    /// Server has crashed.
    Crashed,
}

/// Language server status shown in the panel title.
#[derive(Clone, Debug)]
pub struct LanguageServerStatus {
    /// Language server name (e.g., "rust", "ts").
    pub name: String,
    /// Current lifecycle state.
    pub state: LsState,
}

/// A line in the flattened view — either a message header or a detail line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FlatLine {
    /// A message header (the one-line summary).
    MessageHeader {
        /// Index into the messages vec.
        message_index: usize,
    },
    /// A detail line within an expanded message.
    Detail {
        /// Index into the messages vec.
        message_index: usize,
        /// Index of this detail line within the expansion.
        detail_index: usize,
    },
}

/// State for a single messages panel.
pub struct PanelState<'a> {
    /// Session ID this panel is tailing.
    pub session_id: String,
    /// All messages loaded for this session.
    pub messages: Vec<SessionMessage>,
    /// Cursor position (index into flat lines).
    pub cursor: usize,
    /// Scroll offset from top of content.
    pub scroll_offset: usize,
    /// Whether the panel is attached to the tail (auto-scrolling).
    pub tail_attached: bool,
    /// Horizontal scroll offset (for wide lines).
    pub horizontal_scroll: usize,
    /// Whether this panel is pinned (enlarged).
    pub pinned: bool,
    /// Language server statuses for the title bar.
    pub language_servers: Vec<LanguageServerStatus>,
    /// Indices of expanded messages (in the messages Vec).
    pub expanded: HashSet<usize>,
    /// Active visual selection, if any.
    pub visual_selection: Option<VisualSelection>,
    /// Last known viewport height (updated each render frame).
    pub viewport_height: usize,
    /// Display ID for the title bar (client session ID if available, else internal ID).
    pub display_id: String,
    /// Active filter pattern (case-insensitive substring match).
    pub filter_pattern: Option<String>,
    /// Semantic color theme (borrowed from the application).
    pub theme: &'a Theme,
    /// Resolved icon set (borrowed from the application).
    pub icons: &'a IconSet,
}

// ── Construction & navigation ───────────────────────────────────────────

impl<'a> PanelState<'a> {
    /// Create a new panel for the given session.
    ///
    /// Starts with empty messages, cursor at 0, tail attached, no horizontal
    /// scroll, not pinned.
    #[must_use]
    pub fn new(session_id: String, theme: &'a Theme, icons: &'a IconSet) -> Self {
        let display_id = session_id.clone();
        Self {
            session_id,
            messages: Vec::new(),
            cursor: 0,
            scroll_offset: 0,
            tail_attached: true,
            horizontal_scroll: 0,
            pinned: false,
            language_servers: Vec::new(),
            expanded: HashSet::new(),
            display_id,
            visual_selection: None,
            viewport_height: 0,
            filter_pattern: None,
            theme,
            icons,
        }
    }

    /// Total number of visible lines (flat lines including expanded detail).
    fn total_lines(&self) -> usize {
        self.flat_lines().len()
    }

    /// Load historical messages. Sets cursor to the last line and attaches tail.
    pub fn load_messages(&mut self, messages: Vec<SessionMessage>) {
        self.messages = messages;
        self.expanded.clear();
        let total = self.total_lines();
        self.cursor = total.saturating_sub(1);
        self.tail_attached = true;
        self.snap_viewport(0);
    }

    /// Append a new message.
    ///
    /// If tail attached, advance cursor and scroll to keep the latest message
    /// visible. If detached, just append (cursor stays put).
    pub fn push_message(&mut self, msg: SessionMessage) {
        self.messages.push(msg);
        if self.tail_attached {
            let total = self.total_lines();
            self.cursor = total.saturating_sub(1);
            self.snap_viewport(0);
        }
    }

    /// Move cursor by `delta`. Clamp to `[0, total_lines - 1]`.
    ///
    /// - Moving up (`delta < 0`): detach tail.
    /// - Moving down past the last line: reattach tail, cursor on last.
    /// - Snap viewport to center cursor (`scrolloff=999` behavior).
    #[allow(
        clippy::cast_possible_wrap,
        clippy::cast_sign_loss,
        reason = "terminal item counts never overflow isize"
    )]
    pub fn navigate(&mut self, delta: isize) {
        let total = self.total_lines();
        if total == 0 {
            self.cursor = 0;
            return;
        }

        let max = (total - 1) as isize;
        let new_pos = self.cursor as isize + delta;

        if delta < 0 {
            self.tail_attached = false;
        }

        if new_pos > max {
            // Moved past end — reattach.
            self.cursor = total - 1;
            self.tail_attached = true;
        } else {
            self.cursor = new_pos.max(0) as usize;
        }

        self.snap_viewport(0);
    }

    /// Scroll viewport by `delta` lines without moving the cursor.
    ///
    /// Used for mouse wheel: moves `scroll_offset` only, detaches tail on
    /// scroll-up, reattaches when scrolled to the very bottom.
    #[allow(
        clippy::cast_possible_wrap,
        clippy::cast_sign_loss,
        reason = "terminal item counts never overflow isize"
    )]
    pub fn scroll_viewport(&mut self, delta: isize) {
        let total = self.total_lines();
        if total == 0 {
            return;
        }

        if delta < 0 {
            self.tail_attached = false;
        }

        let new_offset = (self.scroll_offset as isize + delta)
            .max(0)
            .min(total.saturating_sub(1) as isize);

        #[allow(clippy::cast_sign_loss, reason = "clamped to >= 0")]
        {
            self.scroll_offset = new_offset as usize;
        }

        // Reattach tail if scrolled to the very bottom.
        let vh = if self.viewport_height > 0 {
            self.viewport_height
        } else {
            20
        };
        if self.scroll_offset + vh >= total {
            self.tail_attached = true;
        }
    }

    /// Jump to first line — `g` key.
    pub const fn scroll_to_top(&mut self) {
        self.cursor = 0;
        self.scroll_offset = 0;
        self.tail_attached = false;
    }

    /// Jump to last line — `G` key.
    pub fn scroll_to_bottom(&mut self) {
        let total = self.total_lines();
        self.cursor = total.saturating_sub(1);
        self.tail_attached = true;
        self.snap_viewport(0);
    }

    /// Page up — `Ctrl+U`.
    pub fn page_up(&mut self, height: usize) {
        let half = (height / 2).max(1);
        #[allow(
            clippy::cast_possible_wrap,
            reason = "terminal heights never overflow isize"
        )]
        self.navigate(-(half as isize));
    }

    /// Page down — `Ctrl+D`.
    pub fn page_down(&mut self, height: usize) {
        let half = (height / 2).max(1);
        #[allow(
            clippy::cast_possible_wrap,
            reason = "terminal heights never overflow isize"
        )]
        self.navigate(half as isize);
    }

    /// Compute the `(start, end)` indices of lines visible in the viewport.
    ///
    /// `height` is the inner content height (excluding title bar and borders).
    #[must_use]
    pub fn visible_range(&self, height: usize) -> (usize, usize) {
        let total = self.total_lines();
        let start = self.scroll_offset.min(total);
        let end = (start + height).min(total);
        (start, end)
    }

    /// Derive language server statuses from messages.
    ///
    /// Scans for LSP messages and tracks unique server names. Any server
    /// with messages is considered healthy.
    pub fn update_language_servers(&mut self) {
        let mut seen: HashSet<String> = HashSet::new();
        let mut order: Vec<String> = Vec::new();

        for msg in &self.messages {
            if msg.r#type == "lsp" && !msg.server.is_empty() && seen.insert(msg.server.clone()) {
                order.push(msg.server.clone());
            }
        }

        self.language_servers = order
            .into_iter()
            .map(|name| LanguageServerStatus {
                name,
                state: LsState::Healthy,
            })
            .collect();
    }

    /// Snap viewport so cursor is centered (`scrolloff=999` behavior).
    ///
    /// `height` of 0 means use the last known viewport height from the
    /// previous render frame. Falls back to 20 if never rendered.
    fn snap_viewport(&mut self, height: usize) {
        let h = if height > 0 {
            height
        } else if self.viewport_height > 0 {
            self.viewport_height
        } else {
            20
        };
        let total = self.total_lines();
        if total <= h {
            self.scroll_offset = 0;
            return;
        }
        let target = self.cursor.saturating_sub(h / 2);
        self.scroll_offset = target.min(total.saturating_sub(h));
    }

    // ── Expansion ───────────────────────────────────────────────────────

    /// Build a flat list of lines: message headers interleaved with detail
    /// lines for any expanded messages.
    #[must_use]
    pub fn flat_lines(&self) -> Vec<FlatLine> {
        let lower_pattern = self.filter_pattern.as_ref().map(|p| p.to_lowercase());
        let mut lines = Vec::new();
        for (message_index, msg) in self.messages.iter().enumerate() {
            if let Some(ref pat) = lower_pattern {
                let plain = format_message_plain(msg);
                if !plain.to_lowercase().contains(pat) {
                    continue;
                }
            }
            lines.push(FlatLine::MessageHeader { message_index });
            if self.expanded.contains(&message_index) {
                let count = detail_lines(msg, self.theme).len();
                for detail_index in 0..count {
                    lines.push(FlatLine::Detail {
                        message_index,
                        detail_index,
                    });
                }
            }
        }
        lines
    }

    /// Quick check: does this message have expandable detail?
    #[must_use]
    pub fn has_detail(&self, message_index: usize) -> bool {
        self.messages
            .get(message_index)
            .is_some_and(|msg| msg.payload.as_object().is_some_and(|o| !o.is_empty()))
    }

    /// Toggle expansion of the message under the cursor.
    ///
    /// - On a `MessageHeader`: toggle the message in/out of `expanded`.
    /// - On a `Detail` line: collapse the parent message, move cursor to its header.
    /// - On a message with no detail: no-op.
    pub fn toggle_expansion(&mut self) {
        let flat = self.flat_lines();
        let Some(current) = flat.get(self.cursor) else {
            return;
        };
        match *current {
            FlatLine::MessageHeader { message_index } => {
                if !self.has_detail(message_index) {
                    return;
                }
                if self.expanded.contains(&message_index) {
                    self.expanded.remove(&message_index);
                } else {
                    self.expanded.insert(message_index);
                }
            }
            FlatLine::Detail { message_index, .. } => {
                self.expanded.remove(&message_index);
                // Move cursor to the parent header.
                let new_flat = self.flat_lines();
                if let Some(pos) = new_flat.iter().position(|fl| {
                    matches!(fl, FlatLine::MessageHeader { message_index: mi } if *mi == message_index)
                }) {
                    self.cursor = pos;
                }
            }
        }
        self.snap_viewport(0);
    }
}

// ── Expansion helpers ───────────────────────────────────────────────────

/// Generate styled detail lines for an expanded message.
///
/// Returns an empty vec for messages with empty payloads.
#[must_use]
pub fn detail_lines(msg: &SessionMessage, theme: &Theme) -> Vec<Line<'static>> {
    let payload = &msg.payload;
    if payload.as_object().is_none_or(serde_json::Map::is_empty) {
        return Vec::new();
    }

    // Indent to align past the timestamp column ("HH:MM:SS  " = 10 chars).
    let indent = "          ";
    let mut lines = Vec::new();

    // Line 1: method [type]
    lines.push(Line::from(vec![
        Span::raw(indent.to_string()),
        Span::styled(format!("{} [{}]", msg.method, msg.r#type), theme.muted),
    ]));

    // Line 2: separator
    lines.push(Line::from(vec![
        Span::raw(indent.to_string()),
        Span::styled("\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}\u{2500}", theme.muted),
    ]));

    // Lines 3+: pretty-printed payload
    if let Ok(pretty) = serde_json::to_string_pretty(payload) {
        for line in pretty.lines() {
            lines.push(Line::from(vec![
                Span::raw(indent.to_string()),
                Span::styled(line.to_string(), theme.muted),
            ]));
        }
    }

    lines
}

// ── Rendering ───────────────────────────────────────────────────────────

/// Style for a language server status icon.
fn ls_status_style(state: &LsState) -> Style {
    match state {
        LsState::NotLoaded => Style::default().fg(Color::White),
        LsState::Initializing => Style::default().fg(Color::Yellow),
        LsState::Progress => Style::default().fg(Color::Cyan),
        LsState::Healthy => Style::default().fg(Color::Green),
        LsState::Crashed => Style::default().fg(Color::Red),
    }
}

/// Status icon for a language server state, resolved from the icon set.
fn ls_status_icon<'a>(state: &LsState, icons: &'a IconSet) -> &'a str {
    match state {
        LsState::NotLoaded => &icons.ls_inactive,
        _ => &icons.ls_active,
    }
}

/// Build the title line for a panel.
fn build_title(state: &PanelState<'_>) -> Line<'static> {
    let id_short = if state.display_id.len() > 8 {
        &state.display_id[..8]
    } else {
        &state.display_id
    };

    let mut spans = vec![Span::raw(format!(" Events [{id_short}]"))];

    if state.language_servers.is_empty() {
        spans.push(Span::styled(" no ls", Style::default().fg(Color::DarkGray)));
    } else {
        spans.push(Span::raw(" "));
        for (i, ls) in state.language_servers.iter().enumerate() {
            if i > 0 {
                spans.push(Span::raw(" \u{2571} ")); //            }
            let style = ls_status_style(&ls.state);
            spans.push(Span::styled(
                ls_status_icon(&ls.state, state.icons).to_string(),
                style,
            ));
            spans.push(Span::styled(ls.name.clone(), style));
        }
    }

    spans.push(Span::raw(" "));
    Line::from(spans)
}

/// Convert a borrowed `Line` into a fully owned `Line<'static>`.
#[must_use]
pub fn to_owned_line(line: &Line<'_>) -> Line<'static> {
    Line::from(
        line.spans
            .iter()
            .map(|s| Span::styled(s.content.to_string(), s.style))
            .collect::<Vec<_>>(),
    )
}

/// Apply horizontal scrolling to a styled line, inserting clip indicators.
///
/// Returns a new line clipped to `width` display columns, with `◀…` on the
/// left when content is clipped left and `…▶` on the right when clipped right.
fn clip_line_horizontal(line: &Line<'_>, h_scroll: usize, width: usize) -> Line<'static> {
    if width < 4 {
        // Too narrow for indicators, just return empty.
        return Line::default();
    }

    // Flatten the line into a single plain string + collect grapheme-aware info.
    let full_text: String = line.spans.iter().map(|s| s.content.as_ref()).collect();
    let full_width = UnicodeWidthStr::width(full_text.as_str());

    if full_width == 0 {
        return Line::default();
    }

    // No clipping needed at all.
    if h_scroll == 0 && full_width <= width {
        return to_owned_line(line);
    }

    let clipped_left = h_scroll > 0;
    let clipped_right = full_width > h_scroll + width;

    // Calculate available content width after reserving indicator space.
    let left_reserve = if clipped_left { 2 } else { 0 };
    let right_reserve = if clipped_right { 2 } else { 0 };
    let content_width = width.saturating_sub(left_reserve + right_reserve);

    if content_width == 0 {
        let mut spans = Vec::new();
        if clipped_left {
            spans.push(Span::styled(
                "\u{25C0}\u{2026}",
                Style::default().fg(Color::DarkGray),
            ));
        }
        if clipped_right {
            spans.push(Span::styled(
                "\u{2026}\u{25B6}",
                Style::default().fg(Color::DarkGray),
            ));
        }
        return Line::from(spans);
    }

    // Walk through spans, tracking display-width position, and extract the
    // visible portion respecting h_scroll and content_width.
    let vis_start = h_scroll;
    let vis_end = h_scroll + content_width;

    let mut result_spans: Vec<Span<'static>> = Vec::new();

    if clipped_left {
        result_spans.push(Span::styled(
            "\u{25C0}\u{2026}",
            Style::default().fg(Color::DarkGray),
        )); // ◀…
    }

    // Walk spans and extract visible portion.
    let mut col = 0usize;
    for span in &line.spans {
        let span_text = span.content.as_ref();
        let span_width = UnicodeWidthStr::width(span_text);
        let span_end = col + span_width;

        if span_end <= vis_start || col >= vis_end {
            // Entirely outside visible range.
            col = span_end;
            continue;
        }

        // Partially or fully visible. Extract the visible chars.
        let mut visible = String::new();
        let mut char_col = col;
        for ch in span_text.chars() {
            let ch_width = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0);
            let ch_end = char_col + ch_width;
            if char_col >= vis_end {
                break;
            }
            if ch_end > vis_start {
                visible.push(ch);
            }
            char_col = ch_end;
        }

        if !visible.is_empty() {
            result_spans.push(Span::styled(visible, span.style));
        }

        col = span_end;
    }

    if clipped_right {
        result_spans.push(Span::styled(
            "\u{2026}\u{25B6}",
            Style::default().fg(Color::DarkGray),
        )); // …▶
    }

    Line::from(result_spans)
}

/// Render a single messages panel into the given buffer area.
///
/// The panel owns its top row (title bar) and right column (scrollbar,
/// rendered by ticket 06). Left and bottom edges are content. The caller
/// (grid) handles junction characters.
#[allow(
    clippy::cast_possible_truncation,
    reason = "terminal coordinates are always small"
)]
pub fn render_panel(state: &PanelState<'_>, area: Rect, buf: &mut Buffer, focused: bool) {
    if area.width < 4 || area.height < 2 {
        return;
    }

    let border_style = if focused {
        state.theme.border_focused
    } else {
        state.theme.border_unfocused
    };

    let title_style = if focused {
        state.theme.title
    } else {
        state.theme.border_unfocused
    };

    let border_set = if focused {
        symbols::border::THICK
    } else {
        symbols::border::PLAIN
    };

    let title = build_title(state);
    let block = Block::default()
        .borders(Borders::TOP | Borders::RIGHT)
        .border_set(border_set)
        .border_style(border_style)
        .title(title)
        .title_style(title_style);
    let inner = block.inner(area);
    block.render(area, buf);

    if inner.width < 2 || inner.height < 1 {
        return;
    }

    // Build flat line list (headers + expanded detail lines).
    let flat = state.flat_lines();

    // Viewport slicing.
    let height = inner.height as usize;
    let total = flat.len();
    let start = state.scroll_offset.min(total);
    let end = (start + height).min(total);

    // Cache detail lines per expanded message to avoid recomputation.
    let mut detail_cache: HashMap<usize, Vec<Line<'static>>> = HashMap::new();

    // Render each visible line.
    let content_width = inner.width as usize;
    for (i, fl) in flat[start..end].iter().enumerate() {
        let y = inner.y + i as u16;
        if y >= inner.y + inner.height {
            break;
        }

        let line = match fl {
            FlatLine::MessageHeader { message_index } => {
                format_message_styled(&state.messages[*message_index], state.icons, state.theme)
            }
            FlatLine::Detail {
                message_index,
                detail_index,
            } => detail_cache
                .entry(*message_index)
                .or_insert_with(|| detail_lines(&state.messages[*message_index], state.theme))
                .get(*detail_index)
                .cloned()
                .unwrap_or_default(),
        };

        let display_line = if state.horizontal_scroll > 0
            || UnicodeWidthStr::width(
                line.spans
                    .iter()
                    .map(|s| s.content.as_ref())
                    .collect::<String>()
                    .as_str(),
            ) > content_width
        {
            clip_line_horizontal(&line, state.horizontal_scroll, content_width)
        } else {
            to_owned_line(&line)
        };

        // Apply cursor highlight to the entire row.
        let line_index = start + i;
        if line_index == state.cursor {
            // Set selection style on the entire row first.
            for x in inner.x..inner.x + inner.width {
                buf[(x, y)].set_style(state.theme.selection);
            }
        }

        buf.set_line(inner.x, y, &display_line, inner.width);

        // Re-apply selection style on top of content for cursor row.
        if line_index == state.cursor {
            for x in inner.x..inner.x + inner.width {
                buf[(x, y)].set_style(state.theme.selection);
            }
        }
    }
}

#[cfg(test)]
#[allow(
    clippy::expect_used,
    reason = "tests use expect for readable assertions"
)]
mod tests {
    use super::*;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    use crate::config::IconConfig;
    use crate::session::SessionMessage;

    fn test_theme() -> Theme {
        Theme::new()
    }

    fn test_icons() -> IconSet {
        IconSet::from_config(IconConfig::default())
    }

    fn make_message(r#type: &str, method: &str, server: &str) -> SessionMessage {
        SessionMessage {
            id: 0,
            r#type: r#type.to_string(),
            method: method.to_string(),
            server: server.to_string(),
            client: "catenary".to_string(),
            request_id: None,
            parent_id: None,
            timestamp: chrono::Utc::now(),
            payload: serde_json::json!({}),
        }
    }

    fn make_message_with_payload(
        r#type: &str,
        method: &str,
        server: &str,
        payload: serde_json::Value,
    ) -> SessionMessage {
        SessionMessage {
            id: 0,
            r#type: r#type.to_string(),
            method: method.to_string(),
            server: server.to_string(),
            client: "catenary".to_string(),
            request_id: None,
            parent_id: None,
            timestamp: chrono::Utc::now(),
            payload,
        }
    }

    /// An LSP message with a non-empty payload (expandable).
    fn make_lsp_message() -> SessionMessage {
        make_message_with_payload(
            "lsp",
            "textDocument/hover",
            "rust-analyzer",
            serde_json::json!({"id": 1, "method": "textDocument/hover", "params": {}}),
        )
    }

    /// A hook diagnostic message with preview (expandable).
    fn make_hook_diag_message(file: &str, count: u64) -> SessionMessage {
        make_message_with_payload(
            "hook",
            "post-tool",
            "catenary",
            serde_json::json!({
                "file": file,
                "count": count,
                "preview": "\t:12:1 [error] rustc: something"
            }),
        )
    }

    /// Convert a ratatui buffer to a single string for assertion matching.
    fn buffer_to_string(buf: &Buffer) -> String {
        let mut s = String::new();
        for y in 0..buf.area.height {
            for x in 0..buf.area.width {
                let cell = &buf[(x, y)];
                s.push_str(cell.symbol());
            }
            s.push('\n');
        }
        s
    }

    #[test]
    fn test_panel_new_tail_attached() {
        let theme = test_theme();
        let icons = test_icons();
        let panel = PanelState::new("abc123".to_string(), &theme, &icons);
        assert!(panel.tail_attached);
        assert_eq!(panel.cursor, 0);
        assert_eq!(panel.scroll_offset, 0);
        assert_eq!(panel.horizontal_scroll, 0);
        assert!(!panel.pinned);
        assert!(panel.messages.is_empty());
    }

    #[test]
    fn test_panel_load_messages() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..10)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);
        assert_eq!(panel.messages.len(), 10);
        assert_eq!(panel.cursor, 9);
        assert!(panel.tail_attached);
    }

    #[test]
    fn test_panel_push_message_attached() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..5)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);
        assert_eq!(panel.cursor, 4);

        panel.push_message(make_message("mcp", "tools/list", "catenary"));
        assert_eq!(panel.messages.len(), 6);
        assert_eq!(panel.cursor, 5);
        assert!(panel.tail_attached);
    }

    #[test]
    fn test_panel_push_message_detached() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..5)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        // Navigate up to detach.
        panel.navigate(-1);
        assert!(!panel.tail_attached);
        let cursor_before = panel.cursor;

        panel.push_message(make_message("mcp", "tools/list", "catenary"));
        assert_eq!(panel.messages.len(), 6);
        assert_eq!(panel.cursor, cursor_before);
        assert!(!panel.tail_attached);
    }

    #[test]
    fn test_panel_navigate_up_detaches() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..10)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);
        assert_eq!(panel.cursor, 9);
        assert!(panel.tail_attached);

        panel.navigate(-1);
        assert_eq!(panel.cursor, 8);
        assert!(!panel.tail_attached);
    }

    #[test]
    fn test_panel_navigate_down_past_end_reattaches() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..5)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        // Navigate up to detach.
        panel.navigate(-2);
        assert_eq!(panel.cursor, 2);
        assert!(!panel.tail_attached);

        // Navigate down past the end.
        panel.navigate(1);
        assert_eq!(panel.cursor, 3);
        panel.navigate(1);
        assert_eq!(panel.cursor, 4);
        // One more should clamp and reattach.
        panel.navigate(1);
        assert_eq!(panel.cursor, 4);
        assert!(panel.tail_attached);
    }

    #[test]
    fn test_panel_scroll_to_top() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..20)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        panel.scroll_to_top();
        assert_eq!(panel.cursor, 0);
        assert_eq!(panel.scroll_offset, 0);
        assert!(!panel.tail_attached);
    }

    #[test]
    fn test_panel_scroll_to_bottom() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..20)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        panel.scroll_to_top();
        assert!(!panel.tail_attached);

        panel.scroll_to_bottom();
        assert_eq!(panel.cursor, 19);
        assert!(panel.tail_attached);
    }

    #[test]
    fn test_panel_visible_range() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..100)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        // Move cursor to 50 and snap viewport.
        panel.cursor = 50;
        panel.snap_viewport(20);

        let (start, end) = panel.visible_range(20);
        // Cursor at 50, centered in height 20 → offset ~40.
        assert_eq!(start, 40);
        assert_eq!(end, 60);
    }

    #[test]
    fn test_panel_visible_range_at_top() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..100)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        // Cursor near top — can't center.
        panel.cursor = 3;
        panel.snap_viewport(20);

        let (start, end) = panel.visible_range(20);
        assert_eq!(start, 0);
        assert_eq!(end, 20);
    }

    #[test]
    fn test_panel_visible_range_at_bottom() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..100)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        // Cursor near bottom.
        panel.cursor = 97;
        panel.snap_viewport(20);

        let (start, end) = panel.visible_range(20);
        assert_eq!(end, 100);
        assert_eq!(start, 80);
    }

    #[test]
    fn test_panel_render_messages() {
        let theme = test_theme();
        let icons = test_icons();
        let messages: Vec<SessionMessage> = vec![
            make_message_with_payload(
                "mcp",
                "tools/call",
                "catenary",
                serde_json::json!({"params": {"name": "grep"}}),
            ),
            make_message_with_payload(
                "mcp",
                "tools/call",
                "catenary",
                serde_json::json!({"params": {"name": "glob"}}),
            ),
        ];

        let mut panel = PanelState::new("test1234".to_string(), &theme, &icons);
        panel.load_messages(messages);

        let backend = TestBackend::new(60, 10);
        let mut terminal = Terminal::new(backend).expect("terminal creation");
        terminal
            .draw(|f| {
                let area = f.area();
                render_panel(&panel, area, f.buffer_mut(), true);
            })
            .expect("draw");

        let buf = terminal.backend().buffer().clone();
        let content = buffer_to_string(&buf);

        assert!(content.contains("grep"), "expected grep tool name");
        assert!(content.contains("glob"), "expected glob tool name");
    }

    #[test]
    fn test_panel_render_empty() {
        let theme = test_theme();
        let icons = test_icons();
        let panel = PanelState::new("empty123".to_string(), &theme, &icons);

        let backend = TestBackend::new(60, 10);
        let mut terminal = Terminal::new(backend).expect("terminal creation");
        terminal
            .draw(|f| {
                let area = f.area();
                render_panel(&panel, area, f.buffer_mut(), true);
            })
            .expect("draw");

        let buf = terminal.backend().buffer().clone();
        let content = buffer_to_string(&buf);

        // Should contain the title, no panic.
        assert!(content.contains("Events"), "expected title in empty panel");
    }

    #[test]
    fn test_panel_render_cursor_highlight() {
        let theme = test_theme();
        let icons = test_icons();
        let messages: Vec<SessionMessage> = (0..5)
            .map(|_| {
                make_message_with_payload(
                    "mcp",
                    "tools/call",
                    "catenary",
                    serde_json::json!({"params": {"name": "grep"}}),
                )
            })
            .collect();

        let mut panel = PanelState::new("test1234".to_string(), &theme, &icons);
        panel.load_messages(messages);
        // Set cursor to row 1 (second message in visible area).
        panel.cursor = 1;
        panel.snap_viewport(8);

        let backend = TestBackend::new(60, 10);
        let mut terminal = Terminal::new(backend).expect("terminal creation");
        terminal
            .draw(|f| {
                let area = f.area();
                render_panel(&panel, area, f.buffer_mut(), true);
            })
            .expect("draw");

        let buf = terminal.backend().buffer().clone();

        // The cursor row (row 1 in content = y=2 in buffer since y=0 is border,
        // y=1 is first content row, y=2 is second content row).
        // With cursor at index 1 and scroll_offset 0, cursor is at visible row 1.
        // Inner area starts at y=1 (after top border), so cursor row is at y=2.
        let cursor_y = 2u16;
        let inner_x = 1u16; // after left border
        let cell = &buf[(inner_x, cursor_y)];
        // The selection style uses REVERSED modifier.
        assert!(
            cell.modifier.contains(ratatui::style::Modifier::REVERSED),
            "expected REVERSED modifier on cursor row"
        );
    }

    #[test]
    fn test_panel_language_server_status() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("abc123".to_string(), &theme, &icons);
        panel.messages = vec![
            make_message("lsp", "textDocument/hover", "rust-analyzer"),
            make_message(
                "lsp",
                "textDocument/completion",
                "typescript-language-server",
            ),
        ];

        panel.update_language_servers();
        assert_eq!(panel.language_servers.len(), 2);
        assert_eq!(panel.language_servers[0].name, "rust-analyzer");
        assert_eq!(panel.language_servers[0].state, LsState::Healthy);
        assert_eq!(panel.language_servers[1].name, "typescript-language-server");
        assert_eq!(panel.language_servers[1].state, LsState::Healthy);
    }

    // ── Expansion tests ─────────────────────────────────────────────────

    #[test]
    fn test_flat_lines_no_expansion() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        let messages: Vec<SessionMessage> = (0..5)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        panel.load_messages(messages);

        let flat = panel.flat_lines();
        assert_eq!(flat.len(), 5);
        for (i, fl) in flat.iter().enumerate() {
            assert_eq!(*fl, FlatLine::MessageHeader { message_index: i });
        }
    }

    #[test]
    fn test_flat_lines_one_expanded() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        let messages = vec![
            make_message("lsp", "initialized", "rust-analyzer"),
            make_hook_diag_message("/src/lib.rs", 3),
            make_message("mcp", "tools/list", "catenary"),
        ];
        panel.load_messages(messages);
        panel.expanded.insert(1);

        let flat = panel.flat_lines();
        // 3 headers + detail lines for the expanded hook message
        let detail_count = detail_lines(&panel.messages[1], &theme).len();
        assert!(detail_count > 0, "hook diag message should have details");
        assert_eq!(flat.len(), 3 + detail_count);
        assert_eq!(flat[0], FlatLine::MessageHeader { message_index: 0 });
        assert_eq!(flat[1], FlatLine::MessageHeader { message_index: 1 });
        for i in 0..detail_count {
            assert_eq!(
                flat[2 + i],
                FlatLine::Detail {
                    message_index: 1,
                    detail_index: i
                }
            );
        }
        assert_eq!(
            flat[2 + detail_count],
            FlatLine::MessageHeader { message_index: 2 }
        );
    }

    #[test]
    fn test_toggle_expansion_header() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        let messages = vec![
            make_message("lsp", "initialized", "rust-analyzer"),
            make_lsp_message(),
        ];
        panel.load_messages(messages);
        // Cursor on message 1 (the expandable LSP message).
        panel.cursor = 1;

        panel.toggle_expansion();
        assert!(panel.expanded.contains(&1));

        panel.toggle_expansion();
        assert!(!panel.expanded.contains(&1));
    }

    #[test]
    fn test_toggle_expansion_detail() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        let messages = vec![
            make_message("lsp", "initialized", "rust-analyzer"),
            make_lsp_message(),
            make_message("mcp", "tools/list", "catenary"),
        ];
        panel.load_messages(messages);
        panel.expanded.insert(1);
        // Find a detail line index.
        let flat = panel.flat_lines();
        let detail_pos = flat
            .iter()
            .position(|fl| {
                matches!(
                    fl,
                    FlatLine::Detail {
                        message_index: 1,
                        ..
                    }
                )
            })
            .expect("should have detail lines");
        panel.cursor = detail_pos;

        panel.toggle_expansion();
        assert!(!panel.expanded.contains(&1));
        // After collapse: cursor should be on message 1's header.
        assert_eq!(panel.cursor, 1);
    }

    #[test]
    fn test_toggle_expansion_no_detail() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        // Empty payload → no detail
        let messages = vec![make_message("lsp", "initialized", "rust-analyzer")];
        panel.load_messages(messages);
        panel.cursor = 0;

        panel.toggle_expansion();
        assert!(panel.expanded.is_empty());
    }

    #[test]
    fn test_cursor_walks_detail_lines() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        let messages = vec![
            make_message("lsp", "initialized", "rust-analyzer"),
            make_lsp_message(),
            make_message("mcp", "tools/list", "catenary"),
        ];
        panel.load_messages(messages);
        panel.expanded.insert(1);

        let flat = panel.flat_lines();
        panel.cursor = 0;
        panel.tail_attached = false;

        // Walk through all lines one by one.
        for expected in flat.iter().skip(1) {
            panel.navigate(1);
            let current_flat = panel.flat_lines();
            assert_eq!(current_flat[panel.cursor], *expected);
        }
    }

    #[test]
    fn test_detail_lines_non_empty_payload() {
        let msg = make_lsp_message();
        let theme = test_theme();

        let lines = detail_lines(&msg, &theme);
        // Should have: method [type] header + separator + payload lines
        assert!(lines.len() >= 3, "should have header + sep + payload");
        let hdr: String = lines[0].spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(
            hdr.contains("textDocument/hover"),
            "header should contain method"
        );
        assert!(hdr.contains("[lsp]"), "header should contain type");
        let sep: String = lines[1].spans.iter().map(|s| s.content.as_ref()).collect();
        assert!(sep.contains("\u{2500}"), "second line should be separator");
    }

    #[test]
    fn test_detail_lines_empty_payload() {
        let msg = make_message("lsp", "initialized", "rust-analyzer");
        let theme = test_theme();

        let lines = detail_lines(&msg, &theme);
        assert!(
            lines.is_empty(),
            "empty payload should have no detail lines"
        );
    }

    #[test]
    fn test_visible_range_with_expansion() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        let mut messages: Vec<SessionMessage> = (0..10)
            .map(|_| make_message("lsp", "initialized", "rust-analyzer"))
            .collect();
        // Replace message 5 with an expandable one.
        messages[5] = make_lsp_message();
        panel.load_messages(messages);
        panel.expanded.insert(5);

        let flat = panel.flat_lines();
        let detail_count = detail_lines(&panel.messages[5], &theme).len();
        // 10 headers + detail lines for message 5
        assert_eq!(flat.len(), 10 + detail_count);

        // Set cursor to 0, snap viewport.
        panel.cursor = 0;
        panel.snap_viewport(10);
        let (start, end) = panel.visible_range(10);
        assert_eq!(start, 0);
        assert_eq!(end, 10);
    }

    #[test]
    fn test_render_expanded_message() {
        let theme = test_theme();
        let icons = test_icons();
        let messages = vec![make_hook_diag_message("/src/lib.rs", 2)];

        let mut panel = PanelState::new("test1234".to_string(), &theme, &icons);
        panel.load_messages(messages);
        panel.expanded.insert(0);
        panel.cursor = 0;
        panel.snap_viewport(8);

        let backend = TestBackend::new(60, 10);
        let mut terminal = Terminal::new(backend).expect("terminal creation");
        terminal
            .draw(|f| {
                let area = f.area();
                render_panel(&panel, area, f.buffer_mut(), true);
            })
            .expect("draw");

        let buf = terminal.backend().buffer().clone();
        let content = buffer_to_string(&buf);

        // Header should show the diagnostics summary.
        assert!(content.contains("lib.rs"), "expected file name in header");
        // Detail lines should contain the payload.
        assert!(content.contains("post-tool"), "expected method in detail");
    }

    #[test]
    fn test_has_detail_non_empty_payload() {
        let theme = test_theme();
        let icons = test_icons();
        let mut panel = PanelState::new("test".to_string(), &theme, &icons);
        panel.messages = vec![
            make_lsp_message(),
            make_message("lsp", "initialized", "rust-analyzer"),
        ];
        assert!(panel.has_detail(0), "non-empty payload should have detail");
        assert!(!panel.has_detail(1), "empty payload should not have detail");
    }
}