judgevet.testing

Status: draft.

judgevet.testing

Offline fakes of the System One ports for caller tests.

FakeSystemOnePort satisfies judgevet.ports.SystemOnePort and AsyncFakeSystemOnePort satisfies judgevet.ports.AsyncSystemOnePort. Both make no network call. A scripted answer is returned for its question name. Every other typed question receives a seeded answer that passes the domain answer validators. The same seed, state and question give the same answer. A scripted Choice answer whose option is outside its question's criteria raises JevResponseError with status 200, as the HTTP adapters do.

Seeded values are synthetic. They exercise caller code paths; they do not predict what the service would answer. The returned model echoes the model argument.

Each fake takes six keyword-only arguments. Two script the whole call and four match the HTTP adapter arguments of the same name:

Parameters:

Name Type Description Default
usage Usage | None

The Usage every response carries. The default is Usage(), which has no token counts.

required
error BaseException | None

An exception every call raises. The fake records the call in calls first, then raises this instance. The error covers the whole call, because the real adapter never fails one question of a call.

required
spend_cap SpendCap | None

A cap the fake claims one attempt from before each call. A refused claim raises JevBudgetExceededError before the call is recorded. A successful call settles its usage.input_tokens; a failed call settles nothing.

required
audit AuditSink | None

A sink that receives one JudgmentRecord per call, whether it returned, raised or was refused. The record carries no HTTP status on success, because no HTTP response arrived. A sink failure never changes the result.

required
fingerprint_key bytes | None

A caller-held key. With it, each record carries the keyed state_fingerprint the HTTP adapters compute. Without it, the field is None.

required
profile ProviderProfile | None

Opt-in server limits. A breach raises ProviderRequestError before the call is recorded, claimed or audited, as the HTTP adapters do. None checks nothing.

required

Examples:

from judgevet.domain.answers import NoulAnswer
from judgevet.domain.questions import Choice, Noul
from judgevet.testing import FakeSystemOnePort

fake = FakeSystemOnePort(seed=3, answers={"billing": NoulAnswer(noul=0.9)})
response = fake.system_one(
    state="I was charged twice.",
    questions={
        "billing": Noul(instructions="Is this about billing?"),
        "queue": Choice(criteria={"billing": "Money", "technical": "Bugs"}),
    },
    model="jev-1.13.0",
)
assert response.nouls["billing"].noul == 0.9
assert response.choices["queue"].choice in {"billing", "technical"}
assert len(fake.calls) == 1

from judgevet.domain.errors import JevRateLimitError
from judgevet.domain.usage import Usage

metered = FakeSystemOnePort(usage=Usage(input_tokens=12, output_tokens=3))
assert metered.system_one("text", {"q": Noul()}, "m").usage.input_tokens == 12
limited = FakeSystemOnePort(error=JevRateLimitError("slow down", 429))
try:
    limited.system_one("text", {"q": Noul()}, "m")
except JevRateLimitError as error:
    assert error.status_code == 429
else:
    raise AssertionError("the scripted error was not raised")
assert len(limited.calls) == 1

from judgevet.domain.errors import JevBudgetExceededError
from judgevet.domain.spend import SpendCap


class ListSink:
    def __init__(self) -> None:
        self.records = []

    def record(self, record) -> None:
        self.records.append(record)


sink = ListSink()
capped = FakeSystemOnePort(spend_cap=SpendCap(max_attempts=1), audit=sink)
capped.system_one("text", {"q": Noul()}, "m")
try:
    capped.system_one("text", {"q": Noul()}, "m")
except JevBudgetExceededError as error:
    assert error.limit == "attempts"
else:
    raise AssertionError("the spend cap did not refuse the second call")
assert [r.outcome for r in sink.records] == ["success", "error"]
assert len(capped.calls) == 1
See Also

Attributes:

Name Type Description
FakeSystemOnePort type

Synchronous offline fake of SystemOnePort.

AsyncFakeSystemOnePort type

Asynchronous offline fake of AsyncSystemOnePort.

AsyncFakeSystemOnePort

Bases: _FakeCore

Asynchronous offline fake of judgevet.ports.AsyncSystemOnePort.

Attributes:

Name Type Description
seed int

The seed mixed into every generated answer.

answers dict[str, Answer]

Scripted answers keyed by question name.

usage Usage

The usage every response carries.

error BaseException | None

The exception every call raises after it is recorded, or None to answer normally.

spend_cap SpendCap | None

The cap claimed before each call.

audit AuditSink | None

The sink that receives one record per call.

fingerprint_key bytes | None

The key for each record's state fingerprint, or None for no fingerprint.

profile ProviderProfile | None

The limits checked before each call, or None for no check.

calls list[Call]

Each call's (state, questions, model) in order. The questions mapping is copied, so later caller edits do not change the record.

Examples:

import anyio
from judgevet.domain.questions import Noul


async def main() -> float:
    fake = AsyncFakeSystemOnePort(seed=1)
    response = await fake.system_one("text", {"q": Noul()}, "jev-1.13.0")
    return response.nouls["q"].noul


assert 0.0 <= anyio.run(main) <= 1.0
See Also

system_one(state, questions, model) async

Record the call and answer every question without a network call.

Parameters:

Name Type Description Default
state str | dict[str, Any] | list[Any]

The content the caller would judge.

required
questions Mapping[str, Question | Mapping[str, Any]]

Question names mapped to typed or raw questions.

required
model str

The model name, echoed into the response.

required

Returns:

Type Description
SystemOneResponse

A response with one answer per question name and the scripted

SystemOneResponse

usage.

Raises:

Type Description
ProviderRequestError

If the profile refuses the questions.

JevBudgetExceededError

If the spend cap refuses the call.

BaseException

The scripted error, after the call is recorded.

FakeSystemOnePort

Bases: _FakeCore

Synchronous offline fake of judgevet.ports.SystemOnePort.

Attributes:

Name Type Description
seed int

The seed mixed into every generated answer.

answers dict[str, Answer]

Scripted answers keyed by question name.

usage Usage

The usage every response carries.

error BaseException | None

The exception every call raises after it is recorded, or None to answer normally.

spend_cap SpendCap | None

The cap claimed before each call.

audit AuditSink | None

The sink that receives one record per call.

fingerprint_key bytes | None

The key for each record's state fingerprint, or None for no fingerprint.

profile ProviderProfile | None

The limits checked before each call, or None for no check.

calls list[Call]

Each call's (state, questions, model) in order. The questions mapping is copied, so later caller edits do not change the record.

Examples:

from judgevet.domain.questions import Score
from judgevet.ports import SystemOnePort

port: SystemOnePort = FakeSystemOnePort(seed=1)
response = port.system_one(
    "text", {"tone": Score(criteria=["cold", "warm"])}, "jev-1.13.0"
)
assert response.scores["tone"].legend == {0: "cold", 1: "warm"}
See Also

system_one(state, questions, model)

Record the call and answer every question without a network call.

Parameters:

Name Type Description Default
state str | dict[str, Any] | list[Any]

The content the caller would judge.

required
questions Mapping[str, Question | Mapping[str, Any]]

Question names mapped to typed or raw questions.

required
model str

The model name, echoed into the response.

required

Returns:

Type Description
SystemOneResponse

A response with one answer per question name and the scripted

SystemOneResponse

usage.

Raises:

Type Description
ProviderRequestError

If the profile refuses the questions.

JevBudgetExceededError

If the spend cap refuses the call.

BaseException

The scripted error, after the call is recorded.