#!/usr/bin/env python3
"""
Tests for SWIFT FIN Parser
"""

import pytest
from parsers.swift_fin_parser import SWIFTFINParser

@pytest.fixture
def parser():
    return SWIFTFINParser()

@pytest.fixture
def sample_mt103():
    return """{1:F01BANKUS33AXXX1234567890}
{2:O1031200240101BANKUS33AXXX1234567890123456789012345678901234567890}
{3:{108:MT103}}
{4:
:20:REF123
:23B:CRED
:32A:240101USD1000,00
:50A:/US1234567890
BANK US
:59:/US9876543210
CREDITOR NAME
:70:Payment for services
:71A:SHA
-}
{5:{MAC:12345678}{CHK:ABCDEF123456}{TNG:}{PDE:}}
{S:{COP:P}}"""

def test_parse_mt103(parser, sample_mt103):
    """Test parsing MT103 message"""
    result = parser.parse(sample_mt103)
    
    assert result['type'] == 'SWIFT_FIN'
    assert result['messageType'] == 'MT103'
    assert 'parsed' in result
    assert 'senderReference' in result['parsed']
    assert 'amount' in result['parsed']

def test_extract_message_type(parser, sample_mt103):
    """Test message type extraction"""
    lines = sample_mt103.strip().split('\n')
    app_header = parser._parse_block(lines, 2)
    message_type = parser._extract_message_type(app_header.get('raw', ''))
    assert message_type == 'MT103'

def test_parse_swift_fields(parser):
    """Test SWIFT field parsing"""
    text = ":20:REF123\n:32A:240101USD1000,00\n:50A:/US1234567890\nBANK US"
    fields = parser._parse_swift_fields(text)
    assert '20' in fields
    assert '32A' in fields
    assert '50A' in fields

def test_parse_balance(parser):
    """Test balance parsing"""
    balance_str = "D230101USD1000,00"
    balance = parser._parse_balance(balance_str)
    assert balance['creditDebitIndicator'] == 'Debit'
    assert balance['currency'] == 'USD'
    assert balance['amount'] == '1000.00'

if __name__ == '__main__':
    pytest.main([__file__])

