"""
Parity test for price_basis_v1.py against GAS source.

Tests F02/F03/F04/F06 logic: priceBasis selection based on takeProfit tier prices.
Method: Extract GAS function source, run in Node, compare against Python port.

Source: src/gas_adapter_parts/gdf_01_price_metrics.gs lines 774, 783, 792, 801
"""

import pytest
from formulas.price_basis_v1 import select_price_basis_tier2, select_price_basis_tier1


class TestPriceBasisTier2Parity:
    """F02/F03: select_price_basis_tier2(tp2_price)"""

    def test_tp2_price_finite_returns_tier2(self):
        """When tp2Price is a positive number, return TAKE_PROFIT_TIER2_PRICE"""
        assert select_price_basis_tier2(100.5) == "TAKE_PROFIT_TIER2_PRICE"
        assert select_price_basis_tier2(1.0) == "TAKE_PROFIT_TIER2_PRICE"
        assert select_price_basis_tier2(999999.99) == "TAKE_PROFIT_TIER2_PRICE"

    def test_tp2_price_zero_returns_fallback(self):
        """When tp2Price is 0 or negative, return PRIOR_CLOSE_X_0.998"""
        assert select_price_basis_tier2(0) == "PRIOR_CLOSE_X_0.998"
        assert select_price_basis_tier2(-1.5) == "PRIOR_CLOSE_X_0.998"

    def test_tp2_price_none_returns_fallback(self):
        """When tp2Price is None/NaN, return PRIOR_CLOSE_X_0.998"""
        assert select_price_basis_tier2(None) == "PRIOR_CLOSE_X_0.998"
        assert select_price_basis_tier2(float('nan')) == "PRIOR_CLOSE_X_0.998"


class TestPriceBasisTier1Parity:
    """F04/F06: select_price_basis_tier1(tp1_price)"""

    def test_tp1_price_finite_returns_tier1(self):
        """When tp1Price is a positive number, return TAKE_PROFIT_TIER1_PRICE"""
        assert select_price_basis_tier1(50.25) == "TAKE_PROFIT_TIER1_PRICE"
        assert select_price_basis_tier1(1.0) == "TAKE_PROFIT_TIER1_PRICE"
        assert select_price_basis_tier1(500000.0) == "TAKE_PROFIT_TIER1_PRICE"

    def test_tp1_price_zero_returns_fallback(self):
        """When tp1Price is 0 or negative, return PRIOR_CLOSE_X_0.998"""
        assert select_price_basis_tier1(0) == "PRIOR_CLOSE_X_0.998"
        assert select_price_basis_tier1(-10) == "PRIOR_CLOSE_X_0.998"

    def test_tp1_price_none_returns_fallback(self):
        """When tp1Price is None/NaN, return PRIOR_CLOSE_X_0.998"""
        assert select_price_basis_tier1(None) == "PRIOR_CLOSE_X_0.998"
        assert select_price_basis_tier1(float('nan')) == "PRIOR_CLOSE_X_0.998"


class TestPriceBasisEdgeCases:
    """Edge cases matching GAS Number.isFinite semantics"""

    def test_infinity_returns_fallback(self):
        """When price is Infinity, return fallback"""
        assert select_price_basis_tier2(float('inf')) == "PRIOR_CLOSE_X_0.998"
        assert select_price_basis_tier1(float('inf')) == "PRIOR_CLOSE_X_0.998"

    def test_negative_infinity_returns_fallback(self):
        """When price is -Infinity, return fallback"""
        assert select_price_basis_tier2(float('-inf')) == "PRIOR_CLOSE_X_0.998"
        assert select_price_basis_tier1(float('-inf')) == "PRIOR_CLOSE_X_0.998"
