summaryrefslogtreecommitdiff
path: root/test/behaviour/panes.zig
blob: 9dfc6abdd7389666bf1baee9c9ed47d5b1946387 (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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
//! docs/typ/reference.typ, Panes <panes>.
const std = @import("std");
const Session = @import("session").Session;

test "panes: a collapsed pane's ctl says so" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const first = try s.focused();
    _ = try s.newPane();
    try s.write(s.at("/pane/{d}/ctl", .{first}), "Collapse\n");
    try std.testing.expect(std.mem.indexOf(u8, try s.read(s.at("/pane/{d}/ctl", .{first})), " collapsed ") != null);
}

test "panes: a pane's ctl read ends with its mode, as its tag's box shows it" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
    try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:normal "));
    try s.keys("i");
    try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:insert "));
    try s.keys("\x1bv");
    try std.testing.expect(std.mem.endsWith(u8, try s.read(ctl), " mode:select "));
}

test "panes: the ctl word name refuses a leading blank, as the name file does" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const ctl = s.at("/pane/{d}/ctl", .{try s.focused()});
    try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "name  /tmp/lead.txt\n"));
    try std.testing.expectEqualStrings("bad character in file name: a blank at its start", s.said);
    try s.write(ctl, "name /tmp/ok.txt\n");
    try s.expect(s.at("/pane/{d}/name", .{try s.focused()}), "/tmp/ok.txt\n");
}

test "panes: Putall saves every pane with unsaved edits to a file, one each, and leaves the rest" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "clean.txt", .data = "clean\n" });
    var dir_buf: [4096]u8 = undefined;
    const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const first = try s.focused();
    try s.write(s.at("/pane/{d}/name", .{first}), "/tmp/pardes-putall-a.txt\n");
    try s.write(s.at("/pane/{d}/body", .{first}), "edited\n");
    // A file opened from disk, unedited.
    try s.write(s.at("/pane/{d}/look", .{first}), s.at("{s}/clean.txt\n", .{dir}));
    try std.testing.expectEqual(@as(usize, 0), s.saved);
    try s.write("/ctl", "Putall\n");
    try std.testing.expectEqual(@as(usize, 1), s.saved);
}

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

test "panes: focus reads the pane with the keyboard, and a serial written to it moves the keyboard there, and only that" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const first = try s.focused();
    const second = try s.newPane();
    try s.expect("/focus", s.at("{d}\n", .{second}));
    // A stat says the length a read gives.
    try std.testing.expectEqual(@as(u64, s.at("{d}\n", .{second}).len), (try s.stat("/focus")).size);
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " notcurrent "));
    // A column's tag has the keyboard: no pane does, and a write takes it back.
    try s.write("/ctl", "Newcol\n");
    try s.expect("/focus", "");
    try std.testing.expectEqual(@as(u64, 0), (try s.stat("/focus")).size);
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
    try s.write("/focus", s.at("{d}\n", .{first}));
    try s.expect("/focus", s.at("{d}\n", .{first}));
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{first})), " current "));
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " notcurrent "));
    try std.testing.expectEqual(@as(u16, 2), try s.refused("/focus", "99999\n"));
    try std.testing.expectEqualStrings("no such pane", s.said);
    for ([_][]const u8{ "", "two", "-1", "1 2" }) |bad| try std.testing.expectEqual(@as(u16, 22), try s.refused("/focus", bad));
    try s.expect("/focus", s.at("{d}\n", .{first}));
    // Focus gives the keyboard only: a folded pane stays folded.
    try s.write(s.at("/pane/{d}/ctl", .{second}), "Collapse\n");
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
    try s.write("/focus", s.at("{d}", .{second}));
    try s.expect("/focus", s.at("{d}\n", .{second}));
    try std.testing.expect(has(try s.read(s.at("/pane/{d}/ctl", .{second})), " collapsed "));
}

test "panes: the pane ctl takes acme's verbs and the pane's builtins, and refuses the rest in Plan 9's words" {
    var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
    defer s.deinit();
    const n = try s.focused();
    const ctl = s.at("/pane/{d}/ctl", .{n});
    // A builtin is its own capitalised word: `GET` and `DEL` are none.
    for ([_][]const u8{ "lock x", "bogus", "DEL", "GET", "get x", "look /tmp", "exec Del" }) |bad| {
        try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, s.at("{s}\n", .{bad})));
        try std.testing.expectEqualStrings(s.at("unknown control message \"{s}\"", .{std.mem.sliceTo(bad, ' ')}), s.said);
    }
    _ = try s.refused(ctl, "Newcol\n");
    try std.testing.expectEqualStrings("not a window control message \"Newcol\": write it to /ctl", s.said);
    _ = try s.refused(ctl, "Verbose off\n");
    try std.testing.expectEqualStrings("not a window control message \"Verbose\": write it to /ctl", s.said);
    _ = try s.refused(ctl, "Collapse now\n");
    try std.testing.expectEqualStrings("wrong #args in control message \"Collapse\"", s.said);
    // Checked whole first: a bad line anywhere and nothing ran.
    try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nbogus\n"));
    // A pane's builtin runs at that pane.
    try s.write(ctl, "Msg from ctl\n");
    try std.testing.expect(has(try s.read("/log"), s.at("msg {d} from ctl\n", .{n})));
    // One that means nothing bare is refused before anything runs.
    _ = try s.refused(ctl, "Find\n");
    try std.testing.expectEqualStrings("wrong #args in control message \"Find\"", s.said);
    try std.testing.expectEqual(@as(u16, 22), try s.refused(ctl, "Msg first\nMsg\n"));
    try std.testing.expect(!has(try s.read("/log"), "first"));
    // Save with no name on a scratch would ask for one: refused, not asked.
    const other = try s.newPane();
    _ = try s.refused(s.at("/pane/{d}/ctl", .{other}), "Save\n");
    try std.testing.expectEqualStrings("control message needs its argument \"Save\"", s.said);
    // A line after the one that closed the pane has nowhere to run.
    try std.testing.expectEqual(@as(u16, 2), try s.refused(s.at("/pane/{d}/ctl", .{other}), "Del\nMsg after\n"));
    try std.testing.expectError(error.Refused, s.read(s.at("/pane/{d}/ctl", .{other})));
}

test "panes: a second lock is refused until the holder unlocks or closes, and binds nobody else" {
    var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
    defer s.deinit();
    const n = try s.focused();
    const ctl = s.at("/pane/{d}/ctl", .{n});
    const a = try s.hold(ctl, 2);
    const b = try s.hold(ctl, 1);
    // An open that only reads the status line holds nothing to lock with.
    const reader = try s.hold(ctl, 0);
    try std.testing.expect(try s.writeFid(reader, "lock\n") != 0);
    s.clunk(reader);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\n"));
    // Another open's lock is refused at once, and so is its unlock; writes
    // to the pane's other files are not.
    try std.testing.expectEqual(@as(u16, 16), try s.writeFid(b, "lock\n"));
    try std.testing.expectEqualStrings("file in use: another open of this ctl holds the lock", s.said);
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(b, "unlock\n"));
    try std.testing.expectEqualStrings("pane not locked by this open", s.said);
    try s.write(s.at("/pane/{d}/addr", .{n}), "1");
    try s.write(s.at("/pane/{d}/data", .{n}), "ONE\n");
    try s.expect(s.at("/pane/{d}/body", .{n}), "ONE\ntwo\n");
    // Once the holder unlocks, the other's try takes it.
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "unlock\n"));
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(b, "lock\n"));
    try std.testing.expectEqual(@as(u16, 16), try s.writeFid(a, "lock\nunlock\n"));
    // Closing the holder gives it up.
    s.clunk(b);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(a, "lock\nunlock\nlock\n"));
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(a, "unlock\nunlock\n"));
    // Still a's: another open is refused it.
    const c = try s.hold(ctl, 1);
    try std.testing.expectEqual(@as(u16, 16), try s.writeFid(c, "lock\n"));
    s.clunk(c);
    // The lock lives and dies with the pane.
    const other = try s.newPane();
    const d = try s.hold(s.at("/pane/{d}/ctl", .{other}), 1);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(d, "lock\n"));
    try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
    try std.testing.expectEqual(@as(u16, 2), try s.writeFid(d, "lock\n"));
    s.clunk(d);
    s.clunk(a);
    // a's close gave its lock up too.
    const e = try s.hold(ctl, 1);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(e, "lock\n"));
    s.clunk(e);
}

test "panes: an unsaved file's tag is its name alone, its dirty file says it is unsaved" {
    var s = try Session.initFile(std.testing.allocator, "before\n");
    defer s.deinit();
    const n = try s.focused();
    const tag = s.at("/pane/{d}/tag", .{n});
    const clean = try s.read(tag);
    try s.write(s.at("/pane/{d}/body", .{n}), "after\n");
    try std.testing.expectEqualStrings(clean, try s.read(tag));
    try std.testing.expect(std.mem.indexOf(u8, clean, " *") == null);
    try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n");
}

test "panes: a pane closed while its question stands ends the question in the log" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    try s.write("/ctl", "Placement pardes\n");
    const top = try s.focused();
    try s.write(s.at("/pane/{d}/exec", .{top}), "New\n");
    const closing = try s.focused();
    try s.write(s.at("/pane/{d}/exec", .{closing}), "New\n");
    // A middle pane: Del from a key asks which side.
    try s.write("/focus", s.at("{d}\n", .{closing}));
    try s.keys(" d");
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("ask {d} del k j\n", .{closing})) != null);
    // A bare answer says what this question takes.
    const ctl = s.at("/pane/{d}/ctl", .{closing});
    _ = try s.refused(ctl, "answer\n");
    try std.testing.expectEqualStrings("answer takes k j or -", s.said);
    try s.write(ctl, "Del\n");
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), s.at("answer {d} -\ndel {d}", .{ closing, closing })) != null);
}

test "panes: focus on a closed pane goes to the pane that takes its place, not the one focused before" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const left = try s.focused();
    try s.write("/ctl", "Newcol\n");
    try s.write(s.at("/col/{d}/ctl", .{try s.column(1)}), "New\n");
    const right = try s.focused();
    try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
    const low = try s.focused();
    // `right` was focused just before `low`, but `left` grows into `low`'s
    // rows, so `left` gets the focus.
    try s.write(s.at("/pane/{d}/ctl", .{low}), "Del\n");
    try std.testing.expectEqual(left, try s.focused());
    // Closing a pane that has no focus leaves the focus where it is.
    try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
    const other = try s.focused();
    try s.write("/focus", s.at("{d}\n", .{right}));
    try s.write(s.at("/pane/{d}/ctl", .{other}), "Del\n");
    try std.testing.expectEqual(right, try s.focused());
    // Delcol: the column that takes the width holds, level with the closed
    // pane, the pane that takes its place; `right` was focused just before.
    try s.write(s.at("/pane/{d}/exec", .{right}), "New\n");
    const right_low = try s.focused();
    try s.write(s.at("/pane/{d}/exec", .{left}), "New\n");
    const left_low = try s.focused();
    try s.write("/focus", s.at("{d}\n", .{right}));
    try s.write("/focus", s.at("{d}\n", .{left_low}));
    try s.write(s.at("/col/{d}/ctl", .{try s.column(0)}), "Delcol\n");
    try std.testing.expectEqual(right_low, try s.focused());
}

test "panes: Collapse keeps the text and folds the pane to its tag, and again restores it" {
    var s = try Session.initFile(std.testing.allocator, "keep this text\n");
    defer s.deinit();
    var t = try Session.initTerm(std.testing.allocator);
    defer t.deinit();
    const n = try s.focused();
    for ([_][]const u8{
        try s.read(s.at("/pane/{d}/tag", .{n})),
        try s.read(s.at("/pane/{d}/tag", .{try s.newPane()})),
        try t.read(t.at("/pane/{d}/tag", .{try t.focused()})),
    }) |tag| try std.testing.expect(std.mem.endsWith(u8, tag, " Collapse Del"));
    try s.write(s.at("/pane/{d}/ctl", .{try s.focused()}), "Del\n");
    const ctl = s.at("/pane/{d}/ctl", .{n});
    try s.write(ctl, "Collapse\n");
    // Folded to its tag: no text drawn, no cursor in it, the text kept.
    try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") != null);
    try std.testing.expect(std.mem.indexOf(u8, try s.screen(), "keep this text") == null);
    try std.testing.expectEqual(null, try s.cursor());
    try s.expect(s.at("/pane/{d}/body", .{n}), "keep this text\n");
    try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
    // Its tag still takes the keyboard, the cursor on the tag's row.
    try s.keys(":");
    try std.testing.expectEqual((try s.where("/test.txt"))[1], (try s.cursor()).?[1]);
    try s.write(ctl, "Collapse\n");
    try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), " collapsed ") == null);
    try std.testing.expectEqual((try s.where("/test.txt"))[1] + 1, (try s.where("1 keep this text"))[1]);
}

test "panes: a pane its whole text fits in never scrolls to keep a margin" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "f.txt", .data = "f\n" });
    var dir: [4096]u8 = undefined;
    const at = dir[0..try tmp.dir.realPath(std.testing.io, &dir)];
    var s = try Session.initFile(std.testing.allocator, "one\ntwo\nthree\n");
    defer s.deinit();
    const n = try s.focused();
    // A second pane under it, as acme's placement puts one: right under the
    // three lines, which leaves the file exactly three body rows.
    try s.write(s.at("/pane/{d}/look", .{n}), s.at("{s}/f.txt\n", .{at}));
    try s.write("/focus", s.at("{d}\n", .{n}));
    try s.keys("jj");
    const top = (try s.where("/test.txt"))[1];
    try std.testing.expectEqual(top + 4, (try s.where(at[0..@min(at.len, 20)]))[1]);
    // On the last line, and line 1 still the first drawn.
    try std.testing.expectEqual(.{ @as(u32, 8), @as(u32, 8) }, try s.range(s.at("/pane/{d}/dot", .{n})));
    try std.testing.expectEqual(top + 1, (try s.where("1 one"))[1]);
}

/// `name` in `dir`: a one-page PDF with no outline and no text.
fn plainPdf(dir: std.Io.Dir, name: []const u8) !void {
    const objects = [_][]const u8{
        "<< /Type /Catalog /Pages 2 0 R >>",
        "<< /Type /Pages /Count 1 /Kids [3 0 R] >>",
        "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 300] /Resources << >> >>",
    };
    var buf: [1024]u8 = undefined;
    var w: std.Io.Writer = .fixed(&buf);
    var at: [objects.len]usize = undefined;
    try w.writeAll("%PDF-1.4\n");
    for (objects, 0..) |object, i| {
        at[i] = w.end;
        try w.print("{d} 0 obj\n{s}\nendobj\n", .{ i + 1, object });
    }
    const xref = w.end;
    try w.print("xref\n0 {d}\n0000000000 65535 f \n", .{objects.len + 1});
    for (at) |off| try w.print("{d:0>10} 00000 n \n", .{off});
    try w.print("trailer\n<< /Size {d} /Root 1 0 R >>\nstartxref\n{d}\n%%EOF\n", .{ objects.len + 1, xref });
    try dir.writeFile(std.testing.io, .{ .sub_path = name, .data = w.buffered() });
}

test "panes: a PDF's body is the text of the page shown, each line of it a line, never a heading run into the text under it" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const n = try s.lookFile("docs/design.pdf");
    const body = s.at("/pane/{d}/body", .{n});
    try std.testing.expect(std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\nArchitecture and implementation of the rewrite\n"));
    try s.write(s.at("/pane/{d}/look", .{n}), ":2\n");
    try std.testing.expect(!std.mem.startsWith(u8, try s.read(body), "Pardes: A Text Environment\n"));
}

test "panes: PdfTint steps a PDF's tint, read in its ctl and never in its tag, and only a PDF pane takes it" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const text = try s.focused();
    const n = try s.lookFile("docs/design.pdf");
    const ctl = s.at("/pane/{d}/ctl", .{n});
    const tag = try s.read(s.at("/pane/{d}/tag", .{n}));
    try std.testing.expect(std.mem.endsWith(u8, tag, " PdfSections PdfTint PdfFit Collapse Del"));
    for ([_][]const u8{ "full", "disabled", "filtered" }) |want| {
        try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), "fit:width ") != null);
        try s.write(ctl, "PdfTint\n");
        try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null);
        try s.expect(s.at("/pane/{d}/tag", .{n}), tag);
        // Another pane's PdfTint is refused and leaves the PDF's alone.
        try std.testing.expectEqual(@as(u16, 5), try s.refused(s.at("/pane/{d}/ctl", .{text}), "PdfTint\n"));
        try std.testing.expect(std.mem.indexOf(u8, try s.read(ctl), s.at(" tint:{s} ", .{want})) != null);
    }
}

test "panes: PdfSections on a PDF with no outline says so and opens nothing" {
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try plainPdf(tmp.dir, "plain.pdf");
    var buf: [4096]u8 = undefined;
    const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const n = try s.lookFile(s.at("{s}/plain.pdf", .{dir}));
    try std.testing.expectEqual(@as(u32, 1), (try s.pdfPage(n))[1]);
    const index = try s.read("/index");
    for (0..2) |_| {
        _ = try s.refused(s.at("/pane/{d}/exec", .{n}), "PdfSections\n");
        try s.expect("/index", index);
    }
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/log"), "PdfSections: this PDF has no outline") != null);
}

test "panes: a Mini that cannot read its file leaves the panes, the last Mini's snapshot and the keyboard as they were" {
    var s = try Session.initFile(std.testing.allocator, "abc\n");
    defer s.deinit();
    const text = try s.focused();
    const exec = s.at("/pane/{d}/exec", .{text});
    try s.write(exec, s.at("Mini /virtual/pane/{d}/body\n", .{text}));
    const mini = try s.focused();
    try std.testing.expect(mini != text);
    const body = try s.read(s.at("/pane/{d}/body", .{mini}));
    const index = try s.read("/index");
    try std.testing.expectEqual(@as(u16, 2), try s.refused(exec, "Mini /virtual/does-not-exist\n"));
    try std.testing.expectEqual(mini, try s.focused());
    try s.expect("/index", index);
    try s.expect(s.at("/pane/{d}/body", .{mini}), body);
}

test "panes: name refuses a directory, EISDIR, and leaves the name as it was" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const name = s.at("/pane/{d}/name", .{try s.focused()});
    for ([_][]const u8{ "/\n", "/tmp/\n", "sub/\n", "/tmp\n" }) |dir| {
        try std.testing.expectEqual(@as(u16, 21), try s.refused(name, dir));
        try std.testing.expect(std.mem.indexOf(u8, s.said, "is a directory") != null);
    }
    try s.expect(name, "/test.txt\n");
}

test "panes: a write of two lines to name is refused EINVAL, on a held open or not" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const name = s.at("/pane/{d}/name", .{try s.focused()});
    try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/a\n/tmp/b\n"));
    const h = try s.hold(name, 1);
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h, "/tmp/a\n/tmp/b\n"));
    s.clunk(h);
    try s.expect(name, "/test.txt\n");
    // Line by line, as bash writes it: the second line is refused.
    const h2 = try s.hold(name, 1);
    try std.testing.expectEqual(@as(u16, 0), try s.writeFid(h2, "/tmp/pardes-a\n"));
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(h2, "/tmp/pardes-b\n"));
    s.clunk(h2);
    try s.expect(name, "/tmp/pardes-a\n");
}

test "panes: a name with a component over 255 bytes is refused by the write that holds it" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const name = s.at("/pane/{d}/name", .{try s.focused()});
    try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "/tmp/" ++ &@as([256]u8, @splat('c')) ++ "/f.txt\n"));
    try std.testing.expectEqualStrings("invalid file name: a component over 255 bytes", s.said);
    const longest = "/tmp/" ++ &@as([255]u8, @splat('c'));
    try s.write(name, longest ++ "\n");
    // Held with no newline, it is refused by that write, and the close
    // has nothing left to apply.
    const held = try s.hold(name, 1);
    try std.testing.expectEqual(@as(u16, 22), try s.writeFid(held, "/tmp/" ++ &@as([256]u8, @splat('e'))));
    s.clunk(held);
    try s.expect(name, longest ++ "\n");
}

test "panes: a name cut across writes is one name, applied once at its newline or its close" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const name = s.at("/pane/{d}/name", .{try s.focused()});
    const h = try s.hold(name, 1);
    _ = try s.writeFid(h, "/tmp/pardes-na");
    try s.expect(name, "/test.txt\n");
    _ = try s.writeFid(h, "me.txt\n");
    try s.expect(name, "/tmp/pardes-name.txt\n");
    s.clunk(h);
    // No newline: held, and applied at the close.
    const h3 = try s.hold(name, 1);
    _ = try s.writeFid(h3, "/tmp/pardes-closed.txt");
    try s.expect(name, "/tmp/pardes-name.txt\n");
    s.clunk(h3);
    try s.expect(name, "/tmp/pardes-closed.txt\n");
}

test "panes: name reads the file name, and a write promotes a scratch without touching its body or its file" {
    const gpa = std.testing.allocator;
    var s = try Session.initFile(gpa, "opener\n");
    defer s.deinit();
    const n = try s.newPane();
    const name = s.at("/pane/{d}/name", .{n});
    const body = s.at("/pane/{d}/body", .{n});
    const scratch = try s.read(name);
    try std.testing.expect(std.mem.endsWith(u8, scratch, "/+New\n"));
    try std.testing.expectEqual(@as(u64, scratch.len), (try s.stat(name)).size);
    try s.write(body, "scratch ");
    try s.write(body, "work\n");
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "renamed.zig", .data = "existing target\n" });
    var dir_buf: [4096]u8 = undefined;
    const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
    const path = s.at("{s}/renamed.zig", .{dir});
    try s.write(name, s.at("{s}\n", .{path}));
    try s.expect(body, "scratch work\n");
    try s.expect(s.at("/pane/{d}/dirty", .{n}), "1\n");
    try s.expect(name, s.at("{s}\n", .{path}));
    var target_buf: [64]u8 = undefined;
    try std.testing.expectEqualStrings("existing target\n", try tmp.dir.readFile(std.testing.io, "renamed.zig", &target_buf));
    try std.testing.expectEqual(@as(u16, 22), try s.refused(name, "\n"));
    // One name, and what no file name holds: a second line, a NUL.
    for ([_][2][]const u8{
        .{ "two\nlines\n", "invalid file name: one name a write, on one line" },
        .{ "nul\x00.zig\n", "bad character in file name: a NUL" },
    }) |c| {
        try std.testing.expectEqual(@as(u16, 22), try s.refused(name, c[0]));
        try std.testing.expectEqualStrings(c[1], s.said);
    }
    // Any other byte a file name holds is taken, and reads back as written:
    // blanks at its ends, a tab, a control byte, DEL, C1, bytes not UTF-8.
    for ([_][]const u8{ " lead.zig ", "tab\t.zig", "c\x01.zig", "del\x7f.zig", "c1\xc2\x85.zig", "bad\xff.zig" }) |odd| {
        try s.write(name, s.at("{s}\n", .{odd}));
        const read_back = std.mem.trimEnd(u8, try s.read(name), "\n");
        try std.testing.expect(std.mem.endsWith(u8, read_back, odd));
        try s.write(name, read_back);
        try s.expect(name, s.at("{s}\n", .{read_back}));
    }
    // A name that is one line and UTF-8 is taken, é and all.
    try s.write(name, "caf\xc3\xa9.zig\n");
    try std.testing.expect(std.mem.endsWith(u8, try s.read(name), "/caf\xc3\xa9.zig\n"));
    try s.write(name, "two words.zig\n");
    try s.expect(name, s.at("{s}/two words.zig\n", .{dir}));
    // A rename is no edit: Undo takes back the last write.
    try s.write(s.at("/pane/{d}/ctl", .{n}), "Undo\n");
    try s.expect(body, "scratch ");
}

test "panes: relative names go from the file's directory and can name a new target" {
    var s = try Session.initFile(std.testing.allocator, "retained body\n");
    defer s.deinit();
    var tmp = std.testing.tmpDir(.{});
    defer tmp.cleanup();
    try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "note.txt", .data = "from pane directory\n" });
    var dir_buf: [4096]u8 = undefined;
    const dir = dir_buf[0..try tmp.dir.realPath(std.testing.io, &dir_buf)];
    const n = try s.focused();
    const name = s.at("/pane/{d}/name", .{n});
    try s.write(name, s.at("{s}/old.txt\n", .{dir}));
    try s.write(name, "child/../note.txt\n");
    try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n");
    try s.expect(s.at("/pane/{d}/body", .{n}), "from pane directory\n");
    try s.write(name, "./created.txt\n");
    try s.expect(name, s.at("{s}/created.txt\n", .{dir}));
    // A name alone is no edit.
    try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
}

test "panes: relative names follow a pane/new scratch's session directory, and a virtual one" {
    var s = try Session.initFile(std.testing.allocator, "source body\n");
    defer s.deinit();
    const source = try s.focused();
    try s.write(s.at("/pane/{d}/name", .{source}), "/elsewhere/source.zig\n");
    // pane/new's scratch is the session's, not the active pane's.
    const n = try s.newPane();
    const name = s.at("/pane/{d}/name", .{n});
    const session = std.fs.path.dirname(std.mem.trimEnd(u8, try s.read(name), "\n")).?;
    try std.testing.expect(!std.mem.eql(u8, session, "/elsewhere"));
    const up = std.fs.path.dirname(session) orelse "";
    try s.write(name, "../out/./notes.txt\n");
    try s.expect(name, s.at("{s}/out/notes.txt\n", .{up}));
    try s.write(name, "again.txt\n");
    try s.expect(name, s.at("{s}/out/again.txt\n", .{up}));
    try s.write(name, s.at("/virtual/pane/{d}/./body\n", .{source}));
    try s.write(s.at("/pane/{d}/ctl", .{n}), "get\n");
    try s.expect(s.at("/pane/{d}/body", .{n}), "source body\n");
    try s.write(name, "./body\n");
    try s.expect(name, s.at("/virtual/pane/{d}/body\n", .{source}));
    try s.expect(s.at("/pane/{d}/dirty", .{n}), "0\n");
}

test "panes: errors appends to one +Errors pane for the pane's directory" {
    var s = try Session.initFile(std.testing.allocator, "x\n");
    defer s.deinit();
    const n = try s.focused();
    try std.testing.expect(std.mem.indexOf(u8, try s.read("/index"), "+Errors") == null);
    try s.write(s.at("/pane/{d}/errors", .{n}), "boom\n");
    try s.write(s.at("/pane/{d}/errors", .{n}), "again\n");
    const index = try s.read("/index");
    try std.testing.expectEqual(@as(usize, 1), std.mem.count(u8, index, " /+Errors "));
    const at = std.mem.indexOf(u8, index, " /+Errors ").?;
    const line = index[(if (std.mem.lastIndexOfScalar(u8, index[0..at], '\n')) |i| i + 1 else 0)..];
    const serial = line[0..std.mem.indexOfScalar(u8, line, ' ').?];
    try s.expect(s.at("/pane/{s}/body", .{serial}), "boom\nagain\n");
}

test "panes: truncating body empties it, and a stat says the length after" {
    var s = try Session.initFile(std.testing.allocator, "one\ntwo\n");
    defer s.deinit();
    const body = s.at("/pane/{d}/body", .{try s.focused()});
    const h = try s.hold(body, 1);
    try s.truncate(h);
    try std.testing.expectEqual(@as(u64, 0), (try s.stat(body)).size);
    try s.expect(body, "");
    s.clunk(h);
    try s.write(body, "new text\n");
    try std.testing.expectEqual(@as(u64, 9), (try s.stat(body)).size);
}

test "panes: a rename colours a source its new name's language names, its text unchanged" {
    if (!@import("pardes_config").syntax_highlighting) return error.SkipZigTest;
    var s = try Session.initFile(std.testing.allocator, "fn check() void {}\n");
    defer s.deinit();
    const n = try s.focused();
    const x, const y = try s.where("fn check");
    const screen = try s.screen();
    const cols = try std.unicode.utf8CountCodepoints(screen[0..std.mem.indexOfScalar(u8, screen, '\n').?]);
    const plain = try s.styles();
    try std.testing.expectEqualStrings(plain[y * cols + x], plain[y * cols + x + 3]);
    try s.write(s.at("/pane/{d}/name", .{n}), "renamed.zig\n");
    const coloured = try s.styles();
    try std.testing.expectEqualStrings(coloured[y * cols + x], coloured[y * cols + x + 1]);
    try std.testing.expect(!std.mem.eql(u8, coloured[y * cols + x], plain[y * cols + x]));
    try s.expect(s.at("/pane/{d}/body", .{n}), "fn check() void {}\n");
}