*** Settings ***
Library    Process
Library    String

*** Test Cases ***
TUI Headless Startup Check Returns JSON
    [Tags]    tdd_issue    tdd_issue_4193    tdd_issue_4297
    ${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}    textual_available
    Should Contain    ${result.stdout}    default_persona

TUI Headless Works When Shell Disabled
    [Tags]    tdd_issue    tdd_issue_4193    tdd_issue_4297
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    tui    --headless    shell=False    stderr=STDOUT    env:CLEVERAGENTS_DATABASE_URL=sqlite:///:memory:    env:CLEVERAGENTS_DISABLE_SHELL_MODE=1
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    textual_available
    Should Contain    ${result.stdout}    default_persona

TUI Prompt Symbol Updates For Input Modes
    [Tags]    regression    tdd_issue    tdd_issue_6431    prompt_symbol
    ${script}=    Catenate    SEPARATOR=\n
    ...    import sys
    ...    from pathlib import Path
    ...
    ...    sys.path.insert(0, str((Path.cwd() / "src").resolve()))
    ...    from cleveragents.tui.widgets.prompt import PromptInput
    ...
    ...    prompt = PromptInput()
    ...    assert prompt.prompt_symbol == "❯", f"Expected normal mode symbol, got {prompt.prompt_symbol!r}"
    ...
    ...    prompt.value = "/help"
    ...    assert prompt.prompt_symbol == "/", f"Expected command mode symbol, got {prompt.prompt_symbol!r}"
    ...
    ...    prompt.value = "!ls"
    ...    assert prompt.prompt_symbol == "$", f"Expected shell mode symbol, got {prompt.prompt_symbol!r}"
    ...
    ...    prompt.value = "@plan/123"
    ...    assert prompt.prompt_symbol == "❯", f"References should keep normal symbol, got {prompt.prompt_symbol!r}"
    ...
    ...    prompt.value = "line one\\nline two"
    ...    assert prompt.prompt_symbol == "☰", f"Expected multiline symbol, got {prompt.prompt_symbol!r}"
    ...
    ...    prompt.value = ""
    ...    assert prompt.prompt_symbol == "❯", f"Prompt should reset to normal symbol, got {prompt.prompt_symbol!r}"
    ...
    ...    print("prompt-symbol-modes-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    prompt-symbol-modes-ok

TUI Input Mode Router And Prompt Widget Behavior
    [Tags]    tdd_issue    tdd_issue_4193    tdd_issue_4297    tdd_expected_fail
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.tui.input.modes import InputModeRouter
    ...    from cleveragents.tui.widgets.prompt import PromptInput
    ...    prompt = PromptInput()
    ...    prompt.value = "hello"
    ...    prompt.action_submit()
    ...    assert prompt.value == "", f"Expected cleared prompt after submit, got: {prompt.value!r}"
    ...    print("prompt-widget-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    prompt-widget-ok

TUI Chat History Widget Behavior
    [Tags]    tdd_issue    tdd_issue_4193    tdd_issue_4297    tdd_expected_fail
    ${script}=    Catenate    SEPARATOR=\n
    ...    from cleveragents.tui.widgets.history import ChatHistory
    ...    from cleveragents.tui.events import MessageEvent, MessageType
    ...    from textual.app import App
    ...    import asyncio
    ...    class TestApp(App):
    ...        async def on_mount(self):
    ...            hist = ChatHistory()
    ...            await self.mount(hist)
    ...            msg_ev = MessageEvent(msg_type=MessageType.USER, content="Test message")
    ...            await hist.post_message_event(msg_ev)
    ...            # Just verify it doesn't crash
    ...            self.exit(0)
    ...    app = TestApp()
    ...    asyncio.run(app.run_async())
    ...    print("chat-history-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    chat-history-ok

TUI Screen Stack And Overlay Behavior
    [Tags]    tdd_issue    tdd_issue_4193    tdd_issue_4297    tdd_expected_fail
    ${script}=    Catenate    SEPARATOR=\n
    ...    from textual.app import App
    ...    from textual.screen import Screen
    ...    from cleveragents.tui.screens.examples import ExamplesScreen
    ...    import asyncio
    ...    class TestApp(App):
    ...        async def on_mount(self):
    ...            # Push examples screen as overlay
    ...            examples = ExamplesScreen()
    ...            examples.can_replace = False  # Makes it an overlay
    ...            await self.push_screen(examples)
    ...            # Verify stack depth
    ...            assert len(self.screen_stack) == 2, "Expected 2 screens after overlay push"
    ...            # Pop overlay
    ...            await self.pop_screen()
    ...            assert len(self.screen_stack) == 1, "Expected 1 screen after overlay pop"
    ...            self.exit(0)
    ...    app = TestApp()
    ...    asyncio.run(app.run_async())
    ...    print("screen-stack-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    screen-stack-ok

TUI Shell Mode Detection
    [Tags]    tdd_issue    tdd_issue_4193    tdd_issue_4297    tdd_expected_fail
    ${script}=    Catenate    SEPARATOR=\n
    ...    import os
    ...    # Test shell mode enabled (default)
    ...    os.environ.pop('CLEVERAGENTS_DISABLE_SHELL_MODE', None)
    ...    from cleveragents.config import get_config
    ...    cfg = get_config()
    ...    assert cfg.enable_shell_mode is True, f"Expected shell mode enabled, got: {cfg.enable_shell_mode}"
    ...    # Test shell mode disabled
    ...    os.environ['CLEVERAGENTS_DISABLE_SHELL_MODE'] = '1'
    ...    cfg = get_config()
    ...    assert cfg.enable_shell_mode is False, f"Expected shell mode disabled, got: {cfg.enable_shell_mode}"
    ...    print("shell-mode-detection-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    shell-mode-detection-ok
