package crdt

import (
	"testing"

	oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
	"github.com/stretchr/testify/require"
)

func TestObjectVersionsSort(t *testing.T) {
	t.Run("sort by epoch", func(t *testing.T) {
		versions := NewObjectVersions("test")
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 3})
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 1})
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 2})

		last := versions.GetLast()
		require.Equal(t, uint64(3), last.CreationEpoch)
	})

	t.Run("sort by oids", func(t *testing.T) {
		versions := NewObjectVersions("test")
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 3, OjbID: getTestOID(2)})
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 3, OjbID: getTestOID(1)})
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 1, OjbID: getTestOID(3)})
		versions.AppendVersion(&ObjectVersion{CreationEpoch: 2, OjbID: getTestOID(4)})

		last := versions.GetLast()
		require.Equal(t, uint64(3), last.CreationEpoch)
		require.Equal(t, getTestOID(2).String(), last.VersionID())
	})
}

func getTestOID(val byte) oid.ID {
	var res oid.ID
	res.SetSHA256([32]byte{val})
	return res
}