-- test_perf_gate.lua
-- ============================================================================
-- HDG.Perf is gated by a single raw account-wide SavedVariable, HDG_DB.perf
-- -- NOT a Store/Config field. This guards the contract that made /hdgrperf
-- "stick" on fresh accounts: the gate reads straight off the SV root, so it is
-- available before the Config layer hydrates, persists trivially across reload,
-- and is independent of debug mode.
dofile("wow_mock.lua")
dofile("../Core/HDGR_SchemeConstants.lua")
dofile("../Core/HDGR_Constants.lua")
dofile("../Core/HDGR_Format.lua")
dofile("../Modules/HDGR_Perf.lua")

local P = HDG.Perf
local function assertEq(a, e, l) if a ~= e then error(l .. ": expected " .. tostring(e) .. ", got " .. tostring(a), 2) end end

-- 1. Absent SV root reads as disabled (never crashes -- instrumentation is not load-bearing).
_G.HDG_DB = nil
assertEq(P:Enabled(), false, "Enabled() is false when HDG_DB is nil")

-- 2. Fresh account: flag defaults off.
_G.HDG_DB = {}
assertEq(P:Enabled(), false, "Enabled() is false on a fresh HDG_DB (no perf flag)")

-- 3. /hdgrperf on -> flag set, gate opens. Independent of debug (no Store touched).
P:Command("on")
assertEq(_G.HDG_DB.perf, true, "/hdgrperf on writes HDG_DB.perf = true")
assertEq(P:Enabled(), true, "Enabled() is true after /hdgrperf on")

-- 4. RELOAD: WoW keeps HDG_DB across the session boundary. The gate survives
--    with no Store/Config rehydrate involved -- this is the bug that was fixed.
local persisted = _G.HDG_DB          -- same table WoW would serialize + restore
_G.HDG_DB = persisted
assertEq(P:Enabled(), true, "Enabled() still true after a reload (flag persisted on the SV root)")

-- 5. /hdgrperf off -> flag cleared to nil so it doesn't linger in SavedVariables.
P:Command("off")
assertEq(_G.HDG_DB.perf, nil, "/hdgrperf off clears the flag to nil")
assertEq(P:Enabled(), false, "Enabled() is false after /hdgrperf off")

print("test_perf_gate: PASS")
