SNIPPET = b"5.2 Invoices shall be payable within ___ days from the date of invoice." def test_full_ingest_resolve_events_flow(client, fake_llm): fake_llm.token_count_value = 5000 ingest = client.post( "/api/v1/ingest", files={"file": ("contract.txt", SNIPPET, "text/plain")}, ) assert ingest.status_code == 200, ingest.text body = ingest.json() session_id = body["session_id"] assert body["token_count"] == 5000 assert body["missing_variables"][0]["field_id"] == "invoice_due_days" assert body["missing_variables"][0]["raw_text_snippet"].endswith( "from the date of invoice" ) # provenance on the API surface: a blank field carries no pre-fill value, only an estimate assert body["missing_variables"][0]["current_value"] == "" assert body["missing_variables"][0]["suggested_fallback"] == "30" # unit rides alongside so the bare number isn't ambiguous assert body["missing_variables"][0]["unit"] == "days" # parties extracted for the party-reference dropdowns assert body["party_a"] == "Tata Play Limited" assert body["party_b"] == "Krishnaiah Projects Pvt Ltd" resolve = client.post( "/api/v1/resolve", json={ "session_id": session_id, "variables": {"invoice_due_days": "30", "agreement_effective_date": "2026-02-17"}, }, ) assert resolve.status_code == 200, resolve.text assert resolve.json() == {"status": "success", "events_calculated": 9} events_resp = client.get(f"/api/v1/events/{session_id}") assert events_resp.status_code == 200 body = events_resp.json() events = body["events"] assert len(events) == 9 quarterly = next(e for e in events if e["title"].startswith("Quarterly")) assert quarterly["recurrence_rule_rrule"] is None # nulled by quality gate assert quarterly["is_recurring"] is False outage = next(e for e in events if "Outage" in e["title"]) assert outage["calculated_target_date"] is None assert "4 hours" in outage["conditional_trigger_logic"] # taxonomy: mechanism tags ride on events; jurisdiction/governing law come back as metadata assert any("Penalty/Deduction" in e["tags"] for e in events) assert body["metadata"]["governing_law"] == "Laws of India" assert body["metadata"]["jurisdiction"] == "Courts at Mumbai" # blank-dependent obligation parked as pending (no fabricated date) pending = next(e for e in events if e["unresolved_dependencies"]) assert "Agreement Effective Date" in pending["unresolved_dependencies"] assert pending["calculated_target_date"] is None def test_contract_endpoint_returns_raw_text(client, fake_llm): ingest = client.post( "/api/v1/ingest", files={"file": ("contract.txt", SNIPPET, "text/plain")}, ) session_id = ingest.json()["session_id"] resp = client.get(f"/api/v1/contract/{session_id}") assert resp.status_code == 200 assert resp.json()["raw_text"] == SNIPPET.decode() def test_contract_unknown_session_404(client): assert client.get("/api/v1/contract/nope").status_code == 404 def test_small_contract_ingests(client, fake_llm): fake_llm.token_count_value = 100 resp = client.post( "/api/v1/ingest", files={"file": ("tiny.txt", b"short contract ___ blank", "text/plain")}, ) assert resp.status_code == 200, resp.text assert resp.json()["token_count"] == 100 def test_unsupported_file_type_rejected(client): resp = client.post( "/api/v1/ingest", files={"file": ("image.png", b"\x89PNG\r\n", "image/png")}, ) assert resp.status_code == 400 def test_empty_file_rejected(client): resp = client.post( "/api/v1/ingest", files={"file": ("empty.txt", b"", "text/plain")}, ) assert resp.status_code == 400 def test_resolve_unknown_session_404(client): resp = client.post( "/api/v1/resolve", json={"session_id": "does-not-exist", "variables": {}}, ) assert resp.status_code == 404 def test_events_unknown_session_404(client): resp = client.get("/api/v1/events/does-not-exist") assert resp.status_code == 404 def test_scanned_pdf_ingests_via_vision_fallback(client, fake_llm): """A PDF with no text layer must not 400 — it OCRs via Gemini vision, then discovery runs.""" from fpdf import FPDF pdf = FPDF() pdf.add_page() # blank page, no text layer data = bytes(pdf.output()) resp = client.post( "/api/v1/ingest", files={"file": ("scan.pdf", data, "application/pdf")}, ) assert resp.status_code == 200, resp.text assert resp.json()["missing_variables"] # discovery ran on the transcribed text