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_run_dedupe_idempotency_returns_same_job_id():
    app = create_app()
    acct = UUID("44444444-4444-4444-4444-444444444444")
    app.dependency_overrides[get_current_account] = lambda: acct
    client = TestClient(app)

    idem = "dedupe-idem-key-1"
    payload = {
        "mode": "dedupe",
        "input": {"uri": "file://dummy_dedupe.csv"},
        "intelligence": {"profile": "dedupe", "threshold": 0.82, "options": {"blocking": ["domain", "name"]}},
    }

    r1 = client.post("/api/v2/jobs/run", headers={"Idempotency-Key": idem}, json=payload)
    assert r1.status_code in (201, 200), r1.text
    j1 = r1.json()["job_id"]

    r2 = client.post("/api/v2/jobs/run", headers={"Idempotency-Key": idem}, json=payload)
    assert r2.status_code in (200, 201), r2.text
    j2 = r2.json()["job_id"]

    assert j1 == j2

