-- Copyright (c) 2024 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 array_dump_mod = require('array.array_dump')
local common_def = require('common_def')

TestArrayDump = {}

local function make_fp_w()
    local written = {}
    local fp_w = {
        write = function(_, s)
            table.insert(written, s)
        end,
    }
    return fp_w, written
end

-- Covers lines 62-78: dump_info with normal array (Id < 0x8000)
-- Also covers trans_capacity_size line 24 (MB) and line 26 (GB)
-- Also covers lines 68-70 (RefVolumes block) and lines 74-76 (RefDrives block)
-- Also covers line 46 (dump_list drive_id extraction) and line 52 (comma separator)
function TestArrayDump:test_dump_info_normal_array()
    local inst = array_dump_mod.get_instance()
    local fp_w, written = make_fp_w()
    local array = {
        Id = 1,
        UsedSpaceMiB = 500,
        TotalFreeSpaceMiB = 2048,
        RefVolumes = { 'Volume0', 'Volume1' },
        RefDrives = { 'Disk0', 'Disk1' },
    }
    inst:dump_info(fp_w, array)
    lu.assertTrue(#written > 0)
    array_dump_mod.destroy()
end

-- Covers line 22: trans_capacity_size with INVALID_U32 -> 'N/A'
-- Covers line 28: TB range
function TestArrayDump:test_dump_info_invalid_and_tb_capacity()
    local inst = array_dump_mod.get_instance()
    local fp_w, written = make_fp_w()
    local array = {
        Id = 2,
        UsedSpaceMiB = common_def.INVALID_U32,
        TotalFreeSpaceMiB = 1024 * 1024 * 2,
    }
    inst:dump_info(fp_w, array)
    lu.assertTrue(#written > 0)
    array_dump_mod.destroy()
end

-- Covers lines 30, 32: PB range
function TestArrayDump:test_dump_info_pb_capacity()
    local inst = array_dump_mod.get_instance()
    local fp_w, written = make_fp_w()
    local array = {
        Id = 3,
        UsedSpaceMiB = 1024 * 1024 * 1024 * 2,
        TotalFreeSpaceMiB = 1024 * 1024 * 1024 * 3,
    }
    inst:dump_info(fp_w, array)
    lu.assertTrue(#written > 0)
    array_dump_mod.destroy()
end

-- Covers line 39: dump_list with empty list -> 'None'
function TestArrayDump:test_dump_info_empty_ref_volumes()
    local inst = array_dump_mod.get_instance()
    local fp_w, written = make_fp_w()
    local array = {
        Id = 4,
        UsedSpaceMiB = 100,
        TotalFreeSpaceMiB = 200,
        RefVolumes = {},
        RefDrives = nil,
    }
    inst:dump_info(fp_w, array)
    lu.assertTrue(#written > 0)
    array_dump_mod.destroy()
end

-- Covers line 52: dump_list with multiple drives (comma separator)
function TestArrayDump:test_dump_info_multiple_drives()
    local inst = array_dump_mod.get_instance()
    local fp_w, written = make_fp_w()
    local array = {
        Id = 5,
        UsedSpaceMiB = 100,
        TotalFreeSpaceMiB = 200,
        RefVolumes = nil,
        RefDrives = { 'Disk0', 'Disk1', 'Disk2' },
    }
    inst:dump_info(fp_w, array)
    lu.assertTrue(#written > 0)
    array_dump_mod.destroy()
end