package util

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestRandId(t *testing.T) {
	assert := assert.New(t)
	id, err := RandID()
	assert.NoError(err)
	t.Log(id)
	assert.Equal(16, len(id))
}

func TestRandIDWithRandLen(t *testing.T) {
	tests := []struct {
		name      string
		start     int
		end       int
		expectErr bool
	}{
		{
			name:      "start and end are equal",
			start:     5,
			end:       5,
			expectErr: true,
		},
		{
			name:      "start is less than end",
			start:     5,
			end:       10,
			expectErr: false,
		},
		{
			name:      "start is greater than end",
			start:     10,
			end:       5,
			expectErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert := assert.New(t)
			id, err := RandIDWithRandLen(tt.start, tt.end)
			if tt.expectErr {
				assert.Error(err)
			} else {
				assert.NoError(err)
				assert.GreaterOrEqual(len(id), tt.start)
				assert.Less(len(id), tt.end)
			}
		})
	}
}

func TestGetAuthKey(t *testing.T) {
	assert := assert.New(t)
	key := GetAuthKey("1234", 1488720000)
	assert.Equal("6df41a43725f0c770fd56379e12acf8c", key)
}

func TestParseRangeNumbers(t *testing.T) {
	assert := assert.New(t)
	numbers, err := ParseRangeNumbers("2-5")
	if assert.NoError(err) {
		assert.Equal([]int64{2, 3, 4, 5}, numbers)
	}

	numbers, err = ParseRangeNumbers("1")
	if assert.NoError(err) {
		assert.Equal([]int64{1}, numbers)
	}

	numbers, err = ParseRangeNumbers("3-5,8")
	if assert.NoError(err) {
		assert.Equal([]int64{3, 4, 5, 8}, numbers)
	}

	numbers, err = ParseRangeNumbers(" 3-5,8, 10-12 ")
	if assert.NoError(err) {
		assert.Equal([]int64{3, 4, 5, 8, 10, 11, 12}, numbers)
	}

	_, err = ParseRangeNumbers("3-a")
	assert.Error(err)
}
