From 467d705576944d4c734df218f27520f4d4e07f59 Mon Sep 17 00:00:00 2001 From: "agent.develop" Date: Tue, 22 Sep 2026 20:40:27 +0800 Subject: [PATCH] =?UTF-8?q?approve:=20[M11b-2c-T2-b]=20=E4=BA=A7=E5=87=BA?= =?UTF-8?q?=20code=5Ffiles.md=20=E6=96=87=E4=BB=B6=E6=B8=85=E5=8D=95?= =?UTF-8?q?=EF=BC=88=E5=AE=9E=E6=B5=8B=E5=AD=97=E8=8A=82=E6=95=B0+?= =?UTF-8?q?=E8=A1=8C=E5=8F=B7=E9=94=9A=E7=82=B9=EF=BC=8C=E7=A6=81=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D/=E7=A6=81=E8=87=AA=E6=8C=87=E5=B0=BA=E5=AF=B8?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/_m11b2d_scratch.py | 162 +++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 84 deletions(-) diff --git a/tests/_m11b2d_scratch.py b/tests/_m11b2d_scratch.py index ba92bed..2ef9e84 100644 --- a/tests/_m11b2d_scratch.py +++ b/tests/_m11b2d_scratch.py @@ -16,98 +16,92 @@ c._default_clause = patched e = c.RuntimeEngine() c.install(e) -r = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="unit.move", - payload={"dx": 1}, state_updates=[{"entity_id": "e1", "state": {"hp": 2}}], - client_event_id="ev-1", actor_id="u1") -print("R1", r) -print("trace", c.trace_kinds(e)) -print("counts", e.count(c.EVENT_TABLE), e.count(c.STATE_TABLE)) -print("updates", c.update_statements(e)) -print("inserts_event", len(c.insert_statements(e, c.EVENT_TABLE))) -print("broadcasts", len(e.broadcasts), e.broadcasts[0]["channel"] if e.broadcasts else None) -print("read", c.read(tenant_id=7, session_id=22)) -e.seed_state(7, 22, "e9", '{"hp":1}', state_version=3) -r2 = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="unit.move", - payload={"dx": 2}, state_updates=[{"entity_id": "e9", "state": {"hp": 5}}], - client_event_id="ev-2", actor_id="u2", base_version=3) -print("R2", r2) -print("state row", e.query("SELECT * FROM pbl_entity_state WHERE entity_id='e9'")) -print("stmt counts r1/r2", r["stmt_count"], r2["stmt_count"]) +def ts(i): + return "2026-06-01 00:%02d:%02d" % (i // 60, i % 60) + + +def show(tag, r): + print(tag, "keys", sorted(r.keys())) + print(tag, {k: r.get(k) for k in ("ok", "dedup", "event_id", "event_uid", "seq", + "state_rows", "stmt_count", "channel", "cursor", + "sla_breach", "states")}) + + +r1 = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="unit.move", + payload={"dx": 1}, state_updates=[{"entity_id": "e1", "state": {"hp": 2}}], + client_event_id="ev-1", actor_id="u1", occurred_at=ts(1)) +show("R1", r1) + +# dedup: same client_event_id +before = (e.count(c.EVENT_TABLE), e.count(c.STATE_TABLE)) r3 = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="unit.move", - payload={"dx": 2}, state_updates=[{"entity_id": "e9", "state": {"hp": 9}}], - client_event_id="ev-2", actor_id="u2") -print("R3 dedup", r3) -print("counts after dedup", e.count(c.EVENT_TABLE), e.count(c.STATE_TABLE)) -print("trace", c.trace_kinds(e)) -print("broadcasts n", len(e.broadcasts)) + payload={"dx": 2}, state_updates=[{"entity_id": "e1", "state": {"hp": 9}}], + client_event_id="ev-1", actor_id="u2", occurred_at=ts(2)) +show("R3dedup", r3) +print("counts", e.count(c.EVENT_TABLE), e.count(c.STATE_TABLE), before, + "bcast", len(e.broadcasts), "trace", c.trace_kinds(e)) +# conflict try: c.apply(tenant_id=7, world_id=11, session_id=22, event_type="unit.move", - payload={"dx": 3}, state_updates=[{"entity_id": "e9", "state": {"hp": 99}}], - client_event_id="ev-4", base_version=1) + payload={"dx": 9}, state_updates=[{"entity_id": "e1", "state": {"hp": 99}}], + client_event_id="ev-4", occurred_at=ts(4), base_version=1) + print("CONFLICT NOT RAISED") except Exception as exc: - print("CONFLICT", type(exc).__name__, getattr(exc, "code", None), str(exc)[:140]) -print("trace tail", c.trace_kinds(e)[-4:]) -print("counts after conflict", e.count(c.EVENT_TABLE), e.count(c.STATE_TABLE)) -print("state after conflict", e.query("SELECT state_version FROM pbl_entity_state WHERE entity_id='e9'")) + print("CONFLICT", type(exc).__name__, getattr(exc, "code", None), str(exc)[:130]) +print("trace tail", c.trace_kinds(e)[-3:], "counts", e.count(c.EVENT_TABLE), e.count(c.STATE_TABLE)) +print("state now", e.query("SELECT state_version, state_json FROM pbl_entity_state")) -r5 = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="snap", - payload={}, state_updates=[], client_event_id="ev-5", - snapshot={"reason": "reconnect", "entities": {"e9": {"hp": 5}}}) -print("R5", {k: r5[k] for k in ("ok", "seq", "state_rows", "stmt_count", "channel")}) -print("snap count", e.count(c.SNAPSHOT_TABLE)) +# snapshot +r5 = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="snap", payload={}, + state_updates=[], client_event_id="ev-5", occurred_at=ts(5), + snapshot={"reason": "reconnect", "entities": {"e1": {"hp": 5}}}) +show("R5", r5) +print("snap", e.count(c.SNAPSHOT_TABLE)) -try: - c.apply(tenant_id=None, world_id=11, session_id=22, event_type="x") -except Exception as exc: - print("TENANT", type(exc).__name__, getattr(exc, "code", None)) -try: - c.apply(tenant_id=7, world_id=None, session_id=22, event_type="x") -except Exception as exc: - print("PARAM", type(exc).__name__, getattr(exc, "code", None)) -try: - c.apply(tenant_id=7, world_id=11, session_id=22, event_type="x", state_updates="no") -except Exception as exc: - print("PARAM2", type(exc).__name__, getattr(exc, "code", None)) -try: - c.apply(tenant_id=7, world_id=11, session_id=22, event_type="x", - state_updates=[{"state": {"a": 1}}]) -except Exception as exc: - print("PARAM3", type(exc).__name__, getattr(exc, "code", None)) -try: - c.apply(tenant_id=7, world_id=11, session_id=22, event_type="x", - state_updates=[{"entity_id": "e1"}]) -except Exception as exc: - print("PARAM4", type(exc).__name__, getattr(exc, "code", None)) -print("trace after validation", c.trace_kinds(e)[-3:]) +# multi-state updates + seed_state +e.seed_state(7, 22, "e9", '{"hp":1}', state_version=3) +r6 = c.apply(tenant_id=7, world_id=11, session_id=22, event_type="multi", payload={}, + state_updates=[{"entity_id": "e9", "state": {"hp": 7}}, + {"entity_id": "new1", "state": {"x": 1}}], + client_event_id="ev-6", occurred_at=ts(6)) +print("R6", r6["states"], r6["state_rows"], "updates", len(c.update_statements(e))) +print("read e9", c.read(tenant_id=7, session_id=22, entity_ids=["e9"])) +print("event rows", e.query("SELECT id, event_uid, seq, state_version, source, occurred_at FROM pbl_runtime_event ORDER BY id")) -try: - c.build_row(c.EVENT_TABLE, {"no_such_col": 1}) -except Exception as exc: - print("UNKNOWN", getattr(exc, "code", None), str(exc)[:90]) -try: - c.build_row(c.STATE_TABLE, {"tenant_id": "t1"}) -except Exception as exc: - print("MISSING", getattr(exc, "code", None), str(exc)[:140]) -try: - c.build_row(c.STATE_TABLE, {"state_json": "{}", "created_at": "x"}, strict=True, full_row=False) -except Exception as exc: - print("IMMUTABLE", getattr(exc, "code", None), str(exc)[:140]) -try: - c.assert_append_only("UPDATE", c.EVENT_TABLE) -except Exception as exc: - print("APPEND", getattr(exc, "code", None), str(exc)[:90]) -try: - c.assert_append_only("DELETE", "world") -except Exception as exc: - print("BASE", getattr(exc, "code", None), str(exc)[:90]) -print("percentile empty", c.percentile([], 99), "single", c.percentile([5], 99), - "range99", c.percentile(list(range(100)), 99)) -print("ERR codes", c.ERR_STATE_CONFLICT, c.ERR_APPEND_ONLY, c.ERR_UNKNOWN_COLUMN, - c.ERR_MISSING_NOT_NULL, c.ERR_IMMUTABLE_COLUMN, c.ERR_TENANT_MISSING, c.ERR_BAD_PARAM, - c.ERR_BROADCAST_AFTER_COMMIT) -print("tables", c.EVENT_TABLE, c.STATE_TABLE, c.SNAPSHOT_TABLE) +# validation +for label, kw in (("tenant", dict(world_id=11, session_id=22, event_type="x")), + ("param", dict(tenant_id=7, session_id=22, event_type="x")), + ("etype", dict(tenant_id=7, world_id=11, session_id=22)), + ("su", dict(tenant_id=7, world_id=11, session_id=22, event_type="x", state_updates="no")), + ("eid", dict(tenant_id=7, world_id=11, session_id=22, event_type="x", + state_updates=[{"state": {"a": 1}}])), + ("val", dict(tenant_id=7, world_id=11, session_id=22, event_type="x", + state_updates=[{"entity_id": "e1"}]))): + try: + c.apply(**kw) + print(label, "NOT RAISED") + except Exception as exc: + print(label, type(exc).__name__, getattr(exc, "code", None), str(exc)[:60]) +print("trace after validation", c.trace_kinds(e)[-2:], "broadcasts", len(e.broadcasts)) + +# guards +for label, fn in ( + ("unknown", lambda: c.build_row(c.EVENT_TABLE, {"no_such_col": 1})), + ("missing", lambda: c.build_row(c.STATE_TABLE, {"tenant_id": "t1"})), + ("immutable", lambda: c.build_row(c.STATE_TABLE, {"state_json": "{}", "created_at": "x"}, + strict=True, full_row=False)), + ("append_evt", lambda: c.assert_append_only("UPDATE", c.EVENT_TABLE)), + ("append_base", lambda: c.assert_append_only("DELETE", "world")), + ("append_ok", lambda: c.assert_append_only("INSERT", c.EVENT_TABLE)), +): + try: + print(label, "OK", fn()) + except Exception as exc: + print(label, "RAISED", getattr(exc, "code", None), str(exc)[:90]) c.uninstall() -print("OK DONE") + +import os +print("files", os.listdir("/tmp")[:3])