package infra import "testing" // findSample devuelve el primer sample con el nombre dado, o nil si no existe. func findSample(samples []PromSample, name string) *PromSample { for i := range samples { if samples[i].Name == name { return &samples[i] } } return nil } func TestCollectBatteryMetrics_ParseDischarging(t *testing.T) { in := []byte(`{"health":"GOOD","percentage":85,"plugged":"UNPLUGGED","status":"DISCHARGING","temperature":28.9,"current":-350000}`) samples, err := parseBatteryJSON(in) if err != nil { t.Fatalf("parseBatteryJSON returned error: %v", err) } t.Run("percent", func(t *testing.T) { s := findSample(samples, "node_battery_percent") if s == nil { t.Fatal("missing node_battery_percent") } if s.Value != 85 { t.Errorf("got percent %v, want 85", s.Value) } }) t.Run("temp", func(t *testing.T) { s := findSample(samples, "node_battery_temp_celsius") if s == nil { t.Fatal("missing node_battery_temp_celsius") } if s.Value != 28.9 { t.Errorf("got temp %v, want 28.9", s.Value) } }) t.Run("charging zero when discharging and unplugged", func(t *testing.T) { s := findSample(samples, "node_battery_charging") if s == nil { t.Fatal("missing node_battery_charging") } if s.Value != 0 { t.Errorf("got charging %v, want 0", s.Value) } }) t.Run("current", func(t *testing.T) { s := findSample(samples, "node_battery_current_ua") if s == nil { t.Fatal("missing node_battery_current_ua") } if s.Value != -350000 { t.Errorf("got current %v, want -350000", s.Value) } }) t.Run("health info series with labels", func(t *testing.T) { s := findSample(samples, "node_battery_health_info") if s == nil { t.Fatal("missing node_battery_health_info") } if s.Value != 1 { t.Errorf("got health_info value %v, want 1", s.Value) } if s.Labels["health"] != "GOOD" { t.Errorf("got health label %q, want GOOD", s.Labels["health"]) } if s.Labels["status"] != "DISCHARGING" { t.Errorf("got status label %q, want DISCHARGING", s.Labels["status"]) } if s.Labels["plugged"] != "UNPLUGGED" { t.Errorf("got plugged label %q, want UNPLUGGED", s.Labels["plugged"]) } }) } func TestCollectBatteryMetrics_ParseCharging(t *testing.T) { in := []byte(`{"health":"GOOD","percentage":60,"plugged":"PLUGGED_AC","status":"CHARGING","temperature":31.2,"current":420000}`) samples, err := parseBatteryJSON(in) if err != nil { t.Fatalf("parseBatteryJSON returned error: %v", err) } s := findSample(samples, "node_battery_charging") if s == nil { t.Fatal("missing node_battery_charging") } if s.Value != 1 { t.Errorf("got charging %v, want 1 (status CHARGING)", s.Value) } } func TestCollectBatteryMetrics_ParsePluggedNotUnplugged(t *testing.T) { // status no es CHARGING/FULL pero plugged != UNPLUGGED -> charging = 1. in := []byte(`{"health":"GOOD","percentage":100,"plugged":"PLUGGED_USB","status":"NOT_CHARGING","temperature":30.0,"current":0}`) samples, err := parseBatteryJSON(in) if err != nil { t.Fatalf("parseBatteryJSON returned error: %v", err) } s := findSample(samples, "node_battery_charging") if s == nil { t.Fatal("missing node_battery_charging") } if s.Value != 1 { t.Errorf("got charging %v, want 1 (plugged != UNPLUGGED)", s.Value) } } func TestCollectBatteryMetrics_ParseFull(t *testing.T) { in := []byte(`{"health":"GOOD","percentage":100,"plugged":"UNPLUGGED","status":"FULL","temperature":29.5,"current":0}`) samples, err := parseBatteryJSON(in) if err != nil { t.Fatalf("parseBatteryJSON returned error: %v", err) } s := findSample(samples, "node_battery_charging") if s == nil { t.Fatal("missing node_battery_charging") } if s.Value != 1 { t.Errorf("got charging %v, want 1 (status FULL)", s.Value) } } func TestCollectBatteryMetrics_InvalidJSON(t *testing.T) { in := []byte(`not a json at all`) _, err := parseBatteryJSON(in) if err == nil { t.Fatal("expected error for invalid JSON, got nil") } }