-- Palette smoke test (ADR-023).
--
-- Pure-data surface only. Verifies the scheme-invariant color source
-- resolves Constants.EXPANSION_DATA into the same {r,g,b} keyed shape
-- Theme returns, formats |cFFRRGGBB strings correctly, and fails loud
-- on unknown tokens.

dofile("wow_mock.lua")
dofile("../Core/HDGR_SchemeConstants.lua")
dofile("../Core/HDGR_Constants.lua")
dofile("../Core/HDGR_Format.lua")
dofile("../UI/HDGR_Palette.lua")

local function assertEq(actual, expected, label)
    if actual ~= expected then
        error(label .. ": expected " .. tostring(expected) .. ", got " .. tostring(actual), 2)
    end
end
local function assertTrue(v, label) if not v then error(label, 2) end end

-- Reset the lazy cache so test ordering doesn't matter.
HDG.Palette:_ResetCache()

-- ===== expansion.<name> resolves from Constants.EXPANSION_DATA ============

local c = HDG.Palette:GetColor("expansion.Midnight")
assertTrue(c ~= nil, "expansion.Midnight resolves")
assertEq(c.r, 0.6, "Midnight r")
assertEq(c.g, 0.2, "Midnight g")
assertEq(c.b, 0.8, "Midnight b")

c = HDG.Palette:GetColor("expansion.The War Within")
assertTrue(c ~= nil, "expansion.The War Within resolves (multi-word names)")
assertEq(c.r, 1.0, "TWW r")

-- ===== ColorCode formatting =================================================

local cc = HDG.Palette:ColorCode("expansion.Midnight")
assertEq(cc, "|cFF9933CC", "Midnight ColorCode hex (0.6/0.2/0.8 -> 99/33/CC)")

-- ===== Unknown tokens fail loud =============================================

local ok, err = pcall(function()
    return HDG.Palette:ColorCode("expansion.Nonexistent")
end)
assertEq(ok, false, "unknown expansion token errors")
assertTrue(err:find('expansion.Nonexistent', 1, true) ~= nil,
    "error names the bad token: " .. tostring(err))

ok = pcall(function() return HDG.Palette:ColorCode("garbage.Whatever") end)
assertEq(ok, false, "unknown namespace also errors")

-- ===== faction.* brand colors (populated 2026-06-04) ========================

local _fa = HDG.Palette:GetColor("faction.Alliance")
assertTrue(_fa and _fa.r and _fa.g and _fa.b, "faction.Alliance resolves to an {r,g,b} brand color")
local _fh = HDG.Palette:GetColor("faction.Horde")
assertTrue(_fh and _fh.r and _fh.g and _fh.b, "faction.Horde resolves to an {r,g,b} brand color")
assertTrue(HDG.Palette:GetColor("faction.Neutral") ~= nil, "faction.Neutral resolves")
assertEq(HDG.Palette:GetColor("faction.Bogus"), nil, "unknown faction key returns nil")

-- ===== Cross-module namespace enforcement (ADR-023, runtime contract) ======
-- Palette:ColorCode("text.dim") must error and name Theme as the right
-- module. The error path uses error level 3 so the user sees the call
-- site, not Palette internals.

dofile("../UI/HDGR_Theme.lua")

ok, err = pcall(function() return HDG.Palette:ColorCode("text.dim") end)
assertEq(ok, false, "Palette refuses Theme-owned namespace")
assertTrue(err:find("not owned by Palette", 1, true) ~= nil,
    "error names the wrong-owner: " .. tostring(err))
assertTrue(err:find("Theme:ColorCode", 1, true) ~= nil,
    "error hints at Theme:ColorCode as the right module")

ok, err = pcall(function() return HDG.Theme:ColorCode("expansion.Midnight") end)
assertEq(ok, false, "Theme refuses Palette-owned namespace")
assertTrue(err:find("Palette:ColorCode", 1, true) ~= nil,
    "Theme error hints at Palette:ColorCode")

-- Malformed token (no dot) also errors cleanly.
ok, err = pcall(function() return HDG.Palette:ColorCode("notoken") end)
assertEq(ok, false, "malformed token errors")

print("test_palette_smoke.lua: ALL PASS")
