# tests/test_bridge_load.py """Task 6: bridge.py — 游戏原版 Lua 加载链(协议定义/framework.init/networkUtil 公共面)。 I-1 (Task5 评审转): gameEnviroment.Inst/SetNetClient + global.gameEnvironment 别名必须实现并测试。 """ from apilot.bridge import bootstrap_runtime, load_game_module from apilot.stubs.env import EnvData from apilot.stubs.netclient import NetClientStub from apilot.stubs.networkctl import NetworkCtlStub from apilot.stubs.scheduler import Scheduler def _boot(tmp_path): nc = NetClientStub() ctl = NetworkCtlStub(nc) sched = Scheduler() return bootstrap_runtime(nc, ctl, sched, EnvData({"uid": "u1"}), str(tmp_path)) def test_netmessagedef_loaded(tmp_path): """netMessageDef 原样加载, MsgType 全局表含登录与心跳号。""" lua = _boot(tmp_path) assert lua.eval("MsgType.MSG_CS_LOGIN_SERVER") == 20 assert lua.eval("MsgType.MSG_CS_HEART_BEAT_KEEP") == 60000 def test_sendtabletoserver_encodes_json(tmp_path): """SendTableToServer 把 Lua 表走 cjson.encode 后经 networkCtl:SendMsg 发出。""" lua = _boot(tmp_path) sent = [] nc_stub_capture = lua.globals()["AP_networkCtl"] orig = nc_stub_capture.send_msg def cap(self_, mid, recog, p1, p2, p3, body, ln): sent.append((mid, body)) nc_stub_capture.send_msg = cap try: lua.execute("SendTableToServer(3199, {index=303, act='@chiyao_kaiqi'})") finally: nc_stub_capture.send_msg = orig assert sent[0][0] == 3199 # body 在 ctl 层是 str(stub 内 send_msg 才 encode 成 bytes) assert "chiyao_kaiqi" in sent[0][1] assert '"act":"@chiyao_kaiqi"' in sent[0][1] assert '"index":303' in sent[0][1] def test_load_framework(tmp_path): """framework.init 加载, 全局 framework/Facade 表存在。""" lua = _boot(tmp_path) load_game_module(lua, "framework.init") assert lua.eval("type(framework)") == "table" or lua.eval("type(Facade)") == "table" def test_game_environment_inst_set_get_camelcase_alias(tmp_path): """I-1 硬性: gameEnviroment:Inst():SetNetClient(nc) 存引用, global.gameEnvironment:GetNetClient() 返回同一 nc。 """ nc_b = NetClientStub() lua = _boot(tmp_path) lua.globals()["_PY_nc_b"] = nc_b lua.execute("gameEnviroment:Inst():SetNetClient(_PY_nc_b)") assert lua.eval("type(global.gameEnvironment)") == "table" got = lua.eval("global.gameEnvironment:GetNetClient()") assert got is nc_b # Inst() 返自身: 用 Lua == 比对(Lupa 对同一表返不同 _LuaTable 包装, Python is 不可用) assert lua.eval("gameEnviroment:Inst() == gameEnviroment") is True assert lua.eval("global.gameEnvironment == gameEnviroment") is True