modeltest

package
v0.44.0 Latest Latest
Warning

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

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

Documentation

Overview

Package modeltest provides reusable contract tests for Core model interfaces. Provider tests run real SDK transports against local servers to check request immutability, ordered results, stream errors, cancellation, and teardown. Production code must not import this package.

Example
package main

import (
	"fmt"
	"iter"

	"github.com/Tangerg/scope/core/modeltest"
)

func main() {
	sequence := iter.Seq2[string, error](func(yield func(string, error) bool) {
		yield("first", nil)
		yield("second", nil)
	})
	values, err := modeltest.Collect(sequence)
	fmt.Println(values, err)
}
Output:
[first second] 

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnthropicSSEServer

func AnthropicSSEServer(events []AnthropicEvent) *httptest.Server

AnthropicSSEServer emits named events in the supplied order. The caller supplies the complete event sequence and owns server cleanup.

func BinaryServer

func BinaryServer(status int, contentType string, body []byte, inspections ...func(request *http.Request)) *httptest.Server

func Collect

func Collect[T any](seq iter.Seq2[T, error]) ([]T, error)

Collect stops at the first error and returns values yielded before it.

func CollectN

func CollectN[T any](sequence iter.Seq2[T, error], count int) ([]T, error)

CollectN returns at most count values, stopping on the first error. A nonpositive count does not start the iterator.

func JSONServer

func JSONServer(status int, body string, inspections ...func(request *http.Request)) *httptest.Server

Inspections run before the response is written and receive the live request. Read the body during inspection; it is unavailable after the handler returns.

func LookupEnv

func LookupEnv(name string) (string, bool)

LookupEnv treats an empty value as absent.

func MuxServer

func MuxServer(routes ...Route) *httptest.Server

MuxServer matches routes in order; place a catch-all last. The caller owns server cleanup.

func OpenAISSEServer

func OpenAISSEServer(chunks []string) *httptest.Server

OpenAISSEServer frames JSON chunks as data events followed by [DONE]. The caller owns the server and must close it after use.

func RequireEnv

func RequireEnv(t *testing.T, name string) string

RequireEnv skips the test when the named environment variable is empty.

func RequireKey

func RequireKey(t *testing.T, provider string) string

RequireKey reads SCOPE_TEST__KEY and skips the test when empty.

func RunChatBehaviorContract added in v0.36.0

func RunChatBehaviorContract(t *testing.T, contract ChatBehaviorContract)

func RunChatContract added in v0.36.0

func RunChatContract(t *testing.T, contract ChatContract)

func RunEmbeddingContract

func RunEmbeddingContract(t *testing.T, contract EmbeddingContract)

func RunIntegrationEmbedding

func RunIntegrationEmbedding(t *testing.T, probe IntegrationEmbeddingProbe)

func RunIntegrationRerank added in v0.12.0

func RunIntegrationRerank(t *testing.T, probe IntegrationRerankProbe)

Live results may change ordering; validation checks request-relative bounds.

func RunRerankContract added in v0.12.0

func RunRerankContract(t *testing.T, contract RerankContract)

RunRerankContract accepts top_n or top_k on the wire.

func WithTimeout

func WithTimeout(t *testing.T, duration time.Duration) (context.Context, context.CancelFunc)

WithTimeout derives the call deadline from the test context.

Types

type AnthropicEvent

type AnthropicEvent struct {
	Event string
	Data  string
}

type CallBehaviorCase

type CallBehaviorCase struct {
	Model     chat.Model
	Lifecycle Lifecycle
}

type ChatBehaviorContract added in v0.36.0

type ChatBehaviorContract struct {
	Request            func(t *testing.T) *chat.Request
	CallCancellation   func(t *testing.T) CallBehaviorCase
	StreamCancellation func(t *testing.T) StreamBehaviorCase
	EarlyStop          func(t *testing.T) StreamBehaviorCase
	FirstError         func(t *testing.T) chat.Streamer
}

Each factory must return fresh state backed by the provider transport.

type ChatContract added in v0.36.0

type ChatContract struct {
	New              func(t *testing.T) (chat.Model, chat.Streamer)
	Request          func(t *testing.T) *chat.Request
	AssertCall       func(t *testing.T, response *chat.Response)
	AssertStream     func(t *testing.T, deltas []*chat.ResponseDelta)
	AssertAggregated func(t *testing.T, response *chat.Response)
}

New and Request must return fresh state for each Call and Stream subtest.

type EmbeddingContract

type EmbeddingContract struct {
	ModelID string
	// Response must encode two embeddings matching ExpectedEmbeddings in order.
	Response string
	// InputField names the provider JSON field containing the ordered input texts.
	InputField string

	ExpectedEmbeddings [][]float64
	// An empty path skips the URL-path assertion.
	ExpectedPath string

	Build func(t *testing.T, baseURL string) embedding.Model
}

type IntegrationEmbeddingProbe

type IntegrationEmbeddingProbe struct {
	Provider string
	Build    func(t *testing.T, key string) embedding.Model
}

type IntegrationRerankProbe added in v0.12.0

type IntegrationRerankProbe struct {
	Provider string
	Build    func(t *testing.T, key string) rerank.Model
}

type Lifecycle

type Lifecycle struct {
	Started <-chan struct{}
	Stopped <-chan struct{}
}

Lifecycle observes one in-flight provider request. Started closes after the mock has sent any initial stream event; Stopped closes when the request context is released and the handler exits.

func NewBlockingServer

func NewBlockingServer(t *testing.T, writeInitial func(http.ResponseWriter)) (*httptest.Server, Lifecycle)

NewBlockingServer holds one request until cancellation. Lifecycle signals allow tests to synchronize with the initial write and handler exit. The test owns server cleanup.

type PollCounter

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

PollCounter is safe for concurrent handlers.

func (*PollCounter) Inc

func (p *PollCounter) Inc() int32

func (*PollCounter) N

func (p *PollCounter) N() int32

type RerankContract added in v0.12.0

type RerankContract struct {
	ModelID string
	// Response must be valid for a three-document query capped at two results.
	Response string

	ExpectedPath string

	Build func(t *testing.T, baseURL string) rerank.Model
}

type Route

type Route struct {
	Method   string
	Contains string
	Handle   http.HandlerFunc
}

Contains matches a URL-path substring. Empty Method or Contains matches any method or path, respectively.

type StreamBehaviorCase

type StreamBehaviorCase struct {
	Streamer  chat.Streamer
	Lifecycle Lifecycle
}

Jump to

Keyboard shortcuts

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