import pytest
from pathlib import Path
from domin8.tools.request_change import handle_request_change
from domin8.config import RequestChangeConfig
from domin8.git_ops import create_diff
@pytest.mark.asyncio
async def test_repo_summary_disallowed_chars(monkeypatch):
# Force a strict summary regex that permits only letters and spaces
cfg = RequestChangeConfig(allow=["**/*"], deny=[], summary_allowed_regex=r"^[A-Za-z\s]+$")
monkeypatch.setattr('domin8.config.load_request_change_config', lambda repo_root: cfg)
target = Path('docs/refactor_plan.md')
assert target.exists()
original = target.read_text()
new = original.replace('Phase 1 – Single Tool Contract', 'Phase 1 — Single Tool Contract')
diff = create_diff(original, new, str(target))
# This summary contains punctuation which should be rejected by the repo regex
summary = (
("This summary contains punctuation and numbers 1234. " * 6)
+ "It clearly explains the intent because it removes ambiguity and therefore reduces future maintenance burden."
)
res = await handle_request_change({'summary': summary, 'diff': diff, 'actor_id': 'agent.test'})
assert 'disallowed characters according to repository policy' in res
@pytest.mark.asyncio
async def test_summary_redundant_ngrams(monkeypatch):
# Use default config but keep the test self-contained by returning a config instance
cfg = RequestChangeConfig(allow=["**/*"], deny=[])
monkeypatch.setattr('domin8.config.load_request_change_config', lambda repo_root: cfg)
target = Path('docs/refactor_plan.md')
assert target.exists()
original = target.read_text()
new = original.replace('This roadmap takes you from an ad hoc, agent-driven modification flow', 'This roadmap begins the transition from ad hoc agent-driven modifications')
diff = create_diff(original, new, str(target))
# Build a summary with repeated bigram 'fix issue' 10 times to trigger n-gram redundancy
repeated = ('fix issue ' * 10).strip()
filler = ' '.join(f'word{i}' for i in range(10))
summary = f"{repeated} {filler} because it addresses a reproducible problem and therefore reduces risk in the system."
res = await handle_request_change({'summary': summary, 'diff': diff, 'actor_id': 'agent.test'})
assert 'overly repetitive' in res or 'repeated phrases detected' in res
@pytest.mark.asyncio
async def test_summary_ngram_absolute_threshold(monkeypatch):
# Configure an aggressive absolute threshold (ngram_max_count=1) to fail on repeated phrases
cfg = RequestChangeConfig(allow=["**/*"], deny=[], redundancy_ngram_max_count=1)
monkeypatch.setattr('domin8.config.load_request_change_config', lambda repo_root: cfg)
target = Path('docs/refactor_plan.md')
assert target.exists()
original = target.read_text()
new = original.replace('This roadmap takes you from an ad hoc, agent-driven modification flow', 'This roadmap begins the transition from ad hoc agent-driven modifications')
diff = create_diff(original, new, str(target))
summary = ("update logging " * 2) + ("fillerword " * 40) + "because it clarifies what the agent intends and therefore reduces uncertainty in tests."
res = await handle_request_change({'summary': summary, 'diff': diff, 'actor_id': 'agent.test'})
assert 'overly repetitive' in res or 'repeated phrases detected' in res