-- 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.

local lu = require('luaunit')
local hw_log = require('hw_utils.hw_log')
local timestamp_table = require('hw_utils.timestamp_table')
local log = require('mc.logging')
local old_os_date = os.date

TestHwUtils = {}

function TestHwUtils:setUp()
    log.error = function(_, ...)
        local msg = string.format(...)
        local log_file = io.open('/dev/shm/debug_hwproxy_restricted_log_for_test', 'a+')
        log_file:write(msg .. '\n')
        log_file:close()
    end
    os.date = function()
        return '111'
    end
end

function TestHwUtils:tearDown()
    os.execute('rm /dev/shm/debug_hwproxy_restricted_log_for_test')
    os.date = old_os_date
end

function TestHwUtils:test_hw_log()
    local result = true
    -- 测试正常调用, 打印10条一模一样的日志
    for _ = 1, 10, 1 do
        hw_log:restricted_log(log.error, log, 'test hw log %s', result)
    end

    local test_log_file = io.open('/dev/shm/debug_hwproxy_restricted_log_for_test')
    lu.assertNotNil(test_log_file)
    -- 只打印一条
    local sum_lines = 0
    for _ in io.lines('/dev/shm/debug_hwproxy_restricted_log_for_test') do
        sum_lines = sum_lines + 1
    end
    lu.assertEquals(sum_lines, 1)

    local line1 = test_log_file:read('L')
    lu.assertEquals(line1, 'test hw log true\n')
    test_log_file:close()
    os.execute('rm /dev/shm/debug_hwproxy_restricted_log_for_test')

    -- 测试不传cb场景, 不打印, 不报错
    local ok, _ = pcall(hw_log.restricted_log, hw_log, nil, 'test hw log %s', result)
    lu.assertIsFalse(ok)
    -- 测试不传内容场景, 不打印, 不报错
    ok, _ = pcall(hw_log.restricted_log, hw_log)
    lu.assertIsFalse(ok)

    -- 测试打印10条不同的日志
    for i = 1, 10, 1 do
        hw_log:restricted_log(log.error, log, 'test hw log %s', i)
    end
    sum_lines = 0
    for _ in io.lines('/dev/shm/debug_hwproxy_restricted_log_for_test') do
        sum_lines = sum_lines + 1
    end
    lu.assertEquals(sum_lines, 10)
    test_log_file = io.open('/dev/shm/debug_hwproxy_restricted_log_for_test')
    lu.assertNotNil(test_log_file)
    for i = 1, 10, 1 do
        local line2 = test_log_file:read('L')
        lu.assertEquals(line2, 'test hw log ' .. i .. '\n')
    end
    test_log_file:close()
end

function TestHwUtils:test_timestamp_table()
    local test_table = timestamp_table.new(10)
    -- 测试添加数据
    test_table:add(os.date(), 'aaa')
    local res = test_table:get_all_result()
    lu.assertEquals(#res, 1)
    lu.assertEquals(res, { { '111', 'aaa' } })
    for i = 2, 10, 1 do
        os.date = function()
            local key = tostring(i)
            return key .. key .. key
        end
        test_table:add(os.date(), 'aaa')
    end
    res = test_table:get_all_result()
    lu.assertEquals(#res, 10)
    lu.assertEquals(res, {
        { '111', 'aaa' },
        { '222', 'aaa' },
        { '333', 'aaa' },
        { '444', 'aaa' },
        { '555', 'aaa' },
        { '666', 'aaa' },
        { '777', 'aaa' },
        { '888', 'aaa' },
        { '999', 'aaa' },
        { '101010', 'aaa' },
    })
    -- 测试第1个插入的数据被删除
    os.date = function()
        return '111111'
    end
    test_table:add(os.date(), '666')
    res = test_table:get_all_result()
    lu.assertEquals(#res, 10)
    lu.assertEquals(res, {
        { '222', 'aaa' },
        { '333', 'aaa' },
        { '444', 'aaa' },
        { '555', 'aaa' },
        { '666', 'aaa' },
        { '777', 'aaa' },
        { '888', 'aaa' },
        { '999', 'aaa' },
        { '101010', 'aaa' },
        { '111111', '666' },
    })
    -- 测试获取最新的值
    res = test_table:get_latest()
    lu.assertEquals(res, '666')
end

function TestHwUtils:test_escape_str()
    local str = 'test, "mock" \ndata'
    local ret = hw_log.escape_str(str)
    lu.assertEquals(ret, '"test, ""mock"" \ndata"')
end