"""Pure offline tests for target_revenue.remission (WP-0013). No Docker/Postgres — plan_remission / cumulative_remission are pure like fold.py. """ from __future__ import annotations from datetime import datetime, timezone from copy import deepcopy import pytest from conftest import golden_entries, golden_manifest from target_revenue import fold, remission def _dt(s: str) -> datetime: return datetime.fromisoformat(s.replace("Z", "+00:00")) def test_cumulative_remission_endpoints(): t0 = _dt("2026-01-01T00:00:00Z") tL = _dt("2027-01-01T00:00:00Z") T0 = 100_000.0 assert remission.cumulative_remission(T0, t0, tL, t0) == 0.0 assert remission.cumulative_remission(T0, t0, tL, tL) == T0 assert remission.cumulative_remission(T0, t0, tL, _dt("2025-01-01T00:00:00Z")) == 0.0 assert remission.cumulative_remission(T0, t0, tL, _dt("2028-01-01T00:00:00Z")) == T0 def test_cumulative_remission_midpoint(): t0 = _dt("2026-01-01T00:00:00Z") tL = _dt("2027-01-01T00:00:00Z") mid = _dt("2026-07-02T12:00:00Z") # ~half of non-leap 365d span # Exact half of 365 days from 2026-01-01: half = _dt("2026-07-02T12:00:00Z") got = remission.cumulative_remission(100_000.0, t0, tL, half) # 182.5 / 365 = 0.5 exactly for non-leap year. assert abs(got - 50_000.0) < 1.0 def test_plan_remission_delta_is_idempotent_at_same_as_of(): manifest = golden_manifest() # Golden longstop is 2031-08-01; pick a synthetic t0 five years earlier. t0 = _dt("2026-08-01T00:00:00Z") as_of = _dt("2027-08-01T00:00:00Z") # 1/5 of the way if longstop 2031-08-01 plan1 = remission.plan_remission(manifest, [], t0=t0, as_of=as_of) assert plan1 is not None assert plan1.should_append entry = remission.build_remission_entry_input( manifest["phase"]["id"], plan1, currency="USD", entry_id="trsl:entry:remtest1" ) assert entry is not None assert entry["type"] == "remission-credit" assert entry["extension"]["id"] == "trsl:policy:linear-longstop-v0" plan2 = remission.plan_remission(manifest, [entry], t0=t0, as_of=as_of) assert plan2 is not None assert not plan2.should_append assert plan2.delta == 0.0 assert remission.build_remission_entry_input(manifest["phase"]["id"], plan2, "USD") is None def test_plan_ignores_corrections_in_already_remitted(): """A remission-correction must not be treated as policy-produced credit for the next auto-run (would silently undo a deliberate correction).""" manifest = golden_manifest() t0 = _dt("2026-08-01T00:00:00Z") as_of = _dt("2028-08-01T00:00:00Z") plan = remission.plan_remission(manifest, [], t0=t0, as_of=as_of) assert plan is not None and plan.delta > 0 fake_correction = { "id": "trsl:entry:corr1", "phase": manifest["phase"]["id"], "type": "remission-correction", "amount": plan.delta, "currency": "USD", "recognized_at": as_of.isoformat(), "evidence_reference": "x", "previous_entry_hash": "GENESIS", "reverses": "trsl:entry:someone", } plan2 = remission.plan_remission(manifest, [fake_correction], t0=t0, as_of=as_of) assert plan2 is not None # Correction does not count toward already_remitted → still wants full delta. assert plan2.delta == plan.delta def test_unsupported_policy_returns_none(): manifest = deepcopy(golden_manifest()) manifest["phase"]["degeneration_policy"] = "trsl:policy:progress-paused-longstop-v1@1.0" plan = remission.plan_remission( manifest, [], t0=_dt("2026-01-01T00:00:00Z"), as_of=_dt("2027-01-01T00:00:00Z") ) assert plan is None def test_next_monthly_boundary(): assert remission.next_monthly_boundary(_dt("2026-08-05T12:00:00Z")) == _dt( "2026-09-01T00:00:00Z" ) assert remission.next_monthly_boundary(_dt("2026-12-15T00:00:00Z")) == _dt( "2027-01-01T00:00:00Z" ) def test_next_scheduled_prefers_longstop_when_sooner(): as_of = _dt("2026-08-05T00:00:00Z") longstop = _dt("2026-08-20T00:00:00Z") assert remission.next_scheduled_remission_at(as_of, longstop) == longstop assert remission.next_scheduled_remission_at(longstop, longstop) is None def test_planned_entry_validates_and_folds(): from target_revenue import validation manifest = golden_manifest() t0 = _dt("2026-08-01T00:00:00Z") as_of = _dt("2029-08-01T00:00:00Z") plan = remission.plan_remission(manifest, [], t0=t0, as_of=as_of) entry = remission.build_remission_entry_input( manifest["phase"]["id"], plan, "USD", entry_id="trsl:entry:remfold1" ) assert entry is not None entry["previous_entry_hash"] = "GENESIS" validation.validate_ledger_entry(entry) result = fold.fold_outstanding_target(manifest["phase"]["initial_target"]["amount"], [entry]) assert result.remission_credit == entry["amount"] assert result.outstanding_target == pytest.approx( manifest["phase"]["initial_target"]["amount"] - entry["amount"] ) def test_metrics_remission_forecasts_with_activated_at(): from target_revenue import metrics manifest = golden_manifest() t0 = _dt("2026-08-01T00:00:00Z") as_of = _dt("2027-08-01T00:00:00Z") result = metrics.compute_metrics(manifest, [], as_of, activated_at=t0) assert result["facts"]["activated_at"] == t0.isoformat() assert result["forecasts"]["remission_if_applied_now"] is not None assert result["forecasts"]["remission_if_applied_now"] > 0 assert result["forecasts"]["next_scheduled_remission_at"] is not None assert result["forecasts"]["next_scheduled_remission_amount"] is not None def test_metrics_without_activated_at_leaves_remission_forecasts_null(): from target_revenue import metrics manifest = golden_manifest() as_of = _dt("2027-08-01T00:00:00Z") result = metrics.compute_metrics(manifest, golden_entries(), as_of) assert result["facts"]["activated_at"] is None assert result["forecasts"]["remission_if_applied_now"] is None assert result["forecasts"]["next_scheduled_remission_at"] is None