fix(serialization): Fix Path serialization in events and thread-local fallback in ai_client

This commit is contained in:
2026-05-09 12:35:58 -04:00
parent c6e77f2b99
commit e313802a15
2 changed files with 73 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
import pytest
import time
import sys
import os
# Ensure project root is in path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from src.api_hook_client import ApiHookClient
def wait_for_value(client, field, expected, timeout=5):
"""Polls the GUI state until a field matches the expected value."""
start = time.time()
while time.time() - start < timeout:
val = client.get_value(field)
if val == expected:
return True
time.sleep(0.5)
return False
@pytest.mark.smoke
def test_status_hook(live_gui) -> None:
"""Smoke test for setting and getting ai_status and mma_status via ApiHookClient."""
client = ApiHookClient()
assert client.wait_for_server(timeout=10)
# 1. Set ai_status to 'hook_test'
client.set_value('ai_status', 'hook_test')
# 2. Verify via get_value('ai_status') == 'hook_test' (with retry)
assert wait_for_value(client, 'ai_status', 'hook_test'), f"Failed to set ai_status to hook_test. Current value: {client.get_value('ai_status')}"
# 3. Set mma_status to 'hook_mma_test'
client.set_value('mma_status', 'hook_mma_test')
# 4. Verify via get_value('mma_status') == 'hook_mma_test' (with retry)
assert wait_for_value(client, 'mma_status', 'hook_mma_test'), f"Failed to set mma_status to hook_mma_test. Current value: {client.get_value('mma_status')}"
+38
View File
@@ -0,0 +1,38 @@
import json
from pathlib import Path
from src.events import UserRequestEvent
def test_user_request_event_serialization():
# Setup payload with nested Path objects
file_items = [
{"path": Path("some/file.txt"), "mtime": 1234.5},
{"path": Path("another/path"), "metadata": {"sub_path": Path("sub/path")}}
]
base_dir = Path("C:/projects/test")
event = UserRequestEvent(
prompt="Test prompt",
stable_md="# Context",
file_items=file_items,
disc_text="Discussion history",
base_dir=str(base_dir)
)
# Execute serialization
result = event.to_dict()
# Verify no Path objects remain
assert isinstance(result["file_items"][0]["path"], str)
assert result["file_items"][0]["path"] == str(Path("some/file.txt"))
assert isinstance(result["file_items"][1]["metadata"]["sub_path"], str)
assert result["file_items"][1]["metadata"]["sub_path"] == str(Path("sub/path"))
# Verify JSON serializability
try:
json.dumps(result)
except (TypeError, OverflowError) as e:
assert False, f"Result not JSON serializable: {e}"
if __name__ == "__main__":
test_user_request_event_serialization()
print("Test passed!")