/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package features

import (
	"testing"

	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
	"k8s.io/apimachinery/pkg/util/version"
	clientfeatures "k8s.io/client-go/features"
	"k8s.io/component-base/featuregate"
)

func TestClientAdapterEnabled(t *testing.T) {
	fg := featuregate.NewFeatureGate()
	if err := fg.Add(map[featuregate.Feature]featuregate.FeatureSpec{
		"Foo": {Default: true},
	}); err != nil {
		t.Fatal(err)
	}

	a := &clientAdapter{fg}
	if !a.Enabled("Foo") {
		t.Error("expected Enabled(\"Foo\") to return true")
	}
	var r interface{}
	func() {
		defer func() {
			r = recover()
		}()
		a.Enabled("Bar")
	}()
	if r == nil {
		t.Error("expected Enabled(\"Bar\") to panic due to unknown feature name")
	}
}

func TestClientAdapterAdd(t *testing.T) {
	fg := featuregate.NewFeatureGate()
	a := &clientAdapter{fg}
	defaults := fg.GetAll()
	if err := a.Add(map[clientfeatures.Feature]clientfeatures.FeatureSpec{
		"FeatureAlpha":      {PreRelease: clientfeatures.Alpha, Default: true},
		"FeatureBeta":       {PreRelease: clientfeatures.Beta, Default: false},
		"FeatureGA":         {PreRelease: clientfeatures.GA, Default: true, LockToDefault: true},
		"FeatureDeprecated": {PreRelease: clientfeatures.Deprecated, Default: false, LockToDefault: true},
	}); err != nil {
		t.Fatal(err)
	}
	all := fg.GetAll()
	allexpected := map[featuregate.Feature]featuregate.FeatureSpec{
		"FeatureAlpha":      {PreRelease: featuregate.Alpha, Default: true},
		"FeatureBeta":       {PreRelease: featuregate.Beta, Default: false},
		"FeatureGA":         {PreRelease: featuregate.GA, Default: true, LockToDefault: true},
		"FeatureDeprecated": {PreRelease: featuregate.Deprecated, Default: false, LockToDefault: true},
	}
	for name, spec := range defaults {
		allexpected[name] = spec
	}
	if len(all) != len(allexpected) {
		t.Errorf("expected %d registered features, got %d", len(allexpected), len(all))
	}
	for name, expected := range allexpected {
		actual, ok := all[name]
		if !ok {
			t.Errorf("expected feature %q not found", name)
			continue
		}
		if diff := cmp.Diff(actual, expected, cmpopts.IgnoreFields(featuregate.FeatureSpec{}, "Version", "MinCompatibilityVersion")); diff != "" {
			t.Errorf("expected feature %q spec %#v, got spec %#v", name, expected, actual)
		}
	}

	var r interface{}
	func() {
		defer func() {
			r = recover()
		}()
		_ = a.Add(map[clientfeatures.Feature]clientfeatures.FeatureSpec{
			"FeatureAlpha": {PreRelease: "foobar"},
		})
	}()
	if r == nil {
		t.Error("expected panic when adding feature with unknown prerelease")
	}
}

func TestClientAdapterAddVersioned(t *testing.T) {
	fg := featuregate.NewVersionedFeatureGate(version.MustParse("1.29"))
	a := &clientAdapter{fg}
	defaults := fg.GetAllVersioned()

	clientFeatures := map[clientfeatures.Feature]clientfeatures.VersionedSpecs{
		"FeatureA": {
			{Version: version.MustParse("1.28"), PreRelease: clientfeatures.Beta, Default: true},
		},
		"FeatureB": {
			{Version: version.MustParse("1.28"), PreRelease: clientfeatures.Alpha, Default: false},
			{Version: version.MustParse("1.30"), PreRelease: clientfeatures.Beta, Default: true},
		},
	}

	if err := a.AddVersioned(clientFeatures); err != nil {
		t.Fatal(err)
	}
	all := fg.GetAllVersioned()
	allexpected := map[featuregate.Feature]featuregate.VersionedSpecs{
		"FeatureA": {
			{Version: version.MustParse("1.28"), PreRelease: featuregate.Beta, Default: true},
		},
		"FeatureB": {
			{Version: version.MustParse("1.28"), PreRelease: featuregate.Alpha, Default: false},
			{Version: version.MustParse("1.30"), PreRelease: featuregate.Beta, Default: true},
		},
	}
	for name, spec := range defaults {
		allexpected[name] = spec
	}
	if len(all) != len(allexpected) {
		t.Errorf("expected %d registered features, got %d", len(allexpected), len(all))
	}
	for name, expected := range allexpected {
		actual, ok := all[name]
		if !ok {
			t.Errorf("expected feature %q not found", name)
			continue
		}
		if diff := cmp.Diff(actual, expected, cmpopts.IgnoreFields(featuregate.FeatureSpec{}, "Version")); diff != "" {
			t.Errorf("feature %q spec mismatch (-want +got):\n%s", name, diff)
		}
	}

	var r interface{}
	func() {
		defer func() {
			r = recover()
		}()
		_ = a.AddVersioned(map[clientfeatures.Feature]clientfeatures.VersionedSpecs{
			"FeaturePanic": {{PreRelease: "foobar"}},
		})
	}()
	if r == nil {
		t.Error("expected panic when adding feature with unknown prerelease")
	}
	if !a.Enabled("FeatureA") {
		t.Error("expected Enabled(\"FeatureA\") to return true")
	}
	if a.Enabled("FeatureB") {
		t.Error("expected Enabled(\"FeatureB\") to return false")
	}
}
