| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import pytest
- from cq2_sdk.lua_bridge.runtime import LuaRuntime
- from cq2_sdk.lua_bridge.loader import LuaLoader
- from cq2_sdk.lua_bridge.bindings import Cq2Lua
- @pytest.fixture
- def cq2lua_with_fakes(tmp_path):
- """Set up fake Lua modules and return a Cq2Lua pointing at them."""
- (tmp_path / "encode_util.lua").write_text(
- """
- local M = {}
- -- Note: avoid string.gsub (returns tuple of (result, count)).
- -- Use explicit loop so the function returns a single string.
- M.Encode6BitBuf = function(s)
- local r = ""
- for i = 1, #s do
- r = r .. string.format("%02x", s:byte(i))
- end
- return r
- end
- M.Decode6BitBuf = function(s) return s end
- _G.encode_util = M
- return M
- """
- )
- (tmp_path / "crypto_util.lua").write_text(
- """
- local M = {}
- M.md5 = function(s) return s .. "_md5" end
- M.sha1 = function(s) return s .. "_sha1" end
- M.aes_encrypt = function(p, k) return p .. "_aes(" .. k .. ")" end
- M.aes_decrypt = function(c, k) return c end
- _G.crypto_util = M
- return M
- """
- )
- rt = LuaRuntime(lua_path=str(tmp_path))
- loader = LuaLoader(rt, str(tmp_path))
- return Cq2Lua(rt, loader)
- def test_encode6bit(cq2lua_with_fakes):
- assert cq2lua_with_fakes.encode6bit("abc") == "616263"
- def test_md5(cq2lua_with_fakes):
- assert cq2lua_with_fakes.md5("hello") == "hello_md5"
- def test_sha1(cq2lua_with_fakes):
- assert cq2lua_with_fakes.sha1("x") == "x_sha1"
- def test_aes_encrypt(cq2lua_with_fakes):
- assert cq2lua_with_fakes.aes_encrypt("p", "k") == "p_aes(k)"
- def test_lazy_load(cq2lua_with_fakes):
- # Should work even without pre-loading
- assert cq2lua_with_fakes.sha1("y") == "y_sha1"
- def test_missing_binding_raises_attributeerror(cq2lua_with_fakes):
- with pytest.raises(AttributeError):
- cq2lua_with_fakes.nonexistent_method()
|