# ============================================================================= # Projet : AIR — Accounting Intermediate Representation # Auteur : Simon-Pierre Boucher # Contact : contact@spboucher.ai # Fichier : test_optimize.py # Description : Tests — optimization passes: fusion, netting, duplicate detection. # ============================================================================= """Optimization pass tests (Phase 6). The invariant is verified after every optimization pass by the pass manager; these tests check the transformations themselves and their provenance.""" from __future__ import annotations from datetime import date from decimal import Decimal from pathlib import Path from aic.compiler import compile_document from alsl.loader import load_policy_set from core.events import AirDocument ROOT = Path(__file__).resolve().parents[1] POLICIES = load_policy_set(ROOT / "alsl" / "policies" / "ca-qc-2026.yaml") def _doc(*events: dict) -> AirDocument: return AirDocument.model_validate({"events": list(events)}) def _payment(i: int, amount: str = "125.00") -> dict: return { "id": f"evt_pay_{i:03d}", "type": "PaymentReceived", "date": date(2026, 3, 15), "description": f"payout {i}", "amount": {"amount": amount, "currency": "CAD"}, } # --- fusion -------------------------------------------------------------------- def test_fusion_batches_identical_payments() -> None: document = _doc(*[_payment(i) for i in range(50)]) journal, diags = compile_document(document, POLICIES, optimize=True) (batch,) = journal.entries assert batch.id.startswith("je_batch_") assert batch.description == "Batch: 50 x PaymentReceived" debit = next(l for l in batch.lines if l.side.value == "debit") assert debit.amount.amount == Decimal("125.00") * 50 # 6250.00 # traceability survives fusion: the batch line derives from all 50 originals assert debit.provenance_id is not None node = journal.provenance.get(debit.provenance_id) assert node.kind == "fusion" and len(node.inputs) == 50 assert any(d.code == "AIR-N800" for d in diags) def test_fusion_keeps_different_days_apart() -> None: events = [_payment(1), {**_payment(2), "date": date(2026, 3, 16)}] journal, _ = compile_document(_doc(*events), POLICIES, optimize=True) assert len(journal.entries) == 2 # nothing to fuse across dates def test_default_pipeline_never_fuses() -> None: document = _doc(*[_payment(i) for i in range(5)]) journal, _ = compile_document(document, POLICIES) # optimize off assert len(journal.entries) == 5 # --- netting -------------------------------------------------------------------- SALE = { "id": "evt_net_sale", "type": "Sale", "date": date(2026, 3, 1), "amount": {"amount": "1000.00", "currency": "CAD"}, "tax": {"jurisdiction": "CA-QC"}, } def _refund(amount: str) -> dict: return { "id": "evt_net_refund", "type": "Refund", "date": date(2026, 3, 5), "related_event": "evt_net_sale", "amount": {"amount": amount, "currency": "CAD"}, "tax": {"jurisdiction": "CA-QC"}, } def test_partial_refund_nets_into_one_entry() -> None: journal, diags = compile_document( _doc(SALE, _refund("250.00")), POLICIES, optimize=True) (entry,) = journal.entries assert entry.id == "je_net_evt_net_sale" amounts = {(l.account.code, l.side.value): str(l.amount.amount) for l in entry.lines} assert amounts[("4000", "credit")] == "750.00" # 1000 - 250 assert amounts[("2310", "credit")] == "37.50" # 50.00 - 12.50 assert amounts[("2320", "credit")] == "74.81" # 99.75 - 24.94 assert amounts[("1100", "debit")] == "862.31" # balances assert any(d.code == "AIR-N801" for d in diags) def test_full_refund_nets_to_nothing() -> None: journal, diags = compile_document( _doc(SALE, _refund("1000.00")), POLICIES, optimize=True) assert journal.entries == [] note = next(d for d in diags if d.code == "AIR-N801") assert "fully offset" in note.message def test_refund_exceeding_sale_is_left_alone() -> None: journal, _ = compile_document( _doc(SALE, _refund("1500.00")), POLICIES, optimize=True) assert len(journal.entries) == 2 # not nettable; both entries stay # --- duplicate detection ------------------------------------------------------------- def test_identical_content_different_ids_warns() -> None: twin_a = {**SALE, "id": "evt_dup_a"} twin_b = {**SALE, "id": "evt_dup_b"} journal, diags = compile_document(_doc(twin_a, twin_b), POLICIES, optimize=True) warning = next(d for d in diags if d.code == "AIR-W800") assert "evt_dup_a" in warning.message and "evt_dup_b" in warning.message assert len(journal.entries) == 2 # nothing dropped — a human decides def test_distinct_descriptions_are_not_duplicates() -> None: a = {**SALE, "id": "evt_a", "description": "invoice 1"} b = {**SALE, "id": "evt_b", "description": "invoice 2"} _, diags = compile_document(_doc(a, b), POLICIES, optimize=True) assert not any(d.code == "AIR-W800" for d in diags)