package inventory

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

func TestLoadExpandsEnvironmentVariables(t *testing.T) {
	t.Setenv("METIS_IMAGE_PATH", "file:///tmp/rpi4.img")
	t.Setenv("METIS_K3S_TOKEN", "secret-token")
	invPath := filepath.Join(t.TempDir(), "inventory.yaml")
	if err := os.WriteFile(invPath, []byte(`
classes:
  - name: rpi4
    image: ${METIS_IMAGE_PATH}
    k3s_version: v1.31.5+k3s1
nodes:
  - name: titan-13
    class: rpi4
    hostname: titan-13
    ip: 192.168.22.41
    k3s_role: agent
    k3s_token: ${METIS_K3S_TOKEN}
`), 0o644); err != nil {
		t.Fatal(err)
	}

	inv, err := Load(invPath)
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	node, class, err := inv.FindNode("titan-13")
	if err != nil {
		t.Fatalf("FindNode: %v", err)
	}
	if class.Image != "file:///tmp/rpi4.img" {
		t.Fatalf("image not expanded: %q", class.Image)
	}
	if node.K3sToken != "secret-token" {
		t.Fatalf("token not expanded: %q", node.K3sToken)
	}
}
