server_validate

package
v0.260430.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: MPL-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package server_validate provides a mock HTTP provider server that speaks OpenAI, Anthropic, and Google response formats for testing purposes.

**Architecture Note**: VirtualServer is a **provider mock**, not a gateway mock. It speaks provider-native formats (OpenAI/Anthropic/Google APIs) at provider-native routes (/v1/chat/completions, /v1/messages, /v1beta/models/...).

The test harness routing flow is:

Client → Gateway (/tingly/{scenario}/v1/...) → Protocol Transform → Virtual Server (/v1/...)

A VirtualServer acts as a deterministic "virtual model" — scenario responses are pre-configured and returned without any real model calls. It is used by the protocol_validate test framework to exercise the gateway's protocol transform pipeline end-to-end.

Use VirtualClient (client.go) to send requests directly to the server and inspect parsed responses. A bound client is obtained via vs.Client().

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MockResponseBuilder

type MockResponseBuilder struct {
	// NonStream returns the HTTP status code and response body bytes.
	NonStream func() (statusCode int, body []byte)
	// Stream returns the SSE event lines (each line is "data: ..." or "event: ...").
	Stream func() []string
}

MockResponseBuilder defines how a virtual server should respond for one response format.

type ParsedResponse

type ParsedResponse struct {
	HTTPStatus   int
	IsStreaming  bool
	StreamEvents []string
	RawBody      []byte

	// Parsed semantics (populated from RawBody or StreamEvents)
	sse.ParsedResult
}

ParsedResponse is the result of a request sent to a virtual server. It wraps sse.ParsedResult with HTTP-layer fields.

type ResponseFormat added in v0.260430.1

type ResponseFormat string

ResponseFormat represents the format of the mock response for different endpoints.

const (
	FormatOpenAIChat      ResponseFormat = "openai_chat"      // /v1/chat/completions
	FormatOpenAIResponses ResponseFormat = "openai_responses" // /v1/responses
	FormatAnthropic       ResponseFormat = "anthropic"        // /v1/messages
	FormatGoogle          ResponseFormat = "google"           // /v1beta/models/.../generateContent
)

type Scenario

type Scenario struct {
	Name        string
	Description string
	Tags        []string

	// MockResponses keyed by response format (openai_chat, openai_responses, anthropic, google).
	// Each endpoint (/v1/chat/completions, /v1/responses, /v1/messages, /v1beta/models/.../generateContent)
	// returns the corresponding format.
	MockResponses map[ResponseFormat]MockResponseBuilder
}

Scenario is a named test scenario describing what the mock provider returns.

type VirtualClient

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

VirtualClient sends provider-native HTTP requests for testing. It can operate standalone (pointed at any URL) or bound to a VirtualServer (which auto-registers scenarios before each request).

func NewVirtualClient

func NewVirtualClient(baseURL string) *VirtualClient

NewVirtualClient creates a client pointing at baseURL.

func (*VirtualClient) SendAnthropicV1

func (vc *VirtualClient) SendAnthropicV1(t *testing.T, s Scenario, streaming bool) *ParsedResponse

SendAnthropicV1 sends a request to the Anthropic Messages endpoint.

func (*VirtualClient) SendAnthropicV1Model

func (vc *VirtualClient) SendAnthropicV1Model(t *testing.T, modelID string, streaming bool) *ParsedResponse

SendAnthropicV1Model sends a request to the Anthropic Messages endpoint using the specified model ID instead of the scenario-derived default.

func (*VirtualClient) SendGoogle

func (vc *VirtualClient) SendGoogle(t *testing.T, s Scenario, streaming bool) *ParsedResponse

SendGoogle sends a request to the Google GenerateContent endpoint.

func (*VirtualClient) SendOpenAIChat

func (vc *VirtualClient) SendOpenAIChat(t *testing.T, s Scenario, streaming bool) *ParsedResponse

SendOpenAIChat sends a request to the OpenAI Chat Completions endpoint.

func (*VirtualClient) SendOpenAIChatModel

func (vc *VirtualClient) SendOpenAIChatModel(t *testing.T, modelID string, streaming bool) *ParsedResponse

SendOpenAIChatModel sends a request to the OpenAI Chat Completions endpoint using the specified model ID instead of the scenario-derived default.

func (*VirtualClient) SendOpenAIResponses

func (vc *VirtualClient) SendOpenAIResponses(t *testing.T, s Scenario, streaming bool) *ParsedResponse

SendOpenAIResponses sends a request to the OpenAI Responses API endpoint.

func (*VirtualClient) WithServer

func (vc *VirtualClient) WithServer(vs *VirtualServer) *VirtualClient

WithServer binds the client to a VirtualServer. When bound, Send* methods auto-register the scenario before firing the request.

type VirtualServer

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

VirtualServer is a mock provider server backed by httptest.Server. It speaks OpenAI, Anthropic, and Google response formats and returns pre-configured scenario responses.

**Provider Routes**: This server handles provider-native routes (/v1/chat/completions, /v1/messages, /v1beta/models/...), NOT gateway routes (/tingly/{scenario}/v1/...). The gateway transforms requests to provider format before forwarding to this server.

func NewVirtualServer

func NewVirtualServer(t *testing.T) *VirtualServer

NewVirtualServer creates a new VirtualServer and registers cleanup with t.

func NewVirtualServerForCLI

func NewVirtualServerForCLI() *VirtualServer

NewVirtualServerForCLI creates a new VirtualServer for CLI use (without testing.T). The caller must call Close() to clean up resources.

**Provider Mock**: This is a provider mock, not a gateway. Routes are /v1/... (provider-native), not /tingly/... (gateway). The gateway transforms /tingly/{scenario}/v1/... requests to provider format before forwarding to this server.

func (*VirtualServer) CallCount

func (vs *VirtualServer) CallCount() int

CallCount returns the total number of requests received.

func (*VirtualServer) Client

func (vs *VirtualServer) Client() *VirtualClient

Client returns a VirtualClient pre-pointed at this VirtualServer and bound to it.

func (*VirtualServer) Close

func (vs *VirtualServer) Close()

Close shuts down the virtual server.

func (*VirtualServer) RegisterScenario

func (vs *VirtualServer) RegisterScenario(s Scenario)

RegisterScenario registers a scenario so the virtual server can serve its mock responses.

func (*VirtualServer) URL

func (vs *VirtualServer) URL() string

URL returns the base URL of the virtual server.

Jump to

Keyboard shortcuts

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