package runtimeworker

import (
	"reflect"
	"testing"

	io_prometheus_client "github.com/prometheus/client_model/go"
)

func assertMetricWithLabels(
	t *testing.T,
	families []*io_prometheus_client.MetricFamily,
	name string,
	wantValue float64,
	wantLabels map[string]string,
) {
	t.Helper()
	for _, family := range families {
		if family.GetName() != name {
			continue
		}
		for _, metric := range family.GetMetric() {
			labels := make(map[string]string, len(metric.GetLabel()))
			for _, label := range metric.GetLabel() {
				labels[label.GetName()] = label.GetValue()
			}
			if !reflect.DeepEqual(labels, wantLabels) {
				continue
			}
			value := metric.GetGauge().GetValue()
			if metric.GetCounter() != nil {
				value = metric.GetCounter().GetValue()
			}
			if value != wantValue {
				t.Fatalf("metric %s%v = %v, want %v", name, wantLabels, value, wantValue)
			}
			return
		}
	}
	t.Fatalf("metric %s%v is missing", name, wantLabels)
}
