/*
 * Copyright 2022 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 nphttp2

import (
	"context"
	"testing"

	"github.com/cloudwego/kitex/internal/test"
	"github.com/cloudwego/kitex/pkg/remote/trans/nphttp2/grpc"
	"github.com/cloudwego/kitex/pkg/serviceinfo"
)

func TestStream(t *testing.T) {
	// init
	opt := newMockServerOption()
	conn := newMockNpConn(mockAddr0)
	conn.mockSettingFrame()
	tr, err := newMockServerTransport(conn)
	test.Assert(t, err == nil, err)
	s := grpc.CreateStream(context.Background(), 1, func(i int) {}, "")
	serverConn := newServerConn(tr, s)
	defer serverConn.Close()

	handler, err := NewSvrTransHandlerFactory().NewTransHandler(opt)
	test.Assert(t, err == nil, err)
	ctx := newMockCtxWithRPCInfo()

	// test newServerStream()
	stream := newServerStream(ctx, &serviceinfo.ServiceInfo{
		PackageName:     "",
		ServiceName:     "",
		HandlerType:     nil,
		Methods:         nil,
		PayloadCodec:    0,
		KiteXGenVersion: "",
		Extra:           nil,
	}, serverConn, handler)

	// test Context()
	strCtx := stream.GetGRPCStream().Context()
	test.Assert(t, strCtx == ctx)

	// test recvMsg()
	msg := newMockNewMessage().Data()
	newMockStreamRecvHelloRequest(s)
	err = stream.RecvMsg(ctx, msg)
	test.Assert(t, err == nil, err)

	// test SendMsg()
	err = stream.SendMsg(ctx, msg)
	test.Assert(t, err == nil, err)

	// test Close()
	err = stream.GetGRPCStream().Close()
	test.Assert(t, err == nil, err)
}
