import json
import tempfile
from pathlib import Path

import pytest

from metis.pkg import inventory

def test_load_and_find_node():
    data = {
        "classes": [
            {
                "name": "rpi5",
                "arch": "arm64",
                "os": "ubuntu",
                "image": "file:///tmp/base.img",
                "checksum": "sha256:deadbeef",
                "default_labels": {"hardware": "rpi5"},
            }
        ],
        "nodes": [
            {
                "name": "titan-04",
                "class": "rpi5",
                "hostname": "titan-04",
                "ip": "192.168.22.30",
                "k3s_role": "agent",
            }
        ],
    }
    with tempfile.TemporaryDirectory() as tmp:
        path = Path(tmp) / "inv.yaml"
        path.write_text(json.dumps(data))
        inv = inventory.Load(path)
        node, cls, err = inv.FindNode("titan-04")
        assert err is None
        assert node.Hostname == "titan-04"
        assert cls.Arch == "arm64"


def test_find_node_missing():
    inv = inventory.Inventory(classes=[], nodes=[])
    node, cls, err = inv.FindNode("missing")
    assert err is not None
    assert node is None
    assert cls is None
