*** Settings ***
Library    Process
Library    String

*** Test Cases ***
TUI Help Command Lists All Catalogued Commands
    [Documentation]    /help with no args must list all commands from SLASH_COMMAND_SPECS,
    ...                not the old hardcoded 3-command string.
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.tui.slash_catalog import SLASH_COMMAND_SPECS
    ...    from cleveragents.tui.commands import TuiCommandRouter
    ...    from unittest.mock import MagicMock
    ...    registry = MagicMock()
    ...    registry.list_personas.return_value = []
    ...    state = MagicMock()
    ...    router = TuiCommandRouter(persona_registry=registry, persona_state=state)
    ...    result = router.handle("help", session_id="default")
    ...    assert "Available slash commands:" in result, f"Missing header: {result[:200]}"
    ...    missing = [s.command for s in SLASH_COMMAND_SPECS if s.command not in result]
    ...    assert not missing, f"Missing commands: {missing}"
    ...    assert result != "Commands: /persona, /session, /help", "Old hardcoded string returned"
    ...    print("tui-help-all-commands-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False    stderr=STDOUT
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-help-all-commands-ok

TUI Help Command Groups By Namespace
    [Documentation]    /help output must include group headers for Session, Persona, Plan, Utility.
    [Tags]    tdd_issue    tdd_issue_4298    tdd_expected_fail
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.tui.commands import TuiCommandRouter
    ...    from unittest.mock import MagicMock
    ...    registry = MagicMock()
    ...    registry.list_personas.return_value = []
    ...    state = MagicMock()
    ...    router = TuiCommandRouter(persona_registry=registry, persona_state=state)
    ...    result = router.handle("help", session_id="default")
    ...    for group in ("Session:", "Persona:", "Plan:", "Utility:"):
    ...        assert group in result, f"Missing group header {group!r} in output"
    ...    print("tui-help-groups-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False    stderr=STDOUT
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-help-groups-ok

TUI Help Command With Known Command Returns Specific Help
    [Documentation]    /help persona:list must return description for that specific command.
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.tui.commands import TuiCommandRouter
    ...    from unittest.mock import MagicMock
    ...    registry = MagicMock()
    ...    registry.list_personas.return_value = []
    ...    state = MagicMock()
    ...    router = TuiCommandRouter(persona_registry=registry, persona_state=state)
    ...    result = router.handle("help persona:list", session_id="default")
    ...    assert "/persona:list" in result, f"Missing command name in: {result}"
    ...    assert "Display all personas" in result, f"Missing description in: {result}"
    ...    assert "Persona" in result, f"Missing group in: {result}"
    ...    print("tui-help-specific-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False    stderr=STDOUT
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-help-specific-ok

TUI Help Command With Unknown Command Returns Not Found
    [Documentation]    /help nonexistent must return an "Unknown command" message.
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.tui.commands import TuiCommandRouter
    ...    from unittest.mock import MagicMock
    ...    registry = MagicMock()
    ...    registry.list_personas.return_value = []
    ...    state = MagicMock()
    ...    router = TuiCommandRouter(persona_registry=registry, persona_state=state)
    ...    result = router.handle("help nonexistent:cmd", session_id="default")
    ...    assert "Unknown command" in result, f"Expected not-found message, got: {result}"
    ...    print("tui-help-unknown-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False    stderr=STDOUT
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tui-help-unknown-ok

TUI Headless Startup Help Payload Contains All Commands
    [Documentation]    run_tui --headless JSON payload help field must list all commands.
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    tui    --headless    shell=False    stderr=STDOUT    env:CLEVERAGENTS_DATABASE_URL=sqlite:///:memory:
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    Available slash commands:
    Should Contain    ${result.stdout}    persona:list
    Should Contain    ${result.stdout}    session:create
