package collector

import "testing"

func TestParseSensorsJSON(t *testing.T) {
	raw := []byte(`{
  "coretemp-isa-0000": {
    "Adapter": "ISA adapter",
    "Package id 0": {
      "temp1_input": 61.5,
      "temp1_max": 80.0,
      "temp1_crit": 95.0
    },
    "fan1": {
      "fan1_input": 4200
    }
  },
  "acpitz-acpi-0": {
    "Adapter": "ACPI interface",
    "in0": {
      "in0_input": 12.06
    },
    "curr1": {
      "curr1_input": 0.64
    },
    "power1": {
      "power1_average": 137.0
    },
    "humidity1": {
      "humidity1_input": 38.5
    }
  }
}`)

	got, err := parseSensorsJSON(raw)
	if err != nil {
		t.Fatalf("parseSensorsJSON error: %v", err)
	}
	if got == nil {
		t.Fatal("expected sensors")
	}
	if len(got.Temperatures) != 1 || got.Temperatures[0].Celsius == nil || *got.Temperatures[0].Celsius != 61.5 {
		t.Fatalf("temperatures mismatch: %#v", got.Temperatures)
	}
	if len(got.Fans) != 1 || got.Fans[0].RPM == nil || *got.Fans[0].RPM != 4200 {
		t.Fatalf("fans mismatch: %#v", got.Fans)
	}
	if len(got.Power) != 3 {
		t.Fatalf("power sensors mismatch: %#v", got.Power)
	}
	if len(got.Other) != 1 || got.Other[0].Unit == nil || *got.Other[0].Unit != "%" {
		t.Fatalf("other sensors mismatch: %#v", got.Other)
	}
}
