package service

import (
	"reflect"
	"testing"

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

	apiv1 "github.com/tidbcloud/infra-provider/api/tidbcloud/cluster-agent/v1"
)

func TestAgentService_buildClusterTopology(t *testing.T) {

	type args struct {
		tc *v1alpha1.TidbCluster
	}
	tests := []struct {
		name    string
		args    args
		want    *apiv1.GetClusterTopologyResp
		wantErr bool
	}{
		{
			name: "1 pd 1 tikv",
			args: args{
				buildTestTC(tcName, testNS, true, func(tc *v1alpha1.TidbCluster) {
					tc.Status.PD.Members = map[string]v1alpha1.PDMember{
						"db-pd-0": {
							Name: "db-pd-0",
						},
					}
					tc.Status.TiKV.Stores = map[string]v1alpha1.TiKVStore{
						"1": {
							PodName: "db-tikv-0",
							IP:      "db-tikv-0.db-tikv-peer.tidb12345.svc",
						},
					}
				}),
			},
			want: &apiv1.GetClusterTopologyResp{
				PdNodes: []*apiv1.GetClusterTopologyResp_PDNodeInfo{
					{
						Host: "db-pd-0.db-pd-peer.tidb12345.svc",
						Name: "db-pd-0",
						Port: 2379,
					},
				},
				TikvNodes: []*apiv1.GetClusterTopologyResp_TiKVNodeInfo{
					{
						Host: "db-tikv-0.db-tikv-peer.tidb12345.svc",
						Name: "db-tikv-0",
						Port: 20160,
					},
				},
			},
			wantErr: false,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			svc := NewAgentService(nil)
			got := svc.buildClusterTopology(tt.args.tc)

			if !reflect.DeepEqual(got, tt.want) {
				t.Errorf("GetClusterTopology() got = %v, want %v", got, tt.want)
			}
		})
	}
}
