*** Settings ***
Documentation    SEC7 - Audit logging integration smoke tests
Library          OperatingSystem
Library          Process

*** Variables ***
${AUDIT_SERVICE}      ${CURDIR}/../src/cleveragents/application/services/audit_service.py
${AUDIT_CLI}          ${CURDIR}/../src/cleveragents/cli/commands/audit.py
${AUDIT_MODEL}        ${CURDIR}/../src/cleveragents/infrastructure/database/models.py
${AUDIT_DOC}          ${CURDIR}/../docs/reference/audit_logging.md

*** Test Cases ***
Audit Service Module Exists
    [Documentation]    Verify the audit service module is present
    File Should Exist    ${AUDIT_SERVICE}

Audit CLI Module Exists
    [Documentation]    Verify the audit CLI module is present
    File Should Exist    ${AUDIT_CLI}

Audit Log Model Defined
    [Documentation]    Verify AuditLogModel is defined in models.py
    ${content}=    Get File    ${AUDIT_MODEL}
    Should Contain    ${content}    class AuditLogModel
    Should Contain    ${content}    audit_log
    Should Contain    ${content}    event_type
    Should Contain    ${content}    plan_id
    Should Contain    ${content}    details
    Should Contain    ${content}    created_at

Audit Service Has Record Method
    [Documentation]    Verify the audit service has a record method
    ${content}=    Get File    ${AUDIT_SERVICE}
    Should Contain    ${content}    def record(
    Should Contain    ${content}    event_type
    Should Contain    ${content}    VALID_EVENT_TYPES

Audit Service Has List Method
    [Documentation]    Verify the audit service has a list_entries method
    ${content}=    Get File    ${AUDIT_SERVICE}
    Should Contain    ${content}    def list_entries(
    Should Contain    ${content}    plan_id
    Should Contain    ${content}    project_name
    Should Contain    ${content}    since

Audit Service Has Prune Method
    [Documentation]    Verify the audit service has a prune method
    ${content}=    Get File    ${AUDIT_SERVICE}
    Should Contain    ${content}    def prune(
    Should Contain    ${content}    retention_days

Audit Service Has Thread Safe Session Init
    [Documentation]    Verify _ensure_session() uses threading.Lock (bug #991 fix)
    ${content}=    Get File    ${AUDIT_SERVICE}
    Should Contain    ${content}    import threading
    Should Contain    ${content}    _session_lock
    Should Contain    ${content}    threading.Lock()

Audit Service Has Close And Context Manager
    [Documentation]    Verify the audit service supports close() and context manager
    ${content}=    Get File    ${AUDIT_SERVICE}
    Should Contain    ${content}    def close(
    Should Contain    ${content}    def __enter__(
    Should Contain    ${content}    def __exit__(

Audit CLI Has List Command
    [Documentation]    Verify the audit CLI has a list command
    ${content}=    Get File    ${AUDIT_CLI}
    Should Contain    ${content}    name="list"
    Should Contain    ${content}    --plan
    Should Contain    ${content}    --project
    Should Contain    ${content}    --since

Audit CLI Has Show Command
    [Documentation]    Verify the audit CLI has a show command
    ${content}=    Get File    ${AUDIT_CLI}
    Should Contain    ${content}    name="show"
    Should Contain    ${content}    audit_id

Audit CLI Has Prune Command
    [Documentation]    Verify the audit CLI has a prune command
    ${content}=    Get File    ${AUDIT_CLI}
    Should Contain    ${content}    name="prune"
    Should Contain    ${content}    --days
    Should Contain    ${content}    --yes

Audit Documentation Exists
    [Documentation]    Verify the audit logging reference doc exists
    File Should Exist    ${AUDIT_DOC}
    ${content}=    Get File    ${AUDIT_DOC}
    Should Contain    ${content}    Audit Logging
    Should Contain    ${content}    agents audit list
    Should Contain    ${content}    agents audit show
    Should Contain    ${content}    agents audit prune
    Should Contain    ${content}    audit_retention_days
