*** Settings ***
Documentation    Integration coverage for the CleverAgents TUI loading throbber.
Test Tags        tui    throbber    widget
Resource         ${CURDIR}/common.resource
Library          Process
Library          Collections
Library          indentation_library.py
Suite Setup      Setup Test Environment With Database Isolation
Suite Teardown   Cleanup Test Environment

*** Variables ***
${PYTHON}        python

*** Test Cases ***
Throbber Show Hide Cycle Updates Visibility
    [Documentation]    The throbber toggles visibility attributes when shown and hidden.
    ${script}=    Catenate    SEPARATOR=\n
    ...    from types import SimpleNamespace
    ...    from cleveragents.tui.widgets.throbber import LoadingThrobber
    ...    throbber = LoadingThrobber(style="rainbow")
    ...    throbber.styles = SimpleNamespace(height=None, min_height=None, padding=None)
    ...    assert throbber.is_running is False, "Expected throbber idle on init"
    ...    throbber.show_loading()
    ...    assert throbber.is_running is True, "Expected throbber running after show_loading()"
    ...    assert getattr(throbber, "display", True) is True, "display should be True after show_loading()"
    ...    assert getattr(throbber.styles, "height", None) == 1, "height should be 1 when visible"
    ...    assert getattr(throbber.styles, "min_height", None) == 1, "min_height should be 1 when visible"
    ...    throbber.hide_loading()
    ...    assert throbber.is_running is False, "Expected throbber idle after hide_loading()"
    ...    assert getattr(throbber, "display", False) is False, "display should be False after hide_loading()"
    ...    assert getattr(throbber.styles, "height", None) == 0, "height should collapse when hidden"
    ...    assert getattr(throbber.styles, "min_height", None) == 0, "min_height should collapse when hidden"
    ...    print("throbber-visibility-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    throbber-visibility-ok

Throbber Style Switch Preserves Running State
    [Documentation]    Switching styles while running keeps animation active and rotates quotes.
    ${script}=    Catenate    SEPARATOR=\n
    ...    from types import SimpleNamespace
    ...    from cleveragents.tui.widgets.throbber import LoadingThrobber
    ...    throbber = LoadingThrobber(style="rainbow", quotes=("alpha", "beta", "gamma", "delta"))
    ...    throbber.styles = SimpleNamespace(height=None, min_height=None, padding=None)
    ...    throbber.show_loading()
    ...    assert throbber.is_running is True, "Expected throbber to run after show_loading()"
    ...    throbber.set_style("quotes")
    ...    assert throbber.is_running is True, "Expected throbber to remain running after style change"
    ...    quotes_before = list(throbber._quotes)
    ...    throbber.hide_loading()
    ...    throbber.show_loading()
    ...    quotes_after = list(throbber._quotes)
    ...    assert quotes_before != quotes_after, "Quotes deque should rotate between animations"
    ...    throbber.hide_loading()
    ...    print("throbber-style-ok")
    ${result}=    Run Process    ${PYTHON}    -c    ${script}    shell=False    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    throbber-style-ok

Throbber Rejects Invalid Styles
    [Documentation]    Invalid styles raise ValueError to protect configuration flow.
    ${script}=    Catenate    SEPARATOR=\n
    ...    from types import SimpleNamespace
    ...    from cleveragents.tui.widgets.throbber import LoadingThrobber
    ...    throbber = LoadingThrobber()
    ...    throbber.styles = SimpleNamespace(height=None, min_height=None, padding=None)
    ...    try:
    ...        throbber.set_style("blink")
    ...    except ValueError:
    ...        print("throbber-invalid-style-ok")
    ...    else:
    ...        raise AssertionError("Expected ValueError for invalid throbber style")
    ${code}=    Fix Python Indentation    ${script}
    ${temp_file}=    Evaluate    (lambda t: (__import__('os').close(t[0]), t[1])[-1])(__import__('tempfile').mkstemp(suffix='.py', dir='/tmp'))
    Create File    ${temp_file}    ${code}
    ${result}=    Run Process    ${PYTHON}    ${temp_file}    shell=False    timeout=120s    on_timeout=kill
    Remove File    ${temp_file}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    throbber-invalid-style-ok
