/*
 * Copyright 2021 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package serviceinfo_test

import (
	"context"
	"testing"

	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/serviceinfo"
)

func TestServiceInfo(t *testing.T) {
	si := &serviceinfo.ServiceInfo{
		PackageName: "123",
		Extra: map[string]interface{}{
			"PackageName": "456",
		},
	}
	test.Assert(t, si.GetPackageName() == "123")

	si = &serviceinfo.ServiceInfo{
		Extra: map[string]interface{}{
			"PackageName": "456",
		},
	}
	test.Assert(t, si.GetPackageName() == "456")
}

func TestMethodInfo(t *testing.T) {
	mi := serviceinfo.NewMethodInfo(nil, nil, nil, false)
	test.Assert(t, mi != nil)

	mh := func(ctx context.Context, handler, args, result interface{}) error { return nil }
	af := func() interface{} { return "arg" }
	rf := func() interface{} { return "res" }
	mi = serviceinfo.NewMethodInfo(mh, af, rf, true)
	test.Assert(t, mi != nil)
	test.Assert(t, mi.Handler() != nil)
	test.Assert(t, mi.NewArgs().(string) == "arg")
	test.Assert(t, mi.NewResult().(string) == "res")
	test.Assert(t, mi.OneWay())
}

func TestPayloadCodec(t *testing.T) {
	test.Assert(t, serviceinfo.Thrift.String() == "Thrift")
	test.Assert(t, serviceinfo.Protobuf.String() == "Protobuf")
	test.Panic(t, func() {
		_ = serviceinfo.PayloadCodec(111).String()
	})
}

func TestNewMethodInfoStreaming(t *testing.T) {
	t.Run("no-streaming", func(t *testing.T) {
		mi := serviceinfo.NewMethodInfo(nil, nil, nil, false)
		test.Assert(t, !mi.IsStreaming())
	})
	t.Run("streaming-client", func(t *testing.T) {
		mi := serviceinfo.NewMethodInfo(nil, nil, nil, true,
			serviceinfo.WithStreamingMode(serviceinfo.StreamingClient))
		test.Assert(t, mi.IsStreaming())
		test.Assert(t, mi.StreamingMode() == serviceinfo.StreamingClient)
	})
	t.Run("streaming-unary", func(t *testing.T) {
		mi := serviceinfo.NewMethodInfo(nil, nil, nil, true,
			serviceinfo.WithStreamingMode(serviceinfo.StreamingUnary))
		test.Assert(t, mi.IsStreaming())
		test.Assert(t, mi.StreamingMode() == serviceinfo.StreamingUnary)
	})
	t.Run("streaming-unary-compatible-middleware", func(t *testing.T) {
		mi := serviceinfo.NewMethodInfo(nil, nil, nil, true,
			serviceinfo.WithStreamingMode(serviceinfo.StreamingUnary))
		test.Assert(t, mi.IsStreaming())
		test.Assert(t, mi.StreamingMode() == serviceinfo.StreamingUnary)
	})
}
