package metrics import ( "net/http/httptest" "strings" "testing" "time" ) func TestExporterEmitsCoreMetrics(t *testing.T) { e := New() e.UpdateBudget(321) e.UpdateSample(Sample{ Name: "Pyrphoros", Target: "pyrphoros@localhost", OnBattery: true, LowBattery: false, RuntimeSecond: 412, BatteryCharge: 81.5, LoadPercent: 27.4, PowerNominalW: 510, ThresholdSec: 354, Trigger: true, BreachCount: 2, Status: "OB", UpdatedAt: time.Unix(1710000000, 0).UTC(), }) e.MarkShutdown("ups-threshold") req := httptest.NewRequest("GET", "/metrics", nil) rr := httptest.NewRecorder() e.Handler("/metrics").ServeHTTP(rr, req) body := rr.Body.String() mustContain := []string{ "ananke_shutdown_budget_seconds 321", "ananke_shutdown_triggers_total 1", "ananke_ups_on_battery{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"", "ananke_ups_runtime_seconds{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"", "ananke_ups_battery_charge_percent{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"", "ananke_ups_load_percent{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"", "ananke_ups_power_nominal_watts{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"", "ananke_ups_threshold_seconds{source=\"Pyrphoros\",target=\"pyrphoros@localhost\"", } for _, m := range mustContain { if !strings.Contains(body, m) { t.Fatalf("missing metric fragment %q in output:\n%s", m, body) } } }