package router

import (
	"testing"

	. "github.com/onsi/gomega"
)

func TestReplicasWithManagerFallback(t *testing.T) {
	g := NewWithT(t)

	replicas, known := replicasWithManagerFallback(1, 2, true)
	g.Expect(replicas).To(Equal(1))
	g.Expect(known).To(BeTrue())

	replicas, known = replicasWithManagerFallback(0, 2, true)
	g.Expect(replicas).To(Equal(2))
	g.Expect(known).To(BeTrue())

	replicas, known = replicasWithManagerFallback(0, 0, false)
	g.Expect(replicas).To(Equal(0))
	g.Expect(known).To(BeFalse())
}

func TestShouldFetchManagerWhenReplicasUnknown(t *testing.T) {
	g := NewWithT(t)

	g.Expect(shouldFetchManager(nil, 0, false, true, "pool-a", "pool-a")).To(BeTrue())
	g.Expect(shouldFetchManager([]string{"127.0.0.1"}, 0, false, false, "pool-a", "pool-a")).To(BeTrue())
	g.Expect(shouldFetchManager([]string{"127.0.0.1"}, 1, true, false, "pool-a", "pool-a")).To(BeFalse())
	g.Expect(shouldFetchManager(nil, 1, true, true, "pool-a", "pool-a")).To(BeTrue())
}

func TestShouldNotFetchManagerRepeatedlyWithInfraReplicas(t *testing.T) {
	g := NewWithT(t)

	replicas, known := replicasWithManagerFallback(1, 0, false)
	g.Expect(shouldFetchManager(
		[]string{"127.0.0.1"},
		replicas,
		known,
		false,
		"pool-a",
		"pool-a",
	)).To(BeFalse())
}
