
import subprocess
import os
from pathlib import Path

def test_semantics():
    # We'll create a python script, transpile it, and (if possible) run it?
    # Since we can't run Luau here easily without a standalone interpreter, 
    # we'll at least verify the generated code contains the expected shims.
    
    test_code = """
if []:
    print("Empty list is truthy!")
else:
    print("Empty list is falsey")

x = [1, 2]
y = [1, 2]
if x == y:
    print("Deep equality works")
"""
    Path("test_sem.py").write_text(test_code)
    
    try:
        # Transpile
        subprocess.run(["python", "-m", "cli.main", "build", "test_sem.py", "test_sem.lua", "--no-runtime"], check=True)
        
        lua_code = Path("test_sem.lua").read_text()
        print("Generated Luau Code:")
        print(lua_code)
        
        # Verify shims are present
        assert "py_bool({})" in lua_code or "py_bool({ })" in lua_code or "py_bool" in lua_code
        assert "py_eq" in lua_code
        
        print("\n[OK] Semantics shims found in generated code.")
        
        # Verify source map
        if os.path.exists("test_sem.lua.map"):
            import json
            map_data = json.loads(Path("test_sem.lua.map").read_text())
            print(f"Source Map found for: {map_data['source']}")
            print(f"Mappings: {map_data['mapping']}")
            assert len(map_data['mapping']) > 0
            print("[OK] Source map generated correctly.")
            
    finally:
        if os.path.exists("test_sem.py"): os.remove("test_sem.py")
        if os.path.exists("test_sem.lua"): os.remove("test_sem.lua")
        if os.path.exists("test_sem.lua.map"): os.remove("test_sem.lua.map")

if __name__ == "__main__":
    test_semantics()
