test_lua_loader.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import pytest
  2. from cq2_sdk.lua_bridge.runtime import LuaRuntime
  3. from cq2_sdk.lua_bridge.loader import LuaLoader, MissingLuaModule
  4. @pytest.fixture
  5. def fake_lua_path(tmp_path):
  6. """Create a small fake Lua module tree in a temp dir."""
  7. (tmp_path / "encode_util.lua").write_text(
  8. 'local M = {}\nM.encode6bit = function(s) return s .. "_enc" end\nreturn M\n'
  9. )
  10. (tmp_path / "sub").mkdir()
  11. (tmp_path / "sub" / "helper.lua").write_text(
  12. 'return {hi = function() return "hi" end}\n'
  13. )
  14. return str(tmp_path)
  15. def test_load_simple_module(fake_lua_path):
  16. rt = LuaRuntime(lua_path=fake_lua_path)
  17. loader = LuaLoader(rt, fake_lua_path)
  18. assert loader.load_module("encode_util") is True
  19. def test_load_nested_module(fake_lua_path):
  20. rt = LuaRuntime(lua_path=fake_lua_path)
  21. loader = LuaLoader(rt, fake_lua_path)
  22. assert loader.load_module("sub.helper") is True
  23. def test_load_missing_module_returns_false(fake_lua_path):
  24. rt = LuaRuntime(lua_path=fake_lua_path)
  25. loader = LuaLoader(rt, fake_lua_path)
  26. assert loader.load_module("does_not_exist") is False
  27. def test_list_modules(fake_lua_path):
  28. rt = LuaRuntime(lua_path=fake_lua_path)
  29. loader = LuaLoader(rt, fake_lua_path)
  30. mods = loader.list_modules()
  31. assert "encode_util" in mods
  32. assert "sub/helper" in mods
  33. def test_verify_required_missing(fake_lua_path):
  34. rt = LuaRuntime(lua_path=fake_lua_path)
  35. loader = LuaLoader(rt, fake_lua_path)
  36. missing = loader.verify_required(["encode_util", "does_not_exist"])
  37. assert "does_not_exist" in missing
  38. assert "encode_util" not in missing
  39. def test_list_modules_empty_dir(tmp_path):
  40. rt = LuaRuntime(lua_path=str(tmp_path))
  41. loader = LuaLoader(rt, str(tmp_path))
  42. assert loader.list_modules() == []