package collector

import (
	"os/exec"
	"testing"
)

func TestParseROCmSingleValueCSV(t *testing.T) {
	raw := "device,Serial Number\ncard0,ABC123\ncard1,XYZ789\n"
	got := parseROCmSingleValueCSV(raw)
	if got["card0"] != "ABC123" {
		t.Fatalf("card0=%q want ABC123", got["card0"])
	}
	if got["card1"] != "XYZ789" {
		t.Fatalf("card1=%q want XYZ789", got["card1"])
	}
}

func TestQueryAMDNumericFieldParsesUnits(t *testing.T) {
	origExec := amdSMIExecCommand
	origLookPath := amdSMILookPath
	t.Cleanup(func() {
		amdSMIExecCommand = origExec
		amdSMILookPath = origLookPath
	})

	amdSMILookPath = func(string) (string, error) { return "/usr/bin/rocm-smi", nil }
	amdSMIExecCommand = func(name string, args ...string) *exec.Cmd {
		return exec.Command("sh", "-c", "printf 'device,Temperature\\ncard0,45.5c\\ncard1,67.0c\\n'")
	}

	got, err := queryAMDNumericField("--showtemp")
	if err != nil {
		t.Fatalf("queryAMDNumericField: %v", err)
	}
	if got["card0"] != 45.5 {
		t.Fatalf("card0=%v want 45.5", got["card0"])
	}
	if got["card1"] != 67.0 {
		t.Fatalf("card1=%v want 67.0", got["card1"])
	}
}

func TestNormalizeROCmCardKey(t *testing.T) {
	tests := map[string]string{
		"0":      "card0",
		"card1":  "card1",
		"Device": "",
		"":       "",
	}
	for input, want := range tests {
		if got := normalizeROCmCardKey(input); got != want {
			t.Fatalf("normalizeROCmCardKey(%q)=%q want %q", input, got, want)
		}
	}
}
