The personal finance app for everyone (by everyone)
Ruby
require "test_helper"
class RecurringOccurrenceTest < ActiveSupport::TestCase
include EntriesTestHelper
def setup
@family = families(:dylan_family)
@series = recurring_transactions(:netflix_subscription)
end
test "expected amount inherits from the series until overridden" do
occurrence = create_occurrence
assert_equal @series.amount, occurrence.resolved_expected_amount
# A series price edit reaches every open occurrence with zero sweeps.
@series.update!(amount: 18.99)
assert_equal 18.99, occurrence.resolved_expected_amount
occurrence.override_amount!(20)
assert_equal 20, occurrence.resolved_expected_amount
occurrence.override_amount!(nil)
assert_equal 18.99, occurrence.resolved_expected_amount
end
test "remaining_spending_money is the whole remaining amount for a series never split" do
occurrence = create_occurrence
assert_equal occurrence.remaining_amount_money, occurrence.remaining_spending_money
end
# Only the interest of a mortgage instalment is spending; the principal is a
# transfer to the loan, and an excluded part is not counted either.
test "remaining_spending_money keeps the spending share of the series' latest split charge" do
interest = @family.categories.create!(name: "Mortgage interest")
mortgage = create_bill(category: interest, amount: 1_500, name: "Mortgage")
split_payment(pay_bill(mortgage, date: Date.current - 1.month),
{ amount: 700, category: interest },
{ amount: 40, category: interest, excluded: true },
{ amount: 760, kind: "funds_movement" })
assert_equal Money.new(700, "USD"), expect_bill(mortgage, due_on: Date.current).remaining_spending_money
end
# A charge split into spending and a contribution to an investment account
# reserves the contribution only for a family that counts investing as
# spending, as IncomeStatement::Totals does.
test "remaining_spending_money counts a split contribution only when investing counts as spending" do
[ [ true, 1_000 ], [ false, 200 ] ].each do |as_spending, expected|
@family.update!(investment_contributions_as_spending: as_spending)
groceries = @family.categories.create!(name: "Groceries #{as_spending}")
bill = create_bill(category: groceries, amount: 1_000, name: "Groceries and savings #{as_spending}")
split_payment(pay_bill(bill, date: Date.current - 1.month),
{ amount: 200, category: groceries },
{ amount: 800, kind: "investment_contribution" })
due = expect_bill(bill, due_on: Date.current)
assert_equal Money.new(expected, "USD"), due.remaining_spending_money, "as_spending: #{as_spending}"
loaded = RecurringOccurrence.open_bills_due(@family, Date.current..Date.current).find { |o| o.id == due.id }
assert_equal Money.new(expected, "USD"), loaded.remaining_spending_money, "open_bills_due, as_spending: #{as_spending}"
end
end
test "average strategy resolves through the series variance" do
@series.update!(amount_strategy: "average", expected_amount_min: 10, expected_amount_max: 20, expected_amount_avg: 15.5)
occurrence = create_occurrence
assert_equal 15.5, occurrence.resolved_expected_amount
end
test "closing freezes the resolved amount so later series edits cannot rewrite history" do
occurrence = create_occurrence
occurrence.skip!
@series.update!(amount: 99.99)
assert_equal 15.99, occurrence.reload.expected_amount
assert_equal 15.99, occurrence.resolved_expected_amount
end
test "reopening keeps the frozen amount as an explicit override" do
occurrence = create_occurrence
occurrence.close!("paid", source: "user")
occurrence.reopen!
assert occurrence.scheduled?
assert_nil occurrence.closed_at
assert_equal 15.99, occurrence.expected_amount
end
test "derived state walks upcoming, due, overdue with snooze respected" do
travel_to Date.new(2026, 8, 13) do
assert_equal :upcoming, create_occurrence(due_on: Date.new(2026, 8, 20), original: Date.new(2026, 8, 20)).derived_state
assert_equal :due, create_occurrence(due_on: Date.new(2026, 8, 15), original: Date.new(2026, 8, 15)).derived_state
assert_equal :due, create_occurrence(due_on: Date.new(2026, 8, 11), original: Date.new(2026, 8, 11)).derived_state
overdue = create_occurrence(due_on: Date.new(2026, 8, 5), original: Date.new(2026, 8, 5))
assert_equal :overdue, overdue.derived_state
overdue.snooze!(Date.new(2026, 8, 25))
assert_equal :upcoming, overdue.derived_state
end
end
# Nobody is paying a bill that isn't active, so its leftover isn't late or
# due. The schedule state stays raw: the matcher reads it to keep a late
# payment able to settle the leftover.
test "a leftover of a bill that isn't active is neither overdue nor due" do
travel_to Date.new(2026, 8, 13) do
overdue = create_occurrence(due_on: Date.new(2026, 8, 5), original: Date.new(2026, 8, 5))
due = create_occurrence(due_on: Date.new(2026, 8, 11), original: Date.new(2026, 8, 11))
assert overdue.overdue?
assert due.due?
%w[inactive paused ended].each do |status|
@series.update!(status: status)
assert_not overdue.reload.overdue?, status
assert_equal :overdue, overdue.derived_state, status
assert_not due.reload.due?, status
assert_equal :due, due.derived_state, status
end
end
end
test "partial payment is exact-sum, never tolerance" do
occurrence = create_occurrence
occurrence.allocations.create!(allocated_amount: 10, currency: "USD", source: "user_created")
assert occurrence.partially_paid?
assert_equal 5.99, occurrence.remaining_amount
end
test "last strategy resolves to what the previous cycle actually cost" do
@series.update!(amount_strategy: "last")
# Expected 15.99, really charged 17.25: inside tolerance, so it closes as
# actual-replaces-estimate and the frozen expected stays at the estimate.
settled = create_occurrence(due_on: Date.current - 30, original: Date.current - 30)
settled.allocations.create!(allocated_amount: 17.25, currency: "USD",
state: "confirmed", source: "user_created")
settled.close!("paid", source: "auto")
assert_equal 15.99, settled.reload.expected_amount, "the frozen value is the estimate"
assert_equal 17.25, create_occurrence.resolved_expected_amount,
"'last' must propose the real charge, not the old estimate"
end
test "last strategy ignores a paid cycle that carries no payments" do
@series.update!(amount_strategy: "last")
create_occurrence(due_on: Date.current - 30, original: Date.current - 30)
.close!("paid", source: "user")
assert_equal 15.99, create_occurrence.resolved_expected_amount
end
test "the system never sets missed" do
occurrence = create_occurrence(due_on: 3.months.ago.to_date, original: 3.months.ago.to_date)
assert_equal :overdue, occurrence.derived_state
occurrence.miss!
assert occurrence.missed?
assert_equal "user", occurrence.closed_source
end
private
def create_occurrence(due_on: Date.current + 10, original: nil)
@series.recurring_occurrences.create!(
family: @family,
original_due_on: original || due_on,
due_on: due_on,
currency: "USD"
)
end
end