Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
36.3 KB
Raw Blame
require "test_helper"

class RecurringTransaction::AllocatorTest < ActiveSupport::TestCase
  Allocator = RecurringTransaction::Allocator

  def setup
    @family = families(:dylan_family)
    @account = accounts(:depository)
    @rent = @family.recurring_transactions.create!(
      account: @account,
      name: "Watson Property",
      amount: 2000,
      currency: "USD",
      expected_day_of_month: 29,
      last_occurrence_date: Date.current,
      next_expected_date: 1.month.from_now.to_date,
      status: "active",
      manual: true
    )
    @occurrence = @rent.recurring_occurrences.order(:due_on).last ||
                  @rent.recurring_occurrences.create!(family: @family,
                                                      original_due_on: (Date.current + 1.month).beginning_of_month + 9,
                                                      due_on: (Date.current + 1.month).beginning_of_month + 9,
                                                      currency: "USD")
    @allocator = Allocator.new(@occurrence)
  end

  test "a currency mismatch reads as a sentence, not a missing translation" do
    allocation = RecurringAllocation.new(
      recurring_occurrence: @occurrence, state: :confirmed, source: :user_confirmed,
      allocated_amount: 10, currency: "EUR", paid_on: Date.current
    )

    assert_not allocation.valid?
    message = allocation.errors.full_messages_for(:currency).join
    assert_no_match(/translation missing/i, message)
  end

  test "manual attach does not record the detected name as a new alias after a rename" do
    @rent.update!(name: "Watson Rent", matcher_hints: { "detected_name" => "WATSON PROPERTY MGMT" })
    entry = @account.entries.create!(
      date: Date.current,
      amount: 2000,
      currency: "USD",
      name: "WATSON PROPERTY MGMT",
      entryable: Transaction.new
    )

    @allocator.allocate!(entry: entry)

    assert_not_includes Array(@rent.reload.matcher_hints["name_aliases"]), "WATSON PROPERTY MGMT"
  end

  test "a price rise does not re-target an occurrence that has already been paid against" do
    @occurrence.update!(expected_amount: nil) # inheriting from the series
    @allocator.allocate!(entry: entry_for(500))

    assert_equal 1500, @occurrence.reload.remaining_amount

    @rent.update!(amount: 2500)

    occurrence = @occurrence.reload
    assert_equal 2000, occurrence.resolved_expected_amount,
      "the $500 was paid against a $2,000 obligation and must stay against it"
    assert_equal 1500, occurrence.remaining_amount
  end

  test "a price rise still updates an open occurrence nobody has paid against" do
    @occurrence.update!(expected_amount: nil)

    @rent.update!(amount: 2500)

    assert_equal 2500, @occurrence.reload.resolved_expected_amount
  end

  test "a suggestion does not pin the amount" do
    @occurrence.update!(expected_amount: nil)
    @allocator.allocate_matched!(entry: entry_for(500), state: "suggested", confidence: 0.7, signals: {})

    @rent.update!(amount: 2500)

    assert_nil @occurrence.reload.expected_amount
    assert_equal 2500, @occurrence.resolved_expected_amount
  end

  test "a partial payment teaches the matcher nothing about tolerance" do
    # $1,625 against $2,000 rent is an installment, not evidence rent varies.
    # It is the only allocation, which is what used to be mistaken for proof.
    @allocator.allocate!(entry: entry_for(1625))

    assert @occurrence.reload.partially_paid?
    assert_nil @rent.reload.matcher_hints["learned_tolerance_pct"]
  end

  test "a single payment that settles the bill above the band does widen tolerance" do
    # $2,200 settles a $2,000 obligation and sits outside the 7.5% band, so the
    # band really was too tight.
    @allocator.allocate!(entry: entry_for(2200))

    assert @occurrence.reload.paid?
    assert_equal 10.0, @rent.reload.matcher_hints["learned_tolerance_pct"]
  end

  test "partial payments accumulate and close only at the exact sum" do
    @allocator.allocate!(entry: entry_for(750))
    @allocator.allocate!(entry: entry_for(500))

    assert @occurrence.reload.partially_paid?
    assert_equal 750, @occurrence.remaining_amount

    @allocator.allocate!(entry: entry_for(750))

    assert @occurrence.reload.paid?
    assert_equal "auto", @occurrence.closed_source
  end

  test "1850 of 2000 stays partially paid, never paid" do
    # The Check Mode regression: tolerance-close must not apply to
    # accumulated partials.
    @allocator.allocate!(entry: entry_for(750))
    @allocator.allocate!(entry: entry_for(500))
    @allocator.allocate!(entry: entry_for(600))

    occurrence = @occurrence.reload
    assert occurrence.scheduled?, "must NOT close"
    assert occurrence.partially_paid?
    assert_equal 150, occurrence.remaining_amount
  end

  test "a single payment within tolerance closes as actual-replaces-estimate" do
    # Expected $2,000 ± 7.5%; one charge of $2,080 IS the bill.
    @allocator.allocate!(entry: entry_for(2080))

    occurrence = @occurrence.reload
    assert occurrence.paid?
    assert_equal "auto", occurrence.closed_source
  end

  test "a single payment below the band stays partial" do
    @allocator.allocate!(entry: entry_for(1500))
    assert @occurrence.reload.partially_paid?
  end

  test "a single payment inside the tolerance band is recorded at its real amount" do
    # The bill is 2000 and the charge is 2080 (4%, inside the 7.5% band).
    allocation = @allocator.allocate!(entry: entry_for(2080))

    assert_equal 2080, allocation.allocated_amount
    assert_equal 2080, @occurrence.reload.confirmed_allocated
    assert @occurrence.paid?
    assert_not @occurrence.overpaid?, "a payment inside the band is the bill, not an overpayment"
  end

  test "a single payment outside the band is still capped at the estimate" do
    # 2600 is 30% over, beyond the 25% limit a manual link can teach.
    allocation = @allocator.allocate!(entry: entry_for(2600))

    assert_equal 2000, allocation.allocated_amount
  end

  test "a learned tolerance widens the band the allocator uses" do
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => 10 })

    allocation = @allocator.allocate!(entry: entry_for(2180))

    assert_equal 2180, allocation.allocated_amount, "9% over is inside the learned 10% band"
  end

  test "a null learned tolerance hint reads as no widening" do
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => nil })

    allocation = @allocator.allocate!(entry: entry_for(2080))

    assert_equal 2080, allocation.allocated_amount
    assert @occurrence.reload.paid?
  end

  test "a single below-estimate payment inside the band is recorded at its real amount" do
    # 1900 is 5% under the 2000 bill, inside the 7.5% band.
    allocation = @allocator.allocate!(entry: entry_for(1900))

    assert_equal 1900, allocation.allocated_amount
    assert @occurrence.reload.paid?
  end

  test "a payment just past the band is recorded in full on the first manual link" do
    # 2151 is 7.55% over 2000, just outside the 7.5% default. The manual link
    # teaches a band (7.6%) that covers it, and the whole payment is recorded.
    allocation = @allocator.allocate!(entry: entry_for(2151))

    assert_equal 2151, allocation.allocated_amount
    assert_equal 2151, @occurrence.reload.confirmed_allocated
    assert_equal 7.6, @rent.reload.matcher_hints["learned_tolerance_pct"].to_f
    assert @occurrence.paid?
    assert_not @occurrence.overpaid?
  end

  test "a payment 7.51% over the bill stores the ceiling of the deviation" do
    # 7.51% rounds to 7.5 at one decimal but must store 7.6 (the ceiling).
    allocation = @allocator.allocate!(entry: entry_for(BigDecimal("2150.20")))

    assert_equal BigDecimal("2150.20"), allocation.allocated_amount
    assert_equal 7.6, @rent.reload.matcher_hints["learned_tolerance_pct"].to_f
  end

  test "a non-numeric learned hint reads as no widening" do
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => "" })

    allocation = @allocator.allocate!(entry: entry_for(2080))

    assert_equal 2080, allocation.allocated_amount
  end

  test "a non-finite learned hint reads as no widening" do
    # "Infinity" would otherwise widen the band to everything. 30% over is
    # beyond the 25% learnable limit, so the default must still cap it.
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => "Infinity" })

    allocation = @allocator.allocate!(entry: entry_for(2600))

    assert_equal 2000, allocation.allocated_amount
  end

  test "a non-finite learned hint does not widen the matched-payment band" do
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => "Infinity" })

    allocation = @allocator.allocate_matched!(
      entry: entry_for(2300), state: "confirmed", confidence: 1.0, signals: {}
    )

    assert_equal 2000, allocation.allocated_amount
  end

  test "an imported learned hint is capped at the learnable maximum" do
    # Learning never exceeds 25%, but imported hints are not validated. A 100%
    # hint must not turn the band into everything: 30% over stays capped.
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => 100 })

    allocation = @allocator.allocate!(entry: entry_for(2600))

    assert_equal 2000, allocation.allocated_amount
  end

  test "the displayed learned tolerance is capped the same way as the matcher's" do
    @rent.update!(matcher_hints: { "learned_tolerance_pct" => 100 })

    assert_equal 25, @rent.reload.learned_amount_tolerance_pct.to_f
    assert_equal 25, @rent.effective_amount_tolerance_pct.to_f
  end

  test "the first manual link of a payment outside the default band records the actual amount" do
    # The Codex case: a 40.00 bill, a 46.80 payment (17% over). The first link
    # teaches a 17% band and records 46.80, not the 40.00 estimate.
    occurrence = usd_occurrence(expected: 40)
    allocation = Allocator.new(occurrence).allocate!(entry: entry_for(46.80))

    assert_equal 46.80, allocation.allocated_amount
    assert_equal 46.80, occurrence.reload.confirmed_allocated
    assert occurrence.paid?
    assert_not occurrence.overpaid?
  end

  test "a manual partial payment outside the band teaches nothing and stays partial" do
    allocation = @allocator.allocate!(entry: entry_for(1500))

    assert_equal 1500, allocation.allocated_amount
    assert @occurrence.reload.partially_paid?
    assert_nil @rent.reload.matcher_hints["learned_tolerance_pct"]
  end

  test "an explicit amount on a manual link is never promoted to the entry amount" do
    occurrence = usd_occurrence(expected: 40)
    allocation = Allocator.new(occurrence).allocate!(entry: entry_for(46.80), amount: 40)

    assert_equal 40, allocation.allocated_amount
  end

  test "a partial payment is never recorded as the whole bill" do
    allocation = @allocator.allocate!(entry: entry_for(1500))

    assert_equal 1500, allocation.allocated_amount
    assert @occurrence.reload.partially_paid?
  end

  test "a second payment on an occurrence is capped at what remains, not the band" do
    @allocator.allocate!(entry: entry_for(1900))
    allocation = @allocator.allocate!(entry: entry_for(500))

    assert_equal 100, allocation.allocated_amount
  end

  test "overpayment closes and flags" do
    # The default allocation takes only what the occurrence needs, so an
    # overpay has to be explicit.
    @allocator.allocate!(entry: entry_for(1200), amount: 1200)
    @allocator.allocate!(entry: entry_for(1200), amount: 1200)

    occurrence = @occurrence.reload
    assert occurrence.paid?
    assert occurrence.overpaid?
  end

  test "removing the satisfying allocation reopens an auto-closed occurrence but never a user-closed one" do
    allocation = @allocator.allocate!(entry: entry_for(2000))
    assert @occurrence.reload.paid?

    @allocator.unallocate!(allocation)
    assert @occurrence.reload.scheduled?, "auto-closed reopens"

    @allocator.mark_paid!
    assert @occurrence.reload.paid?
    assert_equal "user", @occurrence.closed_source

    padding = @occurrence.allocations.order(:created_at).last
    @allocator.unallocate!(padding)
    assert @occurrence.reload.paid?, "user-closed never auto-reopens"
  end

  test "mark_paid! settles the remainder without a transaction" do
    @allocator.allocate!(entry: entry_for(537.50))
    @allocator.mark_paid!

    occurrence = @occurrence.reload
    assert occurrence.paid?
    manual = occurrence.allocations.where(entry_id: nil).first
    assert_equal 1462.50, manual.allocated_amount
    assert manual.from_user_created?
  end

  test "one entry can pay several occurrences but never more than itself" do
    lump = entry_for(1612.50)
    september = free_occurrence

    @allocator.allocate!(entry: lump, amount: 1000)
    Allocator.new(september).allocate!(entry: lump, amount: 612.50)

    assert_raises Allocator::OverAllocationError do
      Allocator.new(september).allocate!(entry: lump, amount: 500)
    end
  end

  test "an entry another occurrence already uses up is refused as exhausted" do
    lump = entry_for(500)
    @allocator.allocate!(entry: lump)
    september = free_occurrence

    # With no amount the default is what the entry has left, which is zero.
    # That used to reach the allocation's "greater than 0" validation instead.
    assert_raises Allocator::EntryExhaustedError do
      Allocator.new(september).allocate!(entry: lump)
    end
    assert_empty september.allocations
  end

  test "relinking an entry this occurrence already uses up is a duplicate, not exhaustion" do
    lump = entry_for(500)
    @allocator.allocate!(entry: lump)

    assert_raises Allocator::AlreadyAllocatedError do
      @allocator.allocate!(entry: lump)
    end
    assert_equal 1, @occurrence.allocations.where(entry: lump).count
  end

  test "remaining_by_entry reports what each used entry has left in one query" do
    partly = entry_for(500)
    fully = entry_for(300)
    unused = entry_for(100)
    @allocator.allocate!(entry: partly, amount: 200)
    @allocator.allocate!(entry: fully, amount: 300)

    remaining = assert_queries_count(1) { Allocator.remaining_by_entry([ partly, fully, unused ]) }

    assert_equal({ partly.id => 300, fully.id => 0 }, remaining)
  end

  # A split parent and its children are the same money: whichever side pays a
  # bill first, the other side is spent, suggestions included.
  test "a split parent and its children never both pay" do
    parent = entry_for(1612.50)
    child, = parent.split!([ { name: "Rent", amount: 1000 }, { name: "Parking", amount: 612.50 } ])
    september = free_occurrence

    @allocator.allocate!(entry: parent, amount: 1000)
    assert_raises(Allocator::SplitCounterpartAllocatedError) { Allocator.new(september).allocate!(entry: child) }

    @occurrence.allocations.destroy_all
    Allocator.new(september).allocate_matched!(entry: child, state: "suggested", confidence: 0.7, signals: {})
    assert_raises(Allocator::SplitCounterpartAllocatedError) { @allocator.allocate!(entry: parent) }
    assert_empty @occurrence.allocations
  end

  test "the parts of a split can each pay a bill" do
    rent, parking = entry_for(1612.50).split!([ { name: "Rent", amount: 1000 }, { name: "Parking", amount: 612.50 } ])

    @allocator.allocate!(entry: rent)
    Allocator.new(free_occurrence).allocate!(entry: parking)

    assert_equal 2, RecurringAllocation.where(entry: [ rent, parking ]).count
  end

  test "remaining_by_entry reports the other side of a paying split as used up, in one query" do
    parent = entry_for(1612.50)
    rent, parking = parent.split!([ { name: "Rent", amount: 1000 }, { name: "Parking", amount: 612.50 } ])
    @allocator.allocate!(entry: rent, amount: 400)

    remaining = assert_queries_count(1) { Allocator.remaining_by_entry([ parent, rent, parking ]) }

    assert_equal({ parent.id => 0, rent.id => 600 }, remaining)

    other_parent = entry_for(500)
    other_child, = other_parent.split!([ { name: "A", amount: 200 }, { name: "B", amount: 300 } ])
    @allocator.allocate!(entry: other_parent, amount: 100)

    assert_equal({ other_child.id => 0 }, Allocator.remaining_by_entry([ other_child ]))
  end

  # The guard ignores ended series, so the drawer must too: a deleted bill
  # that a child paid leaves the parent free.
  test "remaining_by_entry ignores what an ended bill took, on the entry or its counterpart" do
    parent = entry_for(1612.50)
    rent, = parent.split!([ { name: "Rent", amount: 1000 }, { name: "Parking", amount: 612.50 } ])
    @allocator.allocate!(entry: rent, amount: 400)
    @rent.update!(status: "ended")

    assert_empty Allocator.remaining_by_entry([ parent, rent ])
  end

  # Different locks would let a parent and a child pass their counterpart
  # checks at once, each blind to the other's uncommitted allocation.
  test "a split parent and its children share one entry lock" do
    parent = entry_for(1612.50)
    children = parent.split!([ { name: "Rent", amount: 1000 }, { name: "Parking", amount: 612.50 } ])

    assert_equal [ Allocator.entry_lock_id(parent) ], children.map { |child| Allocator.entry_lock_id(child) }.uniq
    assert_not_equal Allocator.entry_lock_id(parent), Allocator.entry_lock_id(entry_for(10))
  end

  test "remaining_by_entry treats an unmeasurable entry as used up" do
    ExchangeRate.delete_all
    entry = foreign_entry(amount: 100, currency: "EUR")
    Allocator.new(usd_occurrence(expected: 150)).allocate!(amount: 150, entry: entry)

    assert_equal({ entry.id => 0 }, Allocator.remaining_by_entry([ entry ]))
  end

  test "the same entry cannot be allocated twice to one occurrence" do
    # Partial amounts so the capacity guard passes; the unique index is the
    # backstop this test pins.
    payment = entry_for(500)
    @allocator.allocate!(entry: payment, amount: 200)

    assert_raises ActiveRecord::RecordNotUnique do
      @allocator.allocate!(entry: payment, amount: 100)
    end
  end

  test "default allocation takes what the occurrence needs, not the whole entry" do
    @allocator.allocate!(entry: entry_for(1900))
    big = entry_for(500)
    allocation = @allocator.allocate!(entry: big)

    assert_equal 100, allocation.allocated_amount
    assert @occurrence.reload.paid?
  end

  test "a cross-currency entry without a rate demands an explicit amount" do
    foreign = @account.entries.create!(
      date: Date.current, amount: 100, currency: "EUR", name: "EU charge",
      entryable: Transaction.new
    )

    assert_raises Allocator::MissingRateError do
      @allocator.allocate!(entry: foreign)
    end

    allocation = @allocator.allocate!(entry: foreign, amount: 110)
    assert_equal 110, allocation.allocated_amount
    assert_equal "USD", allocation.currency
    assert_equal "EUR", allocation.source_currency
  end

  test "a partly allocated cross-currency entry defaults to its leftover" do
    ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", rate: 1.1, date: Date.current)
    foreign = @account.entries.create!(
      date: Date.current, amount: 100, currency: "EUR", name: "EU charge",
      entryable: Transaction.new
    )
    @allocator.allocate!(entry: foreign, amount: 55)

    other = free_occurrence
    # 50 EUR of the entry is spoken for, so the default is the 50 EUR
    # leftover converted, not the full 100 the old default reached for.
    allocation = Allocator.new(other).allocate!(entry: foreign)

    assert_equal 55, allocation.allocated_amount
    assert_equal 50, allocation.source_amount
  end

  test "a cross-currency default takes what the occurrence needs, not the whole entry" do
    ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", rate: 1.1, date: Date.current)
    @allocator.allocate!(entry: entry_for(1900))
    foreign = @account.entries.create!(
      date: Date.current, amount: 200, currency: "EUR", name: "EU charge",
      entryable: Transaction.new
    )
    allocation = @allocator.allocate!(entry: foreign)

    assert_equal 100, allocation.allocated_amount
    assert @occurrence.reload.paid?
  end

  test "entry deletion nullifies the link but keeps the payment" do
    payment = entry_for(500)
    allocation = @allocator.allocate!(entry: payment)

    payment.destroy!

    allocation.reload
    assert_nil allocation.entry_id
    assert_equal 500, allocation.allocated_amount
  end


  # Without a rate for the entry's date an allocation has no source amount, so
  # it cannot be measured against the transaction. The guard used to skip those
  # entirely, and entry_capacity summed COALESCE(source_amount, allocated_amount)
  # across currencies, so one 100 EUR transaction could pay 150 USD twice and
  # keep going.
  test "an unmeasurable entry cannot be spread across several occurrences" do
    ExchangeRate.delete_all
    entry = foreign_entry(amount: 100, currency: "EUR")
    first_occurrence = usd_occurrence(expected: 150)
    second_occurrence = usd_occurrence(expected: 150, due_on: Date.current + 30)

    RecurringTransaction::Allocator.new(first_occurrence).allocate!(amount: 150, entry: entry)

    assert_raises RecurringTransaction::Allocator::OverAllocationError do
      RecurringTransaction::Allocator.new(second_occurrence).allocate!(amount: 150, entry: entry)
    end

    assert_equal 1, RecurringAllocation.where(entry: entry).count,
      "one judgement call is allowed; an unbounded fan-out is not"
  end

  test "the first unmeasurable allocation is still permitted" do
    ExchangeRate.delete_all
    entry = foreign_entry(amount: 100, currency: "EUR")
    occurrence = usd_occurrence(expected: 150)

    allocation = RecurringTransaction::Allocator.new(occurrence).allocate!(amount: 150, entry: entry)

    assert allocation.persisted?
    assert_nil allocation.source_amount
  end

  # The remainder guard is opt-in and lives INSIDE the occurrence lock,
  # because a caller's pre-lock capacity check is advisory: two concurrent
  # payments can both read the same stale remainder. Sequentially that
  # surfaces as: the second payment sees the first one's allocation.
  test "a capped amount cannot exceed what remains on the cycle" do
    occurrence = usd_occurrence(expected: 100)
    allocator = RecurringTransaction::Allocator.new(occurrence)

    allocator.allocate!(amount: 60, source: "user_created", cap_at_remaining: true)

    error = assert_raises(RecurringTransaction::Allocator::OverAllocationError) do
      allocator.allocate!(amount: 60, source: "user_created", cap_at_remaining: true)
    end
    assert_match(/remaining/, error.message)
    assert_equal 60, occurrence.reload.allocations.sum(:allocated_amount)
  end

  # Exceeding the remainder stays legal for every caller that does not ask
  # for capping: a single settlement above the expected amount is how a price
  # rise gets recorded, and PriceChangeDetector reads exactly those.
  test "an uncapped settlement above the expected amount is still permitted" do
    occurrence = usd_occurrence(expected: 80)

    allocation = RecurringTransaction::Allocator.new(occurrence)
                                                .allocate!(amount: 95, source: "user_created")

    assert allocation.persisted?
    assert_equal 95, occurrence.reload.allocations.sum(:allocated_amount)
  end

  test "a payment can be linked to a new bill after the original bill is ended" do
    # Reproduces: we-promise/sure#3592
    # entry_capacity was summing allocations from ended series, making the
    # entry appear fully spent and blocking re-use on a new bill.
    payment = entry_for(2000)

    # Link the payment to the original bill's occurrence and close it.
    @allocator.allocate!(entry: payment)
    assert @occurrence.reload.paid?

    # End the original series (what the app does when a user "deletes" a bill).
    @rent.update!(status: "ended")

    # Create a replacement bill for the same obligation.
    replacement = @family.recurring_transactions.create!(
      account: @account,
      name: "Watson Property",
      amount: 2000,
      currency: "USD",
      expected_day_of_month: 29,
      last_occurrence_date: Date.current,
      next_expected_date: 1.month.from_now.to_date,
      status: "active",
      manual: true,
      dedup_scope: "replacement"
    )
    new_occurrence = replacement.recurring_occurrences.create!(
      family: @family,
      original_due_on: (Date.current + 1.month).beginning_of_month + 9,
      due_on: (Date.current + 1.month).beginning_of_month + 9,
      currency: "USD"
    )

    # Linking the same payment to the new bill must succeed.
    assert_nothing_raised do
      Allocator.new(new_occurrence).allocate!(entry: payment)
    end

    assert new_occurrence.reload.paid?
  end

  test "a payment fully linked to an active bill cannot be re-linked to another active bill" do
    # The ended-series exclusion must NOT weaken the double-allocation guard for
    # allocations that belong to a still-active series: a payment already spent
    # on an active bill stays spent.
    payment = entry_for(2000)

    @allocator.allocate!(entry: payment)
    assert @occurrence.reload.paid?

    # A second, still-active bill (the original @rent is left active).
    other = @family.recurring_transactions.create!(
      account: @account,
      name: "Unrelated Active Bill",
      amount: 2000,
      currency: "USD",
      expected_day_of_month: 29,
      last_occurrence_date: Date.current,
      next_expected_date: 1.month.from_now.to_date,
      status: "active",
      manual: true,
      dedup_scope: "unrelated"
    )
    other_occurrence = other.recurring_occurrences.create!(
      family: @family,
      original_due_on: (Date.current + 1.month).beginning_of_month + 9,
      due_on: (Date.current + 1.month).beginning_of_month + 9,
      currency: "USD"
    )

    # The entry is fully consumed by the active allocation, so there is no
    # capacity left for another active bill.
    assert_raises(Allocator::EntryExhaustedError) do
      Allocator.new(other_occurrence).allocate!(entry: payment)
    end
    assert_not other_occurrence.reload.paid?
  end

  test "two equal in-band settlements on a non-manual bill record a price change" do
    series = @family.recurring_transactions.create!(
      name: "Price Watch", account: @account, amount: 2000, currency: "USD",
      status: "active", bill_type: "bill", manual: false,
      dedup_scope: "price-watch-#{SecureRandom.hex(4)}", expected_day_of_month: 29,
      last_occurrence_date: 1.month.ago.to_date, next_expected_date: Date.current + 1.month
    )
    first_due = (Date.current + 1.month).beginning_of_month + 9
    second_due = (Date.current + 2.months).beginning_of_month + 9
    first = series.recurring_occurrences.create!(
      family: @family, original_due_on: first_due, due_on: first_due,
      currency: "USD", expected_amount: 2000, status: "scheduled"
    )
    second = series.recurring_occurrences.create!(
      family: @family, original_due_on: second_due, due_on: second_due,
      currency: "USD", expected_amount: 2000, status: "scheduled"
    )

    Allocator.new(first).allocate!(entry: entry_for(2080))
    Allocator.new(second).allocate!(entry: entry_for(2080))

    RecurringTransaction::PriceChangeDetector.new(@family).detect!

    assert_equal 2080, series.reload.amount
    assert_equal 2080, series.recurring_price_changes.order(:effective_on).last.new_amount
  end

  # A bill's category is the user's statement of what its payments are, so a
  # confirmed payment inherits it instead of being categorized a second time.
  # Only a blank, unlocked category is filled: a locked one is a user, rule or
  # provider decision, and the bill must not undo it.
  test "a confirmed manual attach gives an uncategorized transaction the bill's category" do
    category = bill_category!
    entry = entry_for(2000)

    @allocator.allocate!(entry: entry, categorize: true)

    transaction = entry.transaction.reload
    assert_equal category, transaction.category
    assert_not transaction.locked?(:category_id), "a bill-set category stays overridable by the user"
    assert_equal "recurring_bill", transaction.data_enrichments.find_by(attribute_name: "category_id").source
  end

  # The caller knows whether its user may annotate the transaction, so
  # categorizing is something it asks for, never something it gets by default.
  test "allocate! does not categorize unless asked to" do
    bill_category!
    entry = entry_for(2000)

    @allocator.allocate!(entry: entry)

    assert_nil entry.transaction.reload.category
  end

  test "confirm_suggestion! does not categorize unless asked to" do
    bill_category!
    entry = entry_for(2000)
    allocation = @allocator.allocate_matched!(entry: entry, state: "suggested", confidence: 0.8, signals: {})

    @allocator.confirm_suggestion!(allocation)

    assert_nil entry.transaction.reload.category
  end

  test "a confirmed auto match gives an uncategorized transaction the bill's category" do
    category = bill_category!
    entry = entry_for(2000)

    @allocator.allocate_matched!(entry: entry, state: "confirmed", confidence: 1, signals: {})

    assert_equal category, entry.transaction.reload.category
  end

  test "a suggested match leaves the category alone until the suggestion is confirmed" do
    category = bill_category!
    entry = entry_for(2000)

    allocation = @allocator.allocate_matched!(entry: entry, state: "suggested", confidence: 0.8, signals: {})
    assert_nil entry.transaction.reload.category

    @allocator.confirm_suggestion!(allocation, categorize: true)
    assert_equal category, entry.transaction.reload.category
  end

  test "a transaction that already has a category keeps it" do
    bill_category!
    other = @family.categories.create!(name: "Other pick", color: "#e99537", lucide_icon: "shapes")
    entry = entry_for(2000)
    entry.transaction.update!(category: other)

    @allocator.allocate!(entry: entry, categorize: true)

    assert_equal other, entry.transaction.reload.category
  end

  test "an uncategorized transaction whose category the user locked stays uncategorized" do
    bill_category!
    entry = entry_for(2000)
    entry.transaction.lock_attr!(:category_id)

    @allocator.allocate!(entry: entry, categorize: true)

    assert_nil entry.transaction.reload.category
  end

  test "a bill without a category changes nothing" do
    entry = entry_for(2000)

    @allocator.allocate!(entry: entry, categorize: true)

    assert_nil entry.transaction.reload.category
    assert_empty entry.transaction.data_enrichments
  end

  test "releasing the payment leaves the category the transaction picked up" do
    category = bill_category!
    entry = entry_for(2000)
    allocation = @allocator.allocate!(entry: entry, categorize: true)

    @allocator.unallocate!(allocation)

    assert_equal category, entry.transaction.reload.category,
      "the user may have relied on it; un-matching is not a request to uncategorize"
  end

  test "a payment without a transaction has nothing to categorize" do
    bill_category!

    assert_nothing_raised { @allocator.allocate!(amount: 500, categorize: true) }
  end

  # Auto-matching has no signed-in user, and a bill that is not tied to an
  # account matches anything in the family, so only a person's own confirmation
  # (with their own access) categorizes for it.
  test "an account-less bill does not categorize an auto match" do
    bill_category!
    @rent.update!(account: nil)
    entry = card_entry_for(2000)

    @allocator.allocate_matched!(entry: entry, state: "confirmed", confidence: 1, signals: {})

    assert_nil entry.transaction.reload.category
  end

  test "an account-less bill categorizes when a person confirms the suggestion" do
    category = bill_category!
    @rent.update!(account: nil)
    entry = card_entry_for(2000)
    suggestion = @allocator.allocate_matched!(entry: entry, state: "suggested", confidence: 0.7, signals: {})
    assert_nil entry.transaction.reload.category

    @allocator.confirm_suggestion!(suggestion, categorize: true)

    assert_equal category, entry.transaction.reload.category
  end

  test "an account-less bill does not categorize a repaired orphan on its own" do
    bill_category!
    @rent.update!(account: nil)
    entry = card_entry_for(2000)
    allocation = @allocator.allocate!(entry: entry)

    @allocator.categorize_reattached!(allocation)

    assert_nil entry.transaction.reload.category
  end

  test "a category committed after the matcher loaded the transaction is not overwritten" do
    bill_category!
    other = @family.categories.create!(name: "Picked meanwhile", color: "#e99537", lucide_icon: "shapes")
    entry = entry_for(2000)
    stale = Entry.includes(:entryable).find(entry.id)
    stale.entryable # loaded while still blank

    Transaction.where(id: entry.entryable_id).update_all(category_id: other.id)
    @allocator.allocate_matched!(entry: stale, state: "confirmed", confidence: 1, signals: {})

    assert_equal other, entry.transaction.reload.category
  end

  test "confirming an allocation that is already confirmed does not categorize it later" do
    entry = entry_for(2000)
    allocation = @allocator.allocate_matched!(entry: entry, state: "suggested", confidence: 0.8, signals: {})
    @allocator.confirm_suggestion!(allocation, categorize: true)
    assert_nil entry.transaction.reload.category

    bill_category!
    @allocator.confirm_suggestion!(allocation.reload, categorize: true)

    assert_nil entry.transaction.reload.category
  end

  test "a transaction whose category is not editable is not categorized" do
    bill_category!
    entry = entry_for(2000)
    Transaction.any_instance.stubs(:category_editable?).returns(false)

    @allocator.allocate!(entry: entry, categorize: true)

    assert_nil entry.transaction.reload.category
  end

  test "a category that cannot be written does not stop the payment from being confirmed" do
    bill_category!
    entry = entry_for(2000)
    Transaction.any_instance.stubs(:enrich_attribute).raises(ActiveRecord::InvalidForeignKey)

    allocation = @allocator.allocate!(entry: entry, categorize: true)

    assert allocation.persisted?
    assert allocation.allocation_confirmed?
  end

  test "re-attaching a payment confirmed since it was loaded still categorizes the replacement" do
    category = bill_category!
    entry = entry_for(2000)
    allocation = @allocator.allocate_matched!(entry: entry, state: "suggested", confidence: 0.8, signals: {})
    stale = RecurringAllocation.find(allocation.id)
    assert stale.allocation_suggested?

    allocation.update!(state: "confirmed", source: "user_confirmed")
    @allocator.categorize_reattached!(stale)

    assert_equal category, entry.transaction.reload.category
  end

  test "re-attaching a payment that is still only a suggestion does not categorize" do
    bill_category!
    entry = entry_for(2000)
    allocation = @allocator.allocate_matched!(entry: entry, state: "suggested", confidence: 0.8, signals: {})

    @allocator.categorize_reattached!(allocation)

    assert_nil entry.transaction.reload.category
  end

  private

    def foreign_entry(amount:, currency:)
      account = accounts(:depository)
      account.entries.create!(
        name: "Foreign charge", date: Date.current, amount: amount,
        currency: currency, entryable: Transaction.new
      )
    end

    def usd_occurrence(expected:, due_on: Date.current)
      series = @family.recurring_transactions.create!(
        name: "Bill #{expected} #{due_on}", account: accounts(:depository),
        amount: expected, currency: "USD", status: "active", bill_type: "bill",
        manual: true, dedup_scope: "bill-#{expected}-#{due_on}",
        expected_day_of_month: due_on.day, last_occurrence_date: 1.month.ago.to_date,
        next_expected_date: due_on
      )
      series.recurring_occurrences.destroy_all
      series.recurring_occurrences.create!(
        family: @family, original_due_on: due_on, due_on: due_on,
        currency: "USD", expected_amount: expected, status: "scheduled"
      )
    end
    def bill_category!
      category = @family.categories.create!(name: "Housing bills", color: "#e99537", lucide_icon: "house")
      @rent.update!(category: category)
      category
    end

    def card_entry_for(amount)
      accounts(:credit_card).entries.create!(
        date: Date.current, amount: amount, currency: "USD", name: "Watson Property", entryable: Transaction.new
      )
    end

    def entry_for(amount)
      @account.entries.create!(
        date: Date.current,
        amount: amount,
        currency: "USD",
        name: "Watson Property",
        entryable: Transaction.new
      )
    end

    # A mid-month date the day-29 series never generates, so this manual
    # row can never collide with the generator's rows regardless of what
    # Date.current is when the suite runs.
    def free_occurrence
      free_date = (Date.current + 2.months).beginning_of_month + 9
      @rent.recurring_occurrences.create!(
        family: @family, original_due_on: free_date, due_on: free_date, currency: "USD"
      )
    end
end