import pytest
from aceiot_cim_rdf.conversion import parse_raw
import os

def test_parser_basic(tmp_path):
    raw_content = """0, 100.0, 33, 0, 0, 60.0 / Header
Header line 2
Header line 3
1, 'Bus 1', 138.0, 3, 1, 1, 1, 1.06, 0.0
2, 'Bus 2', 138.0, 2, 1, 1, 1, 1.045, -5.0
0 / END OF BUS DATA
1, 1, 1, 1, 1, 10.0, 5.0
0 / END OF LOAD DATA
0 / END OF FIXED SHUNT DATA
1, '1', 100.0, 50.0
0 / END OF GENERATOR DATA
1, 2, '1', 0.01, 0.05, 0.02
0 / END OF BRANCH DATA
1, 2, 0, '1', 1, 1, 1, 0.0, 0.0, 2, ' ', 1, 1, 1.0, 0, 1.0, 0, 1.0, 0, 1.0
0.0, 0.1, 100.0
1.0, 138.0
1.0, 138.0
0 / END OF TRANSFORMER DATA
"""
    raw_file = tmp_path / "test.raw"
    raw_file.write_text(raw_content)
    
    data = parse_raw(str(raw_file))
    
    assert len(data["buses"]) == 2
    assert data["buses"][0]["name"] == "Bus 1"
    assert len(data["loads"]) == 1
    assert data["loads"][0]["p"] == 10.0
    assert len(data["generators"]) == 1
    assert len(data["branches"]) == 1
    assert len(data["transformers"]) == 1
    assert data["transformers"][0]["x12"] == 0.1

def test_parser_empty(tmp_path):
    raw_content = """0, 100.0, 33, 0, 0, 60.0
Line 2
Line 3
0 / END OF BUS DATA
0 / END OF LOAD DATA
0 / END OF FIXED SHUNT DATA
0 / END OF GENERATOR DATA
0 / END OF BRANCH DATA
0 / END OF TRANSFORMER DATA
"""
    raw_file = tmp_path / "empty.raw"
    raw_file.write_text(raw_content)
    data = parse_raw(str(raw_file))
    assert all(len(v) == 0 for v in data.values())
