from pathlib import Path


ROOT = Path(__file__).resolve().parents[1]


def test_competitor_feeder_persists_all_match_attempt_outcomes():
    source = (ROOT / "services/competitor_price_feeder.py").read_text(encoding="utf-8")
    migration = (ROOT / "migrations/023_competitor_match_attempts.sql").read_text(encoding="utf-8")

    assert "attempts_written" in source
    assert "_ensure_competitor_match_attempts_table" in source
    assert "_record_match_attempt" in source
    assert "INSERT INTO competitor_match_attempts" in source
    assert "CAST(:search_terms AS jsonb)" in source
    assert 'attempt_status="matched"' in source
    assert 'attempt_status="low_score"' in source
    assert 'attempt_status="no_result"' in source
    assert 'attempt_status="no_match"' in source
    assert 'attempt_status="error"' in source
    assert "_search_pchome_candidates(crawler, momo_name, search_terms)" in source

    assert "CREATE TABLE IF NOT EXISTS competitor_match_attempts" in migration
    assert "attempt_status" in migration
    assert "search_terms" in migration
    assert "best_match_score" in migration
    assert "error_message" in migration
    assert "idx_comp_match_attempts_sku_source_time" in migration
