package facts

import (
	"testing"

	"metis/pkg/inventory"
)

func TestRecommendTargetsPerClass(t *testing.T) {
	inv := &inventory.Inventory{
		Classes: []inventory.NodeClass{{Name: "c1"}, {Name: "c2"}},
		Nodes: []inventory.NodeSpec{
			{Name: "n1", Class: "c1"},
			{Name: "n2", Class: "c2"},
		},
	}
	snaps := []Snapshot{
		{Hostname: "n1", Kernel: "k1", Containerd: "2.0", PackageSample: map[string]string{"containerd": "2.0"}},
		{Hostname: "n2", Kernel: "k2", Containerd: "1.7", PackageSample: map[string]string{"containerd": "1.7"}},
	}
	targets := RecommendTargets(inv, snaps)
	if targets["c1"].Kernel != "k1" || targets["c1"].Containerd != "2.0" {
		t.Fatalf("unexpected targets for c1: %+v", targets["c1"])
	}
	if targets["c2"].Kernel != "k2" || targets["c2"].Packages["containerd"] != "1.7" {
		t.Fatalf("unexpected targets for c2: %+v", targets["c2"])
	}
}

func TestRecommendHandlesUnknownClass(t *testing.T) {
	inv := &inventory.Inventory{}
	snaps := []Snapshot{{Hostname: "ghost", Kernel: "k"}}
	targets := RecommendTargets(inv, snaps)
	if _, ok := targets["unknown"]; !ok {
		t.Fatalf("expected unknown class entry")
	}
}
