require "test_helper" class IbkrItemReportParserTest < ActiveSupport::TestCase test "parses accounts, balances, and positions from flex xml" do parsed = IbkrItem::ReportParser.new(file_fixture("ibkr/flex_statement.xml").read).parse assert_equal "Sure Test", parsed[:metadata]["query_name"] assert_equal 2, parsed[:accounts].size first_account = parsed[:accounts].first assert_equal "U1234567", first_account[:ibkr_account_id] assert_equal "CHF", first_account[:currency] assert_equal BigDecimal("1000.50"), first_account[:cash_balance] assert_equal BigDecimal("3351.00"), first_account[:current_balance] assert_equal 2, first_account[:equity_summary_in_base].size assert_equal 1, first_account[:open_positions].size assert_equal 2, first_account[:trades].size assert_equal 2, first_account[:cash_transactions].size second_account = parsed[:accounts].second assert_equal "U7654321", second_account[:ibkr_account_id] assert_equal BigDecimal("250"), second_account[:cash_balance] assert_equal BigDecimal("250"), second_account[:current_balance] assert_equal 1, second_account[:equity_summary_in_base].size end test "raises parse error for malformed xml" do error = assert_raises(IbkrItem::ReportParser::ParseError) do IbkrItem::ReportParser.new("").parse end assert_match "Invalid IBKR Flex XML", error.message end test "raises parse error when flex statements are missing" do error = assert_raises(IbkrItem::ReportParser::ParseError) do IbkrItem::ReportParser.new('').parse end assert_equal "Invalid IBKR Flex XML: no FlexStatement nodes found.", error.message end test "raises parse error when flex statement account id is missing" do xml = <<~XML XML error = assert_raises(IbkrItem::ReportParser::ParseError) do IbkrItem::ReportParser.new(xml).parse end assert_equal "Invalid IBKR Flex XML: missing account identifier in FlexStatement.", error.message end end