*** Settings ***
Documentation    M1 end-to-end verification: action creation, resource registration, project linking, plan lifecycle, SQLite persistence, ChangeSet from tool invocations, git worktree sandbox isolation, and post-apply commit
Resource         ${CURDIR}/common.resource
Suite Setup      Setup Test Environment
Suite Teardown   Cleanup Test Environment

*** Variables ***
${HELPER}    ${CURDIR}/helper_m1_e2e_verification.py

*** Test Cases ***
Action Create From YAML Config
    [Documentation]    Create an action from a YAML config file via agents action create --config
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    action-create    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-action-create-ok

Register Git Checkout Resource
    [Documentation]    Register a git-checkout resource via agents resource add
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    resource-register    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-resource-register-ok

Create Project And Link Resource
    [Documentation]    Create a project and link a resource to it
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    project-create-link    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-project-create-link-ok

Full Plan Lifecycle Use Execute Diff Apply
    [Documentation]    Run full plan lifecycle: plan use -> plan execute -> plan diff -> plan apply
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    plan-lifecycle    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-plan-lifecycle-ok

SQLite Persistence For Plan And Action Records
    [Documentation]    Verify Plan and Action records persist to SQLite with correct serialisation
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    sqlite-persistence    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-sqlite-persistence-ok

ChangeSet Built From Tool Invocations
    [Documentation]    Verify ChangeSet is built from tool invocations, not parsed CLI output
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    changeset-invocations    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-changeset-invocations-ok

Git Worktree Sandbox Isolation
    [Documentation]    Verify git worktree sandbox creates isolated directory and changes do not affect original until apply
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    sandbox-isolation    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-sandbox-isolation-ok

Post Apply Commit Exists In Target Repo
    [Documentation]    Verify post-apply commit exists in the target repo after sandbox commit
    ${result}=    Run Process    ${PYTHON}    ${HELPER}    post-apply-commit    cwd=${WORKSPACE}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    m1-post-apply-commit-ok
