Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
18.1 KB
Raw Blame
require "test_helper"

class SnaptradeDataHelpersTest < ActiveSupport::TestCase
  # Create a test class that includes the concern
  class TestHelper
    include SnaptradeAccount::DataHelpers

    # Expose private methods for testing
    def test_parse_decimal(value)
      parse_decimal(value)
    end

    def test_parse_date(value)
      parse_date(value)
    end

    def test_resolve_security(symbol, symbol_data, account: nil, account_provider_id: nil, provider_row: false)
      resolve_security(symbol, symbol_data, account: account, account_provider_id: account_provider_id, provider_row: provider_row)
    end

    def test_extract_currency(data, symbol_data = {}, fallback = nil)
      extract_currency(data, symbol_data, fallback)
    end
  end

  setup do
    @helper = TestHelper.new
  end

  # === parse_decimal tests ===

  test "parse_decimal handles BigDecimal" do
    result = @helper.test_parse_decimal(BigDecimal("123.45"))
    assert_equal BigDecimal("123.45"), result
  end

  test "parse_decimal handles String" do
    result = @helper.test_parse_decimal("456.78")
    assert_equal BigDecimal("456.78"), result
  end

  test "parse_decimal handles Integer" do
    result = @helper.test_parse_decimal(100)
    assert_equal BigDecimal("100"), result
  end

  test "parse_decimal handles Float" do
    result = @helper.test_parse_decimal(99.99)
    assert_equal BigDecimal("99.99"), result
  end

  test "parse_decimal returns nil for nil input" do
    result = @helper.test_parse_decimal(nil)
    assert_nil result
  end

  test "parse_decimal returns nil for invalid string" do
    result = @helper.test_parse_decimal("not_a_number")
    assert_nil result
  end

  # === parse_date tests ===

  test "parse_date handles Date object" do
    date = Date.new(2024, 6, 15)
    result = @helper.test_parse_date(date)
    assert_equal date, result
  end

  test "parse_date handles ISO string" do
    result = @helper.test_parse_date("2024-06-15")
    assert_equal Date.new(2024, 6, 15), result
  end

  test "parse_date handles Time object" do
    time = Time.zone.parse("2024-06-15 10:30:00")
    result = @helper.test_parse_date(time)
    assert_equal Date.new(2024, 6, 15), result
  end

  test "parse_date handles DateTime" do
    dt = DateTime.new(2024, 6, 15, 10, 30)
    result = @helper.test_parse_date(dt)
    # DateTime is a subclass of Date, so it matches Date branch and returns as-is
    # which is acceptable behavior - the result is still usable as a date
    assert result.respond_to?(:year)
    assert_equal 2024, result.year
    assert_equal 6, result.month
    assert_equal 15, result.day
  end

  test "parse_date returns nil for nil input" do
    result = @helper.test_parse_date(nil)
    assert_nil result
  end

  test "parse_date returns nil for invalid string" do
    result = @helper.test_parse_date("invalid_date")
    assert_nil result
  end

  # === resolve_security tests ===

  test "resolve_security finds existing security by ticker" do
    existing = Security.create!(ticker: "TEST", name: "Test Company")

    result = @helper.test_resolve_security("TEST", {})
    assert_equal existing, result
  end

  test "resolve_security creates new security when not found" do
    symbol_data = { "description" => "New Corp Inc" }

    result = @helper.test_resolve_security("NEWCORP", symbol_data)

    assert_not_nil result
    assert_equal "NEWCORP", result.ticker
    assert_equal "New Corp Inc", result.name
  end

  test "resolve_security uppercases ticker" do
    symbol_data = { "description" => "Lowercase Test" }

    result = @helper.test_resolve_security("lowercase", symbol_data)

    assert_equal "LOWERCASE", result.ticker
  end

  test "resolve_security returns nil for blank ticker" do
    result = @helper.test_resolve_security("", {})
    assert_nil result

    result = @helper.test_resolve_security(nil, {})
    assert_nil result
  end

  test "resolve_security handles race condition on creation" do
    # Simulate race condition by creating after first check
    symbol_data = { "description" => "Race Condition Test" }

    # Create the security before resolve_security can
    Security.create!(ticker: "RACECOND", name: "Already Created")

    # Should return existing instead of raising
    result = @helper.test_resolve_security("RACECOND", symbol_data)
    assert_equal "RACECOND", result.ticker
  end

  test "resolve_security prefers the row on the exchange SnapTrade reports, whichever was created first" do
    xnys_first = Security.create!(ticker: "DUPA", name: "Dup A", exchange_operating_mic: "XNYS")
    Security.create!(ticker: "DUPA", name: "Dup A")
    Security.create!(ticker: "DUPB", name: "Dup B")
    xnys_last = Security.create!(ticker: "DUPB", name: "Dup B", exchange_operating_mic: "XNYS")

    symbol_data = { "exchange" => { "mic_code" => "XNYS" } }

    assert_equal xnys_first, @helper.test_resolve_security("DUPA", symbol_data)
    assert_equal xnys_last, @helper.test_resolve_security("DUPB", symbol_data)
  end

  test "resolve_security without an exchange prefers an online priced row over an older unpriced one" do
    Security.create!(ticker: "DUPC", name: "Dup C", exchange_operating_mic: "XASX", offline: true)
    Security.create!(ticker: "DUPC", name: "Dup C", exchange_operating_mic: "XNAS")
    priced = Security.create!(ticker: "DUPC", name: "Dup C", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")

    assert_equal priced, @helper.test_resolve_security("DUPC", {})
  end

  test "resolve_security leaves a different ticker alone when another ticker has duplicates" do
    Security.create!(ticker: "DUPD", name: "Dup D")
    Security.create!(ticker: "DUPD", name: "Dup D", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    other = Security.create!(ticker: "SOLO", name: "Solo")

    assert_equal other, @helper.test_resolve_security("SOLO", { "exchange" => { "mic_code" => "XNYS" } })
  end

  test "resolve_security race retry picks the same row as the ordered lookup" do
    priced = nil
    Security.define_singleton_method(:create!) do |*|
      # Another process creates both rows between the lookup and the create.
      Security.new(ticker: "RACEDUP", name: "Unpriced").save!
      priced = Security.new(ticker: "RACEDUP", name: "Priced", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance").tap(&:save!)
      raise ActiveRecord::RecordNotUnique
    end

    result = @helper.test_resolve_security("RACEDUP", {})

    assert_not_nil priced
    assert_equal priced, result
  ensure
    Security.singleton_class.send(:remove_method, :create!)
  end

  test "resolve_security race retry still prefers the row on the reported exchange" do
    reported = nil
    Security.define_singleton_method(:create!) do |*|
      Security.new(ticker: "RACEMIC", name: "Priced", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance").save!
      reported = Security.new(ticker: "RACEMIC", name: "Reported", exchange_operating_mic: "XNAS").tap(&:save!)
      raise ActiveRecord::RecordNotUnique
    end

    result = @helper.test_resolve_security("RACEMIC", { "exchange" => { "mic_code" => "XNAS" } })

    assert_not_nil reported
    assert_equal reported, result
  ensure
    Security.singleton_class.send(:remove_method, :create!)
  end

  test "resolve_security matches a row SnapTrade created on the reported exchange" do
    Security.create!(ticker: "DUPS", name: "Other exchange", exchange_operating_mic: "XASX", price_provider: "yahoo_finance")
    snaptrade_row = Security.create!(ticker: "DUPS", name: "SnapTrade row", exchange_mic: "XNYS")

    assert_equal snaptrade_row, @helper.test_resolve_security("DUPS", { "exchange" => { "mic_code" => "XNYS" } })
  end

  test "resolve_security follows the account's latest holding when its position already moved rows" do
    account = accounts(:investment)
    earlier = Security.create!(ticker: "DUPL", name: "Earlier row", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    latest = Security.create!(ticker: "DUPL", name: "Latest row")
    hold(account, earlier, date: 2.days.ago.to_date)
    hold(account, latest, date: 1.day.ago.to_date)

    assert_equal latest, @helper.test_resolve_security("DUPL", {}, account: account)
  end

  test "resolve_security keeps a holding the user remapped to another row of the same ticker" do
    account = accounts(:investment)
    provider_row = Security.create!(ticker: "DUPR", name: "Provider row")
    remapped_to = Security.create!(ticker: "DUPR", name: "Remapped row", exchange_operating_mic: "XNYS")
    hold(account, remapped_to, date: 1.day.ago.to_date, provider_security: provider_row)

    assert_equal remapped_to, @helper.test_resolve_security("DUPR", {}, account: account)
  end

  # Holdings ask for the provider's row of a locked remap, so the import
  # adapter can carry the lock to the next day's row; trades keep the row
  # the user chose, where the remap moved the position's trades.
  test "resolve_security answers a locked remap with the provider's row only when asked" do
    account = accounts(:investment)
    provider_row = Security.create!(ticker: "DUPK", name: "Provider row")
    remapped_to = Security.create!(ticker: "DUPK", name: "Remapped row", exchange_operating_mic: "XNYS")
    hold(account, remapped_to, date: 1.day.ago.to_date, provider_security: provider_row).update!(security_locked: true)

    assert_equal remapped_to, @helper.test_resolve_security("DUPK", {}, account: account)
    assert_equal provider_row, @helper.test_resolve_security("DUPK", {}, account: account, provider_row: true)
  end

  # Only a lock says the user chose the row. An unlocked holding that still
  # names another provider row is followed where it sits.
  test "resolve_security follows an unlocked holding even when asked for the provider's row" do
    account = accounts(:investment)
    provider_row = Security.create!(ticker: "DUPU", name: "Provider row")
    held = Security.create!(ticker: "DUPU", name: "Held row", exchange_operating_mic: "XNYS")
    hold(account, held, date: 1.day.ago.to_date, provider_security: provider_row)

    assert_equal held, @helper.test_resolve_security("DUPU", {}, account: account, provider_row: true)
  end

  test "resolve_security race retry still answers a locked remap with the provider's row" do
    account = accounts(:investment)
    provider_row = nil
    Security.define_singleton_method(:create!) do |*|
      # Another sync creates the rows and the user's remap between the lookup and the create.
      provider_row = Security.new(ticker: "RACEK", name: "Provider row").tap(&:save!)
      remapped_to = Security.new(ticker: "RACEK", name: "Remapped row", exchange_operating_mic: "XNYS").tap(&:save!)
      account.holdings.create!(security: remapped_to, provider_security: provider_row, security_locked: true,
                               date: 1.day.ago.to_date, qty: 1, price: 100, amount: 100, currency: "USD")
      raise ActiveRecord::RecordNotUnique
    end

    result = @helper.test_resolve_security("RACEK", {}, account: account, provider_row: true)

    assert_not_nil provider_row
    assert_equal provider_row, result
  ensure
    Security.singleton_class.send(:remove_method, :create!)
  end

  # Another provider linked to the same account can hold the same ticker on
  # another row. Without an external_id the import adapter matches holdings by
  # security, date and currency alone, so following that holding would make
  # SnapTrade overwrite it.
  test "resolve_security does not follow another provider's holding" do
    account = accounts(:investment)
    snaptrade = AccountProvider.create!(account: account, provider: snaptrade_accounts(:fidelity_401k))
    other = AccountProvider.create!(account: account, provider: other_provider_account)
    theirs = Security.create!(ticker: "DUPO", name: "Other provider's row", exchange_operating_mic: "XNAS")
    ours = Security.create!(ticker: "DUPO", name: "SnapTrade's row")
    hold(account, ours, date: 3.days.ago.to_date, account_provider: snaptrade)
    hold(account, theirs, date: 1.day.ago.to_date, account_provider: other)

    assert_equal ours, @helper.test_resolve_security("DUPO", {}, account: account, account_provider_id: snaptrade.id)
  end

  # Holdings materialized from the account's trades carry no provider, and a
  # remap there is the user's choice, so they still count.
  test "resolve_security still follows a holding with no provider" do
    account = accounts(:investment)
    snaptrade = AccountProvider.create!(account: account, provider: snaptrade_accounts(:fidelity_401k))
    Security.create!(ticker: "DUPM", name: "Priced row", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    materialized = Security.create!(ticker: "DUPM", name: "Materialized row")
    hold(account, materialized, date: 1.day.ago.to_date)

    assert_equal materialized, @helper.test_resolve_security("DUPM", {}, account: account, account_provider_id: snaptrade.id)
  end

  # The last tier has no identity signal: with no held position and no row on
  # SnapTrade's reported exchange, two online, priced rows for different
  # markets are told apart only by age. This documents that known limit, and
  # that the pick is recorded for support rather than made silently.
  test "resolve_security picks the older of two priced rows differing only by exchange, and logs it" do
    account = accounts(:investment)
    snaptrade = AccountProvider.create!(account: account, provider: snaptrade_accounts(:fidelity_401k))
    older = Security.create!(ticker: "DUPX", name: "Dup X", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    newer = Security.create!(ticker: "DUPX", name: "Dup X", exchange_operating_mic: "XNAS", price_provider: "yahoo_finance")
    # Rewriting the older row moves its tuple behind the newer one.
    older.update!(name: "Dup X (renamed)")

    assert_difference -> { DebugLogEntry.count }, 2 do
      2.times { assert_equal older, @helper.test_resolve_security("DUPX", {}, account: account, account_provider_id: snaptrade.id) }
    end

    entry = DebugLogEntry.order(:created_at).last
    assert_equal [ "provider_sync", "warn", "snaptrade", TestHelper.name ],
                 [ entry.category, entry.level, entry.provider_key, entry.source ]
    assert_equal [ account.family, account, snaptrade ], [ entry.family, entry.account, entry.account_provider ]
    assert_equal "DUPX", entry.metadata["ticker"]
    assert_equal older.id, entry.metadata["chosen_security_id"]
    assert_equal [ older.id, newer.id ], entry.metadata["candidates"].map { |c| c["id"] }
    assert_equal [ "XNYS", "XNAS" ], entry.metadata["candidates"].map { |c| c["exchange_operating_mic"] }
  end

  test "resolve_security logs a fallback pick when the reported exchange matches no row, and without an account" do
    older = Security.create!(ticker: "DUPU", name: "Dup U", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    Security.create!(ticker: "DUPU", name: "Dup U", exchange_operating_mic: "XNAS", price_provider: "yahoo_finance")

    assert_difference -> { DebugLogEntry.count }, 1 do
      assert_equal older, @helper.test_resolve_security("DUPU", { "exchange" => { "mic_code" => "XTSE" } })
    end

    entry = DebugLogEntry.order(:created_at).last
    assert_equal "XTSE", entry.metadata["reported_exchange"]
    assert_nil entry.family
    assert_nil entry.account_provider
  end

  test "resolve_security does not log when the ticker has a single row" do
    only = Security.create!(ticker: "ONLY", name: "Only row", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")

    assert_no_difference -> { DebugLogEntry.count } do
      assert_equal only, @helper.test_resolve_security("ONLY", {})
    end
  end

  test "resolve_security does not log when the held position decides" do
    account = accounts(:investment)
    Security.create!(ticker: "DUPHL", name: "Priced row", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    held = Security.create!(ticker: "DUPHL", name: "Held row", exchange_operating_mic: "XNAS", price_provider: "yahoo_finance")
    hold(account, held, date: 1.day.ago.to_date)

    assert_no_difference -> { DebugLogEntry.count } do
      assert_equal held, @helper.test_resolve_security("DUPHL", {}, account: account)
    end
  end

  test "resolve_security does not log when the reported exchange decides" do
    Security.create!(ticker: "DUPE", name: "Dup E", exchange_operating_mic: "XNYS", price_provider: "yahoo_finance")
    reported = Security.create!(ticker: "DUPE", name: "Dup E", exchange_operating_mic: "XNAS", price_provider: "yahoo_finance")

    assert_no_difference -> { DebugLogEntry.count } do
      assert_equal reported, @helper.test_resolve_security("DUPE", { "exchange" => { "mic_code" => "XNAS" } })
    end
  end

  # === extract_currency tests ===

  test "extract_currency handles hash with code key (symbol access)" do
    data = { currency: { code: "CAD" } }
    result = @helper.test_extract_currency(data)
    assert_equal "CAD", result
  end

  test "extract_currency handles hash with code key (string access)" do
    data = { "currency" => { "code" => "EUR" } }
    result = @helper.test_extract_currency(data)
    assert_equal "EUR", result
  end

  test "extract_currency handles string currency" do
    data = { currency: "GBP" }
    result = @helper.test_extract_currency(data)
    assert_equal "GBP", result
  end

  test "extract_currency falls back to symbol_data" do
    data = {}
    symbol_data = { currency: "JPY" }
    result = @helper.test_extract_currency(data, symbol_data)
    assert_equal "JPY", result
  end

  test "extract_currency uses fallback when no currency found" do
    data = {}
    result = @helper.test_extract_currency(data, {}, "USD")
    assert_equal "USD", result
  end

  test "extract_currency returns nil when no currency and no fallback" do
    data = {}
    result = @helper.test_extract_currency(data, {}, nil)
    assert_nil result
  end

  private

    def hold(account, security, date:, provider_security: nil, account_provider: nil)
      account.holdings.create!(
        security: security, provider_security: provider_security, date: date,
        qty: 1, price: 100, amount: 100, currency: "USD", account_provider: account_provider
      )
    end

    def other_provider_account
      item = families(:dylan_family).coinstats_items.create!(name: "CoinStats", api_key: "test-key")
      item.coinstats_accounts.create!(name: "Other provider", currency: "USD")
    end
end