import re
from pathlib import Path
import pytest
from domin8.tools.request_change import handle_request_change
from domin8.git_ops import create_diff
from domin8.storage import get_agent_data_root
VALID_LONG_SUMMARY = (
"This is a sufficiently long justification intended to satisfy the strict word-count rules. "
"It explains why the change is necessary because it removes an ambiguous API and clarifies the public surface, "
"therefore reducing future maintenance burden and preventing accidental misuse. "
"The change is limited in scope and targets a single file to keep blast radius minimal, and thus is safe to review."
)
@pytest.mark.asyncio
async def test_handle_request_writes_normalized_and_blast(tmp_path, monkeypatch):
target = Path('src/domin8/config.py')
assert target.exists()
original = target.read_text()
new = original + "\n# _test_change: metadata inclusion\n"
diff = create_diff(original, new, str(target))
res = await handle_request_change({'summary': VALID_LONG_SUMMARY, 'diff': diff, 'actor_id': 'agent.test'})
# extract artifact path and UUID
m = re.search(r'~/.domin8/agent_data/(.+?)/artifacts/([0-9a-fA-F-]+)', res)
assert m is not None
repo_rel = m.group(1)
uuid = m.group(2)
artifact_dir = get_agent_data_root() / repo_rel / 'artifacts' / uuid
meta = (artifact_dir / 'meta.json').read_text()
assert 'normalized_changes' in meta
assert 'blast' in meta
# Quick sanity check: blast.score exists and is between 0 and 100
assert '"score":' in meta