/*
 * Copyright (c) 2026 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <gtest/gtest.h>

#include <lua.hpp>

#include <cstring>
#include <string>
#include <vector>

#include <internal/chip/chip_base/chip.h>
#include <internal/chip/chip_base/lua_chip_binding.h>
#include <internal/hasdef.h>

namespace {

// 可控的 chip 桩:记录每个操作入参并按预设返回,避免依赖底层总线
class MockChip final : public dev::chip_base {
public:
    // chip_base 6 个读写操作全部覆写
    int32_t bit_read(uint32_t offset, uint8_t length, uint32_t mask, std::vector<uint8_t>& in_buffer,
                     std::vector<uint8_t>& out_buffer, std::string& error) override
    {
        ++m_bit_read_calls;
        last_offset = offset;
        last_mask   = mask;
        last_length = length;
        out_buffer  = read_payload;
        error       = error_text;
        return result;
    }

    int32_t bit_write(uint32_t offset, uint8_t length, uint32_t mask, const std::vector<uint8_t>& in_buffer,
                      std::vector<uint8_t>& out_buffer, std::string& error) override
    {
        ++m_bit_write_calls;
        last_offset     = offset;
        last_mask       = mask;
        last_length     = length;
        last_write_data = in_buffer;
        error           = error_text;
        return result;
    }

    int32_t block_read(uint32_t offset, uint32_t length, std::vector<uint8_t>& in_buffer,
                       std::vector<uint8_t>& out_buffer, std::string& error) override
    {
        ++m_block_read_calls;
        last_offset = offset;
        last_length = length;
        out_buffer  = read_payload;
        error       = error_text;
        return result;
    }

    int32_t block_write(uint32_t offset, const std::vector<uint8_t>& in_buffer, std::vector<uint8_t>& out_buffer,
                        std::string& error) override
    {
        ++m_block_write_calls;
        last_offset     = offset;
        last_write_data = in_buffer;
        error           = error_text;
        return result;
    }

    int32_t block_write_read(uint32_t offset, const std::vector<uint8_t>& indata, uint32_t read_length,
                             std::vector<uint8_t>& out_buffer, std::string& error,
                             uint8_t /*protocol_flag*/ = 0) override
    {
        ++m_block_write_read_calls;
        last_offset     = offset;
        last_length     = read_length;
        last_write_data = indata;
        out_buffer      = read_payload;
        error           = error_text;
        return result;
    }

    int32_t combo_block_write_read(uint32_t write_offset, const std::vector<uint8_t>& write_buffer,
                                   uint32_t read_offset, uint32_t read_length, std::vector<uint8_t>& in_buffer,
                                   std::vector<uint8_t>& out_buffer, std::string& error) override
    {
        ++m_combo_calls;
        last_offset     = write_offset;
        last_length     = read_length;
        last_write_data = write_buffer;
        out_buffer      = read_payload;
        error           = error_text;
        return result;
    }

    int32_t              result = HAS_OK;
    std::string          error_text;
    std::vector<uint8_t> read_payload;
    std::vector<uint8_t> last_write_data;
    uint32_t             last_offset              = 0;
    uint32_t             last_mask                = 0;
    uint32_t             last_length              = 0;
    int                  m_bit_read_calls         = 0;
    int                  m_bit_write_calls        = 0;
    int                  m_block_read_calls       = 0;
    int                  m_block_write_calls      = 0;
    int                  m_block_write_read_calls = 0;
    int                  m_combo_calls            = 0;
};

// ---- Lua 栈辅助 ----
static void push_uint(lua_State* L, uint32_t v)
{
    lua_pushinteger(L, static_cast<lua_Integer>(v));
}

// 压入带 chip_base_mt 元表的 chip userdata(仿 lua_engine::register_chip_binding)
static void push_chip_userdata(lua_State* L, dev::chip_base* chip)
{
    auto** ud = static_cast<dev::chip_base**>(lua_newuserdata(L, sizeof(dev::chip_base*)));
    *ud       = chip;
    luaL_newmetatable(L, dev::CHIP_PLUGIN_METATABLE);
    lua_setmetatable(L, -2);
}

// 压入带 driver 字段的 chip 表(get_chip 的第二种调用方式)
static void push_chip_table(lua_State* L, dev::chip_base* chip)
{
    lua_newtable(L);
    push_chip_userdata(L, chip);
    lua_setfield(L, -2, "driver");
}

// 压入字节串参数
static void push_bytes(lua_State* L, const std::vector<uint8_t>& data)
{
    lua_pushlstring(L, reinterpret_cast<const char*>(data.data()), data.size());
}

// 压入字节表参数
static void push_byte_table(lua_State* L, const std::vector<uint8_t>& data)
{
    lua_createtable(L, static_cast<int>(data.size()), 0);
    for (size_t i = 0; i < data.size(); ++i) {
        lua_pushinteger(L, static_cast<lua_Integer>(data[i]));
        lua_rawseti(L, -2, static_cast<lua_Integer>(i + 1));
    }
}

// 取栈顶字符串
static std::string top_string(lua_State* L, int index = -1)
{
    size_t      len = 0;
    const char* s   = lua_tolstring(L, index, &len);
    return s ? std::string(s, len) : std::string();
}

class LuaChipBindingTest : public ::testing::Test {
protected:
    lua_State* m_lua = nullptr;
    MockChip   m_chip;

    void SetUp() override
    {
        m_lua = luaL_newstate();
        ASSERT_NE(m_lua, nullptr);
    }

    void TearDown() override
    {
        if (m_lua != nullptr) {
            lua_close(m_lua);
        }
    }
};

// ================= bit_read =================
TEST_F(LuaChipBindingTest, bit_read_success_returns_bytes)
{
    m_chip.read_payload = {0xAB, 0xCD, 0x12, 0x34};

    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x100);
    push_uint(m_lua, 4);
    push_uint(m_lua, 0xF0);

    ASSERT_EQ(lua_pcall(m_lua, 4, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 1);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\xAB\xCD\x12\x34", 4));
    EXPECT_EQ(m_chip.m_bit_read_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x100U);
    EXPECT_EQ(m_chip.last_mask, 0xF0U);
    EXPECT_EQ(m_chip.last_length, 4U);
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, bit_read_error_returns_nil_and_message)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "bit-read-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_uint(m_lua, 1);
    push_uint(m_lua, 1);

    ASSERT_EQ(lua_pcall(m_lua, 4, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 2);
    EXPECT_TRUE(lua_isnil(m_lua, -2));
    EXPECT_EQ(top_string(m_lua, -1), "bit-read-failed");
    lua_pop(m_lua, 2);
}

// ================= get_chip 异常路径 =================
TEST_F(LuaChipBindingTest, get_chip_table_missing_driver_raises)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_read);
    lua_newtable(m_lua); // 无 driver 字段

    ASSERT_NE(lua_pcall(m_lua, 1, LUA_MULTRET, 0), LUA_OK);
    EXPECT_NE(top_string(m_lua, -1).find("missing driver field"), std::string::npos);
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, get_chip_null_userdata_raises)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_read);
    // 正确的元表,但 *ud == nullptr
    auto** ud = static_cast<dev::chip_base**>(lua_newuserdata(m_lua, sizeof(dev::chip_base*)));
    *ud       = nullptr;
    luaL_newmetatable(m_lua, dev::CHIP_PLUGIN_METATABLE);
    lua_setmetatable(m_lua, -2);

    ASSERT_NE(lua_pcall(m_lua, 1, LUA_MULTRET, 0), LUA_OK);
    EXPECT_NE(top_string(m_lua, -1).find("invalid chip object"), std::string::npos);
    lua_pop(m_lua, 1);
}

// ================= bit_write =================
TEST_F(LuaChipBindingTest, bit_write_accepts_string_buffer)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x200);
    push_uint(m_lua, 2);
    push_uint(m_lua, 0xFF);
    push_bytes(m_lua, {0x01, 0x02}); // string 类型

    ASSERT_EQ(lua_pcall(m_lua, 5, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 1);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.m_bit_write_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x200U);
    EXPECT_EQ(m_chip.last_mask, 0xFFU);
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{0x01, 0x02}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, bit_write_accepts_table_buffer)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_uint(m_lua, 3);
    push_uint(m_lua, 0x0F);
    push_byte_table(m_lua, {0x11, 0x22, 0x33}); // table 类型

    ASSERT_EQ(lua_pcall(m_lua, 5, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{0x11, 0x22, 0x33}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, bit_write_number_buffer_yields_empty)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_uint(m_lua, 1);
    push_uint(m_lua, 1);
    push_uint(m_lua, 7); // 非 string/table → 空 buffer

    ASSERT_EQ(lua_pcall(m_lua, 5, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_TRUE(m_chip.last_write_data.empty());
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, bit_write_error_returns_nil_and_message)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "bit-write-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::bit_write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_uint(m_lua, 1);
    push_uint(m_lua, 1);
    push_bytes(m_lua, {0x01});

    ASSERT_EQ(lua_pcall(m_lua, 5, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 2);
    EXPECT_TRUE(lua_isnil(m_lua, -2));
    EXPECT_EQ(top_string(m_lua, -1), "bit-write-failed");
    lua_pop(m_lua, 2);
}

// ================= block_read =================
TEST_F(LuaChipBindingTest, block_read_success_returns_bytes)
{
    m_chip.read_payload = {0xDE, 0xAD};

    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x300);
    push_uint(m_lua, 2);

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_block_read_calls, 1);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\xDE\xAD", 2));
    EXPECT_EQ(m_chip.last_offset, 0x300U);
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, block_read_error_returns_nil_and_message)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "block-read-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0);
    push_uint(m_lua, 1);

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 2);
    EXPECT_TRUE(lua_isnil(m_lua, -2));
    EXPECT_EQ(top_string(m_lua, -1), "block-read-failed");
    lua_pop(m_lua, 2);
}

// ================= block_write =================
TEST_F(LuaChipBindingTest, block_write_success_returns_true)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x400);
    push_bytes(m_lua, {'h', 'i'});

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.m_block_write_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x400U);
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{'h', 'i'}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, block_write_error_returns_nil_and_message)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "block-write-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_bytes(m_lua, {0x00});

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 2);
    EXPECT_TRUE(lua_isnil(m_lua, -2));
    EXPECT_EQ(top_string(m_lua, -1), "block-write-failed");
    lua_pop(m_lua, 2);
}

// ================= block_write_read =================
TEST_F(LuaChipBindingTest, block_write_read_success)
{
    m_chip.read_payload = {0x55, 0x66};

    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_write_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x500);
    push_bytes(m_lua, {0xAA});
    push_uint(m_lua, 2);

    ASSERT_EQ(lua_pcall(m_lua, 4, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_block_write_read_calls, 1);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\x55\x66", 2));
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{0xAA}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, block_write_read_error)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "bwr-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_write_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_bytes(m_lua, {0xAA});
    push_uint(m_lua, 1);

    ASSERT_EQ(lua_pcall(m_lua, 4, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 2);
    EXPECT_TRUE(lua_isnil(m_lua, -2));
    EXPECT_EQ(top_string(m_lua, -1), "bwr-failed");
    lua_pop(m_lua, 2);
}

// ================= combo_block_write_read =================
TEST_F(LuaChipBindingTest, combo_block_write_read_success)
{
    m_chip.read_payload = {0x01, 0x02, 0x03};

    lua_pushcfunction(m_lua, dev::lua_chip_binding::combo_block_write_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x600);
    push_bytes(m_lua, {0xBB});
    push_uint(m_lua, 0x700);
    push_uint(m_lua, 3);

    ASSERT_EQ(lua_pcall(m_lua, 5, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_combo_calls, 1);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\x01\x02\x03", 3));
    EXPECT_EQ(m_chip.last_offset, 0x600U);
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, combo_block_write_read_error)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "combo-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::combo_block_write_read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 1);
    push_bytes(m_lua, {0xBB});
    push_uint(m_lua, 2);
    push_uint(m_lua, 1);

    ASSERT_EQ(lua_pcall(m_lua, 5, LUA_MULTRET, 0), LUA_OK);
    ASSERT_EQ(lua_gettop(m_lua), 2);
    EXPECT_TRUE(lua_isnil(m_lua, -2));
    EXPECT_EQ(top_string(m_lua, -1), "combo-failed");
    lua_pop(m_lua, 2);
}

// ================= get_chip 的 table(driver) 调用方式 =================
TEST_F(LuaChipBindingTest, chip_table_driver_dispatch)
{
    m_chip.read_payload = {0x77};

    // chip 表(含 driver)+ 位置参数 → block_read
    lua_pushcfunction(m_lua, dev::lua_chip_binding::block_read);
    push_chip_table(m_lua, &m_chip);
    push_uint(m_lua, 0x10);
    push_uint(m_lua, 1);

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_block_read_calls, 1);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\x77", 1));
    lua_pop(m_lua, 1);
}

// ================= read() 封装 =================
TEST_F(LuaChipBindingTest, read_positional_block_success)
{
    m_chip.read_payload = {0x01, 0x02, 0x03, 0x04};

    lua_pushcfunction(m_lua, dev::lua_chip_binding::read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x10);
    push_uint(m_lua, 4);

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_block_read_calls, 1);
    EXPECT_EQ(m_chip.m_bit_read_calls, 0);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\x01\x02\x03\x04", 4));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, read_table_bit_access)
{
    m_chip.read_payload = {0x80};

    // read{offset=, length=, mask=, type=0} → bit_read
    lua_pushcfunction(m_lua, dev::lua_chip_binding::read);
    push_chip_userdata(m_lua, &m_chip);
    lua_newtable(m_lua);
    lua_pushliteral(m_lua, "offset");
    push_uint(m_lua, 0x20);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "length");
    push_uint(m_lua, 1);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "mask");
    push_uint(m_lua, 0xFF);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "type");
    push_uint(m_lua, 0); // BIT_ACCESS_TYPE
    lua_rawset(m_lua, -3);

    ASSERT_EQ(lua_pcall(m_lua, 2, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_bit_read_calls, 1);
    EXPECT_EQ(m_chip.m_block_read_calls, 0);
    EXPECT_EQ(m_chip.last_mask, 0xFFU);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\x80", 1));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, read_table_partial_fields_use_defaults)
{
    m_chip.read_payload = {0x0A};

    // read{offset=} 其它字段缺省 → block_read(offset, length=0)
    lua_pushcfunction(m_lua, dev::lua_chip_binding::read);
    push_chip_userdata(m_lua, &m_chip);
    lua_newtable(m_lua);
    lua_pushliteral(m_lua, "offset");
    push_uint(m_lua, 0x30);
    lua_rawset(m_lua, -3);

    ASSERT_EQ(lua_pcall(m_lua, 2, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_block_read_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x30U);
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, read_table_input_with_chip_table_arg)
{
    m_chip.read_payload = {0x42};

    // chip 表 + read{...} 输入表,同时覆盖 get_chip table 分支与 read table 分支
    lua_pushcfunction(m_lua, dev::lua_chip_binding::read);
    push_chip_table(m_lua, &m_chip);
    lua_newtable(m_lua);
    lua_pushliteral(m_lua, "offset");
    push_uint(m_lua, 0x40);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "length");
    push_uint(m_lua, 1);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "type");
    push_uint(m_lua, 1);
    lua_rawset(m_lua, -3);

    ASSERT_EQ(lua_pcall(m_lua, 2, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(m_chip.m_block_read_calls, 1);
    EXPECT_EQ(top_string(m_lua, -1), std::string("\x42", 1));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, read_error_raises_lua_error)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "read-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::read);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x10);
    push_uint(m_lua, 1);

    ASSERT_NE(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(top_string(m_lua, -1), "read-failed");
    lua_pop(m_lua, 1);
}

// ================= write() 封装 =================
TEST_F(LuaChipBindingTest, write_positional_block_success)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x50);
    push_bytes(m_lua, {'a', 'b', 'c'}); // 位置参数 data

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.m_block_write_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x50U);
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{'a', 'b', 'c'}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, write_table_data_field_block)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::write);
    push_chip_userdata(m_lua, &m_chip);
    lua_newtable(m_lua);
    lua_pushliteral(m_lua, "offset");
    push_uint(m_lua, 0x60);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "data");
    push_bytes(m_lua, {0x11, 0x22});
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "type");
    push_uint(m_lua, 1);
    lua_rawset(m_lua, -3);

    ASSERT_EQ(lua_pcall(m_lua, 2, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.m_block_write_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x60U);
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{0x11, 0x22}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, write_table_data_in_bit_uses_data_length)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::write);
    push_chip_userdata(m_lua, &m_chip);
    lua_newtable(m_lua);
    lua_pushliteral(m_lua, "offset");
    push_uint(m_lua, 0x70);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "data_in");
    push_bytes(m_lua, {0x01, 0x02});
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "mask");
    push_uint(m_lua, 0xFF);
    lua_rawset(m_lua, -3);
    lua_pushliteral(m_lua, "type");
    push_uint(m_lua, 0); // bit_write,未给 length → 取数据长度
    lua_rawset(m_lua, -3);

    ASSERT_EQ(lua_pcall(m_lua, 2, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.m_bit_write_calls, 1);
    EXPECT_EQ(m_chip.last_mask, 0xFFU);
    EXPECT_EQ(m_chip.last_length, 2U); // 由数据长度推导
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{0x01, 0x02}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, write_table_falls_back_to_extra_arg)
{
    lua_pushcfunction(m_lua, dev::lua_chip_binding::write);
    push_chip_userdata(m_lua, &m_chip);
    lua_newtable(m_lua);
    lua_pushliteral(m_lua, "offset");
    push_uint(m_lua, 0x80);
    lua_rawset(m_lua, -3);         // 无 data/data_in
    push_bytes(m_lua, {'x', 'y'}); // 额外第 3 参数兜底

    ASSERT_EQ(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_TRUE(lua_toboolean(m_lua, -1));
    EXPECT_EQ(m_chip.m_block_write_calls, 1);
    EXPECT_EQ(m_chip.last_offset, 0x80U);
    EXPECT_EQ(m_chip.last_write_data, (std::vector<uint8_t>{'x', 'y'}));
    lua_pop(m_lua, 1);
}

TEST_F(LuaChipBindingTest, write_error_raises_lua_error)
{
    m_chip.result     = HAS_ERR;
    m_chip.error_text = "write-failed";

    lua_pushcfunction(m_lua, dev::lua_chip_binding::write);
    push_chip_userdata(m_lua, &m_chip);
    push_uint(m_lua, 0x10);
    push_bytes(m_lua, {0x00});

    ASSERT_NE(lua_pcall(m_lua, 3, LUA_MULTRET, 0), LUA_OK);
    EXPECT_EQ(top_string(m_lua, -1), "write-failed");
    lua_pop(m_lua, 1);
}

} // namespace