from pathlib import Path

from kernbench.topology.builder import _read_spec, resolve_topology

TOPOLOGY_PATH = Path(__file__).parent.parent / "topology.yaml"


def test_topology_yaml_loads_without_error():
    # _compile_graph is still stubbed (returns None); load must not raise
    resolve_topology(str(TOPOLOGY_PATH))


def test_pe_layout_structure():
    spec = _read_spec(TOPOLOGY_PATH)
    pe_layout = spec["cube"]["pe_layout"]
    assert set(pe_layout["corners"]) == {"NW", "NE", "SW", "SE"}
    assert pe_layout["pe_per_corner"] == 2
    # derived total must equal original pe_per_cube: 8
    assert pe_layout["pe_per_corner"] * len(pe_layout["corners"]) == 8


def test_pe_template_components():
    spec = _read_spec(TOPOLOGY_PATH)
    comps = spec["cube"]["pe_template"]["components"]
    assert set(comps.keys()) == {
        "pe_cpu", "pe_scheduler", "pe_dma", "pe_fetch_store",
        "pe_gemm", "pe_math", "pe_mmu", "pe_tcm", "pe_ipcq",
    }


def test_pe_template_links_present():
    spec = _read_spec(TOPOLOGY_PATH)
    links = spec["cube"]["pe_template"]["links"]
    required = {
        "pe_cpu_to_scheduler_mm",
        "scheduler_to_dma_mm",
        "scheduler_to_gemm_mm",
        "scheduler_to_math_mm",
        "dma_to_tcm_bw_gbs", "dma_to_tcm_mm",
        "gemm_to_tcm_bw_gbs", "gemm_to_tcm_mm",
        "math_to_tcm_bw_gbs", "math_to_tcm_mm",
    }
    assert required.issubset(set(links.keys()))


def test_pe_dma_not_in_cube_components():
    spec = _read_spec(TOPOLOGY_PATH)
    assert "pe_dma" not in spec["cube"]["components"]


def test_pe_per_cube_removed():
    spec = _read_spec(TOPOLOGY_PATH)
    assert "pe_per_cube" not in spec["cube"].get("device", {})


def test_shared_resource_accel_slot():
    # ADR-0014 D4: PE_GEMM and PE_MATH share PE_ACCEL capacity = 1
    spec = _read_spec(TOPOLOGY_PATH)
    comps = spec["cube"]["pe_template"]["components"]
    assert comps["pe_gemm"]["attrs"]["shared_resource"] == "accel_slot"
    assert comps["pe_math"]["attrs"]["shared_resource"] == "accel_slot"
