package testing_test

import (
	"testing"

	"metis/pkg/config"
	"metis/pkg/inventory"
)

func TestConfigBuildLabelsAndTaints(t *testing.T) {
	inv := inventory.Inventory{
		Classes: []inventory.NodeClass{{
			Name:          "c1",
			Arch:          "arm64",
			OS:            "linux",
			Image:         "file:///tmp/base.img",
			DefaultLabels: map[string]string{"a": "1"},
			DefaultTaints: []string{"t1"},
		}},
		Nodes: []inventory.NodeSpec{{
			Name:          "n1",
			Class:         "c1",
			Hostname:      "n1",
			IP:            "1.1.1.1",
			K3sRole:       "agent",
			Labels:        map[string]string{"b": "2"},
			Taints:        []string{"t2"},
			LonghornDisks: []inventory.LonghornDisk{{Mountpoint: "/mnt/d1", UUID: "uuid-1", FS: "ext4"}},
			SSHUser:       "ubuntu",
			SSHAuthorized: []string{"key"},
		}},
	}
	cfg, err := config.Build(&inv, "n1")
	if err != nil {
		t.Fatalf("Build: %v", err)
	}
	if got, want := cfg.Labels["a"], "1"; got != want {
		t.Fatalf("label a = %q, want %q", got, want)
	}
	if got, want := cfg.Labels["b"], "2"; got != want {
		t.Fatalf("label b = %q, want %q", got, want)
	}
	if len(cfg.Taints) != 2 || cfg.Taints[0] != "t1" || cfg.Taints[1] != "t2" {
		t.Fatalf("taints = %#v", cfg.Taints)
	}
	if len(cfg.Fstab) != 1 {
		t.Fatalf("fstab entries = %d, want 1", len(cfg.Fstab))
	}
	if got, want := cfg.Fstab[0].Mountpoint, "/mnt/d1"; got != want {
		t.Fatalf("fstab mountpoint = %q, want %q", got, want)
	}
	if got, want := cfg.Fstab[0].UUID, "uuid-1"; got != want {
		t.Fatalf("fstab uuid = %q, want %q", got, want)
	}
	if got, want := cfg.K3s.Role, "agent"; got != want {
		t.Fatalf("k3s role = %q, want %q", got, want)
	}
}
