summaryrefslogtreecommitdiff
path: root/test/behaviour/addons.zig
blob: 46c8e01292e006ba2960c06aabdace00ba4b358b (plain) (blame)
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
//! docs/typ/reference.typ, Addons <addons>: a library that brings a kind
//! of pane, driven here through addons/fake/fake.c (every function of
//! abi/pardes_addon.h) and the copies of it built wrong.
const std = @import("std");
const Session = @import("session").Session;
const fake = Session.fake_addons;

fn has(hay: []const u8, needle: []const u8) bool {
    return std.mem.indexOf(u8, hay, needle) != null;
}

/// The serial of the pane /index lists of kind `kind`.
fn paneOf(s: *Session, kind: []const u8) !u32 {
    var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
    while (lines.next()) |line| {
        var words = std.mem.tokenizeScalar(u8, line, ' ');
        const serial = words.next() orelse continue;
        if (std.mem.eql(u8, words.next() orelse continue, kind)) return std.fmt.parseInt(u32, serial, 10);
    }
    return error.NoSuchPane;
}

/// The library at `path` loaded, as from a terminal's directory.
fn load(s: *Session, path: []const u8) !void {
    const real = try std.Io.Dir.cwd().realPathFileAlloc(std.testing.io, path, s.arena.allocator());
    try s.write("/ctl", s.at("Addon load {s}\n", .{real}));
}

fn unload(s: *Session, name: []const u8) void {
    _ = s.refused("/ctl", s.at("Addon unload {s}\n", .{name})) catch {};
}

test "addons: Addon load takes a library in, /addons lists it with the kinds compiled in, its words join /commands and Help, and Addon unload lets it go" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.ok);
    defer unload(&s, "fake");
    // `<kind> <version> <abi> <panes open> <path> <blake3> <state>`.
    const listed = try s.read("/addons");
    try std.testing.expect(std.mem.startsWith(u8, listed, "fake 1 3.4 0 /"));
    var fields = std.mem.tokenizeScalar(u8, listed[0..std.mem.indexOfScalar(u8, listed, '\n').?], ' ');
    for (0..5) |_| _ = fields.next();
    try std.testing.expectEqual(@as(usize, 64), fields.next().?.len);
    try std.testing.expectEqualStrings("loaded", fields.next().?);
    const commands = try s.read("/commands");
    try std.testing.expect(has(commands, "\nFake both -- a fake pane, counting (addon fake)\n"));
    try std.testing.expect(has(commands, "\nFakeBump arg pane -- add to the count (addon fake)\n"));
    // Its word run where no pane of its kind is: refused, saying where.
    try std.testing.expect(try s.refused("/ctl", "FakeBump\n") != 0);
    try std.testing.expect(has(s.said, "FakeBump is a fake pane's word"));
    // A pane of it open, it stays; closed, it goes.
    try s.write("/ctl", "Fake\n");
    const pane = try paneOf(&s, "fake");
    try std.testing.expect(std.mem.startsWith(u8, try s.read("/addons"), "fake 1 3.4 1 /"));
    try std.testing.expect(try s.refused("/ctl", "Addon unload fake\n") != 0);
    try std.testing.expect(has(s.said, "1 fake pane is open"));
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n");
    try s.write("/ctl", "Addon unload fake\n");
    try std.testing.expect(!has(try s.read("/addons"), "fake"));
    try std.testing.expect(!has(try s.read("/commands"), "Fake"));
}

test "addons: a library of another ABI, one with no pardes_addon, and one whose word is the core's are refused, each listed with why" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    for ([_][]const u8{ fake.abi, fake.no_export, fake.core_word }) |path| try load(&s, path);
    const listed = try s.read("/addons");
    try std.testing.expect(std.mem.startsWith(u8, listed, "fakeabi 1 2.2 0 /"));
    try std.testing.expect(has(listed, " refused: wants abi 2, this pardes speaks 3\n"));
    try std.testing.expect(has(listed, " refused: it has no pardes_addon\n"));
    try std.testing.expect(has(listed, "\nfakeword 1 3.4 0 /"));
    try std.testing.expect(has(listed, " refused: word Save is the core's\n"));
    // A file that is no library at all: listed with what the loader said.
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notalib.so", .data = "not a library\n" });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    try s.write("/ctl", s.at("Addon load {s}/notalib.so\n", .{dir}));
    try std.testing.expect(has(try s.read("/addons"), s.at("notalib.so 0 0.0 0 {s}/notalib.so - ", .{dir})));
    try std.testing.expect(!has(try s.read("/addons"), "notalib.so - loaded"));
    // None of them brought a word.
    try std.testing.expect(!has(try s.read("/commands"), "(addon fake"));
}

test "addons: a pane of an addon's kind shows its cells and its picture, takes its keys, clicks, words and ticks, and answers its body and ctl" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.ok);
    defer unload(&s, "fake");
    try s.write("/ctl", "Fake\n");
    const pane = try paneOf(&s, "fake");
    try std.testing.expectEqual(pane, try s.focused());
    try std.testing.expect(has(try s.screen(), "count 0"));
    // Its picture is a placement over its cells, in /screen's images.
    try std.testing.expect(has(try s.read("/screen"), "\"images\":[{"));
    // Every key but Esc is its own.
    try s.keys("ab");
    try std.testing.expect(has(try s.screen(), "count 2"));
    try std.testing.expect(has(try s.screen(), "kb"));
    try s.click("count 2", 3, 1);
    try std.testing.expect(has(try s.screen(), "count 3"));
    try std.testing.expect(has(try s.screen(), "m3"));
    // Its words go to it from its ctl; one it refuses fails the write.
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 10\n");
    try std.testing.expect(has(try s.screen(), "count 13"));
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{pane})), "count:13 "));
    // A job's answer lands at the end of the step that asked for it (a
    // test's job runs at once; a real one lands on a later step).
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeJob\n");
    try std.testing.expect(has(try s.screen(), "count 19"));
    try s.expect(s.at("/pane/{d}/body", .{pane}), "fake\n\n");
    // Esc leaves it, as from a raw terminal.
    try s.keys("\x1b");
    try std.testing.expect(try s.focused() != pane);
}

test "addons: the image addon opens the pictures a Look names, fitted, with the core's art words in its tag; refuses what it cannot read; and a dump's pane reads its file again" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, Session.image_addon);
    const listed = try s.read("/addons");
    try std.testing.expect(std.mem.startsWith(u8, listed, "image 1 3.4 0 /") and has(listed, " loaded\n"));
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    // A PNG 4x2, a JPEG 16x8 baseline and progressive, a PPM 2x1: each a
    // pane showing its pixels (`src`), over the cells it fits.
    const b64 = std.base64.standard.Decoder;
    const pictures = [_]struct { []const u8, []const u8, u32, u32 }{
        .{ "red.png", "iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAIAAADwyuo0AAAAE0lEQVR4nGP4z8AARGDiPwMyBwBnsgf5Q2OaZwAAAABJRU5ErkJggg==", 4, 2 },
        .{ "red.jpg", "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAIABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5Dooor8LP9VD/2Q==", 16, 8 },
        .{ "red.jpeg", "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wgARCAAIABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAVAQEBAAAAAAAAAAAAAAAAAAAGCP/aAAwDAQACEAMQAAABkIC1V//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAQUCP//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Bf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Bf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEABj8CP//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8hP//aAAwDAQACAAMAAAAQA//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Qf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Qf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8QP//Z", 16, 8 },
        .{ "pixel.ppm", "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7", 2, 1 },
    };
    var red: []const u8 = "";
    for (pictures) |pic| {
        const bytes = if (std.mem.endsWith(u8, pic[0], ".ppm")) pic[1] else blk: {
            const out = try s.arena.allocator().alloc(u8, try b64.calcSizeForSlice(pic[1]));
            try b64.decode(out, pic[1]);
            break :blk out;
        };
        try tmp.dir.writeFile(std.testing.io, .{ .sub_path = pic[0], .data = bytes });
        if (red.len == 0) red = bytes;
        try s.write("/look", s.at("{s}/{s}\n", .{ dir, pic[0] }));
        try std.testing.expect(has(try s.read("/screen"), s.at("\"serial\":{d},\"rect\":", .{try s.focused()})));
        try std.testing.expect(has(try s.read("/screen"), s.at("\"src\":[0,0,{d},{d}]", .{ pic[2], pic[3] })));
    }
    const pane = try s.focused();
    // Its tag: the kind's `img`, the core's art words, its path. With no
    // pixels to show (no frontend), glyph art whatever Petscii says.
    const tag = s.at("/pane/{d}/tag", .{pane});
    try std.testing.expect(std.mem.startsWith(u8, try s.read(tag), s.at("img petscii:on palette:commodore ascii:on {s}/pixel.ppm ", .{dir})));
    const ctl = s.at("/pane/{d}/ctl", .{pane});
    for ([_][]const u8{ "Petscii on\n", "Palette terminal\n", "Palette terminal\n", "Ascii off\n" }) |line| try s.write(ctl, line);
    try std.testing.expect(std.mem.startsWith(u8, try s.read(tag), "img petscii:on palette:terminal ascii:off "));
    try std.testing.expect(has(try s.read("/screen"), "\"petscii\":true"));
    // A file it cannot read: the Look fails saying so, and opens nothing.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "bad.png", .data = "\x89PNG not really" });
    const panes = (try s.read("/index")).len;
    try std.testing.expect(try s.refused("/look", s.at("{s}/bad.png\n", .{dir})) != 0);
    try std.testing.expect(has(s.said, "bad.png: not an image pardes can read"));
    try std.testing.expectEqual(panes, (try s.read("/index")).len);
    // A dump keeps its path, not its bytes: restored without the addon it
    // waits as a placeholder, and reads its file again when the addon comes.
    try s.write("/ctl", "Dump\n");
    const label = try s.arena.allocator().dupe(u8, std.mem.sliceTo(try s.read("/dumps"), ' '));
    var lines = std.mem.tokenizeScalar(u8, try s.read("/index"), '\n');
    while (lines.next()) |line| if (std.mem.indexOf(u8, line, " image ") != null) try s.write(s.at("/pane/{s}/ctl", .{std.mem.sliceTo(line, ' ')}), "Del\n");
    try s.write("/ctl", "Addon unload image\n");
    try s.write("/ctl", s.at("Restore {s}\n", .{label}));
    try std.testing.expect(has(try s.screen(), "addon image 1 is not loaded: Addon load <path>"));
    try load(&s, Session.image_addon);
    defer unload(&s, "image");
    try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,4,2]"));
    // Its file gone, a restore waits saying so; back, a Look of it opens it.
    try tmp.dir.deleteFile(std.testing.io, "red.png");
    try s.write("/ctl", s.at("Restore {s}\n", .{label}));
    const said = s.at("{s}/red.png is not here", .{dir});
    try std.testing.expect(has(try s.screen(), said[0..@min(said.len, 40)]));
    try std.testing.expect(!has(try s.read("/screen"), "\"src\":[0,0,4,2]"));
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "red.png", .data = red });
    try s.write("/look", s.at("{s}/red.png\n", .{dir}));
    try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,4,2]"));
}

test "addons: a plumbing rule to a loaded kind opens that kind's pane; to one not loaded, or with its kind gone, the look falls through to text" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    // About a jump opening a pane: `Jumps new-pane` (SPEC §6.6).
    try s.write("/ctl", "Jumps new-pane\n");
    try load(&s, Session.image_addon);
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    // A PNG under a name no kind claims.
    const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82";
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "red.pic", .data = png });
    // A port no kind reads is no refusal (plumber(4) never refuses one):
    // nothing reads it, the rule set starts nothing, and Look looks itself.
    const rule = "type is text\ndata matches '.*\\.pic'\narg isfile $data\nplumb to {s}\n\ninclude basic\n";
    try s.replace("/plumb/rules", s.at(rule, .{"nokind"}));
    try s.write("/look", s.at("{s}/red.pic\n", .{dir}));
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{try s.focused()})), "red.pic"));
    try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
    try s.replace("/plumb/rules", s.at(rule, .{"image"}));
    try s.write("/look", s.at("{s}/red.pic\n", .{dir}));
    try std.testing.expectEqual(try paneOf(&s, "image"), try s.focused());
    try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
    try s.write("/ctl", "Addon unload image\n");
    try s.write("/look", s.at("{s}/red.pic\n", .{dir}));
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/tag", .{try s.focused()})), "red.pic"));
    try std.testing.expectError(error.NoSuchPane, paneOf(&s, "image"));
}

test "addons: a dump keeps an addon's pane; restored without its addon it waits as a placeholder, and opens in place when the addon comes" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.ok);
    try s.write("/ctl", "Fake\n");
    const pane = try paneOf(&s, "fake");
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 41\n");
    try s.write("/ctl", "Dump\n");
    const label = std.mem.sliceTo(try s.read("/dumps"), ' ');
    const kept = try s.arena.allocator().dupe(u8, label);
    // Restored with it: the pane as it was, through the kind's own record.
    try s.write("/ctl", s.at("Restore {s}\n", .{kept}));
    try std.testing.expect(has(try s.screen(), "count 41"));
    // Restored without it: a placeholder in its place, saying what it waits for.
    try s.write(s.at("/pane/{d}/ctl", .{try paneOf(&s, "fake")}), "Del\n");
    try s.write("/ctl", "Addon unload fake\n");
    try s.write("/ctl", s.at("Restore {s}\n", .{kept}));
    const waiting = try paneOf(&s, "fake");
    try std.testing.expect(has(try s.screen(), "addon fake 1 is not loaded: Addon load <path>"));
    try std.testing.expect(try s.refused(s.at("/pane/{d}/ctl", .{waiting}), "FakeBump\n") != 0);
    // The addon back, the pane opens where it stands, its count kept.
    try load(&s, fake.ok);
    defer unload(&s, "fake");
    try std.testing.expectEqual(waiting, try paneOf(&s, "fake"));
    try std.testing.expect(has(try s.screen(), "count 41"));
}

test "addons: Addon reload moves every pane of the kind to the library as it is now, its state kept; a library that fails its checks leaves the old one running" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    const lib = s.at("{s}/fake.so", .{dir});
    const bytes = struct {
        fn of(ss: *Session, path: []const u8) ![]u8 {
            return std.Io.Dir.cwd().readFileAlloc(std.testing.io, path, ss.arena.allocator(), .limited(64 << 20));
        }
    }.of;
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.ok) });
    try s.write("/ctl", s.at("Addon load {s}\n", .{lib}));
    defer unload(&s, "fake");
    try s.write("/ctl", "Fake\n");
    const pane = try paneOf(&s, "fake");
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 7\n");
    try s.keys("ab");
    try std.testing.expect(has(try s.screen(), "count 9"));
    const before = try s.arena.allocator().dupe(u8, try s.read("/addons"));
    // Half built: another ABI's. Refused, and the old library still runs.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.abi) });
    try s.write("/ctl", "Addon reload fake\n");
    try std.testing.expectEqualStrings(before, try s.read("/addons"));
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 1\n");
    try std.testing.expect(has(try s.screen(), "count 10"));
    // As it is now: its record taken across, by the old library's dump
    // and the new one's load.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.ok) });
    try s.write("/ctl", "Addon reload fake\n");
    try std.testing.expectEqual(pane, try paneOf(&s, "fake"));
    try std.testing.expect(has(try s.screen(), "count 10"));
    // A library with no record of its own: the pane opens again from its
    // words' last values (FakeBump 1), what keys did gone.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try bytes(&s, fake.no_dump) });
    try s.write("/ctl", "Addon reload fake\n");
    try std.testing.expect(has(try s.screen(), "count 1"));
    // /addons says the file it runs now: another BLAKE3.
    try std.testing.expect(!std.mem.eql(u8, before, try s.read("/addons")));
}

test "addons: a job under way across a reload answers into nothing, its library kept until it has; the reloaded pane's own jobs land" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    const copy = struct {
        fn of(ss: *Session, d: std.Io.Dir, from: []const u8) !void {
            try d.writeFile(std.testing.io, .{ .sub_path = "fake.so", .data = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, from, ss.arena.allocator(), .limited(64 << 20)) });
        }
    }.of;
    try copy(&s, tmp.dir, fake.ok);
    try s.write("/ctl", s.at("Addon load {s}/fake.so\n", .{dir}));
    defer unload(&s, "fake");
    try s.write("/ctl", "Fake\n");
    const pane = try paneOf(&s, "fake");
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeBump 5\n");
    // A job taken by a worker, still running as the library is replaced
    // by another (one with no record: the pane comes back at its place, 5,
    // then its words' last values, FakeBump 5: 10).
    s.hold_jobs = true;
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeJob\n");
    try copy(&s, tmp.dir, fake.no_dump);
    try s.write("/ctl", "Addon reload fake\n");
    try std.testing.expect(has(try s.screen(), "count 10 "));
    // It answers now, from the old library's code: of another generation,
    // the answer is dropped, and the old library goes after it.
    s.releaseJobs();
    try std.testing.expect(has(try s.screen(), "count 10 "));
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "FakeJob\n");
    try std.testing.expect(has(try s.screen(), "count 16 "));
}

test "addons: a Look at a file whose extension an addon claims opens a pane of its kind, and its words are in Help" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.ok);
    defer unload(&s, "fake");
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "five.fake", .data = "12345" });
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    const pane = try s.lookFile(s.at("{s}/five.fake", .{dir}));
    try std.testing.expectEqual(pane, try paneOf(&s, "fake"));
    // Its bytes were the kind's to open: five of them.
    try std.testing.expect(has(try s.screen(), "count 5"));
    try s.write(s.at("/pane/{d}/ctl", .{pane}), "Del\n");
    try s.write("/ctl", "Help\n");
    const help = try s.read(s.at("/pane/{d}/body", .{try s.focused()}));
    try std.testing.expect(has(help, "SPC z f   Fake   (addon fake)\na fake pane, counting\n\n"));
}

test "addons: an image pane whose file changes on disk shows the new picture, under a new key: a window keeps a texture by its key" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    s.graphics();
    try load(&s, Session.image_addon);
    defer unload(&s, "image");
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" });
    try s.write("/look", s.at("{s}/pic.ppm\n", .{dir}));
    const n = try s.focused();
    const key = "\"key\":\"";
    const before = try s.arena.allocator().dupe(u8, try s.read("/screen"));
    try std.testing.expect(has(before, s.at("\"serial\":{d},", .{n})) and has(before, "\"src\":[0,0,2,1]"));
    const old_key = before[std.mem.indexOf(u8, before, key).? + key.len ..][0..16];
    // The same size, other pixels: only its key tells a window it changed.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\xe7\x4d\x0d\xe7\x4d\x0d" });
    try std.testing.expect(try s.filesChanged());
    const after = try s.read("/screen");
    try std.testing.expect(has(after, "\"src\":[0,0,2,1]"));
    try std.testing.expect(!has(after, old_key));
    // Another size: the picture as it is now.
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n3 1\n255\n\xe7\x4d\x0d\xe7\x4d\x0d\xe7\x4d\x0d" });
    try std.testing.expect(try s.filesChanged());
    try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,3,1]"));
    // A second Look of it finds its pane, the new picture in it.
    try s.write("/look", s.at("{s}/pic.ppm\n", .{dir}));
    try std.testing.expectEqual(n, try s.focused());
    try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,3,1]"));
}

/// A tmp directory's real path, kept in the session's arena.
fn dirOf(s: *Session, tmp: *std.testing.TmpDir) ![]const u8 {
    var buf: [4096]u8 = undefined;
    return s.arena.allocator().dupe(u8, buf[0..try tmp.dir.realPath(std.testing.io, &buf)]);
}

test "addons: a picker shows a file of an addon's kind through its kind in the pane it lands in, Esc puts that pane back as it was, and Enter lands as a Look does" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seven.fake", .data = "1234567" });
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.ok);
    defer unload(&s, "fake");
    const dir = try dirOf(&s, &tmp);
    const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
    const body = s.at("/pane/{d}/body", .{pane});
    const a = s.arena.allocator();
    const screen = try a.dupe(u8, try s.screen());
    const index = try a.dupe(u8, try s.read("/index"));
    // Its kind's paint in the reading pane's body (the fake counts the
    // file's 7 bytes), not the pane's own text, nor the file's bytes.
    try s.keys(" ffseven");
    s.tick();
    const shown = try s.screen();
    try std.testing.expect(has(shown, "count 7"));
    try std.testing.expect(!has(shown, "the notes") and !has(shown, "1234567"));
    // Esc: the pane exactly as it was, and no pane more or less.
    try s.keys("\x1b");
    s.tick();
    // (The notice row says the Find ran: the session's, not the pane's.)
    const after = try s.screen();
    try std.testing.expectEqualStrings(screen[0..std.mem.lastIndexOfScalar(u8, screen, '\n').?], after[0..std.mem.lastIndexOfScalar(u8, after, '\n').?]);
    try s.expect(body, "the notes\n");
    try std.testing.expectEqualStrings(index, try s.read("/index"));
    // Enter: the kind's pane where a Look puts it; the reading pane keeps
    // its text. (The listing is the next frame's, as for the keyboard.)
    try s.keys(" ffseven");
    s.tick();
    try s.keys("\n");
    s.tick();
    const opened = try paneOf(&s, "fake");
    try std.testing.expect(opened != pane);
    try s.expect(body, "the notes\n");
    try std.testing.expect(has(try s.screen(), "count 7"));
}

test "addons: a picker's row of a file no kind takes and not text shows one line naming it, and the row before's file never stays in the body" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "other.txt", .data = "other text\n" });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "seven.fake", .data = "1234567" });
    var blob: [3000]u8 = undefined;
    for (&blob, 0..) |*b, i| b.* = @truncate(i *% 7);
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "blob.bin", .data = &blob });
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.ok);
    defer unload(&s, "fake");
    const dir = try dirOf(&s, &tmp);
    const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
    try s.keys(" ffother");
    s.tick();
    try std.testing.expect(has(try s.screen(), "other text"));
    // Onto the binary: its one line, never its bytes, nor other.txt.
    try s.keys("\x08\x08\x08\x08\x08blob");
    s.tick();
    var shown = try s.screen();
    try std.testing.expect(has(shown, "blob.bin · binary · 3.0 kB"));
    try std.testing.expect(!has(shown, "other text"));
    // Onto a kind's file: its kind's paint, not the binary's line.
    try s.keys("\x08\x08\x08\x08seven");
    s.tick();
    shown = try s.screen();
    try std.testing.expect(has(shown, "count 7"));
    try std.testing.expect(!has(shown, "blob.bin · binary"));
    // Back onto text, then Esc: the pane's own.
    try s.keys("\x08\x08\x08\x08\x08other");
    s.tick();
    shown = try s.screen();
    try std.testing.expect(has(shown, "other text") and !has(shown, "count 7"));
    try s.keys("\x1b");
    s.tick();
    try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n");
    try std.testing.expect(has(try s.screen(), "the notes"));
}

test "addons: moving the cursor over pictures in a picker draws only the one it rests on, and a draw of one passed by is dropped whenever it comes back" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82";
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic1.png", .data = png });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic2.png", .data = png });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic3.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" });
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    s.graphics();
    try load(&s, Session.image_addon);
    defer unload(&s, "image");
    const dir = try dirOf(&s, &tmp);
    const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
    s.holdJobs();
    try s.keys(" ffpic");
    s.tick();
    // Until its picture is drawn, one line names it.
    const first = try s.screen();
    try std.testing.expect(has(first, " · image · "));
    try std.testing.expectEqual(1, s.held.items.len);
    // Two rows down with no frame between: only the row it rests on draws.
    try s.keys("\u{F800}\u{F800}");
    s.tick();
    _ = try s.screen();
    try std.testing.expectEqual(2, s.held.items.len);
    const rested = if (has(try s.screen(), "▶ pic3.ppm")) "\"src\":[0,0,2,1]" else "\"src\":[0,0,1,1]";
    const passed = if (std.mem.eql(u8, rested, "\"src\":[0,0,2,1]")) "\"src\":[0,0,1,1]" else "\"src\":[0,0,2,1]";
    // The rested one's answer first, the passed one's after: dropped.
    std.mem.reverse(@TypeOf(s.held.items[0]), s.held.items);
    s.releaseJobs();
    const screen = try s.read("/screen");
    try std.testing.expect(has(screen, s.at("\"serial\":{d},", .{pane})) and has(screen, rested));
    try std.testing.expect(!has(screen, passed));
}

test "addons: Enter on a picture in a picker opens its kind's pane, never its bytes as text in the reading pane (the user's report)" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "notes.txt", .data = "the notes\n" });
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" });
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    s.graphics();
    try load(&s, Session.image_addon);
    defer unload(&s, "image");
    const dir = try dirOf(&s, &tmp);
    const pane = try s.lookFile(s.at("{s}/notes.txt", .{dir}));
    // The listing is the next frame's, as for the keyboard.
    try s.keys(" ffpic");
    s.tick();
    try s.keys("\n");
    s.tick();
    try s.expect(s.at("/pane/{d}/body", .{pane}), "the notes\n");
    const image = try paneOf(&s, "image");
    try std.testing.expect(image != pane);
    const screen = try s.read("/screen");
    try std.testing.expect(has(screen, s.at("\"serial\":{d},", .{image})) and has(screen, "\"src\":[0,0,2,1]"));
    try std.testing.expect(!has(try s.screen(), "1 P6"));
}

test "addons: a kind's pane is painted with the theme's search hit colours, the current hit's apart (ABI 3.4), and a library whose head says 3.3 still loads, opens and paints" {
    {
        var s = try Session.initFile(std.testing.allocator, "x\n");
        defer s.deinit();
        try load(&s, fake.ok);
        defer unload(&s, "fake");
        try s.write("/ctl", "Theme acme\n");
        try s.write("/ctl", "Fake\n");
        // acme's: its hit ground, and its amber held for its page's ink.
        try std.testing.expect(has(try s.screen(), "hits ffc790 a05200"));
        try s.write("/ctl", "Theme neonotte84\n");
        try std.testing.expect(has(try s.screen(), "hits 4c301a e59d3c"));
    }
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try load(&s, fake.old);
    defer unload(&s, "fakeold");
    const listed = try s.read("/addons");
    try std.testing.expect(std.mem.startsWith(u8, listed, "fakeold 1 3.3 0 /") and has(listed, " loaded\n"));
    try s.write("/ctl", "Fake\n");
    try std.testing.expect(has(try s.screen(), "count 0"));
}