*** Settings ***
Documentation     Integration test for agents init in fresh environment (issue #4180).
...               Verifies that agents init properly initializes the database and
...               creates all required directories in a clean, containerized context.
Resource          ${CURDIR}/common.resource
Library           Process
Library           OperatingSystem
Library           String
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Test Cases ***
Init In Fresh Environment Exits Successfully
    [Documentation]    agents init should complete with exit code 0 in a fresh environment
    [Tags]    issue_4180    fresh_environment
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='init_fresh_')
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    init    --yes
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=Expected exit code 0 in fresh environment, got ${result.rc}. stderr: ${result.stderr}
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Init In Fresh Environment Initializes Database
    [Documentation]    agents init should properly initialize the database in fresh environment
    [Tags]    issue_4180    fresh_environment
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='init_fresh_db_')
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    init    --yes
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=Init should succeed in fresh environment
    Should Contain    ${result.stdout}    Database:
    ...    msg=Output should indicate database was initialized
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Init In Fresh Environment Creates Required Directories
    [Documentation]    agents init should create all required directories for packaged/containerized context
    [Tags]    issue_4180    fresh_environment
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='init_fresh_dirs_')
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    init    --yes
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    Directories:
    ...    msg=Output should indicate directories were created
    # Verify required directories exist
    ${config_dir}=    Evaluate    __import__('os').path.join('${tmpdir}', '.cleveragents')
    Directory Should Exist    ${config_dir}
    ...    msg=.cleveragents config directory should exist
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Init In Fresh Environment Reports All Init Output Fields
    [Documentation]    agents init should produce all spec-required output fields for fresh environment
    [Tags]    issue_4180    fresh_environment
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='init_fresh_out_')
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    init    --yes
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    # All spec-required output fields must be present
    Should Contain    ${result.stdout}    Data Dir:
    ...    msg=Output should contain Data Dir field per spec
    Should Contain    ${result.stdout}    Config:
    ...    msg=Output should contain Config field per spec
    Should Contain    ${result.stdout}    Database:
    ...    msg=Output should contain Database field per spec
    Should Contain    ${result.stdout}    Directories:
    ...    msg=Output should contain Directories field per spec
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True

Init Handles Packaged Alembic Files Correctly
    [Documentation]    agents init should find and use alembic files from packaged location in fresh environment
    [Tags]    issue_4180    fresh_environment    alembic_packaging
    ${tmpdir}=    Evaluate    __import__('tempfile').mkdtemp(prefix='init_fresh_alembic_')
    ${result}=    Run Process    ${PYTHON}    -m    cleveragents    init    --yes
    ...    timeout=120s    on_timeout=kill    cwd=${tmpdir}
    Should Be Equal As Integers    ${result.rc}    0
    ...    msg=Init should succeed with packaged alembic files. stderr: ${result.stderr}
    Should Contain    ${result.stdout}    Database:
    ...    msg=Database should be initialized from packaged alembic files
    [Teardown]    Remove Directory    ${tmpdir}    recursive=True
