package alert

import "testing"

func TestAggKeyDesc_StableAndOrderIndependent(t *testing.T) {
	a := AggKeyDesc(ClassPage, map[string]string{
		"alertname":  "TiKVDown",
		"cluster_id": "c1", "severity": "critical", "instance": "10.0.0.1",
	})
	// Same agg labels, different non-agg label (instance) → same key.
	b := AggKeyDesc(ClassPage, map[string]string{
		"alertname":  "TiKVDown",
		"cluster_id": "c1", "severity": "critical", "instance": "10.0.0.2",
	})
	if a != b {
		t.Fatalf("non-agg labels must not affect key:\n%s\n%s", a, b)
	}
}

func TestAggKeyDesc_ClassificationSeparates(t *testing.T) {
	labels := map[string]string{"alertname": "X", "cluster_id": "c1", "severity": "critical"}
	if AggKeyHash(AggKeyDesc(ClassPage, labels)) == AggKeyHash(AggKeyDesc(ClassTicket, labels)) {
		t.Fatal("page and ticket must not share an aggregation key")
	}
}

func TestAggKeyDesc_DistinctClustersDiffer(t *testing.T) {
	l1 := map[string]string{"alertname": "X", "cluster_id": "c1"}
	l2 := map[string]string{"alertname": "X", "cluster_id": "c2"}
	if AggKeyDesc(ClassPage, l1) == AggKeyDesc(ClassPage, l2) {
		t.Fatal("different cluster_id must produce different keys")
	}
}
