package slprober

import (
	"testing"

	tidbClusterV1alpha1 "github.com/pingcap/tidb-operator/pkg/apis/pingcap/v1alpha1"
)

func TestGetClusterIDFromNamespace(t *testing.T) {
	tests := []struct {
		name      string
		namespace string
		want      string
	}{
		{
			name:      "should be ok",
			namespace: "tidb123",
			want:      "123",
		},
		{
			name:      "should be ok",
			namespace: "tidb",
			want:      "",
		},
		{
			name:      "should return empty",
			namespace: "111",
			want:      "",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			got := getClusterIDFromNamespace(tt.namespace)
			if got != tt.want {
				t.Errorf("getClusterIDFromNamespace() want=%s got=%s", tt.want, got)
			}
		})
	}
}

func TestIsTiCDCTC(t *testing.T) {
	tests := []struct {
		name    string
		tcName  string
		want    bool
		wantErr bool
	}{
		{
			name:   "should be ok",
			tcName: "rg1-op1-c1",
			want:   true,
		},
		{
			name:   "should be ok",
			tcName: "rg1-op1-c1-abcd",
			want:   true,
		},
		{
			name:   "should be ok",
			tcName: "changefeed-1",
			want:   true,
		},
		{
			name:   "should be ok",
			tcName: "ticdc-1",
			want:   true,
		},
		{
			name:   "should be false",
			tcName: "ticdc-1-abcd",
			want:   false,
		},
		{
			name:   "should be false",
			tcName: "db-1901897336590700544-e0e452ea",
			want:   false,
		},
		{
			name:   "should be false",
			tcName: "db",
			want:   false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tc := &tidbClusterV1alpha1.TidbCluster{}
			tc.Name = tt.tcName
			got, err := IsTiCDCTC(tc)
			if (err != nil) != tt.wantErr {
				t.Errorf("IsTiCDCTC() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if got != tt.want {
				t.Errorf("IsTiCDCTC() got = %v, want %v", got, tt.want)
			}
		})
	}
}
