import pytest from cq2_sdk.lua_bridge.runtime import LuaRuntime from cq2_sdk.lua_bridge.loader import LuaLoader, MissingLuaModule @pytest.fixture def fake_lua_path(tmp_path): """Create a small fake Lua module tree in a temp dir.""" (tmp_path / "encode_util.lua").write_text( 'local M = {}\nM.encode6bit = function(s) return s .. "_enc" end\nreturn M\n' ) (tmp_path / "sub").mkdir() (tmp_path / "sub" / "helper.lua").write_text( 'return {hi = function() return "hi" end}\n' ) return str(tmp_path) def test_load_simple_module(fake_lua_path): rt = LuaRuntime(lua_path=fake_lua_path) loader = LuaLoader(rt, fake_lua_path) assert loader.load_module("encode_util") is True def test_load_nested_module(fake_lua_path): rt = LuaRuntime(lua_path=fake_lua_path) loader = LuaLoader(rt, fake_lua_path) assert loader.load_module("sub.helper") is True def test_load_missing_module_returns_false(fake_lua_path): rt = LuaRuntime(lua_path=fake_lua_path) loader = LuaLoader(rt, fake_lua_path) assert loader.load_module("does_not_exist") is False def test_list_modules(fake_lua_path): rt = LuaRuntime(lua_path=fake_lua_path) loader = LuaLoader(rt, fake_lua_path) mods = loader.list_modules() assert "encode_util" in mods assert "sub/helper" in mods def test_verify_required_missing(fake_lua_path): rt = LuaRuntime(lua_path=fake_lua_path) loader = LuaLoader(rt, fake_lua_path) missing = loader.verify_required(["encode_util", "does_not_exist"]) assert "does_not_exist" in missing assert "encode_util" not in missing def test_list_modules_empty_dir(tmp_path): rt = LuaRuntime(lua_path=str(tmp_path)) loader = LuaLoader(rt, str(tmp_path)) assert loader.list_modules() == []