package collections

import (
	"context"
	"math"
	"testing"

	"github.com/stretchr/testify/require"

	store "cosmossdk.io/collections/corecompat"
	"cosmossdk.io/collections/internal/testutil"
)

func deps() (store.KVStoreService, context.Context) {
	ctx := testutil.Context()
	kv := testutil.KVStoreService(ctx, "test")
	return kv, ctx
}

func TestPrefix(t *testing.T) {
	t.Run("panics on invalid int", func(t *testing.T) {
		require.Panics(t, func() {
			NewPrefix(math.MaxUint8 + 1)
		})
	})

	t.Run("string", func(t *testing.T) {
		require.Equal(t, []byte("prefix"), NewPrefix("prefix").Bytes())
	})

	t.Run("int", func(t *testing.T) {
		require.Equal(t, []byte{0x1}, NewPrefix(1).Bytes())
	})

	t.Run("[]byte", func(t *testing.T) {
		bytes := []byte("prefix")
		prefix := NewPrefix(bytes)
		require.Equal(t, bytes, prefix.Bytes())
		// assert if modification happen they do not propagate to prefix
		bytes[0] = 0x0
		require.Equal(t, []byte("prefix"), prefix.Bytes())
	})
}
