-- tests/test_nav_tree.lua
dofile("wow_mock.lua")
dofile("../Core/HDGR_SchemeConstants.lua")
dofile("../Core/HDGR_Constants.lua")
dofile("../Core/HDGR_Format.lua")

local function assertTrue(v, label) if not v then error(label, 2) end end

local NT = HDG.Constants.NAV_TREE
assertTrue(NT, "NAV_TREE present")

local validViews = {}
for _, t in ipairs(HDG.Constants.TABS) do validViews[t.view] = true end

local VALID_KINDS = {
    home = true, divider = true, header = true,
    parent = true, config = true, launcher = true,
}

local sawHome, sawCrafting = false, false
for _, node in ipairs(NT) do
    assertTrue(node.kind, "node has kind")
    assertTrue(VALID_KINDS[node.kind], "node.kind is a known nav kind: " .. tostring(node.kind))
    if node.kind == "home" then sawHome = true; assertTrue(validViews[node.view], "home view exists in TABS") end
    if node.kind == "config" then assertTrue(validViews[node.view], "config view exists in TABS") end
    if node.kind == "parent" then
        if node.noNavigate then
            -- No-view group (e.g. Tools): collapse state keyed by collapseKey, label doesn't navigate.
            assertTrue(node.collapseKey, "noNavigate parent has a collapseKey: " .. tostring(node.label))
        else
            assertTrue(validViews[node.view], "parent view exists in TABS: " .. tostring(node.view))
        end
        if node.label == "Crafting" then
            sawCrafting = true
            assertTrue(node.children and #node.children == 4, "Crafting has 4 children")
        end
        for _, child in ipairs(node.children or {}) do
            local isMode     = child.activePath ~= nil  -- mode-leaf: action OR transient form
            local isView     = child.view ~= nil
            local isLauncher = child.launcher ~= nil    -- dispatch-only leaf (Shopping/Zone under Tools)
            assertTrue(isMode or isView or isLauncher,
                "child is mode-leaf, view-leaf, or launcher-leaf: " .. tostring(child.label))
            if isMode then
                assertTrue(child.activePath and child.activeValue ~= nil,
                    "mode-leaf has activePath+activeValue: " .. tostring(child.label))
                -- Must have exactly one of: action (dispatch form) or transient (helper form).
                assertTrue((child.action ~= nil) ~= (child.transient ~= nil),
                    "mode-leaf has action XOR transient: " .. tostring(child.label))
            elseif isView then
                assertTrue(validViews[child.view], "view-leaf view exists: " .. tostring(child.view))
            end
        end
    end
    if node.kind == "launcher" then assertTrue(node.action, "launcher has action") end
end
assertTrue(sawHome, "NAV_TREE has a home node")
assertTrue(sawCrafting, "NAV_TREE has the Crafting hub")

print("test_nav_tree.lua passed")
