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

import (
	"reflect"
	"testing"

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

func Test_newVersion(t *testing.T) {
	testcases := []struct {
		verStr        string
		parts         []int
		preRelease    string
		buildMetadata string
		expectErr     bool
	}{
		{
			verStr: "v1.0.0",
			parts:  []int{1, 0, 0},
		},
		{
			verStr: "v12.34.56",
			parts:  []int{12, 34, 56},
		},
		{
			verStr:        "v1.0.0-rc1.0.12345678901234-abcd12345678+build.1.0.0",
			parts:         []int{1, 0, 0},
			preRelease:    "rc1.0.12345678901234-abcd12345678",
			buildMetadata: "build.1.0.0",
		},
		{
			verStr:     "v1.0.0-12345678901234-abcd12345678",
			parts:      []int{1, 0, 0},
			preRelease: "12345678901234-abcd12345678",
		},
		{
			verStr:    "1.0.0",
			expectErr: true,
		},
	}
	for _, tc := range testcases {
		ver, err := newVersion(tc.verStr)
		if !tc.expectErr {
			test.Assert(t, err == nil, err)
			test.Assertf(t, reflect.DeepEqual(tc.parts, ver.parts), "want %v, got %v", tc.parts, ver.parts)
		} else {
			test.Assert(t, err != nil)
		}

	}
}

func TestVersion_lessThan(t *testing.T) {
	testcases := []struct {
		base     string
		other    string
		expected bool
	}{
		{
			base:     "v0.0.0",
			other:    "v0.0.1",
			expected: true,
		},
		{
			base:     "v0.0.0",
			other:    "v0.1.0",
			expected: true,
		},
		{
			base:     "v0.0.0",
			other:    "v1.0.0",
			expected: true,
		},
		{
			base:     "v1.0.0",
			other:    "v0.0.0",
			expected: false,
		},
		{
			base:     "v1.0.0-rc1.0.12345678901233-abcd12345678",
			other:    "v1.0.0",
			expected: true,
		},
		{
			base:     "v1.0.0",
			other:    "v1.0.0",
			expected: false,
		},
		{
			base:     "v0.2.1",
			other:    "v1.0.0",
			expected: true,
		},
	}

	for _, tc := range testcases {
		base, err := newVersion(tc.base)
		test.Assert(t, err == nil, err)
		other, err := newVersion(tc.other)
		test.Assert(t, err == nil, err)
		test.Assert(t, base.lessThan(other) == tc.expected)
	}
}
