testutil

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 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 MockCodingAgent added in v0.12.0

type MockCodingAgent struct {
	MockProvider // base common.Provider behavior (Query/GetName/...)

	// Symlinks maps a shown destination path to the real path the agent
	// resolves it to. When a FileOperation's ShownDestination matches a key,
	// the write resolves to the mapped target (the SymJack misrepresentation).
	// Absent a mapping, the resolved destination equals the shown destination.
	Symlinks map[string]string

	// NoApprovalStep makes ApproveFileOperation report HasApprovalStep=false
	// (the SkipNoMutationTarget case — agent has nothing to hijack).
	NoApprovalStep bool
	// DenyApproval makes the agent decline the operation (OutcomeRefused).
	DenyApproval bool
	// ApproveErr, when set, is returned from ApproveFileOperation (SkipProviderError).
	ApproveErr error

	// AutoExecuteOnTrust executes the repo's ProjectMCPPaths on a trust accept
	// (the TrustFall default-trust behavior). When false, trust does not
	// auto-execute project MCP (OutcomeRefused).
	AutoExecuteOnTrust bool
	// NoTrustPrompt makes TrustFolder report HasTrustPrompt=false.
	NoTrustPrompt bool
	// TrustErr, when set, is returned from TrustFolder (SkipProviderError).
	TrustErr error

	// Recorded effects for assertions.
	Writes      []ResolvedWrite
	ExecutedMCP []string
	// contains filtered or unexported fields
}

MockCodingAgent is a controllable in-memory implementation of common.CodingAgentProvider for exercising the v0.12.0 SymJack and TrustFall modules end-to-end. It performs no real filesystem or process operations — symlink resolution and MCP auto-execution are simulated and recorded for assertion. Refuse-mode knobs let tests drive the OutcomeRefused and SkipNoMutationTarget paths, not just success.

func (*MockCodingAgent) ApproveFileOperation added in v0.12.0

func (m *MockCodingAgent) ApproveFileOperation(_ context.Context, op common.FileOperation) (common.ApprovalOutcome, error)

ApproveFileOperation simulates presenting a file operation to the agent's approval surface, resolving the destination through the configured symlink table and recording any write.

func (*MockCodingAgent) TrustFolder added in v0.12.0

TrustFolder simulates a folder-trust decision, auto-executing the repo's project MCP paths when AutoExecuteOnTrust is set.

func (*MockCodingAgent) WriteCount added in v0.12.0

func (m *MockCodingAgent) WriteCount() int

WriteCount returns the number of recorded writes (for assertions).

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 MockMemoryProvider added in v0.12.0

type MockMemoryProvider struct {
	*MockProvider
	// contains filtered or unexported fields
}

MockMemoryProvider is a stateful provider for exercising the #168 cleanup loop. It records the content of every prompt it receives (its "memory store") and implements common.MemoryProbe (so memory-poisoning modules proceed past their capability gate) and common.Purger (so the implant can be rolled back). It is the in-memory reference Purger from #168's acceptance.

func NewMockMemoryProvider added in v0.12.0

func NewMockMemoryProvider(defaultResponse string) *MockMemoryProvider

NewMockMemoryProvider returns a stateful memory provider whose plain Query responses default to defaultResponse.

func (*MockMemoryProvider) Has added in v0.12.0

func (m *MockMemoryProvider) Has(id string) bool

Has reports whether any stored entry contains the given record ID — used by tests to assert an implant is present or absent.

func (*MockMemoryProvider) ProbeMemory added in v0.12.0

func (m *MockMemoryProvider) ProbeMemory(_ context.Context) (bool, error)

ProbeMemory reports the target retains memory (it's a stateful store). It returns true regardless of current contents — the probe is a capability statement, not a "has records right now" check.

func (*MockMemoryProvider) Purge added in v0.12.0

func (m *MockMemoryProvider) Purge(_ context.Context, recordIDs []string) error

Purge drops every stored entry that contains any of the given record IDs. Idempotent: purging an absent ID is a no-op, not an error.

func (*MockMemoryProvider) Query added in v0.12.0

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

Query records each message's content into the store, then delegates to the embedded MockProvider for the response.

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)

type ResolvedWrite added in v0.12.0

type ResolvedWrite struct {
	Source   string
	Shown    string
	Resolved string
}

ResolvedWrite records a file write the agent performed, capturing the gap between what the approval prompt showed and where the bytes actually landed.

Jump to

Keyboard shortcuts

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