import pytest
from scripts.check_imgui_scopes import ImGuiScopeLinter

def test_valid_scopes():
 linter = ImGuiScopeLinter()
 source = """
def valid_func():
 imgui.begin("Window")
 imgui.push_id("sub")
 imgui.text("Hello")
 imgui.pop_id()
 imgui.end()
"""
 errors = linter.check_source(source)
 assert not errors

def test_unclosed_scope():
 linter = ImGuiScopeLinter()
 source = """
def unclosed_func():
 imgui.begin("Window")
 imgui.text("Hello")
 # Missing imgui.end()
"""
 errors = linter.check_source(source)
 assert len(errors) == 1
 assert "Unclosed scope 'begin'" in errors[0]

def test_extra_pop():
 linter = ImGuiScopeLinter()
 source = """
def extra_pop_func():
 imgui.begin("Window")
 imgui.end()
 imgui.end() # Extra
"""
 errors = linter.check_source(source)
 assert len(errors) == 1
 assert "Extra 'end'" in errors[0]

def test_mismatched_scopes():
 linter = ImGuiScopeLinter()
 source = """
def mismatched_func():
 imgui.begin("Window")
 imgui.push_id("id")
 imgui.end() # Should be pop_id
 imgui.pop_id() # Should be end
"""
 errors = linter.check_source(source)
 # mismatch pops 'push_id', then pop_id() mismatches with 'begin'
 assert len(errors) == 2
 assert "Mismatched scope" in errors[0]
 assert "end" in errors[0]
 assert "push_id" in errors[0]
 assert "Mismatched scope" in errors[1]
 assert "pop_id" in errors[1]
 assert "begin" in errors[1]

def test_nested_functions():
 linter = ImGuiScopeLinter()
 source = """
def outer():
 imgui.begin("Outer")
 def inner():
  imgui.push_id("Inner")
  # Missing pop_id
 imgui.end()
"""
 errors = linter.check_source(source)
 assert len(errors) == 1
 assert "Function 'inner': Unclosed scope 'push_id'" in errors[0]

def test_node_editor_scopes():
 linter = ImGuiScopeLinter()
 source = """
def ed_func():
 ed.begin("NodeEditor")
 ed.end()
"""
 errors = linter.check_source(source)
 assert not errors

def test_popup_modal_end():
 linter = ImGuiScopeLinter()
 source = """
def popup_func():
 imgui.begin_popup_modal("Modal")
 imgui.end_popup()
"""
 errors = linter.check_source(source)
 assert not errors
