1
+"""D-02 acceptance: controls C-01..C-04 run as an ingestion preflight."""
2
+
3
+from __future__ import annotations
4
+
5
+from decimal import Decimal
6
+
7
+from aram_backend.domain.controls import MappingTarget
8
+from aram_backend.ledger_parser import LedgerRecord
9
+from aram_backend.services.preflight import run_preflight
10
+
11
+
12
+def _rec(row, code, opening, debit, credit, closing, general="14"):
13
+ return LedgerRecord(
14
+ row_number=row, account_code=code, general_code=general,
15
+ account_name="نمونه", source_year=1404, entry_type="O",
16
+ t1_code=None, t2_code=None, t3_code=None, t4_code=None,
17
+ opening=Decimal(opening), debit=Decimal(debit),
18
+ credit=Decimal(credit), closing=Decimal(closing),
19
+ )
20
+
21
+
22
+def test_clean_ledger_passes_c01_c02():
23
+ # Two accounts that balance: debit total == credit total, closing sums to 0.
24
+ records = [
25
+ _rec(3, "8348", 0, 100, 0, 100),
26
+ _rec(4, "8349", 0, 0, 100, -100),
27
+ ]
28
+ report = run_preflight(records)
29
+ by_code = {r.code: r for r in report.results}
30
+ assert by_code["C-01"].passed
31
+ assert by_code["C-02"].passed
32
+
33
+
34
+def test_c01_violation_blocks():
35
+ records = [_rec(3, "8348", 0, 100, 0, 999)] # closing wrong
36
+ report = run_preflight(records)
37
+ assert not report.can_approve
38
+ assert any(r.code == "C-01" and not r.passed for r in report.blocking)
39
+
40
+
41
+def test_unmapped_accounts_enter_unknown_queue():
42
+ records = [_rec(3, "8348", 0, 100, 0, 100)]
43
+ report = run_preflight(records) # no mappings
44
+ assert report.unknown_accounts == ("148348",)
45
+ assert not report.can_approve # C-03/C-04 block approval
46
+
47
+
48
+def test_mapping_clears_unknown_queue():
49
+ records = [_rec(3, "8348", 0, 100, 0, 100)]
50
+ mappings = {"148348": [MappingTarget(target="note-1")]}
51
+ report = run_preflight(records, mappings=mappings)
52
+ assert report.unknown_accounts == ()
53
+ by_code = {r.code: r for r in report.results}
54
+ assert by_code["C-03"].passed
55
+ assert by_code["C-04"].passed
56
+
57
+
58
+def test_all_four_controls_present():
59
+ report = run_preflight([_rec(3, "8348", 0, 0, 0, 0)])
60
+ assert [r.code for r in report.results] == ["C-01", "C-02", "C-03", "C-04"]
61
+
62
+
63
+def test_inactive_account_not_queued():
64
+ # All-zero account is inactive; must not enter the unknown queue.
65
+ report = run_preflight([_rec(3, "8348", 0, 0, 0, 0)])
66
+ assert report.unknown_accounts == ()