*** Settings ***
Documentation     End-to-end persistence lifecycle tests covering full phase
...               transitions, process restart simulation, plan status reopen,
...               concurrent CLI access safeguards, argument/invariant ordering,
...               and project link persistence.
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

*** Variables ***
${HELPER_SCRIPT}    robot/helper_persistence_lifecycle.py

*** Test Cases ***
Plan Full Lifecycle Phase Transitions
    [Documentation]    Create action and plan, walk through every phase
    ...                (action -> strategize -> execute -> apply) and verify
    ...                terminal state is reached and persisted.
    [Tags]    database    plan    lifecycle    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    full-lifecycle    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    full-lifecycle-ok

Process Restart Simulation
    [Documentation]    Write plan to file-backed SQLite, close the connection
    ...                entirely, reopen from disk and verify all plan fields
    ...                (phase, state, description, project links) survive.
    [Tags]    database    plan    restart    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    restart-simulation    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    restart-simulation-ok

Reopen Plan Status After Restart
    [Documentation]    Close DB, reopen, and verify plan phase/state,
    ...                automation level, and tags are unchanged.
    [Tags]    database    plan    reopen    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    reopen-plan-status    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    reopen-plan-status-ok

Concurrent CLI Access Safeguards
    [Documentation]    Open two independent sessions against the same DB file
    ...                and verify committed plans are visible across sessions.
    [Tags]    database    plan    concurrent    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    concurrent-access    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    concurrent-access-ok

Stored Arguments Ordering Persistence
    [Documentation]    Create an action with 4 ordered arguments, persist,
    ...                retrieve and verify positional ordering is preserved.
    [Tags]    database    action    arguments    ordering
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    arguments-ordering    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    arguments-ordering-ok

Stored Invariants Ordering Persistence
    [Documentation]    Create an action with 3 ordered invariants, persist,
    ...                retrieve and verify list ordering is preserved.
    [Tags]    database    action    invariants    ordering
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    invariants-ordering    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    invariants-ordering-ok

Project Links Persistence Through Restart
    [Documentation]    Create plan with 2 project links, close DB, reopen,
    ...                verify both links survive the restart cycle.
    [Tags]    database    plan    project-links    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    project-links    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    project-links-ok
