package balancer_test

import (
	"testing"

	"github.com/rose_cat707/luminary/internal/balancer"
	"github.com/rose_cat707/luminary/internal/model"
)

func TestSelectWeighted(t *testing.T) {
	candidates := []balancer.KeyCandidate{
		{Key: model.ProviderKey{ID: 1, Name: "a"}, Weight: 1},
		{Key: model.ProviderKey{ID: 2, Name: "b"}, Weight: 3},
	}
	counts := map[uint]int{}
	for i := 0; i < 1000; i++ {
		k := balancer.SelectWeighted(candidates)
		if k == nil {
			t.Fatal("nil key")
		}
		counts[k.ID]++
	}
	if counts[2] <= counts[1] {
		t.Fatalf("expected heavier weight key selected more often: %v", counts)
	}
}

func TestSelectWeightedEmpty(t *testing.T) {
	if k := balancer.SelectWeighted(nil); k != nil {
		t.Fatal("expected nil")
	}
}
