//! The window's own (T4 of docs/next-tests.md): its own input, on the //! hidden window `zig build process-test -Dplatform=gui` drives, settling //! alive. Its pixels are its unit tests' (`unit-test -Dplatform=gui`) and //! the image and kitty harnesses'; what the core draws, the tty cases'. const std = @import("std"); const process = @import("../process.zig"); 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 }, }; fn envOr(name: [*:0]const u8, default: []const u8) []const u8 { return if (std.c.getenv(name)) |v| std.mem.span(v) else default; } const themes = [_][]const u8{ "lapis", "forge", "acme", "dusk", "daybreak" }; /// test/gui_monkey.py's: random bursts, each run then settling with its /// process alive and no crash record. Two runs of 30 steps in /// process-test; PARDES_GUI_MONKEY=all runs 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; for (0..@as(usize, if (all) 5 else 2)) |n| { if (!try monkeyOne(gpa, exe, seed * 1000 + n, steps)) failed += 1; } if (failed > 0) return error.MonkeyFailed; } fn monkeyOne(gpa: std.mem.Allocator, exe: [:0]const u8, seed: u64, steps: usize) !bool { var arena = std.heap.ArenaAllocator.init(gpa); defer arena.deinit(); const a = arena.allocator(); const init = "Shell /bin/sh\nMessageLinger 0\n"; 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 (seed {d}): {s}\n{s}", .{ 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; }