package cvt

import (
	"testing"

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

	apprent "github.com/tidbcloud/devops/entity/approval"
	apprmod "github.com/tidbcloud/devops/models/approval"
	"github.com/tidbcloud/devops/pkg/infra/utils/test"
)

func TestConverter(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "Approval Repo Converter Suite", test.ReporterConfig())
}

var _ = Describe("[Approval Converter]", func() {
	cvter := NewConverter()

	It("should convert overview between entity and model", func() {
		appr := &apprent.Approval{
			Name: "UT",
			Overview: map[string]string{
				"Operation":   "Scale",
				"Target Type": "cluster",
			},
		}

		mod := cvter.EntityToModel().Approval(appr)
		Expect(mod.Overview).NotTo(BeNil())

		got := cvter.ModelToEntity().Approval(mod)
		Expect(got.Overview).To(Equal(appr.Overview))
	})

	It("should keep nil overview as nil", func() {
		appr := &apprent.Approval{Name: "UT"}

		mod := cvter.EntityToModel().Approval(appr)
		Expect(mod.Overview).To(BeNil())

		got := cvter.ModelToEntity().Approval(&apprmod.Approval{Name: "UT"})
		Expect(got.Overview).To(BeNil())
	})

	It("should convert applicant notification suppression between entity and model", func() {
		appr := &apprent.Approval{
			Name: "UT",
			Configuration: apprent.ApprovalConfiguration{
				SkipApplicantNotification: true,
			},
		}

		mod := cvter.EntityToModel().Approval(appr)
		Expect(mod.Configuration).NotTo(BeNil())

		got := cvter.ModelToEntity().Approval(mod)
		Expect(got.Configuration.SkipApplicantNotification).To(BeTrue())
	})
})
