package config

import (
	"testing"

	"metis/pkg/inventory"
)

func TestBuildBranches(t *testing.T) {
	inv := &inventory.Inventory{
		Classes: []inventory.NodeClass{{
			Name:          "rpi4",
			Arch:          "arm64",
			OS:            "armbian",
			Image:         "file:///tmp/base.img",
			K3sVersion:    "v1.31.5+k3s1",
			DefaultLabels: map[string]string{"a": "class", "b": "class"},
			DefaultTaints: []string{"class-taint"},
		}},
		Nodes: []inventory.NodeSpec{{
			Name:          "titan-15",
			Class:         "rpi4",
			Hostname:      "titan-15",
			IP:            "192.168.22.43",
			K3sRole:       "agent",
			K3sVersion:    "v1.31.5+k3s2",
			K3sURL:        "https://192.168.22.7:6443",
			K3sToken:      "token",
			Labels:        map[string]string{"c": "node"},
			Taints:        []string{"node-taint"},
			SSHUser:       "atlas",
			SSHAuthorized: []string{"ssh-ed25519 AAA"},
			LonghornDisks: []inventory.LonghornDisk{{UUID: "u1", Mountpoint: "/var/lib/longhorn"}},
			USBScratch:    &inventory.USBScratchDisk{Mountpoint: "/mnt/scratch", UUID: "usb-1", BindTargets: []string{"/var/lib/rancher"}},
		}},
	}
	cfg, err := Build(inv, "titan-15")
	if err != nil {
		t.Fatalf("Build: %v", err)
	}
	if cfg.K3s.Version != "v1.31.5+k3s2" || len(cfg.Fstab) != 3 || cfg.Fstab[0].FS != "ext4" {
		t.Fatalf("unexpected config: %#v", cfg)
	}
	if cfg.USBScratch == nil || cfg.USBScratch.Mountpoint != "/mnt/scratch" {
		t.Fatalf("expected usb scratch config: %#v", cfg.USBScratch)
	}
	if _, err := Build(&inventory.Inventory{}, "missing"); err == nil {
		t.Fatal("expected Build to fail for missing node")
	}

	inv.Nodes[0].Hostname = ""
	if _, err := Build(inv, "titan-15"); err == nil {
		t.Fatal("expected Build to fail without hostname")
	}
}
