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] <nil>
Index ¶
- func AnthropicSSEServer(events []AnthropicEvent) *httptest.Server
- func BinaryServer(status int, contentType string, body []byte, ...) *httptest.Server
- func Collect[T any](seq iter.Seq2[T, error]) ([]T, error)
- func CollectN[T any](sequence iter.Seq2[T, error], count int) ([]T, error)
- func JSONServer(status int, body string, inspections ...func(request *http.Request)) *httptest.Server
- func LookupEnv(name string) (string, bool)
- func MuxServer(routes ...Route) *httptest.Server
- func OpenAISSEServer(chunks []string) *httptest.Server
- func RequireEnv(t *testing.T, name string) string
- func RequireKey(t *testing.T, provider string) string
- func RunChatBehaviorContract(t *testing.T, contract ChatBehaviorContract)
- func RunChatContract(t *testing.T, contract ChatContract)
- func RunEmbeddingContract(t *testing.T, contract EmbeddingContract)
- func RunHTTPClientContract(t *testing.T, contract HTTPClientContract)
- func RunIntegrationEmbedding(t *testing.T, probe IntegrationEmbeddingProbe)
- func RunIntegrationRerank(t *testing.T, probe IntegrationRerankProbe)
- func RunRerankContract(t *testing.T, contract RerankContract)
- type AnthropicEvent
- type CallBehaviorCase
- type ChatBehaviorContract
- type ChatContract
- type EmbeddingContract
- type HTTPClientContract
- type IntegrationEmbeddingProbe
- type IntegrationRerankProbe
- type Lifecycle
- type RerankContract
- type Route
- type StreamBehaviorCase
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
BinaryServer is JSONServer for a non-JSON body of the given content type.
func CollectN ¶
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
JSONServer answers every request with body and status. Inspections run before the response is written and receive the live request; read the body during inspection, because it is unavailable after the handler returns.
func MuxServer ¶
MuxServer matches routes in order; place a catch-all last. The caller owns server cleanup.
func OpenAISSEServer ¶
OpenAISSEServer frames JSON chunks as data events followed by [DONE]. The caller owns the server and must close it after use.
func RequireEnv ¶
RequireEnv skips the test when the named environment variable is empty.
func RequireKey ¶
RequireKey reads SCOPE_TEST_<PROVIDER>_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 RunHTTPClientContract ¶ added in v0.58.0
func RunHTTPClientContract(t *testing.T, contract HTTPClientContract)
The default-transport probe changes process state and must run sequentially.
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 checks the wire request and the effective result limit: Core owns how a request limit, the default and the document count combine, and how many results the response must then hold.
Types ¶
type AnthropicEvent ¶
type CallBehaviorCase ¶
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 HTTPClientContract ¶ added in v0.58.0
type IntegrationRerankProbe ¶ added in v0.12.0
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.
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
// LimitField is the wire member carrying the effective result limit, and
// ResultsField the response member listing {index, relevance_score} items.
LimitField, ResultsField string
// Build constructs the model against baseURL with defaults as its
// DefaultOptions.
Build func(t *testing.T, baseURL string, defaults rerank.Options) rerank.Model
}
type Route ¶
type Route struct {
Method string
Contains string
Handle http.HandlerFunc
}
Route answers requests whose method equals Method and whose URL path contains Contains; an empty Method or Contains matches any method or path.