Skip to content
Source on GitHub (opens in a new tab)

An agent’s behaviour comes from three things: what the model says, what your tools and rules do with it, and what the session records. A test fixes the first, so the other two are what it checks. zelk gives you two ways to script the model, and the session’s log to check against. Neither needs an API key.

provider/replay is a provider that answers each model call with the next scripted turn. Everything else is real: the agent, your tools, the permission rules, the store, the loop.

The turns are built with replay.Text for a plain answer, replay.ToolCalls for a turn in which the model asks for tools, and replay.Reported for the turn that ends the run by reporting the goal met. A replay.Turn can also carry a Check that inspects the request the agent sent, or an Err to make the call fail.

Check the durable events, not the model’s words: the log says what the agent did. Session.Events reads it, and each tool call that ran is an EffectStarted with a RunTool effect:

core/guide_testing_example_test.go
lookup, err := tool.Define("order_status", "Look up an order's status.",
func(_ context.Context, in struct {
ID string `json:"id" desc:"The order's ID."`
}) (tool.Result, error) {
return tool.Text("Order " + in.ID + " shipped on Monday."), nil
}, tool.WithReadOnly())
if err != nil {
panic(err)
}
// Two scripted turns: the model asks for the tool, then reports.
p, err := replay.New(provider.Caps{},
replay.ToolCalls(history.ToolCall{ID: "c1", Name: "order_status", Input: json.RawMessage(`{"id":"A-17"}`)}),
replay.Reported("met", "It shipped on Monday."))
if err != nil {
panic(err)
}
ctx := context.Background()
a, err := zelk.New(ctx, p, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeDefault), zelk.WithTools(lookup))
if err != nil {
panic(err)
}
defer a.Close(ctx)
s, err := a.Session(ctx, "support-2")
if err != nil {
panic(err)
}
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Where is order A-17?")))
if err != nil {
panic(err)
}
res, err := x.Wait(ctx)
if err != nil {
panic(err)
}
var tools []string
for ev, err := range s.Events(ctx, 1) {
if err != nil {
panic(err)
}
if started, ok := ev.Payload.(event.EffectStarted); ok && started.Effect.Kind == event.EffectRunTool {
tools = append(tools, started.Effect.RunTool.Tool+" "+string(started.Effect.RunTool.Input))
}
}
fmt.Println("tool calls:", tools)
fmt.Println(res.Outcome, "-", res.Text)
// Output:
// tool calls: [order_status {"id":"A-17"}]
// success - It shipped on Monday.

The sample shows a function body, not the imports. zelk is the root package, github.com/barevoidco/zelk/core (reference); the rest come from its subpackages, at github.com/barevoidco/zelk/core/<name>: provider and provider/replay, permission, event for the log’s events, history and tool.

What else a test can read:

To check Read
how the run ended, and what the model reported Result.Outcome, Result.Text, Result.Report from Execution.Wait
which tools ran, with what input EffectStarted events with Effect.Kind == event.EffectRunTool
that a call was refused, and why PolicyDecided events: the verdict, the rule, the reason
what the model was sent replay.Provider.Requests()
that the script was used up replay.Provider.Remaining() is zero
where a session stands Session.Status, Session.History

A test that waits on Execution.Wait, or reads Subscribe to the end of a run, needs no sleep and no timer. For code that depends on the time, pass zelk.WithClock and lease.WithClock a clock the test moves.

Approvals are testable the same way. With WithInputSuspend, the first Wait returns zelk.ErrSuspended with the requests in Result.Requests; the test answers one and starts the session again, as in Approvals and questions.

The replay provider skips the HTTP layer. To exercise the real provider client, its request rendering and its parsing of a stream, run the agent against a server that speaks the Chat Completions format. core/testkit/openaifake is one: it serves scripted turns from net/http/httptest, on the loopback interface of the machine running the test, and keeps each request body so a test can read what the model was sent:

core/guide_testing_example_test.go
lookup, err := tool.Define("order_status", "Look up an order's status.",
func(_ context.Context, in struct {
ID string `json:"id" desc:"The order's ID."`
}) (tool.Result, error) {
return tool.Text("Order " + in.ID + " shipped on Monday."), nil
}, tool.WithReadOnly())
if err != nil {
panic(err)
}
fake := openaifake.New(
openaifake.Turn{Calls: []openaifake.Call{{ID: "c1", Name: "order_status", Arguments: `{"id":"A-17"}`}}},
openaifake.Reported("met", "It shipped on Monday."))
srv := httptest.NewServer(fake)
defer srv.Close()
client, err := openaicompat.New(srv.URL+"/v1", openaicompat.WithHTTPClient(srv.Client()))
if err != nil {
panic(err)
}
defer client.Close()
ctx := context.Background()
a, err := zelk.New(ctx, client, zelk.WithModel("m"), zelk.WithPermissionMode(permission.ModeDefault), zelk.WithTools(lookup))
if err != nil {
panic(err)
}
defer a.Close(ctx)
s, err := a.Session(ctx, "support-3")
if err != nil {
panic(err)
}
x, err := s.Start(ctx, zelk.WithCommands(zelk.UserMessage("Where is order A-17?")))
if err != nil {
panic(err)
}
res, err := x.Wait(ctx)
if err != nil {
panic(err)
}
bodies := fake.Bodies()
fmt.Println("model calls:", len(bodies))
// The tool's result went back to the model in the second request.
fmt.Println("result sent back:", strings.Contains(string(bodies[1]), "Order A-17 shipped on Monday."))
fmt.Println(res.Outcome)
// Output:
// model calls: 2
// result sent back: true
// success

This one adds provider/openaicompat, the real client under test, and testkit/openaifake, the server it talks to, besides net/http/httptest from the standard library.

Nothing leaves the machine, and nothing needs a key, but the test does open a socket on 127.0.0.1. Use it for what only the wire shows, such as an openaicompat option, a tool’s schema as the provider renders it or the body sent after a compaction, and use replay for the rest: it is faster and has no port.

openaifake is test infrastructure and may change without notice. Import it from _test.go files only, and don’t put it in code that ships.

  • Close what you start, the agent first. Defer srv.Close() when you build the server and a.Close(ctx) when you build the agent, so the agent closes before the server it talks to. A test that leaves an execution running leaks a goroutine into the next one. go.uber.org/goleak in TestMain catches it: zelk’s own tests run under it.
  • Give each test its own store and workspace. A t.TempDir() for a file store and a workspace, or the default in-memory store, keeps tests independent and parallel.
  • Don’t test the model. A scripted turn is a fixture, not a claim about what a real model would do. A test shows that, given this reply, your tools, rules and wiring do the right thing. Whether the real model chooses well is a different kind of check, run by hand or against a real API on purpose.
  • Test recovery by cutting the process, not by sleeping. A session reopened on the same store and lease, as in Stores, leases and recovery, is what a restarted process sees. examples/external-driver in the repository kills a child process mid-call and resumes it, with the lease’s clock injected, to prove the guarantee.

Testing and observability is a complete program with tests in both styles and an OpenTelemetry trace. That is the last guide; the overview lists them all by task.