#!/usr/bin/env python3 """Spezifikation der Zuschauer-Haende im Gedaechtnis 2.0 (lesen/zuschauer_handsatz.py, lesen/zuschauer_strom.py). Eingang: eine Hand des Zuschauer-Sammlers = der hhDealer-Text (lesen/zuschauer_haende.py: hand_text) samt Datensatz (`datensatz`, gleicher Dateiname mit .jsonl). Der Datensatz allein traegt die Zuege nicht, der Text schon. Ausgang: Handsatz v1 (docs/GEDAECHTNIS.md). Alle Namen und Haende hier sind erfunden. Das Werkzeug haengt nur neue Haende an /strom.jsonl und weigert sich, solange ein Laeufer laeuft (Sperrdatei laeufer.pid). python3 tests/zuschauer_handsatz_spec.py -v """ import json import os 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, "lesen")): _w = os.path.dirname(_w) sys.path.append(os.path.join(_w, "lesen")) import handsatz import zuschauer_handsatz as ZS import zuschauer_strom as ST def schluessel(name): return f"vw-{name}" if name else None RAUM = "Testraum 1" KOPF = "CoinPoker Hand #{h}: Hold'em No Limit (₮0.25/₮0.50{ante}) - 2026/11/24 00:00:09 UTC" VIER = """Table 'Testraum 1' 6-max Seat #6 is the button Seat 1: Anna (₮50.00 in chips) Seat 3: Bert (₮60.00 in chips) Seat 4: Cleo (₮25.00 in chips) Seat 6: Dirk (₮100.00 in chips) Anna: posts small blind ₮0.25 Bert: posts big blind ₮0.50 *** HOLE CARDS *** Cleo: raises ₮1.00 to ₮1.50 Dirk: folds Anna: folds Bert: calls ₮1.00 *** FLOP *** [Ah 7c 2d] Bert: checks Cleo: bets ₮2.00 Bert: calls ₮2.00 *** TURN *** [Ah 7c 2d] [5s] Bert: checks Cleo: checks *** RIVER *** [Ah 7c 2d 5s] [9h] Bert: bets ₮3.00 Cleo: raises ₮5.00 to ₮8.00 Bert: calls ₮5.00 *** SHOW DOWN *** Cleo: shows [As Qd] Bert: shows [Kd Kc] Cleo collected ₮36.00 from pot *** SUMMARY *** Total pot ₮37.50 | Rake ₮1.50 Board [ Ah 7c 2d 5s 9h ] Seat 1: Anna (small blind) folded """ def hand(h="900000000001", text=VIER, ante=""): return KOPF.format(h=h, ante=ante) + "\n" + text def datensatz(h="900000000001", **kw): d = {"hand": h, "raum": RAUM, "zeit": "2026-11-24T00:00:09Z", "bb": 0.5, "sb": 0.25, "ante": 0.0, "spieler": 4, "rit": False, "splash_flagge": False} d.update(kw) return d def satz(text=None, ds=None): return ZS.handsatz_aus_zuschauer(text or hand(), ds or datensatz(), schluessel=schluessel) class Zuege(unittest.TestCase): def test_vollstaendige_hand_in_bb_ab_small_blind(self): s = satz() self.assertEqual((s["v"], s["q"], s["h"], s["bb"], s["sp"]), (1, "live", "Testraum 1|900000000001", 0.5, 4)) self.assertEqual(s["t"], 1795478409) # 2026-11-24T00:00:09Z self.assertEqual([x["id"] for x in s["s"]], ["vw-Anna", "vw-Bert", "vw-Cleo", "vw-Dirk"]) self.assertEqual([x["st"] for x in s["s"]], [100.0, 120.0, 50.0, 200.0]) self.assertEqual(s["bl"], [0.5, 1.0, 0.0, 0.0]) self.assertEqual(s["an"], 0.0) self.assertEqual(s["z"], [[2, 0, "r", 3.0], [3, 0, "f", 0], [0, 0, "f", 0], [1, 0, "c", 3.0], [1, 1, "x", 0], [2, 1, "b", 4.0], [1, 1, "c", 4.0], [1, 2, "x", 0], [2, 2, "x", 0], [1, 3, "b", 6.0], [2, 3, "r", 16.0], [1, 3, "c", 16.0]]) self.assertEqual(s["b"], "Ah7c2d5s9h") def test_gezeigte_karten_je_index(self): self.assertEqual(satz()["k"], {"1": "KdKc", "2": "AsQd"}) def test_keine_gezeigten_karten_leeres_feld(self): t = hand(text=VIER.replace("Cleo: shows [As Qd]\n", "").replace("Bert: shows [Kd Kc]\n", "")) self.assertEqual(satz(t)["k"], {}) def test_heads_up_small_blind_ist_der_knopf(self): t = hand(text="""Table 'Testraum 1' 6-max Seat #2 is the button Seat 2: Anna (₮50.00 in chips) Seat 5: Bert (₮50.00 in chips) Anna: posts small blind ₮0.25 Bert: posts big blind ₮0.50 *** HOLE CARDS *** Anna: folds *** SUMMARY *** Total pot ₮0.75 | Rake ₮0.00 """) s = satz(t, datensatz(spieler=2)) self.assertEqual([x["id"] for x in s["s"]], ["vw-Anna", "vw-Bert"]) self.assertEqual((s["z"], s["b"], s["k"]), ([[0, 0, "f", 0]], "", {})) def test_ante_steht_in_an_nicht_im_einsatz(self): t = hand(text=VIER.replace("Anna: posts small", "Anna: posts the ante ₮0.13\nBert: posts the ante ₮0.13\n" "Cleo: posts the ante ₮0.13\nDirk: posts the ante ₮0.13\nAnna: posts small"), ante=" - Ante ₮0.13 CPCC") s = satz(t, datensatz(ante=0.13)) self.assertEqual(s["an"], 0.26) self.assertEqual(s["z"][0], [2, 0, "r", 3.0]) self.assertEqual(s["bl"], [0.5, 1.0, 0.0, 0.0]) def test_allin_call_weniger_als_der_einsatz_ist_ein_call(self): t = hand(text=VIER.replace("Bert: calls ₮5.00", "Bert: calls ₮2.00 and is all-in")) s = satz(t) self.assertEqual(s["z"][-1], [1, 3, "c", 10.0]) def test_name_mit_doppelpunkt_und_leerzeichen(self): t = hand(text=VIER.replace("Cleo", "Zed: Pro")) s = satz(t) self.assertEqual(s["s"][2]["id"], "vw-Zed: Pro") self.assertEqual(s["z"][0], [2, 0, "r", 3.0]) self.assertEqual(s["k"]["2"], "AsQd") def test_unbekannte_kennung_ist_leer_klarnamen_stehen_nie_im_satz(self): s = ZS.handsatz_aus_zuschauer(hand(), datensatz(), schluessel=lambda n: None) self.assertEqual([x["id"] for x in s["s"]], ["", "", "", ""]) text = json.dumps(satz()) for n in ("Anna", "Bert", "Cleo", "Dirk"): self.assertNotIn(n, text.replace("vw-" + n, "")) def test_nicht_abbildbares_ist_none(self): self.assertIsNone(satz(ds=datensatz(splash_flagge=True))) self.assertIsNone(ZS.handsatz_aus_zuschauer("Muell", datensatz(), schluessel=schluessel)) self.assertIsNone(satz(hand(text=VIER.replace("Anna: posts small blind ₮0.25\n", "")))) self.assertIsNone(satz(ds={"hand": "1"})) # kein Raum def test_run_it_twice_zaehlt_mit_dem_ersten_brett(self): self.assertEqual(satz(ds=datensatz(rit=True))["b"], "Ah7c2d5s9h") def test_satz_ist_json(self): self.assertEqual(json.loads(json.dumps(satz()))["h"], "Testraum 1|900000000001") def stapel(*haende): return "\n\n\n".join(haende) + "\n\n\n" class Werkzeug(unittest.TestCase): def setUp(self): self.t = tempfile.TemporaryDirectory() self.addCleanup(self.t.cleanup) self.ordner = os.path.join(self.t.name, "g2") self.quelle = os.path.join(self.t.name, "2026-11-24_zuschauer_NL50") self.sperre = os.path.join(self.t.name, "laeufer.pid") def schreibe(self, hands, saetze=None, name=None): q = name or self.quelle with open(q + ".txt", "w", encoding="utf-8") as f: f.write(stapel(*(hand(h) for h in hands))) with open(q + ".jsonl", "w", encoding="utf-8") as f: for h in (saetze if saetze is not None else hands): f.write(json.dumps(datensatz(h)) + "\n") return q + ".jsonl" def strom(self): try: with open(os.path.join(self.ordner, "strom.jsonl"), encoding="utf-8") as f: return [json.loads(x) for x in f] except OSError: return None def lauf(self, quellen, **kw): return ST.uebernehme(quellen, self.ordner, schluessel=schluessel, sperren=[self.sperre], **kw) def test_haengt_neue_haende_an_und_zaehlt(self): q = self.schreibe(["1", "2", "3"]) r = self.lauf([q]) self.assertEqual((r["neu"], r["doppelt"], r["ohne"]), (3, 0, 0)) self.assertEqual([x["h"] for x in self.strom()], [f"{RAUM}|{i}" for i in "123"]) def test_zweiter_lauf_haengt_nichts_an(self): q = self.schreibe(["1", "2"]) self.lauf([q]) r = self.lauf([q]) self.assertEqual((r["neu"], r["doppelt"]), (0, 2)) self.assertEqual(len(self.strom()), 2) def test_hand_die_der_laeufer_schon_geschrieben_hat_faellt_weg(self): os.makedirs(self.ordner) handsatz.anhaengen(self.ordner, {"v": 1, "q": "live", "h": f"{RAUM}|2"}) r = self.lauf([self.schreibe(["1", "2"])]) self.assertEqual((r["neu"], r["doppelt"]), (1, 1)) self.assertEqual([x["h"] for x in self.strom()], [f"{RAUM}|2", f"{RAUM}|1"]) def test_archiv_hand_mit_nackter_nummer_im_strom_faellt_weg(self): os.makedirs(self.ordner) with open(os.path.join(self.ordner, "strom.jsonl"), "w", encoding="utf-8") as f: f.write(json.dumps({"v": 1, "q": "anfrage", "h": "2"}) + "\n") r = self.lauf([self.schreibe(["1", "2"])]) self.assertEqual((r["neu"], r["doppelt"]), (1, 1)) self.assertEqual([x["h"] for x in self.strom()], ["2", f"{RAUM}|1"]) def test_hand_ohne_datensatz_und_kaputte_hand_werden_gezaehlt_nicht_geschrieben(self): q = self.schreibe(["1", "2"], saetze=["1"]) r = self.lauf([q]) self.assertEqual((r["neu"], r["ohne"]), (1, 1)) def test_mehrere_quellen_dieselbe_hand_nur_einmal(self): a = self.schreibe(["1", "2"]) b = self.schreibe(["2", "3"], name=os.path.join(self.t.name, "2026-11-25_zuschauer_NL50")) r = self.lauf([a, b]) self.assertEqual((r["neu"], r["doppelt"]), (3, 1)) def test_weigert_sich_solange_der_laeufer_laeuft(self): q = self.schreibe(["1"]) with open(self.sperre, "w") as f: f.write(str(os.getpid())) # ein lebender Prozess with self.assertRaises(ST.LaeuferLaeuft): self.lauf([q]) self.assertIsNone(self.strom()) self.assertFalse(os.path.exists(self.ordner)) def test_json_sperre_und_tote_pid_blockieren_wie_im_sammler(self): q = self.schreibe(["1"]) with open(self.sperre, "w") as f: json.dump({"pid": os.getpid()}, f) with self.assertRaises(ST.LaeuferLaeuft): self.lauf([q]) with open(self.sperre, "w") as f: f.write("999999999") # gibt es nicht self.assertEqual(self.lauf([q])["neu"], 1) def test_ohne_sperrdatei_laeuft_es(self): self.assertEqual(self.lauf([self.schreibe(["1"])])["neu"], 1) def test_eigene_standard_sperren_nennen_scratch_laeufer_pid(self): self.assertTrue(any(p.endswith(os.path.join("scratch", "laeufer.pid")) for p in ST.standard_sperren())) def test_streng_ascii_im_strom(self): os.makedirs(self.ordner) q = self.schreibe(["1"]) with open(q[:-6] + ".txt", "w", encoding="utf-8") as f: f.write(hand("1").replace(RAUM, "Räum") + "\n\n\n") with open(q, "w", encoding="utf-8") as f: f.write(json.dumps(datensatz("1", raum="Räum"), ensure_ascii=False) + "\n") self.lauf([q]) with open(os.path.join(self.ordner, "strom.jsonl"), "rb") as f: f.read().decode("ascii") if __name__ == "__main__": unittest.main()