testutil

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package testutil provides shared test helpers for attack module tests. It includes mock implementations of Provider and Logger, a configurable mock HTTP server, and fixture loading utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAttackConfig

func DefaultAttackConfig() common.AttackConfig

DefaultAttackConfig returns a reasonable AttackConfig for testing.

func LoadFixture

func LoadFixture(path string) ([]byte, error)

LoadFixture reads a JSON fixture file from the testdata directory relative to the calling package. The path should be relative (e.g., "testdata/payload.json").

func LoadFixtureJSON

func LoadFixtureJSON(path string, target interface{}) error

LoadFixtureJSON reads a JSON fixture and unmarshals it into target.

Types

type LogEntry

type LogEntry struct {
	Level   string
	Message string
	Args    []interface{}
}

LogEntry records a single log call.

type MockCall

type MockCall struct {
	Messages []common.Message
	Options  map[string]interface{}
}

MockCall records a single Query invocation.

type MockLLMServer

type MockLLMServer struct {
	Server *httptest.Server
	// contains filtered or unexported fields
}

MockLLMServer wraps httptest.Server to simulate an LLM API endpoint. By default it returns a JSON chat-completion-style response.

func NewMockLLMServer

func NewMockLLMServer(defaultResponse string) *MockLLMServer

NewMockLLMServer creates and starts a mock HTTP server that responds with configurable chat completion responses.

func (*MockLLMServer) Close

func (m *MockLLMServer) Close()

Close shuts down the test server.

func (*MockLLMServer) QueueResponse

func (m *MockLLMServer) QueueResponse(resp string)

QueueResponse adds a response to the queue.

func (*MockLLMServer) RequestCount

func (m *MockLLMServer) RequestCount() int

RequestCount returns the number of requests received.

func (*MockLLMServer) URL

func (m *MockLLMServer) URL() string

URL returns the base URL of the mock server.

type MockLogger

type MockLogger struct {
	Entries []LogEntry
	// contains filtered or unexported fields
}

MockLogger implements common.Logger and captures log entries for assertion.

func (*MockLogger) Debug

func (l *MockLogger) Debug(msg string, keysAndValues ...interface{})

func (*MockLogger) Error

func (l *MockLogger) Error(msg string, keysAndValues ...interface{})

func (*MockLogger) HasMessage

func (l *MockLogger) HasMessage(level, substr string) bool

HasMessage returns true if any entry at the given level contains substr.

func (*MockLogger) Info

func (l *MockLogger) Info(msg string, keysAndValues ...interface{})

func (*MockLogger) Warn

func (l *MockLogger) Warn(msg string, keysAndValues ...interface{})

type MockProvider

type MockProvider struct {

	// Name and Model returned by GetName / GetModel.
	ProviderName string
	ModelName    string

	// Responses is a queue of responses. Each call to Query pops the first
	// entry. When the queue is exhausted, DefaultResponse is returned.
	Responses       []string
	DefaultResponse string

	// ErrorOn can be set to make Query return an error on the Nth call (1-based).
	ErrorOn int
	// ErrorMsg is the error message returned when ErrorOn triggers.
	ErrorMsg string

	// Calls records every Query call for assertions.
	Calls []MockCall
	// contains filtered or unexported fields
}

MockProvider implements common.Provider for testing. It returns canned responses and records every call for later assertion.

func (*MockProvider) CallCount

func (m *MockProvider) CallCount() int

CallCount returns the number of Query calls made.

func (*MockProvider) GetModel

func (m *MockProvider) GetModel() string

func (*MockProvider) GetName

func (m *MockProvider) GetName() string

func (*MockProvider) GetTokenCount

func (m *MockProvider) GetTokenCount(text string) int

func (*MockProvider) LastCall

func (m *MockProvider) LastCall() *MockCall

LastCall returns the most recent Query call, or nil if none.

func (*MockProvider) Query

func (m *MockProvider) Query(_ context.Context, messages []common.Message, options map[string]interface{}) (string, error)

Jump to

Keyboard shortcuts

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