"""Tests for trust resolver."""

import pytest

from discourse_toll.trust import TrustResolver, static_resolver


@pytest.mark.asyncio
async def test_static_resolver():
    resolver = static_resolver({"a1": 75, "a2": 30})
    assert await resolver.get_score("a1") == 75
    assert await resolver.get_score("a2") == 30


@pytest.mark.asyncio
async def test_static_resolver_unknown():
    resolver = static_resolver({"a1": 75})
    assert await resolver.get_score("unknown") is None


@pytest.mark.asyncio
async def test_custom_resolver():
    async def _resolve(agent_id: str):
        return 90 if agent_id.startswith("trusted") else 10

    resolver = TrustResolver(resolver=_resolve)
    assert await resolver.get_score("trusted-agent") == 90
    assert await resolver.get_score("unknown") == 10


@pytest.mark.asyncio
async def test_no_resolver():
    resolver = TrustResolver()
    assert await resolver.get_score("anything") is None


@pytest.mark.asyncio
async def test_cache():
    call_count = 0

    async def _resolve(agent_id: str):
        nonlocal call_count
        call_count += 1
        return 50

    resolver = TrustResolver(resolver=_resolve, cache_ttl_s=300)
    await resolver.get_score("a1")
    await resolver.get_score("a1")
    assert call_count == 1  # Second call should be cached
