Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
56.5 KB
Raw Blame
require "test_helper"

class AccountTest < ActiveSupport::TestCase
  include SyncableInterfaceTest, EntriesTestHelper, ActiveJob::TestHelper

  setup do
    @account = @syncable = accounts(:depository)
    @family = families(:dylan_family)
    @admin = users(:family_admin)
    @member = users(:family_member)
  end

  test "negative_positions lists calculated positions below zero" do
    account = accounts(:investment)
    msft = securities(:msft)
    # A sale without its purchase, as an import or a deleted buy can leave behind.
    account.entries.create!(
      name: "Sell 3 shares of MSFT", date: 3.days.ago.to_date, amount: -1200, currency: "USD",
      entryable: Trade.new(security: msft, qty: -3, price: 400, currency: "USD", investment_activity_label: "Sell")
    )
    Holding::Materializer.new(account, strategy: :forward).materialize_holdings

    positions = account.negative_positions

    assert_equal [ msft ], positions.map(&:security)
    assert_equal 3.days.ago.to_date, positions.first.first_date
    assert_equal(-3, positions.first.lowest_qty)
  end

  test "negative_positions ignores linked accounts" do
    account = accounts(:investment)
    account.entries.create!(
      name: "Sell 3 shares of MSFT", date: 3.days.ago.to_date, amount: -1200, currency: "USD",
      entryable: Trade.new(security: securities(:msft), qty: -3, price: 400, currency: "USD", investment_activity_label: "Sell")
    )
    Holding::Materializer.new(account, strategy: :forward).materialize_holdings
    account.stubs(:linked?).returns(true)

    assert_empty account.negative_positions
  end

  test "negative_positions skips the holdings query for an account without holdings" do
    account = accounts(:depository)
    account.expects(:holdings).never

    assert_empty account.negative_positions
  end

  test "linked cash and investment accounts hide balance updates by default" do
    assert accounts(:connected).hides_balance_updates_by_default?

    investment = accounts(:investment)
    investment.stubs(:linked?).returns(true)
    assert investment.hides_balance_updates_by_default?
  end

  test "manual accounts and linked non-cash accounts show balance updates" do
    assert_not accounts(:depository).hides_balance_updates_by_default?

    property = accounts(:property)
    property.stubs(:linked?).returns(true)
    assert_not property.hides_balance_updates_by_default?
  end

  test "can destroy" do
    assert_difference "Account.count", -1 do
      @account.destroy
    end
  end

  test "a destroy that raises leaves a pending_deletion account disabled" do
    @account.mark_for_deletion!
    @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")

    error = assert_raises(StandardError) { @account.destroy }

    assert_equal "destroy failed", error.message
    assert_equal "disabled", @account.reload.status
  end

  test "a disabled account can be scheduled for deletion again after a failed destroy" do
    @account.mark_for_deletion!
    @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")
    assert_raises(StandardError) { @account.destroy }
    @account.unstub(:cleanup_transfers)
    @account.reload

    assert_enqueued_with(job: DestroyJob, args: [ @account ]) do
      @account.destroy_later
    end
    assert_equal "pending_deletion", @account.reload.status
  end

  test "a failed direct destroy still disables an active account" do
    assert @account.active?
    @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")

    assert_raises(StandardError) { @account.destroy }

    assert_equal "disabled", @account.reload.status
  end

  test "the failed-destroy restore leaves a status written since the record was loaded" do
    @account.mark_for_deletion!
    Account.where(id: @account.id).update_all(status: "active")

    @account.restore_after_failed_destroy

    assert @account.pending_deletion?
    assert_equal "active", @account.reload.status
  end

  test "disable from active is unchanged" do
    assert @account.active?

    @account.disable!

    assert_equal "disabled", @account.reload.status
  end

  test "no event other than the failed-deletion recovery leaves pending_deletion" do
    @account.mark_for_deletion!

    assert_not @account.may_activate?
    assert_not @account.may_enable?
    assert_not @account.may_disable?
    assert_not @account.may_mark_for_deletion?
    permitted = @account.aasm.events(permitted: true).map(&:name)
    assert_equal [ :restore_after_failed_deletion ], permitted
  end

  test "the failed-deletion recovery only fires from pending_deletion" do
    assert @account.active?
    assert_not @account.aasm.may_fire_event?(:restore_after_failed_deletion)

    @account.disable!
    assert_not @account.aasm.may_fire_event?(:restore_after_failed_deletion)
  end

  # disabled_at is the net-worth cutoff for a disabled account
  # (NetWorthSeriesBuilder, NetWorthBreakdownSeriesBuilder). A failed delete
  # must not move it to the failure, which would count the account's balances
  # for the whole time it was already disabled.
  test "a failed destroy of a disabled account keeps its original disabled_at" do
    disabled_at = 40.days.ago.change(usec: 0)
    @account.update_columns(status: "disabled", disabled_at: disabled_at)
    @account.mark_for_deletion!
    @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")

    assert_raises(StandardError) { @account.destroy }

    @account.reload
    assert_equal "disabled", @account.status
    assert_equal disabled_at, @account.disabled_at
  end

  # The other side: an account deleted from active has no cutoff yet, so the
  # failure is when it stopped counting. One disabled and enabled again earlier
  # must not inherit that old cutoff.
  test "a failed destroy of an account that was active stamps disabled_at at the failure" do
    @account.disable!
    @account.enable!
    @account.mark_for_deletion!
    @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")

    failed_at = 3.days.from_now.change(usec: 0)
    travel_to(failed_at) { assert_raises(StandardError) { @account.destroy } }

    @account.reload
    assert_equal "disabled", @account.status
    assert_equal failed_at, @account.disabled_at
  end

  # An account disabled and enabled again before enable cleared disabled_at
  # still carries that date while active or draft. Deleting it must drop the
  # stale date, so a failed destroy stamps the failure instead of reviving it
  # as the account's net-worth cutoff.
  test "a failed destroy of an active or draft account with a stale disabled_at stamps the failure" do
    stale = 1.year.ago.change(usec: 0)
    failed_at = 3.days.from_now.change(usec: 0)

    results = %w[active draft].index_with do |status|
      @account.update_columns(status: status, disabled_at: stale)
      @account.reload.mark_for_deletion!
      @account.stubs(:cleanup_transfers).raises(StandardError, "destroy failed")

      travel_to(failed_at) { assert_raises(StandardError) { @account.destroy } }
      @account.unstub(:cleanup_transfers)

      @account.reload
      [ @account.status, @account.disabled_at ]
    end

    assert_equal({ "active" => [ "disabled", failed_at ], "draft" => [ "disabled", failed_at ] }, results)
  end

  test "marking an active or draft account for deletion clears a stale disabled_at" do
    results = %w[active draft].index_with do |status|
      @account.update_columns(status: status, disabled_at: 1.year.ago)
      @account.reload.mark_for_deletion!
      @account.reload.disabled_at
    end

    assert_equal({ "active" => nil, "draft" => nil }, results)
  end

  test "marking a disabled account for deletion keeps its disabled_at" do
    disabled_at = 40.days.ago.change(usec: 0)
    @account.update_columns(status: "disabled", disabled_at: disabled_at)

    @account.reload.mark_for_deletion!

    assert_equal disabled_at, @account.reload.disabled_at
  end

  test "enabling an account clears disabled_at" do
    @account.disable!
    assert_not_nil @account.reload.disabled_at

    @account.enable!
    assert_nil @account.reload.disabled_at

    @account.disable!
    @account.activate!
    assert_nil @account.reload.disabled_at
  end

  test "default owner prefers a family admin before a super admin" do
    family = families(:empty)
    admin = users(:empty)
    super_admin = users(:sure_support_staff)

    # Fixtures stamp every row with one `created_at`, and this family holds a
    # second admin (`sso_only`), so "the earliest admin" is only meaningful
    # once the timestamps differ. Without this the assertion below rides on
    # whichever admin the query plan happens to return first.
    family.users.update_all(created_at: 1.hour.ago)
    admin.update!(created_at: 2.hours.ago)

    Current.reset

    account = family.accounts.create!(
      name: "Unowned test account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )

    assert_equal admin, account.owner
    assert_not_equal super_admin, account.owner
  end

  test "default owner is stable when two admins share a created_at" do
    family = families(:empty)
    family.users.where(role: "admin").update_all(created_at: 1.hour.ago)

    Current.reset

    owners = 2.times.map do |i|
      family.accounts.create!(
        name: "Unowned tie-break account #{i}",
        balance: 0,
        currency: "USD",
        accountable: Depository.new
      ).owner
    end

    # `id` breaks the tie, so the winner is the tied admin with the lowest id.
    # Asserting only that the two calls agree would pass without the
    # tie-breaker whenever the database returned the same tied row twice.
    expected_owner = family.users.where(role: "admin").order(:id).first

    assert_equal expected_owner, owners.first
    assert_equal expected_owner, owners.last
  end

  test "set_current_balance schedules an account sync by default" do
    @account.expects(:sync_later).once

    assert @account.set_current_balance(1234).success?
  end

  test "set_current_balance skips the account sync when the caller schedules it" do
    @account.expects(:sync_later).never

    assert @account.set_current_balance(1234, schedule_sync: false).success?
    assert_equal 1234, @account.reload.current_anchor_balance
  end

  test "create_and_sync calls sync_later by default" do
    Account.any_instance.expects(:sync_later).once

    account = Account.create_and_sync({
      family: @family,
      owner: @admin,
      name: "Test Account",
      balance: 100,
      currency: "USD",
      accountable_type: "Depository",
      accountable_attributes: {}
    })

    assert account.persisted?
    assert_equal "USD", account.currency
    assert_equal 100, account.balance
  end

  test "create_and_sync skips sync_later when skip_initial_sync is true" do
    Account.any_instance.expects(:sync_later).never

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Linked Account",
        balance: 500,
        currency: "EUR",
        accountable_type: "Depository",
        accountable_attributes: {}
      },
      skip_initial_sync: true
    )

    assert account.persisted?
    assert_equal "EUR", account.currency
    assert_equal 500, account.balance
  end

  test "create_and_sync creates opening anchor with correct currency" do
    Account.any_instance.stubs(:sync_later)

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Test Account",
        balance: 1000,
        currency: "GBP",
        accountable_type: "Depository",
        accountable_attributes: {}
      },
      skip_initial_sync: true
    )

    opening_anchor = account.valuations.opening_anchor.first
    assert_not_nil opening_anchor
    assert_equal "GBP", opening_anchor.entry.currency
    assert_equal 1000, opening_anchor.entry.amount
  end

  test "create_and_sync keeps the entered current balance when it differs from the opening balance" do
    Account.any_instance.stubs(:sync_later)

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Student Loan",
        balance: 8_000,
        currency: "USD",
        accountable_type: "Loan",
        accountable_attributes: { initial_balance: 20_000, rate_type: "fixed", interest_rate: 4.5, term_months: 120 }
      },
      skip_initial_sync: true
    )

    assert_equal 20_000, account.valuations.opening_anchor.first.entry.amount
    # Without a today anchor, the initial sync would walk forward from the
    # opening valuation and overwrite the entered 8,000 with 20,000.
    today_valuation = account.entries.valuations.find_by(date: Date.current)
    assert_not_nil today_valuation
    assert_equal 8_000, today_valuation.amount
  end

  test "create_and_sync leaves the opening anchor alone when the opening balance date is today" do
    Account.any_instance.stubs(:sync_later)

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Student Loan",
        balance: 8_000,
        currency: "USD",
        accountable_type: "Loan",
        accountable_attributes: { initial_balance: 20_000, rate_type: "fixed", interest_rate: 4.5, term_months: 120 }
      },
      skip_initial_sync: true,
      opening_balance_date: Date.current
    )

    # Both balances land on the same day, so today's balance cannot be
    # anchored without reusing (and overwriting) the opening anchor.
    valuations = account.entries.valuations.where(date: Date.current)
    assert_equal 1, valuations.count
    assert_equal 20_000, valuations.first.amount
    assert_equal "opening_anchor", valuations.first.entryable.kind
  end

  test "create_and_sync treats a blank initial balance as absent" do
    Account.any_instance.stubs(:sync_later)

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Student Loan",
        balance: 8_000,
        currency: "USD",
        accountable_type: "Loan",
        accountable_attributes: { initial_balance: "", rate_type: "fixed", interest_rate: 4.5, term_months: 120 }
      },
      skip_initial_sync: true
    )

    assert_equal 8_000, account.valuations.opening_anchor.first.entry.amount
    assert_nil account.entries.valuations.find_by(date: Date.current)
  end

  test "create_and_sync treats a zero initial balance as a real opening balance" do
    Account.any_instance.stubs(:sync_later)

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Student Loan",
        balance: 8_000,
        currency: "USD",
        accountable_type: "Loan",
        accountable_attributes: { initial_balance: 0, rate_type: "fixed", interest_rate: 4.5, term_months: 120 }
      },
      skip_initial_sync: true
    )

    assert_equal 0, account.valuations.opening_anchor.first.entry.amount
    assert_equal 8_000, account.entries.valuations.find_by(date: Date.current)&.amount
  end

  # #3383: only a loan's start date moves the opening anchor.
  test "create_and_sync dates a loan's opening anchor on its start date" do
    Account.any_instance.stubs(:sync_later)
    opening_balance_date = Date.current - 2.years

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Mortgage",
        balance: 399_600,
        currency: "USD",
        accountable_type: "Loan",
        accountable_attributes: { initial_balance: 400_000, start_date: Date.new(2020, 1, 15),
                                  rate_type: "fixed", interest_rate: 5, term_months: 360 }
      },
      skip_initial_sync: true,
      opening_balance_date: opening_balance_date
    )

    assert_equal Date.new(2020, 1, 15), account.valuations.opening_anchor.first.entry.date
    assert_equal 399_600, account.entries.valuations.find_by(date: Date.current)&.amount
  end

  # The anchor is dated on the start date only because Loan rejects a future
  # one: an anchor after today would skip today's reconciliation and leave the
  # account with no balance for today.
  # Entry rejects a date on or before Entry.min_supported_date, so a loan drawn
  # down that long ago keeps the form's opening date, as before the start date
  # moved the anchor. Both sides of the boundary.
  test "a loan whose start date is on the earliest supported entry date keeps the form's opening date" do
    Account.any_instance.stubs(:sync_later)
    opening_balance_date = Date.current - 2.years
    start_date = Entry.min_supported_date

    account = create_loan_with_start_date(start_date, opening_balance_date)

    assert_equal start_date, account.accountable.start_date
    assert_equal opening_balance_date, account.valuations.opening_anchor.first.entry.date
  end

  test "a loan started the day after the earliest supported date is anchored on its start date" do
    Account.any_instance.stubs(:sync_later)
    start_date = Entry.min_supported_date + 1.day

    account = create_loan_with_start_date(start_date, Date.current - 2.years)

    assert_equal start_date, account.valuations.opening_anchor.first.entry.date
  end

  test "create_and_sync rejects a loan with a future start date and writes no anchor" do
    Account.any_instance.stubs(:sync_later)

    assert_no_difference [ "Account.count", "Entry.count" ] do
      assert_raises ActiveRecord::RecordInvalid do
        Account.create_and_sync(
          {
            family: @family,
            owner: @admin,
            name: "Mortgage",
            balance: 399_600,
            currency: "USD",
            accountable_type: "Loan",
            accountable_attributes: { initial_balance: 400_000, start_date: Date.current + 1.month,
                                      rate_type: "fixed", interest_rate: 5, term_months: 360 }
          },
          skip_initial_sync: true,
          opening_balance_date: Date.current - 3.years
        )
      end
    end
  end

  test "create_and_sync keeps a depository account's opening balance date" do
    Account.any_instance.stubs(:sync_later)
    opening_balance_date = Date.new(2020, 1, 15)

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Checking",
        balance: 1_000,
        currency: "USD",
        accountable_type: "Depository",
        accountable_attributes: {}
      },
      skip_initial_sync: true,
      opening_balance_date: opening_balance_date
    )

    assert_equal opening_balance_date, account.valuations.opening_anchor.first.entry.date
  end

  test "a provider-created loan keeps the default opening date" do
    item = SimplefinItem.create!(family: @family, name: "SF Conn", access_url: "https://example.com/access")
    simplefin_account = SimplefinAccount.create!(
      simplefin_item: item, name: "My Loan", account_id: "acct_424",
      currency: "USD", account_type: "loan", current_balance: 5_000
    )

    account = Account.create_from_simplefin_account(simplefin_account, "Loan")

    assert_nil account.accountable.start_date
    assert_equal 2.years.ago.to_date, account.valuations.opening_anchor.first.entry.date
    assert_equal 1, account.entries.valuations.count
  end

  test "create_and_sync uses provided opening balance date" do
    Account.any_instance.stubs(:sync_later)
    opening_date = Time.zone.today

    account = Account.create_and_sync(
      {
        family: @family,
        owner: @admin,
        name: "Test Account",
        balance: 1000,
        currency: "USD",
        accountable_type: "Depository",
        accountable_attributes: {}
      },
      skip_initial_sync: true,
      opening_balance_date: opening_date
    )

    opening_anchor = account.valuations.opening_anchor.first
    assert_equal opening_date, opening_anchor.entry.date
  end

  test "subtype set as a top-level account attribute persists on create" do
    Account.any_instance.stubs(:sync_later)

    # Mirrors the create flow: the form submits `account[subtype]` as a
    # top-level attribute (not nested under accountable_attributes). The
    # accountable does not exist yet, so the delegating writer must build it.
    account = Account.create_and_sync({
      family: @family,
      owner: @admin,
      name: "Savings Account",
      balance: 100,
      currency: "USD",
      accountable_type: "Depository",
      subtype: "savings"
    })

    assert account.persisted?
    assert_equal "savings", account.reload.subtype
    assert_equal "savings", account.accountable.subtype
  end

  test "subtype assigned before accountable is built is not dropped" do
    account = Account.new
    account.accountable_type = "Depository"
    account.subtype = "checking"

    assert_not_nil account.accountable
    assert_equal "checking", account.subtype
  end

  test "subtype assigned before accountable_type is not dropped" do
    # The real controller path: strong-params `permit` preserves filter order,
    # and `account_params` lists `:subtype` before `:accountable_type`, so the
    # subtype writer runs while the type is still unknown.
    account = Account.new
    account.subtype = "savings"
    account.accountable_type = "Depository"

    assert_not_nil account.accountable
    assert_equal "savings", account.subtype
    assert_equal "savings", account.accountable.subtype
  end

  test "subtype persists on create when attributes arrive in permit order" do
    Account.any_instance.stubs(:sync_later)

    # Mirrors `account_params`: `permit` yields keys in filter order, so the
    # create hash carries `subtype` before `accountable_type` — the ordering
    # that previously dropped the subtype on create.
    account = Account.create_and_sync({
      family: @family,
      owner: @admin,
      name: "Savings Account",
      balance: 100,
      subtype: "savings",
      currency: "USD",
      accountable_type: "Depository"
    })

    assert account.persisted?
    assert_equal "savings", account.reload.subtype
    assert_equal "savings", account.accountable.subtype
  end

  test "accountable display names expose singular and group contexts" do
    assert_equal "Investment", Investment.singular_display_name
    assert_equal "Investments", Investment.display_name
    assert_equal "Cash", Depository.singular_display_name
    assert_equal "Cash", Depository.display_name
  end

  test "gets short/long subtype label" do
    investment = Investment.new(subtype: "hsa")
    account = @family.accounts.create!(
      owner: @admin,
      name: "Test Investment",
      balance: 1000,
      currency: "USD",
      accountable: investment
    )

    assert_equal "HSA", account.short_subtype_label
    assert_equal "Health Savings Account", account.long_subtype_label

    # Test with nil subtype
    account.accountable.update!(subtype: nil)
    assert_equal "Investments", account.short_subtype_label
    assert_equal "Investments", account.long_subtype_label
  end

  # Tax treatment tests (TaxTreatable concern)

  test "tax_treatment delegates to accountable for Investment" do
    investment = Investment.new(subtype: "401k")
    account = @family.accounts.create!(
      owner: @admin,
      name: "Test 401k",
      balance: 1000,
      currency: "USD",
      accountable: investment
    )

    assert_equal :tax_deferred, account.tax_treatment
    assert_equal I18n.t("accounts.tax_treatments.tax_deferred"), account.tax_treatment_label
  end

  test "tax_treatment delegates to accountable for Crypto" do
    crypto = Crypto.new(tax_treatment: :taxable)
    account = @family.accounts.create!(
      owner: @admin,
      name: "Test Crypto",
      balance: 500,
      currency: "USD",
      accountable: crypto
    )

    assert_equal :taxable, account.tax_treatment
    assert_equal I18n.t("accounts.tax_treatments.taxable"), account.tax_treatment_label
  end

  test "tax_treatment returns nil for non-HSA depository accounts" do
    # Depository exposes a `tax_treatment` method so HSA cash flips
    # tax-advantaged, but non-HSA subtypes (checking, savings, cd,
    # money_market) return nil. nil still reads as taxable via `taxable?`,
    # and keeps `tax_treatment.present?` false so the header tax badge does
    # not appear on ordinary bank accounts that never displayed it before.
    assert_nil @account.tax_treatment
    assert_nil @account.tax_treatment_label
    assert_not @account.tax_treatment.present?
    assert @account.taxable?
  end

  test "tax_treatment returns nil for accountables that do not implement it" do
    # CreditCard / Loan / Property / OtherAsset / OtherLiability do not
    # implement `tax_treatment`, so the `TaxTreatable#respond_to?` short-
    # circuit still returns nil for them.
    credit_card_account = @family.accounts.create!(
      owner: @admin,
      name: "Test Credit Card",
      balance: 100,
      currency: "USD",
      accountable: CreditCard.new
    )

    assert_nil credit_card_account.tax_treatment
    assert_nil credit_card_account.tax_treatment_label
  end

  test "tax_advantaged? returns true for tax-advantaged accounts" do
    investment = Investment.new(subtype: "401k")
    account = @family.accounts.create!(
      owner: @admin,
      name: "Test 401k",
      balance: 1000,
      currency: "USD",
      accountable: investment
    )

    assert account.tax_advantaged?
    assert_not account.taxable?
  end

  test "tax_advantaged? returns true for HSA depository accounts" do
    hsa_depository = @family.accounts.create!(
      owner: @admin,
      name: "Fidelity HSA Cash",
      balance: 3_000,
      currency: "USD",
      accountable: Depository.new(subtype: "hsa")
    )

    assert_equal :tax_advantaged, hsa_depository.tax_treatment
    assert hsa_depository.tax_advantaged?
    assert_not hsa_depository.taxable?
  end

  test "tax_advantaged? returns false for taxable accounts" do
    investment = Investment.new(subtype: "brokerage")
    account = @family.accounts.create!(
      owner: @admin,
      name: "Test Brokerage",
      balance: 1000,
      currency: "USD",
      accountable: investment
    )

    assert_not account.tax_advantaged?
    assert account.taxable?
  end

  test "taxable? returns true for non-HSA depository accounts" do
    # `@account` is the checking depository fixture; `tax_treatment` is
    # `nil` (no subtype override), which `taxable?` reads as true.
    assert @account.taxable?
    assert_not @account.tax_advantaged?
  end

  test "destroying account purges attached logo" do
    @account.logo.attach(
      io: StringIO.new("fake-logo-content"),
      filename: "logo.png",
      content_type: "image/png"
    )

    attachment_id = @account.logo.id
    assert ActiveStorage::Attachment.exists?(attachment_id)

    perform_enqueued_jobs do
      @account.destroy!
    end

    assert_not ActiveStorage::Attachment.exists?(attachment_id)
  end

  test "destroying account moves linked statements to inbox after commit" do
    statement = AccountStatement.create_from_upload!(
      family: @family,
      account: @account,
      file: uploaded_file(filename: "statement.csv", content_type: "text/csv", content: "date,amount\n2024-01-01,1\n")
    )
    statement.update!(match_confidence: 0.8)

    @account.destroy!

    statement.reload
    assert_nil statement.account_id
    assert_equal "unmatched", statement.review_status
    assert_nil statement.match_confidence
  end

  test "destroying an account keeps a statement an import carries, and the import keeps it" do
    statement = AccountStatement.create_from_upload!(
      family: @family,
      account: @account,
      file: uploaded_file(filename: "statement.csv", content_type: "text/csv", content: "date,amount\n2024-01-01,1\n")
    )
    import = TransactionImport.create!(family: @family, account: @account, account_statement: statement)

    @account.destroy!

    assert_nil statement.reload.account_id
    assert_equal statement.id, import.reload.account_statement_id
  end

  test "rolled back account destroy keeps linked statements unchanged" do
    statement = AccountStatement.create_from_upload!(
      family: @family,
      account: @account,
      file: uploaded_file(filename: "statement.csv", content_type: "text/csv", content: "date,amount\n2024-01-01,1\n")
    )
    statement.update!(match_confidence: 0.8)

    Account.transaction do
      @account.destroy!
      raise ActiveRecord::Rollback
    end

    statement.reload
    assert Account.exists?(@account.id)
    assert_equal @account.id, statement.account_id
    assert_equal "linked", statement.review_status
    assert_equal 0.8.to_d, statement.match_confidence
  end

  # Account sharing tests

  test "owned_by? returns true for account owner" do
    assert @account.owned_by?(@admin)
    assert_not @account.owned_by?(@member)
  end

  test "shared_with? returns true for owner and shared users" do
    assert @account.shared_with?(@admin) # owner
    # depository already shared with member via fixture
    assert @account.shared_with?(@member)
  end

  test "shared? returns true when account has shares" do
    account = accounts(:investment)
    account.account_shares.destroy_all
    assert_not account.shared?

    account.share_with!(@member, permission: "read_only")
    assert account.shared?
  end

  test "permission_for returns correct permission level" do
    assert_equal :owner, @account.permission_for(@admin)

    # depository already shared with member via fixture
    share = @account.account_shares.find_by(user: @member)
    share.update!(permission: "read_write")
    assert_equal :read_write, @account.permission_for(@member)
  end

  test "accessible_by scope returns owned and shared accounts" do
    # Clear existing shares for clean test
    AccountShare.delete_all

    admin_accessible = @family.accounts.accessible_by(@admin)
    member_accessible = @family.accounts.accessible_by(@member)

    # Admin owns all fixture accounts
    assert_equal @family.accounts.count, admin_accessible.count
    # Member has no access (no shares, no owned accounts)
    assert_equal 0, member_accessible.count

    # Share one account
    @account.share_with!(@member, permission: "read_only")
    member_accessible = @family.accounts.accessible_by(@member)
    assert_equal 1, member_accessible.count
    assert_includes member_accessible, @account
  end

  test "included_in_finances_for scope respects include_in_finances flag" do
    AccountShare.delete_all

    @account.share_with!(@member, permission: "read_only", include_in_finances: true)
    assert_includes @family.accounts.included_in_finances_for(@member), @account

    share = @account.account_shares.find_by(user: @member)
    share.update!(include_in_finances: false)
    assert_not_includes @family.accounts.included_in_finances_for(@member), @account
  end

  test "included_in_reports scope excludes accounts marked as exclude_from_reports" do
    included = @family.accounts.create! name: "Included", balance: 100, currency: "USD", accountable: Depository.new
    excluded = @family.accounts.create! name: "Excluded", balance: 200, currency: "USD", accountable: Depository.new, exclude_from_reports: true

    results = @family.accounts.included_in_reports
    assert_includes results, included
    assert_not_includes results, excluded
  end

  test "auto_share_with_family creates shares for all non-owner members" do
    @family.update!(default_account_sharing: "private")

    account = Account.create_and_sync({
      family: @family,
      owner: @admin,
      name: "New Shared Account",
      balance: 100,
      currency: "USD",
      accountable_type: "Depository",
      accountable_attributes: {}
    })

    assert_difference -> { AccountShare.count }, @family.users.where.not(id: @admin.id).count do
      account.auto_share_with_family!
    end

    share = account.account_shares.find_by(user: @member)
    assert_not_nil share
    assert_equal "read_write", share.permission
    assert share.include_in_finances?
  end

  test "auto_share_with_family grants guests read_only and other members read_write" do
    @family.update!(default_account_sharing: "private")
    guest = users(:empty)
    guest.update_columns(family_id: @family.id, role: "guest")

    account = Account.create_and_sync({
      family: @family,
      owner: @admin,
      name: "Guest Permission Account",
      balance: 100,
      currency: "USD",
      accountable_type: "Depository",
      accountable_attributes: {}
    })

    account.auto_share_with_family!

    assert_equal "read_only", account.account_shares.find_by(user: guest).permission
    assert_equal "read_write", account.account_shares.find_by(user: @member).permission
  end

  test "current_holdings prefers latest provider snapshot holdings across currencies" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Linked Brokerage",
      balance: 1000,
      currency: "USD",
      accountable: Investment.new
    )

    coinstats_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
    coinstats_account = coinstats_item.coinstats_accounts.create!(name: "Brokerage", currency: "USD")
    account_provider = AccountProvider.create!(account: account, provider: coinstats_account)

    eur_security = Security.create!(ticker: "ASML", name: "ASML")
    chf_security = Security.create!(ticker: "NOVN", name: "Novartis")

    provider_holding = account.holdings.create!(
      security: eur_security,
      date: Date.current,
      qty: 2,
      price: 500,
      amount: 1000,
      currency: "EUR",
      account_provider: account_provider,
      cost_basis: 450
    )

    account.holdings.create!(
      security: eur_security,
      date: Date.current,
      qty: 2,
      price: 540,
      amount: 1080,
      currency: "USD"
    )

    second_provider_holding = account.holdings.create!(
      security: chf_security,
      date: Date.current,
      qty: 3,
      price: 90,
      amount: 270,
      currency: "CHF",
      account_provider: account_provider,
      cost_basis: 80
    )

    assert_equal [ provider_holding.id, second_provider_holding.id ].sort, account.current_holdings.pluck(:id).sort
    assert_equal %w[CHF EUR], account.current_holdings.pluck(:currency).sort
  end

  test "a closed provider cannot hide an earlier active provider snapshot" do
    account = accounts(:investment)
    account.holdings.delete_all
    item = @family.simplefin_items.create!(name: "Snapshot providers", access_url: "https://example.com/access")
    closed = item.simplefin_accounts.create!(name: "Closed", account_id: "closed", currency: "USD", account_type: "investment", current_balance: 0, raw_holdings_payload: [])
    active_item = @family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")
    active = active_item.coinstats_accounts.create!(name: "Active", currency: "USD")
    closed_link = account.account_providers.create!(provider: closed)
    active_link = account.account_providers.create!(provider: active)
    account.holdings.create!(account_provider: closed_link, security: securities(:aapl), date: Date.current,
      qty: 0, amount: 0, price: 100, currency: "USD")
    holding = account.holdings.create!(account_provider: active_link, security: securities(:aapl), date: Date.yesterday,
      qty: 2, amount: 200, price: 100, currency: "USD")

    assert_equal [ holding.id ], account.current_holdings.pluck(:id)

    holding.update!(account_provider: nil)
    assert_empty account.current_holdings
  end

  test "on account destroyed cascade transfer destroyed" do
    outflow_account = @family.accounts.create!({
      owner: @admin,
      name: "test_account_outflow",
      balance: 100,
      currency: "USD",
      accountable_type: "Depository",
      accountable_attributes: {}
    })
    inflow_account = @family.accounts.create!({
      owner: @admin,
      name: "test_account_inflow",
      balance: 100,
      currency: "USD",
      accountable_type: "Depository",
      accountable_attributes: {}
    })

    transfer = create_transfer(
      from_account: outflow_account,
      to_account: inflow_account,
      amount: 50
    )

    outflow_transaction = transfer.outflow_transaction

    outflow_transaction.reload
    assert_equal "funds_movement", outflow_transaction.kind

    inflow_account.destroy!

    assert_raises(ActiveRecord::RecordNotFound) { transfer.reload }

    outflow_transaction.reload
    assert_equal "standard", outflow_transaction.kind
  end

  test "cleanup transfers preloads transaction associations" do
    counterparty = @family.accounts.create!(
      owner: @admin,
      name: "Transfer counterparty",
      balance: 100,
      currency: "USD",
      accountable: Depository.new
    )
    transfers = 3.times.map do |index|
      create_transfer(
        from_account: @account,
        to_account: counterparty,
        amount: 10 + index
      )
    end

    queries = capture_sql_queries { @account.send(:cleanup_transfers) }

    assert_empty queries.grep(/SELECT "transactions"\.\* FROM "transactions" WHERE "transactions"\."id" =/)
    assert transfers.all? { |transfer| !Transfer.exists?(transfer.id) }
  end

  test "projected_balance_money nets scheduled entries against an asset account's balance" do
    create_transaction(account: @account, amount: 200, date: 3.days.from_now.to_date) # expense, decreases balance
    create_transaction(account: @account, amount: -50, date: 5.days.from_now.to_date) # income, increases balance

    assert_equal Money.new(-150, "USD"), @account.scheduled_entries_total_money
    assert_equal Money.new(4850, "USD"), @account.projected_balance_money
  end

  test "projected_balance_money keeps the sign for a liability account (expense increases debt)" do
    credit_card = accounts(:credit_card)
    create_transaction(account: credit_card, amount: 200, date: 3.days.from_now.to_date) # expense, increases debt

    assert_equal Money.new(200, "USD"), credit_card.scheduled_entries_total_money
    assert_equal Money.new(1200, "USD"), credit_card.projected_balance_money
  end

  test "projected_balance_money ignores transactions on non-cash valuation-only accounts" do
    property = accounts(:property)
    create_transaction(account: property, amount: 5000, date: 3.days.from_now.to_date)

    assert_equal Money.new(0, "USD"), property.scheduled_entries_total_money
    assert_equal property.balance_money, property.projected_balance_money
  end

  test "scheduled_entries_total_money ignores future-dated valuations and buy/sell trades" do
    create_valuation(account: @account, amount: 999_999, date: 3.days.from_now.to_date)

    investment = accounts(:investment)
    create_trade(securities(:aapl), account: investment, qty: 1, price: 100, date: 3.days.from_now.to_date)

    assert_equal Money.new(0, "USD"), @account.scheduled_entries_total_money
    assert_equal Money.new(0, "USD"), investment.scheduled_entries_total_money
  end

  test "scheduled_entries_total_money includes future-dated interest/dividend trades" do
    investment = accounts(:investment)

    Entry.create!(
      account: investment,
      name: "Interest",
      date: 3.days.from_now.to_date,
      currency: "USD",
      amount: -50,
      entryable: Trade.new(qty: 0, price: 0, currency: "USD", security: securities(:aapl), investment_activity_label: "Interest")
    )

    assert_equal Money.new(50, "USD"), investment.scheduled_entries_total_money
    assert_equal investment.balance_money + Money.new(50, "USD"), investment.projected_balance_money
  end

  test "projected_balance_money equals balance_money when nothing is scheduled" do
    assert_equal Money.new(0, "USD"), @account.scheduled_entries_total_money
    assert_equal @account.balance_money, @account.projected_balance_money
  end

  test "scheduled_entries_total_money uses custom exchange_rate when present" do
    eur_account = @family.accounts.create!(
      name: "EUR Checking",
      balance: 1000,
      currency: "EUR",
      accountable: Depository.new
    )

    transaction = Transaction.new
    transaction.exchange_rate = 1.10

    Entry.create!(
      account: eur_account,
      name: "Scheduled FX expense",
      date: 3.days.from_now.to_date,
      currency: "USD",
      amount: 100,
      entryable: transaction
    )

    # 100 USD × custom_rate 1.10 = 110 EUR, negated because asset
    assert_equal Money.new(-110, "EUR"), eur_account.scheduled_entries_total_money
  end

  test "scheduled_entries_total_money drops entries with no available exchange rate instead of using the raw amount" do
    create_transaction(account: @account, amount: 200, date: 3.days.from_now.to_date, currency: "USD")
    create_transaction(account: @account, amount: 100, date: 3.days.from_now.to_date, currency: "EUR")

    # The EUR entry has no exchange rate to USD in this test and none can be
    # fetched -- it must be dropped, not added in as a raw 100, to match how
    # Balance::SyncCache drops it once the date arrives (see #1143).
    assert_equal Money.new(-200, "USD"), @account.scheduled_entries_total_money
  end

  test "scheduled_entries_total_money(through_date:) only sums entries on or before that date" do
    create_transaction(account: @account, amount: 200, date: 3.days.from_now.to_date)
    create_transaction(account: @account, amount: 50, date: 10.days.from_now.to_date)

    assert_equal Money.new(-200, "USD"), @account.scheduled_entries_total_money(through_date: 5.days.from_now.to_date)
    assert_equal Money.new(-250, "USD"), @account.scheduled_entries_total_money(through_date: 10.days.from_now.to_date)
  end

  test "scheduled entries are loaded once per instance and reloaded on reload" do
    create_transaction(account: @account, amount: 200, date: 3.days.from_now.to_date)
    create_transaction(account: @account, amount: 50, date: 10.days.from_now.to_date)

    queries = 0
    counter = ->(*, payload) { queries += 1 if payload[:sql].include?("entries.date >") }
    ActiveSupport::Notifications.subscribed(counter, "sql.active_record") do
      @account.scheduled_entries_total_money
      @account.projected_balance_money
      @account.scheduled_entries_total_money(through_date: 3.days.from_now.to_date)
      @account.scheduled_entries_total_money(through_date: 10.days.from_now.to_date)
    end
    assert_equal 1, queries

    create_transaction(account: @account, amount: 25, date: 5.days.from_now.to_date)
    assert_equal Money.new(-275, "USD"), @account.reload.scheduled_entries_total_money
  end

  test "history_start_date resolves to the earliest of opening anchor, entries, and balances" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "History Test Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    assert_nil account.history_start_date

    anchor_date = 30.days.ago.to_date
    account.set_opening_anchor_balance(balance: 100, date: anchor_date)
    assert_equal anchor_date, account.history_start_date

    earlier_entry_date = 45.days.ago.to_date
    account.entries.create!(
      name: "Past Entry",
      date: earlier_entry_date,
      amount: 50,
      currency: "USD",
      entryable: Transaction.new
    )
    assert_equal earlier_entry_date, account.history_start_date
  end

  test "history_start_date returns nil when account has no opening anchor, entries, or balances" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Empty History Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    assert_nil account.history_start_date
  end

  test "history_start_date resolves to transaction date when there is no opening valuation" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Transaction Only Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    trade_date = 2.years.ago.to_date
    account.entries.create!(
      name: "Old Trade",
      date: trade_date,
      amount: 500,
      currency: "USD",
      entryable: Transaction.new
    )

    assert_equal trade_date, account.history_start_date
  end

  test "history_start_date resolves to balance date when there are no entries or opening valuation" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Balance Only Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    balance_date = 1.year.ago.to_date
    account.balances.create!(
      date: balance_date,
      balance: 1000,
      currency: "USD"
    )

    assert_equal balance_date, account.history_start_date
  end

  test "history_start_date prefers earlier transaction when valuation is added much later" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Late Valuation Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    trade_date = 2.years.ago.to_date
    account.entries.create!(
      name: "Initial Buy",
      date: trade_date,
      amount: 100,
      currency: "USD",
      entryable: Transaction.new
    )

    # Later reconciliation valuation added 6 months ago
    account.set_opening_anchor_balance(balance: 500, date: 6.months.ago.to_date)

    assert_equal trade_date, account.history_start_date
  end

  test "history_start_date handles transaction from 10 years ago" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Decade Old Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    ten_years_ago = 10.years.ago.to_date
    account.entries.create!(
      name: "Decade Ago Trade",
      date: ten_years_ago,
      amount: 250,
      currency: "USD",
      entryable: Transaction.new
    )

    assert_equal ten_years_ago, account.history_start_date
  end

  test "history_start_date ignores pending transactions" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Pending Account",
      balance: 0,
      currency: "USD",
      accountable: Depository.new
    )
    account.entries.destroy_all
    account.balances.destroy_all

    posted_date = 5.days.ago.to_date
    account.entries.create!(
      name: "Posted Entry",
      date: posted_date,
      amount: 100,
      currency: "USD",
      entryable: Transaction.new
    )

    account.entries.create!(
      name: "Pending Entry",
      date: 10.days.ago.to_date,
      amount: 50,
      currency: "USD",
      entryable: Transaction.new(extra: { "plaid" => { "pending" => true } })
    )

    assert_equal posted_date, account.history_start_date
  end

  test "history_start_date on linked investment account resolves to provider activity ignoring default anchor" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Linked Investment Account",
      balance: 0,
      currency: "USD",
      accountable: Investment.new,
      plaid_account: plaid_accounts(:one)
    )
    assert account.linked?

    default_anchor_date = 2.years.ago.to_date
    account.set_opening_anchor_balance(balance: 0, date: default_anchor_date)

    recent_trade_date = 14.days.ago.to_date
    account.entries.create!(
      name: "Recent Provider Trade",
      date: recent_trade_date,
      amount: 100,
      currency: "USD",
      source: "plaid",
      entryable: Transaction.new
    )

    assert_equal recent_trade_date, account.history_start_date
  end

  test "history_start_date on linked investment account returns nil when no provider activity exists" do
    account = @family.accounts.create!(
      owner: @admin,
      name: "Fresh Linked Investment",
      balance: 0,
      currency: "USD",
      accountable: Investment.new,
      plaid_account: plaid_accounts(:one)
    )
    assert account.linked?

    default_anchor_date = 2.years.ago.to_date
    account.set_opening_anchor_balance(balance: 0, date: default_anchor_date)

    assert_nil account.history_start_date
  end

  test "annotatable_by includes owned and annotate-tier shares but not read-only ones" do
    member = users(:family_member)
    ids = Account.annotatable_by(member).pluck(:id)

    assert_includes ids, accounts(:depository).id, "full_control share"
    assert_not_includes ids, accounts(:credit_card).id, "read_only share"

    accounts(:credit_card).account_shares.find_by!(user: member).update!(permission: "read_write")
    assert_includes Account.annotatable_by(member).pluck(:id), accounts(:credit_card).id, "read_write share"
  end

  test "linked_to_goal? is true only for an account linked to a goal" do
    account = @family.accounts.create!(name: "Goal savings", balance: 0, currency: "USD", accountable: Depository.new)

    assert_not account.linked_to_goal?

    goals(:vacation_italy).goal_accounts.create!(account: account)

    assert account.linked_to_goal?
  end

  test "institution_domain is normalized to a bare host" do
    {
      "https://www.tbank.ru/" => "tbank.ru",
      "https://www.krungsri.com/en/personal" => "krungsri.com",
      "  HTTP://Chase.com:443/?q=1#top " => "chase.com",
      "www.example.co.uk" => "example.co.uk",
      "sberbank.ru" => "sberbank.ru"
    }.each do |input, expected|
      @account.update!(institution_domain: input)
      assert_equal expected, @account.read_attribute(:institution_domain), "for #{input.inspect}"
    end

    @account.update!(institution_domain: "   ")
    assert_nil @account.read_attribute(:institution_domain)
  end

  test "logo_url builds a Brandfetch URL from a legacy unnormalized domain" do
    Setting.stubs(:brand_fetch_client_id).returns("test-client-id")
    @account.update_column(:institution_domain, "https://www.tbank.ru/")

    assert_match %r{\Ahttps://cdn\.brandfetch\.io/tbank\.ru/icon/}, @account.logo_url
  end

  test "an uploaded logo wins over the Brandfetch icon" do
    Setting.stubs(:brand_fetch_client_id).returns("test-client-id")
    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "logo.png")

    assert_equal Rails.application.routes.url_helpers.rails_blob_path(@account.logo, only_path: true), @account.logo_url
  end

  test "logo must be a small raster image" do
    @account.logo = { io: file_fixture("test.txt").open, filename: "logo.txt" }
    assert_not @account.valid?
    assert @account.errors.added?(:logo, :invalid_content_type)

    @account.logo = { io: StringIO.new("\x89PNG\r\n\x1a\n".b + "\x00" * Account::LOGO_MAX_SIZE), filename: "logo.png", content_type: "image/png" }
    assert_not @account.valid?
    assert @account.errors.added?(:logo, :too_large, max_size: 1)
  end

  test "choosing Brandfetch shows it while keeping the stored logo" do
    @account.update!(institution_domain: "tbank.ru", logo_selection: "brandfetch")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "logo.png")

    Setting.stubs(:brand_fetch_client_id).returns("test-client-id")
    assert_match %r{\Ahttps://cdn\.brandfetch\.io/tbank\.ru/}, @account.logo_url
    assert @account.logo.attached?

    Setting.stubs(:brand_fetch_client_id).returns(nil)
    assert_equal Rails.application.routes.url_helpers.rails_blob_path(@account.logo, only_path: true), @account.logo_url
  end

  test "choose_logo switches between Brandfetch and the stored logo without deleting it" do
    Setting.stubs(:brand_fetch_client_id).returns("test-client-id")
    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "mine.png")
    Account::LogoFetcher.any_instance.expects(:fetch).never

    assert @account.choose_logo("brandfetch")
    assert @account.reload.logo_brandfetch?

    assert @account.choose_logo("stored")
    assert @account.reload.logo_automatic?
    assert @account.logo.attached?
  end

  test "choose_logo downloads a picked icon by its key" do
    Account::LogoFetcher.any_instance.expects(:fetch).with(source: "appstore:123").returns(true)

    assert @account.choose_logo("appstore:123")
  end

  test "removing the stored logo brings back the fallback" do
    Setting.stubs(:brand_fetch_client_id).returns("test-client-id")
    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "mine.png")
    assert_equal @account.stored_logo_url, @account.logo_url

    @account.choose_logo("remove")

    assert_equal @account.brandfetch_logo_url, @account.reload.logo_url
    assert_equal @account.fallback_logo_url, @account.logo_url
  end

  test "clearing the domain drops an automatic icon but keeps an uploaded one" do
    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "tbank.ru.png",
                         metadata: { "logo_source" => "google", "logo_key" => "google", "logo_auto" => true })
    @account.update!(institution_domain: "")
    assert_not @account.reload.logo.attached?

    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "mine.png")
    @account.update!(institution_domain: "")
    assert @account.reload.logo.attached?
  end

  test "stored_logo? matches a favicon key only for the domain it was fetched for" do
    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "tbank.ru.png",
                         metadata: { "logo_source" => "google", "logo_key" => "google" })
    assert @account.stored_logo?("google")

    @account.institution_domain = "sberbank.ru"
    assert_not @account.stored_logo?("google")
  end

  test "a stored logo from before the upload check does not block saves" do
    # Attached around the validation, as a logo stored before it existed.
    blob = ActiveStorage::Blob.create_and_upload!(io: StringIO.new("<svg/>"), filename: "old.svg", content_type: "image/svg+xml")
    ActiveStorage::Attachment.create!(name: "logo", record: @account, blob: blob)
    assert @account.reload.logo.attached?

    assert @account.update(name: "Renamed")
  end

  test "clearing the domain keeps the automatic icon when the provider supplies one" do
    @account.update!(institution_domain: "tbank.ru")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "tbank.ru.png",
                         metadata: { "logo_source" => "google", "logo_key" => "google", "logo_auto" => true })
    @account.stubs(:provider).returns(stub(institution_domain: "tbank.ru"))

    assert_enqueued_with(job: AccountLogoFetchJob) { @account.update!(institution_domain: "") }
    assert @account.reload.logo.attached?
  end

  test "uploading or removing a logo touches the account" do
    @account.update_columns(updated_at: 1.day.ago)
    @account.update!(logo: { io: file_fixture("square-placeholder.png").open, filename: "mine.png", content_type: "image/png" })
    assert_operator @account.reload.updated_at, :>, 1.hour.ago

    @account.update_columns(updated_at: 1.day.ago)
    @account.choose_logo("remove")
    assert_operator @account.reload.updated_at, :>, 1.hour.ago
  end

  test "choose_logo removes the stored logo" do
    @account.update!(logo_selection: "brandfetch")
    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "mine.png")

    assert @account.choose_logo("remove")
    assert_not @account.reload.logo.attached?
    assert @account.logo_automatic?
  end

  test "only empty or automatically picked logos are fetched automatically" do
    @account.update!(institution_domain: "tbank.ru")
    assert @account.logo_auto_fetchable?

    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "mine.png")
    assert_not @account.logo_auto_fetchable?, "an uploaded logo stays"

    @account.logo.attach(io: file_fixture("square-placeholder.png").open, filename: "tbank.ru.png",
                         metadata: { "logo_source" => "google", "logo_auto" => true })
    assert @account.logo_auto_fetchable?

    @account.update!(logo_selection: "brandfetch")
    assert_not @account.logo_auto_fetchable?, "a chosen Brandfetch logo stays"

    @account.update!(logo_selection: "automatic", institution_domain: nil)
    assert_not @account.logo_auto_fetchable?
  end

  test "setting or changing the institution domain fetches a logo in the background" do
    assert_enqueued_with(job: AccountLogoFetchJob, args: [ @account ]) do
      @account.update!(institution_domain: "tbank.ru")
    end

    assert_no_enqueued_jobs(only: AccountLogoFetchJob) do
      @account.update!(name: "Renamed")
      @account.update!(institution_domain: nil)
    end
  end

  private
    def create_loan_with_start_date(start_date, opening_balance_date)
      Account.create_and_sync(
        {
          family: @family,
          owner: @admin,
          name: "Mortgage",
          balance: 399_600,
          currency: "USD",
          accountable_type: "Loan",
          accountable_attributes: { initial_balance: 400_000, start_date: start_date,
                                    rate_type: "fixed", interest_rate: 5, term_months: 360 }
        },
        skip_initial_sync: true,
        opening_balance_date: opening_balance_date
      )
    end
end