*** Settings ***
Documentation    Integration test: file tool writes produce ChangeSet output
Library          OperatingSystem
Library          Process
Library          Collections

*** Variables ***
${PYTHON}    python3

*** Test Cases ***
File Write Produces ChangeSet Entry
    [Documentation]    Run a file-write tool via ChangeSetCapture and
    ...                verify the resulting ChangeSet contains the entry.
    ${result}=    Run Process    ${PYTHON}    -c
    ...    ${CHANGESET_SCRIPT}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS:entries=1
    Should Contain    ${result.stdout}    PASS:operation=create

ChangeSet Summary Counts Match
    [Documentation]    Verify summary counts after multiple operations.
    ${result}=    Run Process    ${PYTHON}    -c
    ...    ${SUMMARY_SCRIPT}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS:total=2
    Should Contain    ${result.stdout}    PASS:creates=1
    Should Contain    ${result.stdout}    PASS:modifies=1

InMemoryChangeSetStore Round Trip
    [Documentation]    Verify start/record/get via InMemoryChangeSetStore.
    ${result}=    Run Process    ${PYTHON}    -c
    ...    ${STORE_SCRIPT}
    Log    ${result.stdout}
    Log    ${result.stderr}
    Should Be Equal As Integers    ${result.rc}    0
    Should Contain    ${result.stdout}    PASS:store_get_ok
    Should Contain    ${result.stdout}    PASS:entry_count=1

*** Keywords ***

*** Variables ***
${CHANGESET_SCRIPT}    SEPARATOR=\n
...    import tempfile, os, sys
...    sys.path.insert(0, os.path.join(os.getcwd(), "src"))
...    from pathlib import Path
...    from cleveragents.tool.builtins.changeset import ChangeSetCapture
...    from cleveragents.tool.builtins.file_tools import FILE_WRITE_SPEC
...    tmpdir = tempfile.mkdtemp()
...    capture = ChangeSetCapture(plan_id="plan-robot", resource_id="res-r", sandbox_root=tmpdir)
...    wrapped = capture.wrap_tool(FILE_WRITE_SPEC)
...    result = wrapped.handler({"path": "hello.txt", "content": "hello world", "sandbox_root": tmpdir})
...    cs = capture.get_changeset()
...    print(f"PASS:entries={len(cs.entries)}")
...    print(f"PASS:operation={cs.entries[0].operation}")

${SUMMARY_SCRIPT}    SEPARATOR=\n
...    import tempfile, os, sys
...    sys.path.insert(0, os.path.join(os.getcwd(), "src"))
...    from pathlib import Path
...    from cleveragents.tool.builtins.changeset import ChangeSetCapture
...    from cleveragents.tool.builtins.file_tools import FILE_WRITE_SPEC, FILE_EDIT_SPEC
...    tmpdir = tempfile.mkdtemp()
...    capture = ChangeSetCapture(plan_id="plan-robot2", resource_id="res-r", sandbox_root=tmpdir)
...    w = capture.wrap_tool(FILE_WRITE_SPEC)
...    e = capture.wrap_tool(FILE_EDIT_SPEC)
...    w.handler({"path": "f.txt", "content": "aaa", "sandbox_root": tmpdir})
...    e.handler({"path": "f.txt", "old_text": "aaa", "new_text": "bbb", "sandbox_root": tmpdir})
...    spec = capture.to_spec_changeset()
...    s = spec.summary()
...    print(f"PASS:total={s['total']}")
...    print(f"PASS:creates={s['creates']}")
...    print(f"PASS:modifies={s['modifies']}")

${STORE_SCRIPT}    SEPARATOR=\n
...    import os, sys
...    sys.path.insert(0, os.path.join(os.getcwd(), "src"))
...    from cleveragents.domain.models.core.change import (
...        ChangeEntry, ChangeOperation, InMemoryChangeSetStore
...    )
...    store = InMemoryChangeSetStore()
...    cid = store.start("plan-robot-store")
...    entry = ChangeEntry(
...        plan_id="plan-robot-store", resource_id="r1",
...        tool_name="builtin/file-write",
...        operation=ChangeOperation.CREATE, path="new.py",
...    )
...    store.record(cid, entry)
...    cs = store.get(cid)
...    if cs is not None:
...    ${SPACE}${SPACE}${SPACE}${SPACE}print("PASS:store_get_ok")
...    ${SPACE}${SPACE}${SPACE}${SPACE}print(f"PASS:entry_count={len(cs.entries)}")
...    else:
...    ${SPACE}${SPACE}${SPACE}${SPACE}print("FAIL:store_get_returned_none")
