test_bridge_load.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # tests/test_bridge_load.py
  2. """Task 6: bridge.py — 游戏原版 Lua 加载链(协议定义/framework.init/networkUtil 公共面)。
  3. I-1 (Task5 评审转): gameEnviroment.Inst/SetNetClient + global.gameEnvironment 别名必须实现并测试。
  4. """
  5. from apilot.bridge import bootstrap_runtime, load_game_module
  6. from apilot.stubs.env import EnvData
  7. from apilot.stubs.netclient import NetClientStub
  8. from apilot.stubs.networkctl import NetworkCtlStub
  9. from apilot.stubs.scheduler import Scheduler
  10. def _boot(tmp_path):
  11. nc = NetClientStub()
  12. ctl = NetworkCtlStub(nc)
  13. sched = Scheduler()
  14. return bootstrap_runtime(nc, ctl, sched, EnvData({"uid": "u1"}), str(tmp_path))
  15. def test_netmessagedef_loaded(tmp_path):
  16. """netMessageDef 原样加载, MsgType 全局表含登录与心跳号。"""
  17. lua = _boot(tmp_path)
  18. assert lua.eval("MsgType.MSG_CS_LOGIN_SERVER") == 20
  19. assert lua.eval("MsgType.MSG_CS_HEART_BEAT_KEEP") == 60000
  20. def test_sendtabletoserver_encodes_json(tmp_path):
  21. """SendTableToServer 把 Lua 表走 cjson.encode 后经 networkCtl:SendMsg 发出。"""
  22. lua = _boot(tmp_path)
  23. sent = []
  24. nc_stub_capture = lua.globals()["AP_networkCtl"]
  25. orig = nc_stub_capture.send_msg
  26. def cap(self_, mid, recog, p1, p2, p3, body, ln):
  27. sent.append((mid, body))
  28. nc_stub_capture.send_msg = cap
  29. try:
  30. lua.execute("SendTableToServer(3199, {index=303, act='@chiyao_kaiqi'})")
  31. finally:
  32. nc_stub_capture.send_msg = orig
  33. assert sent[0][0] == 3199
  34. # body 在 ctl 层是 str(stub 内 send_msg 才 encode 成 bytes)
  35. assert "chiyao_kaiqi" in sent[0][1]
  36. assert '"act":"@chiyao_kaiqi"' in sent[0][1]
  37. assert '"index":303' in sent[0][1]
  38. def test_load_framework(tmp_path):
  39. """framework.init 加载, 全局 framework/Facade 表存在。"""
  40. lua = _boot(tmp_path)
  41. load_game_module(lua, "framework.init")
  42. assert lua.eval("type(framework)") == "table" or lua.eval("type(Facade)") == "table"
  43. def test_game_environment_inst_set_get_camelcase_alias(tmp_path):
  44. """I-1 硬性: gameEnviroment:Inst():SetNetClient(nc) 存引用,
  45. global.gameEnvironment:GetNetClient() 返回同一 nc。
  46. """
  47. nc_b = NetClientStub()
  48. lua = _boot(tmp_path)
  49. lua.globals()["_PY_nc_b"] = nc_b
  50. lua.execute("gameEnviroment:Inst():SetNetClient(_PY_nc_b)")
  51. assert lua.eval("type(global.gameEnvironment)") == "table"
  52. got = lua.eval("global.gameEnvironment:GetNetClient()")
  53. assert got is nc_b
  54. # Inst() 返自身: 用 Lua == 比对(Lupa 对同一表返不同 _LuaTable 包装, Python is 不可用)
  55. assert lua.eval("gameEnviroment:Inst() == gameEnviroment") is True
  56. assert lua.eval("global.gameEnvironment == gameEnviroment") is True