package tmobile

import (
	"testing"

	"github.com/pkg/errors"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
)

func Test_errorToGRPCError(t *testing.T) {
	type args struct {
		err error
	}
	tests := []struct {
		name    string
		args    args
		wantErr error
	}{
		{
			name: "nil",
			args: args{
				err: nil,
			},
			wantErr: nil,
		},
		{
			name: "ErrCreateRequest",
			args: args{
				err: ErrCreateRequest,
			},
			wantErr: status.Errorf(codes.Internal, "failed to create request"),
		},
		{
			name: "ErrDoRequest",
			args: args{
				err: ErrDoRequest,
			},
			wantErr: status.Errorf(codes.Internal, "failed to do request"),
		},
		{
			name: "ErrReadResponseBody",
			args: args{
				err: ErrReadResponseBody,
			},
			wantErr: status.Errorf(codes.Internal, "failed to read response body"),
		},
		{
			name: "ErrBadStatusCode",
			args: args{
				err: ErrBadStatusCode,
			},
			wantErr: status.Errorf(codes.Internal, "bad status code"),
		},
		{
			name: "ErrJSONMarshal",
			args: args{
				err: ErrJSONMarshal,
			},
			wantErr: status.Errorf(codes.Internal, "json marshal & unmarshal error"),
		},
		{
			name: "ErrTokenGen",
			args: args{
				err: ErrTokenGen,
			},
			wantErr: status.Errorf(codes.Internal, "failed to generate token"),
		},
		{
			name: "ErrBadMsgStatus",
			args: args{
				err: ErrBadMsgStatus,
			},
			wantErr: status.Errorf(codes.Internal, "bad message status"),
		},
		{
			name: "ErrTimeoutSendingMessage",
			args: args{
				err: ErrTimeoutSendingMessage,
			},
			wantErr: status.Errorf(codes.DeadlineExceeded, "timeout sending message"),
		},

		{
			name: "ErrUnknown",
			args: args{
				err: errors.New("unknown error"),
			},
			wantErr: status.Errorf(codes.Unknown, "unknown error: unknown error"),
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			err := ErrorToGRPCError(tt.args.err)
			if err != nil {
				if err.Error() != tt.wantErr.Error() {
					t.Errorf("errorToGRPCError() error = %v, wantErr %v", err, tt.wantErr)
				}
			} else if err != tt.wantErr {
				t.Errorf("errorToGRPCError() error = %v, wantErr %v", err, tt.wantErr)

			}
		})
	}
}
