bmux_terminal_protocol 0.0.1-alpha.1

Shared terminal query/reply protocol engine for bmux
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
#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]

use serde::{Deserialize, Serialize};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex};
use std::time::{SystemTime, UNIX_EPOCH};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ParseState {
    Ground,
    Esc,
    Csi,
    Osc,
    OscEsc,
    Dcs,
    DcsEsc,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtocolProfile {
    Bmux,
    Xterm,
    Screen,
    Conservative,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProtocolDirection {
    Query,
    Reply,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProtocolTraceEvent {
    pub timestamp_ms: u128,
    pub pane_id: Option<u16>,
    pub profile: String,
    pub family: String,
    pub name: String,
    pub direction: ProtocolDirection,
    pub raw_hex: String,
    pub decoded: String,
}

#[derive(Debug, Default)]
pub struct ProtocolTraceBuffer {
    capacity: usize,
    dropped: usize,
    events: VecDeque<ProtocolTraceEvent>,
}

impl ProtocolTraceBuffer {
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            capacity: capacity.max(1),
            dropped: 0,
            events: VecDeque::new(),
        }
    }

    fn push(&mut self, event: ProtocolTraceEvent) {
        if self.events.len() == self.capacity {
            let _ = self.events.pop_front();
            self.dropped = self.dropped.saturating_add(1);
        }
        self.events.push_back(event);
    }

    #[must_use]
    pub fn snapshot(&self, limit: usize) -> Vec<ProtocolTraceEvent> {
        if limit == 0 {
            return Vec::new();
        }
        let len = self.events.len();
        let start = len.saturating_sub(limit);
        self.events.iter().skip(start).cloned().collect()
    }

    #[must_use]
    pub const fn dropped(&self) -> usize {
        self.dropped
    }
}

pub type SharedProtocolTraceBuffer = Arc<Mutex<ProtocolTraceBuffer>>;

#[derive(Debug)]
pub struct TerminalProtocolEngine {
    state: ParseState,
    csi_buffer: Vec<u8>,
    osc_buffer: Vec<u8>,
    dcs_buffer: Vec<u8>,
    profile: ProtocolProfile,
    pane_id: Option<u16>,
    trace: Option<SharedProtocolTraceBuffer>,
    /// Stack of kitty keyboard enhancement flags pushed by the inner program.
    #[cfg(feature = "kitty-keyboard")]
    keyboard_flag_stack: Vec<u32>,
}

impl TerminalProtocolEngine {
    #[must_use]
    pub fn new(profile: ProtocolProfile) -> Self {
        Self {
            state: ParseState::Ground,
            csi_buffer: Vec::new(),
            osc_buffer: Vec::new(),
            dcs_buffer: Vec::new(),
            profile,
            pane_id: None,
            trace: None,
            #[cfg(feature = "kitty-keyboard")]
            keyboard_flag_stack: Vec::new(),
        }
    }

    #[must_use]
    pub fn with_trace(
        profile: ProtocolProfile,
        pane_id: u16,
        trace: SharedProtocolTraceBuffer,
    ) -> Self {
        let mut this = Self::new(profile);
        this.pane_id = Some(pane_id);
        this.trace = Some(trace);
        this
    }

    #[must_use]
    pub fn process_output(&mut self, bytes: &[u8], cursor_pos: (u16, u16)) -> Vec<u8> {
        let mut replies = Vec::new();

        for byte in bytes {
            match self.state {
                ParseState::Ground => {
                    if *byte == 0x1b {
                        self.state = ParseState::Esc;
                    }
                }
                ParseState::Esc => {
                    if *byte == b'[' {
                        self.state = ParseState::Csi;
                        self.csi_buffer.clear();
                    } else if *byte == b']' {
                        self.state = ParseState::Osc;
                        self.osc_buffer.clear();
                    } else if *byte == b'P' {
                        self.state = ParseState::Dcs;
                        self.dcs_buffer.clear();
                    } else if *byte == 0x1b {
                        self.state = ParseState::Esc;
                    } else {
                        self.state = ParseState::Ground;
                    }
                }
                ParseState::Csi => {
                    if *byte == 0x1b {
                        self.state = ParseState::Esc;
                        self.csi_buffer.clear();
                        continue;
                    }

                    self.csi_buffer.push(*byte);

                    if byte.is_ascii_alphabetic() {
                        // First try kitty keyboard protocol sequences (state-changing).
                        #[cfg(feature = "kitty-keyboard")]
                        if *byte == b'u'
                            && let Some((name, reply)) =
                                self.handle_kitty_keyboard_csi(&self.csi_buffer.clone())
                        {
                            self.trace_event(
                                "csi",
                                name,
                                ProtocolDirection::Query,
                                &self.csi_buffer,
                            );
                            if !reply.is_empty() {
                                self.trace_event("csi", name, ProtocolDirection::Reply, &reply);
                                replies.extend_from_slice(&reply);
                            }
                            self.state = ParseState::Ground;
                            self.csi_buffer.clear();
                            continue;
                        }

                        if let Some((name, reply)) =
                            csi_query_reply(&self.csi_buffer, cursor_pos, self.profile)
                        {
                            self.trace_event(
                                "csi",
                                name,
                                ProtocolDirection::Query,
                                &self.csi_buffer,
                            );
                            self.trace_event("csi", name, ProtocolDirection::Reply, &reply);
                            replies.extend_from_slice(&reply);
                        }
                        self.state = ParseState::Ground;
                        self.csi_buffer.clear();
                    } else if self.csi_buffer.len() > 32 {
                        self.state = ParseState::Ground;
                        self.csi_buffer.clear();
                    }
                }
                ParseState::Osc => {
                    if *byte == 0x07 {
                        if let Some((name, reply)) = osc_query_reply(&self.osc_buffer, self.profile)
                        {
                            self.trace_event(
                                "osc",
                                name,
                                ProtocolDirection::Query,
                                &self.osc_buffer,
                            );
                            self.trace_event("osc", name, ProtocolDirection::Reply, &reply);
                            replies.extend_from_slice(&reply);
                        }
                        self.state = ParseState::Ground;
                        self.osc_buffer.clear();
                    } else if *byte == 0x1b {
                        self.state = ParseState::OscEsc;
                    } else {
                        self.osc_buffer.push(*byte);
                        if self.osc_buffer.len() > 512 {
                            self.state = ParseState::Ground;
                            self.osc_buffer.clear();
                        }
                    }
                }
                ParseState::OscEsc => {
                    if *byte == b'\\' {
                        if let Some((name, reply)) = osc_query_reply(&self.osc_buffer, self.profile)
                        {
                            self.trace_event(
                                "osc",
                                name,
                                ProtocolDirection::Query,
                                &self.osc_buffer,
                            );
                            self.trace_event("osc", name, ProtocolDirection::Reply, &reply);
                            replies.extend_from_slice(&reply);
                        }
                        self.state = ParseState::Ground;
                        self.osc_buffer.clear();
                    } else {
                        self.state = ParseState::Ground;
                        self.osc_buffer.clear();
                    }
                }
                ParseState::Dcs => {
                    if *byte == 0x1b {
                        self.state = ParseState::DcsEsc;
                    } else {
                        self.dcs_buffer.push(*byte);
                        if self.dcs_buffer.len() > 512 {
                            self.state = ParseState::Ground;
                            self.dcs_buffer.clear();
                        }
                    }
                }
                ParseState::DcsEsc => {
                    if *byte == b'\\' {
                        if let Some((name, reply)) = dcs_query_reply(&self.dcs_buffer, self.profile)
                        {
                            self.trace_event(
                                "dcs",
                                name,
                                ProtocolDirection::Query,
                                &self.dcs_buffer,
                            );
                            self.trace_event("dcs", name, ProtocolDirection::Reply, &reply);
                            replies.extend_from_slice(&reply);
                        }
                        self.state = ParseState::Ground;
                        self.dcs_buffer.clear();
                    } else {
                        self.state = ParseState::Ground;
                        self.dcs_buffer.clear();
                    }
                }
            }
        }

        replies
    }

    fn trace_event(&self, family: &str, name: &str, direction: ProtocolDirection, bytes: &[u8]) {
        let Some(trace) = &self.trace else {
            return;
        };
        let event = ProtocolTraceEvent {
            timestamp_ms: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map_or(0, |dur| dur.as_millis()),
            pane_id: self.pane_id,
            profile: protocol_profile_name(self.profile).to_string(),
            family: family.to_string(),
            name: name.to_string(),
            direction,
            raw_hex: bytes
                .iter()
                .map(|byte| format!("{byte:02x}"))
                .collect::<Vec<_>>()
                .join(""),
            decoded: String::from_utf8_lossy(bytes).into_owned(),
        };
        if let Ok(mut guard) = trace.lock() {
            guard.push(event);
        }
    }

    /// Handle kitty keyboard protocol CSI sequences.
    ///
    /// Returns `Some((name, reply_bytes))` if the sequence was recognized.
    /// Push/pop sequences return an empty reply (they are state changes only).
    #[cfg(feature = "kitty-keyboard")]
    fn handle_kitty_keyboard_csi(&mut self, sequence: &[u8]) -> Option<(&'static str, Vec<u8>)> {
        // Only Bmux profile responds to kitty keyboard queries.
        if !matches!(self.profile, ProtocolProfile::Bmux) {
            return None;
        }

        match sequence {
            // Query: CSI ? u -> reply with current flags
            b"?u" => {
                let flags = self.keyboard_flag_stack.last().copied().unwrap_or(0);
                Some((
                    "csi_kitty_keyboard_query",
                    format!("\x1b[?{flags}u").into_bytes(),
                ))
            }
            // Pop: CSI < u -> pop one level from the stack
            b"<u" => {
                self.keyboard_flag_stack.pop();
                Some(("csi_kitty_keyboard_pop", Vec::new()))
            }
            // Push: CSI > {flags} u -> push flags onto the stack
            _ if sequence.starts_with(b">") && sequence.ends_with(b"u") => {
                let flags_str = &sequence[1..sequence.len() - 1];
                let flags = std::str::from_utf8(flags_str)
                    .ok()
                    .and_then(|s| s.parse::<u32>().ok())
                    .unwrap_or(0);
                self.keyboard_flag_stack.push(flags);
                Some(("csi_kitty_keyboard_push", Vec::new()))
            }
            _ => None,
        }
    }

    /// Get the current kitty keyboard enhancement flags for this pane.
    ///
    /// Returns 0 if no flags have been pushed.
    #[cfg(feature = "kitty-keyboard")]
    #[must_use]
    pub fn keyboard_enhancement_flags(&self) -> u32 {
        self.keyboard_flag_stack.last().copied().unwrap_or(0)
    }
}

impl Default for TerminalProtocolEngine {
    fn default() -> Self {
        Self::new(ProtocolProfile::Conservative)
    }
}

#[must_use]
pub fn supported_query_names() -> &'static [&'static str] {
    &[
        "csi_primary_da",
        "csi_secondary_da",
        "csi_dsr_status_report",
        "csi_dsr_cursor_position",
        "csi_dec_dsr_status_report",
        "csi_dec_dsr_cursor_position",
        "csi_dec_mode_report",
        #[cfg(feature = "kitty-keyboard")]
        "csi_kitty_keyboard_query",
        #[cfg(feature = "kitty-keyboard")]
        "csi_kitty_keyboard_push",
        #[cfg(feature = "kitty-keyboard")]
        "csi_kitty_keyboard_pop",
        "osc_color_query",
        "dcs_xtgettcap_query",
        "dcs_decrqss_query",
    ]
}

#[must_use]
pub fn protocol_profile_name(profile: ProtocolProfile) -> &'static str {
    match profile {
        ProtocolProfile::Bmux => "bmux",
        ProtocolProfile::Xterm => "xterm",
        ProtocolProfile::Screen => "screen",
        ProtocolProfile::Conservative => "conservative",
    }
}

#[must_use]
pub fn primary_da_for_profile(profile: ProtocolProfile) -> &'static [u8] {
    primary_da_response(profile)
}

#[must_use]
pub fn secondary_da_for_profile(profile: ProtocolProfile) -> &'static [u8] {
    secondary_da_response(profile)
}

#[must_use]
pub fn protocol_profile_for_term(term: &str) -> ProtocolProfile {
    let normalized = term.trim().to_ascii_lowercase();
    match normalized.as_str() {
        "bmux" | "bmux-256color" => ProtocolProfile::Bmux,
        "screen" | "screen-256color" | "tmux" | "tmux-256color" => ProtocolProfile::Screen,
        "xterm" | "xterm-color" | "xterm-256color" | "xterm-direct" => ProtocolProfile::Xterm,
        _ => ProtocolProfile::Conservative,
    }
}

fn csi_query_reply(
    sequence: &[u8],
    cursor_pos: (u16, u16),
    profile: ProtocolProfile,
) -> Option<(&'static str, Vec<u8>)> {
    match sequence {
        b"c" | b"0c" => Some(("csi_primary_da", primary_da_response(profile).to_vec())),
        b">c" => Some(("csi_secondary_da", secondary_da_response(profile).to_vec())),
        b"5n" => Some(("csi_dsr_status_report", b"\x1b[0n".to_vec())),
        b"6n" => Some(("csi_dsr_cursor_position", dsr_cursor_response(cursor_pos))),
        b"?5n" => Some(("csi_dec_dsr_status_report", b"\x1b[?0n".to_vec())),
        b"?6n" => Some((
            "csi_dec_dsr_cursor_position",
            dec_dsr_cursor_response(cursor_pos),
        )),
        _ if sequence.starts_with(b"?") && sequence.ends_with(b"$p") => {
            dec_mode_report_response(sequence, profile).map(|reply| ("csi_dec_mode_report", reply))
        }
        _ => None,
    }
}

fn dec_mode_report_response(sequence: &[u8], profile: ProtocolProfile) -> Option<Vec<u8>> {
    let mode_bytes = &sequence[1..sequence.len().saturating_sub(2)];
    let modes = parse_mode_list(mode_bytes)?;
    let mut out = Vec::new();
    for mode in modes {
        let status = dec_mode_status(profile, mode);
        out.extend_from_slice(format!("\x1b[?{mode};{status}$y").as_bytes());
    }
    (!out.is_empty()).then_some(out)
}

fn parse_mode_list(bytes: &[u8]) -> Option<Vec<u16>> {
    let text = std::str::from_utf8(bytes).ok()?;
    let mut out = Vec::new();
    for token in text.split(';') {
        if token.is_empty() {
            continue;
        }
        out.push(token.parse::<u16>().ok()?);
    }
    Some(out)
}

fn dec_mode_status(profile: ProtocolProfile, mode: u16) -> u8 {
    match profile {
        ProtocolProfile::Bmux => match mode {
            1 => 2,
            7 => 1,
            25 => 1,
            1000 => 2,
            1002 => 2,
            1006 => 2,
            1004 => 2,
            2004 => 2,
            1049 => 2,
            #[cfg(feature = "kitty-keyboard")]
            2048 => 1, // Kitty keyboard protocol supported
            _ => 0,
        },
        ProtocolProfile::Xterm => match mode {
            1 => 2,
            7 => 1,
            25 => 1,
            1000 => 2,
            1002 => 2,
            1006 => 2,
            1004 => 2,
            2004 => 2,
            1049 => 2,
            _ => 0,
        },
        ProtocolProfile::Screen => match mode {
            1 => 2,
            7 => 1,
            25 => 1,
            1049 => 2,
            _ => 0,
        },
        ProtocolProfile::Conservative => match mode {
            7 => 1,
            25 => 1,
            _ => 0,
        },
    }
}

fn osc_query_reply(sequence: &[u8], profile: ProtocolProfile) -> Option<(&'static str, Vec<u8>)> {
    match sequence {
        b"10;?" => Some((
            "osc_color_query",
            format!("\x1b]10;{}\x1b\\", osc_foreground_color(profile)).into_bytes(),
        )),
        b"11;?" => Some((
            "osc_color_query",
            format!("\x1b]11;{}\x1b\\", osc_background_color(profile)).into_bytes(),
        )),
        _ => None,
    }
}

fn osc_foreground_color(profile: ProtocolProfile) -> &'static str {
    match profile {
        ProtocolProfile::Bmux => "rgb:bfbf/bfbf/bfbf",
        ProtocolProfile::Xterm | ProtocolProfile::Screen => "rgb:ffff/ffff/ffff",
        ProtocolProfile::Conservative => "rgb:c0c0/c0c0/c0c0",
    }
}

fn osc_background_color(profile: ProtocolProfile) -> &'static str {
    match profile {
        ProtocolProfile::Bmux => "rgb:1a1a/1a1a/1a1a",
        ProtocolProfile::Xterm | ProtocolProfile::Screen | ProtocolProfile::Conservative => {
            "rgb:0000/0000/0000"
        }
    }
}

fn dcs_query_reply(sequence: &[u8], profile: ProtocolProfile) -> Option<(&'static str, Vec<u8>)> {
    if let Some(hex_keys) = sequence.strip_prefix(b"+q") {
        return Some(("dcs_xtgettcap_query", xtgettcap_reply(hex_keys, profile)));
    }
    if let Some(request) = sequence.strip_prefix(b"$q") {
        return Some(("dcs_decrqss_query", decrqss_reply(request)));
    }
    None
}

fn xtgettcap_reply(hex_keys: &[u8], profile: ProtocolProfile) -> Vec<u8> {
    let mut parts = Vec::new();
    for key_hex in hex_keys.split(|byte| *byte == b';') {
        if key_hex.is_empty() {
            continue;
        }
        if let Some(value_hex) = xtgettcap_value_hex(key_hex, profile) {
            parts.push(format!(
                "{}={}",
                String::from_utf8_lossy(key_hex),
                String::from_utf8_lossy(value_hex)
            ));
        }
    }

    if parts.is_empty() {
        b"\x1bP0+r\x1b\\".to_vec()
    } else {
        format!("\x1bP1+r{}\x1b\\", parts.join(";"))
            .as_bytes()
            .to_vec()
    }
}

fn xtgettcap_value_hex(key_hex: &[u8], profile: ProtocolProfile) -> Option<&'static [u8]> {
    match key_hex {
        b"5443" => Some(if matches!(profile, ProtocolProfile::Conservative) {
            b"30"
        } else {
            b"31"
        }),
        b"636f" => Some(b"323536"),
        b"6b42" => Some(b"1b5b5a"),
        b"6b44" => Some(b"1b5b44"),
        _ => None,
    }
}

fn decrqss_reply(request: &[u8]) -> Vec<u8> {
    match request {
        b"m" => b"\x1bP1$r0m\x1b\\".to_vec(),
        b" q" => b"\x1bP1$r q\x1b\\".to_vec(),
        _ => b"\x1bP0$r\x1b\\".to_vec(),
    }
}

fn primary_da_response(profile: ProtocolProfile) -> &'static [u8] {
    match profile {
        ProtocolProfile::Bmux | ProtocolProfile::Xterm | ProtocolProfile::Conservative => {
            b"\x1b[?1;2c"
        }
        ProtocolProfile::Screen => b"\x1b[?64;1;2;6;9;15;18;21;22c",
    }
}

fn secondary_da_response(profile: ProtocolProfile) -> &'static [u8] {
    match profile {
        ProtocolProfile::Bmux => b"\x1b[>84;0;0c",
        ProtocolProfile::Xterm => b"\x1b[>0;115;0c",
        ProtocolProfile::Screen => b"\x1b[>83;40003;0c",
        ProtocolProfile::Conservative => b"\x1b[>0;1000;0c",
    }
}

fn dsr_cursor_response(cursor_pos: (u16, u16)) -> Vec<u8> {
    let (row, col) = (
        cursor_pos.0.saturating_add(1),
        cursor_pos.1.saturating_add(1),
    );
    format!("\x1b[{row};{col}R").into_bytes()
}

fn dec_dsr_cursor_response(cursor_pos: (u16, u16)) -> Vec<u8> {
    let (row, col) = (
        cursor_pos.0.saturating_add(1),
        cursor_pos.1.saturating_add(1),
    );
    format!("\x1b[?{row};{col}R").into_bytes()
}

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

    #[test]
    fn protocol_profile_for_term_recognizes_common_aliases() {
        assert_eq!(protocol_profile_for_term("bmux"), ProtocolProfile::Bmux);
        assert_eq!(protocol_profile_for_term("tmux"), ProtocolProfile::Screen);
        assert_eq!(protocol_profile_for_term("xterm"), ProtocolProfile::Xterm);
        assert_eq!(
            protocol_profile_for_term("xterm-direct"),
            ProtocolProfile::Xterm
        );
        assert_eq!(
            protocol_profile_for_term("weird-term"),
            ProtocolProfile::Conservative
        );
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_query_empty_stack() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        let reply = engine.process_output(b"\x1b[?u", (0, 0));
        assert_eq!(reply, b"\x1b[?0u");
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_push_then_query() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        // Push flags=1 (disambiguate escape codes)
        let reply = engine.process_output(b"\x1b[>1u", (0, 0));
        assert!(reply.is_empty(), "push should not produce a reply");
        // Query should return 1
        let reply = engine.process_output(b"\x1b[?u", (0, 0));
        assert_eq!(reply, b"\x1b[?1u");
        assert_eq!(engine.keyboard_enhancement_flags(), 1);
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_push_push_pop_query() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        // Push flags=1
        let _ = engine.process_output(b"\x1b[>1u", (0, 0));
        // Push flags=3
        let _ = engine.process_output(b"\x1b[>3u", (0, 0));
        assert_eq!(engine.keyboard_enhancement_flags(), 3);
        // Pop -> should return to 1
        let _ = engine.process_output(b"\x1b[<u", (0, 0));
        let reply = engine.process_output(b"\x1b[?u", (0, 0));
        assert_eq!(reply, b"\x1b[?1u");
        assert_eq!(engine.keyboard_enhancement_flags(), 1);
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_pop_empty_stack() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        // Pop from empty stack should be a no-op
        let reply = engine.process_output(b"\x1b[<u", (0, 0));
        assert!(reply.is_empty());
        assert_eq!(engine.keyboard_enhancement_flags(), 0);
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_not_supported_on_xterm_profile() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Xterm);
        let reply = engine.process_output(b"\x1b[?u", (0, 0));
        assert!(
            reply.is_empty(),
            "xterm profile should not respond to kitty queries"
        );
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_not_supported_on_conservative_profile() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Conservative);
        let reply = engine.process_output(b"\x1b[?u", (0, 0));
        assert!(reply.is_empty());
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn dec_mode_2048_supported_on_bmux_profile() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        let reply = engine.process_output(b"\x1b[?2048$p", (0, 0));
        // Mode 2048 should be reported as "1" (set) for Bmux profile.
        assert_eq!(reply, b"\x1b[?2048;1$y");
    }

    #[test]
    fn dec_mode_2048_unknown_on_xterm_profile() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Xterm);
        let reply = engine.process_output(b"\x1b[?2048$p", (0, 0));
        // Mode 2048 should be reported as "0" (not recognized) for Xterm profile.
        assert_eq!(reply, b"\x1b[?2048;0$y");
    }

    #[test]
    fn primary_da_replies() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        let reply = engine.process_output(b"\x1b[c", (0, 0));
        assert_eq!(reply, b"\x1b[?1;2c");
    }

    #[test]
    fn secondary_da_replies() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        let reply = engine.process_output(b"\x1b[>c", (0, 0));
        assert_eq!(reply, b"\x1b[>84;0;0c");
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn kitty_keyboard_interleaved_with_normal_output() {
        let mut engine = TerminalProtocolEngine::new(ProtocolProfile::Bmux);
        // Simulate normal output with embedded kitty keyboard push
        let reply = engine.process_output(b"Hello\x1b[>1uWorld", (0, 0));
        assert!(reply.is_empty()); // Push produces no reply
        assert_eq!(engine.keyboard_enhancement_flags(), 1);
    }

    #[cfg(feature = "kitty-keyboard")]
    #[test]
    fn supported_query_names_includes_kitty() {
        let names = supported_query_names();
        assert!(names.contains(&"csi_kitty_keyboard_query"));
        assert!(names.contains(&"csi_kitty_keyboard_push"));
        assert!(names.contains(&"csi_kitty_keyboard_pop"));
    }
}