package dashboard

import (
	"context"

	"github.com/tidbcloud/devops/entity"
	"github.com/tidbcloud/devops/pkg/service"

	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
)

type warmupSpySLACache struct {
	warmupCalled chan struct{}
}

func (c *warmupSpySLACache) Get(_ context.Context, _ []string) (*entity.SLAInfo, error) {
	return &entity.SLAInfo{}, nil
}

func (c *warmupSpySLACache) Warmup(_ context.Context) error {
	select {
	case <-c.warmupCalled:
	default:
		close(c.warmupCalled)
	}
	return nil
}

var _ = Describe("dashboard service warmup", func() {
	It("starts SLA warmup in background", func() {
		spy := &warmupSpySLACache{warmupCalled: make(chan struct{})}
		s := &svc{
			BaseService: service.NewBaseService("DashboardService"),
			slaCache:    spy,
		}

		s.startSLAWarmup()

		Eventually(spy.warmupCalled).Should(BeClosed())
	})
})
