package platform

import "testing"

func TestFirstTempInputValue(t *testing.T) {
	feature := map[string]any{
		"temp1_input": 61.5,
		"temp1_max":   80.0,
	}
	got, ok := firstTempInputValue(feature)
	if !ok {
		t.Fatal("expected value")
	}
	if got != 61.5 {
		t.Fatalf("got %v want 61.5", got)
	}
}

func TestClassifyLiveTempGroup(t *testing.T) {
	tests := []struct {
		chip string
		name string
		want string
	}{
		{chip: "coretemp-isa-0000", name: "Package id 0", want: "cpu"},
		{chip: "amdgpu-pci-4300", name: "edge", want: "gpu"},
		{chip: "nvme-pci-0100", name: "Composite", want: "ambient"},
		{chip: "acpitz-acpi-0", name: "temp1", want: "ambient"},
	}
	for _, tc := range tests {
		if got := classifyLiveTempGroup(tc.chip, tc.name); got != tc.want {
			t.Fatalf("classifyLiveTempGroup(%q,%q)=%q want %q", tc.chip, tc.name, got, tc.want)
		}
	}
}

func TestCompactAmbientTempName(t *testing.T) {
	if got := compactAmbientTempName("nvme-pci-0100", "Composite"); got != "nvme-pci-0100 / Composite" {
		t.Fatalf("got %q", got)
	}
	if got := compactAmbientTempName("", "Inlet Temp"); got != "Inlet Temp" {
		t.Fatalf("got %q", got)
	}
}

func TestCPULoadPctBetween(t *testing.T) {
	tests := []struct {
		name      string
		prevTotal uint64
		prevIdle  uint64
		total     uint64
		idle      uint64
		want      float64
	}{
		{
			name:      "busy half",
			prevTotal: 100,
			prevIdle:  40,
			total:     200,
			idle:      90,
			want:      50,
		},
		{
			name:      "fully busy",
			prevTotal: 100,
			prevIdle:  40,
			total:     200,
			idle:      40,
			want:      100,
		},
		{
			name:      "no progress",
			prevTotal: 100,
			prevIdle:  40,
			total:     100,
			idle:      40,
			want:      0,
		},
		{
			name:      "idle delta larger than total clamps to zero",
			prevTotal: 100,
			prevIdle:  40,
			total:     200,
			idle:      150,
			want:      0,
		},
	}

	for _, tc := range tests {
		if got := cpuLoadPctBetween(tc.prevTotal, tc.prevIdle, tc.total, tc.idle); got != tc.want {
			t.Fatalf("%s: cpuLoadPctBetween(...)=%v want %v", tc.name, got, tc.want)
		}
	}
}
