assert

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2026 License: MIT Imports: 18 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package assert defines the Assertion contract and the deterministic assertions tamesu ships (contains, regex, json_path, status, tool_called, metric, turns). LLM judges and Genkit evaluators live in the llm module.

Index

Constants

View Source
const (
	EventDTMFReceived = "dtmf_received" // Detail: the digits the agent sent
	EventCallEnded    = "call_ended"    // Detail: agent, caller, timeout, error, no_answer or busy
	EventTransfer     = "transfer"      // Detail: the SIP REFER target
)

Event kinds the voice targets emit on the session timeline. The sip assertions read them.

View Source
const (
	GoldenExact   = "exact"
	GoldenSimilar = "similar"
	GoldenJudge   = "judge"
)

Golden modes.

View Source
const DefaultGoldenMin = 0.8

DefaultGoldenMin is the word overlap a similar golden turn needs.

View Source
const DefaultSimilarity = 0.8

DefaultSimilarity is the cosine similarity similar_to needs when the assertion gives no min.

Variables

This section is empty.

Functions

func CompileSchema added in v1.4.0

func CompileSchema(schema map[string]any) (*jsonschema.Schema, error)

CompileSchema compiles a JSON Schema given as a document (the YAML mapping of a suite), so a mistake in the schema is a config error and not a test failure.

func Cosine added in v1.4.0

func Cosine(a, b []float32) float64

Cosine is the cosine similarity of two vectors: 1 for the same direction, 0 for unrelated ones. Vectors of different lengths or without magnitude score 0.

func Register

func Register(name string, factory func() Assertion)

Register makes a Go assertion available to suites as `custom: <name>`.

func Registered

func Registered() []string

Registered lists the custom assertion names, sorted.

func Similarity

func Similarity(a, b string) float64

Similarity is the Jaccard overlap of the two texts' word sets, 0 to 1.

Types

type Assertion

type Assertion interface {
	Name() string
	Evaluate(ctx context.Context, r *RunResult) Result
}

Assertion is one check over a finished run.

func CallEndedBy

func CallEndedBy(who []string) Assertion

CallEndedBy passes when the call ended for one of the listed reasons (agent, caller, timeout, error, no_answer, busy).

func Captured

func Captured(name string, equals, matches *string) (Assertion, error)

Captured passes when the run captured name and, when given, the value equals or matches the expectation.

func Contains

func Contains(s string) Assertion

Contains passes when the agent transcript contains s.

func DTMFReceived

func DTMFReceived(digits string) Assertion

DTMFReceived passes when the digits the agent sent, concatenated, equal digits.

func Evaluate

func Evaluate(evaluator string, min float64) Assertion

Evaluate runs a registered Genkit evaluator on the run (input: the first user turn, output: the agent transcript) and passes when its score is at least min (or it reports PASS when it has no numeric score).

func Func

func Func(name string, fn func(ctx context.Context, r *RunResult) Result) Assertion

Func makes an Assertion from a function.

func Golden

func Golden(reference []string, mode string, min float64, source string) Assertion

Golden compares the agent's turns, in order, with reference turns: exact (normalised text), similar (word overlap of at least min) or judge (the judge decides whether each turn means the same as its reference). Fewer agent turns than references fail; extra agent turns are ignored.

func JSONPath

func JSONPath(path string, equals any) (Assertion, error)

JSONPath passes when path exists in every agent response and, when equals is not nil, equals it (numbers and strings compared after JSON normalisation).

func Judge

func Judge(rubric string, opts ...JudgeOption) Assertion

Judge grades the run against a rubric with the run's Judger (an LLM).

func Lookup

func Lookup(name string) (Assertion, bool)

Lookup returns the registered custom assertion.

func MustRegex

func MustRegex(pattern string) Assertion

MustRegex is Regex that panics on a bad pattern, for Go tests.

func NotContains

func NotContains(s string) Assertion

NotContains passes when no agent turn contains s.

func NotRegex

func NotRegex(pattern string) (Assertion, error)

NotRegex passes when no agent turn matches pattern.

func OnTurn

func OnTurn(n int, last bool, inner Assertion) Assertion

OnTurn evaluates inner on one agent turn only: the 1-based turn n, or the last turn when last is set.

func Regex

func Regex(pattern string) (Assertion, error)

Regex passes when the agent transcript matches pattern.

func SLO

func SLO(metric string, max float64) Assertion

SLO passes when the metric is at most max, or was not measured in this run (an audio metric on a text test), so one SLO applies to every test.

func SimilarTo added in v1.4.0

func SimilarTo(text string, min float64, model string) Assertion

SimilarTo passes when some agent turn means the same as text: the cosine similarity of their embeddings is at least min (DefaultSimilarity when 0). model picks the embedding model; "" uses the configured one.

func Status

func Status(code int) Assertion

Status passes when every agent turn had the HTTP status code.

func ToolCalled

func ToolCalled(name string, args map[string]any) Assertion

ToolCalled passes when the agent called the tool, with args matching every key of args (a subset of the call's arguments) when args is not empty.

func ToolCalledWithSchema added in v1.4.0

func ToolCalledWithSchema(name string, args map[string]any, schema map[string]any) (Assertion, error)

ToolCalledWithSchema passes when the agent called the tool (with args matching, like ToolCalled) and the arguments of every call to it validate against the JSON Schema: a call with arguments the schema rejects fails the assertion, whatever the other calls looked like. A nil schema is ToolCalled.

func ToolNotCalled

func ToolNotCalled(name string, args map[string]any) Assertion

ToolNotCalled passes when the agent never called the tool (with those args).

func ToolSequence

func ToolSequence(names []string, exact bool) Assertion

ToolSequence passes when the tool calls happened in the given order: as a subsequence of the run's calls, or exactly that list with exact.

func TransferredTo

func TransferredTo(pattern string) (Assertion, error)

TransferredTo passes when the agent transferred the call (a SIP REFER) to a target matching pattern.

func Turns

func Turns(c Comparison) Assertion

Turns compares the number of agent turns: assert.Turns(assert.Comparison{Op: "lte", Value: 6}).

type Comparison

type Comparison struct {
	Op    string
	Value float64
}

Comparison is one of lt, lte, gt, gte, eq against a number.

func (Comparison) Holds

func (c Comparison) Holds(v float64) bool

Holds evaluates the comparison.

func (Comparison) Symbol

func (c Comparison) Symbol() string

Symbol is the comparison operator for people.

type EmbedRequest added in v1.4.0

type EmbedRequest struct {
	// Model overrides the configured embedding model (a Genkit reference).
	Model string   `json:"model,omitempty"`
	Texts []string `json:"texts"`
}

EmbedRequest asks for the embedding of each text, with one model.

type EmbedResponse added in v1.4.0

type EmbedResponse struct {
	Vectors [][]float32 `json:"vectors"`
	Model   string      `json:"model,omitempty"`
}

EmbedResponse is one vector per text, in order, and the model that made them.

type Embedder added in v1.4.0

type Embedder interface {
	Embed(ctx context.Context, req EmbedRequest) (EmbedResponse, error)
}

Embedder turns texts into vectors for the semantic assertions. The llm module implements it; the runner sets it on every RunResult.

type EvalRequest

type EvalRequest struct {
	Evaluator string `json:"evaluator"`
	Input     any    `json:"input"`
	Output    any    `json:"output,omitempty"`
	Context   []any  `json:"context,omitempty"`
	Reference any    `json:"reference,omitempty"`
}

EvalRequest runs a registered Genkit evaluator on one example.

type EvalVerdict

type EvalVerdict struct {
	Pass   bool    `json:"pass"`
	Score  float64 `json:"score"`
	Reason string  `json:"reason,omitempty"`
}

EvalVerdict is an evaluator's score.

type Evaluator

type Evaluator interface {
	Evaluate(ctx context.Context, req EvalRequest) (EvalVerdict, error)
}

Evaluator runs Genkit evaluators by name.

type JudgeOption

type JudgeOption func(*JudgeRequest)

JudgeOption configures a judge assertion.

func WithMin

func WithMin(min float64) JudgeOption

WithMin sets the minimum score to pass on the 1-5 scale (default 4).

func WithModel

func WithModel(model string) JudgeOption

WithModel sets the judge model (a Genkit reference).

func WithScale

func WithScale(scale string) JudgeOption

WithScale sets the judge scale: "binary" or "1-5".

type JudgeRequest

type JudgeRequest struct {
	Rubric string `json:"rubric"`
	// Scale is "binary" (pass or fail) or "1-5" (a score compared with Min).
	Scale string  `json:"scale,omitempty"`
	Min   float64 `json:"min,omitempty"`
	// Model overrides the configured judge model (a Genkit reference).
	Model string `json:"model,omitempty"`
	// Language is the conversation's language (BCP 47), when the persona has one.
	Language   string             `json:"language,omitempty"`
	Transcript []Turn             `json:"transcript,omitempty"`
	ToolCalls  []target.ToolCall  `json:"tool_calls,omitempty"`
	Metrics    map[string]float64 `json:"metrics,omitempty"`
}

JudgeRequest is what an LLM judge receives: the rubric and the whole run.

type JudgeVerdict

type JudgeVerdict struct {
	Pass     bool     `json:"pass"`
	Score    float64  `json:"score"`
	Reason   string   `json:"reason"`
	Evidence []string `json:"evidence,omitempty"`
	// TraceID and PromptVersion make judge drift traceable.
	TraceID       string `json:"trace_id,omitempty"`
	PromptVersion string `json:"prompt_version,omitempty"`
}

JudgeVerdict is the judge's structured answer.

type Judger

type Judger interface {
	Judge(ctx context.Context, req JudgeRequest) (JudgeVerdict, error)
}

Judger grades a run against a rubric. The llm module implements it; the runner sets it on every RunResult.

type MetricBuilder

type MetricBuilder struct {
	// contains filtered or unexported fields
}

MetricBuilder builds a metric assertion: assert.Metric("time_to_first_audio_ms").Lt(1500).

func Metric

func Metric(name string) MetricBuilder

Metric starts a metric assertion on the named built-in metric.

func (MetricBuilder) Compare

func (m MetricBuilder) Compare(c Comparison) Assertion

Compare finishes the metric assertion with an explicit comparison.

func (MetricBuilder) Eq

func (MetricBuilder) Gt

func (MetricBuilder) Gte

func (m MetricBuilder) Gte(v float64) Assertion

func (MetricBuilder) Lt

Lt, Lte, Gt, Gte and Eq finish the metric assertion.

func (MetricBuilder) Lte

func (m MetricBuilder) Lte(v float64) Assertion

type Result

type Result struct {
	Name string `json:"name"`
	Pass bool   `json:"pass"`
	// Score is the judge's score when the assertion has one (0 to 1 or 1 to
	// 5); zero otherwise.
	Score float64 `json:"score,omitempty"`
	// Reason explains the outcome in one or two sentences.
	Reason string `json:"reason"`
	// Evidence quotes the turns or values the outcome rests on.
	Evidence []string `json:"evidence,omitempty"`
	// TraceID is the Genkit trace of a judge or evaluator call.
	TraceID string `json:"trace_id,omitempty"`
}

Result is the outcome of one assertion.

type RunResult

type RunResult struct {
	TestName   string
	Target     string
	Transcript []Turn
	ToolCalls  []target.ToolCall
	Metrics    map[string]float64
	Artifacts  target.Artifacts
	// Status lists the HTTP status of every agent turn (rest).
	Status []int
	// Responses holds the raw payload of every agent turn (the JSON body for
	// rest), in order, for json_path.
	Responses []any
	// Captures are the values the run captured from the agent's turns.
	Captures map[string]string
	Events   []target.Event
	// Language is the persona's language, so judges know the conversation
	// is not in English on purpose.
	Language string
	// Judger, Evaluator and Embedder are the AI hooks the runner sets; nil
	// means no model is available and judge, evaluator or similar_to
	// assertions fail.
	Judger    Judger
	Evaluator Evaluator
	Embedder  Embedder
}

RunResult is everything one run produced, the input of every assertion.

func (*RunResult) AgentText

func (r *RunResult) AgentText() string

AgentText joins the agent's turns, newline separated: what contains, regex and the judges read.

func (*RunResult) AgentTurns

func (r *RunResult) AgentTurns() int

AgentTurns counts the agent's turns.

func (*RunResult) Turn

func (r *RunResult) Turn(n int) *RunResult

Turn returns a view of the run restricted to one agent turn (1-based; 0 or a number past the last turn means the last turn): its transcript holds the user turn that caused it and the agent turn, and Status and Responses hold that turn's entries. The metrics, tool calls and hooks are shared.

type Turn

type Turn struct {
	// Role is "user" or "agent".
	Role string    `json:"role"`
	Text string    `json:"text"`
	At   time.Time `json:"at"`
	// Index is the 1-based agent-turn number for agent turns, 0 for user turns.
	Index int `json:"index,omitempty"`
}

Turn is one line of the transcript.

Jump to

Keyboard shortcuts

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