package console

import (
	"testing"

	"github.com/stretchr/testify/require"
	dataflow "github.com/tidbcloud/dataflow-service/idl/pbgen/proto/dataflow"
)

func TestConvertClusterComponentsForRestoreMapsTiKVDataStoreIops(t *testing.T) {
	components := (&backupControllerImpl{}).convertClusterComponents(&dataflow.BackupRestoreReq{
		Components: []*dataflow.SimpleClusterComponent{
			{
				ComponentType:   dataflow.ComponentType_COMPONENT_TYPE_TIKV,
				NodeProfileName: "8C32G",
				Replicas:        3,
				StorageSizeGi:   200,
				StorageType:     dataflow.SimpleClusterComponent_Plus,
				DataStoreIops:   12000,
			},
		},
	})

	require.NotNil(t, components.Tikv)
	require.Equal(t, int32(12000), components.Tikv.DataStoreIops)
}

func TestSimpleClusterComponentDataStoreIopsValidation(t *testing.T) {
	tests := map[string]struct {
		dataStoreIops int32
		wantErr       bool
	}{
		"zero is accepted when IOPS is omitted": {
			dataStoreIops: 0,
		},
		"positive IOPS is accepted": {
			dataStoreIops: 12000,
		},
		"negative IOPS is rejected": {
			dataStoreIops: -1,
			wantErr:       true,
		},
	}

	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			req := &dataflow.SimpleClusterComponent{
				ComponentType:   dataflow.ComponentType_COMPONENT_TYPE_TIKV,
				NodeProfileName: "8C32G",
				Replicas:        3,
				StorageSizeGi:   200,
				DataStoreIops:   tc.dataStoreIops,
			}

			if tc.wantErr {
				require.Error(t, req.Validate())
				return
			}
			require.NoError(t, req.Validate())
		})
	}
}
