vibe.embedding_providers.mock

Mock embedding provider for testing.

ConfigurableMockEmbeddingProvider allows tests to: - Configure specific embeddings for known texts - Generate deterministic embeddings based on text content - Verify embedding calls were made with expected inputs - Simulate various embedding scenarios (errors, delays)

Following patterns from vibe.llm_providers.mock.ConfigurableMockProvider.

ConfigurableMockEmbeddingProvider

Test-friendly mock embedding provider.

Features: - Call capture for test assertions - Configurable embeddings for specific texts - Deterministic hash-based embeddings for reproducibility - Simulated delays and errors - Cross-process configuration via temp files

Usage

Configure before test

configure_mock_embedding_provider(dimension=1024)

Use provider

provider = ConfigurableMockEmbeddingProvider() embedding = provider.embed("some text")

Verify calls

assert len(provider.calls) == 1 assert provider.calls[0] == "some text"

__init__

__init__(config: dict[str, Any] | None = None) -> None

Initialize the mock provider.

Configuration can come from: 1. Constructor config dict 2. File-based config (from configure_mock_embedding_provider) 3. Defaults

embed

embed(text: str) -> list[float]

Generate embedding for a single text.

Checks configuration in order: 1. Local embeddings (self.local_embeddings) 2. File-based configured embeddings 3. Deterministic generation

Also applies configured delays and error simulation.

embed_batch

embed_batch(texts: list[str]) -> list[list[float]]

Generate embeddings for multiple texts.

Records the batch call for test assertions.

set_embedding

set_embedding(text: str, embedding: list[float]) -> None

Set a specific embedding for a text.

Useful for tests that need exact embedding values.

set_embeddings

set_embeddings(embeddings: dict[str, list[float]]) -> None

Set multiple text-to-embedding mappings.

clear_calls

clear_calls() -> None

Clear recorded calls (useful between test cases).

assert_called_with

assert_called_with(text: str) -> None

Assert that embed() was called with specific text.

assert_call_count

assert_call_count(expected: int) -> None

Assert number of embed() calls.

configure_mock_embedding_provider

configure_mock_embedding_provider(*, port: int = 0, dimension: int = 1024, embeddings: dict[str, list[float]] | None = None, deterministic: bool = True, delay_ms: int = 0, error_on_texts: list[str] | None = None, normalize: bool = True) -> None

Configure the mock embedding provider for tests.

This writes configuration to a port-specific temp file that both the test process and any server process can read.

Parameters:
  • port (int, default: 0 ) –

    Port for test isolation (use live_server.port for browser tests)

  • dimension (int, default: 1024 ) –

    Embedding dimension to generate

  • embeddings (dict[str, list[float]] | None, default: None ) –

    Optional dict mapping exact texts to specific embeddings

  • deterministic (bool, default: True ) –

    If True, generate consistent embeddings based on text hash

  • delay_ms (int, default: 0 ) –

    Artificial delay per embedding (for testing async behavior)

  • error_on_texts (list[str] | None, default: None ) –

    List of texts that should raise errors

  • normalize (bool, default: True ) –

    Whether embeddings should be L2-normalized

Examples:

Basic usage for unit tests

configure_mock_embedding_provider(dimension=384)

Specific embeddings for known texts

configure_mock_embedding_provider( embeddings={ "audit rights": [0.1, 0.2, ...], "data protection": [0.3, 0.4, ...], } )

Simulate slow embedding service

configure_mock_embedding_provider(delay_ms=100)

Test error handling

configure_mock_embedding_provider( error_on_texts=["invalid text that should fail"] )

clear_mock_embedding_config

clear_mock_embedding_config(port: int | None = None) -> None

Clear mock embedding provider configuration.

Parameters:
  • port (int | None, default: None ) –

    Specific port to clear, or None to clear all