"""record_hand tolerance: recover when the model calls it with log_hand's fields.""" from __future__ import annotations from lyra import tools _GRANULAR = { "position": "UTG", "hole_cards": "9h6h", "board": "8h7h5s 5h Kc", "preflop": "raised to 15, BTN calls", "flop": "bet 25, BTN calls", "turn": "bet 50, BTN raises to 150, call", "river": "check, BTN all in, snap call", "showdown": "BTN shows 55 for quads, hero shows straight flush", "result": 300, "tag": "notable", "lesson": "rare straight flush over quads", } def test_shorthand_from_fields_builds_a_parseable_description(): s = tools._shorthand_from_fields(_GRANULAR) assert "UTG with 9h6h" in s assert "Preflop:" in s and "River:" in s and "Board: 8h7h5s 5h Kc" in s assert "Hero net: 300" in s def test_record_hand_recovers_from_granular_fields(monkeypatch): # The model called record_hand with log_hand's schema (no `shorthand`). The # handler must reconstruct one and pass it to poker.record_hand, not fail empty. seen = {} def fake_record_hand(shorthand, stakes=None, tag=None, lesson=None, backend=None): seen["shorthand"] = shorthand return {"id": 42, "parsed": {"hero_involved": True, "hero_pos": "UTG", "hero_cards": ["9h", "6h"]}, "linked": 0} monkeypatch.setattr(tools.poker, "record_hand", fake_record_hand) out = tools.dispatch("record_hand", _GRANULAR, {}) assert "UTG with 9h6h" in seen["shorthand"] # reconstructed, not empty assert "#42" in out and "couldn't parse" not in out def test_record_hand_still_prefers_explicit_shorthand(monkeypatch): seen = {} def fake_record_hand(shorthand, stakes=None, tag=None, lesson=None, backend=None): seen["shorthand"] = shorthand return {"id": 7, "parsed": {"hero_involved": True, "hero_pos": "BTN", "hero_cards": ["As", "Ks"]}, "linked": 0} monkeypatch.setattr(tools.poker, "record_hand", fake_record_hand) tools.dispatch("record_hand", {"shorthand": "BTN AKs, I open, everyone folds"}, {}) assert seen["shorthand"] == "BTN AKs, I open, everyone folds" # verbatim, not rebuilt def test_record_hand_empty_call_still_fails_gracefully(monkeypatch): monkeypatch.setattr(tools.poker, "record_hand", lambda *a, **k: {"id": None, "parsed": None}) out = tools.dispatch("record_hand", {}, {}) assert "couldn't parse" in out.lower()