218 lines
6.3 KiB
GDScript
218 lines
6.3 KiB
GDScript
extends GutTest
|
|
# Unit tests for NPC chat history and serialization features
|
|
|
|
var mock_client
|
|
var test_npc
|
|
|
|
|
|
func before_each():
|
|
# Create a mock client
|
|
mock_client = Node.new()
|
|
|
|
# Create a test NPC with sample data
|
|
var npc_data = {
|
|
"npc_id": 123456789,
|
|
"name": "Test Merchant",
|
|
"profession": "merchant",
|
|
"personality_type": 7,
|
|
"universe_id": "test-universe",
|
|
"seed": 42,
|
|
"spawned": {"location": "market", "mood": "friendly", "stress_level": 3},
|
|
"cached": false,
|
|
"cache_url": ""
|
|
}
|
|
test_npc = GumYumNPC.new(npc_data, mock_client)
|
|
|
|
|
|
func after_each():
|
|
if mock_client:
|
|
mock_client.queue_free()
|
|
|
|
|
|
func test_chat_history_starts_empty():
|
|
assert_eq(test_npc.chat_history.size(), 0, "Chat history should start empty")
|
|
|
|
|
|
func test_chat_history_tracking():
|
|
# Simulate adding messages to history
|
|
test_npc.chat_history.append({"role": "user", "content": "Hello there!"})
|
|
test_npc.chat_history.append({"role": "assistant", "content": "Greetings, traveler!"})
|
|
|
|
assert_eq(test_npc.chat_history.size(), 2, "Should have 2 messages")
|
|
assert_eq(test_npc.chat_history[0]["role"], "user")
|
|
assert_eq(test_npc.chat_history[1]["content"], "Greetings, traveler!")
|
|
|
|
|
|
func test_clear_history():
|
|
# Add some messages
|
|
test_npc.chat_history.append({"role": "user", "content": "Test 1"})
|
|
test_npc.chat_history.append({"role": "assistant", "content": "Response 1"})
|
|
|
|
# Clear history
|
|
test_npc.clear_history()
|
|
|
|
assert_eq(test_npc.chat_history.size(), 0, "History should be empty after clear")
|
|
|
|
|
|
func test_chat_history_to_json():
|
|
# Add test messages
|
|
test_npc.chat_history.append({"role": "user", "content": "What are you selling?"})
|
|
test_npc.chat_history.append({"role": "assistant", "content": "Fine weapons and armor!"})
|
|
|
|
var json_str = test_npc.chat_history_to_json()
|
|
assert_ne(json_str, "", "JSON should not be empty")
|
|
|
|
# Parse it back to verify
|
|
var json = JSON.new()
|
|
var parse_result = json.parse(json_str)
|
|
assert_eq(parse_result, OK, "Should parse valid JSON")
|
|
assert_eq(json.data.size(), 2, "Should have 2 messages")
|
|
assert_eq(json.data[0]["content"], "What are you selling?")
|
|
|
|
|
|
func test_to_dict():
|
|
# Add some chat history
|
|
test_npc.chat_history.append({"role": "user", "content": "Hello"})
|
|
|
|
var dict = test_npc.to_dict()
|
|
|
|
# Verify all fields are present
|
|
assert_has(dict, "npc_id")
|
|
assert_has(dict, "name")
|
|
assert_has(dict, "profession")
|
|
assert_has(dict, "personality_type")
|
|
assert_has(dict, "universe_id")
|
|
assert_has(dict, "seed")
|
|
assert_has(dict, "spawned")
|
|
assert_has(dict, "chat_history")
|
|
|
|
# Verify values
|
|
assert_eq(dict["npc_id"], 123456789)
|
|
assert_eq(dict["name"], "Test Merchant")
|
|
assert_eq(dict["profession"], "merchant")
|
|
assert_eq(dict["chat_history"].size(), 1)
|
|
|
|
|
|
func test_to_json():
|
|
var json_str = test_npc.to_json()
|
|
assert_ne(json_str, "", "JSON should not be empty")
|
|
|
|
# Parse to verify it's valid
|
|
var json = JSON.new()
|
|
var parse_result = json.parse(json_str)
|
|
assert_eq(parse_result, OK, "Should produce valid JSON")
|
|
assert_eq(json.data["name"], "Test Merchant")
|
|
|
|
|
|
func test_from_dict():
|
|
# Create dict with test data
|
|
var test_data = {
|
|
"npc_id": 987654321,
|
|
"name": "Guard Captain",
|
|
"profession": "guard",
|
|
"personality_type": 1,
|
|
"universe_id": "kingdom",
|
|
"seed": 100,
|
|
"spawned": {"location": "castle", "mood": "stern", "stress_level": 5},
|
|
"chat_history":
|
|
[
|
|
{"role": "user", "content": "Who goes there?"},
|
|
{"role": "assistant", "content": "State your business!"}
|
|
]
|
|
}
|
|
|
|
var loaded_npc = GumYumNPC.from_dict(test_data, mock_client)
|
|
|
|
assert_not_null(loaded_npc, "Should create NPC from dict")
|
|
assert_eq(loaded_npc.npc_id, 987654321)
|
|
assert_eq(loaded_npc.name, "Guard Captain")
|
|
assert_eq(loaded_npc.profession, "guard")
|
|
assert_eq(loaded_npc.chat_history.size(), 2)
|
|
assert_eq(loaded_npc.client, mock_client)
|
|
|
|
|
|
func test_from_json():
|
|
# Create JSON string
|
|
var json_data = {
|
|
"npc_id": 111222333,
|
|
"name": "Wise Elder",
|
|
"profession": "sage",
|
|
"personality_type": 5,
|
|
"universe_id": "fantasy",
|
|
"seed": 777,
|
|
"spawned": {"location": "temple", "mood": "contemplative"},
|
|
"chat_history": []
|
|
}
|
|
var json_str = JSON.stringify(json_data)
|
|
|
|
var loaded_npc = GumYumNPC.from_json(json_str, mock_client)
|
|
|
|
assert_not_null(loaded_npc, "Should create NPC from JSON")
|
|
assert_eq(loaded_npc.name, "Wise Elder")
|
|
assert_eq(loaded_npc.universe_id, "fantasy")
|
|
|
|
|
|
func test_from_json_invalid():
|
|
var invalid_json = "{ invalid json ["
|
|
var loaded_npc = GumYumNPC.from_json(invalid_json, mock_client)
|
|
|
|
assert_null(loaded_npc, "Should return null for invalid JSON")
|
|
|
|
|
|
func test_round_trip_serialization():
|
|
# Add chat history
|
|
test_npc.chat_history.append({"role": "user", "content": "Test message 1"})
|
|
test_npc.chat_history.append({"role": "assistant", "content": "Test response 1"})
|
|
|
|
# Convert to JSON and back
|
|
var json_str = test_npc.to_json()
|
|
var restored_npc = GumYumNPC.from_json(json_str, mock_client)
|
|
|
|
assert_not_null(restored_npc, "Should restore NPC")
|
|
assert_eq(restored_npc.npc_id, test_npc.npc_id)
|
|
assert_eq(restored_npc.name, test_npc.name)
|
|
assert_eq(restored_npc.chat_history.size(), 2)
|
|
assert_eq(restored_npc.chat_history[0]["content"], "Test message 1")
|
|
|
|
|
|
func test_save_to_file():
|
|
# Note: This test requires file system access
|
|
var test_path = "user://test_npc_save.json"
|
|
|
|
# Add some data
|
|
test_npc.chat_history.append({"role": "user", "content": "Save test"})
|
|
|
|
var result = test_npc.save_to_file(test_path)
|
|
assert_eq(result, OK, "Should save successfully")
|
|
|
|
# Verify file exists
|
|
var file = FileAccess.open(test_path, FileAccess.READ)
|
|
assert_not_null(file, "File should exist")
|
|
if file:
|
|
file.close()
|
|
|
|
# Clean up
|
|
DirAccess.remove_absolute(test_path)
|
|
|
|
|
|
func test_load_from_file():
|
|
# First save an NPC
|
|
var test_path = "user://test_npc_load.json"
|
|
test_npc.chat_history.append({"role": "user", "content": "Load test"})
|
|
test_npc.save_to_file(test_path)
|
|
|
|
# Load it back
|
|
var loaded_npc = GumYumNPC.load_from_file(test_path, mock_client)
|
|
|
|
assert_not_null(loaded_npc, "Should load NPC from file")
|
|
assert_eq(loaded_npc.name, test_npc.name)
|
|
assert_eq(loaded_npc.chat_history.size(), 1)
|
|
assert_eq(loaded_npc.chat_history[0]["content"], "Load test")
|
|
|
|
# Clean up
|
|
DirAccess.remove_absolute(test_path)
|
|
|
|
|
|
func test_load_from_nonexistent_file():
|
|
var loaded_npc = GumYumNPC.load_from_file("user://does_not_exist.json", mock_client)
|
|
assert_null(loaded_npc, "Should return null for nonexistent file")
|