citeformer.backends.mock

Scripted backend used in unit tests.

MockBackend emits predetermined responses given a prompt; unknown prompts fall back to a deterministic echo that respects the source-id range. It lets the orchestration layer be tested end-to-end without loading a real model.

Module Contents

Classes

MockBackend

Scripted backend for tests.

API

class citeformer.backends.mock.MockBackend(responses: dict[str, str] | None = None)

Bases: citeformer.backends.base.Backend

Scripted backend for tests.

Construct with a responses mapping from prompt to pre-canned output. Any prompt not in the mapping gets a deterministic echo of the form "Mock response for: <prompt> [1]." — which satisfies the REQUIRED policy trivially when at least one source is in scope. The marker in the fallback honours the marker_style option so tests for non-bracket styles see the right delimiters in the echoed output.

Attributes: responses: Pre-canned responses keyed by exact prompt string.

Initialization

Construct a MockBackend.

Args: responses: Optional mapping from prompt to canned response.

generate(prompt: str, sources: list[citeformer.core.Source], policy: citeformer.core.Policy, **options: Any) str

Return a scripted response or a deterministic fallback.

The fallback echo emits a marker (shape determined by the marker_style option, default [1]) when sources are in scope, so that downstream parsers and verifiers see at least one citation. When there are no sources, it returns the echo with no marker regardless of policy — the caller is responsible for not invoking generate() with an empty source list under REQUIRED.

stream(prompt: str, sources: list[citeformer.core.Source], policy: citeformer.core.Policy, **options: Any) collections.abc.Iterator[str]

Yield the mock response in small chunks to exercise stream consumers.

Splits the scripted response at word boundaries so downstream streaming tests see more than one chunk without relying on tokenizer behavior.