npc-clients-python/tests/test_npc_unit.py

131 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Unit tests for NPC chat functionality that don't require API"""
import pytest
import sys
import os
# Add parent directory to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gumyum_npc_client import NPC, NPCChatProxy, SpawnedData
def test_npc_chat_proxy_creation():
"""Test NPCChatProxy is created properly"""
npc = NPC(
npc_id=123456789,
name="Test NPC",
profession="Tester",
personality_type=5,
spawned=SpawnedData(location="test", mood="neutral", stress_level=5),
universe_id="test-universe",
seed=12345,
)
# Should have chat property
assert hasattr(npc, "chat")
# Chat should be NPCChatProxy instance
assert isinstance(npc.chat, NPCChatProxy)
# Should be the same instance on multiple accesses
assert npc.chat is npc.chat
def test_npc_without_client_error():
"""Test that NPC without client raises proper error"""
npc = NPC(
npc_id=123456789,
name="Test NPC",
profession="Tester",
personality_type=5,
spawned=SpawnedData(location="test", mood="neutral", stress_level=5),
universe_id="test-universe",
seed=12345,
)
# Should not have client
assert npc.client is None
# Should raise error when trying to use chat.completions
with pytest.raises(ValueError, match="NPC has no client reference"):
import asyncio
asyncio.run(npc.chat.completions([{"role": "user", "content": "Hello"}]))
# Should raise error when trying to use completions alias
with pytest.raises(ValueError, match="NPC has no client reference"):
import asyncio
asyncio.run(npc.completions([{"role": "user", "content": "Hello"}]))
def test_npc_data_preservation():
"""Test that NPC preserves all its data"""
npc = NPC(
npc_id=999888777,
name="Data Test NPC",
profession="Data Analyst",
personality_type=7,
spawned=SpawnedData(location="lab", mood="focused", stress_level=3),
universe_id="data-universe",
seed=54321,
cached=True,
cache_url="http://cache.example.com/npc/999888777",
)
# Verify all data is preserved
assert npc.npc_id == 999888777
assert npc.name == "Data Test NPC"
assert npc.profession == "Data Analyst"
assert npc.personality_type == 7
assert npc.universe_id == "data-universe"
assert npc.seed == 54321
assert npc.cached == True
assert npc.cache_url == "http://cache.example.com/npc/999888777"
def test_npc_client_field_excluded_from_dict():
"""Test that client field is excluded from serialization"""
from gumyum_npc_client import GumYumClient
npc = NPC(
npc_id=111222333,
name="Serialize Test",
profession="Serializer",
personality_type=3,
spawned=SpawnedData(location="void", mood="neutral", stress_level=5),
universe_id="serial-universe",
seed=11111,
)
# Convert to dict
npc_dict = npc.model_dump()
# Client should not be in the dict
assert "client" not in npc_dict
assert "_chat" not in npc_dict
# Other fields should be present
assert npc_dict["npc_id"] == 111222333
assert npc_dict["name"] == "Serialize Test"
if __name__ == "__main__":
print("Running NPC unit tests...")
# Run tests
test_npc_chat_proxy_creation()
print("✅ test_npc_chat_proxy_creation passed")
test_npc_without_client_error()
print("✅ test_npc_without_client_error passed")
test_npc_data_preservation()
print("✅ test_npc_data_preservation passed")
test_npc_client_field_excluded_from_dict()
print("✅ test_npc_client_field_excluded_from_dict passed")
print("\n✨ All unit tests passed!")