/*
 * SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
 * SPDX-License-Identifier: AGPL-3.0-only
 */

package uuid

import (
	"strings"
	"testing"
)

type test struct {
	in     string
	isUuid bool
	bytes  [16]byte
}

var tests = []test{
	{"f47ac10b-58cc-0372-8567-0e02b2c3d479", true, [16]byte{0xf4, 0x7a, 0xc1, 0x0b, 0x58, 0xcc, 0x03, 0x72, 0x85, 0x67, 0x0e, 0x02, 0xb2, 0xc3, 0xd4, 0x79}},
	{"2bc1be74-169d-4300-a239-49a1196a045d", true, [16]byte{0x2b, 0xc1, 0xbe, 0x74, 0x16, 0x9d, 0x43, 0x00, 0xa2, 0x39, 0x49, 0xa1, 0x19, 0x6a, 0x04, 0x5d}},
	{"12bc1be74-169d-4300-a239-49a1196a045d", false, [16]byte{}},
	{"2bc1be74-169d-4300-a239-49a1196a045", false, [16]byte{}},
	{"2bc1be74-1x9d-4300-a239-49a1196a045d", false, [16]byte{}},
	{"2bc1be74-169d-4300-a239-49a1196ag45d", false, [16]byte{}},
}

func testValid(t *testing.T, in string, isUuid bool) {
	if ok := Valid(in); ok != isUuid {
		t.Errorf("Valid(%s) got %v expected %v", in, ok, isUuid)
	}
}

func testParse(t *testing.T, in string, expected [16]byte, isUuid bool) {
	actual, err := Parse(in)
	if isUuid && err != nil {
		t.Errorf("Parse(%s) unexpected error %v", in, err)
	} else if !isUuid && err == nil {
		t.Errorf("Parse(%s) expected to fail but got %v", in, actual)
	} else if actual != expected {
		t.Errorf("Parse(%s) got %v expected %v", in, actual, expected)
	}
}

func TestUUID(t *testing.T) {
	for _, tt := range tests {
		testValid(t, tt.in, tt.isUuid)
		testValid(t, strings.ToUpper(tt.in), tt.isUuid)
		testParse(t, tt.in, tt.bytes, tt.isUuid)
		testParse(t, strings.ToUpper(tt.in), tt.bytes, tt.isUuid)
	}
}
