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.
test_billing_runner.py · 10.2 KB · 237 lines
| 1 | """The period biller: decide money is owed, say so exactly once. |
| 2 | |
| 3 | Double-billing is the failure customers never forgive, so the property |
| 4 | under test hardest here is idempotency -- a biller re-run after a crash, |
| 5 | or simply run twice in a day, must raise nothing the second time. The |
| 6 | other half is the status ladder, which has to climb in BOTH directions: |
| 7 | a ladder that only descends leaves a customer who just paid still cut |
| 8 | off. |
| 9 | """ |
| 10 | |
| 11 | import pathlib |
| 12 | |
| 13 | from conftest import stage_collection |
| 14 | |
| 15 | import object_execution |
| 16 | import object_records |
| 17 | import python_object_runtime |
| 18 | |
| 19 | REPO_ROOT = pathlib.Path(__file__).resolve().parents[1] |
| 20 | PACKAGES = REPO_ROOT / "packages" |
| 21 | BILLING_OBJECTS = PACKAGES / "app-billing" / "objects" |
| 22 | RUNTIME = python_object_runtime.PythonObjectRuntime() |
| 23 | |
| 24 | |
| 25 | def setup_env(tmp_path, monkeypatch, *, settings=()): |
| 26 | data_dir = tmp_path / "data" |
| 27 | for pkg, name in (("app-billing", "billing_plans"), ("app-billing", "subscriptions"), |
| 28 | ("app-billing", "wallets"), ("app-billing", "wallet_entries"), |
| 29 | ("app-invoices", "invoices"), ("app-invoices", "invoice_lines"), |
| 30 | ("app-finance", "denominations")): |
| 31 | stage_collection(data_dir, pkg, name, seed=(name == "denominations")) |
| 32 | rows = "" |
| 33 | for i, (k, v) in enumerate(settings): |
| 34 | rows += f"s{i}\t{k}\t{v}\t\n" |
| 35 | stage_collection(data_dir, "app-settings", "app_settings", rows=rows) |
| 36 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 37 | object_records.create_collection_record( |
| 38 | "billing_plans", |
| 39 | {"id": "plan-pro", "name": "Pro", "code": "pro", "period": "month", |
| 40 | "base_minor": "4900", "is_active": "true", "owner_id": "dan"}, |
| 41 | base_dir=data_dir) |
| 42 | return data_dir |
| 43 | |
| 44 | |
| 45 | def subscribe(data_dir, sid="sub-1", **fields): |
| 46 | record = {"id": sid, "customer_name": "Acme Ltd", "customer_email": "a@acme.test", |
| 47 | "plan_id": "plan-pro", "billing_mode": "subscription", "status": "active", |
| 48 | "current_period_start": "2026-06-01", "current_period_end": "2026-06-30", |
| 49 | "owner_id": "dan"} |
| 50 | record.update(fields) |
| 51 | return object_records.create_collection_record( |
| 52 | "subscriptions", record, base_dir=data_dir) |
| 53 | |
| 54 | |
| 55 | def run(payload=None): |
| 56 | return object_execution.execute_object( |
| 57 | RUNTIME, |
| 58 | object_execution.ObjectExecutionRequest( |
| 59 | "system_billing_runner", method="POST", payload=payload or {}), |
| 60 | roots=[BILLING_OBJECTS]).result |
| 61 | |
| 62 | |
| 63 | def invoices(data_dir): |
| 64 | return object_records.read_collection_records("invoices", base_dir=data_dir) |
| 65 | |
| 66 | |
| 67 | def sub(data_dir, sid="sub-1"): |
| 68 | return object_records.get_collection_record("subscriptions", sid, base_dir=data_dir) |
| 69 | |
| 70 | |
| 71 | # --- raising the invoice -------------------------------------------------------- |
| 72 | |
| 73 | def test_the_base_fee_is_billed_in_advance_and_a_close_bills_the_next_period(tmp_path, monkeypatch): |
| 74 | """Decision 4 (owner, 2026-11-30): the base fee is billed in advance, |
| 75 | usage in arrears. A period that has begun with its base fee unbilled is |
| 76 | billed at once; closing it bills the NEXT period's base fee (and the |
| 77 | closed period's usage). It used to bill each period's base fee after |
| 78 | the period had been used.""" |
| 79 | data_dir = setup_env(tmp_path, monkeypatch) |
| 80 | subscribe(data_dir) |
| 81 | opened = run({"today": "2026-06-01"}) |
| 82 | assert opened["invoiced"] == 1 and opened["advanced"] == 0 |
| 83 | [june] = invoices(data_dir) |
| 84 | assert june["total_cents"] == "4900" and june["status"] == "sent" |
| 85 | assert june["customer_email"] == "a@acme.test" |
| 86 | [line] = object_records.read_collection_records("invoice_lines", base_dir=data_dir) |
| 87 | assert (line["service_start"], line["service_end"]) == ("2026-06-01", "2026-06-30") |
| 88 | |
| 89 | result = run({"today": "2026-07-01"}) |
| 90 | assert result["invoiced"] == 1 and result["advanced"] == 1 |
| 91 | rows = invoices(data_dir) |
| 92 | assert len(rows) == 2 |
| 93 | closing = next(row for row in rows if row["id"] != june["id"]) |
| 94 | assert closing["total_cents"] == "4900" |
| 95 | lines = [row for row in object_records.read_collection_records( |
| 96 | "invoice_lines", base_dir=data_dir) if row["invoice_id"] == closing["id"]] |
| 97 | assert len(lines) == 1 and lines[0]["line_total_cents"] == "4900" |
| 98 | assert "2026-06-30" in lines[0]["description"] # the period it covers: the next |
| 99 | assert (lines[0]["service_start"], lines[0]["service_end"]) == ("2026-06-30", "2026-07-30") |
| 100 | |
| 101 | moved = sub(data_dir) |
| 102 | assert moved["current_period_start"] == "2026-06-30" |
| 103 | assert moved["current_period_end"] == "2026-07-30" |
| 104 | |
| 105 | |
| 106 | def test_running_twice_never_double_bills(tmp_path, monkeypatch): |
| 107 | """The property customers never forgive us for getting wrong.""" |
| 108 | data_dir = setup_env(tmp_path, monkeypatch) |
| 109 | subscribe(data_dir) |
| 110 | run({"today": "2026-07-01"}) |
| 111 | # Re-run the SAME day (a crashed pass, a manual retry, a double cron). |
| 112 | again = run({"today": "2026-07-01"}) |
| 113 | assert again["invoiced"] == 0 |
| 114 | assert len(invoices(data_dir)) == 2 # June's base (late) and July's |
| 115 | |
| 116 | |
| 117 | def test_a_period_still_open_bills_only_its_base_fee_once(tmp_path, monkeypatch): |
| 118 | data_dir = setup_env(tmp_path, monkeypatch) |
| 119 | subscribe(data_dir) |
| 120 | result = run({"today": "2026-06-15"}) |
| 121 | assert result["invoiced"] == 1 and result["advanced"] == 0 |
| 122 | assert run({"today": "2026-06-16"})["invoiced"] == 0 |
| 123 | assert [row["total_cents"] for row in invoices(data_dir)] == ["4900"] |
| 124 | |
| 125 | |
| 126 | def test_a_period_not_yet_begun_bills_nothing(tmp_path, monkeypatch): |
| 127 | data_dir = setup_env(tmp_path, monkeypatch) |
| 128 | subscribe(data_dir) |
| 129 | result = run({"today": "2026-05-31"}) |
| 130 | assert result["invoiced"] == 0 and invoices(data_dir) == [] |
| 131 | |
| 132 | |
| 133 | def test_wallet_mode_gets_no_periodic_invoice(tmp_path, monkeypatch): |
| 134 | """Its money already moved when the usage did; invoicing again would |
| 135 | charge twice for one thing.""" |
| 136 | data_dir = setup_env(tmp_path, monkeypatch) |
| 137 | subscribe(data_dir, billing_mode="wallet") |
| 138 | result = run({"today": "2026-07-01"}) |
| 139 | assert result["invoiced"] == 0 and invoices(data_dir) == [] |
| 140 | |
| 141 | |
| 142 | def test_dry_run_says_what_it_would_do_and_writes_nothing(tmp_path, monkeypatch): |
| 143 | data_dir = setup_env(tmp_path, monkeypatch) |
| 144 | subscribe(data_dir) |
| 145 | result = run({"today": "2026-07-01", "dry_run": "true"}) |
| 146 | assert invoices(data_dir) == [] |
| 147 | assert any(r.get("would_invoice_period") for r in result["results"]) |
| 148 | assert sub(data_dir)["current_period_end"] == "2026-06-30" # unmoved |
| 149 | |
| 150 | |
| 151 | def test_month_end_clamps_instead_of_skipping_a_cycle(tmp_path, monkeypatch): |
| 152 | """A 31st subscriber must not skip February.""" |
| 153 | data_dir = setup_env(tmp_path, monkeypatch) |
| 154 | subscribe(data_dir, current_period_start="2026-12-31", |
| 155 | current_period_end="2027-01-31") |
| 156 | run({"today": "2027-02-01"}) |
| 157 | assert sub(data_dir)["current_period_end"] == "2027-02-28" |
| 158 | |
| 159 | |
| 160 | # --- the ladder ----------------------------------------------------------------- |
| 161 | |
| 162 | def test_a_trial_ends_into_active(tmp_path, monkeypatch): |
| 163 | data_dir = setup_env(tmp_path, monkeypatch) |
| 164 | subscribe(data_dir, status="trialing", trial_ends_on="2026-06-20", |
| 165 | current_period_end="2026-12-31") |
| 166 | run({"today": "2026-07-01"}) |
| 167 | assert sub(data_dir)["status"] == "active" |
| 168 | |
| 169 | |
| 170 | def test_unpaid_invoices_walk_active_to_past_due_to_suspended(tmp_path, monkeypatch): |
| 171 | data_dir = setup_env(tmp_path, monkeypatch, |
| 172 | settings=(("billing.past_due_grace_days", "7"), |
| 173 | ("billing.suspend_after_days", "30"))) |
| 174 | subscribe(data_dir) |
| 175 | run({"today": "2026-07-01"}) # raises the invoice, due +14d |
| 176 | assert sub(data_dir)["status"] == "active" |
| 177 | |
| 178 | # A week past due: still active (inside grace). |
| 179 | run({"today": "2026-07-20"}) |
| 180 | assert sub(data_dir)["status"] == "active" |
| 181 | # Two weeks past due: past_due. |
| 182 | run({"today": "2026-07-30"}) |
| 183 | assert sub(data_dir)["status"] == "past_due" |
| 184 | # Well past: suspended. |
| 185 | run({"today": "2026-09-01"}) |
| 186 | assert sub(data_dir)["status"] == "suspended" |
| 187 | |
| 188 | |
| 189 | def test_paying_lets_the_ladder_be_climbed_back(tmp_path, monkeypatch): |
| 190 | """A ladder that only descends leaves a paying customer cut off.""" |
| 191 | data_dir = setup_env(tmp_path, monkeypatch, |
| 192 | settings=(("billing.past_due_grace_days", "7"),)) |
| 193 | subscribe(data_dir) |
| 194 | run({"today": "2026-07-01"}) |
| 195 | run({"today": "2026-07-30"}) |
| 196 | assert sub(data_dir)["status"] == "past_due" |
| 197 | |
| 198 | for row in invoices(data_dir): |
| 199 | object_records.update_collection_record( |
| 200 | "invoices", row["id"], {"status": "paid"}, base_dir=data_dir, actor="dan") |
| 201 | # Nothing is overdue now, so the runner stops pushing it down; the |
| 202 | # transition table allows past_due -> active for the operator. |
| 203 | result = run({"today": "2026-08-01"}) |
| 204 | assert not any("suspended" in str(r) for r in result["results"]) |
| 205 | object_records.update_collection_record( |
| 206 | "subscriptions", "sub-1", {"status": "active"}, base_dir=data_dir, actor="dan") |
| 207 | assert sub(data_dir)["status"] == "active" |
| 208 | |
| 209 | |
| 210 | def test_cancel_at_period_end_bills_the_last_period_then_stops(tmp_path, monkeypatch): |
| 211 | """Service runs to the end of the period already paid for -- cutting it |
| 212 | off at the click would bill for time the customer cannot use.""" |
| 213 | data_dir = setup_env(tmp_path, monkeypatch) |
| 214 | subscribe(data_dir) |
| 215 | assert run({"today": "2026-06-01"})["invoiced"] == 1 # June, in advance |
| 216 | object_records.update_collection_record( |
| 217 | "subscriptions", "sub-1", {"cancel_at_period_end": "true"}, base_dir=data_dir) |
| 218 | result = run({"today": "2026-07-01"}) |
| 219 | # The period served was billed in advance; the close bills no next |
| 220 | # period for a subscription that is ending (decision 4, 2026-11-30). |
| 221 | assert result["invoiced"] == 0 |
| 222 | assert sub(data_dir)["status"] == "canceled" |
| 223 | assert run({"today": "2026-08-01"})["invoiced"] == 0 # and never again |
| 224 | assert [row["total_cents"] for row in invoices(data_dir)] == ["4900"] |
| 225 | |
| 226 | |
| 227 | def test_canceled_subscriptions_are_left_entirely_alone(tmp_path, monkeypatch): |
| 228 | data_dir = setup_env(tmp_path, monkeypatch) |
| 229 | subscribe(data_dir, status="canceled") |
| 230 | assert run({"today": "2026-07-01"})["invoiced"] == 0 |
| 231 | |
| 232 | |
| 233 | def test_billing_not_installed_is_a_graceful_skip(tmp_path, monkeypatch): |
| 234 | data_dir = tmp_path / "data" |
| 235 | data_dir.mkdir() |
| 236 | monkeypatch.setenv("DBBASIC_DATA_DIR", str(data_dir)) |
| 237 | assert run()["skipped"].startswith("billing not installed") |