Testing with core's fakes
Deterministic clocks and ids, a trajectory builder, collecting sinks and fake tools, for pytest and vitest.
Everything in core that varies between runs is injectable, and viewportai.core.testing /
@viewportai/core/testing ships fakes for all of it. They are public, typed and used by core's own tests.
| Helper | What it gives you |
|---|---|
FakeClock | a clock that only moves on advance(ms), starting at 2026-01-01T00:00:00Z |
SequentialIds | readable ids: evt_1, evt_2, trj_1 |
trajectory() | a fluent builder for trajectories in tests |
collect() | a sink that keeps every event (events, started, clear()) |
fake_tool / fakeTool | a recorded tool with scripted responses and a calls log |
normalize | strips timestamps and latencies and renumbers ids, for snapshots |
Pass the clock and ids to trace() and every span and tool inside uses them, so trajectories are byte-stable:
import asyncio
from viewportai.core import match, stable_json, trace
from viewportai.core.testing import FakeClock, SequentialIds, fake_tool, trajectory
async def test_refund_flow() -> None:
clock = FakeClock()
def slow_refund(args: dict[str, object], ctx: object) -> dict[str, str]:
clock.advance(120)
return {"refund_id": "R-1"}
lookup = fake_tool("lookup_order", [{"status": "delivered"}])
refund = fake_tool("issue_refund", slow_refund)
async with trace(clock=clock, ids=SequentialIds()) as t:
await lookup.execute({"order_id": "A-1"})
await refund.execute({"order_id": "A-1", "amount": 42.5})
print(stable_json([c.args for c in refund.calls])) # [{"amount":42.5,"order_id":"A-1"}]
call = t.trajectory.tool_calls("issue_refund")[0]
print(call.id, int(call.latency_ms or 0)) # evt_2 120
print(str(match({"result": {"refund_id": "R-1"}}, call).passed).lower()) # true
def test_builder() -> None:
t = (
trajectory()
.agent("support")
.model("m", usage={"input_tokens": 10})
.tool("issue_refund", args={"amount": 5})
.build()
)
print(t.tool_calls()[0].parent_id) # evt_1
asyncio.run(test_refund_flow())
test_builder()import { match, stableJson, toolCalls, trace } from "@viewportai/core";
import { FakeClock, fakeTool, SequentialIds, trajectory } from "@viewportai/core/testing";
const clock = new FakeClock();
const lookup = fakeTool("lookup_order", [{ status: "delivered" }]);
const refund = fakeTool("issue_refund", () => {
clock.advance(120);
return { refund_id: "R-1" };
});
const { trajectory: t } = await trace(
async () => {
await lookup.execute({ order_id: "A-1" });
await refund.execute({ order_id: "A-1", amount: 42.5 });
},
{ clock, ids: new SequentialIds() },
);
console.log(stableJson(refund.calls.map((c) => c.args))); // [{"amount":42.5,"order_id":"A-1"}]
const call = toolCalls(t, "issue_refund")[0];
console.log(call?.id, call?.latency_ms); // evt_2 120
console.log(match({ result: { refund_id: "R-1" } }, call).passed); // true
const built = trajectory()
.agent("support")
.model("m", { usage: { input_tokens: 10 } })
.tool("issue_refund", { args: { amount: 5 } })
.build();
console.log(toolCalls(built)[0]?.parent_id); // evt_1In vitest, expect.extend(matchers) from @viewportai/core/vitest adds toMatchSpec, so a test reads
expect(output).toMatchSpec({ amount: { lte: 100 } }). The example calls the same matcher directly, so it runs (and
is checked) outside the vitest runner:
import { matchers } from "@viewportai/core/vitest";
// In a vitest file: expect.extend(matchers); expect({ amount: 42.5 }).toMatchSpec({ amount: { lte: 100 } });
const result = matchers.toMatchSpec.call({ isNot: false }, { amount: 42.5 }, { amount: { lte: 100 } });
console.log(result.pass); // trueIn pytest, a MatchResult is truthy only when it passes and its repr shows the failure message, so plain
assert match(spec, value) gives a readable failure.
Crash conformance
runCrashScenarios(doc, adapter) / run_crash_scenarios(doc, adapter) run scenario files where named processes
share only durable state and switching to a new process name is a crash of the previous one.
Step and final expectations are matcher specs. A package plugs in its durable boundaries through an adapter
(reset, process, ops, final); core runs its own store scenarios from spec/conformance/crash/store.yaml
this way, and HITL runs its consume and settle scenarios through the same harness.