package v1

import (
	"context"
	"testing"

	"github.com/stretchr/testify/assert"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"sigs.k8s.io/controller-runtime/pkg/client/fake"
)

func TestConfigChange_validateSpec(t *testing.T) {
	client := fake.NewClientBuilder().Build()
	webhookClient = readThroughReader{
		direct: client,
		cached: client,
	}
	tests := []struct {
		name          string
		runBeforeEach func(cc *ConfigChange)
		wantErr       bool
	}{
		{
			name:          "cluster is not found",
			runBeforeEach: nil,
			wantErr:       true,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			cc := &ConfigChange{
				ObjectMeta: metav1.ObjectMeta{
					Namespace: "ns1",
					Name:      "cc1",
				},
				Spec: ConfigChangeSpec{
					ClusterName:        "cluster1",
					ConfigRevisionName: "cr1",
					ChangeStrategy:     ConfigChangeStrategyHotReload,
					Paused:             nil,
				},
			}

			if tt.runBeforeEach != nil {
				tt.runBeforeEach(cc)
			}

			errs := cc.validateSpec(context.Background())
			if tt.wantErr {
				assert.Truef(t, len(errs) != 0, "validateSpec")
			} else {
				assert.Truef(t, len(errs) == 0, "validateSpec")
			}
		})
	}
}
