package knife

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestWorkerDegraded(t *testing.T) {
	testTables := []struct {
		name       string
		workerName string
		conf       DegradationConfig
		result     bool
	}{
		{
			name:       "disabled",
			workerName: "w1",
			conf: DegradationConfig{
				Enable:          false,
				DisabledWorkers: []string{"w1"},
			},
			result: false,
		},
		{
			name:       "missmatch worker name",
			workerName: "w1",
			conf: DegradationConfig{
				Enable:          true,
				DisabledWorkers: []string{"w2"},
			},
			result: false,
		},
		{
			name:       "matched",
			workerName: "w1",
			conf: DegradationConfig{
				Enable:          true,
				DisabledWorkers: []string{"w1"},
			},
			result: true,
		},
	}

	for _, each := range testTables {
		t.Run(each.name, func(t *testing.T) {
			r1 := WorkerDegraded(each.workerName, each.conf)
			assert.Equal(t, each.result, r1)
		})
	}
}

func TestMethodDegraded(t *testing.T) {
	testTables := []struct {
		name       string
		methodName string
		conf       DegradationConfig
		result     bool
	}{
		{
			name:       "disabled",
			methodName: "method1",
			conf: DegradationConfig{
				Enable:             false,
				DisabledRPCMethods: []string{"method1"},
			},
			result: false,
		},
		{
			name:       "missmatch worker name",
			methodName: "method1",
			conf: DegradationConfig{
				Enable:             true,
				DisabledRPCMethods: []string{"method2"},
			},
			result: false,
		},
		{
			name:       "matched",
			methodName: "method1",
			conf: DegradationConfig{
				Enable:             true,
				DisabledRPCMethods: []string{"method1"},
			},
			result: true,
		},
	}

	for _, each := range testTables {
		t.Run(each.name, func(t *testing.T) {
			r1 := RPCMethodDegraded(each.methodName, each.conf)
			assert.Equal(t, each.result, r1)
		})
	}
}
