Help us shape the future of Go! Take the 2026 Go Developer Survey

modeltest

package
v0.25.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package modeltest checks an HTTP-backed agent.Model against the streaming contract the agent loop relies on:

  • The adapter releases its response when the consumer stops early. An adapter that reads a streamed response in its own goroutine and sends each event on an unbuffered channel blocks forever once nobody is receiving, holding the response body and its connection. Breaking out of Stream.Events, or cancelling the context passed to Model.Stream, must close the response body.
  • A response that ends before the provider's end-of-turn marker is an error. A connection closed partway through a turn must never read as a complete message, which the agent would journal as the model's answer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckFinish added in v0.8.0

func CheckFinish(t testing.TB, f agent.Finish)

CheckFinish checks the Finish a first-party adapter sends. Its Reason is never empty: the adapter maps the provider's reason onto a neutral one (agent.FinishStop, agent.FinishToolUse, agent.FinishLength, agent.FinishFiltered), and a turn the provider ended without naming a reason is agent.FinishStop. A reason outside that set is the provider's own value passed through, so it equals Raw. Discarded is zero: a live adapter discards nothing.

func CheckSSEPrefix added in v0.7.0

func CheckSSEPrefix(t testing.TB, streamSSE func(body io.ReadCloser, send func(agent.Emit) bool), body []byte, cut int)

CheckSSEPrefix checks, on top of ReadSSE's properties for body and for its first cut bytes, that a response cut short is never a success that differs from the whole response's success: a connection that closes partway must not turn a truncated answer into a complete one. A fuzz target for an adapter's SSE reader calls it with arbitrary bytes and an arbitrary cut.

func Run

func Run(t *testing.T, newModel func(baseURL string, client *http.Client) agent.Model, event func(i int) string)

Run runs the checks. newModel returns the adapter under test pointed at baseURL and using client for its requests. event returns the i-th server-sent event of an endless text response, in the provider's wire format and including its trailing blank line; it must not end the turn. The test server sends one every few milliseconds until the client goes away, as a long model response does, or, for the truncation check, stops after two.

func ToolConfig added in v0.7.0

func ToolConfig(t *testing.T, newModel func(baseURL string, client *http.Client) agent.Model)

ToolConfig checks that the adapter refuses a tool setup every provider rejects with agent.ErrConfig, before it sends anything: a tool name outside [a-zA-Z0-9_-] or longer than 64 characters, two tools with one name, and a tool choice that cannot be met (an unknown mode; "required" or "tool" with no tools declared; "tool" naming no tool or one not declared). A name every provider accepts, and "auto" or "none" with no tools, must reach the provider.

func ToolNames added in v0.8.0

func ToolNames(t *testing.T, newModel func(baseURL string, client *http.Client) agent.Model, refused, accepted []string)

ToolNames checks the adapter's own tool-name rule against names an MCP server may list (the MCP grammar is 1 to 128 of A-Z a-z 0-9 _ - ., wider than every provider's): each name in refused must fail with agent.ErrConfig that quotes the name, before anything is sent, and each name in accepted must reach the provider.

Types

type SSEResult added in v0.7.0

type SSEResult struct {
	Emits []agent.Emit
	Msg   agent.Message
	Usage agent.Usage
	Err   error
}

SSEResult is what an adapter's SSE reader made of one response body: every Emit it sent, in order, and the message, usage, and error a consumer reading them gets.

func ReadSSE added in v0.7.0

func ReadSSE(t testing.TB, streamSSE func(body io.ReadCloser, send func(agent.Emit) bool), body []byte) SSEResult

ReadSSE runs an adapter's SSE reader (streamSSE, which reads body and sends each event through send) over body and checks the properties every response must keep, whatever its bytes:

  • the reader sends at most one Finish, and nothing after it (the adapter ends the turn once; agent.Stream would otherwise report agent.ErrStreamProtocol);
  • every Finish it sends keeps CheckFinish's properties (a first-party adapter never leaves the reason empty);
  • every error it sends is its last Emit;
  • a failed response fails with an agent.ErrModel (or agent.ErrTruncatedToolArgs, which the message assembly reports for tool arguments that are not complete JSON).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL