//! 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 themes = [_][]const u8{ "lapis", "lapis_plain", "forge", "acme", "dusk", "daybreak" }; /// test/gui_monkey.py's: each Motion gets a random burst, then must /// settle with its process alive and no crash record. Two of them and 30 /// steps in process-test; PARDES_GUI_MONKEY=all runs all 5 (`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| { defer n += 1; // The smoke run: the quietest and the busiest. if (!all and n != 0 and n != motions.len - 1) continue; if (!try monkeyOne(gpa, exe, m, 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, 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}\n", .{motion}); 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} (seed {d}): {s}\n{s}", .{ motion, 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= keeps each frame as .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: the same frames on any machine. .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"); 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; }