"""
Tests for `funkycache.functions`.
"""

from pathlib import Path
import subprocess
import textwrap


def test_canonical_name_varies_by_path(tmp_path: Path) -> None:
    """
    The canonical name of functions in the top-level of a script
    varies by path.

    For these functions, func.__module__ is __main__, so we need a way
    to distinguish them to avoid cache collisions.
    """
    (tmp_path / "s1.py").write_text(
        textwrap.dedent("""
        from funkycache.functions import canonical_func_name

        def greet(name: str) -> str:
            return f"Hello {name}!"

        print(canonical_func_name(greet))
    """)
    )

    (tmp_path / "s2.py").write_text(
        textwrap.dedent("""
        from funkycache.functions import canonical_func_name

        def greet(name: str) -> str:
            return f"Hello {name}!"

        print(canonical_func_name(greet))
    """)
    )

    name1 = subprocess.check_output(["python3", (tmp_path / "s1.py")])
    name2 = subprocess.check_output(["python3", (tmp_path / "s2.py")])

    assert name1 != name2
