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 ¶
- 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 RunIntegrationEmbedding(t *testing.T, probe IntegrationEmbeddingProbe)
- func RunIntegrationRerank(t *testing.T, probe IntegrationRerankProbe)
- func RunRerankContract(t *testing.T, contract RerankContract)
- func WithTimeout(t *testing.T, duration time.Duration) (context.Context, context.CancelFunc)
- type AnthropicEvent
- type CallBehaviorCase
- type ChatBehaviorContract
- type ChatContract
- type EmbeddingContract
- type IntegrationEmbeddingProbe
- type IntegrationRerankProbe
- type Lifecycle
- type PollCounter
- 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 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
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 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_
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 ¶
WithTimeout derives the call deadline from the test context.
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 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. 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 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.