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
|
//! The window's own (T4 of docs/next-tests.md): what only its pixels and
//! its own input show, on the hidden window `zig build process-test
//! -Dplatform=gui` drives. test/gui_monkey.py and test/gui_golden.py, here.
const std = @import("std");
const process = @import("../process.zig");
extern "c" fn system(cmd: [*:0]const u8) c_int;
pub const cases = .{
.{ "window: a monkey over the effects: random clicks, drags, wheels, keys, columns, looks and themes, then it settles, alive, with no crash record", monkey },
.{ "window: its scenes are the frames test/gui-goldens.txt hashes (PARDES_GUI_GOLDEN=check|update, this machine's)", goldens },
};
fn envOr(name: [*:0]const u8, default: []const u8) []const u8 {
return if (std.c.getenv(name)) |v| std.mem.span(v) else default;
}
/// The serial of the first pane whose tag holds `word`, waited for.
fn paneWith(f: *process.Files, word: []const u8) !u32 {
const deadline = process.nowMs() + 10_000;
while (process.nowMs() < deadline) : (process.nap(50)) {
var rows = std.mem.tokenizeScalar(u8, try f.read("/index"), '\n');
while (rows.next()) |row| {
const serial = std.fmt.parseInt(u32, row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse continue], 10) catch continue;
if (process.has(try f.read(f.at("/pane/{d}/tag", .{serial})), word)) return serial;
}
}
std.debug.print("no pane's tag holds {s}\n", .{word});
return error.NoSuchPane;
}
const motions = [_][]const u8{ "off", "crisp", "smooth", "bouncy", "playful" };
const lifts = [_][]const u8{ "off", "shadow", "rim", "auto" };
const blooms = [_][]const u8{ "0", "2" };
const themes = [_][]const u8{ "lapis", "lapis_plain", "forge", "acme", "dusk", "daybreak" };
/// test/gui_monkey.py's: each Motion x Lift x Bloom gets a random burst,
/// then must settle with its process alive and no crash record. Two
/// combinations and 30 steps in process-test; PARDES_GUI_MONKEY=all runs
/// all 40 (`zig build monkey-gui`), PARDES_GUI_MONKEY_SEED and _STEPS
/// choose the rest.
fn monkey(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try process.only(.gui);
const all = std.mem.eql(u8, envOr("PARDES_GUI_MONKEY", ""), "all");
const seed = try std.fmt.parseInt(u64, envOr("PARDES_GUI_MONKEY_SEED", "1"), 10);
const steps = try std.fmt.parseInt(usize, envOr("PARDES_GUI_MONKEY_STEPS", if (all) "60" else "30"), 10);
var failed: usize = 0;
var n: usize = 0;
for (motions) |m| for (lifts) |l| for (blooms) |b| {
defer n += 1;
// The smoke run: the quietest combination and the busiest.
if (!all and n != 0 and n != motions.len * lifts.len * blooms.len - 1) continue;
if (!try monkeyOne(gpa, exe, m, l, b, seed * 1000 + n, steps)) failed += 1;
};
if (failed > 0) return error.MonkeyFailed;
}
fn monkeyOne(gpa: std.mem.Allocator, exe: [:0]const u8, motion: []const u8, lift: []const u8, bloom: []const u8, seed: u64, steps: usize) !bool {
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
const init = try std.fmt.allocPrint(a, "Shell /bin/sh\nMessageLinger 0\nMotion {s}\nLift {s}\nBloom {s}\nPanelSlide\nSelectionGlow on\nHoverGlow on\n", .{ motion, lift, bloom });
var d = try process.Detached.start(gpa, exe, "monkey", .{ .init = init, .grid = .{ 80, 28 }, .env = &.{
.{ "PARDES_TEST_CLOCK", "1" }, .{ "PARDES_TEST_PAD", "0" },
} });
defer d.deinit();
const f = &d.files;
var src: std.ArrayList(u8) = .empty;
for (0..200) |k| try src.print(a, "pub fn f{d}(x: u32) u32 {{ return x * {d}; }} // a line\n", .{ k, k });
try d.put("a.zig", src.items);
for ([_][]const u8{ "b.txt", "c.txt" }) |name| {
var text: std.ArrayList(u8) = .empty;
for (0..80) |k| try text.print(a, "{s} line {d} with some words\n", .{ name, k });
try d.put(name, text.items);
}
const names = [_][]const u8{ "b.txt", "c.txt", "a.zig" };
var prng = std.Random.DefaultPrng.init(seed);
const r = prng.random();
const w: u32 = 1280;
const h: u32 = 840;
var log: std.ArrayList(u8) = .empty;
for (0..steps) |_| {
const x = r.uintLessThan(u32, w);
const y = r.uintLessThan(u32, h);
const action = r.uintLessThan(u32, 12);
// A refusal is part of the burst (a Delcol of the last column, a
// look at a moved pane): logged, never the verdict.
step(&d, f, a, r, action, x, y, &names, &log) catch |err| try log.print(a, "refused {t}\n", .{err});
process.nap(30);
}
var verdict: []const u8 = "ok";
d.settle(gpa) catch {
verdict = "never settled";
};
if (d.exited(0) != null) verdict = "process gone";
_ = f.read("/index") catch {
verdict = "no /index";
};
const config = try std.fmt.allocPrint(a, "{s}/config/pardes", .{d.root()});
if (process.entries(a, config)) |listed| {
for (listed) |name| if (process.has(name, "crash")) {
verdict = try std.fmt.allocPrint(a, "crash record {s}", .{name});
};
} else |_| {}
const ok = std.mem.eql(u8, verdict, "ok");
if (!ok) std.debug.print("monkey {s}-{s}-bloom{s} (seed {d}): {s}\n{s}", .{ motion, lift, bloom, seed, verdict, log.items });
return ok;
}
fn step(d: *process.Detached, f: *process.Files, a: std.mem.Allocator, r: std.Random, action: u32, x: u32, y: u32, names: []const []const u8, log: *std.ArrayList(u8)) !void {
switch (action) {
0, 1 => {
try d.mouse("down", 1, x, y);
try d.mouse("up", 1, x, y);
try log.print(a, "click {d},{d}\n", .{ x, y });
},
2 => {
const x2 = r.uintLessThan(u32, 1280);
const y2 = r.uintLessThan(u32, 840);
try d.mouse("down", 1, x, y);
for (1..6) |i| {
const k: i64 = @intCast(i);
try d.mouse("motion", 1, @intCast(@as(i64, x) + @divTrunc((@as(i64, x2) - x) * k, 5)), @intCast(@as(i64, y) + @divTrunc((@as(i64, y2) - y) * k, 5)));
}
try d.mouse("up", 1, x2, y2);
try log.print(a, "drag {d},{d}->{d},{d}\n", .{ x, y, x2, y2 });
},
3 => {
const amount = ([_]i32{ -6, -2, 2, 6 })[r.uintLessThan(usize, 4)];
try d.mouse("wheel", amount, x, y);
try log.print(a, "wheel {d}\n", .{amount});
},
4 => {
try d.mouse("motion", 0, x, y);
try log.print(a, "hover {d},{d}\n", .{ x, y });
},
5 => {
try log.print(a, "Newcol\n", .{});
try f.write("/ctl", "Newcol\n");
},
6 => if (try firstPane(f)) |serial| {
const target = names[r.uintLessThan(usize, names.len)];
try log.print(a, "look {s}\n", .{target});
try f.write(f.at("/pane/{d}/look", .{serial}), f.at("{s}/{s}\n", .{ d.root(), target }));
},
7 => if (try firstPane(f)) |serial| {
// Delcol too: the last column's going leaves an empty window
// (acme's), not a quit.
const word = ([_][]const u8{ "Joincol", "Collapse", "Delcol" })[r.uintLessThan(usize, 3)];
try log.print(a, "{s}\n", .{word});
try f.write(f.at("/pane/{d}/exec", .{serial}), f.at("{s}\n", .{word}));
} else {
// An empty window: pane/new makes its column and a pane.
try log.print(a, "pane/new (empty window)\n", .{});
_ = try f.newPane();
},
8 => {
const keys = ([_][]const u8{ "j", "k", "10j", "gg", "w", "%", "v5j", "\x1b" })[r.uintLessThan(usize, 8)];
try log.print(a, "keys\n", .{});
try d.typeKeys(keys);
},
9 => {
const theme = themes[r.uintLessThan(usize, themes.len)];
try log.print(a, "Theme {s}\n", .{theme});
try f.write("/ctl", f.at("Theme {s}\n", .{theme}));
},
10 => {
try d.mouse("down", 3, x, y);
try d.mouse("up", 3, x, y);
try log.print(a, "look-click {d},{d}\n", .{ x, y });
},
else => {
process.nap(([_]u32{ 0, 50, 200 })[r.uintLessThan(usize, 3)]);
try log.print(a, "pause\n", .{});
},
}
}
fn firstPane(f: *process.Files) !?u32 {
const index = try f.read("/index");
const end = std.mem.indexOfScalar(u8, index, ' ') orelse return null;
return std.fmt.parseInt(u32, index[0..end], 10) catch null;
}
/// test/gui_golden.py's scenes, each frame's sha256 against
/// test/gui-goldens.txt: an exact gate for one machine (a GPU, a driver,
/// software rendering here draw their own pixels), never a CI one, so it
/// runs only when asked: PARDES_GUI_GOLDEN=check, or =update to rewrite the
/// goldens; PARDES_GUI_GOLDEN_KEEP=<dir> keeps each frame as <scene>.ppm.
/// The root is fixed: its paths are drawn in the tags.
fn goldens(gpa: std.mem.Allocator, exe: [:0]const u8) !void {
try process.only(.gui);
const mode = envOr("PARDES_GUI_GOLDEN", "");
if (!std.mem.eql(u8, mode, "check") and !std.mem.eql(u8, mode, "update")) return error.SkipZigTest;
var arena = std.heap.ArenaAllocator.init(gpa);
defer arena.deinit();
const a = arena.allocator();
const root = "/tmp/pardes-gui-golden";
_ = system("rm -rf -- /tmp/pardes-gui-golden");
if (std.c.mkdir(root, 0o700) != 0) return error.NoRoot;
defer _ = system("rm -rf -- /tmp/pardes-gui-golden");
var zig: std.ArrayList(u8) = .empty;
try zig.appendSlice(a,
\\const std = @import("std");
\\
\\pub const Shape = union(enum) {
\\ circle: f32,
\\ square: f32,
\\
\\ pub fn area(shape: Shape) f32 {
\\ return switch (shape) {
\\ .circle => |r| std.math.pi * r * r,
\\ .square => |s| s * s,
\\ };
\\ }
\\};
\\
\\pub fn main() void {
\\ var total: f32 = 0;
\\ const shapes = [_]Shape{ .{ .circle = 1 }, .{ .square = 2 } };
\\ for (shapes) |shape| {
\\
);
for (0..60) |n| try zig.print(a, " total += shape.area() * {d}; // line {d}\n", .{ n, n });
try zig.appendSlice(a, " }\n std.debug.print(\"{d}\\n\", .{total});\n}\n");
try process.writeFile(a, root ++ "/shapes.zig", zig.items);
var notes: std.ArrayList(u8) = .empty;
for (0..4) |_| try notes.appendSlice(a, "Pardes GUI golden\n\nA second document, opened by Look.\n");
try process.writeFile(a, root ++ "/notes.txt", notes.items);
// One of the two alignments puts a wide glyph across the body's right edge.
var wide: std.ArrayList(u8) = .empty;
try wide.append(a, 'x');
for (0..80) |_| try wide.appendSlice(a, "\u{754c}");
try wide.append(a, '\n');
for (0..80) |_| try wide.appendSlice(a, "\u{754c}");
try wide.append(a, '\n');
try process.writeFile(a, root ++ "/wide.txt", wide.items);
var ppm: std.ArrayList(u8) = .empty;
try ppm.print(a, "P6\n{d} {d}\n255\n", .{ 96, 64 });
for (0..64) |yy| for (0..96) |xx| try ppm.appendSlice(a, &.{ @intCast(xx * 255 / 96), @intCast(yy * 255 / 64), 128 });
try process.writeFile(a, root ++ "/ramp.ppm", ppm.items);
var d = try process.Detached.start(gpa, exe, "gui-golden", .{
.root = root,
.file = root ++ "/shapes.zig",
.init = "Shell /bin/sh\nTheme forge\nMessageLinger 0\n",
// The virtual clock: a transition's frames are the same frames on
// any machine, so one partway through can be hashed too.
.grid = .{ 120, 40 },
.env = &.{ .{ "PARDES_TEST_CLOCK", "1" }, .{ "PARDES_TEST_PAD", "7" } },
});
defer d.deinit();
const f = &d.files;
var results: std.ArrayList(u8) = .empty;
const keep = std.c.getenv("PARDES_GUI_GOLDEN_KEEP");
const Scene = struct {
fn shot(dd: *process.Detached, al: std.mem.Allocator, out: *std.ArrayList(u8), kept: ?[*:0]const u8, label: []const u8, file: []const u8) !void {
const bytes = try dd.capture(al, file);
var digest: [32]u8 = undefined;
std.crypto.hash.sha2.Sha256.hash(bytes, &digest, .{});
try out.print(al, "{s} {x}\n", .{ label, digest });
if (kept) |dir| try process.writeFile(al, try std.fmt.allocPrint(al, "{s}/{s}.ppm", .{ std.mem.span(dir), label }), bytes);
std.debug.print("{s} {x}\n", .{ label, digest[0..8] });
}
};
const shot = Scene.shot;
const first = try paneWith(f, "shapes.zig");
try shot(&d, a, &results, keep, "01-boot", "latest.ppm");
try f.write(f.at("/pane/{d}/look", .{first}), root ++ "/notes.txt\n");
const notes_pane = try paneWith(f, "notes.txt");
try shot(&d, a, &results, keep, "02-second-document", "latest.ppm");
try f.write(f.at("/pane/{d}/ctl", .{first}), "Msg a golden notice\n");
try shot(&d, a, &results, keep, "03-notice", "latest.ppm");
try f.write(f.at("/pane/{d}/ctl", .{first}), "TreeContext\n");
try f.write(f.at("/pane/{d}/dot", .{first}), "45\n");
try shot(&d, a, &results, keep, "04-context-rows-mid-thumb", "latest.ppm");
try f.write(f.at("/pane/{d}/dot", .{first}), "40,43\n");
try shot(&d, a, &results, keep, "05-body-selection", "latest.ppm");
// A press and a drag along the source's tag: the tag takes the keyboard
// with a selection and a caret. The pointer then rests on empty body
// space, far right.
for ([_]struct { []const u8, i32, u32, u32 }{ .{ "down", 1, 500, 87 }, .{ "motion", 1, 620, 87 }, .{ "up", 1, 620, 87 }, .{ "motion", 0, 1880, 1300 } }) |ev| {
try d.mouse(ev[0], ev[1], ev[2], ev[3]);
process.nap(100);
}
try shot(&d, a, &results, keep, "06-tag-focus-selection", "latest.ppm");
try d.typeKeys("\x1b");
process.nap(200);
const scratch = try f.newPane();
try f.write(f.at("/pane/{d}/exec", .{scratch}), "Save\n");
try shot(&d, a, &results, keep, "07-prompt-notice", "latest.ppm");
try d.typeKeys("\x1b");
process.nap(200);
try f.write(f.at("/pane/{d}/exec", .{scratch}), "Del\n");
try f.write(f.at("/pane/{d}/look", .{first}), root ++ "/wide.txt\n");
const wide_pane = try paneWith(f, "wide.txt");
var name: std.ArrayList(u8) = .empty;
try name.append(a, 'x');
for (0..70) |_| try name.appendSlice(a, "\u{754c}");
try name.append(a, '\n');
try f.write(f.at("/pane/{d}/name", .{wide_pane}), name.items);
try f.write("/ctl", "Wrap off\n");
try shot(&d, a, &results, keep, "08-wide-glyph-edges", "latest.ppm");
try f.write("/ctl", "Wrap on\n");
try f.write(f.at("/pane/{d}/exec", .{wide_pane}), "Del\n");
try f.write("/ctl", "FocusTint on\n");
try f.write(f.at("/pane/{d}/ctl", .{notes_pane}), "Collapse\n");
try shot(&d, a, &results, keep, "09-collapsed-focus-tint", "latest.ppm");
try f.write(f.at("/pane/{d}/ctl", .{notes_pane}), "Collapse\n");
try f.write("/ctl", "Tagbottom\n");
try shot(&d, a, &results, keep, "10-tagbottom", "latest.ppm");
try f.write("/ctl", "Tagbottom\n");
try f.write(f.at("/pane/{d}/tag", .{first}), " Second\nthird line of the tag");
try shot(&d, a, &results, keep, "12-multiline-tag", "latest.ppm");
try f.write(f.at("/pane/{d}/look", .{first}), root ++ "/ramp.ppm\n");
_ = try paneWith(f, "ramp.ppm");
try shot(&d, a, &results, keep, "13-image", "latest.ppm");
try f.write("/ctl", "Theme ink\n");
try shot(&d, a, &results, keep, "14-theme-ink", "latest.ppm");
try f.write("/ctl", "Theme acme\n");
try shot(&d, a, &results, keep, "15-theme-acme-light", "latest.ppm");
try f.write("/ctl", "Debug\n");
try shot(&d, a, &results, keep, "16-debug", "latest.ppm");
try f.write("/ctl", "Debug\n");
// A terminal pane: raw rows, ANSI colours, a hidden prompt.
const before = try f.read("/index");
try f.write(f.at("/pane/{d}/exec", .{first}), "Term\n");
const terminal = blk: {
const deadline = process.nowMs() + 10_000;
while (process.nowMs() < deadline) : (process.nap(50)) {
var rows = std.mem.tokenizeScalar(u8, try f.read("/index"), '\n');
while (rows.next()) |row| {
const head = row[0 .. std.mem.indexOfScalar(u8, row, ' ') orelse continue];
if (std.mem.indexOf(u8, before, f.at("{s} ", .{head})) == null) break :blk try std.fmt.parseInt(u32, head, 10);
}
}
return error.NoTerminal;
};
try f.write(f.at("/pane/{d}/pty/data", .{terminal}), "PS1='$ '; clear; printf '\\033[1;36mcolour\\033[0m plain \\033[32mgreen\\033[0m\\n'\r");
{
const deadline = process.nowMs() + 10_000;
while (!process.has(try f.read(f.at("/pane/{d}/body", .{terminal})), "green")) : (process.nap(50))
if (process.nowMs() > deadline) return error.NoTerminalOutput;
}
try shot(&d, a, &results, keep, "17-terminal", "latest.ppm");
// Partway through a transition: the panes, the picture among them,
// sliding over for a new column, frame 6 of PanelSlide's 12.
try f.write("/ctl", "PanelSlide\n");
try f.write(f.at("/pane/{d}/exec", .{first}), "Newcol\n");
try shot(&d, a, &results, keep, "18-mid-transition", "transition-6.ppm");
const goldens_path = "test/gui-goldens.txt";
if (std.mem.eql(u8, mode, "update")) {
try process.writeFile(a, goldens_path, results.items);
std.debug.print("updated {s}\n", .{goldens_path});
return;
}
const want = try process.readWhole(a, goldens_path);
var bad: usize = 0;
var got = std.mem.tokenizeScalar(u8, results.items, '\n');
while (got.next()) |line| if (std.mem.indexOf(u8, want, line) == null) {
bad += 1;
std.debug.print("frame changed: {s}\n", .{line[0..std.mem.indexOfScalar(u8, line, ' ').?]});
};
if (bad > 0) return error.FramesChanged;
}
|