import os
import sys
from transpiler.middleend.query_service import QueryService

def test_type_diagnostics():
    qs = QueryService()
    
    # Test file with type mismatch
    content = 'x = 1 + "wrong"\ny = 10\nz = 1 + "wrong"'
    uri = "file:///test_diag.rpy"
    
    print("Testing diagnostics for type mismatch...")
    # Manual reload with content
    mod_name = "test_diag"
    import ast
    from transpiler.middleend.query_service import _query_transform
    
    qs.diagnostics_manager.clear()
    tree = ast.parse(content)
    _query_transform(tree, filename="test_diag.rpy", project=qs.project_context, module_name=mod_name, uri=uri, diagnostics=qs.diagnostics_manager)
    
    diags = qs.diagnostics_manager.diagnostics
    print(f"Produced {len(diags)} diagnostics.")
    for d in diags:
        print(f"[{d.severity.name}] {d.message} at line {d.line}, col {d.col}")
    
    # We expect 2 errors (one for x, one for z)
    # Actually, 1 + "wrong" is one binary op.
    # x = 1 + "wrong" has one binary op.
    # z = 1 + "wrong" has another.
    # So 2 errors.
    
    assert any("Operator '+' is not supported for type 'string'" in d.message for d in diags)
    print("Test internal diagnostics: PASS")

if __name__ == "__main__":
    test_type_diagnostics()
