ohne_kennung entfernt jedes Feld kennung in jeder Tiefe (villains, profile, reads) beim Anhaengen; die id bleibt die der geloggten Anfrage, ein zweites Fuellen haengt nichts doppelt an. Spec 9/9. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
154 lines
7.6 KiB
Python
154 lines
7.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Spezifikation werkzeuge/livefaelle.py - der Echthand-Regressionssatz.
|
|
|
|
Rein: kleine Fixtures, Schein-Kern (Python-Funktion), Dateien nur in einem Temp-Ordner.
|
|
|
|
python3 tests/livefaelle_spec.py -v
|
|
"""
|
|
import json
|
|
import os
|
|
import shutil
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
|
|
_w = os.path.dirname(os.path.abspath(__file__))
|
|
while _w != "/" and not os.path.isdir(os.path.join(_w, "werkzeuge")):
|
|
_w = os.path.dirname(_w)
|
|
sys.path.insert(0, os.path.join(_w, "werkzeuge"))
|
|
import livefaelle
|
|
|
|
|
|
def hand(hid, netto, aktionen=(), bb=0.02, arm="schalter:OPEN_JAM=1"):
|
|
return {"hand": hid, "netto": netto, "bb": bb, "arm": arm, "raum": "R",
|
|
"aktionen": [{"aktion": a, "betrag": b, "street": s} for s, a, b in aktionen]}
|
|
|
|
|
|
def zug(hid, street, aktion, grund="x"):
|
|
return {"hand": hid, "street": street, "aktion": aktion, "grund": grund}
|
|
|
|
|
|
def anfrage(street="RIVER", to_call=0.5, hero_stack=2.0, gegner_stack=1.0, legal=("FOLD", "CALL")):
|
|
return {"street": street, "toCall": to_call, "heroStack": hero_stack, "pot": 1.0,
|
|
"legalActions": list(legal),
|
|
"heroCards": [{"rank": "ACE", "suit": "HEARTS"}, {"rank": "KING", "suit": "DIAMONDS"}],
|
|
"board": {"FLOP": [{"rank": "THREE", "suit": "HEARTS"}, {"rank": "SEVEN", "suit": "DIAMONDS"},
|
|
{"rank": "FOUR", "suit": "CLUBS"}]},
|
|
"villains": [{"seat": 2, "stack": gegner_stack, "folded": False}]}
|
|
|
|
|
|
def schatten(hid, a, aktion="CALL", pid=5):
|
|
return {"pid": pid, "zeit": "2026-11-15 22:50:00", "zug": f"R|{hid}|{a['street']}|1|0.5|AhKd|()",
|
|
"anfrage": a, "antwort": {"action": aktion, "reason": "EV call +0.4"}}
|
|
|
|
|
|
class Auswahl(unittest.TestCase):
|
|
def test_verlust_schwelle(self):
|
|
hs = [hand("1", -0.60), hand("2", -0.58), hand("3", +5.0)] # -30 BB, -29 BB, +250 BB
|
|
aus = livefaelle.auswaehlen(hs, [])
|
|
self.assertEqual([h["hand"] for h, _, _ in aus], ["1"])
|
|
self.assertEqual(aus[0][1], "Verlust -30.0 BB")
|
|
|
|
def test_allin_call_aus_haende(self):
|
|
# haende bucht den Call, der Hero all-in bringt, als ALL_IN; die Entscheidung war CALL
|
|
h = hand("7", +1.0, [("FLOP", "BET", 0.1), ("FLOP", "ALL_IN", 1.9)])
|
|
zs = [zug("7", "FLOP", "RAISE"), zug("7", "FLOP", "CALL"), zug("7", "PREFLOP", "FOLD", "Übersetzung setzt aus")]
|
|
self.assertTrue(livefaelle.allin_call(h, zs))
|
|
# eigener Shove (letzte Entscheidung RAISE) ist kein Call
|
|
self.assertFalse(livefaelle.allin_call(h, zs[:1]))
|
|
|
|
def test_allin_call_gegner_gedeckt(self):
|
|
# Gegner steht all-in (Stack 0), Hero deckt ihn: im Log nur CALL
|
|
h = hand("8", -2.26, [("RIVER", "CALL", 0.56)])
|
|
self.assertTrue(livefaelle.allin_call(h, [], [(anfrage(gegner_stack=0.0), "CALL")]))
|
|
self.assertFalse(livefaelle.allin_call(h, [], [(anfrage(gegner_stack=0.0), "FOLD")]))
|
|
self.assertFalse(livefaelle.allin_call(h, [], [(anfrage(gegner_stack=1.0), "CALL")]))
|
|
self.assertTrue(livefaelle.ist_allin_spot(anfrage(to_call=2.0, hero_stack=1.5))) # Hero geht all-in
|
|
|
|
def test_linie(self):
|
|
h = hand("1", 0, [("PREFLOP", "CALL", 0.05), ("FLOP", "BET", 0.08), ("FLOP", "ALL_IN", 1.9), ("TURN", "CHECK", None)])
|
|
self.assertEqual(livefaelle.linie(h), "PREFLOP CALL 0.05 | FLOP BET 0.08, ALL_IN 1.9 | TURN CHECK")
|
|
|
|
|
|
class Faelle(unittest.TestCase):
|
|
def setUp(self):
|
|
self.d = tempfile.mkdtemp()
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.d, ignore_errors=True)
|
|
|
|
def test_faelle_aus_schatten_dedupliziert(self):
|
|
a = anfrage()
|
|
aus = livefaelle.auswaehlen([hand("9", -2.0)], [])
|
|
s = [schatten("9", a), schatten("9", a), # Wiederholung derselben Anfrage
|
|
schatten("9", {"legalActions": [], "_unklar": "Knopf unbekannt", "street": "PREFLOP"}), # Enthaltung
|
|
schatten("9", anfrage("TURN"), pid=6), # anderer Lauf
|
|
schatten("10", anfrage())] # andere Hand
|
|
fs = livefaelle.faelle(aus, s, pid=5)
|
|
self.assertEqual(len(fs), 1)
|
|
f = fs[0]
|
|
self.assertEqual((f["hand"], f["street"], f["aktion"], f["netto_bb"]), ("9", "RIVER", "CALL", -100.0))
|
|
self.assertEqual(f["spot"], "AhKd | 3h7d4c | RIVER toCall 0.5 Pot 1.0")
|
|
self.assertEqual(f["grund"], "EV call +0.4")
|
|
p = os.path.join(self.d, "livefaelle.jsonl")
|
|
self.assertEqual(livefaelle.anhaengen(p, fs), 1)
|
|
self.assertEqual(livefaelle.anhaengen(p, fs), 0) # zweiter Lauf: nichts doppelt
|
|
with open(p, encoding="utf-8") as fh:
|
|
self.assertEqual(len(fh.readlines()), 1)
|
|
|
|
def test_kennungen_werden_nie_geschrieben(self):
|
|
a = anfrage()
|
|
a["villains"][0]["kennung"] = "spieler-123"
|
|
a["villains"][0]["profile"] = {"vpip": 0.3, "hands": 20, "kennung": "spieler-123",
|
|
"reads": [{"kennung": "x", "n": 1}]}
|
|
a["kennung"] = "oben"
|
|
fs = livefaelle.faelle(livefaelle.auswaehlen([hand("12", -2.0)], []), [schatten("12", a)], pid=5)
|
|
p = os.path.join(self.d, "livefaelle.jsonl")
|
|
self.assertEqual(livefaelle.anhaengen(p, fs), 1)
|
|
with open(p, encoding="utf-8") as fh:
|
|
roh = fh.read()
|
|
self.assertNotIn("kennung", roh)
|
|
self.assertNotIn("spieler-123", roh)
|
|
f = json.loads(roh)
|
|
self.assertEqual(f["anfrage"]["villains"][0]["profile"], {"vpip": 0.3, "hands": 20, "reads": [{"n": 1}]})
|
|
self.assertEqual(f["anfrage"]["villains"][0]["stack"], 1.0) # alles andere bleibt
|
|
self.assertEqual(livefaelle.anhaengen(p, fs), 0) # id stabil: nichts doppelt
|
|
self.assertIn("kennung", fs[0]["anfrage"]["villains"][0]) # die Eingabe bleibt unberuehrt
|
|
|
|
def test_rueckfall_auf_spot_der_zugzeile(self):
|
|
z = dict(zug("11", "RIVER", "CALL", "EV call"), spot=anfrage())
|
|
aus = livefaelle.auswaehlen([hand("11", -1.0)], [z])
|
|
fs = livefaelle.faelle(aus, [], pid=5)
|
|
self.assertEqual(len(fs), 1)
|
|
self.assertEqual(fs[0]["quelle"], "zug")
|
|
|
|
|
|
class Kandidat(unittest.TestCase):
|
|
def test_arm_lesen_und_env(self):
|
|
arm = livefaelle.arm_lesen("OPEN_JAM=aus; RERAISE_TIEFE=2;MDF_UNREAD=")
|
|
self.assertEqual(arm, {"OPEN_JAM": None, "RERAISE_TIEFE": "2", "MDF_UNREAD": None})
|
|
live = {"OPEN_JAM": "1", "MDF_UNREAD": "0", "TURN_SPIEL": "grund"}
|
|
self.assertEqual(livefaelle.kandidat_env(live, arm), {"TURN_SPIEL": "grund", "RERAISE_TIEFE": "2"})
|
|
|
|
def test_vergleich_listet_geaenderte(self):
|
|
fs = [{"hand": "1", "netto_bb": -97.5, "street": "FLOP", "spot": "s1", "aktion": "CALL", "anfrage": {"n": 1}},
|
|
{"hand": "2", "netto_bb": -40.0, "street": "RIVER", "spot": "s2", "aktion": "CALL", "anfrage": {"n": 2}}]
|
|
aufrufe = []
|
|
|
|
def kern(anfr, env):
|
|
aufrufe.append(env)
|
|
# Kandidat (VORSICHT=1) foldet Fall 1, sonst CALL
|
|
return [{"action": "FOLD" if env.get("VORSICHT") and a["n"] == 1 else "CALL", "reason": "r"} for a in anfr]
|
|
r = livefaelle.vergleichen(fs, kern, kern, {}, {"VORSICHT": "1"})
|
|
self.assertEqual((r["n"], r["haende"], r["live_treu"]), (2, 2, 2))
|
|
self.assertEqual([(g["hand"], g["vorher"], g["nachher"]) for g in r["geaendert"]], [("1", "CALL", "FOLD")])
|
|
self.assertEqual(aufrufe, [{}, {"VORSICHT": "1"}])
|
|
t = livefaelle.vergleich_text(r, "VORSICHT=1")
|
|
self.assertIn("1 von 2 Entscheidungen", t)
|
|
self.assertIn("kein Torkriterium", t)
|
|
self.assertIn("CALL -> FOLD", t)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|