package common

import (
	"testing"

	"github.com/pingcap/tidb-operator/api/v2/core/v1alpha1"
	"github.com/stretchr/testify/assert"
	"github.com/tidbcloud/config-service/sdk/api"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

	cloudv1 "github.com/tidbcloud/infra-api/api/v1"
)

func TestComponentContext_computePDConfig(t *testing.T) {
	tests := []struct {
		name    string
		cluster *cloudv1.Cluster
		config  *api.ComponentOutput
		wantCf  v1alpha1.ConfigFile
		wantErr bool
	}{
		{
			name: "happy path",
			cluster: &cloudv1.Cluster{
				ObjectMeta: metav1.ObjectMeta{Name: "123456"},
			},
			config: &api.ComponentOutput{
				Component: "pd",
				Config: &api.TomlConfig{
					MP: map[string]interface{}{
						"key1": "value1",
					},
				},
			},
			wantCf: v1alpha1.ConfigFile(`key1 = "value1"

[dashboard]
  public-path-prefix = "/proxy/cluster/123456/pd/dashboard"

[security]
  cert-allowed-cn = ["3c4d6c2f"]
`),
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cc := &ComponentContext{
				allowedCN: "3c4d6c2f",
				Config:    tt.config,
			}
			gotCf, err := cc.computePDConfig(tt.cluster)
			if (err != nil) != tt.wantErr {
				t.Errorf("ComputePDConfig() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			assert.Equalf(t, tt.wantCf, gotCf, "ComputePDConfig()")
		})
	}
}
