package facts

import (
	"testing"

	"metis/pkg/inventory"
)

func TestAggregateGroupsByClass(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", PackageSample: map[string]string{"containerd": "2.0"}},
		{Hostname: "n2", Kernel: "k2", PackageSample: map[string]string{"containerd": "1.7"}},
		{Hostname: "n1", Kernel: "k1"},
	}
	sum := Aggregate(inv, snaps)
	if len(sum) != 2 {
		t.Fatalf("expected 2 classes, got %d", len(sum))
	}
	c1 := sum["c1"]
	if c1 == nil || c1.Kernels["k1"] != 2 {
		t.Fatalf("expected k1 count 2, got %#v", c1)
	}
	if c1.PackageStats["containerd"]["2.0"] != 1 {
		t.Fatalf("package stats not tallied: %#v", c1.PackageStats)
	}
}
