from pathlib import Path
import json
from domin8.storage.artifact_storage import write_json_atomic, atomic_write, compute_artifact_hash, verify_artifact, get_agent_data_root
def test_compute_and_verify(tmp_path, monkeypatch):
monkeypatch.setenv("HOME", str(tmp_path))
root = get_agent_data_root()
artifact_dir = root / "README.md" / "artifacts" / "uuid-hash"
artifact_dir.mkdir(parents=True)
meta = {"uuid": "uuid-hash", "repo_relative_path": "README.md", "action": "EDIT"}
intent = {"explainability": {"bullets": ["reason one","reason two","reason three"]}}
write_json_atomic(artifact_dir / "meta.json", meta)
write_json_atomic(artifact_dir / "intent.json", intent)
atomic_write(artifact_dir / "diff.patch", "-old\n+new\n")
# No approval.json yet; compute hash should still work
h = compute_artifact_hash(artifact_dir)
# Write verification file
atomic_write(artifact_dir / "verification.sha256", h)
assert verify_artifact(artifact_dir) is True
# If verification changed, should fail
atomic_write(artifact_dir / "diff.patch", "-old\n+modified\n")
assert verify_artifact(artifact_dir) is False