Testing
Fakes that keep the real policy engine and lifecycle and replace only the humans, the store and the clock.
Code blocks on this page are excerpts of tested programs; each block's title names its file in examples/, where the complete program runs.
The testing helpers swap three things: the people who answer (a script), the store (memory) and the clock (a
FakeClock). The policy engine, the approval lifecycle and the gated functions stay real, so a passing test means
your production policy really decides that way.
Fake approvals
Inside a fake scope, every gate answers asks from the script instead of a channel. The quickstart's last region proves the approve path without a terminal:
with hitl.testing.fake_approvals(answer="approve") as approvals:
assert issue_refund("A-1002", 900) == "Refunded $900 on A-1002"
assert approvals.asked("issue_refund", args={"amount": {"gt": 50}})await fakeApprovals.run({ answer: "approve" }, async (approvals) => {
assert.equal(await issueRefund({ order_id: "A-1002", amount: 900 }), "Refunded $900 on A-1002");
assert.ok(approvals.asked("issue_refund", { args: { amount: { gt: 50 } } }).passed);
});answeris"approve","deny","timeout", a review (approve(edited_args=...)), a function of the request, a list consumed in order, or a mapping by tool name.strict=True/strict: truefails on any ask the script did not expect.approvals.asked(tool, args=...),allowed(...)anddenied(...)return match results whose message says what differed.approvals.advance("4h")moves the fake clock.
pytest: installing viewport-hitl registers an approvals fixture, so a test takes approvals as an argument
and calls approvals.answer("deny"). Outside pytest, use with hitl.testing.fake_approvals(...) or async with.
Vitest: expect.extend(hitlMatchers) from @viewportai/hitl/vitest adds toHaveAsked, toHaveAllowed,
toHaveDenied and toDecide. @viewportai/hitl/testing itself never imports vitest, so plain Node scripts can use
fakeApprovals, fakeGate and decide. Scope with using approvals = fakeApprovals(...) or
await fakeApprovals.run(options, async (approvals) => ...).
fake_gate(...) / fakeGate(...) builds a gate pinned to its own fake scope, for code that creates gates per test.
decide(policy, action, facts) evaluates a policy with no gate at all.
Time
Every timed wait in the gate (approval timeouts, store polling, scorer timeouts, receipt retries) sleeps on the
injected clock. Pass a FakeClock and advance it: a four hour approval expires instantly.
call = asyncio.create_task(send_wire("ACME-001", 25_000)) # waits on the clock, not on a timer
await until(lambda: clock.pending > 0) # the gate is asleep until the deadline
clock.advance(FOUR_HOURS)
try:
await call
except hitl.ActionDenied as refused:
print(f"wire {refused.status}: {refused.message}") # wire expired: The approval request expired
assert refused.status == "expired"
else:
raise AssertionError("an expired approval must refuse the transfer")const call = sendWire({ to: "ACME-001", amount: 25_000 }); // waits on the clock, not on a timer
await until(() => clock.pending > 0); // the gate is asleep until the deadline
clock.advance(FOUR_HOURS);
const refused = await call.catch((e: unknown) => e);
assert.ok(refused instanceof ActionDenied);
console.log(`wire ${refused.status}: ${refused.message}`); // wire expired: The approval request expiredSigned requests
slack_request(...) / slackRequest(...) build a correctly signed Slack block_actions request, and
webhook_request(...) / webhookRequest(...) a Standard Webhooks signed review, for testing a mounted
gate.webhooks(). The channels page uses slack_request to press Approve.
Policy tests
Policies carry their own tests: a tests: block in the policy, or policies/*.test.yaml files. viewport-hitl test
runs them, and so does the pytest plugin. It collects only YAML files under a policies folder that are shaped like
a HITL policy with tests, so other tools' policies/ YAML (Gatekeeper, OPA, Kubernetes) is left alone. To turn the
plugin off, run pytest -p no:viewport_hitl.
viewport-hitl test policies/
npx @viewportai/hitl test policies/