"""
Value API surface regression tests.

The Value layer now exposes a single owning type (`Value`) with typed-null
semantics. Policy-specific variants are intentionally not exposed.
"""

import pytest

from hgraph._feature_switch import is_feature_enabled
if not is_feature_enabled("use_cpp"):
    pytest.skip("Buffer protocol tests require C++ extension", allow_module_level=True)

_hgraph = pytest.importorskip("hgraph._hgraph", reason="C++ extension not available")
value = _hgraph.value


def test_plain_value_is_exposed():
    assert hasattr(value, "Value")


def test_policy_value_variants_not_exposed():
    assert not hasattr(value, "CachedValue")
    assert not hasattr(value, "TSValue")
    assert not hasattr(value, "ValidatedValue")


def test_plain_value_typed_null_roundtrip():
    int_schema = value.scalar_type_meta_int64()

    v = value.Value(int_schema)
    assert not v.has_value()
    assert not v.valid()
    assert v.schema is int_schema
    assert v.to_python() is None

    v.from_python(123)
    assert v.has_value()
    assert v.valid()
    assert v.as_int() == 123

    v.from_python(None)
    assert not v.has_value()
    assert not v.valid()
    assert v.schema is int_schema
    assert v.to_python() is None


def test_plain_value_schema_and_python_ctor():
    int_schema = value.scalar_type_meta_int64()

    v = value.Value(int_schema, 456)
    assert v.has_value()
    assert v.valid()
    assert v.schema is int_schema
    assert v.as_int() == 456


def test_plain_value_emplace_and_reset():
    int_schema = value.scalar_type_meta_int64()

    v = value.Value(int_schema)
    assert not v.has_value()

    v.emplace()
    assert v.has_value()
    assert v.as_int() == 0

    v.reset()
    assert not v.has_value()
    assert v.schema is int_schema
