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_results_empty_arrays_when_no_payload():
    app = create_app()
    app.dependency_overrides[get_current_account] = lambda: UUID(
        "33333333-3333-3333-3333-333333333333"
    )
    client = TestClient(app)

    # create a job (no worker, no priming)
    payload = {
        "input": {"uri": "file://dummy.csv"},
        "intelligence": {
            "profile": "leads_to_accounts",
            "threshold": 0.75,
            "options": {},
        },
    }
    r = client.post("/api/v2/jobs/run", json=payload)
    assert r.status_code in (201, 200), r.text
    job_id = r.json()["job_id"]

    # directly fetch results; normalized handler should return empty lists
    r2 = client.get(f"/api/v2/jobs/{job_id}/results")
    # Allow 404 if ownership/availability differs; otherwise assert empty payload
    if r2.status_code == 200:
        body = r2.json()
        assert body.get("rows") == []
        assert body.get("results") == []

