// KEKS -- Go KEKS codec implementation
// Copyright (C) 2024-2025 Anton Rudenko <rudenko.ad@phystech.edu>
//                         Sergey Matveev <stargrave@stargrave.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program.  If not, see <http://www.gnu.org/licenses/>.

package keks

import (
	"bytes"
	"io"
	"math/rand/v2"
	"slices"
	"testing"
	"testing/quick"
)

func TestListEmpty(t *testing.T) {
	obj := []any{}
	bin := []byte{0x08, 0x00}
	decoder := NewDecoderFromBytes(append(bin, Junk...), nil)
	decoded, err := decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	encoded, err := EncodeBuf(obj, nil)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(encoded, bin) {
		t.Fatal("encoded differs")
	}
	casted, ok := decoded.([]any)
	if !ok {
		t.Fatal("failed to cast")
	}
	if !slices.Equal(obj, casted) {
		t.Fatal("casted differs")
	}
	if !bytes.Equal(decoder.B, Junk) {
		t.Fatal("tail differs")
	}
}

func TestListNoEOC(t *testing.T) {
	encoded, err := EncodeBuf([]int{123}, nil)
	if err != nil {
		t.Fatal(err)
	}
	_, err = NewDecoderFromBytes(encoded[:len(encoded)-1], nil).Decode()
	if err != io.ErrUnexpectedEOF {
		t.Fatal(err)
	}
}

func TestListMaxContLen(t *testing.T) {
	l := 2 + rand.UintN(8)
	bin, err := EncodeBuf(make([]int, l), nil)
	if err != nil {
		t.Fatal(err)
	}
	decoder := NewDecoderFromBytes(bin, nil)
	_, err = decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	decoder = NewDecoderFromBytes(bin, &DecodeOpts{MaxContLen: int64(l) + 1})
	_, err = decoder.Decode()
	if err != nil {
		t.Fatal(err)
	}
	decoder = NewDecoderFromBytes(bin, &DecodeOpts{MaxContLen: int64(l)})
	_, err = decoder.Decode()
	if err != ErrLenTooBig {
		t.Fatal(err)
	}
}

func TestListIntSymmetric(t *testing.T) {
	f := func(vs []int64) bool {
		encoded, err := EncodeBuf(vs, nil)
		if err != nil {
			t.Fatal(err)
		}
		decoder := NewDecoderFromBytes(append(encoded, Junk...), nil)
		decoded, err := decoder.Decode()
		if err != nil {
			t.Fatal(err)
		}
		casted, ok := decoded.([]any)
		if !ok {
			t.Fatal("failed to cast")
		}
		if len(vs) != len(casted) {
			return false
		}
		for i := range len(vs) {
			switch v := casted[i].(type) {
			case uint64:
				if v != uint64(vs[i]) {
					return false
				}
			case int64:
				if v != int64(vs[i]) {
					return false
				}
			default:
				return false
			}
		}
		return true
	}
	if err := quick.Check(f, nil); err != nil {
		t.Fatal(err)
	}
}

func TestListTooDeep(t *testing.T) {
	if _, err := NewDecoderFromBytes(
		bytes.Repeat([]byte{byte(AtomList)}, 1000),
		nil).Decode(); err != ErrTooDeep {
		t.Fatal(err)
	}
}
