-- RLConsoleTests.lua
-- Purpose: Existence tests for console command manager and error handling
-- Author: Ritter

RLConsoleTests = {}

local Log = RmLogging.getLogger("RLRM")

--- Run all console command tests
---@param assert function Assert closure from RLTestRunner
function RLConsoleTests:runAll(assert)
    Log:info("--- Console Command Tests ---")

    -- =========================================================================
    -- Module existence (1 assertion)
    -- =========================================================================

    assert("g_rlConsoleCommandManager exists", g_rlConsoleCommandManager ~= nil, "not nil", tostring(g_rlConsoleCommandManager))

    -- =========================================================================
    -- Handler method existence (4 assertions)
    -- =========================================================================

    local hasSetAnimal = type(g_rlConsoleCommandManager.setAnimal) == "function"
    assert("setAnimal handler is function", hasSetAnimal, true, hasSetAnimal)

    local hasSetGenetics = type(g_rlConsoleCommandManager.setGenetics) == "function"
    assert("setGenetics handler is function", hasSetGenetics, true, hasSetGenetics)

    local hasSetInput = type(g_rlConsoleCommandManager.setInput) == "function"
    assert("setInput handler is function", hasSetInput, true, hasSetInput)

    local hasSetOutput = type(g_rlConsoleCommandManager.setOutput) == "function"
    assert("setOutput handler is function", hasSetOutput, true, hasSetOutput)

    -- =========================================================================
    -- Error handling — no target animal (1 assertion)
    -- =========================================================================

    local savedAnimal = g_rlConsoleCommandManager.animal
    local savedPlaceable = g_rlConsoleCommandManager.placeable
    g_rlConsoleCommandManager.animal = nil
    local result = g_rlConsoleCommandManager:setGenetics(nil, nil)
    assert("setGenetics no target returns error",
        result == "rlSetAnimalGenetics: no targeted animal",
        "rlSetAnimalGenetics: no targeted animal", tostring(result))
    g_rlConsoleCommandManager.animal = savedAnimal
    g_rlConsoleCommandManager.placeable = savedPlaceable

    -- =========================================================================
    -- Error handling — invalid animal type (1 assertion)
    -- =========================================================================

    savedAnimal = g_rlConsoleCommandManager.animal
    savedPlaceable = g_rlConsoleCommandManager.placeable
    local result2 = g_rlConsoleCommandManager:setAnimal("INVALID_TYPE_XYZ", "1", "1")
    assert("setAnimal invalid type returns not found",
        result2 == "rlSetTargetAnimal: animal not found",
        "rlSetTargetAnimal: animal not found", tostring(result2))
    g_rlConsoleCommandManager.animal = savedAnimal
    g_rlConsoleCommandManager.placeable = savedPlaceable
end
