package tags

import "testing"

func TestTopKByWeight(t *testing.T) {
	merged := map[string]float64{
		"a": 0.9,
		"b": 0.5,
		"c": 0.5, // ties with b → name asc breaks it (b before c)
		"d": 0.1,
	}
	got := topKByWeight(merged, 3)
	if len(got) != 3 {
		t.Fatalf("cap not applied: got %d", len(got))
	}
	wantOrder := []string{"a", "b", "c"}
	for i, w := range wantOrder {
		if got[i].Name != w {
			t.Errorf("position %d = %q, want %q (full: %v)", i, got[i].Name, w, got)
		}
	}
	// k<=0 means no cap.
	if len(topKByWeight(merged, 0)) != 4 {
		t.Error("k=0 should not cap")
	}
	if len(topKByWeight(nil, 5)) != 0 {
		t.Error("empty input yields empty output")
	}
}

func TestSourceLabel(t *testing.T) {
	cases := []struct {
		ids  []string
		want string
	}{
		{nil, sourceNone},
		{[]string{"musicbrainz"}, "musicbrainz"},
		{[]string{"lastfm"}, "lastfm"},
		{[]string{"lastfm", "musicbrainz"}, sourceMixed},
	}
	for _, c := range cases {
		if got := sourceLabel(c.ids); got != c.want {
			t.Errorf("sourceLabel(%v) = %q, want %q", c.ids, got, c.want)
		}
	}
}

func TestContributorIDs_Sorted(t *testing.T) {
	got := contributorIDs(map[string]bool{"lastfm": true, "musicbrainz": true})
	if len(got) != 2 || got[0] != "lastfm" || got[1] != "musicbrainz" {
		t.Errorf("contributorIDs sorted = %v, want [lastfm musicbrainz]", got)
	}
}
