package domain

import (
	"testing"

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

func TestBizChangefeed_GetConnectorStatus(t *testing.T) {
	tests := map[string]struct {
		operationStatus entity.BizChangefeedOperationStatus
		kernelStatus    entity.ChangefeedKernelStatus
		want            dataflow.Connector_Status
	}{
		"normal uses kernel normal": {
			operationStatus: entity.BizChangefeedOperationStatusNormal,
			kernelStatus:    entity.ChangefeedKernelStatusNormal,
			want:            dataflow.Connector_RUNNING,
		},
		"paused uses business paused": {
			operationStatus: entity.BizChangefeedOperationStatusPaused,
			kernelStatus:    entity.ChangefeedKernelStatusStopped,
			want:            dataflow.Connector_PAUSED,
		},
		"paused with kernel failed exposes error": {
			operationStatus: entity.BizChangefeedOperationStatusPaused,
			kernelStatus:    entity.ChangefeedKernelStatusFailed,
			want:            dataflow.Connector_ERROR,
		},
		"paused with kernel error exposes error": {
			operationStatus: entity.BizChangefeedOperationStatusPaused,
			kernelStatus:    entity.ChangefeedKernelStatusError,
			want:            dataflow.Connector_ERROR,
		},
		"business failed remains failed": {
			operationStatus: entity.BizChangefeedOperationStatusFailed,
			kernelStatus:    entity.ChangefeedKernelStatusNormal,
			want:            dataflow.Connector_FAILED,
		},
	}

	for name, tc := range tests {
		t.Run(name, func(t *testing.T) {
			c := &BizChangefeed{
				OperationStatus:  tc.operationStatus,
				ChangefeedStatus: tc.kernelStatus,
			}
			require.Equal(t, tc.want, c.GetConnectorStatus())
		})
	}
}
