# TANRAIL ONE — Phase 4.0 Verification Strategy & Test Matrix

## 1. Automated Adversarial Test Matrix

When Phase 4.1 implementation is authorized, a dedicated test suite `tests/Feature/Core/Phase4InvoicingTest.php` must be created containing adversarial tests covering all security, financial, and state machine invariants:

| Category | Test Scenario | Expected Outcome |
|---|---|---|
| **Multi-Tenancy** | Allocate Org 1 Payment to Org 2 Invoice | Throws `CrossOrganizationException` |
| **Multi-Tenancy** | Allocate BU 1 Payment to BU 2 Invoice | Throws `CrossBusinessUnitException` |
| **Immutability** | Update `subtotal` or `total` on `ISSUED` Invoice | Throws `JournalImmutableException` |
| **Immutability** | Delete line item from `APPROVED` Supplier Invoice | Throws `JournalImmutableException` |
| **State Machine** | Transition Invoice from `ISSUED` to `DRAFT` | Throws `InvalidStateTransitionException` |
| **State Machine** | Transition `PAID` Invoice directly to `VOIDED` | Throws `InvalidStateTransitionException` |
| **Allocation** | Allocate amount exceeding Invoice `balance_due` | Throws `InvalidAllocationException` |
| **Allocation** | Allocate amount exceeding Payment `unallocated_amount` | Throws `InvalidAllocationException` |
| **Concurrency** | Concurrent allocations against same Payment balance | Pessimistic lock holds; second allocation fails safely |
| **3-Way Match** | Approve Supplier Bill where `Billed Qty > Received Qty` | Flagged as `DISCREPANCY`; GL posting blocked |
| **3-Way Match** | Approve Supplier Bill where unit price exceeds PO price tolerance | Flagged as `DISCREPANCY`; GL posting blocked |
| **Period Lock** | Issue Invoice with date in closed `FiscalPeriod` | Throws `FiscalPeriodClosedException`; transaction rolled back |
| **Idempotency** | Re-dispatch `InvoiceIssued` domain event | Exactly 1 GL Journal created; second dispatch ignored |
| **Reversal** | Issue `CreditNote` against `ISSUED` Invoice | Revenue & Tax reversed; AR balance reduced; GL balanced |
| **Boundary Scan** | Scan Phase 4 codebase for forbidden industry jargon | Zero occurrences of `Restaurant`, `POS`, `Kitchen`, `Menu`, etc. |

---

## 2. System-Wide Regression Requirements
- **Baseline Test Suite**: All 142 existing tests across Phases 1–3.4 must pass with 0 failures, 0 errors, and 0 skipped.
- **Code Formatting**: Clean execution of `vendor/bin/pint --format agent`.
- **Architectural Boundary Verification**: `Phase34BoundaryTest` must pass cleanly.
