Documentation
¶
Overview ¶
Package mockllm provides a mock LLM server for E2E testing.
It implements an OpenAI-compatible chat completions API that can be configured to return specific responses based on message patterns or sequences.
Basic usage:
server := mockllm.NewServer()
server.OnMessage("hello", mockllm.TextResponse("Hello! How can I help?"))
server.Start(t)
defer server.Close()
// Configure crush to use server.URL() as the provider base URL
Tool call example:
server := mockllm.NewServer()
server.OnAny(mockllm.ToolCall("ping", `{}`))
server.OnToolResult("ping", mockllm.TextResponse("Pong received!"))
server.Start(t)
Index ¶
- func AssertLastMessageContains(t *testing.T, server *Server, text string)
- func AssertRequestCount(t *testing.T, server *Server, expected int)
- func AssertToolWasCalled(t *testing.T, server *Server, toolName string)
- func AssertToolWasNotCalled(t *testing.T, server *Server, toolName string)
- func EchoResponse(prefix string) func(req *ChatRequest) *ChatResponse
- func EmptyResponse() func(req *ChatRequest) *ChatResponse
- func ErrorResponse(errorMessage string) func(req *ChatRequest) *ChatResponse
- func MultiToolCallResponse(calls ...ToolCallSpec) func(req *ChatRequest) *ChatResponse
- func SetupTestEnv(t *testing.T, serverURL string) string
- func SetupTestEnvWithConfig(t *testing.T, serverURL string, additionalConfig map[string]any) string
- func TestConfig(serverURL string) string
- func TextAndToolResponse(content, toolName string, arguments any) func(req *ChatRequest) *ChatResponse
- func TextResponse(content string) func(req *ChatRequest) *ChatResponse
- func ToolCallResponse(toolName string, arguments any) func(req *ChatRequest) *ChatResponse
- type ChatRequest
- type ChatResponse
- type Choice
- type Conversation
- type Delta
- type Function
- type FunctionCall
- type FunctionDelta
- type MatchFunc
- func Always() MatchFunc
- func And(matchers ...MatchFunc) MatchFunc
- func HasSystemPrompt() MatchFunc
- func HasToolCall(toolName string) MatchFunc
- func HasToolResult(toolName string) MatchFunc
- func MessageContains(text string) MatchFunc
- func MessageCount(n int) MatchFunc
- func MessageEquals(text string) MatchFunc
- func Never() MatchFunc
- func Not(m MatchFunc) MatchFunc
- func Or(matchers ...MatchFunc) MatchFunc
- func SystemPromptContains(text string) MatchFunc
- type Message
- type Request
- type ResponseFunc
- type Server
- func (s *Server) Close()
- func (s *Server) Default(respond ResponseFunc) *Server
- func (s *Server) LastRequest() *Request
- func (s *Server) On(match MatchFunc, respond ResponseFunc) *Server
- func (s *Server) OnAny(respond ResponseFunc) *Server
- func (s *Server) OnMessage(contains string, respond ResponseFunc) *Server
- func (s *Server) OnToolResult(toolName string, respond ResponseFunc) *Server
- func (s *Server) Requests() []Request
- func (s *Server) Reset()
- func (s *Server) Sequence(responses ...ResponseFunc) *Server
- func (s *Server) Start(t *testing.T) string
- func (s *Server) URL() string
- type StreamChoice
- type StreamChunk
- type Tool
- type ToolCall
- type ToolCallDelta
- type ToolCallSpec
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertLastMessageContains ¶
AssertLastMessageContains checks if the last user message contains the text.
func AssertRequestCount ¶
AssertRequestCount checks the number of requests made to the server.
func AssertToolWasCalled ¶
AssertToolWasCalled checks if a specific tool was called.
func AssertToolWasNotCalled ¶
AssertToolWasNotCalled checks that a specific tool was not called.
func EchoResponse ¶
func EchoResponse(prefix string) func(req *ChatRequest) *ChatResponse
EchoResponse creates a response that echoes the last user message.
func EmptyResponse ¶
func EmptyResponse() func(req *ChatRequest) *ChatResponse
EmptyResponse creates a response with no content (edge case testing).
func ErrorResponse ¶
func ErrorResponse(errorMessage string) func(req *ChatRequest) *ChatResponse
ErrorResponse creates a response with an error message.
func MultiToolCallResponse ¶
func MultiToolCallResponse(calls ...ToolCallSpec) func(req *ChatRequest) *ChatResponse
MultiToolCallResponse creates a response that invokes multiple tools.
func SetupTestEnv ¶
SetupTestEnv creates an isolated test environment with the mock LLM server. Returns the tmpDir for use with NewIsolatedTerminalWithConfigAndEnv.
func SetupTestEnvWithConfig ¶
SetupTestEnvWithConfig creates an isolated test environment with custom config. Merges the provided config with mock LLM settings.
func TestConfig ¶
TestConfig creates config JSON that points to the mock server.
func TextAndToolResponse ¶
func TextAndToolResponse(content, toolName string, arguments any) func(req *ChatRequest) *ChatResponse
TextAndToolResponse creates a response with both text and a tool call.
func TextResponse ¶
func TextResponse(content string) func(req *ChatRequest) *ChatResponse
TextResponse creates a simple text response.
func ToolCallResponse ¶
func ToolCallResponse(toolName string, arguments any) func(req *ChatRequest) *ChatResponse
ToolCallResponse creates a response that invokes a tool.
Types ¶
type ChatRequest ¶
type ChatRequest struct {
Model string `json:"model"`
Messages []Message `json:"messages"`
Tools []Tool `json:"tools,omitempty"`
ToolChoice any `json:"tool_choice,omitempty"`
Stream bool `json:"stream,omitempty"`
MaxTokens int `json:"max_tokens,omitempty"`
Temperature *float64 `json:"temperature,omitempty"`
TopP *float64 `json:"top_p,omitempty"`
}
ChatRequest represents an OpenAI chat completion request.
type ChatResponse ¶
type ChatResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []Choice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
ChatResponse represents an OpenAI chat completion response.
func NewResponse ¶
func NewResponse(model string) *ChatResponse
NewResponse creates a new chat response with defaults.
type Choice ¶
type Choice struct {
Index int `json:"index"`
Message Message `json:"message"`
FinishReason string `json:"finish_reason,omitempty"`
}
Choice represents a completion choice.
type Conversation ¶
type Conversation struct {
// contains filtered or unexported fields
}
Conversation is a helper for building multi-turn conversations in tests.
func NewConversation ¶
func NewConversation(server *Server) *Conversation
NewConversation creates a new conversation builder.
func (*Conversation) Apply ¶
func (c *Conversation) Apply()
Apply sets up the conversation on the server.
func (*Conversation) Then ¶
func (c *Conversation) Then(respond ResponseFunc) *Conversation
Then adds the next response in the conversation.
func (*Conversation) ThenError ¶
func (c *Conversation) ThenError(message string) *Conversation
ThenError adds an error response.
func (*Conversation) ThenText ¶
func (c *Conversation) ThenText(content string) *Conversation
ThenText adds a text response.
func (*Conversation) ThenTool ¶
func (c *Conversation) ThenTool(name string, args any) *Conversation
ThenTool adds a tool call response.
type Delta ¶
type Delta struct {
Role string `json:"role,omitempty"`
Content string `json:"content,omitempty"`
ToolCalls []ToolCallDelta `json:"tool_calls,omitempty"`
}
Delta represents incremental content in a stream.
type Function ¶
type Function struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Parameters any `json:"parameters,omitempty"`
}
Function represents a function definition.
type FunctionCall ¶
FunctionCall represents a function call.
type FunctionDelta ¶
type FunctionDelta struct {
Name string `json:"name,omitempty"`
Arguments string `json:"arguments,omitempty"`
}
FunctionDelta represents incremental function call data.
type MatchFunc ¶
type MatchFunc func(req ChatRequest) bool
MatchFunc determines if a handler should respond to a request.
func HasSystemPrompt ¶
func HasSystemPrompt() MatchFunc
HasSystemPrompt returns true if the request has a system message.
func HasToolCall ¶
HasToolCall returns true if any assistant message contains a tool call with the given name.
func HasToolResult ¶
HasToolResult returns true if there's a tool result message with the given name.
func MessageContains ¶
MessageContains returns true if the last user message contains the text.
func MessageCount ¶
MessageCount returns true if the request has exactly n messages.
func MessageEquals ¶
MessageEquals returns true if the last user message equals the text exactly.
func SystemPromptContains ¶
SystemPromptContains returns true if the system prompt contains the text.
type Message ¶
type Message struct {
Role string `json:"role"` // system, user, assistant, tool
Content string `json:"content,omitempty"`
Name string `json:"name,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
ToolCallID string `json:"tool_call_id,omitempty"`
}
Message represents a chat message.
type Request ¶
type Request struct {
Method string
Path string
Body ChatRequest
Timestamp time.Time
}
Request represents a captured request to the mock server.
type ResponseFunc ¶
type ResponseFunc func(req *ChatRequest) *ChatResponse
ResponseFunc generates a response for a request.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a mock OpenAI-compatible LLM server.
func (*Server) Default ¶
func (s *Server) Default(respond ResponseFunc) *Server
Default sets the default response when no handlers match.
func (*Server) LastRequest ¶
LastRequest returns the most recent request.
func (*Server) On ¶
func (s *Server) On(match MatchFunc, respond ResponseFunc) *Server
On adds a custom handler with a matcher.
func (*Server) OnAny ¶
func (s *Server) OnAny(respond ResponseFunc) *Server
OnAny adds a handler that matches any request.
func (*Server) OnMessage ¶
func (s *Server) OnMessage(contains string, respond ResponseFunc) *Server
OnMessage adds a handler that matches when the last user message contains the text.
func (*Server) OnToolResult ¶
func (s *Server) OnToolResult(toolName string, respond ResponseFunc) *Server
OnToolResult adds a handler that matches when there's a tool result with the given name.
func (*Server) Sequence ¶
func (s *Server) Sequence(responses ...ResponseFunc) *Server
Sequence configures the server to return responses in order. Each call to the server returns the next response in the sequence.
type StreamChoice ¶
type StreamChoice struct {
Index int `json:"index"`
Delta Delta `json:"delta"`
FinishReason string `json:"finish_reason,omitempty"`
}
StreamChoice represents a streaming choice.
type StreamChunk ¶
type StreamChunk struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []StreamChoice `json:"choices"`
Usage *Usage `json:"usage,omitempty"`
}
StreamChunk represents a streaming response chunk.
func ParseSSEStream ¶
func ParseSSEStream(r io.Reader) ([]StreamChunk, error)
ParseSSEStream parses an SSE stream and returns chunks. Useful for testing streaming responses.
type ToolCall ¶
type ToolCall struct {
ID string `json:"id"`
Type string `json:"type"` // "function"
Function FunctionCall `json:"function"`
}
ToolCall represents a tool invocation.
type ToolCallDelta ¶
type ToolCallDelta struct {
Index int `json:"index"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Function FunctionDelta `json:"function,omitempty"`
}
ToolCallDelta represents incremental tool call data.
type ToolCallSpec ¶
ToolCallSpec defines a tool call for MultiToolCallResponse.