"""Verify serialization roundtrip vectors."""

import json
from decimal import Decimal

import pytest

from finlint.vectors import load_suite


@pytest.fixture(scope="module")
def vectors():
    return load_suite("serialization")


def test_serde_001_basic_roundtrip(vectors):
    """Decimal('100000.01') → JSON string → parse → exact match."""
    v = vectors[0]
    d = Decimal(v["inputs"]["value"])
    # Serialize as JSON string, parse back
    serialized = json.dumps(str(d))
    parsed = Decimal(json.loads(serialized))
    assert parsed == d


def test_serde_002_trailing_zero(vectors):
    """Decimal('0.10') preserves trailing zero in string representation."""
    v = vectors[1]
    d = Decimal(v["inputs"]["value"])
    assert str(d) == v["expected"]["value"]


def test_serde_003_max_safe_integer(vectors):
    """Decimal survives beyond JS MAX_SAFE_INTEGER."""
    v = vectors[2]
    d = Decimal(v["inputs"]["value"])
    assert str(d) == v["expected"]["value"]
    # Float would lose precision
    f = float(v["inputs"]["value"])
    assert f != int(v["inputs"]["value"])  # float truncates


def test_serde_004_crypto_precision(vectors):
    """18 decimal places preserved through Decimal roundtrip."""
    v = vectors[3]
    d = Decimal(v["inputs"]["value"])
    # Decimal normalizes to 1E-18, but value is preserved
    assert d == Decimal(v["expected"]["value"])
    # Fixed-point format preserves all digits
    assert f"{d:f}" == v["expected"]["value"]


def test_serde_005_negative_balance(vectors):
    """Negative balance roundtrip."""
    v = vectors[4]
    d = Decimal(v["inputs"]["value"])
    serialized = json.dumps(str(d))
    parsed = Decimal(json.loads(serialized))
    assert parsed == d
    assert str(parsed) == v["expected"]["value"]


def test_serde_006_zero(vectors):
    """Zero roundtrip preserves '0.00'."""
    v = vectors[5]
    d = Decimal(v["inputs"]["value"])
    assert str(d) == v["expected"]["value"]
