"""Unit tests for PKB resources via the in-memory client/server harness."""

from __future__ import annotations

import pytest

from mcp.shared.memory import create_connected_server_and_client_session

from pkb import tools as pkb_tools
from pkb.server import mcp


@pytest.fixture
def fresh_notes(monkeypatch):
    seed = {"hello": "Hello note body", "todo": "Pick up groceries",
            "ideas": "Build an MCP server tutorial"}
    monkeypatch.setattr(pkb_tools, "NOTES", dict(seed))
    return seed


@pytest.mark.asyncio
async def test_about_resource_returns_text():
    async with create_connected_server_and_client_session(mcp._mcp_server) as client:
        result = await client.read_resource("pkb://about")
        # contents is a list of ResourceContents (TextResourceContents here).
        assert any("pkb" in c.text.lower() for c in result.contents)


@pytest.mark.asyncio
async def test_notes_resource_known(fresh_notes):
    async with create_connected_server_and_client_session(mcp._mcp_server) as client:
        result = await client.read_resource("pkb://notes/hello")
        assert any("Hello note body" in c.text for c in result.contents)


@pytest.mark.asyncio
async def test_notes_resource_unknown(fresh_notes):
    async with create_connected_server_and_client_session(mcp._mcp_server) as client:
        with pytest.raises(Exception):  # SDK maps unknown URI to a protocol error
            await client.read_resource("pkb://notes/does-not-exist")
