The personal finance app for everyone (by everyone)
Ruby
fix: support all snaptrade sandbox transaction types and zero-cash corporate actions (#3653)
Teju Nareddy75a0c41af0f0
diff --git a/app/models/snaptrade_account/activities_processor.rb b/app/models/snaptrade_account/activities_processor.rbindex 8d8f6b9fb5..66734db372 100644--- a/app/models/snaptrade_account/activities_processor.rb+++ b/app/models/snaptrade_account/activities_processor.rb@@ -1,48 +1,75 @@class SnaptradeAccount::ActivitiesProcessorinclude SnaptradeAccount::DataHelpers- # Map SnapTrade activity types to Sure activity labels- # SnapTrade types: https://docs.snaptrade.com/reference/get_activities- SNAPTRADE_TYPE_TO_LABEL = {- "BUY" => "Buy",- "SELL" => "Sell",- "DIVIDEND" => "Dividend",- "DIV" => "Dividend",- "CONTRIBUTION" => "Contribution",- "WITHDRAWAL" => "Withdrawal",- "TRANSFER_IN" => "Transfer",- "TRANSFER_OUT" => "Transfer",- "TRANSFER" => "Transfer",- "INTEREST" => "Interest",- "FEE" => "Fee",- "TAX" => "Fee",- "REI" => "Reinvestment", # Reinvestment- "REINVEST" => "Reinvestment",- "SPLIT" => "Other",- "SPLIT_REVERSE" => "Other", # Reverse stock split- "MERGER" => "Other",- "SPIN_OFF" => "Other",- "STOCK_DIVIDEND" => "Dividend",- "JOURNAL" => "Other",- "CASH" => "Contribution", # Cash deposit (non-retirement)- "CORP_ACTION" => "Other", # Corporate action- "OTHER" => "Other",- # Option activity types- "OPTION_BUY" => "Buy", # Buy to open/close option- "OPTION_SELL" => "Sell", # Sell to open/close option- "EXERCISED" => "Other", # Option exercised- "EXPIRED" => "Other", # Option expired worthless- "ASSIGNED" => "Other" # Option assignment+ # Declarative configuration for SnapTrade activity types.+ # Defines category (:trade, :unit_dependent, or :cash), investment activity label,+ # quantity signing convention (:buy, :sell, or :preserve), and amount handling (:unconditional, :if_absent, or nil).+ ACTIVITY_RULES = {+ # Buys+ "BUY" => { category: :trade, label: "Buy", sign: :buy },++ # Sells+ "SELL" => { category: :trade, label: "Sell", sign: :sell },++ # Reinvestments+ "REI" => { category: :trade, label: "Reinvestment", sign: :buy },+ "REINVEST" => { category: :trade, label: "Reinvestment", sign: :buy },++ # Options+ "OPTION_BUY" => { category: :trade, label: "Buy", sign: :buy },+ "OPTION_SELL" => { category: :trade, label: "Sell", sign: :sell },+ "OPTIONASSIGNMENT" => { category: :trade, label: "Other", sign: :sell },+ "ASSIGNED" => { category: :trade, label: "Other", sign: :sell },+ "OPTIONEXERCISE" => { category: :trade, label: "Other", sign: :preserve },+ "EXERCISED" => { category: :trade, label: "Other", sign: :preserve },+ "OPTIONEXPIRATION" => { category: :trade, label: "Other", sign: :preserve, zero_amount: :unconditional },+ "EXPIRED" => { category: :trade, label: "Other", sign: :preserve, zero_amount: :unconditional },++ # Stock dividends and splits+ "STOCK_DIVIDEND" => { category: :trade, label: "Dividend", sign: :buy, zero_amount: :unconditional },+ "SPLIT" => { category: :trade, label: "Other", sign: :buy, zero_amount: :unconditional },+ "REVERSE_SPLIT" => { category: :trade, label: "Other", sign: :sell, zero_amount: :unconditional },+ "SPLIT_REVERSE" => { category: :trade, label: "Other", sign: :sell, zero_amount: :unconditional },+ "SPINOFF" => { category: :trade, label: "Other", sign: :buy, zero_amount: :unconditional },+ "SPIN_OFF" => { category: :trade, label: "Other", sign: :buy, zero_amount: :unconditional },++ # Asset transfers+ "EXTERNAL_ASSET_TRANSFER_IN" => { category: :trade, label: "Transfer", sign: :buy, zero_amount: :unconditional },+ "EXTERNAL_ASSET_TRANSFER_OUT" => { category: :trade, label: "Transfer", sign: :sell, zero_amount: :unconditional },+ "INTERNAL_ASSET_TRANSFER_IN" => { category: :trade, label: "Transfer", sign: :buy, zero_amount: :unconditional },+ "INTERNAL_ASSET_TRANSFER_OUT" => { category: :trade, label: "Transfer", sign: :sell, zero_amount: :unconditional },++ # Unit-dependent types:+ # When units are present and non-zero, treated as a trade preserving signed units.+ # When units are nil/zero, treated as a cash activity.+ "ADJUSTMENT" => { category: :unit_dependent, label: "Other", sign: :preserve, zero_amount: :unconditional, cash_flow: :invert },+ "STOCK_MERGER" => { category: :unit_dependent, label: "Other", sign: :preserve, zero_amount: :if_absent, cash_flow: :invert },+ "MERGER" => { category: :unit_dependent, label: "Other", sign: :preserve, zero_amount: :if_absent, cash_flow: :invert },+ "CORP_ACTION" => { category: :unit_dependent, label: "Other", sign: :preserve, zero_amount: :if_absent, cash_flow: :invert },++ # Cash activities+ "DIVIDEND" => { category: :cash, label: "Dividend", cash_flow: :inflow },+ "DIV" => { category: :cash, label: "Dividend", cash_flow: :inflow },+ "CONTRIBUTION" => { category: :cash, label: "Contribution", cash_flow: :inflow },+ "WITHDRAWAL" => { category: :cash, label: "Withdrawal", cash_flow: :outflow },+ "TRANSFER" => { category: :cash, label: "Transfer", cash_flow: :invert },+ "TRANSFER_IN" => { category: :cash, label: "Transfer", cash_flow: :inflow },+ "TRANSFER_OUT" => { category: :cash, label: "Transfer", cash_flow: :outflow },+ "INTERNAL_CASH_TRANSFER_IN" => { category: :cash, label: "Transfer", cash_flow: :inflow },+ "INTERNAL_CASH_TRANSFER_OUT" => { category: :cash, label: "Transfer", cash_flow: :outflow },+ "INTEREST" => { category: :cash, label: "Interest", cash_flow: :inflow },+ "FEE" => { category: :cash, label: "Fee", cash_flow: :outflow },+ "TAX" => { category: :cash, label: "Fee", cash_flow: :outflow },+ "CASH" => { category: :cash, label: "Contribution", cash_flow: :inflow },+ "REBATE" => { category: :cash, label: "Other", cash_flow: :inflow },+ "RETURN_OF_CAPITAL" => { category: :cash, label: "Dividend", cash_flow: :inflow },+ "DISTRIBUTION" => { category: :cash, label: "Dividend", cash_flow: :inflow },+ "SUBSTITUTE_DIVIDEND" => { category: :cash, label: "Dividend", cash_flow: :inflow },+ "JOURNAL" => { category: :cash, label: "Other" },+ "OTHER" => { category: :cash, label: "Other" }}.freeze- # Activity types that result in Trade records (involves securities)- TRADE_TYPES = %w[BUY SELL REI REINVEST OPTION_BUY OPTION_SELL EXERCISED ASSIGNED].freeze-- # Sell-side activity types (quantity should be negative)- SELL_SIDE_TYPES = %w[SELL OPTION_SELL ASSIGNED].freeze-- # Activity types that result in Transaction records (cash movements)- CASH_TYPES = %w[DIVIDEND DIV CONTRIBUTION WITHDRAWAL TRANSFER_IN TRANSFER_OUT TRANSFER INTEREST FEE TAX CASH].freeze+ SNAPTRADE_TYPE_TO_LABEL = ACTIVITY_RULES.transform_values { |r| r[:label] }.freezedef initialize(snaptrade_account)@snaptrade_account = snaptrade_account@@ -122,15 +149,27 @@ class SnaptradeAccount::ActivitiesProcessorRails.logger.info "SnaptradeAccount::ActivitiesProcessor - Processing activity: type=#{activity_type}, id=#{external_id}"# Determine if this is a trade or cash activity- if trade_activity?(activity_type)+ if trade_activity?(activity_type, data)process_trade(data, activity_type, external_id)elseprocess_cash_activity(data, activity_type, external_id)endend- def trade_activity?(activity_type)- TRADE_TYPES.include?(activity_type)+ def trade_activity?(activity_type, data = {})+ rule = ACTIVITY_RULES[activity_type]+ return false unless rule++ case rule[:category]+ when :trade+ true+ when :unit_dependent+ units = parse_decimal(data[:units]) || parse_decimal(data["units"]) ||+ parse_decimal(data[:quantity]) || parse_decimal(data["quantity"])+ units&.nonzero? ? true : false+ else+ false+ endenddef process_trade(data, activity_type, external_id)@@ -188,19 +227,37 @@ class SnaptradeAccount::ActivitiesProcessorreturnend+ rule = ACTIVITY_RULES[activity_type] || {}+# Determine sign based on activity type (sell-side should be negative)- quantity = if SELL_SIDE_TYPES.include?(activity_type)+ quantity = case rule[:sign]+ when :preserve+ quantity+ when :sell-quantity.abselsequantity.absend- amount = if amount&.nonzero?- quantity.negative? ? -amount.abs : amount.abs- elsif price- # Same convention as a manually entered trade: the fee adds to a buy's- # cost and comes out of a sell's proceeds.- quantity * price + (fee || 0)+ case rule[:zero_amount]+ when :unconditional+ amount = BigDecimal("0.0")+ price = price.presence || BigDecimal("0.0")+ when :if_absent+ if amount&.nonzero?+ amount = quantity.negative? ? -amount.abs : amount.abs+ else+ amount = BigDecimal("0.0")+ price = price.presence || BigDecimal("0.0")+ end+ else+ amount = if amount&.nonzero?+ quantity.negative? ? -amount.abs : amount.abs+ elsif price+ # Same convention as a manually entered trade: the fee adds to a buy's+ # cost and comes out of a sell's proceeds.+ quantity * price + (fee || 0)+ endendif amount.nil?@@ -245,6 +302,8 @@ class SnaptradeAccount::ActivitiesProcessorRails.logger.info "SnaptradeAccount::ActivitiesProcessor - Importing trade: #{ticker} qty=#{quantity} price=#{price} amount=#{amount} fee=#{fee} date=#{activity_date}"+ return unless reclassify_entry_if_needed(external_id, "Trade")+result = import_adapter.import_trade(external_id: external_id,security: security,@@ -291,6 +350,8 @@ class SnaptradeAccount::ActivitiesProcessorRails.logger.info "SnaptradeAccount::ActivitiesProcessor - Importing cash activity: type=#{activity_type} amount=#{amount} date=#{activity_date}"+ return unless reclassify_entry_if_needed(external_id, "Transaction")+result = import_adapter.import_transaction(external_id: external_id,amount: amount,@@ -303,13 +364,34 @@ class SnaptradeAccount::ActivitiesProcessor@transactions_count += 1 if resultend+ def reclassify_entry_if_needed(external_id, expected_type)+ return true if external_id.blank?++ existing = account.entries.find_by(external_id: external_id, source: "snaptrade")+ return true unless existing && existing.entryable_type != expected_type++ if existing.protected_from_sync?+ capture_debug_log(+ message: "Skipping reclassification of protected entry #{existing.id} (#{external_id})",+ metadata: { activity_id: external_id, reason: "protected" }+ )+ return false+ end++ Rails.logger.info("SnaptradeAccount::ActivitiesProcessor - Reclassifying activity #{external_id} from #{existing.entryable_type} to #{expected_type}")+ existing.destroy!+ true+ end+def normalize_cash_amount(amount, activity_type)- case activity_type- when "WITHDRAWAL", "TRANSFER_OUT", "FEE", "TAX"+ rule = ACTIVITY_RULES[activity_type] || {}++ case rule[:cash_flow]+ when :outflowamount.abs # Money out should be positive in Sure- when "CONTRIBUTION", "TRANSFER_IN", "DIVIDEND", "DIV", "INTEREST", "CASH"+ when :inflow-amount.abs # Money in should be negative in Sure- when "TRANSFER"+ when :invert# Direction is not encoded in the type (unlike TRANSFER_IN/TRANSFER_OUT), so the# provider's sign is the only directional signal available. SnapTrade signs these# from the account's perspective (positive = money in), which is the inverse ofdiff --git a/test/models/snaptrade_account/activities_processor_test.rb b/test/models/snaptrade_account/activities_processor_test.rbindex ac0dd76efc..32cbc924e5 100644--- a/test/models/snaptrade_account/activities_processor_test.rb+++ b/test/models/snaptrade_account/activities_processor_test.rb@@ -317,29 +317,619 @@ class SnaptradeAccount::ActivitiesProcessorTest < ActiveSupport::TestCaseassert_equal 500.00, outbound.amount.to_f, "money out must be stored positive on an asset account"end- test "maps all known activity types correctly" do- type_mappings = {- "BUY" => "Buy",- "SELL" => "Sell",- "DIVIDEND" => "Dividend",- "DIV" => "Dividend",- "CONTRIBUTION" => "Contribution",- "WITHDRAWAL" => "Withdrawal",- "TRANSFER_IN" => "Transfer",- "TRANSFER_OUT" => "Transfer",- "INTEREST" => "Interest",- "FEE" => "Fee",- "TAX" => "Fee",- "REI" => "Reinvestment",- "REINVEST" => "Reinvestment",- "CASH" => "Contribution",- "CORP_ACTION" => "Other",- "SPLIT_REVERSE" => "Other"- }-- type_mappings.each do |snaptrade_type, expected_label|- actual = SnaptradeAccount::ActivitiesProcessor::SNAPTRADE_TYPE_TO_LABEL[snaptrade_type]- assert_equal expected_label, actual, "Type #{snaptrade_type} should map to #{expected_label}"+ test "processes TRANSFER as cash transaction even when units are present in payload" do+ # In 401(k) accounts, payroll contributions arrive as type: TRANSFER with units and cash amount+ process_activities(+ build_trade_activity(+ id: "xfer_payroll_contrib",+ type: "TRANSFER",+ symbol: "5022",+ units: 8.699,+ price: 156.36,+ amount: 1388.84+ )+ )++ entry = snaptrade_entry("xfer_payroll_contrib")+ assert_not_nil entry+ assert entry.entryable.is_a?(Transaction), "TRANSFER must be processed as a cash Transaction"+ assert_equal BigDecimal("-1388.84"), entry.amount, "cash contribution amount must be preserved as negative inflow"+ assert_equal "Transfer", entry.entryable.investment_activity_label+ end++ test "keeps cash TRANSFER with zero units on the cash transaction path" do+ process_activities(+ build_cash_activity(+ id: "xfer_cash_zero_units",+ type: "TRANSFER",+ amount: 250.00,+ settlement_date: Date.current.to_s+ ).merge("units" => 0)+ )++ entry = snaptrade_entry("xfer_cash_zero_units")+ assert_not_nil entry+ assert entry.entryable.is_a?(Transaction), "cash transfer with 0 units must remain a Transaction"+ assert_equal(-250.00, entry.amount.to_f)+ assert_equal "Transfer", entry.entryable.investment_activity_label+ end++ test "processes REBATE as negative cash inflow" do+ process_activities(+ build_cash_activity(id: "rebate_001", type: "REBATE", amount: 50.00, settlement_date: Date.current.to_s)+ )++ entry = snaptrade_entry("rebate_001")+ assert_not_nil entry+ assert_equal(-50.00, entry.amount.to_f, "rebate credited to account must be stored as negative inflow")+ assert_equal "Other", entry.entryable.investment_activity_label+ end++ test "processes RETURN_OF_CAPITAL, DISTRIBUTION, and SUBSTITUTE_DIVIDEND as negative cash inflows with Dividend label" do+ process_activities(+ build_cash_activity(id: "roc_001", type: "RETURN_OF_CAPITAL", amount: 75.00, settlement_date: Date.current.to_s, symbol: "VTI"),+ build_cash_activity(id: "dist_001", type: "DISTRIBUTION", amount: 120.00, settlement_date: Date.current.to_s, symbol: "VNQ"),+ build_cash_activity(id: "sub_div_001", type: "SUBSTITUTE_DIVIDEND", amount: 45.00, settlement_date: Date.current.to_s, symbol: "AAPL")+ )++ roc = snaptrade_entry("roc_001")+ dist = snaptrade_entry("dist_001")+ sub_div = snaptrade_entry("sub_div_001")++ assert_not_nil roc+ assert_not_nil dist+ assert_not_nil sub_div+ assert_equal(-75.00, roc.amount.to_f)+ assert_equal "Dividend", roc.entryable.investment_activity_label+ assert_equal(-120.00, dist.amount.to_f)+ assert_equal "Dividend", dist.entryable.investment_activity_label+ assert_equal(-45.00, sub_div.amount.to_f)+ assert_equal "Dividend", sub_div.entryable.investment_activity_label+ end++ test "processes cash FEE and TAX as positive outflow and INTEREST as negative inflow" do+ process_activities(+ build_cash_activity(id: "fee_001", type: "FEE", amount: -25.00, settlement_date: Date.current.to_s),+ build_cash_activity(id: "tax_001", type: "TAX", amount: 15.00, settlement_date: Date.current.to_s),+ build_cash_activity(id: "interest_001", type: "INTEREST", amount: 12.50, settlement_date: Date.current.to_s)+ )++ fee = snaptrade_entry("fee_001")+ tax = snaptrade_entry("tax_001")+ interest = snaptrade_entry("interest_001")++ assert_not_nil fee+ assert_not_nil tax+ assert_not_nil interest++ assert_equal 25.00, fee.amount.to_f+ assert_equal "Fee", fee.entryable.investment_activity_label++ assert_equal 15.00, tax.amount.to_f+ assert_equal "Fee", tax.entryable.investment_activity_label++ assert_equal(-12.50, interest.amount.to_f)+ assert_equal "Interest", interest.entryable.investment_activity_label+ end++ test "processes INTERNAL_CASH_TRANSFER_IN and OUT with correct signs" do+ process_activities(+ build_cash_activity(id: "int_cash_in", type: "INTERNAL_CASH_TRANSFER_IN", amount: 250.00, settlement_date: Date.current.to_s),+ build_cash_activity(id: "int_cash_out", type: "INTERNAL_CASH_TRANSFER_OUT", amount: 100.00, settlement_date: Date.current.to_s)+ )++ inbound = snaptrade_entry("int_cash_in")+ outbound = snaptrade_entry("int_cash_out")++ assert_not_nil inbound+ assert_not_nil outbound+ assert_equal(-250.00, inbound.amount.to_f)+ assert_equal "Transfer", inbound.entryable.investment_activity_label+ assert_equal 100.00, outbound.amount.to_f+ assert_equal "Transfer", outbound.entryable.investment_activity_label+ end++ test "processes cash ADJUSTMENT by inverting provider sign" do+ process_activities(+ build_cash_activity(id: "adj_credit", type: "ADJUSTMENT", amount: 15.00, settlement_date: Date.current.to_s),+ build_cash_activity(id: "adj_debit", type: "ADJUSTMENT", amount: -20.00, settlement_date: Date.current.to_s)+ )++ credit = snaptrade_entry("adj_credit")+ debit = snaptrade_entry("adj_debit")++ assert_not_nil credit+ assert_not_nil debit+ assert_equal(-15.00, credit.amount.to_f, "credit adjustment must be stored as negative inflow")+ assert_equal 20.00, debit.amount.to_f, "debit adjustment must be stored as positive outflow"+ end++ test "processes forward SPLIT as zero-cash trade adding shares (LAB-156)" do+ process_activities(+ build_trade_activity(+ id: "split_001",+ type: "SPLIT",+ symbol: "VGT",+ units: 3010.014,+ price: 0.0,+ amount: 304071.61 # SnapTrade notional value must NOT become a cash withdrawal+ )+ )++ entry = snaptrade_entry("split_001")+ assert_not_nil entry+ assert entry.entryable.is_a?(Trade), "SPLIT must be imported as a Trade, not a cash Transaction"+ trade = entry.entryable+ assert_equal BigDecimal("3010.014"), trade.qty+ assert_equal BigDecimal("0"), trade.price+ assert_equal BigDecimal("0"), entry.amount, "cash impact must be zero to prevent balance distortion"+ assert_equal "Other", trade.investment_activity_label+ end++ test "processes REVERSE_SPLIT as zero-cash trade with negative quantity" do+ process_activities(+ build_trade_activity(+ id: "rev_split_001",+ type: "REVERSE_SPLIT",+ symbol: "SOXL",+ units: 50.0,+ price: 0.0+ )+ )++ entry = snaptrade_entry("rev_split_001")+ assert_not_nil entry+ assert entry.entryable.is_a?(Trade)+ trade = entry.entryable+ assert_equal BigDecimal("-50.0"), trade.qty+ assert_equal BigDecimal("0"), trade.price+ assert_equal BigDecimal("0"), entry.amount+ assert_equal "Other", trade.investment_activity_label+ end++ test "processes STOCK_DIVIDEND as zero-cash trade with Dividend label" do+ process_activities(+ build_trade_activity(+ id: "stock_div_001",+ type: "STOCK_DIVIDEND",+ symbol: "AAPL",+ units: 2.0,+ price: 0.0+ )+ )++ entry = snaptrade_entry("stock_div_001")+ assert_not_nil entry+ assert entry.entryable.is_a?(Trade)+ trade = entry.entryable+ assert_equal BigDecimal("2.0"), trade.qty+ assert_equal BigDecimal("0"), trade.price+ assert_equal BigDecimal("0"), entry.amount+ assert_equal "Dividend", trade.investment_activity_label+ end++ test "processes SPINOFF as zero-cash trade" do+ process_activities(+ build_trade_activity(+ id: "spinoff_001",+ type: "SPINOFF",+ symbol: "GEV",+ units: 10.0,+ price: 0.0+ )+ )++ entry = snaptrade_entry("spinoff_001")+ assert_not_nil entry+ assert entry.entryable.is_a?(Trade)+ trade = entry.entryable+ assert_equal BigDecimal("10.0"), trade.qty+ assert_equal BigDecimal("0"), trade.price+ assert_equal BigDecimal("0"), entry.amount+ assert_equal "Other", trade.investment_activity_label+ end++ test "processes OPTIONASSIGNMENT and OPTIONEXERCISE as trades" do+ process_activities(+ build_trade_activity(+ id: "opt_assign_001",+ type: "OPTIONASSIGNMENT",+ symbol: "AAPL",+ units: 1.0,+ price: 150.0+ ),+ build_trade_activity(+ id: "opt_exercise_001",+ type: "OPTIONEXERCISE",+ symbol: "MSFT",+ units: 1.0,+ price: 300.0+ ),+ build_trade_activity(+ id: "opt_exercise_close",+ type: "OPTIONEXERCISE",+ symbol: "QQQ",+ units: -1.0,+ price: 0.0,+ amount: -23805.0 # Exercise cash outlay+ )+ )++ assign = snaptrade_entry("opt_assign_001")+ exercise = snaptrade_entry("opt_exercise_001")+ exercise_close = snaptrade_entry("opt_exercise_close")++ assert_not_nil assign+ assert_not_nil exercise+ assert_not_nil exercise_close+ assert assign.entryable.is_a?(Trade)+ assert exercise.entryable.is_a?(Trade)+ assert exercise_close.entryable.is_a?(Trade)+ assert_equal BigDecimal("-1.0"), assign.entryable.qty, "assignment should be sell-side (negative)"+ assert_equal BigDecimal("1.0"), exercise.entryable.qty+ assert_equal BigDecimal("-1.0"), exercise_close.entryable.qty, "negative units removes exercised contract"+ end++ test "processes EXTERNAL_ASSET_TRANSFER_IN and OUT as zero-cash asset trades" do+ process_activities(+ build_trade_activity(+ id: "acat_in_001",+ type: "EXTERNAL_ASSET_TRANSFER_IN",+ symbol: "VTI",+ units: 100.0,+ price: 220.0+ ),+ build_trade_activity(+ id: "acat_out_001",+ type: "EXTERNAL_ASSET_TRANSFER_OUT",+ symbol: "VXUS",+ units: 50.0,+ price: 60.0+ )+ )++ inbound = snaptrade_entry("acat_in_001")+ outbound = snaptrade_entry("acat_out_001")++ assert_not_nil inbound+ assert_not_nil outbound+ assert inbound.entryable.is_a?(Trade)+ assert outbound.entryable.is_a?(Trade)+ assert_equal BigDecimal("100.0"), inbound.entryable.qty+ assert_equal BigDecimal("-50.0"), outbound.entryable.qty+ assert_equal BigDecimal("0"), inbound.amount, "ACAT transfer in has zero cash flow"+ assert_equal BigDecimal("0"), outbound.amount, "ACAT transfer out has zero cash flow"+ assert_equal "Transfer", inbound.entryable.investment_activity_label+ assert_equal "Transfer", outbound.entryable.investment_activity_label+ end++ test "processes INTERNAL_ASSET_TRANSFER_IN and OUT as zero-cash asset trades" do+ process_activities(+ build_trade_activity(+ id: "int_asset_in_001",+ type: "INTERNAL_ASSET_TRANSFER_IN",+ symbol: "VTI",+ units: 25.0,+ price: 220.0+ ),+ build_trade_activity(+ id: "int_asset_out_001",+ type: "INTERNAL_ASSET_TRANSFER_OUT",+ symbol: "VXUS",+ units: 15.0,+ price: 60.0+ )+ )++ inbound = snaptrade_entry("int_asset_in_001")+ outbound = snaptrade_entry("int_asset_out_001")++ assert_not_nil inbound+ assert_not_nil outbound+ assert inbound.entryable.is_a?(Trade)+ assert outbound.entryable.is_a?(Trade)+ assert_equal BigDecimal("25.0"), inbound.entryable.qty+ assert_equal BigDecimal("-15.0"), outbound.entryable.qty+ assert_equal BigDecimal("0"), inbound.amount, "internal asset transfer in has zero cash flow"+ assert_equal BigDecimal("0"), outbound.amount, "internal asset transfer out has zero cash flow"+ assert_equal "Transfer", inbound.entryable.investment_activity_label+ assert_equal "Transfer", outbound.entryable.investment_activity_label+ end++ test "processes STOCK_MERGER as zero-cash trade preserving signed units" do+ process_activities(+ build_trade_activity(+ id: "merger_retire",+ type: "STOCK_MERGER",+ symbol: "AAPL",+ units: -100.0,+ price: nil,+ amount: nil+ ),+ build_trade_activity(+ id: "merger_receive",+ type: "STOCK_MERGER",+ symbol: "MSFT",+ units: 50.0,+ price: nil,+ amount: nil+ )+ )++ retire = snaptrade_entry("merger_retire")+ receive = snaptrade_entry("merger_receive")++ assert_not_nil retire+ assert_not_nil receive+ assert retire.entryable.is_a?(Trade)+ assert receive.entryable.is_a?(Trade)+ assert_equal BigDecimal("-100.0"), retire.entryable.qty, "negative units must retire acquired shares"+ assert_equal BigDecimal("50.0"), receive.entryable.qty, "positive units must add new shares"+ assert_equal BigDecimal("0"), retire.amount+ assert_equal BigDecimal("0"), receive.amount+ assert_equal "Other", retire.entryable.investment_activity_label+ assert_equal "Other", receive.entryable.investment_activity_label+ end++ test "processes STOCK_MERGER with cash-in-lieu or cash consideration preserving cash amount" do+ process_activities(+ build_trade_activity(+ id: "merger_cash_in_lieu",+ type: "STOCK_MERGER",+ symbol: "AAPL",+ units: -0.5,+ price: nil,+ amount: 45.00+ )+ )++ entry = snaptrade_entry("merger_cash_in_lieu")+ assert_not_nil entry+ assert entry.entryable.is_a?(Trade)+ assert_equal BigDecimal("-0.5"), entry.entryable.qty+ assert_equal BigDecimal("-45.00"), entry.amount, "cash-in-lieu proceeds must be negative entry amount (money in)"+ assert_equal BigDecimal("90.00"), entry.entryable.price+ end++ test "processes STOCK_MERGER with cash-only payload as a Transaction" do+ process_activities(+ build_cash_activity(+ id: "merger_cash_only",+ type: "STOCK_MERGER",+ amount: 150.00,+ settlement_date: Date.current.to_s+ )+ )++ entry = snaptrade_entry("merger_cash_only")+ assert_not_nil entry+ assert entry.entryable.is_a?(Transaction)+ assert_equal BigDecimal("-150.00"), entry.amount, "cash merger payout must be negative entry amount (money in)"+ assert_equal "Other", entry.entryable.investment_activity_label+ end++ test "reclassifies pre-existing Transaction entry to Trade on resync without type collision error" do+ # Simulate an account that previously synced a SPLIT activity under the old cash-only code+ stale_entry = @account.entries.create!(+ external_id: "split_reclass_test",+ source: "snaptrade",+ amount: 304071.61,+ currency: "USD",+ date: Date.current,+ name: "DISTRIBUTION VANGUARD WORLD FD INF TECH ETF (VGT)",+ entryable: Transaction.new(investment_activity_label: "Other")+ )+ stale_entry_id = stale_entry.id++ assert stale_entry.entryable.is_a?(Transaction)++ # Re-sync with the updated ActivitiesProcessor+ process_activities(+ build_trade_activity(+ id: "split_reclass_test",+ type: "SPLIT",+ symbol: "VGT",+ units: 3010.014,+ price: 0.0,+ amount: 304071.61+ )+ )++ reclassified_entry = snaptrade_entry("split_reclass_test")+ assert_not_nil reclassified_entry+ assert_not_equal stale_entry_id, reclassified_entry.id, "stale entry should be replaced with reclassified entry"+ assert reclassified_entry.entryable.is_a?(Trade), "entry must be reclassified to Trade"+ assert_equal BigDecimal("3010.014"), reclassified_entry.entryable.qty+ assert_equal BigDecimal("0.0"), reclassified_entry.amount, "split trade must have zero cash amount"+ assert_equal BigDecimal("0.0"), reclassified_entry.entryable.price+ assert_nil snaptrade_debug_log("split_reclass_test", level: "error")+ end++ test "reclassifies pre-existing Trade entry to Transaction on resync without type collision error" do+ # Simulate an entry previously imported as a Trade (e.g. stock merger with units)+ security = Security.find_or_create_by!(ticker: "AAPL") { |s| s.name = "Apple Inc"; s.currency = "USD" }+ stale_entry = @account.entries.create!(+ external_id: "merger_reclass_test",+ source: "snaptrade",+ amount: 0.0,+ currency: "USD",+ date: Date.current,+ name: "STOCK_MERGER AAPL",+ entryable: Trade.new(security: security, qty: 10, price: 150.0, currency: "USD")+ )+ stale_entry_id = stale_entry.id++ assert stale_entry.entryable.is_a?(Trade)++ # Re-sync as a cash-only merger payout (0 units)+ process_activities(+ build_cash_activity(+ id: "merger_reclass_test",+ type: "STOCK_MERGER",+ amount: 500.00,+ settlement_date: Date.current.to_s+ )+ )++ reclassified_entry = snaptrade_entry("merger_reclass_test")+ assert_not_nil reclassified_entry+ assert_not_equal stale_entry_id, reclassified_entry.id, "stale trade entry should be replaced"+ assert reclassified_entry.entryable.is_a?(Transaction), "entry must be reclassified to Transaction"+ assert_equal BigDecimal("-500.00"), reclassified_entry.amount+ assert_nil snaptrade_debug_log("merger_reclass_test", level: "error")+ end++ test "skips reclassification and preserves existing entry when import_locked" do+ # When user manually locked an entry, resync must not destroy or reclassify it+ locked_entry = @account.entries.create!(+ external_id: "locked_split_test",+ source: "snaptrade",+ amount: 0.0,+ currency: "USD",+ date: Date.current,+ name: "DISTRIBUTION VGT (Manual Lock)",+ import_locked: true,+ entryable: Transaction.new(investment_activity_label: "Other")+ )+ original_id = locked_entry.id++ process_activities(+ build_trade_activity(+ id: "locked_split_test",+ type: "SPLIT",+ symbol: "VGT",+ units: 3010.014,+ price: 0.0,+ amount: 304071.61+ )+ )++ entry = snaptrade_entry("locked_split_test")+ assert_not_nil entry+ assert_equal original_id, entry.id, "locked entry ID must be preserved"+ assert entry.entryable.is_a?(Transaction), "locked entry must not be converted to Trade"+ assert_equal BigDecimal("0.0"), entry.amount++ log = snaptrade_debug_log("locked_split_test", level: "warn")+ assert_not_nil log+ assert_includes log.message, "Skipping reclassification of protected entry"+ end++ test "skips reclassification and preserves existing entry when user_modified" do+ user_entry = @account.entries.create!(+ external_id: "user_modified_split_test",+ source: "snaptrade",+ amount: 0.0,+ currency: "USD",+ date: Date.current,+ name: "Edited Split Entry",+ user_modified: true,+ entryable: Transaction.new(investment_activity_label: "Other")+ )+ original_id = user_entry.id++ process_activities(+ build_trade_activity(+ id: "user_modified_split_test",+ type: "SPLIT",+ symbol: "VGT",+ units: 100.0,+ price: 0.0,+ amount: 10000.00+ )+ )++ entry = snaptrade_entry("user_modified_split_test")+ assert_not_nil entry+ assert_equal original_id, entry.id, "user-modified entry ID must be preserved"+ assert entry.entryable.is_a?(Transaction), "user-modified entry must not be converted"++ log = snaptrade_debug_log("user_modified_split_test", level: "warn")+ assert_not_nil log+ assert_includes log.message, "Skipping reclassification of protected entry"+ end++ test "processes zero-amount trade types when price and amount are omitted" do+ process_activities(+ build_trade_activity(+ id: "opt_exp_long",+ type: "OPTIONEXPIRATION",+ symbol: "AAPL",+ units: -1.0,+ price: nil,+ amount: nil+ ),+ build_trade_activity(+ id: "opt_exp_short",+ type: "OPTIONEXPIRATION",+ symbol: "MSFT",+ units: 1.0,+ price: nil,+ amount: nil+ )+ )++ long_entry = snaptrade_entry("opt_exp_long")+ short_entry = snaptrade_entry("opt_exp_short")++ assert_not_nil long_entry, "zero-amount trade types must import even when price and amount are omitted"+ assert_not_nil short_entry+ assert long_entry.entryable.is_a?(Trade)+ assert short_entry.entryable.is_a?(Trade)+ assert_equal BigDecimal("-1.0"), long_entry.entryable.qty, "negative units must close out expired long contract"+ assert_equal BigDecimal("1.0"), short_entry.entryable.qty, "positive units must close out expired short contract"+ assert_equal BigDecimal("0"), long_entry.entryable.price+ assert_equal BigDecimal("0"), short_entry.entryable.price+ assert_equal BigDecimal("0"), long_entry.amount+ assert_equal BigDecimal("0"), short_entry.amount+ assert_equal "Other", long_entry.entryable.investment_activity_label+ assert_equal "Other", short_entry.entryable.investment_activity_label+ end++ test "processes share ADJUSTMENT as a zero-cash trade preserving signed units" do+ process_activities(+ build_trade_activity(+ id: "share_adj_add",+ type: "ADJUSTMENT",+ symbol: "VTI",+ units: 5.0,+ price: nil,+ amount: 500.00 # Notional value must not become cash flow+ ),+ build_trade_activity(+ id: "share_adj_remove",+ type: "ADJUSTMENT",+ symbol: "VTI",+ units: -3.0,+ price: nil,+ amount: -300.00+ )+ )++ add_entry = snaptrade_entry("share_adj_add")+ remove_entry = snaptrade_entry("share_adj_remove")++ assert_not_nil add_entry+ assert_not_nil remove_entry+ assert add_entry.entryable.is_a?(Trade), "ADJUSTMENT with units must be imported as a Trade"+ assert remove_entry.entryable.is_a?(Trade)++ assert_equal BigDecimal("5.0"), add_entry.entryable.qty, "positive units represents shares added"+ assert_equal BigDecimal("-3.0"), remove_entry.entryable.qty, "negative units represents shares removed"+ assert_equal BigDecimal("0"), add_entry.entryable.price+ assert_equal BigDecimal("0"), remove_entry.entryable.price+ assert_equal BigDecimal("0"), add_entry.amount+ assert_equal BigDecimal("0"), remove_entry.amount+ assert_equal "Other", add_entry.entryable.investment_activity_label+ assert_equal "Other", remove_entry.entryable.investment_activity_label+ end++ test "ignores cash activities with zero or nil amount" do+ assert_no_difference -> { @account.entries.count } do+ process_activities(+ build_cash_activity(id: "zero_cash_001", type: "DIVIDEND", amount: 0.0, settlement_date: Date.current.to_s),+ build_cash_activity(id: "nil_cash_001", type: "CONTRIBUTION", amount: nil, settlement_date: Date.current.to_s)+ )endenddiff --git a/test/models/snaptrade_account_processor_test.rb b/test/models/snaptrade_account_processor_test.rbindex b87cb781af..abb3e17311 100644--- a/test/models/snaptrade_account_processor_test.rb+++ b/test/models/snaptrade_account_processor_test.rb@@ -396,6 +396,68 @@ class SnaptradeAccountProcessorTest < ActiveSupport::TestCaseassert_equal 1, entries.countend+ test "activities processor handles SPLIT as zero-amount trade" do+ security = securities(:aapl)++ @snaptrade_account.update!(+ raw_activities_payload: [+ {+ "id" => "activity_split_1",+ "type" => "SPLIT",+ "symbol" => { "symbol" => security.ticker, "description" => security.name },+ "units" => "10",+ "price" => "150.00",+ "amount" => "1500.00",+ "settlement_date" => Date.current.to_s,+ "currency" => "USD"+ }+ ]+ )++ processor = SnaptradeAccount::ActivitiesProcessor.new(@snaptrade_account)+ result = processor.process++ assert_equal 1, result[:trades]+ trade_entry = @account.entries.find_by(external_id: "activity_split_1")+ assert_not_nil trade_entry+ assert_equal "Other", trade_entry.entryable.investment_activity_label+ assert_equal 10, trade_entry.entryable.qty+ assert_equal 0.0, trade_entry.amount.to_f+ end++ test "activities processor handles internal cash transfers with proper inflow and outflow signs" do+ @snaptrade_account.update!(+ raw_activities_payload: [+ {+ "id" => "activity_xfer_in_1",+ "type" => "INTERNAL_CASH_TRANSFER_IN",+ "amount" => "500.00",+ "settlement_date" => Date.current.to_s,+ "currency" => "USD"+ },+ {+ "id" => "activity_xfer_out_1",+ "type" => "INTERNAL_CASH_TRANSFER_OUT",+ "amount" => "200.00",+ "settlement_date" => Date.current.to_s,+ "currency" => "USD"+ }+ ]+ )++ processor = SnaptradeAccount::ActivitiesProcessor.new(@snaptrade_account)+ result = processor.process++ assert_equal 2, result[:transactions]+ in_entry = @account.entries.find_by(external_id: "activity_xfer_in_1")+ assert_equal "Transfer", in_entry.entryable.investment_activity_label+ assert_equal(-500.00, in_entry.amount.to_f)++ out_entry = @account.entries.find_by(external_id: "activity_xfer_out_1")+ assert_equal "Transfer", out_entry.entryable.investment_activity_label+ assert_equal 200.00, out_entry.amount.to_f+ end+# === Multi-currency cash (issue #1809) ===test "upsert_balances! persists all entries and keeps the primary currency in cash_balance" do@@ -618,4 +680,115 @@ class SnaptradeAccountProcessorTest < ActiveSupport::TestCaseeur_cash = @account.holdings.joins(:security).where(securities: { kind: "cash" }, currency: "EUR")assert_equal 1, eur_cash.select(:external_id).distinct.countend++ # === Full Account Sync Integration ===++ test "processor synchronizes balances, holdings, and activities end-to-end without cash distortion" do+ security = securities(:aapl)++ @snaptrade_account.update!(+ currency: "USD",+ current_balance: BigDecimal("5000.00"),+ cash_balance: BigDecimal("1500.00"),+ raw_holdings_payload: [+ {+ "symbol" => {+ "symbol" => { "symbol" => security.ticker, "description" => security.name }+ },+ "units" => "20",+ "price" => "175.00",+ "currency" => "USD"+ }+ ],+ raw_activities_payload: [+ {+ "id" => "activity_full_split",+ "type" => "SPLIT",+ "symbol" => { "symbol" => security.ticker },+ "units" => "10",+ "price" => "175.00",+ "amount" => "1750.00",+ "settlement_date" => Date.current.to_s,+ "currency" => "USD"+ },+ {+ "id" => "activity_full_xfer",+ "type" => "INTERNAL_CASH_TRANSFER_IN",+ "amount" => "300.00",+ "settlement_date" => Date.current.to_s,+ "currency" => "USD"+ }+ ]+ )++ result = SnaptradeAccount::Processor.new(@snaptrade_account).process++ assert result[:holdings_processed]+ assert result[:activities_processed]++ @account.reload+ assert_equal BigDecimal("5000.00"), @account.balance+ assert_equal BigDecimal("1500.00"), @account.cash_balance++ holding = @account.holdings.find_by(security: security)+ assert_not_nil holding+ assert_equal BigDecimal("20"), holding.qty++ split_entry = @account.entries.find_by(external_id: "activity_full_split")+ assert_not_nil split_entry+ assert_equal "Other", split_entry.entryable.investment_activity_label+ assert_equal 0.0, split_entry.amount.to_f++ xfer_entry = @account.entries.find_by(external_id: "activity_full_xfer")+ assert_not_nil xfer_entry+ assert_equal "Transfer", xfer_entry.entryable.investment_activity_label+ assert_equal(-300.00, xfer_entry.amount.to_f)+ end++ test "processor cleanly reclassifies pre-existing activity from transaction to trade on resync" do+ security = securities(:aapl)++ # 1. Simulate an entry already synced as a Transaction under old code+ stale_entry = @account.entries.create!(+ external_id: "resync_split_001",+ source: "snaptrade",+ amount: 1750.00,+ currency: "USD",+ date: Date.current,+ name: "DISTRIBUTION AAPL",+ entryable: Transaction.new(investment_activity_label: "Other")+ )+ stale_id = stale_entry.id++ # 2. Configure raw payloads for the next sync+ @snaptrade_account.update!(+ raw_holdings_payload: [],+ raw_activities_payload: [+ {+ "id" => "resync_split_001",+ "type" => "SPLIT",+ "symbol" => { "symbol" => security.ticker },+ "units" => "10",+ "price" => "0.0",+ "amount" => "1750.00",+ "settlement_date" => Date.current.to_s,+ "currency" => "USD"+ }+ ]+ )++ # 3. Run full account processor+ result = SnaptradeAccount::Processor.new(@snaptrade_account).process++ assert result[:activities_processed]++ # 4. Verify stale entry was reclassified to Trade+ entry = @account.entries.find_by(external_id: "resync_split_001", source: "snaptrade")+ assert_not_nil entry+ assert_not_equal stale_id, entry.id+ assert entry.entryable.is_a?(Trade)+ assert_equal BigDecimal("10"), entry.entryable.qty+ assert_equal 0.0, entry.amount.to_f+ assert_equal 0.0, entry.entryable.price.to_f+ endend