import pytest
import warnings
from entropyk import (
    Concentration,
    VolumeFlow,
    RelativeHumidity,
    VaporQuality,
    RefrigerantSource,
    RefrigerantSink,
    BrineSource,
    BrineSink,
    AirSource,
    AirSink,
    FlowSource,
    FlowSink,
    System,
)

def test_physical_types_instantiation():
    """Test instantiation and constraints of new physical types."""
    c = Concentration(0.3)
    assert c.value == 0.3
    with pytest.raises(ValueError):
        Concentration(1.5)

    vf = VolumeFlow(0.1)
    assert vf.value == 0.1
    with pytest.raises(ValueError):
        VolumeFlow(-0.1)

    rh = RelativeHumidity(0.5)
    assert rh.value == 0.5
    with pytest.raises(ValueError):
        RelativeHumidity(-0.1)

    vq = VaporQuality(0.8)
    assert vq.value == 0.8
    with pytest.raises(ValueError):
        VaporQuality(1.1)

def test_refrigerant_boundary():
    """Test RefrigerantSource and RefrigerantSink instantiation."""
    source = RefrigerantSource(fluid="R134a", pressure_pa=200000.0, quality=0.5)
    sink = RefrigerantSink(fluid="R134a", p_back_pa=100000.0, quality=1.0)
    
    assert "R134a" in repr(source)
    assert "0.50" in repr(source)
    assert "R134a" in repr(sink)

    sys = System()
    sys.add_component(source)
    sys.add_component(sink)

def test_brine_boundary():
    """Test BrineSource and BrineSink instantiation."""
    source = BrineSource(fluid="EthyleneGlycol", concentration=0.3, temperature_k=280.0, pressure_pa=200000.0)
    sink = BrineSink(p_back_pa=150000.0)

    assert "EthyleneGlycol" in repr(source)
    assert "0.30" in repr(source)
    assert "150000" in repr(sink)

    sys = System()
    sys.add_component(source)
    sys.add_component(sink)

def test_air_boundary():
    """Test AirSource and AirSink instantiation."""
    source = AirSource(temperature_k=293.15, relative_humidity=0.5, pressure_pa=101325.0)
    sink = AirSink(p_back_pa=101325.0)

    assert "293.1" in repr(source)
    assert "0.50" in repr(source)

    sys = System()
    sys.add_component(source)
    sys.add_component(sink)

def test_deprecated_flow_boundary():
    """Test that FlowSource and FlowSink raise deprecation warnings."""
    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        FlowSource(pressure_pa=100000.0, temperature_k=300.0, fluid="Water")
        assert len(w) == 1
        assert issubclass(w[-1].category, DeprecationWarning)
        assert "deprecated" in str(w[-1].message).lower()

    with warnings.catch_warnings(record=True) as w:
        warnings.simplefilter("always")
        FlowSink()
        assert len(w) == 1
        assert issubclass(w[-1].category, DeprecationWarning)
        assert "deprecated" in str(w[-1].message).lower()
