# KEKS -- Python KEKS implementation
# Copyright (C) 2024-2025 Anton Rudenko <rudenko.ad@phystech.edu>
#               2024-2025 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/>.

from os import urandom
from unittest import TestCase

from hypothesis import given
from hypothesis.strategies import integers

from keks import Blob
from keks import DecodeError
from keks import dumps
from keks import loads
from keks import NotEnoughData
from tests.strategies import junk_st


class TestBlob(TestCase):
    @given(junk_st)
    def test_multiple_of_chunkLen(self, junk: bytes) -> None:
        blob = Blob(4, b"testdata")
        encoded = dumps(blob)
        self.assertSequenceEqual(
            encoded,
            b"".join((
                bytes.fromhex("0B0000000000000003"),
                bytes.fromhex("84"), b"test",
                bytes.fromhex("84"), b"data",
                bytes.fromhex("80"),
            )),
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded.l, blob.l)
        self.assertSequenceEqual(decoded.v, blob.v)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_larger_of_chunkLen(self, junk: bytes) -> None:
        blob = Blob(4, b"testdata2")
        encoded = dumps(blob)
        self.assertSequenceEqual(
            encoded,
            b"".join((
                bytes.fromhex("0B0000000000000003"),
                bytes.fromhex("84"), b"test",
                bytes.fromhex("84"), b"data",
                bytes.fromhex("81"), b"2",
            )),
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded.l, blob.l)
        self.assertSequenceEqual(decoded.v, blob.v)
        self.assertSequenceEqual(tail, junk)

    @given(junk_st)
    def test_empty(self, junk: bytes) -> None:
        blob = Blob(4, b"")
        encoded = dumps(blob)
        self.assertSequenceEqual(
            encoded,
            b"".join((
                bytes.fromhex("0B0000000000000003"),
                bytes.fromhex("80"),
            )),
        )
        decoded, tail = loads(encoded + junk)
        self.assertEqual(decoded.l, 4)
        self.assertSequenceEqual(decoded.v, b"")
        self.assertSequenceEqual(tail, junk)

    @given(
        integers(min_value=1, max_value=10),
        integers(min_value=0, max_value=8),
        junk_st,
    )
    def test_symmetric_multiple(self, chunkLen: int, chunks: int, junk: bytes) -> None:
        chunks = [urandom(chunkLen) for _ in range(chunks)]
        encoded = b"".join((
            b"\x0b",
            (chunkLen-1).to_bytes(8, "big"),
            b"".join(dumps(chunk) for chunk in chunks),
            b"\x80",
            junk,
        ))
        decoded, tail = loads(encoded)
        self.assertEqual(decoded.l, chunkLen)
        self.assertSequenceEqual(decoded.v, b"".join(chunks))
        self.assertSequenceEqual(tail, junk)

    def test_throws_when_not_enough_data(self) -> None:
        encoded = b"".join((
            bytes.fromhex("0B0000000000000003"),
            bytes.fromhex("84"), b"test",
            bytes.fromhex("84"), b"da",
        ))
        with self.assertRaises(NotEnoughData) as err:
            loads(encoded)
        self.assertEqual(err.exception.n, 2)

    def test_throws_when_not_enough_data_for_length(self) -> None:
        encoded = bytes.fromhex("0B00000000")
        with self.assertRaises(NotEnoughData) as err:
            loads(encoded)
        self.assertEqual(err.exception.n, 8-4)

    def test_throws_when_wrong_terminator_length(self) -> None:
        encoded = b"".join((
            bytes.fromhex("0B0000000000000003"),
            bytes.fromhex("84"), b"test",
            bytes.fromhex("84"), b"data",
            bytes.fromhex("8A"), b"terminator",
        ))
        with self.assertRaises(DecodeError) as err:
            loads(encoded)
        self.assertEqual(str(err.exception), "wrong chunk len")

    def test_throws_when_wrong_terminator_tag(self) -> None:
        encoded = b"".join((
            bytes.fromhex("0B0000000000000003"),
            bytes.fromhex("84"), b"test",
            bytes.fromhex("84"), b"data",
            bytes.fromhex("04"), b"that was a wrong tag",
        ))
        with self.assertRaises(DecodeError) as err:
            loads(encoded)
        self.assertEqual(str(err.exception), "unexpected tag")
