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_payments_slice2.py · 8.4 KB · 207 lines
| 1 | """Payments slice 2 (plan/payments-spec.md): aging + dunning. |
| 2 | |
| 3 | Doctrinal split under test: paid/partial flips are EVENT-driven |
| 4 | (system_invoice_status reacts the moment money moves); overdue + dunning |
| 5 | escalation are TIME-driven (system_invoice_aging, a daemon-scheduled pass — |
| 6 | docs/logic-decisions.md #2). Dunning emails go straight to the generic |
| 7 | outbox (customer_email is a raw address, not a user id); the owner's in_app |
| 8 | ping is a seeded notify_rule (pure data). |
| 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 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 22 | |
| 23 | |
| 24 | def setup_env(tmp_path, monkeypatch, *, settings=()): |
| 25 | data_dir = tmp_path / "data" |
| 26 | for pkg, name in (("app-invoices", "invoices"), ("app-payments", "payments"), |
| 27 | ("app-payments", "refunds"), ("app-email", "email_outbox")): |
| 28 | stage_collection(data_dir, pkg, name) |
| 29 | |
| 30 | rows = "" |
| 31 | for i, (k, v) in enumerate(settings): |
| 32 | rows += f"s{i}\t{k}\t{v}\t\n" |
| 33 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 34 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 35 | return data_dir |
| 36 | |
| 37 | |
| 38 | def make_invoice(data_dir, iid="inv1", status="sent", total="10000", |
| 39 | due="2026-07-10", email="grace@acme.test", **extra): |
| 40 | rec = {"id": iid, "number": f"N-{iid}", "customer_name": "Acme", |
| 41 | "customer_email": email, "status": status, "due_date": due, |
| 42 | "total_cents": total, "owner_id": "dan"} |
| 43 | rec.update(extra) |
| 44 | return object_records.create_collection_record("invoices", rec, base_dir=data_dir) |
| 45 | |
| 46 | |
| 47 | def pay(data_dir, pid, invoice_id, cents, status="received"): |
| 48 | return object_records.create_collection_record( |
| 49 | "payments", |
| 50 | {"id": pid, "invoice_id": invoice_id, "amount_cents": cents, |
| 51 | "method": "card", "received_on": "2026-07-09", "status": status, |
| 52 | "owner_id": "dan"}, |
| 53 | base_dir=data_dir) |
| 54 | |
| 55 | |
| 56 | def fire_status(collection, record_id, action_raw): |
| 57 | return object_execution.execute_object( |
| 58 | RUNTIME, |
| 59 | object_execution.ObjectExecutionRequest( |
| 60 | "system_invoice_status", method="EVENT", |
| 61 | payload={"event": f"{collection}.record.{action_raw}d", |
| 62 | "collection": collection, "record_id": record_id, |
| 63 | "action": action_raw}), |
| 64 | roots=[PACKAGES / "app-payments" / "objects"]) |
| 65 | |
| 66 | |
| 67 | def run_aging(today): |
| 68 | return object_execution.execute_object( |
| 69 | RUNTIME, |
| 70 | object_execution.ObjectExecutionRequest( |
| 71 | "system_invoice_aging", method="POST", payload={"today": today}), |
| 72 | roots=[PACKAGES / "app-invoices" / "objects"]) |
| 73 | |
| 74 | |
| 75 | def invoice(data_dir, iid="inv1"): |
| 76 | return object_records.get_collection_record("invoices", iid, base_dir=data_dir) |
| 77 | |
| 78 | |
| 79 | def outbox(data_dir): |
| 80 | return object_records.read_collection_records("email_outbox", base_dir=data_dir) |
| 81 | |
| 82 | |
| 83 | def test_status_flips_partial_then_paid_then_back_on_refund(tmp_path, monkeypatch): |
| 84 | data_dir = setup_env(tmp_path, monkeypatch) |
| 85 | make_invoice(data_dir) |
| 86 | pay(data_dir, "p1", "inv1", "4000") |
| 87 | fire_status("payments", "p1", "create") |
| 88 | assert invoice(data_dir)["status"] == "partial" |
| 89 | |
| 90 | pay(data_dir, "p2", "inv1", "6000") |
| 91 | fire_status("payments", "p2", "create") |
| 92 | assert invoice(data_dir)["status"] == "paid" |
| 93 | |
| 94 | object_records.create_collection_record( |
| 95 | "refunds", {"id": "r1", "payment_id": "p2", "invoice_id": "inv1", |
| 96 | "amount_cents": "2000", "refunded_on": "2026-07-11", |
| 97 | "owner_id": "dan"}, base_dir=data_dir) |
| 98 | fire_status("refunds", "r1", "create") |
| 99 | assert invoice(data_dir)["status"] == "partial" |
| 100 | |
| 101 | |
| 102 | def test_status_never_touches_draft_or_void(tmp_path, monkeypatch): |
| 103 | data_dir = setup_env(tmp_path, monkeypatch) |
| 104 | make_invoice(data_dir, iid="invd", status="draft") |
| 105 | pay(data_dir, "p1", "invd", "10000") |
| 106 | fire_status("payments", "p1", "create") |
| 107 | assert invoice(data_dir, "invd")["status"] == "draft" |
| 108 | |
| 109 | |
| 110 | def test_aging_flips_overdue_dunns_and_respects_grace(tmp_path, monkeypatch): |
| 111 | data_dir = setup_env(tmp_path, monkeypatch, |
| 112 | settings=(("payments.grace_days", "3"),)) |
| 113 | make_invoice(data_dir) # due 2026-07-10, unpaid |
| 114 | |
| 115 | # Inside grace: due+3 = 07-13; today 07-12 -> untouched. |
| 116 | run_aging("2026-07-12") |
| 117 | assert invoice(data_dir)["status"] == "sent" |
| 118 | assert outbox(data_dir) == [] |
| 119 | |
| 120 | # Past grace -> overdue, level 1, dunning email queued to the customer. |
| 121 | result = run_aging("2026-07-14") |
| 122 | assert result.result["flipped_overdue"] == 1 |
| 123 | row = invoice(data_dir) |
| 124 | assert row["status"] == "overdue" |
| 125 | assert row["dunning_level"] == "1" |
| 126 | assert row["last_dunned_on"] == "2026-07-14" |
| 127 | mails = outbox(data_dir) |
| 128 | assert len(mails) == 1 |
| 129 | assert mails[0]["to"] == "grace@acme.test" |
| 130 | assert "overdue" in mails[0]["subject"] |
| 131 | assert mails[0]["status"] == "queued" |
| 132 | |
| 133 | |
| 134 | def test_dunning_escalates_on_schedule_and_stops_at_max(tmp_path, monkeypatch): |
| 135 | data_dir = setup_env(tmp_path, monkeypatch, |
| 136 | settings=(("payments.dunning_repeat_days", "7"), |
| 137 | ("payments.dunning_max_level", "3"))) |
| 138 | make_invoice(data_dir) |
| 139 | run_aging("2026-07-11") # -> overdue, level 1 |
| 140 | run_aging("2026-07-12") # too soon: no escalation |
| 141 | assert invoice(data_dir)["dunning_level"] == "1" |
| 142 | run_aging("2026-07-18") # +7d -> level 2 |
| 143 | assert invoice(data_dir)["dunning_level"] == "2" |
| 144 | run_aging("2026-07-25") # +7d -> level 3 (max) |
| 145 | assert invoice(data_dir)["dunning_level"] == "3" |
| 146 | run_aging("2026-08-10") # capped: stays 3 |
| 147 | assert invoice(data_dir)["dunning_level"] == "3" |
| 148 | assert len(outbox(data_dir)) == 3 # one email per level |
| 149 | |
| 150 | |
| 151 | def test_aging_clears_overdue_when_balance_paid_out_of_band(tmp_path, monkeypatch): |
| 152 | data_dir = setup_env(tmp_path, monkeypatch) |
| 153 | make_invoice(data_dir) |
| 154 | run_aging("2026-07-11") |
| 155 | assert invoice(data_dir)["status"] == "overdue" |
| 156 | pay(data_dir, "p1", "inv1", "10000") # storage-level: no event fired |
| 157 | result = run_aging("2026-07-12") |
| 158 | assert invoice(data_dir)["status"] == "paid" |
| 159 | assert result.result["flipped_overdue"] == 0 |
| 160 | |
| 161 | |
| 162 | def test_aging_skips_paid_void_and_no_due_date(tmp_path, monkeypatch): |
| 163 | data_dir = setup_env(tmp_path, monkeypatch) |
| 164 | make_invoice(data_dir, iid="a", status="paid") |
| 165 | make_invoice(data_dir, iid="b", status="void") |
| 166 | make_invoice(data_dir, iid="c", status="sent", due="") |
| 167 | run_aging("2026-08-01") |
| 168 | assert invoice(data_dir, "a")["status"] == "paid" |
| 169 | assert invoice(data_dir, "b")["status"] == "void" |
| 170 | assert invoice(data_dir, "c")["status"] == "sent" |
| 171 | assert outbox(data_dir) == [] |
| 172 | |
| 173 | |
| 174 | def test_dunning_without_customer_email_still_escalates(tmp_path, monkeypatch): |
| 175 | data_dir = setup_env(tmp_path, monkeypatch) |
| 176 | make_invoice(data_dir, email="") |
| 177 | result = run_aging("2026-07-11") |
| 178 | assert invoice(data_dir)["status"] == "overdue" |
| 179 | assert outbox(data_dir) == [] |
| 180 | assert result.result["results"][0]["emailed"] is False |
| 181 | |
| 182 | |
| 183 | def test_owner_notify_rule_seed_matches_the_overdue_change(tmp_path, monkeypatch): |
| 184 | """The dunning notify seed is pure data — prove it fires with the real |
| 185 | notify engine against the change our aging pass produces.""" |
| 186 | import object_notify |
| 187 | data_dir = setup_env(tmp_path, monkeypatch) |
| 188 | make_invoice(data_dir) |
| 189 | run_aging("2026-07-11") |
| 190 | row = invoice(data_dir) |
| 191 | |
| 192 | seed = (PACKAGES / "app-invoices" / "seed" / "notify_rules.tsv").read_text().splitlines() |
| 193 | header = seed[0].split("\t") |
| 194 | import csv, io |
| 195 | parsed = next(csv.DictReader(io.StringIO("\n".join(seed)), delimiter="\t")) |
| 196 | rule = dict(parsed) |
| 197 | # Mirror a REAL record-change entry: changed_fields present (the notify |
| 198 | # engine's transition semantics — a match condition must be on a field the |
| 199 | # write actually changed, so the rule fires once per transition, not on |
| 200 | # every later edit of an overdue invoice). |
| 201 | change = {"collection": "invoices", "record_id": "inv1", |
| 202 | "action": "update", "after": row, "actor": "system_invoice_aging", |
| 203 | "changed_fields": ["status", "dunning_level", "last_dunned_on"]} |
| 204 | notes = object_notify.notifications_for_change(rule, change, base_dir=data_dir) |
| 205 | assert len(notes) == 1 |
| 206 | assert notes[0]["user_id"] == "dan" |
| 207 | assert "overdue" in notes[0]["body"] |