// 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 thriftgo

import (
	"io"
	"reflect"
	"strings"
	"testing"

	"github.com/cloudwego/kitex/internal/test"

	"github.com/cloudwego/thriftgo/config"
)

func Test_loadIDLRefConfig(t *testing.T) {
	testCases := []struct {
		desc     string
		reader   io.Reader
		expected func(t *testing.T, res *config.RawConfig)
	}{
		{
			desc: "normal idl-ref file",
			reader: strings.NewReader(`ref:
    decimal.thrift: dubbo-demo/decimal
    java.thrift: github.com/kitex-contrib/codec-dubbo/java
debug: false`),
			expected: func(t *testing.T, res *config.RawConfig) {
				test.Assert(t, reflect.DeepEqual(res, &config.RawConfig{
					Ref: map[string]interface{}{
						"decimal.thrift": "dubbo-demo/decimal",
						"java.thrift":    "github.com/kitex-contrib/codec-dubbo/java",
					},
					Debug: false,
				}))
			},
		},
		{
			desc:   "empty idl-ref file",
			reader: strings.NewReader(""),
			expected: func(t *testing.T, res *config.RawConfig) {
				test.Assert(t, len(res.Ref) == 0, res.Ref)
				test.Assert(t, res.Debug == false, res.Debug)
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.desc, func(t *testing.T) {
			res, _ := loadIDLRefConfig("", tc.reader)
			tc.expected(t, res)
		})
	}
}
