import pytest

from test.stack.test_helpers import (
    count_defs_refs,
    resolve_symbol,
    setup_stack_resolution,
)


@pytest.mark.blends_test_group("stack_unittesting")
def test_same_function_name_different_scopes() -> None:
    view, record = setup_stack_resolution("case_same_name_diff_scope.py")
    defs, _refs = count_defs_refs(view, "helper")
    assert defs >= 2, f"Expected at least 2 defs of 'helper', got {defs}"  # noqa: PLR2004

    result_0 = resolve_symbol(view, record, "helper", ref_index=0)
    result_1 = resolve_symbol(view, record, "helper", ref_index=1)
    assert len(result_0) == 1, f"Ref in outer() should resolve to 1 def, got {len(result_0)}"
    assert len(result_1) == 1, f"Ref in another() should resolve to 1 def, got {len(result_1)}"
    assert result_0 != result_1, "Should resolve to different definitions"


@pytest.mark.blends_test_group("stack_unittesting")
def test_sibling_function_parameter_isolation() -> None:
    view, record = setup_stack_resolution("case_sibling_param_scope.py")
    defs, _refs = count_defs_refs(view, "x")
    assert defs >= 2  # noqa: PLR2004

    result_0 = resolve_symbol(view, record, "x", ref_index=0)
    assert len(result_0) == 1, f"'x' in compute() should resolve to 1 def, got {len(result_0)}"


@pytest.mark.blends_test_group("stack_unittesting")
def test_module_level_visible_from_function() -> None:
    view, record = setup_stack_resolution("case_func_arg.py")
    result = resolve_symbol(view, record, "value")
    assert len(result) >= 1, "Module-level 'value' should be visible from function call"
