/*
 * Copyright 2024 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 streaming

import (
	"errors"
	"testing"

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

func TestUnaryCompatibleMiddleware(t *testing.T) {
	t.Run("unary+allow", func(t *testing.T) {
		test.Assert(t, UnaryCompatibleMiddleware(serviceinfo.StreamingUnary, true))
	})
	t.Run("unary+not-allow", func(t *testing.T) {
		test.Assert(t, !UnaryCompatibleMiddleware(serviceinfo.StreamingUnary, false))
	})
	t.Run("not-unary+allow", func(t *testing.T) {
		test.Assert(t, UnaryCompatibleMiddleware(serviceinfo.StreamingNone, true))
	})
	t.Run("not-unary+not-allow", func(t *testing.T) {
		test.Assert(t, !UnaryCompatibleMiddleware(serviceinfo.StreamingNone, false))
	})
}

type mockStream struct {
	Stream
}

type mockStreamWithDoFinish struct {
	Stream
	doFinish func(error)
}

func (m *mockStreamWithDoFinish) DoFinish(err error) {
	m.doFinish(err)
}

func TestFinishStream(t *testing.T) {
	t.Run("implemented-do-finish", func(t *testing.T) {
		mockErr := errors.New("mock")
		called := false
		s := &mockStreamWithDoFinish{
			doFinish: func(err error) {
				called = true
				test.Assert(t, err == mockErr)
			},
		}
		FinishStream(s, mockErr)
		test.Assert(t, called)
	})
	t.Run("not-implemented-do-finish", func(t *testing.T) {
		s := &mockStream{}
		FinishStream(s, nil)
	})
}
