askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
danq9
signup: the person chooses the password and name on the confirm page
83c7aa6
2 weeks, 5 days ago
History
test_bank_escalation.py · 11.1 KB · 279 lines
| 1 | """system_bank_escalation (plan/bank-import-reconciliation-spec.md section 5, |
| 2 | "THE SIGNAL"): the runner that turns a quietly-growing unreconciled tail -- |
| 3 | in EITHER direction -- into a task someone is accountable for. |
| 4 | |
| 5 | The control being tested is not "does it flag a stale line" but "does it |
| 6 | ever flag the same item twice, and does it also catch the direction people |
| 7 | forget" (a book payment the bank never confirmed, which is the more |
| 8 | serious of the two per the spec). |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | import pathlib |
| 13 | |
| 14 | import object_execution |
| 15 | import object_records |
| 16 | import python_object_runtime |
| 17 | from conftest import stage_collection |
| 18 | |
| 19 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 20 | PACKAGES = REPO_ROOT / "packages" |
| 21 | BANKING_OBJECTS = PACKAGES / "app-banking" / "objects" |
| 22 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 23 | |
| 24 | ACCOUNT = "bank-1" |
| 25 | DAN = {"user_id": "dan"} |
| 26 | |
| 27 | |
| 28 | def setup_env(tmp_path, monkeypatch, *, settings=(), with_tasks=True): |
| 29 | data_dir = tmp_path / "data" |
| 30 | wanted = [("app-banking", "value_accounts"), ("app-banking", "bank_import_profiles"), |
| 31 | ("app-banking", "bank_statement_imports"), ("app-banking", "bank_lines"), |
| 32 | ("app-finance", "fin_accounts"), |
| 33 | ("app-payments", "payments"), ("app-invoices", "invoices")] |
| 34 | if with_tasks: |
| 35 | wanted.append(("app-tasks", "tasks")) |
| 36 | for pkg, name in wanted: |
| 37 | stage_collection(data_dir, pkg, name) |
| 38 | |
| 39 | rows = "" |
| 40 | for i, (k, v) in enumerate(settings): |
| 41 | rows += f"s{i}\t{k}\t{v}\t\n" |
| 42 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 43 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 44 | |
| 45 | object_records.create_collection_record( |
| 46 | "fin_accounts", {"id": "acct-cash", "name": "Cash", "account_type": "asset", |
| 47 | "owner_id": "dan"}, base_dir=data_dir) |
| 48 | object_records.create_collection_record( |
| 49 | "value_accounts", {"id": ACCOUNT, "name": "Checking", "fin_account_id": "acct-cash", |
| 50 | "owner_id": "dan"}, base_dir=data_dir) |
| 51 | return data_dir |
| 52 | |
| 53 | |
| 54 | def make_line(data_dir, lid, *, amount, on="2026-07-01", desc="", status="unmatched", |
| 55 | matched_to="", account=ACCOUNT, **extra): |
| 56 | rec = {"id": lid, "bank_account_id": account, "posted_on": on, |
| 57 | "amount_cents": str(amount), "description": desc, "raw": f"raw:{desc}", |
| 58 | "line_hash": f"h-{lid}", "match_status": status, "matched_to": matched_to, |
| 59 | "owner_id": "dan"} |
| 60 | rec.update(extra) |
| 61 | return object_records.create_collection_record("bank_lines", rec, base_dir=data_dir) |
| 62 | |
| 63 | |
| 64 | def make_invoice(data_dir, iid="inv1"): |
| 65 | return object_records.create_collection_record( |
| 66 | "invoices", {"id": iid, "number": "N-1", "customer_name": "Acme", |
| 67 | "status": "sent", "total_cents": "500000", "owner_id": "dan"}, |
| 68 | base_dir=data_dir) |
| 69 | |
| 70 | |
| 71 | def make_payment(data_dir, pid, *, cents, on="2026-07-01", status="received", |
| 72 | invoice="inv1", reference=""): |
| 73 | return object_records.create_collection_record( |
| 74 | "payments", |
| 75 | {"id": pid, "invoice_id": invoice, "amount_cents": str(cents), "method": "card", |
| 76 | "received_on": on, "reference": reference, "status": status, "owner_id": "dan"}, |
| 77 | base_dir=data_dir) |
| 78 | |
| 79 | |
| 80 | def run_object(object_id, payload): |
| 81 | return object_execution.execute_object( |
| 82 | RUNTIME, |
| 83 | object_execution.ObjectExecutionRequest( |
| 84 | object_id, method="POST", payload={"_identity": DAN, **payload}), |
| 85 | roots=[BANKING_OBJECTS]).result |
| 86 | |
| 87 | |
| 88 | def tasks(data_dir): |
| 89 | return object_records.read_collection_records("tasks", base_dir=data_dir) |
| 90 | |
| 91 | |
| 92 | def markers(data_dir): |
| 93 | out = [] |
| 94 | for t in tasks(data_dir): |
| 95 | meta = json.loads(t["metadata"]) if t.get("metadata") else {} |
| 96 | out.append(meta.get("generated_from")) |
| 97 | return out |
| 98 | |
| 99 | |
| 100 | TODAY = "2026-07-20" # 19 days after the fixtures' default posted/received date |
| 101 | |
| 102 | |
| 103 | # --- direction 1: bank lines the books cannot explain ------------------------- |
| 104 | |
| 105 | def test_stale_unmatched_line_escalates_once(tmp_path, monkeypatch): |
| 106 | data_dir = setup_env(tmp_path, monkeypatch) |
| 107 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="ACME CORP DEPOSIT") |
| 108 | |
| 109 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 110 | assert result["ok"] is True |
| 111 | assert result["scanned"] == 1 |
| 112 | assert result["escalated"] == 1 |
| 113 | assert result["already_open"] == 0 |
| 114 | assert len(tasks(data_dir)) == 1 |
| 115 | assert markers(data_dir) == ["bank_lines/L1"] |
| 116 | |
| 117 | # A second run must not create a second task for the same line. |
| 118 | result2 = run_object("system_bank_escalation", {"today": TODAY}) |
| 119 | assert result2["escalated"] == 0 |
| 120 | assert result2["already_open"] == 1 |
| 121 | assert len(tasks(data_dir)) == 1 |
| 122 | |
| 123 | |
| 124 | def test_recent_line_does_not_escalate(tmp_path, monkeypatch): |
| 125 | data_dir = setup_env(tmp_path, monkeypatch) |
| 126 | make_line(data_dir, "L1", amount=150000, on="2026-07-18", desc="DEPOSIT") # 2 days old |
| 127 | |
| 128 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 129 | assert result["scanned"] == 1 |
| 130 | assert result["escalated"] == 0 |
| 131 | assert tasks(data_dir) == [] |
| 132 | |
| 133 | |
| 134 | def test_matched_or_resolved_line_never_escalates(tmp_path, monkeypatch): |
| 135 | data_dir = setup_env(tmp_path, monkeypatch) |
| 136 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="DEPOSIT", |
| 137 | status="matched", matched_to="payments/p9") |
| 138 | make_line(data_dir, "L2", amount=-2500, on="2026-07-01", desc="FEE", |
| 139 | status="resolved") |
| 140 | |
| 141 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 142 | assert result["scanned"] == 0 |
| 143 | assert result["escalated"] == 0 |
| 144 | assert tasks(data_dir) == [] |
| 145 | |
| 146 | |
| 147 | def test_escalate_after_days_is_honoured(tmp_path, monkeypatch): |
| 148 | data_dir = setup_env(tmp_path, monkeypatch, |
| 149 | settings=(("reconcile.escalate_after_days", "30"),)) |
| 150 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="DEPOSIT") # 19 days old |
| 151 | |
| 152 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 153 | assert result["after_days"] == 30 |
| 154 | assert result["escalated"] == 0 |
| 155 | assert tasks(data_dir) == [] |
| 156 | |
| 157 | result = run_object("system_bank_escalation", {"today": "2026-08-05"}) # 35 days old |
| 158 | assert result["escalated"] == 1 |
| 159 | |
| 160 | |
| 161 | def test_escalation_can_be_disabled(tmp_path, monkeypatch): |
| 162 | data_dir = setup_env(tmp_path, monkeypatch, |
| 163 | settings=(("reconcile.escalation_enabled", "false"),)) |
| 164 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="DEPOSIT") |
| 165 | |
| 166 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 167 | assert result["ok"] is True |
| 168 | assert "skipped" in result |
| 169 | assert tasks(data_dir) == [] |
| 170 | |
| 171 | |
| 172 | # --- direction 2: book records the bank never confirmed ----------------------- |
| 173 | |
| 174 | def test_unconfirmed_payment_escalates(tmp_path, monkeypatch): |
| 175 | data_dir = setup_env(tmp_path, monkeypatch) |
| 176 | make_invoice(data_dir) |
| 177 | make_payment(data_dir, "p1", cents=150000, on="2026-07-01", reference="INV-1001") |
| 178 | |
| 179 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 180 | assert result["scanned"] == 1 |
| 181 | assert result["escalated"] == 1 |
| 182 | assert markers(data_dir) == ["payments/p1"] |
| 183 | |
| 184 | result2 = run_object("system_bank_escalation", {"today": TODAY}) |
| 185 | assert result2["escalated"] == 0 |
| 186 | assert result2["already_open"] == 1 |
| 187 | assert len(tasks(data_dir)) == 1 |
| 188 | |
| 189 | |
| 190 | def test_payment_confirmed_by_a_bank_line_never_escalates(tmp_path, monkeypatch): |
| 191 | data_dir = setup_env(tmp_path, monkeypatch) |
| 192 | make_invoice(data_dir) |
| 193 | make_payment(data_dir, "p1", cents=150000, on="2026-07-01", reference="INV-1001") |
| 194 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="PAYMENT INV-1001", |
| 195 | status="matched", matched_to="payments/p1") |
| 196 | |
| 197 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 198 | assert result["escalated"] == 0 |
| 199 | assert tasks(data_dir) == [] |
| 200 | |
| 201 | |
| 202 | def test_bounced_payment_never_escalates(tmp_path, monkeypatch): |
| 203 | data_dir = setup_env(tmp_path, monkeypatch) |
| 204 | make_invoice(data_dir) |
| 205 | make_payment(data_dir, "p1", cents=150000, on="2026-07-01", status="bounced") |
| 206 | |
| 207 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 208 | assert result["escalated"] == 0 |
| 209 | assert tasks(data_dir) == [] |
| 210 | |
| 211 | |
| 212 | # --- graceful degradation ------------------------------------------------------ |
| 213 | |
| 214 | def test_missing_tasks_collection_is_skipped_not_fatal(tmp_path, monkeypatch): |
| 215 | data_dir = setup_env(tmp_path, monkeypatch, with_tasks=False) |
| 216 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="DEPOSIT") |
| 217 | |
| 218 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 219 | assert result["ok"] is True |
| 220 | assert "skipped" in result |
| 221 | |
| 222 | |
| 223 | def test_escalation_is_a_no_op_without_banking_data(tmp_path, monkeypatch): |
| 224 | data_dir = setup_env(tmp_path, monkeypatch) |
| 225 | result = run_object("system_bank_escalation", {"today": TODAY}) |
| 226 | assert result["ok"] is True |
| 227 | assert result["scanned"] == 0 |
| 228 | assert result["escalated"] == 0 |
| 229 | |
| 230 | |
| 231 | # --- the task itself is actionable without opening the record ----------------- |
| 232 | |
| 233 | def test_escalated_task_names_amount_date_and_description(tmp_path, monkeypatch): |
| 234 | data_dir = setup_env(tmp_path, monkeypatch) |
| 235 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="ACME CORP DEPOSIT") |
| 236 | |
| 237 | run_object("system_bank_escalation", {"today": TODAY}) |
| 238 | task = tasks(data_dir)[0] |
| 239 | assert "150000" in task["title"] and "150000" in task["description"] |
| 240 | assert "2026-07-01" in task["title"] and "2026-07-01" in task["description"] |
| 241 | assert "ACME CORP DEPOSIT" in task["title"] |
| 242 | assert task["owner_id"] == "dan" |
| 243 | assert task["urgency"] == "high" |
| 244 | |
| 245 | |
| 246 | def test_escalated_payment_task_names_amount_date_and_invoice(tmp_path, monkeypatch): |
| 247 | data_dir = setup_env(tmp_path, monkeypatch) |
| 248 | make_invoice(data_dir) |
| 249 | make_payment(data_dir, "p1", cents=250000, on="2026-07-01", reference="REF-9") |
| 250 | |
| 251 | run_object("system_bank_escalation", {"today": TODAY}) |
| 252 | task = tasks(data_dir)[0] |
| 253 | assert "250000" in task["title"] and "250000" in task["description"] |
| 254 | assert "2026-07-01" in task["title"] and "2026-07-01" in task["description"] |
| 255 | assert "inv1" in task["title"] |
| 256 | assert task["urgency"] == "critical" |
| 257 | |
| 258 | |
| 259 | def test_escalate_to_setting_overrides_the_record_owner(tmp_path, monkeypatch): |
| 260 | data_dir = setup_env(tmp_path, monkeypatch, |
| 261 | settings=(("reconcile.escalate_to", "controller"),)) |
| 262 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="DEPOSIT") |
| 263 | |
| 264 | run_object("system_bank_escalation", {"today": TODAY}) |
| 265 | assert tasks(data_dir)[0]["owner_id"] == "controller" |
| 266 | |
| 267 | |
| 268 | def test_bank_account_filter_scopes_the_scan(tmp_path, monkeypatch): |
| 269 | data_dir = setup_env(tmp_path, monkeypatch) |
| 270 | object_records.create_collection_record( |
| 271 | "value_accounts", {"id": "bank-2", "name": "Savings", "fin_account_id": "acct-cash", |
| 272 | "owner_id": "dan"}, base_dir=data_dir) |
| 273 | make_line(data_dir, "L1", amount=150000, on="2026-07-01", desc="A", account=ACCOUNT) |
| 274 | make_line(data_dir, "L2", amount=90000, on="2026-07-01", desc="B", account="bank-2") |
| 275 | |
| 276 | result = run_object("system_bank_escalation", {"today": TODAY, "bank_account_id": ACCOUNT}) |
| 277 | assert result["scanned"] == 1 |
| 278 | assert result["escalated"] == 1 |
| 279 | assert markers(data_dir) == ["bank_lines/L1"] |