"""End-to-end spec of score.py on a synthetic spot (python3 -m unittest score/test_score.py): outputs, SHA guard, never overwriting, the Tor-7 file format.""" import hashlib, json, os, subprocess, sys, tempfile, unittest HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) from test_messung import REF SID = "FLOP|srp|eroeffnen#00" STRATUM = "FLOP|srp|eroeffnen" def ref_file(): r = json.loads(json.dumps(REF)) r.update({"board": "2sKdAd", "in_round": [], "ref": {"iters": 100, "exploit_chips": 10.0}, "tag": SID}) return r class ScoreEndToEnd(unittest.TestCase): def setUp(self): self.d = tempfile.mkdtemp() for sub in ("refs", "pi", "ev"): os.mkdir(os.path.join(self.d, sub)) name = "".join(c if c.isalnum() else "_" for c in SID) json.dump(ref_file(), open(os.path.join(self.d, "refs", name + ".json"), "w")) # version "cand": plays the best action with each hand; version "base": always checks cand = {"KsAs": [0, 1, 0, 0], "2d7c": [1, 0, 0, 0]} base = {"KsAs": [1, 0, 0, 0], "2d7c": [1, 0, 0, 0]} for f, prof in (("cand", cand), ("base", base)): json.dump({"profiles": {f: prof}, "meta": {"modus": "knoten", "abstand_mittel": 0.0, "fehlend_w": 0.0, "sekunden": 1}}, open(os.path.join(self.d, "pi", name + "." + f + ".json"), "w")) # value of the reference mixture = 0.25*30 + 0.75*(-5) = 3.75 ; perfect-opponent values (chips) for each profile json.dump({"perfect": [{"name": "gg", "ev_chips": 3.0}, {"name": "station", "ev_chips": -20.0}, {"name": "cand", "ev_chips": 2.5}, {"name": "base", "ev_chips": -2.0}]}, open(os.path.join(self.d, "ev", name + ".json"), "w")) self.spots = os.path.join(self.d, "spots.jsonl") open(self.spots, "w").write(json.dumps({"id": SID, "stratum": STRATUM, "rang": 0}) + "\n") self.sha = hashlib.sha256(open(self.spots, "rb").read()).hexdigest() self.meta = os.path.join(self.d, "meta.json") json.dump({"sha256": self.sha, "je100": {STRATUM: 10.0}, "luecken_je100": {"vorflop": 100.0, "mehrspieler": 5.0}}, open(self.meta, "w")) def run_score(self, out, fassung="cand", *extra): return subprocess.run([sys.executable, os.path.join(HERE, "score.py"), "--fassung", fassung, "--refs", os.path.join(self.d, "refs"), "--pi-dir", os.path.join(self.d, "pi"), "--eval-dir", os.path.join(self.d, "ev"), "--spots", self.spots, "--spotsatz", self.meta, "--exe-sha256", "A" * 64, "--out", out, *extra], capture_output=True, text=True) def test_values_hand_computed(self): out = os.path.join(self.d, "o1") r = self.run_score(out, "cand", "--basis", "base") self.assertEqual(r.returncode, 0, r.stderr) s = json.load(open(os.path.join(out, "score.json"))) # cand plays the best action everywhere: regret 0. base always checks: KsAs loses 20 chips at w=.25 -> 5 chips -> 10 per 100 x 0.05 bb = 0.5 bb/100 self.assertAlmostEqual(s["regret_gg_bb100"], 0.0) # V(cand) = (3.75-2.5)/100*10 = 0.125 ; V0 = (3.75+20)/100*10 = 2.375 ; S = 100*(1-0.125/2.375) self.assertAlmostEqual(s["V_bb100"], 0.125) self.assertAlmostEqual(s["V0_bb100"], 2.375) self.assertAlmostEqual(s["S"], 100 * (1 - 0.125 / 2.375)) self.assertTrue(s["gepaart"]) self.assertAlmostEqual(s["differenz_regret_bb100"], -0.5) self.assertIn("Gemessen wird die Entscheidung am Spot mit exakter Fortsetzung, nicht die volle Hand.", open(os.path.join(out, "score.txt")).read()) def test_tor7_file_format(self): out = os.path.join(self.d, "o2") self.assertEqual(self.run_score(out, "cand", "--basis", "base").returncode, 0) b = json.load(open(os.path.join(out, "ausbeute_beleg.json"))) self.assertEqual(set(b) - {"hinweis_modell"}, {"exe_sha256", "modell", "score"}) self.assertEqual(set(b["score"]) - {"hinweis"}, {"spot_satz_sha256", "n_spots", "pool_diff_bb100", "pool_intervall", "leitplanke_diff_bb100", "leitplanke_intervall"}) self.assertEqual(b["score"]["spot_satz_sha256"], self.sha) self.assertEqual(b["exe_sha256"], "A" * 64) self.assertIsNone(b["modell"]) # no model yet: null with a note, never invented def test_a_changed_spot_set_is_refused(self): open(self.spots, "a").write("\n") r = self.run_score(os.path.join(self.d, "o3")) self.assertNotEqual(r.returncode, 0) self.assertIn("frozen", r.stderr + r.stdout) def test_never_overwrites(self): out = os.path.join(self.d, "o4") self.assertEqual(self.run_score(out).returncode, 0) self.assertNotEqual(self.run_score(out).returncode, 0) if __name__ == "__main__": unittest.main()