from uuid import UUID
from starlette.testclient import TestClient

from fmatch.saas.main import create_app
from fmatch.saas.auth import get_current_account


def test_cache_control_no_store_on_run_poll_results():
    app = create_app()
    acct = UUID("22222222-2222-2222-2222-222222222222")
    app.dependency_overrides[get_current_account] = lambda: acct
    client = TestClient(app)

    payload = {
        "input": {"uri": "file://dummy.csv"},
        "intelligence": {"profile": "leads_to_accounts", "threshold": 0.75, "options": {}},
    }

    r1 = client.post("/api/v2/jobs/run", json=payload)
    assert r1.status_code in (201, 200), r1.text
    job_id = r1.json()["job_id"]
    assert r1.headers.get("Cache-Control") == "no-store"
    assert r1.headers.get("Location", "").endswith(f"/api/v2/jobs/{job_id}")

    r2 = client.get(f"/api/v2/jobs/{job_id}")
    assert r2.status_code in (200, 202, 404), r2.text
    assert r2.headers.get("Cache-Control") == "no-store"

    r3 = client.get(f"/api/v2/jobs/{job_id}/results")
    # may be 200 with empty rows or 404 if ownership/availability differs in test env
    if r3.status_code == 200:
        assert r3.headers.get("Cache-Control") == "no-store"

