summaryrefslogtreecommitdiff
path: root/test/process/tree.zig
blob: 4d1b29151e47b0d95cfec0dc053a8b00ce28681c (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
//! docs/typ/reference.typ, The tree <tree>, through a real session's socket.
const std = @import("std");
const libc = std.c;
const process = @import("../process.zig");
const c9 = @import("cloud9");
const eh = @import("../e2e_harness.zig");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const expectEqualStrings = std.testing.expectEqualStrings;

pub const cases = .{
    .{ "tree: listing, stat and find are inert; new, remove, look, exec, name, sel, log, ctl lock, focus, the ctl split, commands, Save and the slots behave", discovery },
    .{ "tree: a write bigger than a 9P message runs each line once, whole, an Edit block once it ends; a detached session is 160x50 until told", cutWrites },
};

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

fn contains(names: []const []const u8, name: []const u8) bool {
    for (names) |n| if (std.mem.eql(u8, n, name)) return true;
    return false;
}

/// A write refused, its words holding `why`.
fn refusedSaying(c: *process.Conn, path: []const u8, data: []const u8, why: []const u8) !void {
    try std.testing.expectError(error.Refused, c.write(path, data));
    if (!has(c.said, why)) {
        std.debug.print("{s} refused saying {s}, not {s}\n", .{ path, c.said, why });
        return error.WrongWords;
    }
}

/// A read on `c` given up after `sec` seconds, so a wait that never ends fails.
fn timeout(c: *process.Conn, sec: i64) void {
    const tv: libc.timeval = .{ .sec = sec, .usec = 0 };
    _ = libc.setsockopt(c.fd, libc.SOL.SOCKET, libc.SO.RCVTIMEO, &tv, @sizeOf(libc.timeval));
}

/// /index's row for `serial`.
fn indexRow(index: []const u8, serial: u32) ![]const u8 {
    var rows = std.mem.tokenizeScalar(u8, index, '\n');
    while (rows.next()) |row| {
        const sp = std.mem.indexOfScalar(u8, row, ' ') orelse continue;
        if ((std.fmt.parseInt(u32, row[0..sp], 10) catch continue) == serial) return row;
    }
    return error.NoRow;
}

/// The serials of /index's rows whose fourth word, the path, is `path`.
fn panesAt(a: std.mem.Allocator, c: *process.Conn, path: []const u8) ![]const []const u8 {
    var out: std.ArrayList([]const u8) = .empty;
    var rows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
    while (rows.next()) |row| {
        var w = std.mem.tokenizeScalar(u8, row, ' ');
        const serial = w.next().?;
        _ = w.next();
        _ = w.next();
        if (std.mem.eql(u8, w.next() orelse "", path)) try out.append(a, serial);
    }
    return out.items;
}

const Seen = struct { path: []const u8, mode: u32, mtime: u32 };

/// Every entry below `path` stated, as `find` does, without opening one.
fn walkTree(a: std.mem.Allocator, c: *process.Conn, path: []const u8, seen: *std.ArrayList(Seen)) !void {
    for (try c.list(a, path)) |name| {
        const child = try std.fmt.allocPrint(a, "{s}/{s}", .{ std.mem.trimEnd(u8, path, "/"), name });
        const st = try c.stat(child);
        try seen.append(a, .{ .path = child, .mode = st.mode, .mtime = st.mtime });
        if (st.mode & c9.dmdir != 0 and !contains(&.{ "/log", "/os", "/screen" }, child)) try walkTree(a, c, child, seen);
    }
}

/// A follower's read on a held /log, which waits for a new record.
const Follower = struct {
    conn: *process.Conn,
    fid: u32,
    off: u64,
    buf: [256]u8 = undefined,
    len: usize = 0,

    fn run(w: *Follower) void {
        const got = w.conn.readFid(w.fid, w.off, 4096) catch return;
        w.len = @min(got.len, w.buf.len);
        @memcpy(w.buf[0..w.len], got[0..w.len]);
    }
};

fn discovery(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var d = try process.Detached.start(gpa, exe, "discovery", .{});
    defer d.deinit();
    const f = &d.files;
    var arena = std.heap.ArenaAllocator.init(gpa);
    defer arena.deinit();
    const a = arena.allocator();
    const c = try process.Conn.dial(gpa, d.socket());
    defer c.deinit(gpa);
    const root = d.root();

    const top = try c.list(a, "/");
    // A set: which files the root holds, not the order a listing gives.
    const want = [_][]const u8{ "README", "index", "status", "focus", "ctl", "commands", "recent", "look", "exec", "pager", "log", "screen", "listeners", "sessions", "layout", "tag", "tagexec", "kbdin", "mousein", "pane", "col", "os" };
    var others: usize = 0;
    for (top) |n| {
        if (contains(&.{ "src", "shaders" }, n)) continue;
        others += 1;
        try expect(contains(&want, n));
    }
    try expectEqual(want.len, others);
    for (want) |n| try expect(contains(top, n));
    const embedded = contains(top, "src");
    const before = try c.read(a, "/index");
    const guide = try c.read(a, "/README");
    try expect(std.mem.count(u8, guide, "\n") <= 45 and has(guide, "Msg hello") and has(guide, "pane/new"));
    try expectEqual(guide.len, (try c.stat("/README")).length);
    // ls/stat/find over the whole tree, without opening, creates nothing.
    var seen: std.ArrayList(Seen) = .empty;
    try walkTree(a, c, "/", &seen);
    var names: std.ArrayList([]const u8) = .empty;
    for (seen.items) |s| try names.append(a, s.path);
    for ([_][]const u8{ "/index", "/status", "/look", "/exec", "/log", "/pane/1", "/pane/1/body", "/pane/1/name", "/pane/1/sel", "/pane/1/ctl", "/pane/1/tag", "/pane/1/event", "/pane/1/dot", "/pane/1/limit", "/pane/1/dirty", "/pane/1/mark", "/pane/1/scroll", "/pane/1/look", "/pane/1/exec" }) |n| try expect(contains(names.items, n));
    try expect(!contains(names.items, "/pane/1/pty")); // the fixture file pane grew no pty/
    for (seen.items) |s| {
        try expect(!std.mem.startsWith(u8, s.path, "/self") and !std.mem.endsWith(u8, s.path, "/rdsel") and !std.mem.endsWith(u8, s.path, "/wrsel"));
        const mode = s.mode & 0o777;
        try expect(mode == 0o755 or mode == 0o644 or mode == 0o666 or mode == 0o444 or mode == 0o222 or mode == 0o555);
        try expect(s.mtime > 0);
    }
    try expectEqualStrings(before, try c.read(a, "/index")); // browsing created no pane
    try expect(before.len > 0);
    try expectEqual(@as(u64, 0), (try c.stat("/index")).length); // a view made by each read stats 0 (reference.typ, Stats)
    try expectEqual(@as(u32, 0o444), (try c.stat("/index")).mode & 0o777);
    try expectEqual(@as(u32, 0o444), (try c.stat("/status")).mode & 0o777);
    try expectEqual(@as(u32, 0o666), (try c.stat("/exec")).mode & 0o777);
    try expectEqual(@as(u32, 0o666), (try c.stat("/pane/1/body")).mode & 0o777);
    try expectEqual(@as(u32, 0o755), (try c.stat("/pane")).mode & 0o777);
    // A range file reads the pair it takes; a flag file reads 0 or 1.
    try expectEqual(@as(u64, 24), (try c.stat("/pane/1/addr")).length);
    try expectEqual(@as(u64, 0), (try c.stat("/pane/1/limit")).length);
    try expectEqualStrings("0\n", try c.read(a, "/pane/1/dirty"));
    try expectEqualStrings("1\n", try c.read(a, "/pane/1/scroll"));
    const fixture = try std.fmt.parseInt(u32, before[0..std.mem.indexOfScalar(u8, before, ' ').?], 10);
    try expect(std.mem.startsWith(u8, before, f.at("{d} text 0 ", .{fixture})));
    try expect(std.mem.endsWith(u8, before[0..std.mem.lastIndexOfScalar(u8, std.mem.trimEnd(u8, before, "\n"), ' ').?], "/discovery.txt"));
    // /status reports the editor; /exec has nothing to report yet.
    var pid: i64 = 0;
    var version = false;
    var panes: i64 = 0;
    var lines = std.mem.tokenizeScalar(u8, try c.read(a, "/status"), '\n');
    while (lines.next()) |line| {
        const sp = std.mem.indexOfScalar(u8, line, ' ') orelse continue;
        const key, const value = .{ line[0..sp], std.mem.trim(u8, line[sp..], " ") };
        if (std.mem.eql(u8, key, "pid")) pid = try std.fmt.parseInt(i64, value, 10);
        if (std.mem.eql(u8, key, "version")) version = value.len > 0;
        if (std.mem.eql(u8, key, "panes")) panes = try std.fmt.parseInt(i64, value, 10);
    }
    try expect(pid > 0 and version and panes >= 1);
    try expectEqualStrings("", try c.read(a, "/exec"));
    // An open of /pane/new makes a pane and the read names it; each open
    // makes another. A stat makes none, which is why new can be listed at
    // all: ls -l stats every name a listing gave it. /log answers the
    // recent past and ends; `follow` written to the same open makes its
    // reads past that wait for new records.
    const log = try c.hold("/log", 2);
    const frozen = (try c.readFid(log, 0, 1 << 16)).len;
    _ = try c.writeFid(log, 0, "follow\n");
    const first = try c.newPane();
    try expectEqual(first, try c.newest(a));
    const second = try c.newPane();
    try expect(first != second and first != fixture);
    try expect(contains(try c.list(a, "/pane"), "new"));
    _ = try c.stat("/pane/new");
    try expectEqual(second, try c.newest(a)); // a stat of new made no pane
    // What the editor said is in the stream too.
    const paneEvent = struct {
        fn next(conn: *process.Conn, al: std.mem.Allocator, fid: u32, off: u64) ![]const u8 {
            while (true) {
                const record = try conn.readFid(fid, off, 1 << 16);
                if (!std.mem.startsWith(u8, record, "msg ") and !std.mem.startsWith(u8, record, "err ")) return al.dupe(u8, record);
            }
        }
    }.next;
    try expectEqualStrings(f.at("new {d} {s}/+New\n", .{ first, root }), try paneEvent(c, a, log, frozen));
    try expectEqualStrings(f.at("new {d} {s}/+New\n", .{ second, root }), try paneEvent(c, a, log, frozen));
    // A follower that is waiting wakes when the editor logs, not when some
    // unrelated request next happens to wait.
    {
        const follower = try process.Conn.dial(gpa, d.socket());
        defer follower.deinit(gpa);
        timeout(follower, 5);
        const waiting = try follower.hold("/log", 2);
        const start = (try follower.readFid(waiting, 0, 1 << 16)).len;
        _ = try follower.writeFid(waiting, 0, "follow\n");
        var w: Follower = .{ .conn = follower, .fid = waiting, .off = start };
        const reader = try std.Thread.spawn(.{}, Follower.run, .{&w});
        process.sleep(200);
        try c.write("/exec", "Msg woken\n");
        reader.join();
        try expect(std.mem.endsWith(u8, w.buf[0..w.len], " woken\n"));
    }
    // A pane's ctl takes acme's lock: a second open's lock fails at once
    // with "file in use" until the holder unlocks, and nothing else waits
    // on it meanwhile.
    {
        const other = try process.Conn.dial(gpa, d.socket());
        defer other.deinit(gpa);
        const ctl = f.at("/pane/{d}/ctl", .{first});
        const mine = try c.hold(ctl, 2);
        _ = try c.writeFid(mine, 0, "lock\n");
        const theirs = try other.hold(ctl, 1);
        try std.testing.expectError(error.Refused, other.writeFid(theirs, 0, "lock\n"));
        try expect(has(other.said, "file in use"));
        try c.write(f.at("/pane/{d}/addr", .{first}), "#0");
        _ = try c.writeFid(mine, 0, "unlock\n");
        _ = try other.writeFid(theirs, 0, "lock\n");
        c.clunk(mine);
        other.clunk(theirs);
    }
    // /focus names the pane with the keyboard and moves it, as rio's wctl
    // `current` does; a pane's ctl line ends saying which it is.
    try c.write("/focus", f.at("{d}\n", .{first}));
    try expectEqualStrings(f.at("{d}\n", .{first}), try c.read(a, "/focus"));
    try expect(has(try c.read(a, f.at("/pane/{d}/ctl", .{first})), " current "));
    try expect(has(try c.read(a, f.at("/pane/{d}/ctl", .{second})), " notcurrent "));
    try refusedSaying(c, "/focus", "99999\n", "no such pane");
    // /ctl reads the settings as a write takes them and takes the session's
    // builtins; a pane's ctl takes the pane's.
    const settings = try c.read(a, "/ctl");
    try expect(has(settings, "Verbose ") and !has(settings, "Platform"));
    try c.write("/ctl", settings);
    try expectEqualStrings(settings, try c.read(a, "/ctl"));
    try c.write("/ctl", "Verbose off\n");
    try expect(has(try c.read(a, "/ctl"), "Verbose off\n"));
    try refusedSaying(c, "/ctl", "Del\n", "not a session control message \"Del\"");
    try refusedSaying(c, "/ctl", "Nope 3\n", "unknown control message \"Nope\"");
    try refusedSaying(c, f.at("/pane/{d}/ctl", .{first}), "Newcol\n", "not a window control message \"Newcol\"");
    const commands = try c.read(a, "/commands");
    const row = struct {
        fn row(text: []const u8, start: []const u8) bool {
            var rows = std.mem.tokenizeScalar(u8, text, '\n');
            while (rows.next()) |r| if (std.mem.eql(u8, r, start) or (std.mem.startsWith(u8, r, start) and r.len > start.len and r[start.len] == ' ')) return true;
            return false;
        }
    }.row;
    try expect(row(commands, "Newcol root") and row(commands, "Save arg pane") and row(commands, "Edit arg both"));
    // word, `arg` if it takes one, its ctl, a setting's values, then `-- ` what it does
    var crows = std.mem.tokenizeScalar(u8, commands, '\n');
    while (crows.next()) |r| {
        var w = std.mem.tokenizeScalar(u8, r, ' ');
        _ = w.next();
        var which = w.next().?;
        if (std.mem.eql(u8, which, "arg")) which = w.next().?;
        try expect(contains(&.{ "root", "pane", "both" }, which));
    }
    try expect(row(commands, "Placement arg root acme,pardes"));
    try expect(std.mem.endsWith(u8, commands, "\n"));
    try expectEqual(commands.len, (try c.stat("/commands")).length);
    try c.write(f.at("/pane/{d}/ctl", .{first}), "Msg said through ctl\n");
    try expect(has(try f.screen(), "said through ctl"));
    const listed = try c.list(a, "/pane");
    try expectEqual(@as(usize, 4), listed.len);
    for ([_]u32{ fixture, first, second }) |n| try expect(contains(listed, f.at("{d}", .{n})));
    try expect(contains(listed, "new"));
    try c.replace(f.at("/pane/{d}/body", .{first}), "first pane");
    try expectEqualStrings("first pane", try c.read(a, f.at("/pane/{d}/body", .{first})));
    try expectEqualStrings("", try c.read(a, f.at("/pane/{d}/body", .{second})));
    try expectEqual(@as(u64, "first pane".len), (try c.stat(f.at("/pane/{d}/body", .{first}))).length);
    const index = try c.read(a, "/index");
    try expect(std.mem.startsWith(u8, try indexRow(index, first), f.at("{d} text 1 ", .{first})));
    try expect(std.mem.startsWith(u8, try indexRow(index, second), f.at("{d} text 0 ", .{second})));
    // name: read the file name, write to rename; sel: the selection.
    const name = f.at("/pane/{d}/name", .{first});
    try expectEqualStrings(f.at("{s}/+New\n", .{root}), try c.read(a, name));
    try c.write(name, "notes.txt\n");
    try expectEqualStrings(f.at("{s}/notes.txt\n", .{root}), try c.read(a, name));
    try expectEqualStrings(f.at("rename {d} {s}/notes.txt\n", .{ first, root }), try paneEvent(c, a, log, frozen));
    try expectEqual(@as(u64, f.at("{s}/notes.txt\n", .{root}).len), (try c.stat(name)).length);
    // Copying addr onto dot selects that range; sel reads and replaces it.
    const addr = f.at("/pane/{d}/addr", .{first});
    const dot = f.at("/pane/{d}/dot", .{first});
    const sel = f.at("/pane/{d}/sel", .{first});
    try c.write(addr, "#0,#5");
    try c.write(dot, try c.read(a, addr));
    try expectEqualStrings(try c.read(a, addr), try c.read(a, dot));
    try expectEqualStrings("first", try c.read(a, sel));
    try expectEqual(@as(u64, 5), (try c.stat(sel)).length);
    try c.write(sel, "FIRST");
    try expectEqualStrings("FIRST pane", try c.read(a, f.at("/pane/{d}/body", .{first})));
    try expectEqualStrings("FIRST", try c.read(a, sel)); // what a write to sel put there is selected
    // exec Msg shows text; exec Save writes the named file; rmdir closes.
    try c.write(f.at("/pane/{d}/exec", .{first}), "Msg hello from 9P\n");
    try expect(has(try f.screen(), "hello from 9P"));
    try expectEqualStrings("1\n", try c.read(a, f.at("/pane/{d}/dirty", .{first})));
    try c.write(f.at("/pane/{d}/exec", .{first}), "Save\n");
    try expectEqualStrings("FIRST pane", try d.get(a, "notes.txt"));
    try expectEqualStrings(f.at("save {d} {s}/notes.txt\n", .{ first, root }), try paneEvent(c, a, log, frozen));
    try expectEqualStrings(f.at("{d}\n", .{first}), try c.read(a, "/exec"));
    try expectEqualStrings("0\n", try c.read(a, f.at("/pane/{d}/dirty", .{first})));
    try c.remove(f.at("/pane/{d}", .{second}));
    try expectEqualStrings(f.at("del {d} {s}/+New\n", .{ second, root }), try paneEvent(c, a, log, frozen));
    try expect(!contains(try c.list(a, "/pane"), f.at("{d}", .{second})));
    // look opens a file; the serial reads back.
    try d.put("opened.txt", "one\ntwo\nthree\n");
    try c.write("/look", f.at("{s}/opened.txt:2\n", .{root}));
    const opened = try std.fmt.parseInt(u32, std.mem.trim(u8, try c.read(a, "/look"), "\n"), 10);
    try expect(opened != fixture and opened != first and opened != second);
    try expectEqualStrings("one\ntwo\nthree\n", try c.read(a, f.at("/pane/{d}/body", .{opened})));
    try expectEqualStrings(f.at("{s}/opened.txt\n", .{root}), try c.read(a, f.at("/pane/{d}/name", .{opened})));
    try expectEqualStrings(f.at("new {d} {s}/opened.txt\n", .{ opened, root }), try paneEvent(c, a, log, frozen));
    try c.write(f.at("/pane/{d}/look", .{first}), f.at("{s}/opened.txt:3\n", .{root}));
    try expectEqualStrings(f.at("{d}\n", .{opened}), try c.read(a, "/look")); // a repeated look targets the open pane
    for ([_]u32{ fixture, first, opened }) |n| try expect(contains(try c.list(a, "/pane"), f.at("{d}", .{n})));
    // Writing an event record back still performs the action it names.
    try c.replace(f.at("/pane/{d}/body", .{first}), "Msg via event");
    try c.write(f.at("/pane/{d}/event", .{first}), "FX0 13\n");
    try expect(has(try f.screen(), "via event"));
    // A command line may not carry control characters, and nothing else in
    // the tree may be created or removed.
    if (c.write("/exec", "Msg a\x00b")) |_| return error.Taken else |_| {}
    if (c.write("/status", "anything\n")) |_| return error.Taken else |_| {}
    if (c.create("/", "x", c9.dmdir | 0o755)) |_| return error.Taken else |_| {}
    if (c.create("/pane", "one", c9.dmdir | 0o755)) |_| return error.Taken else |_| {}
    if (c.create("/pane", "plain-file", 0o644)) |_| return error.Taken else |_| {}
    if (c.remove("/pane/1/body")) |_| return error.Taken else |_| {}
    if (c.remove("/index")) |_| return error.Taken else |_| {}
    c.clunk(log);
    if (embedded) {
        try expect(std.mem.startsWith(u8, try c.read(a, "/src/pardes.zig"), "const std"));
        try expectEqual(@as(u32, 0o444), (try c.stat("/src/pardes.zig")).mode & 0o777);
        try expect(has(try c.read(a, f.at("/pane/{d}/body", .{try c.look(a, "/virtual/src/pardes.zig")})), "pub const Pardes"));
    }
    // An open of data with OTRUNC (a shell's >) deletes the addressed range
    // only, so the write after it replaces that range.
    const scratch = try c.newPane();
    try c.write(f.at("/pane/{d}/body", .{scratch}), "hello world\nsecond line\n");
    try c.write(f.at("/pane/{d}/addr", .{scratch}), "#0,#5");
    try c.replace(f.at("/pane/{d}/data", .{scratch}), "HOWDY");
    try expectEqualStrings("HOWDY world\nsecond line\n", try c.read(a, f.at("/pane/{d}/body", .{scratch})));
    // sam's regexps: ^ at any line; a miss says so and leaves no address,
    // so the data write after it refuses.
    try c.write(f.at("/pane/{d}/addr", .{scratch}), "/^second/");
    try expectEqualStrings("         12          18 ", try c.read(a, f.at("/pane/{d}/addr", .{scratch})));
    try refusedSaying(c, f.at("/pane/{d}/addr", .{scratch}), "/nowhere/", "no match for regexp");
    try refusedSaying(c, f.at("/pane/{d}/data", .{scratch}), "LOST", "no address");
    try expectEqualStrings("HOWDY world\nsecond line\n", try c.read(a, f.at("/pane/{d}/body", .{scratch})));
    // Through a mount both read as EINVAL; the log says which and why.
    try expect(has(try c.read(a, "/log"), f.at("err {d} addr: no match for regexp\n", .{scratch})));
    try c.remove(f.at("/pane/{d}", .{scratch}));
    // Real `diff -u` in a command pane: the terminal turns the tab before
    // each timestamp into blanks, and a look at a changed line still opens
    // the new file at it.
    try d.put("diff-old.txt", "one\ntwo\nthree\n");
    try d.put("diff-new.txt", "one\nTWO\nthree\n");
    const exits = std.mem.count(u8, try c.read(a, "/log"), "\nexit ");
    try c.write("/exec", "diff -u diff-old.txt diff-new.txt\n");
    const deadline = eh.nowMs() + 10_000;
    while (std.mem.count(u8, try c.read(a, "/log"), "\nexit ") <= exits) {
        if (eh.nowMs() > deadline) return error.DiffNeverEnded;
        process.sleep(50);
    }
    // A command's pane is kind cmd, not term: no shell to type into.
    var ran: u32 = 0;
    var irows = std.mem.tokenizeScalar(u8, try c.read(a, "/index"), '\n');
    while (irows.next()) |r| {
        var w = std.mem.tokenizeScalar(u8, r, ' ');
        const serial = w.next().?;
        if (std.mem.eql(u8, w.next() orelse "", "cmd")) ran = try std.fmt.parseInt(u32, serial, 10);
    }
    const shown = try c.read(a, f.at("/pane/{d}/body", .{ran}));
    try expect(has(shown, "+++ diff-new.txt") and !has(shown, "\t"));
    try c.write(f.at("/pane/{d}/look", .{ran}), "+TWO\n");
    const diffed = try panesAt(a, c, f.at("{s}/diff-new.txt", .{root}));
    try expectEqual(@as(usize, 1), diffed.len);
    try c.remove(f.at("/pane/{s}", .{diffed[0]}));
    try c.remove(f.at("/pane/{d}", .{ran}));
    // A Save makes the directories its file goes in, whatever names it: the
    // pane's own name or Save's path. `~` is home (this session's HOME, the
    // test's root) wherever a path is typed: Save, name, a look, with an
    // address or not.
    const tilde = try c.newPane();
    try c.write(f.at("/pane/{d}/body", .{tilde}), "one\ntwo\nthree\n");
    try c.write(f.at("/pane/{d}/ctl", .{tilde}), "Save ~/tilde-made/sub/x.txt\n");
    try expectEqualStrings("one\ntwo\nthree\n", try d.get(a, "tilde-made/sub/x.txt"));
    try c.write(f.at("/pane/{d}/name", .{tilde}), "~/tilde-named.txt\n");
    try expectEqualStrings(f.at("{s}/tilde-named.txt\n", .{root}), try c.read(a, f.at("/pane/{d}/name", .{tilde})));
    try c.remove(f.at("/pane/{d}", .{tilde}));
    try c.write("/look", "~/tilde-made/sub/x.txt:3\n");
    const looked = try panesAt(a, c, f.at("{s}/tilde-made/sub/x.txt", .{root}));
    try expectEqual(@as(usize, 1), looked.len);
    // At line 3: dot, offset 8, where the look put it (addr is a 9P client's own register).
    try expect(std.mem.startsWith(u8, std.mem.trimStart(u8, try c.read(a, f.at("/pane/{s}/dot", .{looked[0]})), " "), "8 "));
    try c.remove(f.at("/pane/{s}", .{looked[0]}));
    const made_dirs = try c.newPane();
    try c.write(f.at("/pane/{d}/body", .{made_dirs}), "deep\n");
    try c.write(f.at("/pane/{d}/ctl", .{made_dirs}), f.at("Save {s}/save-made/a/b/x.txt\n", .{root}));
    try expectEqualStrings("deep\n", try d.get(a, "save-made/a/b/x.txt"));
    try c.write(f.at("/pane/{d}/name", .{made_dirs}), f.at("{s}/save-named/c/y.txt\n", .{root}));
    try c.write(f.at("/pane/{d}/ctl", .{made_dirs}), "Save\n");
    try expectEqualStrings("deep\n", try d.get(a, "save-named/c/y.txt"));
    try c.remove(f.at("/pane/{d}", .{made_dirs}));
    // A save the host cannot do (a directory it may not make) fails the
    // write that asked for it, and the log's err record names the path and
    // why.
    const unsaved = try c.newPane();
    try c.write(f.at("/pane/{d}/body", .{unsaved}), "unsaved\n");
    try refusedSaying(c, f.at("/pane/{d}/ctl", .{unsaved}), "Save /nonexistent-pardes-dir/x.txt\n", "Save /nonexistent-pardes-dir/x.txt: permission denied"); // a directory it may not make
    try expect(has(try c.read(a, "/log"), f.at("err {d} ctl: Save /nonexistent-pardes-dir/x.txt: ", .{unsaved})));
    try c.remove(f.at("/pane/{d}", .{unsaved}));
    // Sixteen connections fill the slots; the next is told why.
    {
        var extra: [15]*process.Conn = undefined;
        var n: usize = 0;
        defer for (extra[0..n]) |e| e.deinit(gpa);
        while (n < extra.len) : (n += 1) extra[n] = try process.Conn.dial(gpa, d.socket());
        var words: [256]u8 = undefined;
        try expect(has(try process.Conn.dialRefusal(gpa, d.socket(), &words), "too many connections"));
    }
    const until = eh.nowMs() + 5_000;
    while (!has(try c.read(a, "/log"), "err - 9p: too many connections")) {
        if (eh.nowMs() > until) return error.NotLogged;
        process.sleep(10);
    }
}

fn screenSize(f: *process.Files) !struct { i64, i64 } {
    const json = try std.json.parseFromSliceLeaky(std.json.Value, f.arena.allocator(), try f.read("/screen"), .{});
    return .{ json.object.get("cols").?.integer, json.object.get("rows").?.integer };
}

/// A write bigger than a 9P message comes as several, cut anywhere: each
/// command line runs once, whole, an Edit block once it ends, and a last
/// line with no newline when the open closes. Never a fragment.
fn cutWrites(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
    var d = try process.Detached.start(gpa, exe, "cut", .{});
    defer d.deinit();
    const f = &d.files;
    var arena = std.heap.ArenaAllocator.init(gpa);
    defer arena.deinit();
    const a = arena.allocator();
    const c = try process.Conn.dial(gpa, d.socket());
    defer c.deinit(gpa);
    // As test/fs.py's client cut them: an 8192-byte msize's writes.
    const cut = 8192 - 23;
    var burst: std.ArrayList(u8) = .empty;
    for (0..1000) |i| try burst.print(a, "Msg burst-{d:0>4}\n", .{i});
    try expect(burst.items.len > 8192);
    try c.writeCut("/exec", 1, burst.items, cut);
    const log = try c.read(a, "/log");
    var i: usize = 0;
    var lines = std.mem.tokenizeScalar(u8, log, '\n');
    while (lines.next()) |line| {
        if (!std.mem.startsWith(u8, line, "msg ") or !has(line, "burst-")) continue;
        var w = std.mem.splitScalar(u8, line, ' ');
        _ = w.next();
        _ = w.next();
        try expectEqualStrings(f.at("burst-{d:0>4}", .{i}), w.rest());
        i += 1;
    }
    try expectEqual(@as(usize, 1000), i);
    try expect(!has(log, "\nrun ") and !has(log, "exit 127"));
    try c.write("/exec", "Msg no-newline-at-end");
    const said = try c.read(a, "/log");
    const upto = said[0..std.mem.indexOf(u8, said, "no-newline-at-end").?];
    try expect(has(upto[upto.len -| 40..], "msg "));
    const serial = try c.newPane();
    var block: std.ArrayList(u8) = .empty;
    try block.appendSlice(a, "Edit ,a\n");
    for (0..2500) |k| try block.print(a, "line {d:0>5} of the block\n", .{k});
    try block.appendSlice(a, ".\n");
    try expect(block.items.len > 50000);
    try c.writeCut(f.at("/pane/{d}/ctl", .{serial}), 1, block.items, cut);
    var body = std.mem.splitScalar(u8, std.mem.trimEnd(u8, try c.read(a, f.at("/pane/{d}/body", .{serial})), "\n"), '\n');
    var k: usize = 0;
    while (body.next()) |line| : (k += 1) try expectEqualStrings(f.at("line {d:0>5} of the block", .{k}), line);
    try expectEqual(@as(usize, 2500), k);
    // A detached session with no frontend is 160x50 until told.
    const cols, const rows = try screenSize(f);
    try expect(cols == 160 and rows == 50);
    try c.write("/ctl", "size 132 40\n");
    const cols2, const rows2 = try screenSize(f);
    try expect(cols2 == 132 and rows2 == 40);
}