feat: headless test harness for models - #133
Merged
Merged
Conversation
meszmate
force-pushed
the
feat/fallible-model-callbacks
branch
from
August 14, 2026 04:37
1fb58a9 to
7d94d85
Compare
Testing a ZigZag application meant standing up a terminal, so in practice applications only tested their components -- never the model that wires them together. `zz.testing.Harness` runs the same Model-Update-View cycle without one: send messages, render, assert on the frame. Commands that only mean something to a terminal are recorded rather than executed, so a test can check that pressing 'q' really did return `.quit`, or that the title was set. Timers are driven by an explicit clock, so a repeating tick is exercised without waiting for it. The frame allocator is reset by `nextFrame`/`advance` exactly as the runtime resets it each tick, so a model that keeps a frame-allocated slice across frames fails in a test instead of in production. `model.ErrorSet` names a callback's error set, which lets the harness declare its own `Error` type -- `send` and `process` call each other, and Zig cannot infer two error sets that depend on one another.
meszmate
force-pushed
the
feat/headless-test-harness
branch
from
August 14, 2026 04:57
bf9933e to
33bbdb5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Also from the codebase review.
Why
Testing a ZigZag application means standing up a terminal, so in practice apps only test their components — never the model that wires them together. The library's own
tests/program_tests.zigcan only assert on allocator plumbing for the same reason.zz.testing.Harnessruns the same Model-Update-View cycle without a terminal:start()Model.initand processes the command it returnssend(msg)press/pressChar/typeTextmouse(event)resize(w, h)window_sizeadvance(ns)nextFrame(ns)view()/plainView()/viewContains(s)hasQuit().quitrecordedEffects()/title()Terminal-only commands (
set_title,println, images, mouse toggles) are recorded rather than executed, so a test can assert on them. Timers run off an explicit clock, so a repeating tick is exercised without waiting for it.The frame allocator is reset by
nextFrame/advanceexactly as the runtime resets it each tick — a model that keeps a frame-allocated slice across frames fails here instead of in production. (That is the bug fixed in #126.)Pairs with the existing
expectSnapshotfor golden-file rendering tests.Note on the design
The harness deliberately does not reuse
Program's command loop. It only handles the commands a model can observe (quit,tick,every,batch,sequence,msg,perform) and records the rest — which is what a test double should do, and keeps it independent of terminal state.model.ErrorSetnames a callback's error set. The harness needs it to declare its ownErrortype:sendandprocesscall each other, and Zig cannot infer two error sets that depend on one another.Tests
tests/harness_tests.zig— 12 cases covering initial render, key/mouse/type input, quit,.msgcommands being re-dispatched rather than recorded, recorded effects, repeating timers, resize, styled vs plain view, frame-allocator reset across 500 frames, and a model with plain (non-fallible) callbacks.Writing them turned up a separate real bug in the library's
batchAPI — see the follow-up PR.zig build testandzig buildclean on 0.16.0.