testutil

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testutil provides shared test utilities including mock HTTP servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnthropicEventJSON

func AnthropicEventJSON(eventType string, data any) string

AnthropicEventJSON returns a valid Anthropic SSE event pair (event: + data:).

func AnthropicStreamHandler

func AnthropicStreamHandler(lines []string) http.HandlerFunc

AnthropicStreamHandler returns an http.HandlerFunc that writes raw Anthropic SSE lines.

func DefaultCompletionResponse

func DefaultCompletionResponse() []byte

DefaultCompletionResponse returns a standard OpenAI-format completion response.

func DefaultEmbeddingResponse

func DefaultEmbeddingResponse() []byte

DefaultEmbeddingResponse returns a standard OpenAI-format embedding response.

func OpenAIChunkJSON

func OpenAIChunkJSON(id, model, content, finishReason string) string

OpenAIChunkJSON returns a valid OpenAI-format streaming chunk JSON string.

func OpenAIChunkJSONWithUsage

func OpenAIChunkJSONWithUsage(id, model string, promptTokens, completionTokens, totalTokens int) string

OpenAIChunkJSONWithUsage returns a final OpenAI chunk with usage info.

func OpenAIStreamHandler

func OpenAIStreamHandler(chunks []string) http.HandlerFunc

OpenAIStreamHandler returns an http.HandlerFunc that streams OpenAI-format chunks.

func WriteSSEChunks

func WriteSSEChunks(w http.ResponseWriter, chunks []string)

WriteSSEChunks writes SSE data lines to the ResponseWriter, then flushes.

Types

type FakeStream

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

FakeStream is a deterministic provider.Stream backed by a slice of chunks. It returns each chunk in order, then io.EOF. Use BlockOn to inject a wait before delivery (e.g. to test heartbeat / cancellation behaviour).

func NewFakeStream

func NewFakeStream(chunks []*provider.StreamChunk, usage *provider.Usage) *FakeStream

NewFakeStream returns a FakeStream that emits the supplied chunks.

func (*FakeStream) ChunksRead

func (f *FakeStream) ChunksRead() int

ChunksRead returns the number of chunks that were actually delivered before the stream was closed or canceled.

func (*FakeStream) Close

func (f *FakeStream) Close() error

Close implements provider.Stream.

func (*FakeStream) Closed

func (f *FakeStream) Closed() bool

Closed reports whether Close has been called.

func (*FakeStream) Next

Next implements provider.Stream.

func (*FakeStream) SetCloseFunc

func (f *FakeStream) SetCloseFunc(fn func())

SetCloseFunc registers a callback fired when Close is called. Useful for tests that need to assert clean shutdown.

func (*FakeStream) Usage

func (f *FakeStream) Usage() *provider.Usage

Usage implements provider.Stream.

type MockController

type MockController struct {

	// Request capture
	LastMethod string
	LastPath   string
	LastBody   []byte
	LastHeader http.Header
	// contains filtered or unexported fields
}

MockController configures how the mock server responds.

func (*MockController) GetLastBody

func (c *MockController) GetLastBody() []byte

GetLastBody returns the last captured request body.

func (*MockController) GetLastHeader

func (c *MockController) GetLastHeader() http.Header

GetLastHeader returns the last captured request headers.

func (*MockController) GetLastPath

func (c *MockController) GetLastPath() string

GetLastPath returns the last captured request path.

func (*MockController) SetCompletion

func (c *MockController) SetCompletion(resp any)

SetCompletion sets the completion response body.

func (*MockController) SetEmbedding

func (c *MockController) SetEmbedding(resp any)

SetEmbedding sets the embedding response body.

func (*MockController) SetStatusCode

func (c *MockController) SetStatusCode(code int)

SetStatusCode sets the HTTP status code to return.

func (*MockController) SetStreamHandler

func (c *MockController) SetStreamHandler(h http.HandlerFunc)

SetStreamHandler sets a custom handler for streaming requests.

type MockServer

type MockServer struct {
	Server *httptest.Server
	Ctrl   *MockController
}

MockServer is a configurable HTTP test server for provider testing.

func NewMockServer

func NewMockServer(t *testing.T) *MockServer

NewMockServer creates a new mock HTTP server for testing providers.

Jump to

Keyboard shortcuts

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