"""
Integration tests for the library.
"""

from datetime import datetime
import time

from pydantic import BaseModel

from funkycache import FunkyCache


def test_long_mutable_key() -> None:
    """
    We can cache a function argument which includes a long key that
    gets reduced during key encoding.

    This is a regression test for a bug in 1.0.1, where the key would be
    mutated when we checked if a value was already cached, which led to
    cache misses and errors about overwriting values.
    """

    class MyType(BaseModel):
        content: str = ""

    cache = FunkyCache(storage="memory")

    @cache.register
    def upcase(tp: MyType) -> str:
        return str(datetime.now())

    t = MyType(content="a" * 2500)

    upcase(t)
    time.sleep(0.1)
    upcase(t)
