// Copyright 2025 The zb Authors
// SPDX-License-Identifier: MIT

package uuid8

import (
	"testing"

	"github.com/google/uuid"
)

var tests = []struct {
	b    []byte
	want uuid.UUID
}{
	{
		b:    nil,
		want: uuid.UUID{6: 0x80, 8: 0x80},
	},
	{
		b:    []byte{0xde, 0xad, 0xbe, 0xef},
		want: uuid.UUID{0xde, 0xad, 0xbe, 0xef, 6: 0x80, 8: 0x80},
	},
	{
		b: []byte{
			0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xfa, 0xde,
			0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
		},
		want: uuid.UUID{
			0x62, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0x8f, 0xad,
			0xb8, 0x48, 0xd1, 0x59, 0xe2, 0x42, 0xaf, 0x37,
		},
	},
	{
		b: []byte{
			0xde, 0xad, 0xbe, 0xef, 0xca, 0xfe, 0xfa, 0xde,
			0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
			0xa5, 0xa5, 0xa5, 0xa5,
		},
		want: uuid.UUID{
			0x60, 0x3b, 0x28, 0x79, 0x5e, 0xfe, 0x8f, 0xad,
			0xb8, 0x48, 0xd1, 0x59, 0xe2, 0x42, 0xaf, 0x37,
		},
	},
}

func TestFromBytes(t *testing.T) {
	for _, test := range tests {
		if got := FromBytes(test.b); got != test.want {
			t.Errorf("FromBytes(%#x)\n   = %v;\nwant %v", test.b, got, test.want)
		}
	}
}

func FuzzFromBytes(f *testing.F) {
	for _, test := range tests {
		f.Add(test.b)
	}

	f.Fuzz(func(t *testing.T, b []byte) {
		got := FromBytes(b)
		if got, want := got.Version(), uuid.Version(8); got != want {
			t.Errorf("version = %d; want %d", got, want)
		}
		if got, want := got.Variant(), uuid.RFC4122; got != want {
			t.Errorf("variant = %v; want %v", got, want)
		}
	})
}
