kraal 0.2.0

A terminal workspace manager for SSH fleets and coding agents
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
use alacritty_terminal::event::VoidListener;
use alacritty_terminal::grid::Dimensions;
use alacritty_terminal::index::{Column, Line};
use alacritty_terminal::term::{Config, Term};
use alacritty_terminal::vte::ansi::{Color as AColor, Processor, Rgb};
use base64::Engine;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Color;

use super::scan::{MAX_APC, Scanner};
use super::*;

struct Size(usize, usize);

impl Dimensions for Size {
    fn total_lines(&self) -> usize {
        self.1
    }
    fn screen_lines(&self) -> usize {
        self.1
    }
    fn columns(&self) -> usize {
        self.0
    }
}

fn term(cols: usize, lines: usize) -> Term<VoidListener> {
    Term::new(Config::default(), &Size(cols, lines), VoidListener)
}

fn shared(available: bool) -> Arc<Shared> {
    let s = Arc::new(Shared::new(true, 64 << 20));
    s.set_available(available);
    s.set_cell(10, 20);
    s
}

fn b64(data: &[u8]) -> String {
    base64::engine::general_purpose::STANDARD.encode(data)
}

/// A kitty command for a `w × h` image of one colour, sent as RGB.
fn rgb_image(keys: &str, w: u32, h: u32) -> Vec<u8> {
    let data: Vec<u8> = (0..w * h).flat_map(|_| [200u8, 10, 10]).collect();
    format!("\x1b_G{keys},f=24,s={w},v={h};{}\x1b\\", b64(&data)).into_bytes()
}

/// Feed `bytes` as a pane does: text to the emulator, images to `g`; the replies.
fn feed(g: &mut PaneGraphics, t: &mut Term<VoidListener>, bytes: &[u8]) -> Vec<u8> {
    let mut parser: Processor = Processor::new();
    let mut replies = Vec::new();
    for piece in g.split(bytes) {
        match piece {
            Piece::Text(text) => parser.advance(t, &text),
            image => replies.extend(g.handle(image, t).unwrap_or_default()),
        }
    }
    replies
}

fn cell_at(t: &Term<VoidListener>, line: i32, col: usize) -> Option<Placeholder> {
    Placeholder::of_cell(&t.grid()[Line(line)][Column(col)])
}

#[test]
fn image_sequences_are_taken_out_wherever_reads_cut_them() {
    let stream: &[u8] = b"a\x1b_Gf=100,a=T;QUJD\x1b\\b\x1bP0;1q#0~~-\x1b\\c\x1b[1md\x1bPzz\x1b\\e";
    for size in 1..stream.len() {
        let mut s = Scanner::default();
        let mut pieces = Vec::new();
        for chunk in stream.chunks(size) {
            pieces.extend(s.split(chunk));
        }
        // Text pieces cut by the reads come back together.
        let mut merged: Vec<Piece> = Vec::new();
        for p in pieces {
            match (merged.last_mut(), p) {
                (Some(Piece::Text(a)), Piece::Text(b)) => a.extend(b),
                (_, p) => merged.push(p),
            }
        }
        assert_eq!(
            merged,
            vec![
                Piece::Text(b"a".to_vec()),
                Piece::Kitty(b"f=100,a=T;QUJD".to_vec()),
                Piece::Text(b"b".to_vec()),
                Piece::Sixel { params: b"0;1".to_vec(), data: b"#0~~-".to_vec() },
                // A DCS that is not sixel, and the rest, go to the emulator as they came.
                Piece::Text(b"c\x1b[1md\x1bPzz\x1b\\e".to_vec()),
            ],
            "chunks of {size}"
        );
    }
}

#[test]
fn a_cancelled_or_oversized_command_is_dropped() {
    let mut s = Scanner::default();
    assert_eq!(s.split(b"x\x1b_Ga=T\x18y"), vec![Piece::Text(b"xy".to_vec())]);
    let mut s = Scanner::default();
    let mut huge = b"\x1b_Ga=T;".to_vec();
    huge.resize(MAX_APC + 100, b'A');
    huge.extend_from_slice(b"\x1b\\z");
    assert_eq!(s.split(&huge), vec![Piece::Text(b"z".to_vec())]);
    // An ESC inside a command starts a new sequence.
    let mut s = Scanner::default();
    assert_eq!(s.split(b"\x1b_Ga=T\x1b[1m"), vec![Piece::Text(b"\x1b[1m".to_vec())]);
}

#[test]
fn every_diacritic_is_a_zero_width_mark() {
    use unicode_width::UnicodeWidthChar;
    assert_eq!(PLACEHOLDER.width(), Some(1));
    for d in DIACRITICS {
        assert_eq!(d.width(), Some(0), "{d:?}");
    }
}

#[test]
fn a_placed_image_becomes_placeholder_cells_at_the_cursor() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 6);
    let replies = feed(&mut g, &mut t, b"ab");
    assert!(replies.is_empty());
    // 3 columns by 2 rows, then text after it.
    let replies = feed(&mut g, &mut t, &rgb_image("a=T,i=5,c=3,r=2", 4, 4));
    assert_eq!(replies, b"\x1b_Gi=5;OK\x1b\\");
    let (kid, shown) = g.shown().next().unwrap();
    assert_eq!(kid >> 24, KRAAL_ID >> 24);
    assert_eq!(shown.placements, vec![(0, 3, 2)]);
    for (line, col, row, column) in [(0, 2, 0, 0), (0, 4, 0, 2), (1, 2, 1, 0), (1, 4, 1, 2)] {
        let p = cell_at(&t, line, col).unwrap();
        assert_eq!((p.id, p.row, p.col), (kid, row, column), "line {line} col {col}");
    }
    assert!(cell_at(&t, 0, 5).is_none() && cell_at(&t, 1, 1).is_none());
    // The cursor is after the image, on its last row; what follows keeps its own colours.
    assert_eq!(t.grid().cursor.point, alacritty_terminal::index::Point::new(Line(1), Column(5)));
    feed(&mut g, &mut t, b"z");
    assert_eq!(t.grid()[Line(1)][Column(5)].c, 'z');
    assert_ne!(
        t.grid()[Line(1)][Column(5)].fg,
        AColor::Spec(Rgb { r: (kid >> 16) as u8, g: (kid >> 8) as u8, b: kid as u8 })
    );
}

#[test]
fn an_image_wider_than_the_screen_is_cut_and_c1_keeps_the_cursor() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(10, 4);
    feed(&mut g, &mut t, b"\x1b[1;8H");
    feed(&mut g, &mut t, &rgb_image("a=T,C=1,c=6,r=2,q=1", 2, 2));
    let (_, shown) = g.shown().next().unwrap();
    assert_eq!(shown.placements, vec![(0, 3, 2)], "three columns fit");
    assert!(cell_at(&t, 1, 9).is_some());
    assert_eq!(t.grid().cursor.point, alacritty_terminal::index::Point::new(Line(0), Column(7)));
}

#[test]
fn queries_are_answered_only_when_a_terminal_can_show_images() {
    let query = b"\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\";
    let mut t = term(10, 4);
    let mut g = PaneGraphics::new(shared(false));
    assert!(feed(&mut g, &mut t, query).is_empty(), "like a terminal without images");
    let mut g = PaneGraphics::new(shared(true));
    assert_eq!(feed(&mut g, &mut t, query), b"\x1b_Gi=31;OK\x1b\\");
    assert_eq!(g.shown().count(), 0, "a query keeps nothing");
    assert_eq!(g.device_attributes("\x1b[?6c"), "\x1b[?62;4;22c");
    assert_eq!(PaneGraphics::new(shared(false)).device_attributes("\x1b[?6c"), "\x1b[?6c");
}

#[test]
fn files_and_shared_memory_are_refused_alike() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(10, 4);
    for medium in ["f", "t", "s"] {
        let cmd = format!("\x1b_Gi=3,a=T,f=100,t={medium};{}\x1b\\", b64(b"/etc/passwd"));
        assert_eq!(feed(&mut g, &mut t, cmd.as_bytes()), b"\x1b_Gi=3;EBADF:Failed to read image file\x1b\\");
    }
    assert_eq!(g.shown().count(), 0);
}

#[test]
fn chunks_numbers_and_quiet_replies() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 6);
    let data: Vec<u8> = (0..4 * 4).flat_map(|_| [1u8, 2, 3, 255]).collect();
    let all = b64(&data);
    let (a, b) = all.split_at(32);
    let first = format!("\x1b_GI=9,f=32,s=4,v=4,m=1;{a}\x1b\\");
    let last = format!("\x1b_Gm=0;{b}\x1b\\");
    assert!(feed(&mut g, &mut t, first.as_bytes()).is_empty());
    let reply = String::from_utf8(feed(&mut g, &mut t, last.as_bytes())).unwrap();
    // A numbered image gets an id of kraal's, given back with the number.
    let id: u32 = reply.strip_prefix("\x1b_Gi=").unwrap().split(',').next().unwrap().parse().unwrap();
    assert_eq!(reply, format!("\x1b_Gi={id},I=9;OK\x1b\\"));
    // Placed by its number; q=1 silences the OK, q=2 errors too.
    assert!(feed(&mut g, &mut t, b"\x1b_Ga=p,I=9,q=1\x1b\\").is_empty());
    assert_eq!(g.shown().count(), 1);
    assert!(feed(&mut g, &mut t, b"\x1b_Ga=p,i=77,q=2\x1b\\").is_empty());
    assert_eq!(feed(&mut g, &mut t, b"\x1b_Ga=p,i=77\x1b\\"), b"\x1b_Gi=77;ENOENT:no such image\x1b\\");
}

#[test]
fn deleted_images_render_blank_and_others_show() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 6);
    feed(&mut g, &mut t, &rgb_image("a=T,i=1,c=2,r=1,q=2", 2, 2));
    feed(&mut g, &mut t, b"\r\n");
    feed(&mut g, &mut t, &rgb_image("a=T,i=2,c=2,r=1,q=2", 2, 2));
    assert_eq!(g.shown().count(), 2);
    let render = |g: &PaneGraphics, t: &Term<VoidListener>, show: bool| {
        let area = Rect::new(0, 0, 20, 6);
        let mut buf = Buffer::empty(area);
        crate::pane::render_term(t, area, &mut buf, None);
        g.show_cells(area, &mut buf, show);
        buf
    };
    let buf = render(&g, &t, true);
    assert!(buf[(0, 0)].symbol().starts_with(PLACEHOLDER) && buf[(0, 1)].symbol().starts_with(PLACEHOLDER));
    assert!(matches!(buf[(0, 0)].fg, Color::Rgb(..)));
    // A client that cannot show images sees blanks, never the placeholder character.
    let buf = render(&g, &t, false);
    assert_eq!(buf[(0, 0)].symbol(), " ");
    feed(&mut g, &mut t, b"\x1b_Ga=d,d=I,i=1\x1b\\");
    assert_eq!(g.shown().count(), 1);
    let buf = render(&g, &t, true);
    assert_eq!(buf[(0, 0)].symbol(), " ");
    assert!(buf[(0, 1)].symbol().starts_with(PLACEHOLDER));
    // By position: the image under the cursor's cell (1-based x, y).
    feed(&mut g, &mut t, b"\x1b_Ga=d,d=p,x=1,y=2\x1b\\");
    assert_eq!(g.shown().count(), 0);
}

#[test]
fn a_programs_own_placeholders_name_its_image_through_kraal() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 6);
    // A virtual placement, then the program prints placeholders for id 42 (256-colour), with
    // the row diacritic only on the first cell, as kitty allows.
    feed(&mut g, &mut t, &rgb_image("a=T,U=1,i=42,c=3,r=1,q=2", 3, 1));
    let (kid, shown) = g.shown().next().unwrap();
    assert_eq!(shown.placements, vec![(0, 3, 1)]);
    assert!(t.grid()[Line(0)][Column(0)].c == ' ', "a virtual placement writes nothing");
    let text = format!("\x1b[38;5;42m{PLACEHOLDER}\u{0305}{PLACEHOLDER}{PLACEHOLDER}\x1b[39m");
    feed(&mut g, &mut t, text.as_bytes());
    let area = Rect::new(0, 0, 20, 6);
    let mut buf = Buffer::empty(area);
    crate::pane::render_term(&t, area, &mut buf, None);
    g.show_cells(area, &mut buf, true);
    for col in 0..3u16 {
        let p = Placeholder::of_symbol(buf[(col, 0)].symbol(), buf[(col, 0)].fg).unwrap();
        assert_eq!((p.id, p.row, p.col), (kid, 0, col as usize), "column {col}");
    }
    // An id nothing shows renders blank.
    let text = format!("\r\n\x1b[38;5;43m{PLACEHOLDER}\x1b[39m");
    feed(&mut g, &mut t, text.as_bytes());
    let mut buf = Buffer::empty(area);
    crate::pane::render_term(&t, area, &mut buf, None);
    g.show_cells(area, &mut buf, true);
    assert_eq!(buf[(0, 1)].symbol(), " ");
}

#[test]
fn the_oldest_images_go_when_the_quota_is_reached() {
    let s = Arc::new(Shared::new(true, 1));
    s.set_available(true);
    let mut g = PaneGraphics::new(s.clone());
    let mut t = term(20, 6);
    feed(&mut g, &mut t, &rgb_image("a=T,i=1,q=2", 2, 2));
    // Even one image is over a quota of one byte: it cannot stay.
    assert_eq!(g.shown().count(), 0);
    assert_eq!(feed(&mut g, &mut t, &rgb_image("a=T,i=2", 2, 2)), b"\x1b_Gi=2;ENOSPC:image storage is full\x1b\\");
    assert_eq!(s.used(), 0, "nothing is left held");
    // With room for one, the second replaces the first.
    // What one picture counts: its PNG and the cost of keeping it.
    let one = {
        let probe = Arc::new(Shared::new(true, usize::MAX));
        let _png = Picture::from_rgba(2, 2, [200, 10, 10, 255].repeat(4), &probe).unwrap();
        probe.used()
    };
    let s = Arc::new(Shared::new(true, one + one / 2));
    s.set_available(true);
    let mut g = PaneGraphics::new(s.clone());
    feed(&mut g, &mut t, &rgb_image("a=T,i=1,q=2", 2, 2));
    feed(&mut g, &mut t, &rgb_image("a=T,i=2,q=2", 2, 2));
    assert_eq!(g.shown().count(), 1);
    assert!(g.shown().all(|(_, sh)| sh.origin == 2));
    assert!(s.used() <= one + one / 2);
    drop(g);
    assert_eq!(s.used(), 0, "a pane's images go with it");
}

#[test]
fn sixel_images_decode_and_take_cells() {
    // Two red columns six pixels high, then a band below in register 1's default blue.
    let img = sixel::decode(b"\"1;1;2;12#0;2;100;0;0#0~~-#1!2~").unwrap();
    assert_eq!((img.width, img.height), (2, 12));
    assert_eq!(&img.pixels[..4], &[255, 0, 0, 255]);
    assert_eq!(&img.pixels[6 * 2 * 4..6 * 2 * 4 + 4], &[51, 51, 204, 255]);
    // Unpainted pixels are transparent.
    let img = sixel::decode(b"#0;2;0;100;0@").unwrap();
    assert_eq!((img.width, img.height), (1, 1));
    assert_eq!(&img.pixels[..], &[0, 255, 0, 255]);
    assert!(sixel::decode(b"!99999999~").is_none(), "too wide");
    assert!(sixel::decode(b"").is_none());

    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 6);
    feed(&mut g, &mut t, b"\x1bPq\"1;1;25;30#0;2;0;0;100#0!25~-!25~-!25~-!25~-!25~\x1b\\");
    let (_, shown) = g.shown().next().unwrap();
    // 25×30 pixels in 10×20 cells: 3 columns, 2 rows; the cursor goes below.
    assert_eq!(shown.placements, vec![(0, 3, 2)]);
    assert_eq!(t.grid().cursor.point.line, Line(2));
}

#[test]
fn outer_terminal_commands_are_plain_and_chunked() {
    let s = shared(true);
    let picture = Picture::new(vec![7u8; 10_000], 1, 1, &s);
    let mut out = Vec::new();
    transmit(KRAAL_ID | 5, &picture, &mut out);
    let text = String::from_utf8(out).unwrap();
    assert!(text.starts_with(&format!("\x1b_Ga=t,f=100,i={},q=2,m=1;", KRAAL_ID | 5)));
    assert!(text.ends_with("\x1b\\"));
    assert_eq!(text.matches("\x1b_G").count(), 10_000_usize.div_ceil(3) * 4 / 4096 + 1);
    let mut out = Vec::new();
    place(9, 3, 4, 2, &mut out);
    assert_eq!(out, b"\x1b_Ga=p,U=1,i=9,p=3,c=4,r=2,q=2\x1b\\");
    assert_eq!(probe_answer(&format!("\x1b_Gi={PROBE_ID};OK\x1b\\")), Some(true));
    assert_eq!(probe_answer(&format!("\x1b_Gi={PROBE_ID};ENOTSUPPORTED\x1b\\")), Some(false));
    assert_eq!(probe_answer("\x1b[?62c"), None);
    assert_eq!(cell_size_answer("\x1b[6;20;9t"), Some((9, 20)));
    assert_eq!(terminal_name("\x1bP>|ghostty 1.1.3\x1b\\").as_deref(), Some("ghostty 1.1.3"));
    assert!(draws_placeholders("kitty(0.39.1)") && draws_placeholders("ghostty 1.1.3"));
    assert!(!draws_placeholders("WezTerm 20240203-110809-5046fc22"));
}

#[test]
fn terminal_replies_reach_the_server_whole() {
    let mut p = crate::input::InputParser::new();
    let mut events = p.feed(format!("\x1b_Gi={PROBE_ID};OK\x1b\\\x1bP>|kitty(0.39.1)\x1b\\\x1b[6;20;9t").as_bytes());
    events.extend(p.flush());
    let raw: Vec<String> = events
        .into_iter()
        .filter_map(|e| match e {
            crate::input::InputEvent::Raw(r) => Some(String::from_utf8_lossy(&r).into_owned()),
            _ => None,
        })
        .collect();
    assert_eq!(
        raw,
        vec![
            format!("\x1b_Gi={PROBE_ID};OK\x1b\\"),
            "\x1bP>|kitty(0.39.1)\x1b\\".to_string(),
            "\x1b[6;20;9t".to_string()
        ]
    );
}

/// A quota that holds `n` pictures of `w × h` sent as RGB.
fn quota_for(n: usize, w: u32, h: u32) -> Arc<Shared> {
    let probe = Arc::new(Shared::new(true, usize::MAX));
    let _png = Picture::from_rgba(w, h, [200, 10, 10, 255].repeat((w * h) as usize), &probe).unwrap();
    let s = Arc::new(Shared::new(true, probe.used() * n + probe.used() / 2));
    s.set_available(true);
    s.set_cell(10, 20);
    s
}

#[test]
fn cropped_placements_count_in_the_quota() {
    // Room for three pictures: the upload, and two parts of it at most.
    let s = quota_for(3, 8, 8);
    let mut g = PaneGraphics::new(s.clone());
    let mut t = term(40, 10);
    feed(&mut g, &mut t, &rgb_image("a=t,i=1,q=2", 8, 8));
    let quota = s.used() * 3 + s.used() / 2;
    for x in 0..20 {
        feed(&mut g, &mut t, format!("\x1b_Ga=p,i=1,x={},w=4,C=1,q=2\x1b\\", x % 4).as_bytes());
        assert!(s.used() <= quota, "placement {x}: {} over {quota}", s.used());
    }
    assert!(g.shown().count() <= 3);
}

#[test]
fn placements_are_bounded_and_those_written_over_are_forgotten() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 4);
    feed(&mut g, &mut t, &rgb_image("a=t,i=1,q=2", 2, 2));
    // The same cell, placed over and over: what was written over goes.
    for _ in 0..200 {
        feed(&mut g, &mut t, b"\x1b[H\x1b_Ga=p,i=1,c=1,r=1,C=1,q=2\x1b\\");
    }
    assert!(g.shown().count() <= 2 * SWEEP_START, "{} images kept", g.shown().count());
    // One per line: those still in the screen or its history stay, the others go.
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 4);
    feed(&mut g, &mut t, &rgb_image("a=t,i=1,q=2", 2, 2));
    for _ in 0..2 * SWEEP_START {
        feed(&mut g, &mut t, b"\x1b_Ga=p,i=1,c=1,r=1,q=2\x1b\\\r\n");
    }
    assert_eq!(g.shown().count(), 2 * SWEEP_START, "all in the history");
    let mut short = Term::new(Config { scrolling_history: 2, ..Config::default() }, &Size(20, 4), VoidListener);
    let mut g = PaneGraphics::new(shared(true));
    feed(&mut g, &mut short, &rgb_image("a=t,i=1,q=2", 2, 2));
    for _ in 0..2 * SWEEP_START {
        feed(&mut g, &mut short, b"\x1b_Ga=p,i=1,c=1,r=1,q=2\x1b\\\r\n");
    }
    assert!(g.shown().count() < SWEEP_START, "{} kept with a history of 2 lines", g.shown().count());
    // A program's own placeholders: placements of one image are bounded.
    let mut g = PaneGraphics::new(shared(true));
    feed(&mut g, &mut t, &rgb_image("a=t,i=7,q=2", 2, 2));
    for p in 1..=200 {
        feed(&mut g, &mut t, format!("\x1b_Ga=p,U=1,i=7,p={p},c=1,r=1,q=2\x1b\\").as_bytes());
    }
    let (_, shown) = g.shown().next().unwrap();
    assert_eq!(shown.placements.len(), MAX_PLACEMENTS);
    assert_eq!(shown.placements.last().map(|p| p.0), Some(200), "the newest stay");
}

#[test]
fn the_alternate_screen_keeps_the_main_screens_images() {
    let mut g = PaneGraphics::new(shared(true));
    let mut t = term(20, 4);
    feed(&mut g, &mut t, &rgb_image("a=t,i=1,q=2", 2, 2));
    feed(&mut g, &mut t, b"\x1b_Ga=p,i=1,c=1,r=1,q=2\x1b\\");
    let main: Vec<u32> = g.shown().map(|(k, _)| k).collect();
    // On the alternate screen, many images over the same cell: the main screen's stays.
    feed(&mut g, &mut t, b"\x1b[?1049h");
    for _ in 0..100 {
        feed(&mut g, &mut t, b"\x1b[H\x1b_Ga=p,i=1,c=1,r=1,C=1,q=2\x1b\\");
    }
    assert!(g.shown().any(|(k, _)| k == main[0]));
    // Back on the main screen, the alternate screen's images go at the next look.
    feed(&mut g, &mut t, b"\x1b[?1049l");
    g.sweep_at = 0;
    feed(&mut g, &mut t, b"\x1b[3;1H\x1b_Ga=p,i=1,c=1,r=1,q=2\x1b\\");
    assert_eq!(g.shown().count(), 2, "the main screen's two");
}

#[test]
fn a_removed_placement_goes_from_the_terminal_and_a_closed_pane_counts_as_a_change() {
    let s = shared(true);
    let mut g = PaneGraphics::new(s.clone());
    let mut t = term(20, 4);
    feed(&mut g, &mut t, &rgb_image("a=T,U=1,i=1,p=7,c=2,r=1,q=2", 2, 2));
    feed(&mut g, &mut t, b"\x1b_Ga=p,U=1,i=1,p=8,c=2,r=1,q=2\x1b\\");
    let live = |g: &PaneGraphics| -> HashMap<u32, Live> {
        g.shown().map(|(k, s)| (k, (s.generation, s.picture.clone(), s.placements.clone()))).collect()
    };
    let mut sent = HashMap::new();
    let mut out = Vec::new();
    assert!(!update_terminal(&mut sent, live(&g), usize::MAX, &mut out));
    let kid = *sent.keys().next().unwrap();
    // Placement 7 removed: the terminal loses the image's placements and gets 8 again.
    feed(&mut g, &mut t, b"\x1b_Ga=d,d=i,i=1,p=7\x1b\\");
    let mut out = Vec::new();
    update_terminal(&mut sent, live(&g), usize::MAX, &mut out);
    let text = String::from_utf8(out).unwrap();
    assert_eq!(
        text,
        format!("\x1b_Ga=d,d=i,i={kid},q=2\x1b\\\x1b_Ga=p,U=1,i={kid},p=8,c=2,r=1,q=2\x1b\\"),
        "no transmission again"
    );
    // The last one removed: the image goes from the terminal.
    feed(&mut g, &mut t, b"\x1b_Ga=d,d=i,i=1,p=8\x1b\\");
    assert_eq!(g.shown().count(), 0);
    let mut out = Vec::new();
    update_terminal(&mut sent, live(&g), usize::MAX, &mut out);
    assert_eq!(out, format!("\x1b_Ga=d,d=I,i={kid},q=2\x1b\\").into_bytes());
    // A pane that closes with images on show is a change clients see.
    feed(&mut g, &mut t, &rgb_image("a=T,i=2,c=1,r=1,q=2", 2, 2));
    let before = s.version();
    drop(g);
    assert_ne!(s.version(), before);
    assert_eq!(s.used(), 0);
}