Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/monetize

crates/monetize-server/src/estate_order/tests/page_close.rs

//! ★ **F-S-02 / F-S-03 (A17, 2026-10-23): a payment page the shop lets go of is never lost.**
//! A renewal's or a plan change's page closed past its time, which the buyer paid in its last
//! second (the vendor then refuses to expire it), is captured and the sale goes on — the rule
//! the ORDERED phase already keeps (A19). A page that cannot be closed where nothing may be
//! charged is kept on the run, said, and asked to close again on every tick.

use super::*;

const HOUR: u64 = 3_600_000;

fn notes(run: &EstateRun) -> String {
    run.history
        .iter()
        .map(|t| t.note.as_str())
        .collect::<Vec<_>>()
        .join("\n")
}

/// The renewal page expires while the buyer pays it: captured, never dropped.
#[test]
fn a_renewal_paid_as_its_page_expires_is_captured_and_never_dropped() {
    let r = delivered("page-renewal-expiry", cfg());
    let paid = r.state.estates.get("acme").unwrap().paid_until_ms;
    let now = paid - 3 * DAY;
    at(&r, now);
    let renewal = term::renew(&r.state, "acme").unwrap();
    r.pay.0.pay_on_release.store(true, Ordering::SeqCst);
    let run = at(&r, now + 2 * HOUR);
    assert_eq!(
        run.renewals.len(),
        1,
        "the renewal paid in the last second is captured: {}",
        notes(&run)
    );
    assert_eq!(run.paid_until_ms, price::add_months(paid, 6));
    assert_eq!(r.pay.captures(), 2);
    assert!(run.renewal.is_none());
    assert!(matches!(
        r.pay.state_of(&renewal.handle.id),
        ChargeStatus::Captured { .. }
    ));
}

/// The buyer asks to renew again after the page went stale, and it was paid meanwhile: the
/// same page is answered (and captured on the tick), never a second hold.
#[test]
fn a_stale_renewal_page_paid_as_it_is_closed_is_answered_again_and_no_second_hold_is_opened() {
    let r = delivered("page-renewal-again", cfg());
    let paid = r.state.estates.get("acme").unwrap().paid_until_ms;
    let now = paid - 3 * DAY;
    at(&r, now);
    let first = term::renew(&r.state, "acme").unwrap();
    let asked = r.pay.0.requests.lock().unwrap().len();
    r.clock.store(now + 2 * HOUR, Ordering::SeqCst);
    r.pay.0.pay_on_release.store(true, Ordering::SeqCst);
    let again = term::renew(&r.state, "acme").unwrap();
    assert_eq!(again.handle, first.handle, "the paid page is the renewal");
    assert_eq!(
        r.pay.0.requests.lock().unwrap().len(),
        asked,
        "no second hold"
    );
    let run = at(&r, now + 2 * HOUR + 60_000);
    assert_eq!(run.renewals.len(), 1, "{}", notes(&run));
}

/// A plan change's page expires while the buyer pays it: the change goes on.
#[test]
fn a_plan_change_paid_as_its_page_expires_goes_on() {
    let r = delivered("page-change-expiry", cfg());
    r.cloud
        .plan_prices
        .lock()
        .unwrap()
        .insert("STARTER-16xCPU-64GB".into(), 5_000);
    let now = r.state.estates.get("acme").unwrap().paid_until_ms - 90 * DAY;
    r.clock.store(now, Ordering::SeqCst);
    let c = change::change(&r.state, "acme", &bigger_main()).unwrap();
    r.pay.0.pay_on_release.store(true, Ordering::SeqCst);
    let mut run = at(&r, now + 2 * HOUR);
    for i in 1..6 {
        if run.change.is_none() {
            break;
        }
        run = at(&r, now + 2 * HOUR + i * 60_000);
    }
    let done = run.changes.last().expect("the change finished");
    assert_eq!(
        done.status,
        change::status::DONE,
        "{} / {}",
        done.why,
        notes(&run)
    );
    assert!(matches!(
        r.pay.state_of(&c.handle.as_ref().unwrap().id),
        ChargeStatus::Captured { .. }
    ));
}

/// A renewal page the vendor will not close at a cancel is KEPT, said, and closed on a tick.
#[test]
fn a_page_the_vendor_will_not_close_is_kept_said_and_closed_on_a_later_tick() {
    let r = delivered("page-kept", cfg());
    let paid = r.state.estates.get("acme").unwrap().paid_until_ms;
    at(&r, paid - 3 * DAY);
    let renewal = term::renew(&r.state, "acme").unwrap();
    r.pay.0.release_down.store(true, Ordering::SeqCst);
    term::cancel(&r.state, "acme").unwrap();
    let run = r.state.estates.get("acme").unwrap();
    assert!(
        notes(&run).contains("NOT closed"),
        "the page left open is said: {}",
        notes(&run)
    );
    assert_eq!(
        r.pay.state_of(&renewal.handle.id),
        ChargeStatus::AwaitingCustomer
    );

    r.pay.0.release_down.store(false, Ordering::SeqCst);
    tick(&r.state);
    let run = r.state.estates.get("acme").unwrap();
    assert!(notes(&run).contains("closed at last"), "{}", notes(&run));
    assert_eq!(r.pay.state_of(&renewal.handle.id), ChargeStatus::Released);
}

/// The operator cancels an unpaid order while its buyer pays: refused, and the tick captures it.
#[test]
fn an_operator_refund_of_an_order_paid_that_second_is_refused_and_the_order_goes_on() {
    let r = rig("page-settle");
    start(
        &r.state,
        "acme",
        "buyer@example.com",
        "",
        &EstateSizes::default(),
    )
    .unwrap();
    r.pay.0.pay_on_release.store(true, Ordering::SeqCst);
    let said = settle(&r.state, "acme", "refund");
    assert!(matches!(said, Err(StartRefusal::NotNow(_))), "{said:?}");
    let run = r.state.estates.get("acme").unwrap();
    assert_eq!(run.phase, phase::ORDERED);
    drive(&r.state, "acme");
    assert_eq!(
        r.pay.captures(),
        1,
        "the order paid at the last second is captured"
    );
}