-- test_smoke.lua
-- ============================================================================
-- Smoke test: Store dispatch + reducer + selectors + BindingEngine.
-- Uses SET_OWNED_ONLY as a fixture (real VDS action + selector; the engine
-- paths exercised are identical to what the Hello sandbox tested before
-- Phase 7 cleanup).

dofile("wow_mock.lua")
dofile("../Core/VDS_Constants.lua")
dofile("../Core/VDS_SchemeConstants.lua")
dofile("../Core/VDS_Paths.lua")
dofile("../Core/VDS_Log.lua")
dofile("../Core/VDS_Store.lua")
dofile("../UI/VDS_Selectors.lua")
dofile("../UI/VDS_WidgetTypes.lua")
dofile("../UI/VDS_Theme.lua")
dofile("../UI/VDS_Palette.lua")
dofile("../UI/VDS_Components.lua")
dofile("../UI/VDS_BindingEngine.lua")
dofile("../UI/VDS_Selectors_Progress.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

-- ===== Action meta (from Core/VDS_ActionMeta.lua -- single source of truth) =====
local A = VDS.Constants.ACTIONS
dofile("../Core/VDS_ActionMeta.lua")

VDS.Store:ValidateActionMetaExhaustiveness()
VDS_DB = {}
VDS.Store:LoadFromSavedVariables()

-- ===== State shape =====
assertEq(VDS.Store:GetState().account.config.ownedOnly, false, "default ownedOnly = false")
assertEq(VDS.Store:GetState().account.config.scheme, "DefaultDark", "default scheme")
assertEq(VDS.Store:GetState().account.cart, nil, "account.cart removed (selector-derived now)")

-- ===== Reducer: SET_OWNED_ONLY (persisted config write) =====
VDS.Store:Dispatch({ type = A.SET_OWNED_ONLY, payload = { value = true } })
assertEq(VDS.Store:GetState().account.config.ownedOnly, true, "ownedOnly = true after dispatch")

VDS.Store:Dispatch({ type = A.SET_OWNED_ONLY, payload = { value = false } })

-- ===== Selectors =====
local v1 = VDS.Selectors:Call("filter.owned", VDS.Store:GetState(), {})
assertEq(v1, false, "filter.owned selector returns false after reset")

VDS.Store:Dispatch({ type = A.SET_OWNED_ONLY, payload = { value = true } })
local v2 = VDS.Selectors:Call("filter.owned", VDS.Store:GetState(), {})
assertEq(v2, true, "filter.owned selector returns true after dispatch")

-- ===== Reads-closure =====
local reads = VDS.Selectors:GetReads("filter.owned")
assertTrue(reads["account.config.ownedOnly"], "filter.owned reads account.config.ownedOnly")

-- ===== Closed taxonomy =====
local ok = pcall(function()
    VDS.Store:Dispatch({ type = "VDS_NOT_A_REAL_ACTION" })
end)
assertEq(ok, false, "unknown action dispatch errors loud")

print("test_smoke.lua passed")
