graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs
Python
test(tui): keep issue 537 checks reachable
yxlyx0823491667ea
diff --git a/TUI/dispatch.zig b/TUI/dispatch.zigindex 7e3633b7..935cba68 100644--- a/TUI/dispatch.zig+++ b/TUI/dispatch.zig@@ -377,233 +377,7 @@ fn onOff(v: bool) []const u8 {return if (v) "on" else "off";}-test "applyLine /quit and /new" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- try std.testing.expectEqual(Effect.quit, applyLine(&m, "/quit"));- m.quit_requested = false;- try m.push(.user, "keep me");- _ = applyLine(&m, "/new");- try std.testing.expectEqual(app.Screen.welcome, m.screen);- try std.testing.expectEqual(@as(usize, 1), m.history.items.len); // system notice-}--test "/debug opens the observability overlay" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- _ = applyLine(&m, "/debug");- try std.testing.expectEqual(app.Overlay.debug, m.overlay);-}--test "/cache opens the same observability overlay" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- _ = applyLine(&m, "/cache");- try std.testing.expectEqual(app.Overlay.debug, m.overlay);-}--test "/usage is not a char-count view" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- _ = applyLine(&m, "/usage");- const text = m.history.items[m.history.items.len - 1].text;- try std.testing.expect(std.mem.indexOf(u8, text, "chars sent") == null);- try std.testing.expect(std.mem.indexOf(u8, text, "no session sink") != null);-}--test "rewind drops the last user turn" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- try m.push(.user, "one");- try m.push(.assistant, "two");- rewind(&m);- try std.testing.expectEqual(@as(usize, 1), m.history.items.len); // rewind notice-}--test "core pager commands change shipped model state" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- try std.testing.expectEqual(Effect.quit, applyLine(&m, "/exit"));- m.quit_requested = false;- try std.testing.expectEqual(Effect.quit, applyLine(&m, "/q"));- m.quit_requested = false;-- _ = applyLine(&m, "/help");- try std.testing.expectEqual(app.Overlay.help, m.overlay);- m.closeOverlay();-- try m.push(.user, "stay");- _ = applyLine(&m, "/home");- try std.testing.expectEqual(app.Screen.welcome, m.screen);- try std.testing.expectEqual(app.Focus.prompt, m.focus);-- try std.testing.expectEqual(app.AgentMode.normal, m.mode);- _ = applyLine(&m, "/plan");- try std.testing.expectEqual(app.AgentMode.plan, m.mode);- _ = applyLine(&m, "/plan");- try std.testing.expectEqual(app.AgentMode.normal, m.mode);- _ = applyLine(&m, "/always-approve");- try std.testing.expectEqual(app.AgentMode.always_approve, m.mode);- _ = applyLine(&m, "/yolo");- try std.testing.expectEqual(app.AgentMode.normal, m.mode);-- _ = applyLine(&m, "/settings");- try std.testing.expectEqual(app.Overlay.settings, m.overlay);- m.closeOverlay();-- _ = applyLine(&m, "/model");- try std.testing.expectEqual(app.Overlay.model, m.overlay);- m.closeOverlay();-- _ = applyLine(&m, "/clear");- try std.testing.expectEqual(app.Screen.welcome, m.screen);-}--test "/usage with a session HUD is the cost line, not chars" {- engine.g_hud_fn = struct {- fn f(kind: engine.HudKind, buf: []u8) usize {- if (kind != .usage) return 0;- const s = "1 api call(s) · 1200 in (200 cached) + 50 out tokens · $0.0123\n";- const n = @min(s.len, buf.len);- @memcpy(buf[0..n], s[0..n]);- return n;- }- }.f;- defer engine.g_hud_fn = null;- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- _ = applyLine(&m, "/cost");- const text = m.history.items[m.history.items.len - 1].text;- try std.testing.expect(std.mem.indexOf(u8, text, "api call(s)") != null);- try std.testing.expect(std.mem.indexOf(u8, text, "$0.0123") != null);- try std.testing.expect(std.mem.indexOf(u8, text, "chars sent") == null);- try std.testing.expect(std.mem.indexOf(u8, text, "offline") == null);-}--test "every slash name printed in /help dispatches" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- m.openOverlay(.help);- var arena = std.heap.ArenaAllocator.init(std.testing.allocator);- defer arena.deinit();- const text = try @import("chrome.zig").overlay(&m, arena.allocator(), 80);- m.closeOverlay();-- var seen: usize = 0;- var i: usize = 0;- while (i < text.len) {- if (text[i] != '/') {- i += 1;- continue;- }- var j = i + 1;- while (j < text.len and (std.ascii.isAlphanumeric(text[j]) or text[j] == '-')) j += 1;- if (j == i + 1) {- i += 1;- continue;- }- const name = text[i..j];- const before = m.history.items.len;- const effect = applyLine(&m, name);- if (std.mem.eql(u8, name, "/quit") or std.mem.eql(u8, name, "/exit") or std.mem.eql(u8, name, "/q")) {- try std.testing.expectEqual(Effect.quit, effect);- m.quit_requested = false;- } else {- try std.testing.expectEqual(Effect.stay, effect);- }- if (m.history.items.len > before) {- const last = m.history.items[m.history.items.len - 1].text;- try std.testing.expect(std.mem.indexOf(u8, last, "unknown command") == null);- }- seen += 1;- i = j;- }- try std.testing.expect(seen >= 9);-}--test "/image attaches a path the next send carries as @[path]" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- _ = applyLine(&m, "/image /tmp/shot.png");- try std.testing.expectEqual(@as(usize, 1), m.images.items.len);- _ = applyLine(&m, "what is this");- var user_text: []const u8 = "";- for (m.history.items) |e| {- if (e.kind == .user) user_text = e.text;- }- try std.testing.expect(std.mem.indexOf(u8, user_text, "@[/tmp/shot.png]") != null);- try std.testing.expect(std.mem.indexOf(u8, user_text, "what is this") != null);- try std.testing.expectEqual(@as(usize, 0), m.images.items.len);-}--test "looksLikeImagePath accepts file URLs and extensions" {- try std.testing.expect(looksLikeImagePath("/tmp/a.png"));- try std.testing.expect(looksLikeImagePath("file:///Users/me/x.JPEG"));- try std.testing.expect(!looksLikeImagePath("hello.png is a format"));- try std.testing.expect(!looksLikeImagePath("readme.md"));- try std.testing.expect(looksLikeImagePath("/Users/me/My Shot.png"));- try std.testing.expect(!looksLikeImagePath("see /tmp/a.png"));-}--test "/effort with no arg opens the effort menu" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- m.effort = .high;- try std.testing.expectEqual(app.Effect.stay, applyLine(&m, "/effort"));- try std.testing.expectEqual(app.Overlay.effort, m.overlay);- try std.testing.expectEqual(@as(usize, @intFromEnum(engine.Effort.high)), m.overlay_sel);- try std.testing.expectEqual(app.Effect.stay, applyLine(&m, "/effort low"));- try std.testing.expectEqual(engine.Effort.low, m.effort);-}--test "/vim-mode toggles and /jump without turns explains" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- try std.testing.expect(!m.vim_mode);- _ = applyLine(&m, "/vim-mode");- try std.testing.expect(m.vim_mode);- _ = applyLine(&m, "/vim");- try std.testing.expect(!m.vim_mode);- _ = applyLine(&m, "/jump");- const last = m.history.items[m.history.items.len - 1].text;- try std.testing.expect(std.mem.indexOf(u8, last, "nothing to jump") != null);- try m.push(.user, "hi");- _ = applyLine(&m, "/jump");- try std.testing.expectEqual(app.Overlay.jump, m.overlay);-}--test "/btw queues an aside while a turn is pending" {- var m: Model = undefined;- m.setup(std.testing.allocator);- defer m.deinit();- const job = try std.testing.allocator.create(engine.Job);- job.* = .{ .gpa = std.testing.allocator, .history = &.{}, .params = .{}, .stream = .{}, .threaded = false };- m.pending = job;- defer {- m.pending = null;- std.testing.allocator.destroy(job);- }- _ = applyLine(&m, "/btw remember the tests");- try std.testing.expectEqual(@as(usize, 1), m.steer_queue.items.len);- try std.testing.expectEqualStrings("remember the tests", m.steer_queue.items[0]);-}--test "lastLines caps ! output to the tail" {- try std.testing.expectEqualStrings("c\nd", lastLines("a\nb\nc\nd", 2));- try std.testing.expectEqualStrings("a\nb", lastLines("a\nb", 5));-}-test {_ = @import("dispatch_tests.zig"); // overflow tests (600-line cap)+ _ = @import("dispatch_command_tests.zig");}diff --git a/TUI/dispatch_command_tests.zig b/TUI/dispatch_command_tests.zignew file mode 100644index 00000000..ec9ea260--- /dev/null+++ b/TUI/dispatch_command_tests.zig@@ -0,0 +1,240 @@+//! dispatch.zig command-path tests.++const std = @import("std");++const app = @import("app.zig");+const dispatch = @import("dispatch.zig");+const engine = @import("engine.zig");+const Effect = app.Effect;+const Model = app.Model;+const applyLine = dispatch.applyLine;+const lastLines = dispatch.lastLines;+const looksLikeImagePath = dispatch.looksLikeImagePath;+const rewind = dispatch.rewind;++test "applyLine /quit and /new" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try std.testing.expectEqual(Effect.quit, applyLine(&m, "/quit"));+ m.quit_requested = false;+ try m.push(.user, "keep me");+ _ = applyLine(&m, "/new");+ try std.testing.expectEqual(app.Screen.welcome, m.screen);+ try std.testing.expectEqual(@as(usize, 1), m.history.items.len); // system notice+}++test "/debug opens the observability overlay" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ _ = applyLine(&m, "/debug");+ try std.testing.expectEqual(app.Overlay.debug, m.overlay);+}++test "/cache opens the same observability overlay" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ _ = applyLine(&m, "/cache");+ try std.testing.expectEqual(app.Overlay.debug, m.overlay);+}++test "/usage is not a char-count view" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ _ = applyLine(&m, "/usage");+ const text = m.history.items[m.history.items.len - 1].text;+ try std.testing.expect(std.mem.indexOf(u8, text, "chars sent") == null);+ try std.testing.expect(std.mem.indexOf(u8, text, "no session sink") != null);+}++test "rewind drops the last user turn" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try m.push(.user, "one");+ try m.push(.assistant, "two");+ rewind(&m);+ try std.testing.expectEqual(@as(usize, 1), m.history.items.len); // rewind notice+}++test "core pager commands change shipped model state" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try std.testing.expectEqual(Effect.quit, applyLine(&m, "/exit"));+ m.quit_requested = false;+ try std.testing.expectEqual(Effect.quit, applyLine(&m, "/q"));+ m.quit_requested = false;++ _ = applyLine(&m, "/help");+ try std.testing.expectEqual(app.Overlay.help, m.overlay);+ m.closeOverlay();++ try m.push(.user, "stay");+ _ = applyLine(&m, "/home");+ try std.testing.expectEqual(app.Screen.welcome, m.screen);+ try std.testing.expectEqual(app.Focus.prompt, m.focus);++ try std.testing.expectEqual(app.AgentMode.normal, m.mode);+ _ = applyLine(&m, "/plan");+ try std.testing.expectEqual(app.AgentMode.plan, m.mode);+ _ = applyLine(&m, "/plan");+ try std.testing.expectEqual(app.AgentMode.normal, m.mode);+ _ = applyLine(&m, "/always-approve");+ try std.testing.expectEqual(app.AgentMode.always_approve, m.mode);+ _ = applyLine(&m, "/yolo");+ try std.testing.expectEqual(app.AgentMode.normal, m.mode);++ _ = applyLine(&m, "/settings");+ try std.testing.expectEqual(app.Overlay.settings, m.overlay);+ m.closeOverlay();++ _ = applyLine(&m, "/model");+ try std.testing.expectEqual(app.Overlay.model, m.overlay);+ m.closeOverlay();++ _ = applyLine(&m, "/clear");+ try std.testing.expectEqual(app.Screen.welcome, m.screen);+}++test "/usage with a session HUD is the cost line, not chars" {+ engine.g_hud_fn = struct {+ fn f(kind: engine.HudKind, buf: []u8) usize {+ if (kind != .usage) return 0;+ const s = "1 api call(s) · 1200 in (200 cached) + 50 out tokens · $0.0123\n";+ const n = @min(s.len, buf.len);+ @memcpy(buf[0..n], s[0..n]);+ return n;+ }+ }.f;+ defer engine.g_hud_fn = null;+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ _ = applyLine(&m, "/cost");+ const text = m.history.items[m.history.items.len - 1].text;+ try std.testing.expect(std.mem.indexOf(u8, text, "api call(s)") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "$0.0123") != null);+ try std.testing.expect(std.mem.indexOf(u8, text, "chars sent") == null);+ try std.testing.expect(std.mem.indexOf(u8, text, "offline") == null);+}++test "every slash name printed in /help dispatches" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.openOverlay(.help);+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);+ defer arena.deinit();+ const text = try @import("chrome.zig").overlay(&m, arena.allocator(), 80);+ m.closeOverlay();++ var seen: usize = 0;+ var i: usize = 0;+ while (i < text.len) {+ if (text[i] != '/') {+ i += 1;+ continue;+ }+ var j = i + 1;+ while (j < text.len and (std.ascii.isAlphanumeric(text[j]) or text[j] == '-')) j += 1;+ if (j == i + 1) {+ i += 1;+ continue;+ }+ const name = text[i..j];+ const before = m.history.items.len;+ const effect = applyLine(&m, name);+ if (std.mem.eql(u8, name, "/quit") or std.mem.eql(u8, name, "/exit") or std.mem.eql(u8, name, "/q")) {+ try std.testing.expectEqual(Effect.quit, effect);+ m.quit_requested = false;+ } else {+ try std.testing.expectEqual(Effect.stay, effect);+ }+ if (m.history.items.len > before) {+ const last = m.history.items[m.history.items.len - 1].text;+ try std.testing.expect(std.mem.indexOf(u8, last, "unknown command") == null);+ }+ seen += 1;+ i = j;+ }+ try std.testing.expect(seen >= 9);+}++test "/image attaches a path the next send carries as @[path]" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ _ = applyLine(&m, "/image /tmp/shot.png");+ try std.testing.expectEqual(@as(usize, 1), m.images.items.len);+ _ = applyLine(&m, "what is this");+ var user_text: []const u8 = "";+ for (m.history.items) |e| {+ if (e.kind == .user) user_text = e.text;+ }+ try std.testing.expect(std.mem.indexOf(u8, user_text, "@[/tmp/shot.png]") != null);+ try std.testing.expect(std.mem.indexOf(u8, user_text, "what is this") != null);+ try std.testing.expectEqual(@as(usize, 0), m.images.items.len);+}++test "looksLikeImagePath accepts file URLs and extensions" {+ try std.testing.expect(looksLikeImagePath("/tmp/a.png"));+ try std.testing.expect(looksLikeImagePath("file:///Users/me/x.JPEG"));+ try std.testing.expect(!looksLikeImagePath("hello.png is a format"));+ try std.testing.expect(!looksLikeImagePath("readme.md"));+ try std.testing.expect(looksLikeImagePath("/Users/me/My Shot.png"));+ try std.testing.expect(!looksLikeImagePath("see /tmp/a.png"));+}++test "/effort with no arg opens the effort menu" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ m.effort = .high;+ try std.testing.expectEqual(app.Effect.stay, applyLine(&m, "/effort"));+ try std.testing.expectEqual(app.Overlay.effort, m.overlay);+ try std.testing.expectEqual(@as(usize, @intFromEnum(engine.Effort.high)), m.overlay_sel);+ try std.testing.expectEqual(app.Effect.stay, applyLine(&m, "/effort low"));+ try std.testing.expectEqual(engine.Effort.low, m.effort);+}++test "/vim-mode toggles and /jump without turns explains" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ try std.testing.expect(!m.vim_mode);+ _ = applyLine(&m, "/vim-mode");+ try std.testing.expect(m.vim_mode);+ _ = applyLine(&m, "/vim");+ try std.testing.expect(!m.vim_mode);+ _ = applyLine(&m, "/jump");+ const last = m.history.items[m.history.items.len - 1].text;+ try std.testing.expect(std.mem.indexOf(u8, last, "nothing to jump") != null);+ try m.push(.user, "hi");+ _ = applyLine(&m, "/jump");+ try std.testing.expectEqual(app.Overlay.jump, m.overlay);+}++test "/btw queues an aside while a turn is pending" {+ var m: Model = undefined;+ m.setup(std.testing.allocator);+ defer m.deinit();+ const job = try std.testing.allocator.create(engine.Job);+ job.* = .{ .gpa = std.testing.allocator, .history = &.{}, .params = .{}, .stream = .{}, .threaded = false };+ m.pending = job;+ defer {+ m.pending = null;+ std.testing.allocator.destroy(job);+ }+ _ = applyLine(&m, "/btw remember the tests");+ try std.testing.expectEqual(@as(usize, 1), m.steer_queue.items.len);+ try std.testing.expectEqualStrings("remember the tests", m.steer_queue.items[0]);+}++test "lastLines caps ! output to the tail" {+ try std.testing.expectEqualStrings("c\nd", lastLines("a\nb\nc\nd", 2));+ try std.testing.expectEqualStrings("a\nb", lastLines("a\nb", 5));+}diff --git a/TUI/root.zig b/TUI/root.zigindex 21bac394..1e330948 100644--- a/TUI/root.zig+++ b/TUI/root.zig@@ -125,6 +125,7 @@ test {_ = @import("files.zig");_ = @import("overlays.zig");_ = run_mod;+ _ = @import("run_tests.zig");_ = @import("paint.zig");_ = @import("paint_tests.zig");_ = @import("scrollpaint.zig");diff --git a/TUI/run.zig b/TUI/run.zigindex aae3ad9c..0df1fe05 100644--- a/TUI/run.zig+++ b/TUI/run.zig@@ -494,109 +494,3 @@ fn parkToShell(io: Io, w: *Io.Writer, raw: *tty.RawState) void {restore_mod.muteStderr(); // fullscreen again: stderr goes back to the log_ = io;}--test "run loop enables click+hover tracking and bracketed paste" {- const src = @embedFile("run.zig");- const mouse_on = [_]u8{ '?', '1', '0', '0', '0', 'h' };- const sgr_on = [_]u8{ '?', '1', '0', '0', '6', 'h' };- const paste_on = [_]u8{ '?', '2', '0', '0', '4', 'h' };- // 1003 (motion) is back ON for image-chip hover previews. The v0.0.255- // leak (raw SGR typed into the thinking line) stays pinned by key.zig's- // flood/orphan tests; the restore seq must pop it so the shell never- // inherits motion tracking.- const hover_on = [_]u8{ '?', '1', '0', '0', '3', 'h' };- const hover_off = [_]u8{ '?', '1', '0', '0', '3', 'l' };- const kitty_on = [_]u8{ '>', '1', '1', 'u' };- const wrap_off = [_]u8{ '?', '7', 'l' };- try std.testing.expect(std.mem.indexOf(u8, src, &mouse_on) != null);- try std.testing.expect(std.mem.indexOf(u8, src, &sgr_on) != null);- try std.testing.expect(std.mem.indexOf(u8, src, &paste_on) != null);- try std.testing.expect(std.mem.indexOf(u8, src, &hover_on) != null);- try std.testing.expect(std.mem.indexOf(u8, restore_mod.seq, &hover_off) != null);- try std.testing.expect(std.mem.indexOf(u8, src, &[_]u8{ '?', '1', '0', '0', '7', 'h' }) == null);- try std.testing.expect(std.mem.indexOf(u8, src, &kitty_on) != null);- try std.testing.expect(std.mem.indexOf(u8, src, &wrap_off) != null);- try std.testing.expect(std.mem.indexOf(u8, src, "a=d,d=A") != null);- // The idle paste sweep must DISCARD whatever was stuck mid-sequence before- // the stall path below can see it. Leaving it there let a lone pending ESC- // become the Escape KEY the instant `in_paste` cleared, cancelling a live- // turn and wiping the composer with no keypress at all.- const sweep_at = std.mem.indexOf(u8, src, "closePaste(&m);").?;- const stall_at = std.mem.indexOfPos(u8, src, sweep_at, "esc_stall +|= 1").?;- const sweep_block = src[sweep_at..stall_at];- try std.testing.expect(std.mem.indexOf(u8, sweep_block, "pending_len = 0;") != null);- try std.testing.expect(std.mem.indexOf(u8, sweep_block, ".dropped") != null);- // Both unbounded holds are bounded: a resting mouse must not keep the paste- // latch alive, and the debris arm must go stale on its own clock.- try std.testing.expect(std.mem.indexOf(u8, src, "if (!stall.onlyMouseReports(") != null);- try std.testing.expect(std.mem.indexOf(u8, src, "if (stall.armExpired(") != null);- const drop_at = std.mem.indexOf(u8, src, ".drop => {").?;- try std.testing.expect(std.mem.indexOfPos(u8, src, drop_at, "key_mod.abandonSequence(") != null);-}--// The stall-verdict / carry-window / arm-window battery lives beside the-// policy it pins, in run_stall.zig; the frame painter's own battery (residue,-// glyph torture, row-style isolation, the self-heal) lives in paint.zig.--test "one tick drains the whole tty, coalesces the wheel, and paints once" {- const src = @embedFile("run.zig");- // (a) Every byte the tty already holds joins THIS tick before dispatch —- // one read per frame is what made momentum scrolling lag and then jump.- const read_at = std.mem.indexOf(u8, src, "const got = tty.readStdin(").?;- const dispatch_at = std.mem.indexOfPos(u8, src, read_at, "key_mod.next(inbuf[0..n]").?;- const drain = src[read_at..dispatch_at];- try std.testing.expect(std.mem.indexOf(u8, drain, "while (filled < inbuf.len and tty.poll(0))") != null);- // ...bounded, or an endless flood would be drained and never painted.- try std.testing.expect(std.mem.indexOf(u8, drain, "pacing.drainExpired(") != null);- // (b) The batch is applied as one unit and the wheel run goes through the- // same door a single report does.- try std.testing.expect(std.mem.indexOf(u8, src, "var batch: pacing.Batch") != null);- try std.testing.expect(std.mem.indexOf(u8, src, "keys.handleBatchItem(&m, item)") != null);- // (c) The frame is gated on the budget, and the gate sits BEFORE the render- // — gating only the paint would still pay for composing every frame.- const gate_at = std.mem.indexOf(u8, src, "pacing.shouldPaint(").?;- try std.testing.expect(gate_at < std.mem.indexOf(u8, src, "render_mod.render(&m, gpa, cols, rows").?);- // ...and the storm signal is a non-blocking poll OR the arrival RATE, so a- // fast loop that reads each report the instant it lands still paces, and a- // quiet one never waits on the budget for a single flick.- try std.testing.expect(std.mem.indexOf(u8, src, "const more_pending = tty.poll(0);") != null);- try std.testing.expect(std.mem.indexOf(u8, src, "last_input_ms = m.now_ms;") != null);- // A deferred frame comes back through the poll timeout the loop already- // has, and only when one is actually owed.- try std.testing.expect(std.mem.indexOf(u8, src, "if (!painted and pending_len == 0) wait = pacing.waitCap(") != null);-}--test "a wheel storm cannot be mistaken for paste activity" {- // The drained buffer is handed to onlyMouseReports whole: a storm is all- // complete SGR reports, so it never refreshes the paste clock, and a read- // carrying one real keystroke still does.- var buf: [512]u8 = undefined;- var n: usize = 0;- while (n + 10 <= 400) : (n += 10) @memcpy(buf[n .. n + 10], "\x1b[<65;4;4M");- try std.testing.expect(stall.onlyMouseReports(buf[0..n]));- buf[n] = 'k';- try std.testing.expect(!stall.onlyMouseReports(buf[0 .. n + 1]));-}--test "the loop self-heals: a resize EVENT and a periodic sweep force a repaint" {- const src = @embedFile("run.zig");- // A SIGWINCH that starts and ends on the same dimensions is invisible to a- // dimension comparison, and one that lands between tty.cols() and the- // paint leaves the diff baseline describing a screen the terminal has- // already reflowed. Both are covered by the EVENT.- try std.testing.expect(std.mem.indexOf(u8, src, "restore_mod.takeResized()") != null);- // ...and anything else that writes over us (an async kitty image delete,- // a terminal-side redraw) is repaired on the heartbeat, which must be able- // to run even when the frame hash has not moved.- try std.testing.expect(std.mem.indexOf(u8, src, "hash != last_hash or heal") != null);- try std.testing.expect(heal_interval_ms > 0);- // ...and the self-heal must never be served by the scroll fast path, whose- // whole point is to SKIP rows that are already correct — which is exactly- // the set of rows a heal exists to rewrite. Same for `full`, which folds in- // kitty graphics (pixels do not move when cells scroll), resize and theme.- try std.testing.expect(std.mem.indexOf(u8, src, "if (full or heal) null else m.paint_hint") != null);- // Theme bg is painted per row, not baked into the frame. Blank rows are- // byte-identical across themes, so a diff paint would strand the old- // canvas — /theme and the startup OSC-11 flip both force a full paint.- try std.testing.expect(std.mem.indexOf(u8, src, "m.theme_id != prev_theme") != null);-}diff --git a/TUI/run_tests.zig b/TUI/run_tests.zignew file mode 100644index 00000000..e36a0fb6--- /dev/null+++ b/TUI/run_tests.zig@@ -0,0 +1,114 @@+//! run.zig runtime-loop contract tests.++const std = @import("std");++const restore_mod = @import("restore.zig");+const run = @import("run.zig");+const stall = @import("run_stall.zig");+const heal_interval_ms = run.heal_interval_ms;++test "run loop enables click+hover tracking and bracketed paste" {+ const src = @embedFile("run.zig");+ const mouse_on = [_]u8{ '?', '1', '0', '0', '0', 'h' };+ const sgr_on = [_]u8{ '?', '1', '0', '0', '6', 'h' };+ const paste_on = [_]u8{ '?', '2', '0', '0', '4', 'h' };+ // 1003 (motion) is back ON for image-chip hover previews. The v0.0.255+ // leak (raw SGR typed into the thinking line) stays pinned by key.zig's+ // flood/orphan tests; the restore seq must pop it so the shell never+ // inherits motion tracking.+ const hover_on = [_]u8{ '?', '1', '0', '0', '3', 'h' };+ const hover_off = [_]u8{ '?', '1', '0', '0', '3', 'l' };+ const kitty_on = [_]u8{ '>', '1', '1', 'u' };+ const wrap_off = [_]u8{ '?', '7', 'l' };+ try std.testing.expect(std.mem.indexOf(u8, src, &mouse_on) != null);+ try std.testing.expect(std.mem.indexOf(u8, src, &sgr_on) != null);+ try std.testing.expect(std.mem.indexOf(u8, src, &paste_on) != null);+ try std.testing.expect(std.mem.indexOf(u8, src, &hover_on) != null);+ try std.testing.expect(std.mem.indexOf(u8, restore_mod.seq, &hover_off) != null);+ try std.testing.expect(std.mem.indexOf(u8, src, &[_]u8{ '?', '1', '0', '0', '7', 'h' }) == null);+ try std.testing.expect(std.mem.indexOf(u8, src, &kitty_on) != null);+ try std.testing.expect(std.mem.indexOf(u8, src, &wrap_off) != null);+ try std.testing.expect(std.mem.indexOf(u8, src, "a=d,d=A") != null);+ // The idle paste sweep must DISCARD whatever was stuck mid-sequence before+ // the stall path below can see it. Leaving it there let a lone pending ESC+ // become the Escape KEY the instant `in_paste` cleared, cancelling a live+ // turn and wiping the composer with no keypress at all.+ const sweep_at = std.mem.indexOf(u8, src, "closePaste(&m);").?;+ const stall_at = std.mem.indexOfPos(u8, src, sweep_at, "esc_stall +|= 1").?;+ const sweep_block = src[sweep_at..stall_at];+ try std.testing.expect(std.mem.indexOf(u8, sweep_block, "pending_len = 0;") != null);+ try std.testing.expect(std.mem.indexOf(u8, sweep_block, ".dropped") != null);+ // Both unbounded holds are bounded: a resting mouse must not keep the paste+ // latch alive, and the debris arm must go stale on its own clock.+ try std.testing.expect(std.mem.indexOf(u8, src, "if (!stall.onlyMouseReports(") != null);+ try std.testing.expect(std.mem.indexOf(u8, src, "if (stall.armExpired(") != null);+ const drop_at = std.mem.indexOf(u8, src, ".drop => {").?;+ try std.testing.expect(std.mem.indexOfPos(u8, src, drop_at, "key_mod.abandonSequence(") != null);+}++// The stall-verdict / carry-window / arm-window battery lives beside the+// policy it pins, in run_stall.zig; the frame painter's own battery (residue,+// glyph torture, row-style isolation, the self-heal) lives in paint.zig.++test "one tick drains the whole tty, coalesces the wheel, and paints once" {+ const src = @embedFile("run.zig");+ // (a) Every byte the tty already holds joins THIS tick before dispatch —+ // one read per frame is what made momentum scrolling lag and then jump.+ const read_at = std.mem.indexOf(u8, src, "const got = tty.readStdin(").?;+ const dispatch_at = std.mem.indexOfPos(u8, src, read_at, "key_mod.next(inbuf[0..n]").?;+ const drain = src[read_at..dispatch_at];+ try std.testing.expect(std.mem.indexOf(u8, drain, "while (filled < inbuf.len and tty.poll(0))") != null);+ // ...bounded, or an endless flood would be drained and never painted.+ try std.testing.expect(std.mem.indexOf(u8, drain, "pacing.drainExpired(") != null);+ // (b) The batch is applied as one unit and the wheel run goes through the+ // same door a single report does.+ try std.testing.expect(std.mem.indexOf(u8, src, "var batch: pacing.Batch") != null);+ try std.testing.expect(std.mem.indexOf(u8, src, "keys.handleBatchItem(&m, item)") != null);+ // (c) The frame is gated on the budget, and the gate sits BEFORE the render+ // — gating only the paint would still pay for composing every frame.+ const gate_at = std.mem.indexOf(u8, src, "pacing.shouldPaint(").?;+ try std.testing.expect(gate_at < std.mem.indexOf(u8, src, "render_mod.render(&m, gpa, cols, rows").?);+ // ...and the storm signal is a non-blocking poll OR the arrival RATE, so a+ // fast loop that reads each report the instant it lands still paces, and a+ // quiet one never waits on the budget for a single flick.+ try std.testing.expect(std.mem.indexOf(u8, src, "const more_pending = tty.poll(0);") != null);+ try std.testing.expect(std.mem.indexOf(u8, src, "last_input_ms = m.now_ms;") != null);+ // A deferred frame comes back through the poll timeout the loop already+ // has, and only when one is actually owed.+ try std.testing.expect(std.mem.indexOf(u8, src, "if (!painted and pending_len == 0) wait = pacing.waitCap(") != null);+}++test "a wheel storm cannot be mistaken for paste activity" {+ // The drained buffer is handed to onlyMouseReports whole: a storm is all+ // complete SGR reports, so it never refreshes the paste clock, and a read+ // carrying one real keystroke still does.+ var buf: [512]u8 = undefined;+ var n: usize = 0;+ while (n + 10 <= 400) : (n += 10) @memcpy(buf[n .. n + 10], "\x1b[<65;4;4M");+ try std.testing.expect(stall.onlyMouseReports(buf[0..n]));+ buf[n] = 'k';+ try std.testing.expect(!stall.onlyMouseReports(buf[0 .. n + 1]));+}++test "the loop self-heals: a resize EVENT and a periodic sweep force a repaint" {+ const src = @embedFile("run.zig");+ // A SIGWINCH that starts and ends on the same dimensions is invisible to a+ // dimension comparison, and one that lands between tty.cols() and the+ // paint leaves the diff baseline describing a screen the terminal has+ // already reflowed. Both are covered by the EVENT.+ try std.testing.expect(std.mem.indexOf(u8, src, "restore_mod.takeResized()") != null);+ // ...and anything else that writes over us (an async kitty image delete,+ // a terminal-side redraw) is repaired on the heartbeat, which must be able+ // to run even when the frame hash has not moved.+ try std.testing.expect(std.mem.indexOf(u8, src, "hash != last_hash or heal") != null);+ try std.testing.expect(heal_interval_ms > 0);+ // ...and the self-heal must never be served by the scroll fast path, whose+ // whole point is to SKIP rows that are already correct — which is exactly+ // the set of rows a heal exists to rewrite. Same for `full`, which folds in+ // kitty graphics (pixels do not move when cells scroll), resize and theme.+ try std.testing.expect(std.mem.indexOf(u8, src, "if (full or heal) null else m.paint_hint") != null);+ // Theme bg is painted per row, not baked into the frame. Blank rows are+ // byte-identical across themes, so a diff paint would strand the old+ // canvas — /theme and the startup OSC-11 flip both force a full paint.+ try std.testing.expect(std.mem.indexOf(u8, src, "m.theme_id != prev_theme") != null);+}