package service

import (
	"strings"
	"testing"
	"time"

	"metis/pkg/facts"
)

func TestMetricsRenderAndKeyHelpers(t *testing.T) {
	metrics := NewMetrics()
	metrics.RecordBuild("n1", "ok")
	metrics.RecordFlash("n1", "h1", "ok")
	metrics.RecordSnapshot("n1", "ok", time.Unix(123, 0))
	metrics.RecordWatch("ok")
	metrics.SetDriftTargets(map[string]facts.Targets{
		"c1": {Kernel: "k", Packages: map[string]string{"p": "1"}},
	}, 2)

	var b strings.Builder
	metrics.Render(&b)
	out := b.String()
	if !strings.Contains(out, `metis_builds_total{node="n1",status="ok"} 1`) {
		t.Fatalf("missing build metric: %s", out)
	}
	if !strings.Contains(out, `metis_class_target_fields{class="c1"} 2`) {
		t.Fatalf("missing target metric: %s", out)
	}
	if got := splitKey("a", 3); len(got) != 3 || got[0] != "a" || got[1] != "" || got[2] != "" {
		t.Fatalf("splitKey fallback = %#v", got)
	}
}
