dodil/erp-brick-purchasing
DODIL ERP brick: purchasing
tests/test_stories_bill_drafts.py
"""PUR-32: the AI step reads a supplier PDF into a DRAFT bill; the clerk decides every field."""
import base64
import hashlib
from types import SimpleNamespace
from conftest import BUYER, CLERK, SUPPLIER_A, api, must, world
PDF = b"%PDF-1.4 invoice"
B64 = base64.b64encode(PDF).decode()
SHA = hashlib.sha256(PDF).hexdigest()
def model(monkeypatch, suggestion):
from brick_purchasing.infrastructure import ai_steps
monkeypatch.setattr(ai_steps.SEAMS["ai_read_bill"], "call", lambda key, payload: SimpleNamespace(status="done", message="", output={"suggestion": suggestion, "rationale": "read from page 1", "model": "kimi-k2.6"}))
def test_pur32_the_pdf_is_kept_and_each_field_is_a_suggestion_the_clerk_decides(neighbours, monkeypatch):
world()
model(monkeypatch, {"supplier_invoice_no": {"value": "INV-5521", "evidence": "top right of page 1"}, "total": {"value": "216.00", "evidence": "bottom right"}})
d = must(api("POST", "/bill-drafts", CLERK, {"bp_id": SUPPLIER_A, "pdf_base64": B64}))
assert d["draft"] == SHA and f"raw/purchasing/bills/{SHA}.pdf" in neighbours.objects.store and d["ready_to_record"] is False
assert [f["field"] for f in d["fields"]] == ["supplier_invoice_no", "total"] and d["fields"][0]["evidence"] == "top right of page 1"
must(api("POST", f"/bill-drafts/{SHA}/fields/1/decide", CLERK, {"decision": "accepted"}))
assert api("POST", f"/bill-drafts/{SHA}/fields/2/decide", CLERK, {"decision": "corrected"})[0] == 422
d = must(api("POST", f"/bill-drafts/{SHA}/fields/2/decide", CLERK, {"decision": "corrected", "corrected_value": "261.00"}))
assert d["ready_to_record"] is True and d["fields"][1]["corrected_value"] == "261.00" and d["fields"][1]["decided_by_email"] == "clerk@shop.example"
assert must(api("POST", "/bill-drafts", CLERK, {"bp_id": SUPPLIER_A, "pdf_base64": B64}))["replay"] is True
def test_pur32_not_a_pdf_is_refused_nothing_is_recorded_as_a_bill_and_access_is_checked(neighbours, monkeypatch):
world()
model(monkeypatch, {"total": {"value": "1.00"}})
assert api("POST", "/bill-drafts", CLERK, {"bp_id": SUPPLIER_A, "pdf_base64": base64.b64encode(b"hello").decode()})[0] == 422
assert api("POST", "/bill-drafts", BUYER, {"bp_id": SUPPLIER_A, "pdf_base64": B64})[0] == 403
must(api("POST", "/bill-drafts", CLERK, {"bp_id": SUPPLIER_A, "pdf_base64": B64}))
assert api("GET", "/bills/open?as_of=2026-12-31", CLERK)[1]["items"] == []