mock

package
v1.0.0 Latest Latest
Warning

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

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

Documentation

Overview

Package mock provides in-memory, network-free test doubles for the stores.Embedder and stores.VectorStore interfaces.

Both doubles record call counts so tests can assert negative facts such as "the RAG stage never ran retrieval when NeedsRetrieval was false". Both are safe for concurrent use.

Index

Constants

View Source
const DefaultDim = 64

DefaultDim is the vector dimensionality used by Embedder when none is given.

Variables

This section is empty.

Functions

func Cosine

func Cosine(a, b []float32) float64

Cosine returns the cosine similarity of a and b. Mismatched lengths are compared over the shorter prefix; a zero-magnitude vector yields 0.

func Vector

func Vector(text string, dim int) []float32

Vector deterministically hashes text into a unit vector of length dim. Exported so tests can build the exact vector a given text will embed to.

Types

type Document

type Document struct {
	Chunk  pipeline.Chunk
	Vector []float32
}

Document is a chunk plus its vector, as held by Store.

type Embedder

type Embedder struct {
	// Dim is the vector dimensionality. Zero means DefaultDim.
	Dim int

	// Err, if set, is returned from every Embed call (fail-open testing).
	Err error

	// EmbedFunc, if set, overrides the default hashing behaviour entirely.
	EmbedFunc func(ctx context.Context, text string) ([]float32, error)
	// contains filtered or unexported fields
}

Embedder is a deterministic, offline stores.Embedder. It hashes the input text into a fixed-dimension unit vector: the same text always yields the same vector, and similar texts (sharing words) yield vectors with higher cosine similarity. It performs no I/O.

func NewEmbedder

func NewEmbedder(dim int) *Embedder

NewEmbedder returns an Embedder with the given dimensionality. dim <= 0 uses DefaultDim.

func NewFailingEmbedder

func NewFailingEmbedder(err error) *Embedder

NewFailingEmbedder returns an Embedder whose Embed always fails with err.

func (*Embedder) Calls

func (e *Embedder) Calls() int

Calls reports how many times Embed was invoked.

func (*Embedder) Embed

func (e *Embedder) Embed(ctx context.Context, text string) ([]float32, error)

Embed implements stores.Embedder.

func (*Embedder) Inputs

func (e *Embedder) Inputs() []string

Inputs returns a copy of every text passed to Embed, in call order.

func (*Embedder) Reset

func (e *Embedder) Reset()

Reset zeroes the recorded call counts and inputs.

type Store

type Store struct {
	// Err, if set, is returned from every Search call (fail-open testing).
	Err error

	// SearchFunc, if set, overrides the default scan entirely.
	SearchFunc func(ctx context.Context, vec []float32, topK int) ([]pipeline.Chunk, error)
	// contains filtered or unexported fields
}

Store is an in-memory stores.VectorStore doing a linear-scan cosine similarity search. It is safe for concurrent use.

func NewFailingStore

func NewFailingStore(err error) *Store

NewFailingStore returns a Store whose Search always fails with err.

func NewStore

func NewStore() *Store

NewStore returns an empty Store.

func (*Store) Add

func (s *Store) Add(dim int, chunks ...pipeline.Chunk)

Add indexes a chunk under the vector produced by embedding its content with Vector at dimensionality dim. It is the convenient way to seed a Store that will be queried through this package's Embedder.

func (*Store) AddDocuments

func (s *Store) AddDocuments(docs ...Document)

AddDocuments indexes pre-vectorized documents.

func (*Store) Calls

func (s *Store) Calls() int

Calls reports how many times Search was invoked.

func (*Store) Len

func (s *Store) Len() int

Len reports how many documents are indexed.

func (*Store) Reset

func (s *Store) Reset()

Reset zeroes the recorded call count (documents are kept).

func (*Store) Search

func (s *Store) Search(ctx context.Context, vec []float32, topK int) ([]pipeline.Chunk, error)

Search implements stores.VectorStore. It returns the topK most cosine-similar chunks in descending similarity order, with Chunk.Similarity populated.

Jump to

Keyboard shortcuts

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