package bizerr

// FIXME(iven): Move logid to message header
// import (
// 	"context"
// 	"fmt"
// 	"testing"
//
// 	. "github.com/smartystreets/goconvey/convey"
// 	"github.com/tidbcloud/idl/pbgen/proto/base"
// 	pbutil "github.com/tidbcloud/idl/pbgen/util"
// 	"github.com/tidbcloud/pkgv2/bizerr"
// 	"google.golang.org/grpc"
// )
//
// type ReqWithBaseReq struct {
// 	BaseReq *base.BaseReq
// 	Foo     string `protobuf:"bytes,2,opt,name=foo,proto3" json:"foo,omitempty"`
// }
//
// type RespWithBaseResp struct {
// 	BaseResp *base.BaseResp
// 	Foo      string `protobuf:"bytes,2,opt,name=foo,proto3" json:"foo,omitempty"`
// }
//
// func TestUnaryServerInterceptor(t *testing.T) {
// 	Convey("TestUnaryServerInterceptor", t, func() {
// 		ctx := context.Background()
// 		req := &ReqWithBaseReq{}
// 		resp := 1
// 		fn := UnaryServerInterceptor(func(resp interface{}, err bizerr.BizError) (resp_ interface{}, err_ error) {
// 			pbutil.SetCodeMsg(resp, int64(err.ErrCode()), err.ErrMsg())
// 			return resp, nil
// 		})
// 		handler := grpc.UnaryHandler(func(ctx context.Context, req interface{}) (interface{}, error) {
// 			return resp, nil
// 		})
//
// 		Convey("normal case", func() {
// 			r1, err := fn(ctx, req, &grpc.UnaryServerInfo{}, handler)
// 			So(err, ShouldBeNil)
// 			So(r1, ShouldEqual, resp)
// 		})
// 		Convey("should set code and msg if err is BizError and resp is not nil", func() {
// 			handler = grpc.UnaryHandler(func(ctx context.Context, req interface{}) (interface{}, error) {
// 				return &RespWithBaseResp{}, bizerr.New(bizerr.ErrCodeParams, "this is message")
// 			})
//
// 			r1, err := fn(ctx, req, &grpc.UnaryServerInfo{}, handler)
// 			So(err, ShouldBeNil)
// 			So(r1.(*RespWithBaseResp).BaseResp.ErrCode, ShouldEqual, bizerr.ErrCodeParams)
// 			So(r1.(*RespWithBaseResp).BaseResp.ErrMsg, ShouldEqual, "this is message")
// 		})
// 		Convey("should do nothing if err is BizError but resp is  nil", func() {
// 			handler = grpc.UnaryHandler(func(ctx context.Context, req interface{}) (interface{}, error) {
// 				return nil, bizerr.New(bizerr.ErrCodeParams, "this is message")
// 			})
//
// 			r1, err := fn(ctx, req, &grpc.UnaryServerInfo{}, handler)
// 			So(err, ShouldNotBeNil)
// 			So(r1, ShouldBeNil)
// 		})
// 		Convey("should set code and msg after redact if err is BizError with exception level", func() {
// 			handler = grpc.UnaryHandler(func(ctx context.Context, req interface{}) (interface{}, error) {
// 				return &RespWithBaseResp{}, bizerr.New(bizerr.ErrCodeDB, "this is message").Escalate()
// 			})
//
// 			r1, err := fn(ctx, req, &grpc.UnaryServerInfo{}, handler)
// 			So(err, ShouldBeNil)
// 			So(r1.(*RespWithBaseResp).BaseResp.ErrCode, ShouldEqual, bizerr.ErrCodeDB+100000000) // TODO(chenfei): convertxx is required to expose
// 			So(r1.(*RespWithBaseResp).BaseResp.ErrMsg, ShouldEqual, "Internal Error")
// 		})
// 		Convey("should do nothing if err is error", func() {
// 			handler = grpc.UnaryHandler(func(ctx context.Context, req interface{}) (interface{}, error) {
// 				return &RespWithBaseResp{}, fmt.Errorf("abc")
// 			})
//
// 			r1, err := fn(ctx, req, &grpc.UnaryServerInfo{}, handler)
// 			So(err, ShouldNotBeNil)
// 			So(r1, ShouldResemble, &RespWithBaseResp{})
// 		})
// 	})
// }
