"""reconciliation.diff_positions: report symbols whose actual exchange qty drifts from intended beyond tol."""
from __future__ import annotations

from fxhnt.domain.execution.reconciliation import diff_positions


def test_within_tolerance_is_clean() -> None:
    drift = diff_positions(intended={"A": 1.0, "B": -2.0}, actual={"A": 1.0005, "B": -2.0},
                           tol_frac=0.01)
    assert drift == {}


def test_drift_beyond_tolerance_flagged() -> None:
    drift = diff_positions(intended={"A": 1.0}, actual={"A": 0.5}, tol_frac=0.01)
    assert "A" in drift and abs(drift["A"] - (-0.5)) < 1e-9     # actual - intended


def test_unexpected_position_flagged() -> None:
    drift = diff_positions(intended={}, actual={"Z": 3.0}, tol_frac=0.01)
    assert "Z" in drift


def test_short_position_drift_flagged() -> None:
    """A short position (negative qty) that drifts beyond tolerance must be flagged."""
    drift = diff_positions(intended={"S": -2.0}, actual={"S": -1.0}, tol_frac=0.01)
    assert "S" in drift
    assert abs(drift["S"] - 1.0) < 1e-9   # actual - intended = -1.0 - (-2.0) = +1.0


def test_expected_position_fully_closed_is_clean() -> None:
    """Intended 0 and actual 0 (position closed) must produce no drift entry."""
    drift = diff_positions(intended={"X": 0.0}, actual={}, tol_frac=0.01)
    assert drift == {}
