mock

package
v1.20.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package mock is Harbor's test-grade LLM driver. It self-registers under `"mock"` via init() and is the default driver in `internal/llm` (DefaultDriver = "mock"). Production deployments configure the `bifrost` driver explicitly.

The driver supports:

  • Text-only AND multimodal (text + image/audio/file parts) round-trips. The returned content is a deterministic synthesis of the input — useful for property tests that need stable assertions.
  • Streaming via `req.Stream` + `req.OnContent` / `req.OnReasoning` callbacks. The mock chunks the synthetic response into N small pieces (controllable via `Options.StreamChunks`), invokes the callbacks for each chunk, and surfaces a final `done=true` call.
  • ctx cancellation. Streaming respects `ctx.Done()` between chunks; non-streaming respects it before the synthesis step.
  • Cost/Usage reporting (synthetic; lets governance tests run without a real provider).

Concurrent-reuse: the driver itself is stateless. The only mutable state is the `closed` atomic.Bool which guards Close idempotency. Concurrent Complete calls are safe by construction.

Test-only behaviour hooks (NOT operator-facing):

  • `Options.SyntheticContent` overrides the generated response content. Useful for failure-mode tests.
  • `Options.ForcedError` makes every Complete return that error. Useful for ErrClientClosed / retry-path tests.
  • `Options.SeenIdentity` is an optional sink channel that receives the identity from every Complete call. Used by the concurrent-reuse test to verify no context bleed.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

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

Driver is the exported mock driver type so test code can build instances directly (without going through the registry) when it needs to inject test-only Options.

func New

func New(opts Options) *Driver

New constructs a mock Driver with the supplied Options.

For registry-path use, callers should NOT call New directly — `llm.Open(...)` with `cfg.Driver = "mock"` constructs a zero-Opts driver via the init() registration. Direct New is for tests that inject SeenIdentity / ForcedError / SyntheticContent.

func (*Driver) Close

func (d *Driver) Close(_ context.Context) error

Close marks the driver closed. Streaming is synchronous on the caller's goroutine, so there are no goroutines to drain. Idempotent.

func (*Driver) Complete

Complete is the Driver.Complete entry point. The safety pass has already run upstream; this method synthesises a response.

Honors ctx cancellation between work units (Step 1: identity observation; Step 2: streaming chunks; Step 3: final assembly). A cancelled ctx returns ctx.Err() (typically context.Canceled or context.DeadlineExceeded) — NOT a wrapped error.

type Options

type Options struct {
	// SyntheticContent overrides the generated response. Empty
	// means "use the default synthesis (echo of the last user
	// message)."
	SyntheticContent string
	// ForcedError makes every Complete fail with this error.
	ForcedError error
	// StreamChunks is the number of chunks the streaming path
	// produces. Zero defaults to 4 (small enough for fast tests).
	StreamChunks int
	// SeenIdentity is an optional sink for the identity each
	// Complete observes. Buffered N=1 by callers that race on it.
	SeenIdentity chan<- identity.Quadruple
	// PreStreamDelay is a hook for the cancellation test —
	// when > 0, the streaming path waits this duration BETWEEN
	// chunks (honouring ctx.Done()). Lets the test cancel
	// mid-stream and observe clean abort.
	PreStreamDelay time.Duration
}

Options carries test-only knobs that influence the mock's response. Construct via `New(opts)` for tests that need the hooks; registry-path construction passes a zero-value Options.

Jump to

Keyboard shortcuts

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