package htrutils_test

import (
	"testing"

	"github.com/prysmaticlabs/go-bitfield"
	"github.com/prysmaticlabs/prysm/shared/hashutil"
	"github.com/prysmaticlabs/prysm/shared/htrutils"
	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
	"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

func TestBitlistRoot(t *testing.T) {
	hasher := hashutil.CustomSHA256Hasher()
	capacity := uint64(10)
	bfield := bitfield.NewBitlist(capacity)
	expected := [32]byte{176, 76, 194, 203, 142, 166, 117, 79, 148, 194, 231, 64, 60, 245, 142, 32, 201, 2, 58, 152, 53, 12, 132, 40, 41, 102, 224, 189, 103, 41, 211, 202}

	result, err := htrutils.BitlistRoot(hasher, bfield, capacity)
	require.NoError(t, err)
	assert.Equal(t, expected, result)
}

func TestBitwiseMerkleize(t *testing.T) {
	hasher := hashutil.CustomSHA256Hasher()
	chunks := [][]byte{
		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
		{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
	}
	count := uint64(2)
	limit := uint64(2)
	expected := [32]byte{194, 32, 213, 52, 220, 127, 18, 240, 43, 151, 19, 79, 188, 175, 142, 177, 208, 46, 96, 20, 18, 231, 208, 29, 120, 102, 122, 17, 46, 31, 155, 30}

	result, err := htrutils.BitwiseMerkleize(hasher, chunks, count, limit)
	require.NoError(t, err)
	assert.Equal(t, expected, result)
}

func TestBitwiseMerkleizeOverLimit(t *testing.T) {
	hasher := hashutil.CustomSHA256Hasher()
	chunks := [][]byte{
		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
		{11, 12, 13, 14, 15, 16, 17, 18, 19, 20},
	}
	count := uint64(2)
	limit := uint64(1)
	expected := "merkleizing list that is too large, over limit"

	_, err := htrutils.BitwiseMerkleize(hasher, chunks, count, limit)
	assert.ErrorContains(t, expected, err)
}

func TestBitwiseMerkleizeArrays(t *testing.T) {
	hasher := hashutil.CustomSHA256Hasher()
	chunks := [][32]byte{
		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
		{33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 62, 63, 64},
	}
	count := uint64(2)
	limit := uint64(2)
	expected := [32]byte{138, 81, 210, 194, 151, 231, 249, 241, 64, 118, 209, 58, 145, 109, 225, 89, 118, 110, 159, 220, 193, 183, 203, 124, 166, 24, 65, 26, 160, 215, 233, 219}

	result, err := htrutils.BitwiseMerkleizeArrays(hasher, chunks, count, limit)
	require.NoError(t, err)
	assert.Equal(t, expected, result)
}

func TestBitwiseMerkleizeArraysOverLimit(t *testing.T) {
	hasher := hashutil.CustomSHA256Hasher()
	chunks := [][32]byte{
		{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
		{33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 62, 62, 63, 64},
	}
	count := uint64(2)
	limit := uint64(1)
	expected := "merkleizing list that is too large, over limit"

	_, err := htrutils.BitwiseMerkleizeArrays(hasher, chunks, count, limit)
	assert.ErrorContains(t, expected, err)
}

func TestPack(t *testing.T) {
	byteSlice2D := [][]byte{
		{1, 2, 3, 4, 5, 6, 7, 8, 9},
		{1, 1, 2, 3, 5, 8, 13, 21, 34},
	}
	expected := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 1, 2, 3, 5, 8, 13, 21, 34, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

	result, err := htrutils.Pack(byteSlice2D)
	require.NoError(t, err)
	assert.Equal(t, len(expected), len(result[0]))
	for i, v := range expected {
		assert.DeepEqual(t, v, result[0][i])
	}
}

func TestMixInLength(t *testing.T) {
	byteSlice := [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
	length := []byte{1, 2, 3}
	expected := [32]byte{105, 60, 167, 169, 197, 220, 122, 99, 59, 14, 250, 12, 251, 62, 135, 239, 29, 68, 140, 1, 6, 36, 207, 44, 64, 221, 76, 230, 237, 218, 150, 88}
	result := htrutils.MixInLength(byteSlice, length)
	assert.Equal(t, expected, result)
}
