clientapi-pve 2026.5.24

Generated from apidoc.js. NOT an official Proxmox specification. See https://pve.proxmox.com/pve-docs/api-viewer/ for the upstream documentation.
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
// Auto-generated by scripts/sdk-facades.ts. Do not edit.
//
// Proxmox VE WebSocket helpers. Feature-gated behind `extras`.
//
// Two console flows share the same two-step pipeline:
//
//   1. POST {prefix}/{termproxy|vncproxy|vncshell} → ticket + port
//   2. GET  {prefix}/vncwebsocket?port=…&vncticket=…  → HTTP 101 Upgrade
//
// Framing on the upgraded socket differs by parent POST:
//   - termproxy → text framing (`0:LEN:MSG`, `1:COLS:ROWS:`, `2` ping)
//   - vncproxy / vncshell → raw binary RFB
//
// Architecture (SOLID):
//
//   - `TicketIssuer` (Strategy) — one impl per (flow × resource) combo.
//     Adding a new console flow is one new impl with no edits to the
//     dispatcher (Open/Closed).
//   - `FrameCodec` (Strategy) — text vs binary framing.
//   - `AuthAttacher` (Strategy) — composes Cookie / API-token.
//   - `WebSocketTransport` (Adapter) — wraps tokio-tungstenite.
//   - `ConsoleConnector` (Template Method) — orchestrates the pipeline.
//   - `TerminalSession` / `VncSession` — delegate framing to a codec.
//
// Enable with: `cargo build --features extras`.

use crate::apis::configuration::Configuration;
use crate::apis::{lxc_api, nodes_api, qemu_api};

use async_trait::async_trait;
use futures_util::{SinkExt, StreamExt};
use std::sync::Arc;
use thiserror::Error;
use tokio::net::TcpStream;
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
use tokio_tungstenite::tungstenite::http::HeaderValue;
use tokio_tungstenite::tungstenite::Message;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};

pub type WsStream = WebSocketStream<MaybeTlsStream<TcpStream>>;

// ─────────────────────────────────────────────────────────────────────────────
// Public types
// ─────────────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub enum TerminalTarget {
    Node { node: String },
    Qemu { node: String, vmid: i32 },
    Lxc { node: String, vmid: i32 },
}

#[derive(Debug, Clone)]
pub enum VncTarget {
    NodeShell { node: String },
    Qemu { node: String, vmid: i32 },
    Lxc { node: String, vmid: i32 },
}

#[derive(Debug, Clone)]
pub struct ConsoleTicket {
    pub ticket: String,
    pub port: u16,
    pub user: String,
    pub upid: Option<String>,
    pub cert: Option<String>,
}

#[derive(Debug, Error)]
pub enum WsError {
    #[error("api call failed: {0}")]
    Api(String),
    #[error("ticket payload missing required fields")]
    BadTicket,
    #[error("invalid base_path: {0}")]
    BadBasePath(String),
    #[error("websocket protocol error: {0}")]
    Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
    #[error("invalid request header: {0}")]
    Header(#[from] tokio_tungstenite::tungstenite::http::header::InvalidHeaderValue),
    #[error("no issuer registered for the requested flow")]
    UnsupportedTarget,
}

// ─────────────────────────────────────────────────────────────────────────────
// FrameCodec (Strategy)
// ─────────────────────────────────────────────────────────────────────────────

pub trait FrameCodec: Send {
    type In;
    type Out;
    fn encode(&self, payload: Self::In) -> Vec<u8>;
    fn decode(&mut self, chunk: &[u8], out: &mut Vec<Self::Out>);
}

/// PVE termproxy text framing: `0:LEN:PAYLOAD` (data),
/// `1:COLS:ROWS:` (resize), `2` (ping).
#[derive(Default)]
pub struct TextFrameCodec {
    pending: String,
}

impl TextFrameCodec {
    pub const PING_FRAME: &'static str = "2";

    pub fn encode_resize(cols: u32, rows: u32) -> String {
        format!("1:{}:{}:", cols, rows)
    }
}

impl FrameCodec for TextFrameCodec {
    type In = String;
    type Out = String;

    fn encode(&self, payload: Self::In) -> Vec<u8> {
        format!("0:{}:{}\n", payload.len(), payload).into_bytes()
    }

    fn decode(&mut self, chunk: &[u8], out: &mut Vec<Self::Out>) {
        // PVE termproxy is asymmetric: client→server is framed
        // (`0:LEN:DATA`), server→client is the underlying TTY output
        // without framing. Surface every chunk to the caller verbatim.
        if chunk.is_empty() {
            return;
        }
        out.push(String::from_utf8_lossy(chunk).into_owned());
    }
}

/// VNC: raw RFB protocol bytes pass through unchanged.
#[derive(Default)]
pub struct BinaryFrameCodec;

impl FrameCodec for BinaryFrameCodec {
    type In = Vec<u8>;
    type Out = Vec<u8>;

    fn encode(&self, payload: Self::In) -> Vec<u8> {
        payload
    }

    fn decode(&mut self, chunk: &[u8], out: &mut Vec<Self::Out>) {
        if !chunk.is_empty() {
            out.push(chunk.to_vec());
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// TicketIssuer (Strategy + Open/Closed)
// ─────────────────────────────────────────────────────────────────────────────

#[async_trait]
pub trait TerminalTicketIssuer: Send + Sync {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError>;
}

#[async_trait]
pub trait VncTicketIssuer: Send + Sync {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError>;
}

struct NodeTermproxyIssuer { node: String }
struct QemuTermproxyIssuer { node: String, vmid: i32 }
struct LxcTermproxyIssuer { node: String, vmid: i32 }
struct NodeVncShellIssuer { node: String }
struct QemuVncproxyIssuer { node: String, vmid: i32 }
struct LxcVncproxyIssuer { node: String, vmid: i32 }

#[async_trait]
impl TerminalTicketIssuer for NodeTermproxyIssuer {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError> {
        // PVE rejects POSTs with `Content-Type: application/json` and an
        // empty body ("malformed JSON string..."). Always send an empty
        // body struct (the generator emits a constructor that defaults
        // every field to None).
        let resp = nodes_api::nodes_termproxy(cfg, &self.node, Some(crate::models::NodesTermproxyRequest::new()))
            .await
            .map_err(|e| WsError::Api(format!("{e}")))?;
        Ok((parse_ticket(&resp.data)?, format!("/nodes/{}", urlencoding::encode(&self.node))))
    }
}

#[async_trait]
impl TerminalTicketIssuer for QemuTermproxyIssuer {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError> {
        let resp = qemu_api::qemu_termproxy(cfg, &self.node, self.vmid, Some(crate::models::QemuTermproxyRequest::new()))
            .await
            .map_err(|e| WsError::Api(format!("{e}")))?;
        Ok((parse_ticket(&resp.data)?, format!("/nodes/{}/qemu/{}", urlencoding::encode(&self.node), self.vmid)))
    }
}

#[async_trait]
impl TerminalTicketIssuer for LxcTermproxyIssuer {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError> {
        let resp = lxc_api::lxc_termproxy(cfg, &self.node, self.vmid)
            .await
            .map_err(|e| WsError::Api(format!("{e}")))?;
        Ok((parse_ticket(&resp.data)?, format!("/nodes/{}/lxc/{}", urlencoding::encode(&self.node), self.vmid)))
    }
}

#[async_trait]
impl VncTicketIssuer for NodeVncShellIssuer {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError> {
        let resp = nodes_api::nodes_vncshell(cfg, &self.node, None)
            .await
            .map_err(|e| WsError::Api(format!("{e}")))?;
        Ok((parse_ticket(&resp.data)?, format!("/nodes/{}", urlencoding::encode(&self.node))))
    }
}

#[async_trait]
impl VncTicketIssuer for QemuVncproxyIssuer {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError> {
        let resp = qemu_api::qemu_vncproxy(cfg, &self.node, self.vmid, None)
            .await
            .map_err(|e| WsError::Api(format!("{e}")))?;
        Ok((parse_ticket(&resp.data)?, format!("/nodes/{}/qemu/{}", urlencoding::encode(&self.node), self.vmid)))
    }
}

#[async_trait]
impl VncTicketIssuer for LxcVncproxyIssuer {
    async fn issue(&self, cfg: &Configuration) -> Result<(ConsoleTicket, String), WsError> {
        let resp = lxc_api::lxc_vncproxy(cfg, &self.node, self.vmid, None)
            .await
            .map_err(|e| WsError::Api(format!("{e}")))?;
        Ok((parse_ticket(&resp.data)?, format!("/nodes/{}/lxc/{}", urlencoding::encode(&self.node), self.vmid)))
    }
}

// Factory functions — Open/Closed: add a new (target × flow) combo by
// adding one new impl + match arm; never edit the existing arms.
fn terminal_issuer_for(target: TerminalTarget) -> Box<dyn TerminalTicketIssuer> {
    match target {
        TerminalTarget::Node { node } => Box::new(NodeTermproxyIssuer { node }),
        TerminalTarget::Qemu { node, vmid } => Box::new(QemuTermproxyIssuer { node, vmid }),
        TerminalTarget::Lxc { node, vmid } => Box::new(LxcTermproxyIssuer { node, vmid }),
    }
}

fn vnc_issuer_for(target: VncTarget) -> Box<dyn VncTicketIssuer> {
    match target {
        VncTarget::NodeShell { node } => Box::new(NodeVncShellIssuer { node }),
        VncTarget::Qemu { node, vmid } => Box::new(QemuVncproxyIssuer { node, vmid }),
        VncTarget::Lxc { node, vmid } => Box::new(LxcVncproxyIssuer { node, vmid }),
    }
}

fn parse_ticket(value: &Option<serde_json::Value>) -> Result<ConsoleTicket, WsError> {
    let v = value.as_ref().ok_or(WsError::BadTicket)?;
    let obj = v.as_object().ok_or(WsError::BadTicket)?;
    let ticket = obj.get("ticket").and_then(|x| x.as_str()).ok_or(WsError::BadTicket)?.to_string();
    let port = obj
        .get("port")
        .and_then(|p| p.as_u64().or_else(|| p.as_str().and_then(|s| s.parse::<u64>().ok())))
        .ok_or(WsError::BadTicket)? as u16;
    let user = obj.get("user").and_then(|x| x.as_str()).unwrap_or("").to_string();
    let upid = obj.get("upid").and_then(|x| x.as_str()).map(String::from);
    let cert = obj.get("cert").and_then(|x| x.as_str()).map(String::from);
    Ok(ConsoleTicket { ticket, port, user, upid, cert })
}

// ─────────────────────────────────────────────────────────────────────────────
// AuthAttacher (Strategy)
// ─────────────────────────────────────────────────────────────────────────────

pub trait AuthAttacher: Send + Sync {
    fn apply(&self, request: &mut tokio_tungstenite::tungstenite::http::Request<()>, cfg: &Configuration) -> Result<(), WsError>;
}

pub struct CompositeAuthAttacher {
    inner: Vec<Box<dyn AuthAttacher>>,
}

impl CompositeAuthAttacher {
    pub fn new(inner: Vec<Box<dyn AuthAttacher>>) -> Self { Self { inner } }
}

impl AuthAttacher for CompositeAuthAttacher {
    fn apply(&self, req: &mut tokio_tungstenite::tungstenite::http::Request<()>, cfg: &Configuration) -> Result<(), WsError> {
        for a in &self.inner { a.apply(req, cfg)?; }
        Ok(())
    }
}

struct TokenAuthAttacher;
impl AuthAttacher for TokenAuthAttacher {
    fn apply(&self, req: &mut tokio_tungstenite::tungstenite::http::Request<()>, cfg: &Configuration) -> Result<(), WsError> {
        if let Some(token) = cfg.bearer_access_token.as_ref() {
            let header = HeaderValue::from_str(&format!("PVEAPIToken={}", token))?;
            req.headers_mut().insert("authorization", header);
        }
        Ok(())
    }
}

struct ApiKeyAuthAttacher;
impl AuthAttacher for ApiKeyAuthAttacher {
    fn apply(&self, req: &mut tokio_tungstenite::tungstenite::http::Request<()>, cfg: &Configuration) -> Result<(), WsError> {
        if let Some(api_key) = cfg.api_key.as_ref() {
            let prefix = api_key.prefix.as_deref().unwrap_or("");
            let value = if prefix.is_empty() { api_key.key.clone() } else { format!("{} {}", prefix, api_key.key) };
            let header = HeaderValue::from_str(&value)?;
            if value.starts_with("PVEAuthCookie=") {
                req.headers_mut().insert("cookie", header);
            } else if !req.headers().contains_key("authorization") {
                req.headers_mut().insert("authorization", header);
            }
        }
        Ok(())
    }
}

fn default_auth() -> Box<dyn AuthAttacher> {
    Box::new(CompositeAuthAttacher::new(vec![
        Box::new(TokenAuthAttacher),
        Box::new(ApiKeyAuthAttacher),
    ]))
}

// ─────────────────────────────────────────────────────────────────────────────
// WebSocketTransport (Adapter)
// ─────────────────────────────────────────────────────────────────────────────

#[async_trait]
pub trait WebSocketTransport: Send + Sync {
    async fn open(&self, url: &str, auth: &dyn AuthAttacher, cfg: &Configuration) -> Result<WsStream, WsError>;
}

struct TungsteniteTransport;

#[async_trait]
impl WebSocketTransport for TungsteniteTransport {
    async fn open(&self, url: &str, auth: &dyn AuthAttacher, cfg: &Configuration) -> Result<WsStream, WsError> {
        let mut req = url.into_client_request()?;
        auth.apply(&mut req, cfg)?;
        let (stream, _) = tokio_tungstenite::connect_async(req).await?;
        Ok(stream)
    }
}

fn default_transport() -> Arc<dyn WebSocketTransport> {
    Arc::new(TungsteniteTransport)
}

// ─────────────────────────────────────────────────────────────────────────────
// UrlBuilder
// ─────────────────────────────────────────────────────────────────────────────

fn build_upgrade_url(cfg: &Configuration, base_path: &str, ticket: &ConsoleTicket) -> Result<String, WsError> {
    let mut url = url::Url::parse(&cfg.base_path).map_err(|e| WsError::BadBasePath(e.to_string()))?;
    let new_scheme = match url.scheme() {
        "https" => "wss",
        "http" => "ws",
        other => return Err(WsError::BadBasePath(format!("unsupported scheme {other}"))),
    };
    url.set_scheme(new_scheme).map_err(|_| WsError::BadBasePath("scheme set failed".into()))?;
    let mut joined = url.to_string();
    if joined.ends_with('/') { joined.pop(); }
    Ok(format!(
        "{joined}{base_path}/vncwebsocket?port={port}&vncticket={tk}",
        port = ticket.port,
        tk = urlencoding::encode(&ticket.ticket),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// Sessions
// ─────────────────────────────────────────────────────────────────────────────

const PING_INTERVAL: std::time::Duration = std::time::Duration::from_secs(30);

pub struct TerminalSession {
    stream: WsStream,
    codec: TextFrameCodec,
    last_ping: std::time::Instant,
}

impl TerminalSession {
    pub async fn send(&mut self, text: &str) -> Result<(), WsError> {
        let frame = self.codec.encode(text.to_string());
        self.stream.send(Message::Binary(frame.into())).await?;
        Ok(())
    }

    pub async fn resize(&mut self, cols: u32, rows: u32) -> Result<(), WsError> {
        self.stream.send(Message::Text(TextFrameCodec::encode_resize(cols, rows).into())).await?;
        Ok(())
    }

    pub async fn recv(&mut self) -> Result<Option<String>, WsError> {
        let mut decoded: Vec<String> = Vec::new();
        loop {
            if let Some(msg) = decoded.pop() { return Ok(Some(msg)); }
            if self.last_ping.elapsed() >= PING_INTERVAL {
                self.stream.send(Message::Text(TextFrameCodec::PING_FRAME.into())).await?;
                self.last_ping = std::time::Instant::now();
            }
            match self.stream.next().await {
                None => return Ok(None),
                Some(Err(e)) => return Err(WsError::Tungstenite(e)),
                Some(Ok(Message::Text(t))) => self.codec.decode(t.as_bytes(), &mut decoded),
                Some(Ok(Message::Binary(b))) => self.codec.decode(b.as_ref(), &mut decoded),
                Some(Ok(Message::Close(_))) => return Ok(None),
                Some(Ok(_)) => {}
            }
        }
    }

    pub async fn close(mut self) -> Result<(), WsError> {
        self.stream.close(None).await?;
        Ok(())
    }
}

pub struct VncSession {
    stream: WsStream,
    codec: BinaryFrameCodec,
}

impl VncSession {
    pub async fn send(&mut self, data: Vec<u8>) -> Result<(), WsError> {
        self.stream.send(Message::Binary(self.codec.encode(data).into())).await?;
        Ok(())
    }

    pub async fn recv(&mut self) -> Result<Option<Vec<u8>>, WsError> {
        loop {
            match self.stream.next().await {
                None => return Ok(None),
                Some(Err(e)) => return Err(WsError::Tungstenite(e)),
                Some(Ok(Message::Binary(b))) => return Ok(Some(b.to_vec())),
                Some(Ok(Message::Text(t))) => return Ok(Some(t.as_bytes().to_vec())),
                Some(Ok(Message::Close(_))) => return Ok(None),
                Some(Ok(_)) => {}
            }
        }
    }

    pub async fn close(mut self) -> Result<(), WsError> {
        self.stream.close(None).await?;
        Ok(())
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// ConsoleConnector (Template Method)
// ─────────────────────────────────────────────────────────────────────────────

pub struct ConsoleConnector {
    auth: Box<dyn AuthAttacher>,
    transport: Arc<dyn WebSocketTransport>,
}

impl ConsoleConnector {
    pub fn new(auth: Box<dyn AuthAttacher>, transport: Arc<dyn WebSocketTransport>) -> Self {
        Self { auth, transport }
    }

    pub fn default() -> Self {
        Self::new(default_auth(), default_transport())
    }

    pub async fn open_terminal(&self, cfg: &Configuration, target: TerminalTarget) -> Result<TerminalSession, WsError> {
        let issuer = terminal_issuer_for(target);
        let (ticket, base_path) = issuer.issue(cfg).await?;
        let url = build_upgrade_url(cfg, &base_path, &ticket)?;
        let mut stream = self.transport.open(&url, self.auth.as_ref(), cfg).await?;
        // PVE termproxy expects `<user>:<vncticket>\n` as the very
        // first frame after the WS handshake. tokio_tungstenite's
        // `connect_async` returns once the upgrade is complete, so
        // the stream is ready to write here — push the auth inline.
        let auth_frame = format!("{}:{}\n", ticket.user, ticket.ticket);
        SinkExt::send(&mut stream, Message::Text(auth_frame.into())).await?;
        Ok(TerminalSession {
            stream,
            codec: TextFrameCodec::default(),
            last_ping: std::time::Instant::now(),
        })
    }

    pub async fn open_vnc(&self, cfg: &Configuration, target: VncTarget) -> Result<VncSession, WsError> {
        let issuer = vnc_issuer_for(target);
        let (ticket, base_path) = issuer.issue(cfg).await?;
        let url = build_upgrade_url(cfg, &base_path, &ticket)?;
        let stream = self.transport.open(&url, self.auth.as_ref(), cfg).await?;
        Ok(VncSession {
            stream,
            codec: BinaryFrameCodec,
        })
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Public API — module-level convenience wrappers
// ─────────────────────────────────────────────────────────────────────────────

pub async fn connect_terminal(cfg: &Configuration, target: TerminalTarget) -> Result<TerminalSession, WsError> {
    ConsoleConnector::default().open_terminal(cfg, target).await
}

pub async fn connect_vnc(cfg: &Configuration, target: VncTarget) -> Result<VncSession, WsError> {
    ConsoleConnector::default().open_vnc(cfg, target).await
}

// ─────────────────────────────────────────────────────────────────────────────
// Frame-codec unit tests. Run with `cargo test --features extras`.
// ─────────────────────────────────────────────────────────────────────────────

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

    fn decode_text(codec: &mut TextFrameCodec, chunks: &[&str]) -> Vec<String> {
        let mut out = Vec::new();
        for c in chunks {
            codec.decode(c.as_bytes(), &mut out);
        }
        out
    }

    // PVE termproxy is asymmetric — server→client is the raw TTY
    // output, not framed. The decoder is intentionally pass-through.

    #[test]
    fn text_codec_passes_chunk_through_verbatim() {
        let mut c = TextFrameCodec::default();
        assert_eq!(decode_text(&mut c, &["hello"]), vec!["hello".to_string()]);
    }

    #[test]
    fn text_codec_emits_each_chunk_separately() {
        let mut c = TextFrameCodec::default();
        assert_eq!(
            decode_text(&mut c, &["foo", "bar"]),
            vec!["foo".to_string(), "bar".to_string()],
        );
    }

    #[test]
    fn text_codec_passes_raw_tty_bytes_through() {
        // ANSI escape sequences are common in PTY output.
        let mut c = TextFrameCodec::default();
        assert_eq!(
            decode_text(&mut c, &["\x1b[Kprompt$ "]),
            vec!["\x1b[Kprompt$ ".to_string()],
        );
    }

    #[test]
    fn text_codec_ignores_empty_chunks() {
        let mut c = TextFrameCodec::default();
        assert!(decode_text(&mut c, &[""]).is_empty());
    }

    #[test]
    fn text_codec_returns_empty_when_no_chunks_arrived() {
        let mut c = TextFrameCodec::default();
        assert!(decode_text(&mut c, &[]).is_empty());
    }

    #[test]
    fn text_codec_preserves_bytes_that_contain_colons() {
        let mut c = TextFrameCodec::default();
        assert_eq!(
            decode_text(&mut c, &["0:5:a:b:c"]),
            vec!["0:5:a:b:c".to_string()],
        );
    }

    #[test]
    fn text_codec_encode_produces_data_frame_format() {
        let c = TextFrameCodec::default();
        let frame = c.encode("hello".to_string());
        assert_eq!(String::from_utf8(frame).unwrap(), "0:5:hello\n");
    }

    #[test]
    fn text_codec_encode_resize_produces_resize_frame_format() {
        assert_eq!(TextFrameCodec::encode_resize(120, 32), "1:120:32:");
    }

    #[test]
    fn text_codec_ping_frame_constant() {
        assert_eq!(TextFrameCodec::PING_FRAME, "2");
    }

    #[test]
    fn binary_codec_passes_bytes_through_unchanged() {
        let mut c = BinaryFrameCodec;
        let mut out: Vec<Vec<u8>> = Vec::new();
        c.decode(&[1, 2, 3, 4, 5], &mut out);
        assert_eq!(out, vec![vec![1u8, 2, 3, 4, 5]]);
    }

    #[test]
    fn binary_codec_encode_passthrough() {
        let c = BinaryFrameCodec;
        assert_eq!(c.encode(vec![7u8, 8, 9]), vec![7u8, 8, 9]);
    }
}