diet-guard/diet_guard/tests/test_claude_schema.py
Krzysztof kuhy Rudnicki 6cd3470502 Add the Claude meal estimator
estimate_meal(text, photo) -> Estimate runs Claude through
claude_agent_sdk in an isolated CLAUDE_CONFIG_DIR: a copy of the access
token (no refresh token, no MCP tokens) and the diet-food skill, never
kuhy's ~/.claude. The skill text is the system prompt; the closest curated
and derived food-bank rows ride in the user message; the photo is an image
block; the answer is constrained by a JSON schema and validated again.

Failure paths, all fail closed (an EstimateError code, nothing logged):
- no host login, CLI/API error, bad transport: estimate_failed
- more than 45 s: estimate_timeout
- no object, missing/mistyped field, NaN/inf/negative/absurd number,
  grams 0, blank or long desc: estimate_invalid
- neither text nor photo: ValueError before any model call
- failed token refresh is logged, the call then fails as estimate_failed

Live smoke (scratch HOME, nothing logged): "2 jajka sadzone i kromka
chleba" -> 315 kcal in 4.3 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWZHWP4KZ6x5TcXDBFC
2026-11-17 21:05:41 +01:00

106 lines
2.8 KiB
Python

"""The estimator's output schema: only a sane, complete JSON object passes."""
from __future__ import annotations
import math
from typing import Any
import pytest
from diet_guard._claude_schema import (
SCHEMA,
Estimate,
InvalidEstimateError,
parse_estimate,
)
GOOD: dict[str, Any] = {
"desc": "2 jajka sadzone, kromka chleba",
"grams": 190,
"kcal": 410,
"protein_g": 17.5,
"carbs_g": 22,
"fat_g": 26,
}
def test_valid_object_becomes_an_estimate() -> None:
"""Every required number is carried over, no components by default."""
est = parse_estimate(GOOD)
assert est == Estimate(
desc="2 jajka sadzone, kromka chleba",
grams=190.0,
kcal=410.0,
protein_g=17.5,
carbs_g=22.0,
fat_g=26.0,
components=None,
)
def test_components_are_validated_and_kept() -> None:
"""A component carries the same five numbers plus its name."""
item = {"name": "jajko", "grams": 60, "kcal": 90, "protein_g": 6, "carbs_g": 0.5}
est = parse_estimate({**GOOD, "components": [{**item, "fat_g": 7}]})
assert est.components is not None
assert est.components[0]["name"] == "jajko"
assert est.components[0]["fat_g"] == 7.0
@pytest.mark.parametrize(
"bad",
[
{k: v for k, v in GOOD.items() if k != "kcal"},
{**GOOD, "desc": " "},
{**GOOD, "desc": "x" * 121},
{**GOOD, "desc": 5},
{**GOOD, "grams": 0},
{**GOOD, "grams": 6000},
{**GOOD, "kcal": -1},
{**GOOD, "kcal": 5001},
{**GOOD, "protein_g": math.nan},
{**GOOD, "fat_g": math.inf},
{**GOOD, "carbs_g": "22"},
{**GOOD, "kcal": True},
{**GOOD, "components": "none"},
{**GOOD, "components": [{"name": "x"}]},
{
**GOOD,
"components": [
{
"name": "",
"grams": 1,
"kcal": 1,
"protein_g": 0,
"carbs_g": 0,
"fat_g": 0,
}
],
},
{**GOOD, "components": ["x"]},
],
)
def test_invalid_object_is_refused(bad: dict[str, Any]) -> None:
"""Missing, mistyped, non-finite or absurd values are never logged."""
with pytest.raises(InvalidEstimateError):
parse_estimate(bad)
def test_non_object_is_refused() -> None:
"""A list or string is not an estimate."""
with pytest.raises(InvalidEstimateError):
parse_estimate([GOOD])
def test_schema_requires_exactly_the_contract_fields() -> None:
"""The JSON schema handed to the model names every required field."""
assert set(SCHEMA["required"]) == {
"desc",
"grams",
"kcal",
"protein_g",
"carbs_g",
"fat_g",
}
assert SCHEMA["additionalProperties"] is False