Files
sure/test/models/wise_entry/processor_test.rb
Blaž Dular 826c4a356e feat(bank-sync): Wise integration (#2433)
* feat(wise): add Wise integration with JAR savings account and activity support

- Add WiseItem/WiseAccount models with full sync pipeline (importer, syncer, processor)
- Detect income vs expense using targetAccount == recipientId from borderless accounts API
- Support JAR (SAVINGS) accounts with totalWorth balance and savings subtype
- Fetch JAR activity via profile activities API (INTERBALANCE, BALANCE_CASHBACK, BALANCE_ASSET_FEE)
- Route INTERBALANCE activities to both JAR and STANDARD accounts and link as Transfer records
- Add provider connection status registration, routes, views, and i18n
- Add migration for wise_items and wise_accounts tables
- Add tests for WiseAccount, WiseEntry::Processor, WiseActivity::Processor, WiseItem::Importer, and WiseItem#link_jar_transfers!

* chore(lint): add ignore to security scan (false positive)

* fix(wise): address PR review feedback on activity routing, HTML stripping, rate limiting, and scope extraction

- Replace title string matching in activity_for_account? with resource.id vs balance_id comparison to avoid breakage when users rename JAR accounts on Wise
- Replace gsub(/<[^>]+>/, "") with ActionController::Base.helpers.strip_tags to safely handle user-controlled HTML-like content
- Wrap paginated API calls in with_rate_limit_retry (up to 3 attempts, exponential backoff) to handle 429 responses during fetch_jar_activities and fetch_transfers
- Extract WiseAccount.unlinked scope and remove duplicated left_joins query from controller

* fix(wise): address PR #2433 review feedback

- Wire @wise_items into AccountsController#index so linked accounts appear on /accounts
- Fix find_wise_account_for_linking to preserve .active scope via .merge instead of .then
- Fix cross-currency incoming transfer amount to use targetValue instead of sourceValue
- Add missing select_profiles.session_expired locale key
- Add missing account_name interpolation to link_existing_account success notice
- Use i18n for profile type labels in profile_display_name
- Trigger wise_item.sync_later after account linking in all three linking actions
- Isolate fee transaction failure so it no longer aborts the main transfer import
- Use bare raise in WiseActivity/WiseEntry processors to preserve original backtrace
- Re-raise in WiseItem::Unlinking to prevent silently orphaned Holdings
- Fix avatar badge to use design system tokens (bg-container-inset, text-primary)

* fix(wise): fix INTERBALANCE routing and encrypt pending token in session

* fix(wise): replace hand-rolled buttons/links with DS::Button and DS::Link

Address repeated sure-design DS drift findings: migrate all manual
Tailwind button_to and link_to calls in Wise views to DS::Button
(outline/outline_destructive variants) and DS::Link (secondary variant).
Add missing provider_panel.disconnect locale key for the disconnect button text.

* fix(wise): use render_provider_panel_error in create instead of missing new template

* fix(wise): add missing comma in PROVIDERS array

CI failed with a SyntaxError because the questrade entry wasn't
comma-terminated before the wise entry.

* chore(wise): re-add pipelock:ignore for pending token param

Lost when the token source moved from session to an encrypted params
field in 0b5dc886, causing the CI secret scanner to flag a false positive.

* fix(test): widen random ticker suffix to avoid rare collision flake

hex(2) only yields 65536 possible tickers, so create_trade's 4 calls per
test run had a small but nonzero chance of colliding on the unique
ticker+exchange index. hex(8) makes collisions practically impossible.
2026-07-14 03:16:28 +02:00

178 lines
6.3 KiB
Ruby

require "test_helper"
class WiseEntry::ProcessorTest < ActiveSupport::TestCase
setup do
@family = families(:empty)
@wise_item = WiseItem.create!(
family: @family,
name: "Test Wise",
token: "test_token",
profile_id: "123",
profile_type: :business
)
@wise_account = WiseAccount.create!(
wise_item: @wise_item,
balance_id: "10000001",
name: "Wise EUR",
currency: "EUR",
raw_payload: { "type" => "STANDARD", "recipient_id" => 99999001 }
)
@account = Account.create!(
family: @family,
name: "Wise EUR",
accountable: Depository.new(subtype: "checking"),
balance: 0,
currency: "EUR"
)
AccountProvider.create!(account: @account, provider: @wise_account)
end
# Income vs expense detection
test "imports outgoing transfer (targetAccount != recipientId) as positive expense" do
transfer = build_transfer(id: 1001, target_account: 9999999, source_value: 500.0, target_value: 500.0)
entry = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal BigDecimal("500.0"), entry.amount
assert_equal "outgoing", entry.entryable.extra.dig("wise", "direction")
end
test "imports incoming transfer (targetAccount == recipientId) as negative income" do
transfer = build_transfer(id: 1002, target_account: 99999001, source_value: 1200.0, target_value: 1200.0)
entry = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal BigDecimal("-1200.0"), entry.amount
assert_equal "incoming", entry.entryable.extra.dig("wise", "direction")
end
test "uses status-based fallback when no recipient_id stored" do
@wise_account.update!(raw_payload: { "type" => "STANDARD" })
outgoing = build_transfer(id: 1003, target_account: 9999, source_value: 100.0,
status: "outgoing_payment_sent")
entry = WiseEntry::Processor.new(outgoing, wise_account: @wise_account).process
assert entry.amount.positive?
incoming = build_transfer(id: 1004, target_account: 9999, source_value: 100.0,
status: "funds_credited")
entry = WiseEntry::Processor.new(incoming, wise_account: @wise_account).process
assert entry.amount.negative?
end
# External ID and deduplication
test "uses stable external_id based on transfer id" do
transfer = build_transfer(id: 2001, target_account: 9999)
entry1 = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
entry2 = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal "wise_transfer_2001", entry1.external_id
assert_equal entry1.id, entry2.id
assert_equal 1, @account.entries.where(source: "wise").count
end
# Reference as name
test "uses details.reference as transaction name when present" do
transfer = build_transfer(id: 3001, target_account: 9999, reference: "Invoice #42")
entry = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal "Invoice #42", entry.name
end
test "falls back to default name when no reference" do
transfer = build_transfer(id: 3002, target_account: 9999, reference: nil)
entry = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal I18n.t("wise_items.entries.default_name"), entry.name
end
# Fee handling
test "creates separate fee entry when sourceValue exceeds targetValue in same currency" do
transfer = build_transfer(id: 4001, target_account: 9999, source_value: 100.5, target_value: 100.0)
WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
entries = @account.entries.where(source: "wise").order(:created_at)
assert_equal 2, entries.count
fee_entry = entries.find { |e| e.external_id == "wise_fee_4001" }
assert_not_nil fee_entry
assert_equal BigDecimal("0.5"), fee_entry.amount
assert_equal I18n.t("wise_items.entries.fee_name"), fee_entry.name
end
test "does not create fee entry when sourceValue equals targetValue" do
transfer = build_transfer(id: 4002, target_account: 9999, source_value: 200.0, target_value: 200.0)
WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal 1, @account.entries.where(source: "wise").count
end
test "does not create fee entry for cross-currency transfers" do
transfer = build_transfer(id: 4003, target_account: 9999, source_value: 100.0, target_value: 90.0,
source_currency: "EUR", target_currency: "GBP")
WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
assert_equal 1, @account.entries.where(source: "wise").count
end
# Skips without linked account
test "returns skipped when no account linked" do
unlinked_account = WiseAccount.create!(
wise_item: @wise_item,
balance_id: "10000099",
name: "Unlinked",
currency: "GBP"
)
result = WiseEntry::Processor.new(build_transfer(id: 5001, target_account: 9999),
wise_account: unlinked_account).process
assert_equal :skipped, result
end
# Extra metadata
test "stores wise metadata in entry extra" do
transfer = build_transfer(id: 6001, target_account: 9999, source_value: 50.0, reference: "test ref")
entry = WiseEntry::Processor.new(transfer, wise_account: @wise_account).process
extra = entry.entryable.extra
assert_equal 6001, extra.dig("wise", "transfer_id")
assert_equal "outgoing_payment_sent", extra.dig("wise", "status")
assert_equal "EUR", extra.dig("wise", "source_currency")
assert_equal "test ref", extra.dig("wise", "reference")
end
private
def build_transfer(id:, target_account:, source_value: 100.0, target_value: nil,
source_currency: "EUR", target_currency: nil, status: "outgoing_payment_sent",
reference: nil)
{
"id" => id,
"targetAccount" => target_account,
"sourceAccount" => nil,
"sourceCurrency" => source_currency,
"sourceValue" => source_value,
"targetCurrency" => target_currency || source_currency,
"targetValue" => target_value || source_value,
"status" => status,
"rate" => 1.0,
"created" => "2026-01-15 10:00:00",
"details" => reference ? { "reference" => reference } : {}
}
end
end