*** Settings ***
Documentation    Smoke tests for Invariant data model persistence contract
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment With Database Isolation
Suite Teardown   Cleanup Test Environment

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

*** Test Cases ***
Create Invariant With Required Fields
    [Documentation]    Create an Invariant with all required fields and verify persistence
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    create_invariant    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=create_invariant failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-create-ok

Is Active Defaults To True
    [Documentation]    Verify that is_active defaults to True on a new Invariant
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    default_is_active    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=default_is_active failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-default-active-ok

Created At Is Populated
    [Documentation]    Verify that created_at is auto-populated on insert
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    created_at    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=created_at failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-created-at-ok

Id Is UUID
    [Documentation]    Verify that the Invariant id is a valid UUID string
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    uuid_id    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=uuid_id failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-uuid-ok

Query Active Invariants
    [Documentation]    Query Invariants filtered by is_active=True
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    query_active    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=query_active failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-query-active-ok

Query Inactive Invariants
    [Documentation]    Query Invariants filtered by is_active=False
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    query_inactive    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=query_inactive failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-query-inactive-ok

Deactivate Invariant
    [Documentation]    Set is_active to False on an existing Invariant
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    deactivate    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=deactivate failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-deactivate-ok

Table Exists After Migration
    [Documentation]    Verify the invariants table exists after schema creation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    table_exists    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=table_exists failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-table-ok

Index On Is Active Exists
    [Documentation]    Verify the index on is_active exists after schema creation
    ${result}=    Run Process    ${PYTHON}    ${HELPER_SCRIPT}    index_exists    cwd=${WORKSPACE}
    Should Be Equal As Integers    ${result.rc}    0    msg=index_exists failed: ${result.stderr}
    Should Contain    ${result.stdout}    invariant-index-ok
