package testing_test

import (
	"os"
	"path/filepath"
	"testing"

	"metis/pkg/inventory"
)

func TestLoadAndFindNode(t *testing.T) {
	invPath := filepath.Join(t.TempDir(), "inventory.yaml")
	if err := os.WriteFile(invPath, []byte(`
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
`), 0o644); err != nil {
		t.Fatal(err)
	}
	inv, err := inventory.Load(invPath)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	node, class, err := inv.FindNode("titan-04")
	if err != nil {
		t.Fatalf("FindNode: %v", err)
	}
	if node.Hostname != "titan-04" {
		t.Fatalf("hostname = %q", node.Hostname)
	}
	if class.Arch != "arm64" {
		t.Fatalf("arch = %q", class.Arch)
	}
}

func TestFindNodeMissing(t *testing.T) {
	inv := inventory.Inventory{Classes: nil, Nodes: nil}
	node, class, err := inv.FindNode("missing")
	if err == nil {
		t.Fatal("expected missing node error")
	}
	if node != nil || class != nil {
		t.Fatalf("unexpected node/class: %#v %#v", node, class)
	}
}
