Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
114 KB
Raw Blame
require "test_helper"

class TransactionsControllerTest < ActionDispatch::IntegrationTest
  include EntryableResourceInterfaceTest, EntriesTestHelper
  include ActionView::RecordIdentifier

  setup do
    sign_in @user = users(:family_admin)
    @entry = entries(:transaction)
  end

  # Bills has always linked out to transactions. Until now nothing linked back,
  # so a transaction that settled a bill was a dead end. The link-back is part
  # of the preview-gated bills surface, so the viewer needs the flag.
  test "index lazy loads filters with selected merchants in the frame URL" do
    get transactions_url(q: { merchants: [ "Amazon" ] })

    assert_response :success
    assert_select "#transactions-search turbo-frame#transaction-filters[loading=lazy][src=?]",
                  filters_transactions_path(q: { merchants: [ "Amazon" ] })
    assert_select "input[type=checkbox][name='q[merchants][]']", count: 0
  end

  test "show lists split rows in the order they were entered" do
    entry = create_transaction(amount: 100, name: "Split parent", account: accounts(:depository))
    entry.split!([ { name: "Part 1", amount: 60 }, { name: "Part 2", amount: 40 } ])

    queries = []
    ActiveSupport::Notifications.subscribed(->(*, payload) { queries << payload[:sql] }, "sql.active_record") do
      get transaction_url(entry), headers: { "Turbo-Frame" => "drawer" }
    end

    assert_response :success
    # Without an ORDER BY PostgreSQL returns the rows in physical order.
    row_reads = queries.select { |sql| sql.include?('"entries"."parent_entry_id" =') && !sql.include?("SELECT 1 AS one") }
    assert row_reads.any?, "expected the split rows to be read"
    assert row_reads.all? { |sql| sql.include?('ORDER BY "entries"."created_at" ASC, "entries"."id" ASC') }, row_reads.join("\n")
    names = css_select("p.font-medium").map(&:text).select { |text| text.start_with?("Part ") }
    assert_equal [ "Part 1", "Part 2" ], names
  end

  test "a German transaction shows the bill it paid with localized copy" do
    @user.update!(locale: "de")
    @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))
    series = @user.family.recurring_transactions.create!(
      account: accounts(:depository), name: "Watson Property", amount: 2000,
      currency: "USD", expected_day_of_month: 9, status: "active", manual: true,
      bill_type: "bill", last_occurrence_date: Date.current,
      next_expected_date: Date.current
    )
    series.recurring_occurrences.destroy_all
    due = Date.current.beginning_of_month + 8
    occurrence = series.recurring_occurrences.create!(
      family: @user.family, original_due_on: due, due_on: due,
      currency: "USD", expected_amount: 2000, status: "scheduled"
    )
    RecurringTransaction::Allocator.new(occurrence).allocate!(entry: @entry)

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_match "Watson Property", response.body
    assert_match bill_path(series), response.body, "the bill must be reachable from the transaction"

    translations = {
      "transactions.show.create_bill" => "Rechnung hinzufügen",
      "transactions.show.applied_to_title" => "Damit bezahlte Rechnungen",
      "transactions.show.applied_to_detail" => "%{amount} für die am %{date} fällige Rechnung",
      "transactions.show.applied_to_unreviewed" => "Prüfung erforderlich"
    }
    translations.each do |key, text|
      assert_equal text, I18n.t(key, locale: :de, fallback: false)
    end

    assert_match translations.fetch("transactions.show.create_bill"), response.body
    assert_match translations.fetch("transactions.show.applied_to_title"), response.body
    assert_match(/für die am .* fällige Rechnung/, response.body)
  end

  test "the drawer lists only the active bill a payment paid, not an ended twin's allocation" do
    @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))
    due = Date.current.beginning_of_month + 8
    series_for = lambda do |name, status|
      series = @user.family.recurring_transactions.create!(
        account: accounts(:depository), name: name, amount: 2000,
        currency: "USD", expected_day_of_month: 9, status: status, manual: true,
        bill_type: "bill", last_occurrence_date: Date.current,
        next_expected_date: Date.current
      )
      series.recurring_occurrences.destroy_all
      series.recurring_occurrences.create!(
        family: @user.family, original_due_on: due, due_on: due,
        currency: "USD", expected_amount: 2000, status: "scheduled"
      )
    end
    ended = series_for.call("Ended Twin", "ended")
    active = series_for.call("Active Twin", "active")
    [ ended, active ].each do |occurrence|
      occurrence.allocations.create!(entry: @entry, allocated_amount: 2000, currency: "USD",
                                     source: "auto_matched", state: "confirmed")
    end

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_match "Active Twin", response.body
    assert_no_match "Ended Twin", response.body
  end

  # The dialog route is GET only. Rendered as a button_to, "Create a bill"
  # POSTed to it and the modal frame showed "Content Missing".
  test "create a bill opens the prefilled dialog with a GET" do
    @user.update!(preferences: (@user.preferences || {}).merge("preview_features_enabled" => true))

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    href = new_recurring_transaction_path(entry_id: @entry.id)
    assert_select "a[href=?][data-turbo-frame=modal]", href, text: /Create a bill/
    assert_select "form[action=?]", href, count: 0

    get href, headers: { "Turbo-Frame" => "modal" }

    assert_response :success
    assert_select "turbo-frame#modal"
  end

  test "the bill link-back stays hidden without preview access" do
    series = @user.family.recurring_transactions.create!(
      account: accounts(:depository), name: "Watson Property", amount: 2000,
      currency: "USD", expected_day_of_month: 9, status: "active", manual: true,
      bill_type: "bill", last_occurrence_date: Date.current,
      next_expected_date: Date.current
    )
    series.recurring_occurrences.destroy_all
    due = Date.current.beginning_of_month + 8
    occurrence = series.recurring_occurrences.create!(
      family: @user.family, original_due_on: due, due_on: due,
      currency: "USD", expected_amount: 2000, status: "scheduled"
    )
    RecurringTransaction::Allocator.new(occurrence).allocate!(entry: @entry)

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_no_match bill_path(series), response.body,
      "the preview-gated bill link must not render for a user without the flag"
  end

  test "index explains a pending transfer match in its tooltip and opens the transfer" do
    outflow = create_transaction(account: accounts(:depository), name: "Match outflow", amount: 320, date: Date.current)
    inflow = create_transaction(account: accounts(:credit_card), name: "Match inflow", amount: -320, date: Date.current)
    transfer = Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")

    get transactions_url

    assert_response :success
    match = "##{dom_id(outflow.transaction, :transfer_match)}"
    assert_select "#{match} a[href='#{transfer_path(transfer)}'][data-turbo-frame=drawer]" do
      assert_select "span", text: I18n.t("transactions.transfer_match.auto_matched"), minimum: 1
    end
    tooltip = "$320.00 in #{accounts(:credit_card).name} on #{I18n.l(Date.current, format: :long)}. Click to review."
    assert_select "#{match} [role=tooltip]", text: tooltip
    # The amount is blurred in privacy mode, which a native title would escape.
    assert_select "#{match} [role=tooltip] span.privacy-sensitive", text: "$320.00"
    assert_select "#{match} [title*='320']", count: 0
    # The link, which takes focus, is described by the tooltip itself.
    tooltip_id = css_select("#{match} [role=tooltip]").first["id"]
    assert_select "#{match} a[href='#{transfer_path(transfer)}'][aria-describedby=?]", tooltip_id

    # The index shows one leg of a pair; the other leg's row is on its own account's page.
    get account_url(accounts(:credit_card))

    assert_response :success
    assert_select "##{dom_id(inflow.transaction, :transfer_match)} a[href='#{transfer_path(transfer)}']"
    assert_select "##{dom_id(inflow.transaction, :transfer_match)} [role=tooltip]", text: /#{Regexp.escape(accounts(:depository).name)}/
  end

  test "index does not name or link a pending match's other side in an account the viewer cannot see" do
    # depository is shared with family_member; investment is not.
    outflow = create_transaction(account: accounts(:depository), name: "Private match outflow", amount: 320, date: Date.current)
    inflow = create_transaction(account: accounts(:investment), name: "Private match inflow", amount: -320, date: Date.current)
    transfer = Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")
    sign_in users(:family_member)

    get transactions_url

    assert_response :success
    match = "##{dom_id(outflow.transaction, :transfer_match)}"
    assert_select "#{match} [role=tooltip]", text: I18n.t("transactions.transfer_match.auto_matched_tooltip_hidden")
    assert_select "#{match} a[href='#{transfer_path(transfer)}']", count: 0
    assert_select "#{match} span[tabindex='0'] [data-testid='transfer-match-pill']", minimum: 1
    assert_select "#{match} span[tabindex='0'][aria-describedby=?]", css_select("#{match} [role=tooltip]").first["id"]
    assert_select match, text: /#{Regexp.escape(accounts(:investment).name)}/, count: 0
  end

  test "index gives a pending match on a transfer row no second link to the transfer" do
    outflow = create_transaction(account: accounts(:depository), name: "Moved match outflow", amount: 320, date: Date.current, kind: "funds_movement")
    inflow = create_transaction(account: accounts(:credit_card), name: "Moved match inflow", amount: -320, date: Date.current)
    transfer = Transfer.create!(outflow_transaction: outflow.transaction, inflow_transaction: inflow.transaction, status: "pending")

    get transactions_url

    assert_response :success
    match = "##{dom_id(outflow.transaction, :transfer_match)}"
    assert_select "#{match} a[href='#{transfer_path(transfer)}']", count: 0
    assert_select "#{match} [role=tooltip]", text: /#{Regexp.escape(accounts(:credit_card).name)}/
  end

  test "index points an unmatched transaction with a counterpart to the transfer matcher" do
    outflow = create_transaction(account: accounts(:depository), name: "Hint outflow", amount: 500, date: Date.current)
    inflow = create_transaction(account: accounts(:credit_card), name: "Hint inflow", amount: -500, date: Date.current)
    lone = create_transaction(account: accounts(:depository), name: "Hint lone", amount: 77, date: Date.current)

    get transactions_url

    assert_response :success
    [ outflow, inflow ].each do |entry|
      assert_select "a[href='#{new_transaction_transfer_match_path(entry)}'][data-turbo-frame=modal]",
                    text: I18n.t("transactions.transaction.transfer_hint")
    end
    assert_select "a[href='#{new_transaction_transfer_match_path(lone)}']", count: 0
    assert_select "##{dom_id(outflow.transaction, :transfer_hint)} form[action='#{transaction_transfer_match_path(outflow)}'] input[name=_method][value=delete]"
  end

  test "index links a transaction's merchant to its page" do
    merchant = merchants(:netflix)
    create_transaction(account: accounts(:depository), name: "Netflix", merchant: merchant, date: Date.current)

    get transactions_url

    assert_response :success
    assert_select "a[href='#{merchant_path(merchant)}'][data-turbo-frame=_top][title=?]", I18n.t("transactions.transaction.view_merchant")
  end

  test "the investment activity badge shows translated labels" do
    @user.update!(locale: "ru")
    accounts(:investment).entries.create!(
      name: "Broker cash move", date: Date.current, amount: 100, currency: "USD",
      entryable: Transaction.new(investment_activity_label: "Transfer")
    )

    get transactions_url

    assert_response :success
    assert_select "[data-activity-label-quick-edit-target=badge]", text: /Перевод/
    assert_select "[data-activity-label-quick-edit-target=badge]", text: /Transfer/, count: 0
    # The picker shows translated names but still submits the stored label.
    assert_select "button[data-label='Sweep In']", text: "Автоперевод (зачисление)"
  end

  # The form passes its own change action on the currency select; the money
  # field must keep its handleCurrencyChange alongside it, or switching the
  # currency never refreshes the amount's step and precision.
  test "new form wires the currency select to both the money field and the transaction form" do
    get new_transaction_path

    assert_response :success
    assert_select "select[data-money-field-target=currency][data-action=?]",
                  "change->money-field#handleCurrencyChange change->transaction-form#onCurrencyChange"
  end

  test "creates with transaction details" do
    assert_difference [ "Entry.count", "Transaction.count" ], 1 do
      post transactions_url, params: {
        entry: {
          account_id: @entry.account_id,
          name: "New transaction",
          date: Date.current,
          currency: "USD",
          amount: 100,
          nature: "inflow",
          entryable_type: @entry.entryable_type,
          entryable_attributes: {
            tag_ids: [ tags(:one).id, tags(:two).id ],
            category_id: Category.first.id,
            merchant_id: Merchant.first.id
          }
        }
      }
    end

    created_entry = Entry.order(:created_at).last

    assert_redirected_to account_url(created_entry.account)
    assert_equal "Transaction created", flash[:notice]
    assert_enqueued_with(job: SyncJob)
  end

  test "resubmitting the same idempotency key does not create a duplicate transaction" do
    idempotency_key = SecureRandom.uuid
    params = {
      entry: {
        account_id: @entry.account_id,
        name: "New transaction",
        date: Date.current,
        currency: "USD",
        amount: 100,
        nature: "inflow",
        entryable_type: @entry.entryable_type,
        entryable_attributes: { category_id: Category.first.id },
        idempotency_key: idempotency_key
      }
    }

    assert_difference [ "Entry.count", "Transaction.count" ], 1 do
      post transactions_url, params: params
    end
    assert_response :redirect
    first_entry = Entry.order(:created_at).last

    # Simulates a double-click or a browser retry: same form, same
    # idempotency key, submitted again after the first request already
    # completed and committed.
    assert_no_difference [ "Entry.count", "Transaction.count" ] do
      post transactions_url, params: params
    end
    assert_response :redirect
    assert_equal "Transaction created", flash[:notice]
    assert_redirected_to account_url(first_entry.account)
  end

  test "the idempotency key does not mark the created transaction as provider-linked" do
    # Regression test: the idempotency key must not be stored in
    # external_id/source (Entry#linked? = external_id.present?), or a plain
    # manual entry would incorrectly look provider-synced - disabling its
    # editable fields in the UI and hiding it from future provider dedup.
    post transactions_url, params: {
      entry: {
        account_id: @entry.account_id,
        name: "New transaction",
        date: Date.current,
        currency: "USD",
        amount: 100,
        nature: "inflow",
        entryable_type: @entry.entryable_type,
        entryable_attributes: { category_id: Category.first.id },
        idempotency_key: SecureRandom.uuid
      }
    }

    created_entry = Entry.order(:created_at).last
    assert_not created_entry.linked?
    assert_nil created_entry.external_id
    assert_nil created_entry.source
  end

  test "handles a genuine concurrent double-submit without raising or duplicating" do
    idempotency_key = SecureRandom.uuid

    # Simulates the race: another request with the same idempotency key wins
    # and commits its INSERT in the window between our pre-check (which
    # therefore still sees nothing, hence the first `nil`) and our own
    # #save (which then hits the real partial unique index on
    # entries(account_id, idempotency_key) and raises RecordNotUnique,
    # exactly like the DB would under real concurrent requests). The rescue
    # then re-runs the same lookup, this time finding the winner.
    winning_entry = @entry.account.entries.create!(
      name: "New transaction", date: Date.current, currency: "USD", amount: 100,
      idempotency_key: idempotency_key,
      entryable: Transaction.new
    )
    TransactionsController.any_instance.stubs(:find_duplicate_manual_entry).returns(nil, winning_entry)
    Entry.any_instance.stubs(:save).raises(ActiveRecord::RecordNotUnique.new("duplicate key value violates unique constraint"))

    assert_no_difference [ "Entry.count", "Transaction.count" ] do
      post transactions_url, params: {
        entry: {
          account_id: @entry.account_id,
          name: "New transaction",
          date: Date.current,
          currency: "USD",
          amount: 100,
          nature: "inflow",
          entryable_type: "Transaction",
          idempotency_key: idempotency_key
        }
      }
    end

    assert_response :redirect
    assert_redirected_to account_url(winning_entry.account)
    assert_equal "Transaction created", flash[:notice]
  end

  test "a RecordNotUnique with no matching entry is not silently swallowed" do
    idempotency_key = SecureRandom.uuid

    # Defensive-branch coverage: if the unique index ever rejects an insert
    # for a reason other than "another request with this exact idempotency
    # key already won" (e.g. a different constraint), we must not pretend it
    # succeeded - the error should propagate instead of being hidden behind
    # a fake success redirect.
    TransactionsController.any_instance.stubs(:find_duplicate_manual_entry).returns(nil)
    Entry.any_instance.stubs(:save).raises(ActiveRecord::RecordNotUnique.new("duplicate key value violates unique constraint"))

    assert_raises(ActiveRecord::RecordNotUnique) do
      post transactions_url, params: {
        entry: {
          account_id: @entry.account_id,
          name: "New transaction",
          date: Date.current,
          currency: "USD",
          amount: 100,
          nature: "inflow",
          entryable_type: "Transaction",
          idempotency_key: idempotency_key
        }
      }
    end
  end

  test "create without an idempotency key still creates a transaction as before" do
    # A raw POST that doesn't go through the rendered form (e.g. a script)
    # simply skips the idempotency check rather than being rejected - the
    # form always supplies a key in normal browser usage.
    assert_difference [ "Entry.count", "Transaction.count" ], 2 do
      2.times do
        post transactions_url, params: {
          entry: {
            account_id: @entry.account_id,
            name: "New transaction",
            date: Date.current,
            currency: "USD",
            amount: 100,
            nature: "inflow",
            entryable_type: "Transaction",
            entryable_attributes: { category_id: Category.first.id }
          }
        }
      end
    end
  end

  test "create without an account re-renders the form instead of raising" do
    assert_no_difference [ "Entry.count", "Transaction.count" ] do
      post transactions_url, params: {
        entry: {
          account_id: "",
          name: "New transaction",
          date: Date.current,
          currency: "USD",
          amount: 100,
          nature: "inflow",
          entryable_type: "Transaction",
          entryable_attributes: {
            category_id: Category.first.id
          }
        }
      }
    end

    assert_response :unprocessable_entity
  end

  test "a blank auto-generated name is not silently kept when another field fails validation" do
    @entry.account.family.update!(auto_generate_transaction_names: true)

    # Name is blank but a category is selected, so Entry#set_default_name
    # fills it in during validation; the date is deliberately out of the
    # supported range so the record still fails to save for an unrelated
    # reason (an invalid currency would break rendering the money field
    # itself, not just validation). The re-rendered form must show the name
    # field blank again (not the generated text), or a resubmission after
    # changing the category would save that now-stale name instead of a
    # fresh one.
    post transactions_url, params: {
      entry: {
        account_id: @entry.account_id,
        name: "",
        date: 40.years.ago.to_date,
        currency: "USD",
        amount: 100,
        nature: "inflow",
        entryable_type: "Transaction",
        entryable_attributes: { category_id: categories(:food_and_drink).id }
      }
    }

    assert_response :unprocessable_entity
    assert_select "input[name='entry[name]'][value='']"
  end

  test "an explicitly provided name survives a failed submission unchanged" do
    @entry.account.family.update!(auto_generate_transaction_names: true)

    post transactions_url, params: {
      entry: {
        account_id: @entry.account_id,
        name: "Explicit name",
        date: 40.years.ago.to_date,
        currency: "USD",
        amount: 100,
        nature: "inflow",
        entryable_type: "Transaction",
        entryable_attributes: { category_id: categories(:food_and_drink).id }
      }
    }

    assert_response :unprocessable_entity
    assert_select "input[name='entry[name]'][value='Explicit name']"
  end

  test "recent name suggestions never include entries from accounts inaccessible to the current user" do
    @entry.account.family.update!(auto_generate_transaction_names: true)

    private_account = Account.create!(
      family: @entry.account.family,
      owner: users(:family_member),
      name: "Family Member Private Account",
      balance: 100,
      currency: "USD",
      accountable: Depository.new,
      status: "active"
    )
    create_transaction(account: private_account, name: "Private Merchant XYZ")

    get name_suggestions_transactions_url, as: :turbo_stream

    assert_response :success
    assert_no_match "Private Merchant XYZ", response.body
  end

  test "recent name suggestions exclude the unknown-name fallback in every supported locale, not just the current one" do
    @entry.account.family.update!(auto_generate_transaction_names: true)

    create_transaction(account: @entry.account, name: I18n.t("transactions.unknown_name", locale: :en))
    create_transaction(account: @entry.account, name: I18n.t("transactions.unknown_name", locale: :de))

    get name_suggestions_transactions_url, as: :turbo_stream

    assert_response :success
    assert_no_match I18n.t("transactions.unknown_name", locale: :de), response.body
  end

  # Regression: the sync window must start at the old date, or the incremental
  # balance calculation counts the moved transaction twice.
  test "moving a transaction later syncs from its original date" do
    original_date = 5.days.ago.to_date
    @entry.update!(date: original_date)

    Account.any_instance.expects(:sync_later).with(window_start_date: original_date)

    patch transaction_url(@entry), params: {
      entry: { date: 2.days.ago.to_date, entryable_type: @entry.entryable_type }
    }

    assert_equal 2.days.ago.to_date, @entry.reload.date
  end

  test "editing a transaction that pairs with another redraws its row with the transfer hint" do
    outflow = accounts(:depository).entries.create!(name: "To card", date: 1.day.ago.to_date, amount: 75, currency: "USD", entryable: Transaction.new)
    accounts(:credit_card).entries.create!(name: "From checking", date: 1.day.ago.to_date, amount: -75, currency: "USD", entryable: Transaction.new)

    patch transaction_url(outflow), params: { entry: { notes: "Card payment", entryable_type: "Transaction" } }, as: :turbo_stream

    assert_response :success
    assert_select "turbo-stream[action=replace][target=?]", dom_id(outflow)
    assert_select "##{dom_id(outflow.entryable, :transfer_hint)}"
  end

  test "name_suggestions returns matching past transaction names with their usual category" do
    get name_suggestions_transactions_url(q: "Starbucks"), as: :turbo_stream

    assert_response :success
    assert_match "Starbucks", response.body
    assert_match "data-category-id=\"#{categories(:food_and_drink).id}\"", response.body
  end

  test "name_suggestions starts with recent names and categories when auto naming is enabled" do
    @entry.account.family.update!(auto_generate_transaction_names: true)

    get name_suggestions_transactions_url, as: :turbo_stream

    assert_response :success
    assert_match "Starbucks", response.body
    assert_match "data-category-id=\"#{categories(:food_and_drink).id}\"", response.body

    get name_suggestions_transactions_url(q: "zz"), as: :turbo_stream
    assert_no_match "Starbucks", response.body
  end

  test "name_suggestions does not load recent names when auto naming is disabled" do
    @entry.account.family.update!(auto_generate_transaction_names: false)

    get name_suggestions_transactions_url, as: :turbo_stream

    assert_response :success
    assert_no_match "Starbucks", response.body
  end

  test "name_suggestions returns no options when no past transaction matches" do
    get name_suggestions_transactions_url(q: "no such merchant"), as: :turbo_stream

    assert_response :success
    assert_no_match "no such merchant", response.body
  end

  test "name_suggestions only considers transactions from accounts accessible to the current user" do
    # accounts(:investment) is owned by family_admin and not shared with family_member
    create_transaction(account: accounts(:investment), name: "Private Yacht Purchase", category: categories(:income))

    sign_in users(:family_member)

    get name_suggestions_transactions_url(q: "Private Yacht"), as: :turbo_stream
    assert_response :success
    assert_no_match "Private Yacht", response.body

    # depository is shared with family_member, so its transactions remain suggestible
    get name_suggestions_transactions_url(q: "Starbucks"), as: :turbo_stream
    assert_response :success
    assert_match "Starbucks", response.body
  end

  test "updates with transaction details" do
    assert_no_difference [ "Entry.count", "Transaction.count" ] do
      patch transaction_url(@entry), params: {
        entry: {
          name: "Updated name",
          date: Date.current,
          currency: "USD",
          amount: 100,
          nature: "inflow",
          entryable_type: @entry.entryable_type,
          notes: "test notes",
          excluded: false,
          entryable_attributes: {
            id: @entry.entryable_id,
            tag_ids: [ tags(:one).id, tags(:two).id ],
            category_id: categories(:income).id,
            merchant_id: Merchant.first.id
          }
        }
      }
    end

    @entry.reload

    assert_equal "Updated name", @entry.name
    assert_equal Date.current, @entry.date
    assert_equal "USD", @entry.currency
    assert_equal -100, @entry.amount
    assert_equal [ tags(:one).id, tags(:two).id ].sort, @entry.entryable.tag_ids.sort
    assert_equal categories(:income).id, @entry.entryable.category_id
    assert_equal Merchant.first.id, @entry.entryable.merchant_id
    assert_equal "test notes", @entry.notes
    assert_equal false, @entry.excluded

    assert_equal "Transaction updated", flash[:notice]
    assert_equal "Updated name", flash[:cta][:merchant_name]
    assert_redirected_to account_url(@entry.account)
    assert_enqueued_with(job: SyncJob)
  end

  test "a blank excluded field includes the transaction again" do
    @entry.update!(excluded: true)

    patch transaction_url(@entry), params: { entry: { excluded: "" } }

    assert_redirected_to account_url(@entry.account)
    assert_equal false, Entry.where(id: @entry.id).pick(:excluded)
  end

  test "re-renders show with mark-recurring state when update fails validation" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)

    family.recurring_transactions.create!(
      account: account,
      merchant: merchant,
      amount: entry.amount,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: true,
      occurrence_count: 1
    )

    patch transaction_url(entry), params: {
      entry: {
        name: "",
        date: entry.date,
        currency: entry.currency,
        amount: entry.amount.abs,
        nature: "outflow",
        entryable_type: entry.entryable_type,
        entryable_attributes: { id: entry.entryable_id }
      }
    }

    assert_response :unprocessable_entity
    assert_includes response.body, I18n.t("recurring_transactions.already_covered")
    assert_select "button[disabled]", text: /Mark as Recurring/
  end

  test "turbo_stream update refreshes mark-recurring state when it newly matches" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, name: "Other Name")

    family.recurring_transactions.create!(
      account: account,
      merchant: merchant,
      amount: entry.amount,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: true,
      occurrence_count: 1
    )

    patch transaction_url(entry), params: {
      entry: {
        date: entry.date,
        currency: entry.currency,
        amount: entry.amount.abs,
        nature: "outflow",
        entryable_type: entry.entryable_type,
        entryable_attributes: { id: entry.entryable_id, merchant_id: merchant.id }
      }
    }, as: :turbo_stream

    assert_response :success
    assert_select "turbo-stream[target='#{dom_id(entry, :mark_recurring)}'] button[disabled]", text: /Mark as Recurring/
  end

  test "turbo_stream update replaces the provenance frame after a category change" do
    transaction = @entry.entryable
    transaction.enrich_attribute(:category_id, categories(:income).id, source: "ai")

    patch transaction_url(@entry), params: {
      entry: {
        entryable_type: @entry.entryable_type,
        entryable_attributes: {
          id: @entry.entryable_id,
          category_id: categories(:subcategory).id
        }
      }
    }, as: :turbo_stream

    assert_response :success
    assert_select "turbo-stream[target='#{dom_id(@entry, :category_provenance)}'] [data-testid='category-provenance']"
  end

  test "transaction count represents filtered total" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new

    3.times do
      create_transaction(account: account)
    end

    get transactions_url(per_page: 10)

    assert_dom "#total-transactions", count: 1, text: family.entries.transactions.size.to_s

    searchable_transaction = create_transaction(account: account, name: "Unique test name")

    get transactions_url(q: { search: searchable_transaction.name })

    # Only finds 1 transaction that matches filter
    assert_dom "#" + dom_id(searchable_transaction), count: 1
    assert_dom "#total-transactions", count: 1, text: "1"
    assert_dom "#scheduled-transactions-count", count: 0
  end

  test "transaction count includes scheduled entries shown in the list" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new

    create_transaction(account: account)
    scheduled = create_transaction(account: account, date: 1.day.from_now.to_date, name: "Scheduled bill")

    get transactions_url

    # The scheduled entry is excluded from totals.count (see Transaction::Search#totals)
    # but still rendered in the list below -- the headline must match the list, not the totals.
    assert_dom "#" + dom_id(scheduled), count: 1
    assert_dom "#total-transactions", count: 1, text: "2"
    assert_dom "#scheduled-transactions-count", count: 1, text: "including 1 scheduled"
  end

  test "can update notes on split child transaction" do
    parent = create_transaction(account: accounts(:depository), amount: 100)
    parent.split!([ { name: "Part 1", amount: 60, category_id: nil }, { name: "Part 2", amount: 40, category_id: nil } ])
    child = parent.child_entries.first

    patch transaction_url(child), params: {
      entry: { notes: "split child note", entryable_attributes: { id: child.entryable_id } }
    }

    assert_response :redirect
    assert_equal "split child note", child.reload.notes
  end

  test "can update tags on split child transaction" do
    parent = create_transaction(account: accounts(:depository), amount: 100)
    parent.split!([ { name: "Part 1", amount: 60, category_id: nil }, { name: "Part 2", amount: 40, category_id: nil } ])
    child = parent.child_entries.first
    tag = tags(:one)

    patch transaction_url(child), params: {
      entry: { entryable_attributes: { id: child.entryable_id, tag_ids: [ tag.id ] } }
    }

    assert_response :redirect
    assert_equal [ tag.id ], child.reload.entryable.tag_ids
  end

  test "split child drawer lets the user change category and merchant but not the name" do
    parent = create_transaction(account: accounts(:depository), amount: 100)
    parent.split!([ { name: "Part 1", amount: 60, category_id: nil }, { name: "Part 2", amount: 40, category_id: nil } ])
    child = parent.child_entries.first

    get transaction_url(child), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_select "[data-category-select-disabled-value='false']", count: 1
    assert_select "[data-merchant-select-disabled-value='false']", count: 1
    assert_select "input[name='entry[name]'][disabled]", count: 1
  end

  test "category and merchant changed on a split child are kept and locked" do
    parent = create_transaction(account: accounts(:depository), amount: 100, merchant: merchants(:amazon))
    parent.split!([ { name: "Part 1", amount: 60, category_id: nil }, { name: "Part 2", amount: 40, category_id: nil } ])
    child = parent.child_entries.find_by(name: "Part 1")
    sibling = parent.child_entries.find_by(name: "Part 2")

    patch transaction_url(child), params: {
      entry: { entryable_attributes: { id: child.entryable_id, category_id: categories(:food_and_drink).id, merchant_id: merchants(:netflix).id } }
    }

    assert_response :redirect
    transaction = child.reload.entryable
    assert_equal categories(:food_and_drink), transaction.category
    assert_equal merchants(:netflix), transaction.merchant
    assert transaction.locked?(:category_id)
    assert transaction.locked?(:merchant_id)
    assert_equal merchants(:amazon), parent.reload.entryable.merchant
    assert_equal merchants(:amazon), sibling.reload.entryable.merchant
  end

  test "can update tags through tag-only endpoint" do
    patch tags_transaction_url(@entry, format: :json), params: {
      tag_ids: [ tags(:one).id, tags(:two).id ]
    }

    assert_response :success
    assert_equal [ tags(:one).id, tags(:two).id ].sort, @entry.reload.entryable.tag_ids.sort
    assert_equal @entry.entryable.tag_ids.sort, JSON.parse(response.body)["tag_ids"].sort
  end

  test "tag-only endpoint ignores tags from another family" do
    other_tag = users(:empty).family.tags.create!(name: "Other family")

    patch tags_transaction_url(@entry, format: :json), params: {
      tag_ids: [ tags(:one).id, other_tag.id ]
    }

    assert_response :success
    assert_equal [ tags(:one).id ], @entry.reload.entryable.tag_ids
  end

  test "tag-only endpoint locks tags when clearing all tags" do
    @entry.entryable.update!(tag_ids: [ tags(:one).id ], locked_attributes: {})

    patch tags_transaction_url(@entry, format: :json), params: {
      tag_ids: []
    }, as: :json

    assert_response :success
    assert_empty @entry.reload.entryable.tag_ids
    assert @entry.entryable.locked?(:tag_ids)
  end

  test "tag-only endpoint replaces a bare tag set under the row lock" do
    other_family_tag = users(:empty).family.tags.create!(name: "Other family")
    @entry.entryable.update!(tag_ids: [ tags(:one).id ], locked_attributes: {})

    assert_queries_match(/FROM "transactions" WHERE .* FOR UPDATE/) do
      patch tags_transaction_url(@entry, format: :json), params: {
        tag_ids: [ tags(:two).id, other_family_tag.id ]
      }, as: :json
    end

    assert_response :success
    assert_equal [ tags(:two).id ], @entry.reload.entryable.tag_ids
  end

  test "tag-only endpoint keeps tags added since the drawer last saved" do
    other_family_tag = users(:empty).family.tags.create!(name: "Other family")
    rule_tag = @user.family.tags.create!(name: "Added by a rule")
    @entry.entryable.update!(tag_ids: [ tags(:one).id, rule_tag.id ], locked_attributes: {})

    patch tags_transaction_url(@entry, format: :json), params: {
      tag_ids: [ tags(:one).id, tags(:two).id, other_family_tag.id ],
      previous_tag_ids: [ tags(:one).id ]
    }, as: :json

    assert_response :success
    assert_equal [ tags(:one).id, tags(:two).id, rule_tag.id ].sort, @entry.reload.entryable.tag_ids.sort

    patch tags_transaction_url(@entry, format: :json), params: {
      tag_ids: [], previous_tag_ids: [ tags(:one).id, tags(:two).id ]
    }, as: :json

    assert_equal [ rule_tag.id ], @entry.reload.entryable.tag_ids
    assert @entry.entryable.locked?(:tag_ids)
  end

  test "tag-only endpoint returns forbidden json for read-only users" do
    sign_in users(:family_member)
    read_only_entry = entries(:transfer_in)
    original_tag_ids = read_only_entry.entryable.tag_ids

    patch tags_transaction_url(read_only_entry), params: {
      tag_ids: [ tags(:one).id ]
    }, headers: {
      "Accept" => "application/json"
    }

    assert_response :forbidden
    assert_equal "application/json", response.media_type
    assert_equal I18n.t("accounts.not_authorized"), JSON.parse(response.body)["error"]
    assert_equal original_tag_ids, read_only_entry.reload.entryable.tag_ids
  end

  test "tag-only endpoint toggles a single tag and streams the row's tag UI" do
    @entry.entryable.update!(tag_ids: [ tags(:one).id ], locked_attributes: {})

    patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:two).id }, as: :turbo_stream

    assert_response :success
    assert_equal [ tags(:one).id, tags(:two).id ].sort, @entry.reload.entryable.tag_ids.sort
    assert @entry.entryable.locked?(:tag_ids)
    assert_select "turbo-stream[action=replace][target=?]", dom_id(@entry.entryable, "tag_summary_desktop")
    assert_select "turbo-stream[action=replace][target=?]", dom_id(@entry.entryable, "tag_summary_mobile")
    assert_select "turbo-stream[action=replace][target=?]", "#{dom_id(@entry, :tag_option)}_#{tags(:two).id}"

    patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:one).id }, as: :turbo_stream

    assert_response :success
    assert_equal [ tags(:two).id ], @entry.reload.entryable.tag_ids
  end

  test "tag-only endpoint falls back to a redirect for plain HTML toggles" do
    @entry.entryable.update!(tag_ids: [], locked_attributes: {})

    patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:one).id }

    assert_redirected_to transaction_path(@entry)
    assert_equal [ tags(:one).id ], @entry.reload.entryable.tag_ids
  end

  test "tag-only endpoint does not toggle tags from another family" do
    other_tag = users(:empty).family.tags.create!(name: "Other family")
    original_tag_ids = @entry.entryable.tag_ids

    patch tags_transaction_url(@entry), params: { toggle_tag_id: other_tag.id }, as: :turbo_stream

    assert_response :not_found
    assert_equal original_tag_ids, @entry.reload.entryable.tag_ids
  end

  test "tag-only endpoint does not toggle tags for read-only users" do
    sign_in users(:family_member)
    read_only_entry = entries(:transfer_in)
    original_tag_ids = read_only_entry.entryable.tag_ids

    patch tags_transaction_url(read_only_entry), params: { toggle_tag_id: tags(:one).id }, as: :turbo_stream

    assert_equal original_tag_ids, read_only_entry.reload.entryable.tag_ids
  end

  # The category and tag dropdowns are fetched when their popover opens, so
  # rows carry no <turbo-frame> markup for them.
  test "transaction rows defer the category and tag dropdown frames until opened" do
    get transactions_url

    assert_response :success
    assert_select "turbo-frame#category_dropdown", count: 0
    assert_select "turbo-frame#tag_dropdown", count: 0

    transaction = @entry.entryable
    %w[desktop mobile].each do |variant|
      assert_select "##{dom_id(transaction, "category_menu_#{variant}")} [data-ds--popover-frame-id-value='category_dropdown'][data-ds--popover-frame-src-value=?]",
        category_dropdown_path(category_id: transaction.category_id, transaction_id: transaction.id, grouped: false)
    end
    assert_select "[data-ds--popover-frame-id-value='tag_dropdown'][data-ds--popover-frame-src-value=?]",
      tag_dropdown_path(entry_id: @entry.id), count: 2
  end

  test "the mobile category cell renders only the icon badge, the desktop cell both" do
    get transactions_url

    assert_response :success
    transaction = @entry.entryable
    assert_select "##{dom_id(transaction, "category_menu_mobile")} [data-testid='category-name']", count: 0
    assert_select "##{dom_id(transaction, "category_menu_desktop")} [data-testid='category-name']", count: 1
  end

  test "transaction rows show tags" do
    @entry.entryable.update!(tag_ids: [ tags(:one).id ])

    get transactions_url

    assert_response :success
    assert_select "##{dom_id(@entry.entryable, "tag_summary_desktop")}", text: /#{tags(:one).name}/
    assert_select "##{dom_id(@entry.entryable, "tag_summary_mobile")}", text: /#{tags(:one).name}/
  end

  test "split parent rows mark amount as privacy-sensitive" do
    entry = create_transaction(account: accounts(:depository), amount: 100, name: "Split parent")

    entry.split!([
      { name: "Part 1", amount: 60, category_id: nil },
      { name: "Part 2", amount: 40, category_id: nil }
    ])

    get transactions_url

    assert_response :success
    assert_select ".split-group > div.opacity-50 p.privacy-sensitive", count: 1
  end

  # Row only opened on a precise click on the name text (whitespace between
  # name/avatar/amount looked clickable via the row's hover styling but did
  # nothing). A row-level click delegates to the name link now, so the whole
  # row opens the drawer while interactive descendants (checkbox, category
  # menu, account link) keep handling their own clicks.
  test "transaction row delegates whole-row clicks to the drawer link" do
    get transactions_url

    assert_response :success
    doc = Nokogiri::HTML::Document.parse(response.body)
    frame_id = ActionView::RecordIdentifier.dom_id(@entry.entryable)
    row = doc.at_css("turbo-frame##{frame_id} [data-controller='clickable-row']")
    drawer_link = row.at_css("a[data-clickable-row-target='link']")

    assert_equal "click->clickable-row#open", row["data-action"]
    assert_equal entry_path(@entry), drawer_link["href"]
  end

  test "split parent row delegates whole-row clicks to the drawer link" do
    entry = create_transaction(account: accounts(:depository), amount: 100, name: "Split parent")

    entry.split!([
      { name: "Part 1", amount: 60, category_id: nil },
      { name: "Part 2", amount: 40, category_id: nil }
    ])

    get transactions_url

    assert_response :success
    doc = Nokogiri::HTML::Document.parse(response.body)
    row = doc.at_css(".split-group [data-controller='clickable-row']")
    drawer_link = row.at_css("a[data-clickable-row-target='link']")

    assert_equal "click->clickable-row#open", row["data-action"]
    assert_equal entry_path(entry), drawer_link["href"]
  end

  test "can paginate" do
  family = families(:empty)
  sign_in users(:empty)

  # Clean up any existing entries to ensure clean test
  family.accounts.each { |account| account.entries.delete_all }

  account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new

  # Create multiple transactions for pagination
  25.times do |i|
    create_transaction(
      account: account,
      name: "Transaction #{i + 1}",
      amount: 100 + i,  # Different amounts to prevent transfer matching
      date: Date.current - i.days  # Different dates
    )
  end

  total_transactions = family.entries.transactions.count
  assert_operator total_transactions, :>=, 20, "Should have at least 20 transactions for testing"

  # Test page 1 - should show limited transactions
  get transactions_url(page: 1, per_page: 10)
  assert_response :success

  page_1_count = css_select("turbo-frame[id^='entry_']").count
  assert_equal 10, page_1_count, "Page 1 should respect per_page limit"

  # Test page 2 - should show different transactions
  get transactions_url(page: 2, per_page: 10)
  assert_response :success

  page_2_count = css_select("turbo-frame[id^='entry_']").count
  assert_operator page_2_count, :>, 0, "Page 2 should show some transactions"
  assert_operator page_2_count, :<=, 10, "Page 2 should not exceed per_page limit"

  # Test Pagy overflow handling - should redirect or handle gracefully
  get transactions_url(page: 9999999, per_page: 10)

  # Either success (if Pagy shows last page) or redirect (if Pagy redirects)
  assert_includes [ 200, 302 ], response.status, "Pagy should handle overflow gracefully"

  if response.status == 302
    follow_redirect!
    assert_response :success
  end

  overflow_count = css_select("turbo-frame[id^='entry_']").count
  assert_operator overflow_count, :>, 0, "Overflow should show some transactions"
end

  test "filtered requests without per_page keep the stored page size" do
    family = families(:empty)
    sign_in users(:empty)

    family.accounts.each { |account| account.entries.delete_all }

    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new

    25.times do |i|
      create_transaction(
        account: account,
        name: "Transaction #{i + 1}",
        amount: 100 + i,
        date: Date.current - i.days
      )
    end

    get transactions_url(per_page: 20)
    assert_response :success

    # A filtered request that omits per_page has query params present, so it
    # is not eligible for the "restore stored params" redirect - it must fall
    # back to the previously stored per_page instead of the hardcoded default.
    get transactions_url(q: { search: "Transaction" })
    assert_response :success
    assert_select "select[name='per_page'] option[value='20'][selected]"
    assert_equal 20, css_select("turbo-frame[id^='entry_']").count
  end

  test "pagination does not duplicate or skip transactions with same date and timestamp" do
    family = families(:empty)
    user = users(:empty)
    sign_in user

    family.accounts.each { |account| account.entries.delete_all }

    account = family.accounts.create! name: "Same day", balance: 0, currency: "USD", accountable: Depository.new
    timestamp = Time.zone.parse("2026-05-05 12:00:00")

    entries = 13.times.map do |index|
      create_transaction(
        account: account,
        name: "May 05 Transaction #{index + 1}",
        amount: 100 + index,
        date: Date.new(2026, 5, 5),
        created_at: timestamp,
        updated_at: timestamp
      )
    end

    expected_entry_ids = Entry.where(id: entries.map(&:id)).reverse_chronological.pluck(:id).map(&:to_s)

    get transactions_url(page: 1, per_page: 10)
    assert_response :success
    page_1_entry_ids = rendered_entry_ids

    get transactions_url(page: 2, per_page: 10)
    assert_response :success
    page_2_entry_ids = rendered_entry_ids

    assert_equal expected_entry_ids.first(10), page_1_entry_ids
    assert_equal expected_entry_ids.drop(10), page_2_entry_ids
    assert_empty page_1_entry_ids & page_2_entry_ids
    assert_equal expected_entry_ids, page_1_entry_ids + page_2_entry_ids
  end

  test "calls Transaction::Search totals method with correct search parameters" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new

    create_transaction(account: account, amount: 100)

    search = Transaction::Search.new(family)
    totals = OpenStruct.new(
      count: 1,
      excluded_scheduled_count: 0,
      expense_money: Money.new(10000, "USD"),
      income_money: Money.new(0, "USD"),
      transfer_inflow_money: Money.new(0, "USD"),
      transfer_outflow_money: Money.new(0, "USD")
    )

    Transaction::Search.expects(:new).with(family, filters: {}, accessible_account_ids: [ account.id ]).returns(search)
    search.expects(:totals).once.returns(totals)

    get transactions_url
    assert_response :success
  end

  test "calls Transaction::Search totals method with filtered search parameters" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    category = family.categories.create! name: "Food", color: "#ff0000"

    create_transaction(account: account, amount: 100, category: category)

    search = Transaction::Search.new(family, filters: { "categories" => [ "Food" ], "types" => [ "expense" ] })
    totals = OpenStruct.new(
      count: 1,
      excluded_scheduled_count: 0,
      expense_money: Money.new(10000, "USD"),
      income_money: Money.new(0, "USD"),
      transfer_inflow_money: Money.new(0, "USD"),
      transfer_outflow_money: Money.new(0, "USD")
    )

    Transaction::Search.expects(:new).with(family, filters: { "categories" => [ "Food" ], "types" => [ "expense" ] }, accessible_account_ids: [ account.id ]).returns(search)
    search.expects(:totals).once.returns(totals)

    get transactions_url(q: { categories: [ "Food" ], types: [ "expense" ] })
    assert_response :success
  end

  test "shows inflow/outflow labels when filtering by transfers only" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new

    create_transaction(account: account, amount: 100)

    search = Transaction::Search.new(family, filters: { "types" => [ "transfer" ] })
    totals = OpenStruct.new(
      count: 2,
      excluded_scheduled_count: 0,
      expense_money: Money.new(0, "USD"),
      income_money: Money.new(0, "USD"),
      transfer_inflow_money: Money.new(5000, "USD"),
      transfer_outflow_money: Money.new(3000, "USD")
    )

    Transaction::Search.expects(:new).with(family, filters: { "types" => [ "transfer" ] }, accessible_account_ids: [ account.id ]).returns(search)
    search.expects(:totals).once.returns(totals)

    get transactions_url(q: { types: [ "transfer" ] })
    assert_response :success
    assert_select "#total-income", text: totals.transfer_inflow_money.format
    assert_select "#total-expense", text: totals.transfer_outflow_money.format
  end

  test "mark_as_recurring creates a manual recurring transaction" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)
    transaction = entry.entryable

    assert_difference "family.recurring_transactions.count", 1 do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal "Transaction marked as recurring", flash[:notice]

    recurring = family.recurring_transactions.last
    assert_equal true, recurring.manual, "Expected recurring transaction to be manual"
    assert_equal merchant.id, recurring.merchant_id
    assert_equal entry.currency, recurring.currency
    assert_equal entry.date.day, recurring.expected_day_of_month
  end

  test "mark_as_recurring shows alert if recurring transaction already exists" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)
    transaction = entry.entryable

    # Create existing recurring transaction
    family.recurring_transactions.create!(
      account: account,
      merchant: merchant,
      amount: entry.amount,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: true,
      occurrence_count: 1
    )

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal I18n.t("recurring_transactions.already_covered"), flash[:alert]
  end

  test "mark_as_recurring allows a second manual recurring transaction with same merchant but different amount" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 34, merchant: merchant)
    transaction = entry.entryable

    # Existing manual recurring row for the same merchant, but a different amount
    family.recurring_transactions.create!(
      account: account,
      merchant: merchant,
      amount: 12,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: true,
      occurrence_count: 1
    )

    assert_difference "family.recurring_transactions.count", 1 do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal "Transaction marked as recurring", flash[:notice]
  end

  test "mark_as_recurring allows a second manual recurring transaction with same name but different amount" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    entry = create_transaction(account: account, name: "Example Payee", amount: 34)
    transaction = entry.entryable

    # Existing manual recurring row for the same payee name, but a different amount
    family.recurring_transactions.create!(
      account: account,
      name: "Example Payee",
      amount: 12,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: true,
      occurrence_count: 1
    )

    assert_difference "family.recurring_transactions.count", 1 do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal "Transaction marked as recurring", flash[:notice]
  end

  test "mark_as_recurring shows alert if recurring transaction with same name and amount already exists" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    entry = create_transaction(account: account, name: "Example Payee", amount: 100)
    transaction = entry.entryable

    family.recurring_transactions.create!(
      account: account,
      name: "Example Payee",
      amount: entry.amount,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: true,
      occurrence_count: 1
    )

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal I18n.t("recurring_transactions.already_covered"), flash[:alert]
  end

  # Only hand-made rows used to count, so a bill that detection had already
  # found never disabled the button (#4368).
  test "mark_as_recurring refuses a transaction a detected bill already covers" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Betashares"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)

    family.recurring_transactions.create!(
      account: account,
      merchant: merchant,
      amount: 100,
      currency: entry.currency,
      expected_day_of_month: entry.date.day,
      last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now,
      status: "active",
      manual: false,
      occurrence_count: 2
    )

    get transaction_url(entry)
    assert_select "button[disabled][title=?]", I18n.t("recurring_transactions.already_covered"), text: /Mark as Recurring/
    assert_select "##{ActionView::RecordIdentifier.dom_id(entry, :mark_recurring)} p",
                  text: I18n.t("recurring_transactions.already_covered")

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(entry.entryable)
    end

    assert_redirected_to transactions_path
    assert_equal I18n.t("recurring_transactions.already_covered"), flash[:alert]
  end

  # The covering series may be income, or only a suggestion, so the message
  # must not tell the user a bill is tracking the payment.
  test "mark_as_recurring does not call a suggested income a bill" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Acme Payroll"
    entry = create_transaction(account: account, amount: -1840, merchant: merchant)
    family.recurring_transactions.create!(
      account: account, merchant: merchant, amount: -1840, currency: "USD", bill_type: "income",
      expected_day_of_month: entry.date.day, last_occurrence_date: entry.date,
      next_expected_date: 1.month.from_now, status: "suggested", manual: false, occurrence_count: 2
    )

    get transaction_url(entry)
    disabled = css_select("button[disabled]").find { |button| button.text.include?("Mark as Recurring") }
    assert disabled, "the covering income must disable the button"
    assert_no_match(/\bbill\b/i, disabled["title"])

    post mark_as_recurring_transaction_path(entry.entryable)
    assert_equal I18n.t("recurring_transactions.already_covered"), flash[:alert]
    assert_no_match(/\bbill\b/i, flash[:alert])
  end

  # An ended series no longer covers the payment, but the unique index still
  # holds its key, so the create would fail. The button says so up front
  # instead of failing after the POST.
  test "mark_as_recurring is disabled when an ended series holds the same key" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Betashares"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)
    RecurringTransaction.create_from_transaction(entry.entryable).update!(status: "ended")

    get transaction_url(entry)
    assert_select "button[disabled][title=?]", I18n.t("recurring_transactions.already_exists"), text: /Mark as Recurring/

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(entry.entryable)
    end
    assert_equal I18n.t("recurring_transactions.already_exists"), flash[:alert]
  end

  # Sharing is per account. A bill on an account this member was never given
  # is not theirs to learn about, as in the add-bill dialog's own check, so
  # it neither disables the button nor refuses the request.
  test "mark_as_recurring ignores a covering bill on an account the user cannot see" do
    member = users(:family_member)
    entry = create_transaction(account: accounts(:depository), amount: 100, name: "PRIVATE PLAN")
    hidden = families(:dylan_family).recurring_transactions.create!(
      account: accounts(:investment), name: "Private Plan", amount: 100, currency: "USD",
      expected_day_of_month: Date.current.day, last_occurrence_date: 1.month.ago.to_date,
      next_expected_date: Date.current, status: "active", manual: true, occurrence_count: 1
    )
    hidden.recurring_occurrences.order(:due_on).first.allocations.create!(
      entry: entry, allocated_amount: 100, currency: "USD", source: "user_created"
    )
    assert_equal hidden, families(:dylan_family).recurring_transactions.covering(entry), "precondition: the hidden bill covers the entry"

    sign_in member
    get transaction_url(entry)
    assert_select "button[disabled]", text: /Mark as Recurring/, count: 0

    assert_difference "RecurringTransaction.count", 1 do
      post mark_as_recurring_transaction_path(entry.entryable)
    end
    assert_equal I18n.t("recurring_transactions.marked_as_recurring"), flash[:notice]
  end

  # The add-bill form checks and saves under the entry lock the Allocator
  # takes. This path makes a series for the same transaction, so it holds the
  # same lock, or the two could both see nothing and both create.
  test "mark_as_recurring checks and creates while holding the entry lock" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    entry = create_transaction(account: account, amount: 100, name: "FRESH CHARGE")
    connection = ActiveRecord::Base.connection
    outside = connection.open_transactions
    seen = {}

    TransactionsController.any_instance.stubs(:mark_recurring_blocked_message)
      .with { seen[:check] = [ connection.open_transactions, entry_lock_held?(entry) ]; true }.returns(nil)
    RecurringTransaction.stubs(:create_from_transaction)
      .with { seen[:create] = [ connection.open_transactions, entry_lock_held?(entry) ]; true }.returns(true)
    assert_not entry_lock_held?(entry), "precondition: nothing holds the entry lock before the request"

    post mark_as_recurring_transaction_path(entry.entryable)

    assert_equal I18n.t("recurring_transactions.marked_as_recurring"), flash[:notice]
    assert_operator seen[:check].first, :>, outside, "the check ran outside a transaction"
    assert_operator seen[:create].first, :>, outside, "the create ran outside a transaction"
    assert seen[:check].last, "the check ran without the entry lock"
    assert seen[:create].last, "the create ran without the entry lock"
  end

  test "mark_as_recurring shows already-exists alert when a concurrent request wins the race" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)
    transaction = entry.entryable

    # Simulate another request creating the identical pattern between our
    # pre-check and our create call.
    RecurringTransaction.expects(:create_from_transaction).raises(
      ActiveRecord::RecordNotUnique.new("duplicate key value violates unique constraint")
    )

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal "A manual recurring transaction already exists for this pattern", flash[:alert]
  end

  test "mark_as_recurring handles validation errors gracefully" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)
    transaction = entry.entryable

    # Stub create_from_transaction to raise a validation error
    RecurringTransaction.expects(:create_from_transaction).raises(
      ActiveRecord::RecordInvalid.new(
        RecurringTransaction.new.tap { |rt| rt.errors.add(:base, "Test validation error") }
      )
    )

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal "Failed to create recurring transaction. Please check the transaction details and try again.", flash[:alert]
  end

  test "mark_as_recurring handles unexpected errors gracefully" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    merchant = family.merchants.create! name: "Test Merchant"
    entry = create_transaction(account: account, amount: 100, merchant: merchant)
    transaction = entry.entryable

    # Stub create_from_transaction to raise an unexpected error
    RecurringTransaction.expects(:create_from_transaction).raises(StandardError.new("Unexpected error"))

    assert_no_difference "RecurringTransaction.count" do
      post mark_as_recurring_transaction_path(transaction)
    end

    assert_redirected_to transactions_path
    assert_equal "An unexpected error occurred while creating the recurring transaction", flash[:alert]
  end

  test "unlock clears protection flags on user-modified entry" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    entry = create_transaction(account: account, amount: 100)
    transaction = entry.entryable

    # Mark as protected with locked_attributes on both entry and entryable
    entry.update!(user_modified: true, locked_attributes: { "date" => Time.current.iso8601 })
    transaction.update!(locked_attributes: { "category_id" => Time.current.iso8601 })

    assert entry.reload.protected_from_sync?

    post unlock_transaction_path(transaction)

    assert_redirected_to transactions_path
    assert_equal "Entry unlocked. It may be updated on next sync.", flash[:notice]

    entry.reload
    assert_not entry.user_modified?
    assert_empty entry.locked_attributes, "Entry locked_attributes should be cleared"
    assert_empty entry.entryable.locked_attributes, "Transaction locked_attributes should be cleared"
    assert_not entry.protected_from_sync?
  end

  test "new renders category and merchant selectors in German" do
    get new_transaction_url(locale: "de")

    assert_response :success

    assert_select "[data-controller='category-select']" do
      assert_select "input[type='search'][placeholder=?]", "Kategorien suchen"
      assert_select "[data-category-select-create-label-value=?]", "„__CATEGORY_NAME__“ erstellen"
      assert_select "[data-category-select-create-error-message-value=?]", "Kategorie konnte nicht erstellt werden"
    end

    assert_select "[data-controller='merchant-select']" do
      assert_select "input[type='search'][placeholder=?]", "Händler suchen oder erstellen"
      assert_select "[data-merchant-select-error-message-value=?]", "Händler konnte nicht erstellt werden"
      assert_select "[data-merchant-select-target='createForm']", text: /Erstellen/
    end
  end

  test "new groups subcategories immediately after their parent in the category select" do
    get new_transaction_url
    assert_response :success

    doc = Nokogiri::HTML::Document.parse(response.body)
    trigger = doc.at_css("#category_id_trigger")
    assert_not_nil trigger, "expected the category select trigger button to render"

    wrapper = trigger.ancestors(".relative").first
    category_values = wrapper.css("[data-value]").map { |node| node["data-value"] }

    parent_index = category_values.index(categories(:food_and_drink).id)
    child_index = category_values.index(categories(:subcategory).id)

    assert_not_nil parent_index
    assert_not_nil child_index
    assert_equal parent_index + 1, child_index

    child_option = wrapper.at_css("[data-category-id='#{categories(:subcategory).id}']")
    assert_not_nil child_option, "expected the subcategory option to render"
    assert child_option.at_css("[data-testid='category-select-subcategory-indicator']"),
           "expected the subcategory option to show the hierarchy indicator used in Settings"
  end

  test "new renders a search box for account selection" do
    get new_transaction_url
    assert_response :success

    doc = Nokogiri::HTML::Document.parse(response.body)
    trigger = doc.at_css("#account_id_trigger")
    assert_not_nil trigger, "expected the account select trigger button to render"

    wrapper = trigger.ancestors(".relative").first
    assert_not_nil wrapper.at_css("input[type='search']"), "expected a search input inside the account select"
  end

  test "new with duplicate_entry_id pre-fills form from source transaction" do
    @entry.reload

    get new_transaction_url(duplicate_entry_id: @entry.id)
    assert_response :success
    assert_select "input[name='entry[name]'][value=?]", @entry.name
    assert_select "input[type='text'][name='entry[amount]']" do |elements|
      assert_equal sprintf("%.2f", @entry.amount.abs), elements.first["value"]
    end
    assert_select "input[type='hidden'][name='entry[entryable_attributes][merchant_id]']"
  end

  test "new with invalid duplicate_entry_id renders empty form" do
    get new_transaction_url(duplicate_entry_id: -1)
    assert_response :success
    assert_select "input[name='entry[name]']" do |elements|
      assert_nil elements.first["value"]
    end
  end

  test "new with duplicate_entry_id from another family does not prefill form" do
    other_family = families(:empty)
    other_account = other_family.accounts.create!(name: "Other", balance: 0, currency: "USD", accountable: Depository.new)
    other_entry = create_transaction(account: other_account, name: "Should not leak", amount: 50)

    get new_transaction_url(duplicate_entry_id: other_entry.id)
    assert_response :success
    assert_select "input[name='entry[name]']" do |elements|
      assert_nil elements.first["value"]
    end
  end

  test "new preloads transaction form option data" do
    family = families(:empty)
    user = users(:empty)
    sign_in user

    manual_account_ids = []
    4.times do |idx|
      account = family.accounts.create!(
        name: "Manual Account #{idx}",
        balance: 0,
        currency: "USD",
        accountable: Depository.new
      )
      assert Account.manual.active.exists?(id: account.id), "Account should be included in the manual active scope"
      manual_account_ids << account.id
      family.categories.create!(
        name: "Category #{idx}",
        color: "#000000",
        lucide_icon: "shapes"
      )
      family.merchants.create!(name: "Merchant #{idx}")
      family.tags.create!(name: "Tag #{idx}")
    end

    inaccessible_account = families(:dylan_family).accounts.create!(
      name: "Other Family Account",
      balance: 0,
      currency: "EUR",
      accountable: Depository.new
    )

    queries = capture_sql_queries { get new_transaction_url }

    assert_response :success
    assert_select "input[name='entry[account_id]']"
    assert_select "input[name='entry[entryable_attributes][category_id]']"
    assert_select "input[name='entry[entryable_attributes][merchant_id]']"
    assert_select "form[data-transaction-form-account-currencies-value]" do |forms|
      account_currencies = JSON.parse(forms.first["data-transaction-form-account-currencies-value"])
      manual_account_ids.each do |account_id|
        assert_equal "USD", account_currencies[account_id.to_s]
      end
      assert_nil account_currencies[inaccessible_account.id.to_s]
    end

    assert_empty queries.grep(/FROM "account_providers" WHERE "account_providers"\."account_id" =/)
    assert_operator queries.grep(/FROM "active_storage_attachments" WHERE "active_storage_attachments"\."record_id" =/).size, :<=, 1
    assert_operator queries.grep(/SELECT "categories"\.\* FROM "categories" WHERE "categories"\."family_id" =/).size, :<=, 1
  end

  test "unlock clears import_locked flag" do
    family = families(:empty)
    sign_in users(:empty)
    account = family.accounts.create! name: "Test", balance: 0, currency: "USD", accountable: Depository.new
    entry = create_transaction(account: account, amount: 100)
    transaction = entry.entryable

    # Mark as import locked
    entry.update!(import_locked: true)

    assert entry.reload.protected_from_sync?

    post unlock_transaction_path(transaction)

    assert_redirected_to transactions_path
    entry.reload
    assert_not entry.import_locked?
    assert_not entry.protected_from_sync?
  end

  test "exchange_rate endpoint returns rate for different currencies" do
    ExchangeRate.expects(:find_or_fetch_rate)
                .with(from: "EUR", to: "USD", date: Date.current)
                .returns(1.2)

    get exchange_rate_url, params: {
      from: "EUR",
      to: "USD",
      date: Date.current
    }

    assert_response :success
    json_response = JSON.parse(response.body)
    assert_equal 1.2, json_response["rate"]
  end

  test "exchange_rate endpoint returns same_currency for matching currencies" do
    get exchange_rate_url, params: {
      from: "USD",
      to: "USD"
    }

    assert_response :success
    json_response = JSON.parse(response.body)
    assert json_response["same_currency"]
    assert_equal 1.0, json_response["rate"]
  end

  test "exchange_rate endpoint uses provided date" do
    custom_date = 3.days.ago.to_date
    ExchangeRate.expects(:find_or_fetch_rate)
                .with(from: "EUR", to: "USD", date: custom_date)
                .returns(1.25)

    get exchange_rate_url, params: {
      from: "EUR",
      to: "USD",
      date: custom_date
    }

    assert_response :success
    json_response = JSON.parse(response.body)
    assert_equal 1.25, json_response["rate"]
  end

  test "exchange_rate endpoint returns 400 when from currency is missing" do
    get exchange_rate_url, params: {
      to: "USD"
    }

    assert_response :bad_request
    json_response = JSON.parse(response.body)
    assert_equal "from and to currencies are required", json_response["error"]
  end

  test "exchange_rate endpoint returns 400 when to currency is missing" do
    get exchange_rate_url, params: {
      from: "EUR"
    }

    assert_response :bad_request
    json_response = JSON.parse(response.body)
    assert_equal "from and to currencies are required", json_response["error"]
  end

  test "exchange_rate endpoint returns 400 on invalid date format" do
    get exchange_rate_url, params: {
      from: "EUR",
      to: "USD",
      date: "not-a-date"
    }

    assert_response :bad_request
    json_response = JSON.parse(response.body)
    assert_equal "Invalid date format", json_response["error"]
  end

  test "exchange_rate endpoint returns 404 when rate not found" do
    ExchangeRate.expects(:find_or_fetch_rate)
                .with(from: "EUR", to: "USD", date: Date.current)
                .returns(nil)

    get exchange_rate_url, params: {
      from: "EUR",
      to: "USD"
    }

    assert_response :not_found
    json_response = JSON.parse(response.body)
    assert_equal "Exchange rate not found", json_response["error"]
  end

  test "creates transaction with custom exchange rate" do
    account = @user.family.accounts.create!(
      name: "USD Account",
      currency: "USD",
      balance: 1000,
      accountable: Depository.new
    )

    assert_difference [ "Entry.count", "Transaction.count" ], 1 do
      post transactions_url, params: {
        entry: {
          account_id: account.id,
          name: "EUR transaction with custom rate",
          date: Date.current,
          currency: "EUR",
          amount: 100,
          nature: "outflow",
          entryable_type: "Transaction",
          entryable_attributes: {
            category_id: Category.first.id,
            exchange_rate: "1.5"
          }
        }
      }
    end

    created_entry = Entry.order(:created_at).last
    assert_equal "EUR", created_entry.currency
    assert_equal 100, created_entry.amount
    assert_equal 1.5, created_entry.transaction.extra["exchange_rate"]
  end

  test "creates transaction without custom exchange rate" do
    account = @user.family.accounts.create!(
      name: "USD Account",
      currency: "USD",
      balance: 1000,
      accountable: Depository.new
    )

    assert_difference [ "Entry.count", "Transaction.count" ], 1 do
      post transactions_url, params: {
        entry: {
          account_id: account.id,
          name: "EUR transaction without custom rate",
          date: Date.current,
          currency: "EUR",
          amount: 100,
          nature: "outflow",
          entryable_type: "Transaction",
          entryable_attributes: {
            category_id: Category.first.id
          }
        }
      }
    end

    created_entry = Entry.order(:created_at).last
    assert_nil created_entry.transaction.extra["exchange_rate"]
  end

  test "index preloads transfer counterparty entry and account to avoid N+1" do
    family = @user.family
    from_account = family.accounts.visible.first
    to_account = family.accounts.create!(
      name: "Transfer Counterparty",
      currency: family.currency,
      balance: 0,
      accountable: Depository.new
    )

    transfers = 6.times.map do |i|
      create_transfer(
        from_account: from_account,
        to_account: to_account,
        amount: 25 + i,
        date: Date.current - i.days
      )
    end

    queries = capture_sql_queries do
      # per_page must fit all transfer legs + fixtures so assertions below
      # actually exercise the transfer render path this preload protects.
      get transactions_url(per_page: 50)
    end

    assert_response :success

    # Index dedupes transfers to the outflow side; assert those rows rendered so
    # the SQL assertions below actually exercise Transfer#to_account.
    rendered_ids = rendered_entry_ids
    transfers.each do |transfer|
      assert_includes rendered_ids, transfer.outflow_transaction.entry.id.to_s,
                      "Expected transfer outflow entry to render on the index"
    end

    # Transfer#categorizable? / #payment? walk to_account via
    # transfer.inflow_transaction.entry.account. Without nested includes those
    # become one lookup triad per transfer row during list render.
    # categorizable? also walks from_account via the outflow leg, which is only
    # loaded through the inverse_of on Transaction::Transferable; dropping it
    # makes the transactions.id assertion below fail.
    normalized_queries = queries.map { |sql| normalize_sql_query(sql) }
    assert_empty single_record_lookups(normalized_queries, table: "transactions", column: "id"),
                 "Expected transfer counterparty transactions to be preloaded"
    assert_empty single_record_lookups(normalized_queries, table: "entries", column: "entryable_id"),
                 "Expected transfer counterparty entries to be preloaded"
    assert_empty single_record_lookups(normalized_queries, table: "accounts", column: "id"),
                 "Expected transfer counterparty accounts to be preloaded"
  end

  test "reconcile is unavailable when manual reconciliation is disabled" do
    manual_account = accounts(:depository)
    entry = create_transaction(account: manual_account, amount: 50)

    patch reconcile_transaction_path(entry.transaction)

    assert_response :not_found
    assert_equal "unreconciled", entry.reload.reconciled_status
  end

  test "reconcile cycles through unreconciled -> cleared -> reconciled -> unreconciled on a manual account" do
    @user.family.update!(manual_reconciliation: true)
    manual_account = accounts(:depository)
    assert manual_account.manual?, "fixture precondition: depository account must be manual"

    entry = create_transaction(account: manual_account, amount: 50)
    assert_equal "unreconciled", entry.reconciled_status

    patch reconcile_transaction_path(entry.transaction)
    assert_redirected_to transactions_path
    assert_equal "cleared", entry.reload.reconciled_status
    assert_equal "Marked as Cleared", flash[:notice]

    patch reconcile_transaction_path(entry.transaction)
    assert_equal "reconciled", entry.reload.reconciled_status

    patch reconcile_transaction_path(entry.transaction)
    assert_equal "unreconciled", entry.reload.reconciled_status
  end

  test "reconcile records a debug log entry when the status update fails" do
    @user.family.update!(manual_reconciliation: true)
    manual_account = accounts(:depository)
    entry = create_transaction(account: manual_account, amount: 50)
    Entry.any_instance.stubs(:advance_reconciled_status!).raises(ActiveRecord::RecordInvalid.new(entry))

    assert_difference -> { DebugLogEntry.where(category: "reconciliation", level: "error").count }, 1 do
      patch reconcile_transaction_path(entry.transaction)
    end

    assert_redirected_to transactions_path
    assert_equal I18n.t("transactions.reconcile.failure"), flash[:alert]
    assert_equal "unreconciled", entry.reload.reconciled_status

    log_entry = DebugLogEntry.order(:created_at).last
    assert_equal "TransactionsController#reconcile", log_entry.source
    assert_equal @user.family, log_entry.family
    assert_equal @user, log_entry.user
    assert_equal manual_account, log_entry.account
    assert_equal entry.transaction.id.to_s, log_entry.metadata["transaction_id"]
    assert_equal entry.id, log_entry.metadata["entry_id"]
  end

  test "reconcile is rejected for accounts with a live bank sync connection" do
    @user.family.update!(manual_reconciliation: true)
    linked_account = accounts(:connected)
    assert_not linked_account.manual?, "fixture precondition: connected account must not be manual"

    entry = create_transaction(account: linked_account, amount: 50)

    patch reconcile_transaction_path(entry.transaction)

    assert_redirected_to transactions_path
    assert_equal "Reconciliation status only applies to manually-entered accounts (no bank sync)", flash[:alert]
    assert_equal "unreconciled", entry.reload.reconciled_status
  end

  # The badge used to render for any manual account, so a read_only share got a
  # control the action refuses -- the click redirected back with an alert and
  # the status never moved. Reconciling needs write access, not annotate access.
  test "reconcile is rejected on a manual account shared with read-only access" do
    @user.family.update!(manual_reconciliation: true)
    read_only_account = accounts(:credit_card)
    assert read_only_account.manual?, "fixture precondition: credit_card account must be manual"

    entry = create_transaction(account: read_only_account, amount: 75)

    sign_in users(:family_member)
    patch reconcile_transaction_path(entry.transaction)

    assert_redirected_to account_path(read_only_account)
    assert_equal "You don't have permission to manage this account", flash[:alert]
    assert_equal "unreconciled", entry.reload.reconciled_status
  end

  test "reconcile is not accessible for another family's transaction" do
    @user.family.update!(manual_reconciliation: true)
    other_family = families(:empty)
    other_account = other_family.accounts.create!(name: "Other family manual account", balance: 0, currency: "USD", accountable: Depository.new)
    other_entry = create_transaction(account: other_account, amount: 25)

    patch reconcile_transaction_path(other_entry.transaction)

    assert_response :not_found
    assert_equal "unreconciled", other_entry.reload.reconciled_status
  end

  test "index caches uncategorized_count and projected_recurring across requests" do
    # Test environment uses null_store; swap in a memory store so the cache
    # actually persists between the two requests below.
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    get transactions_url
    assert_response :success

    queries = capture_sql_queries { get transactions_url }
    assert_response :success

    # uncategorized_transactions is a scope (joins/wheres), so its name never
    # appears in the generated SQL -- match the COUNT query it produces instead.
    assert_empty queries.select { |q| q =~ /SELECT COUNT/i && q =~ /category_id.*IS NULL/i },
      "second request with unchanged data should reuse the cached uncategorized count"
    # Building the cache key itself still runs small COUNT/MAX queries against
    # recurring_transactions (Family#recurring_transactions_version and
    # #recurring_transaction_merchants_version), so match the projection query
    # the cached block itself would run instead of the whole table name.
    assert_empty queries.grep(/next_expected_date/i),
      "second request with unchanged data should reuse the cached projected recurring lookup"
  ensure
    Rails.cache = original_cache
  end

  test "index renders when the projected_recurring cache holds records from an older schema" do
    # Regression: the cache used to hold whole RecurringTransaction objects. After
    # an upgrade that added columns (e.g. payment_url in 0.7.5), the entry written
    # by the previous version was still served and rendering raised
    # ActiveModel::MissingAttributeError until the key rolled over the next day.
    original_cache = Rails.cache
    written_keys = []
    Rails.cache = Class.new(ActiveSupport::Cache::MemoryStore) {
      define_method(:write_entry) do |key, entry, **options|
        written_keys << key
        super(key, entry, **options)
      end
    }.new

    recurring = recurring_transactions(:netflix_subscription)

    get transactions_url
    assert_response :success

    cache_keys = written_keys.grep(/transactions_projected_recurring/).uniq
    assert_not_empty cache_keys, "the first request should populate the projected-recurring cache"

    # Write the Marshal payload the previous version produced: the record's
    # attributes without the columns that version did not have yet.
    stale_payload = [ recurring.attributes_for_database.except("payment_url", "autopay", "notes"), false, [ [ :merchant, recurring.merchant ] ] ]
    stale_record = RecurringTransaction.allocate
    stale_record.define_singleton_method(:marshal_dump) { stale_payload }
    cache_keys.each { |key| Rails.cache.write(key, [ stale_record ]) }
    assert_raises(ActiveModel::MissingAttributeError) { Rails.cache.read(cache_keys.first).first.payment_url }

    get transactions_url
    assert_response :success
    assert_match(/#{Regexp.escape(recurring.merchant.name)}/, response.body,
      "the projected recurring transaction should still render from fresh records")
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count cache reflects new transactions immediately" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    get transactions_url
    initial_count = rendered_uncategorized_count

    account = @user.family.accounts.visible.first
    account.entries.create!(
      name: "New uncategorized transaction",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    get transactions_url
    updated_count = rendered_uncategorized_count

    assert_equal initial_count + 1, updated_count,
      "a new uncategorized transaction must be reflected without a stale cache read"
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count cache reflects categorizing a transaction immediately" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    entry = @user.family.accounts.visible.first.entries.create!(
      name: "Needs a category",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    get transactions_url
    initial_count = rendered_uncategorized_count

    entry.entryable.update!(category: categories(:food_and_drink))

    get transactions_url
    updated_count = rendered_uncategorized_count

    assert_equal initial_count - 1, updated_count,
      "categorizing a transaction must be reflected without a stale cache read"
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count cache is invalidated when account-share access is revoked" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    member = users(:family_member)
    share = account_shares(:depository_shared_with_member)
    share.account.entries.create!(
      name: "Uncategorized on shared account",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    sign_in member
    get transactions_url
    count_with_access = rendered_uncategorized_count
    assert_operator count_with_access, :>, 0,
      "the member should see the shared account's uncategorized transaction before revocation"

    share.destroy!

    get transactions_url
    count_after_revocation = rendered_uncategorized_count

    assert_operator count_after_revocation, :<, count_with_access,
      "revoking account-share access must not leave a stale cached count that still includes the now-inaccessible account"
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count leaves out read-only shared accounts" do
    sign_in users(:family_member)
    get transactions_url
    count_before = rendered_uncategorized_count

    accounts(:credit_card).entries.create!( # shared read_only with family_member
      name: "Uncategorized on read-only share",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )
    accounts(:depository).entries.create!( # shared full_control with family_member
      name: "Uncategorized on full-control share",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    get transactions_url
    assert_equal count_before + 1, rendered_uncategorized_count,
      "the categorize count should only include entries the member can categorize"
  end

  test "index projected_recurring cache is invalidated when account-share access is revoked" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    member = users(:family_member)
    share = account_shares(:depository_shared_with_member)
    recurring = recurring_transactions(:netflix_subscription)
    assert_equal share.account_id, recurring.account_id,
      "fixture assumption: the shared depository account has the netflix recurring charge"

    merchant_name_pattern = /#{Regexp.escape(recurring.merchant.name)}/

    sign_in member
    get transactions_url
    assert_match merchant_name_pattern, response.body,
      "the member should see the shared account's recurring transaction before revocation"

    share.destroy!

    get transactions_url
    assert_no_match merchant_name_pattern, response.body,
      "revoking account-share access must not leave a stale cached projected-recurring list"
  ensure
    Rails.cache = original_cache
  end

  test "index projected_recurring cache reflects a merchant rename immediately" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    recurring = recurring_transactions(:netflix_subscription)
    merchant = recurring.merchant

    get transactions_url
    assert_match(/#{Regexp.escape(merchant.name)}/, response.body,
      "the recurring transaction should render with the merchant's original name")

    merchant.update!(name: "Netflix Renamed")

    get transactions_url
    assert_match(/Netflix Renamed/, response.body,
      "renaming the merchant must not leave a stale cached projected-recurring list")
  ensure
    Rails.cache = original_cache
  end

  test "index projected_recurring cache reflects a provider merchant update immediately" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    # Recurring detection copies transaction.merchant_id, which can point at a
    # shared ProviderMerchant (not just a family-owned FamilyMerchant) -- e.g.
    # ProviderMerchant::Enhancer updates these after the recurring row exists.
    provider_merchant = ProviderMerchant.create!(name: "Provider Merchant Original", source: "enable_banking")
    recurring = recurring_transactions(:netflix_subscription)
    recurring.update!(merchant: provider_merchant, name: nil)

    get transactions_url
    assert_match(/Provider Merchant Original/, response.body,
      "the recurring transaction should render with the provider merchant's original name")

    provider_merchant.update!(name: "Provider Merchant Renamed")

    get transactions_url
    assert_match(/Provider Merchant Renamed/, response.body,
      "updating the provider merchant must not leave a stale cached projected-recurring list")
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count cache reflects deleting an uncategorized transaction immediately" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    entry = @user.family.accounts.visible.first.entries.create!(
      name: "Deleted while uncategorized",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    get transactions_url
    initial_count = rendered_uncategorized_count

    entry.destroy!

    get transactions_url
    updated_count = rendered_uncategorized_count

    assert_equal initial_count - 1, updated_count,
      "deleting an uncategorized transaction must not leave a stale cached count"
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count cache reflects disabling an account immediately" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    account = @user.family.accounts.visible.first
    account.entries.create!(
      name: "Uncategorized on account about to be disabled",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    get transactions_url
    initial_count = rendered_uncategorized_count
    assert_operator initial_count, :>, 0

    account.disable!

    get transactions_url
    updated_count = rendered_uncategorized_count

    assert_operator updated_count, :<, initial_count,
      "disabling an account must not leave a stale cached count that still includes its transactions"
  ensure
    Rails.cache = original_cache
  end

  test "index uncategorized_count cache is scoped per user, not just per family" do
    original_cache = Rails.cache
    Rails.cache = ActiveSupport::Cache::MemoryStore.new

    # family_member has no share on this account (see test/fixtures/account_shares.yml),
    # so only the admin can see its uncategorized transaction.
    accounts(:other_asset).entries.create!(
      name: "Admin-only uncategorized transaction",
      amount: 42,
      currency: "USD",
      date: Date.current,
      entryable: Transaction.new
    )

    get transactions_url
    admin_count = rendered_uncategorized_count
    assert_operator admin_count, :>, 0

    sign_in users(:family_member)
    get transactions_url
    member_count = rendered_uncategorized_count

    assert_not_equal admin_count, member_count,
      "a member without access to the admin-only account must not reuse the admin's cached uncategorized count"
  ensure
    Rails.cache = original_cache
  end

  test "category-only update does not sync the account" do
    assert_no_enqueued_jobs only: SyncJob do
      patch transaction_url(@entry), params: {
        entry: { entryable_attributes: { id: @entry.entryable_id, category_id: categories(:food_and_drink).id } }
      }
    end

    assert_equal categories(:food_and_drink).id, @entry.reload.entryable.category_id
  end

  test "amount update syncs the account" do
    assert_enqueued_with(job: SyncJob) do
      patch transaction_url(@entry), params: { entry: { amount: @entry.amount + 10 } }
    end
  end

  test "category and date updated together still sync the account" do
    assert_enqueued_with(job: SyncJob) do
      patch transaction_url(@entry), params: {
        entry: {
          date: @entry.date - 1.day,
          entryable_attributes: { id: @entry.entryable_id, category_id: categories(:food_and_drink).id }
        }
      }
    end
  end

  test "toggling a tag does not sync the account" do
    assert_no_enqueued_jobs only: SyncJob do
      patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:two).id }, as: :turbo_stream
    end
  end

  test "index with ai_status=current renders the AI filter badge" do
    @entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "ai")

    get transactions_url(q: { ai_status: [ "current" ] })

    assert_response :success
    assert_select "#transaction-search-filters li p", text: "AI", count: 1
    assert_select "#entry_#{@entry.id}", count: 1
  end

  test "index with ai_status=history renders the AI history filter badge" do
    @entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "ai")
    @entry.entryable.update!(category: categories(:subcategory))

    get transactions_url(q: { ai_status: [ "history" ] })

    assert_response :success
    assert_select "#transaction-search-filters li p", text: "AI history", count: 1
    assert_select "#entry_#{@entry.id}", count: 1
  end

  test "index with ai_status=current excludes history-only transactions" do
    @entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "ai")
    @entry.entryable.update!(category: categories(:subcategory))

    get transactions_url(q: { ai_status: [ "current" ] })

    assert_response :success
    assert_select "#transaction-search-filters li p", text: "AI", count: 1
    assert_select "#entry_#{@entry.id}", count: 0
  end

  test "index ignores unsupported ai_status values without rendering a badge" do
    get transactions_url(q: { ai_status: [ "bogus" ] })

    assert_response :success
    assert_select "#transaction-search-filters li", count: 0
    assert_no_match(/translation missing/, response.body)

    # The bogus value must be dropped entirely, not applied as a filter
    assert_select "#entry_#{@entry.id}", count: 1
  end

  test "clear_filter removes an ai_status value and redirects" do
    delete clear_filter_transactions_url(
      param_key: "ai_status",
      param_value: "current",
      q: { ai_status: [ "current" ] }
    )

    assert_response :redirect
    assert_includes response.location, "filter_cleared=1"
    assert_no_match(/ai_status/, response.location)
  end

  test "index says which currencies are left out of its totals" do
    family = @user.family
    family.accounts.create!(name: "KRW Savings", balance: 1_000_000, currency: "KRW", accountable: Depository.new)

    get transactions_path
    assert_response :ok

    assert_select "[data-testid='missing-exchange-rates']", text: /No exchange rate from KRW to USD is stored yet, so some totals may leave out amounts in KRW/
  end

  test "index with attachment_status=with lists only transactions that have a file" do
    @entry.entryable.attachments.attach(io: StringIO.new("invoice"), filename: "invoice.pdf", content_type: "application/pdf")

    get transactions_url(q: { attachment_status: [ "with" ] })

    assert_response :success
    assert_select "#transaction-search-filters li p", text: "With attachment", count: 1
    assert_select "#entry_#{@entry.id}", count: 1
  end

  test "index with attachment_status=without hides transactions that have a file" do
    @entry.entryable.attachments.attach(io: StringIO.new("invoice"), filename: "invoice.pdf", content_type: "application/pdf")

    get transactions_url(q: { attachment_status: [ "without" ] })

    assert_response :success
    assert_select "#transaction-search-filters li p", text: "Without attachment", count: 1
    assert_select "#entry_#{@entry.id}", count: 0
  end

  test "index names the account on an account_ids filter chip" do
    account = @entry.account

    get transactions_url(q: { account_ids: [ account.id ] })

    assert_response :success
    assert_select "#transaction-search-filters li p", text: account.name, count: 1
    assert_select "#transaction-search-filters li p", text: account.id, count: 0
  end

  test "index renders a blank account filter value without failing" do
    get transactions_url(q: { account_ids: [ "" ], accounts: [ "" ] })

    assert_response :success
  end

  test "index ignores unsupported attachment_status values without rendering a badge" do
    get transactions_url(q: { attachment_status: [ "bogus" ] })

    assert_response :success
    assert_select "#transaction-search-filters li", count: 0
    assert_select "#entry_#{@entry.id}", count: 1
  end

  test "clear_filter removes an attachment_status value and redirects" do
    delete clear_filter_transactions_url(
      param_key: "attachment_status",
      param_value: "without",
      q: { attachment_status: [ "without" ] }
    )

    assert_response :redirect
    assert_includes response.location, "filter_cleared=1"
    assert_no_match(/attachment_status/, response.location)
  end

  test "an unconfirmed auto-matched transfer keeps its category menu in the list" do
    outflow = create_transaction(account: accounts(:depository), amount: 500, name: "PENDING_MATCH_OUT").transaction
    inflow = create_transaction(account: accounts(:credit_card), amount: -500).transaction
    Transfer.create!(outflow_transaction: outflow, inflow_transaction: inflow)

    get transactions_url

    assert_response :success
    assert_select "##{dom_id(outflow, "category_menu_desktop")} [data-ds--popover-frame-id-value='category_dropdown']"
  end

  test "deleting a split is refused while a part is linked as a transfer" do
    parent = create_transaction(account: accounts(:depository), amount: 100, name: "Mortgage")
    part = parent.split!([ { name: "Principal", amount: 60 }, { name: "Interest", amount: 40 } ]).first
    link_split_part_to_loan(part)

    assert_no_difference [ "Entry.count", "Transfer.count" ] do
      delete transaction_url(parent)
    end

    assert_equal I18n.t("account.entries.destroy.part_transferred"), flash[:alert]
  end

  test "update keeps a split part's amount, currency and kind" do
    parent = create_transaction(account: accounts(:depository), amount: 100, name: "Mortgage")
    part = parent.split!([ { name: "Principal", amount: 60 }, { name: "Interest", amount: 40 } ]).first
    link_split_part_to_loan(part)
    part.transaction.update!(kind: "loan_payment")

    patch transaction_url(part), params: {
      entry: {
        amount: "75",
        currency: "EUR",
        nature: "outflow",
        notes: "Principal part",
        entryable_type: "Transaction",
        entryable_attributes: { id: part.entryable_id, kind: "standard" }
      }
    }

    part.reload
    assert_equal 60, part.amount
    assert_equal "USD", part.currency
    assert_equal "loan_payment", part.transaction.kind
    assert_equal "Principal part", part.notes
  end

  test "update drops a category sent for a matched transfer's inflow leg" do
    outflow = Transaction.create!(kind: "investment_contribution")
    Entry.create!(account: accounts(:depository), entryable: outflow, name: "Contribution", amount: 500, currency: "USD", date: Date.current)
    inflow = Transaction.create!(kind: "funds_movement")
    inflow_entry = Entry.create!(account: accounts(:investment), entryable: inflow, name: "Contribution", amount: -500, currency: "USD", date: Date.current)
    Transfer.create!(inflow_transaction: inflow, outflow_transaction: outflow, status: "confirmed")

    patch transaction_url(inflow_entry), params: {
      entry: {
        notes: "Monthly savings",
        entryable_type: "Transaction",
        entryable_attributes: { id: inflow.id, category_id: categories(:income).id }
      }
    }

    assert_nil inflow.reload.category_id
    assert_equal "Monthly savings", inflow_entry.reload.notes
  end

  test "update drops a category for a matched inflow leg whose kind was edited to standard" do
    outflow = Transaction.create!(kind: "investment_contribution")
    Entry.create!(account: accounts(:depository), entryable: outflow, name: "Contribution", amount: 500, currency: "USD", date: Date.current)
    inflow = Transaction.create!(kind: "standard")
    inflow_entry = Entry.create!(account: accounts(:investment), entryable: inflow, name: "Contribution", amount: -500, currency: "USD", date: Date.current)
    Transfer.create!(inflow_transaction: inflow, outflow_transaction: outflow, status: "confirmed")

    patch transaction_url(inflow_entry), params: {
      entry: { entryable_type: "Transaction", entryable_attributes: { id: inflow.id, category_id: categories(:income).id } }
    }

    assert_nil inflow.reload.category_id
  end

  test "drawer shows a matched transfer's inflow leg with the outflow's category, disabled" do
    outflow = Transaction.create!(kind: "investment_contribution", category: categories(:income))
    Entry.create!(account: accounts(:depository), entryable: outflow, name: "Contribution", amount: 500, currency: "USD", date: Date.current)
    inflow = Transaction.create!(kind: "funds_movement")
    inflow_entry = Entry.create!(account: accounts(:investment), entryable: inflow, name: "Contribution", amount: -500, currency: "USD", date: Date.current)
    Transfer.create!(inflow_transaction: inflow, outflow_transaction: outflow, status: "confirmed")

    get transaction_url(inflow_entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_select "button#category_id_trigger[disabled]", text: /#{categories(:income).name}/
    assert_select "input[type=hidden][name='entry[entryable_attributes][category_id]'][disabled]"
    assert_match I18n.t("transactions.show.category_set_on_outflow"), response.body
  end

  test "creating a transaction runs rules set to apply immediately" do
    create_immediate_rule

    post transactions_url, params: {
      entry: {
        account_id: @entry.account_id, name: "New transaction", date: Date.current,
        currency: "USD", amount: 100, nature: "outflow", entryable_type: "Transaction",
        entryable_attributes: { category_id: "" }
      }
    }

    created_entry = Entry.order(:created_at).last
    assert_enqueued_with(job: ApplyImmediateRulesJob, args: [ @user.family, { transaction_ids: [ created_entry.entryable_id ] } ])
  end

  test "changing a transaction runs rules set to apply immediately" do
    create_immediate_rule

    patch transaction_url(@entry), params: { entry: { name: "Renamed" } }

    assert_enqueued_with(job: ApplyImmediateRulesJob, args: [ @user.family, { transaction_ids: [ @entry.entryable_id ] } ])
  end

  test "changing a transaction queues nothing without immediate rules" do
    patch transaction_url(@entry), params: { entry: { name: "Renamed" } }

    assert_no_enqueued_jobs(only: ApplyImmediateRulesJob)
  end

  test "an update that changes nothing runs no immediate rules" do
    create_immediate_rule

    patch transaction_url(@entry), params: { entry: { name: @entry.name, notes: @entry.notes } }

    assert_no_enqueued_jobs(only: ApplyImmediateRulesJob)
  end

  test "an update reduced to nothing by the account permission runs no immediate rules" do
    create_immediate_rule
    read_only_entry = entries(:transfer_in) # credit card, shared read_only with family_member
    sign_in users(:family_member)

    patch transaction_url(read_only_entry), params: { entry: { name: "Renamed" } }

    assert_not_equal "Renamed", read_only_entry.reload.name
    assert_no_enqueued_jobs(only: ApplyImmediateRulesJob)
  end

  test "setting the same tags again runs no immediate rules" do
    create_immediate_rule
    @entry.transaction.update!(tag_ids: [ tags(:one).id ])

    patch tags_transaction_url(@entry, format: :json), params: { tag_ids: [ tags(:one).id ] }

    assert_response :success
    assert_no_enqueued_jobs(only: ApplyImmediateRulesJob)
  end

  test "toggling a tag runs immediate rules" do
    create_immediate_rule

    patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:two).id }, as: :turbo_stream

    assert_enqueued_with(job: ApplyImmediateRulesJob, args: [ @user.family, { transaction_ids: [ @entry.entryable_id ] } ])
  end

  # A rule can change these fields while the drawer is open. Sharing a form
  # with another field would send the drawer's stale value back on the next
  # save of that field, undoing the rule's result and locking it.
  test "the drawer sends each field that rules can set in a form of its own" do
    rule_fields = %w[entry[name] entry[excluded] entry[entryable_attributes][category_id] entry[entryable_attributes][merchant_id] entry[entryable_attributes][tag_ids][]]
    form_meta = %w[authenticity_token _method grouped entry[entryable_attributes][id]]

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }
    assert_response :success

    seen = css_select("form[action='#{transaction_path(@entry)}']").filter_map do |form|
      names = form.css("input[name], select[name], textarea[name]").reject { |field| field["disabled"] }.map { |field| field["name"] }.uniq - form_meta
      next if (names & rule_fields).empty?

      assert_equal 1, names.size, "#{names.inspect} share one form"
      names.first
    end

    assert_equal rule_fields.sort, seen.sort
    # Leaving the name field untouched must not send its stale value either.
    assert_select "input[name='entry[name]'][data-autosubmit-trigger-event=change]"
  end

  test "does not offer guests to create a rule through a transaction update" do
    category = categories(:income)
    @entry.update!(name: "AMZN Mktp UK")
    rule = @entry.account.family.rules.build(name: "Amazon", resource_type: "transaction", active: true)
    rule.conditions.build(condition_type: "transaction_name", operator: "like", value: "AMAZON.CO.UK")
    rule.actions.build(action_type: "set_transaction_category", value: category.id.to_s)
    rule.save!
    @entry.account.update!(owner: family_guest)
    sign_in family_guest

    patch transaction_url(@entry), params: {
      entry: {
        entryable_type: @entry.entryable_type,
        entryable_attributes: { id: @entry.entryable_id, category_id: category.id }
      }
    }, as: :turbo_stream

    assert_response :success
    assert_equal category.id, @entry.reload.entryable.category_id
    assert_not_includes response.body, "action_type=set_transaction_category"
  end

  test "transaction drawer shows auto-categorization provenance" do
    @entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "ai")

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_select "[data-testid='category-provenance']"
    assert_match(/Assigned &quot;Income&quot; by AI .* ago\./, response.body)
  end

  test "transaction drawer labels bayes provenance as Bayes" do
    @entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "bayes")

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_select "[data-testid='category-provenance'] span[title=?]", "Bayes"
    assert_match(/Assigned &quot;Income&quot; by the local Bayes classifier .* ago\./, response.body)
  end

  test "transaction drawer shows history for a category returned to a superseded claim" do
    @entry.entryable.enrich_attribute(:category_id, categories(:income).id, source: "ai")
    @entry.entryable.update!(category: categories(:subcategory))
    @entry.entryable.update!(category: categories(:income))

    get transaction_url(@entry), headers: { "Turbo-Frame" => "drawer" }

    assert_response :success
    assert_select "[data-testid='category-provenance'] span[title=?]", "AI history"
    assert_includes response.body, "The category was changed afterwards."
  end

  private
    def create_immediate_rule
      @user.family.rules.create!(
        name: "Immediate", resource_type: "transaction", active: true, apply_immediately: true,
        conditions: [ Rule::Condition.new(condition_type: "transaction_name", operator: "like", value: "Renamed") ],
        actions: [ Rule::Action.new(action_type: "set_transaction_category", value: categories(:food_and_drink).id) ]
      )
    end

    # The Allocator's two-key advisory lock on this entry, held by this session.
    def entry_lock_held?(entry)
      ActiveRecord::Base.connection.select_value(ActiveRecord::Base.sanitize_sql_array([ <<~SQL, RecurringTransaction::Allocator::ENTRY_LOCK_NAMESPACE, RecurringTransaction::Allocator.entry_lock_id(entry) ]))
        SELECT EXISTS (
          SELECT 1 FROM pg_locks
          WHERE locktype = 'advisory' AND pid = pg_backend_pid() AND granted
            AND classid = ? AND objid = ? AND objsubid = 2
        )
      SQL
    end

    def rendered_entry_ids
      css_select("turbo-frame[id^='entry_']").map { |node| node["id"].delete_prefix("entry_") }
    end

    def normalize_sql_query(sql)
      sql.to_s.squish.gsub(/[`"]/, "").downcase
    end

    # The "Categorize (N)" menu item only renders when @uncategorized_count
    # is positive, so an absent match means the count is 0.
    def rendered_uncategorized_count
      match = response.body.match(/Categorize \((\d+)\)/)
      match ? match[1].to_i : 0
    end

    # Per-row lazy loads use `column = ?`. Do not treat `IN (...)` as lazy
    # loads — ActiveRecord nested preloads also use single-value IN when only
    # one associated record is needed (e.g. one counterparty account).
    def single_record_lookups(normalized_queries, table:, column:)
      pattern = /
        from\s+#{Regexp.escape(table)}\s+
        where\s+#{Regexp.escape(table)}\.#{Regexp.escape(column)}\s*=
      /x

      normalized_queries.grep(pattern)
    end

    def capture_sql_queries
      queries = []
      callback = lambda do |_name, _started, _finished, _unique_id, payload|
        next if payload[:cached]
        next if %w[SCHEMA TRANSACTION].include?(payload[:name])

        queries << payload[:sql].squish
      end

      ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
        yield
      end

      queries
    end
end