*** Settings ***
Documentation     Smoke tests for decision persistence via DecisionRepository
Resource          ${CURDIR}/common.resource
Suite Setup       Setup Test Environment
Suite Teardown    Cleanup Test Environment

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

*** Test Cases ***
Create And Retrieve Root Decision
    [Documentation]    Create a root prompt_definition decision, persist, retrieve
    [Tags]    database    decision    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    create-retrieve    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    create-retrieve-ok

Create Child Decision With Parent
    [Documentation]    Create a child strategy_choice under a root decision
    [Tags]    database    decision    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    create-child    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    create-child-ok

JSON Fields Round-Trip
    [Documentation]    Persist alternatives, context snapshot, artifacts and verify round-trip
    [Tags]    database    decision    json    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    json-roundtrip    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    json-roundtrip-ok

Get Decisions By Plan
    [Documentation]    Retrieve all decisions for a plan in sequence order
    [Tags]    database    decision    query    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    get-by-plan    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    get-by-plan-ok

Decision Tree BFS Traversal
    [Documentation]    Build a 3-level tree and retrieve via BFS
    [Tags]    database    decision    tree    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    tree-bfs    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    tree-bfs-ok

Path To Root Traversal
    [Documentation]    Walk from a leaf decision up to the root
    [Tags]    database    decision    tree    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    path-to-root    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    path-to-root-ok

Superseded By Update
    [Documentation]    Mark a decision as superseded and verify
    [Tags]    database    decision    correction    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    superseded    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    superseded-ok

Delete Decision
    [Documentation]    Delete a decision and verify it is gone
    [Tags]    database    decision    persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    delete    cwd=${WORKSPACE}    timeout=120s    on_timeout=kill
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    delete-ok
