78 lines
3.2 KiB
Python
78 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Unit tests for web_server.py"""
|
|
|
|
import asyncio
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
import aiohttp
|
|
from model_manager import ModelManager
|
|
from voice_manager import VoiceManager
|
|
import web_server
|
|
|
|
async def test_web_server():
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
workspace = Path(tmpdir)
|
|
(workspace / "test.txt").write_text("Hello World Content!")
|
|
(workspace / "journal.jsonl").write_text(json.dumps({"at": "2026-08-08T12:00:00", "heard": "Hello", "reply": "Hi!"}) + "\n")
|
|
|
|
model_mgr = ModelManager(workspace)
|
|
voice_mgr = VoiceManager(workspace)
|
|
|
|
await web_server.start_server(workspace, host="127.0.0.1", port=9999)
|
|
web_server.set_managers(workspace, model_mgr, voice_mgr)
|
|
|
|
# Test HTTP requests
|
|
async with aiohttp.ClientSession() as session:
|
|
# Index HTML
|
|
async with session.get("http://127.0.0.1:9999/") as resp:
|
|
assert resp.status == 200
|
|
html = await resp.text()
|
|
assert "VoiceAgent Companion" in html
|
|
print("PASS: Index HTML endpoint")
|
|
|
|
# History API
|
|
async with session.get("http://127.0.0.1:9999/api/history") as resp:
|
|
assert resp.status == 200
|
|
data = await resp.json()
|
|
assert len(data["turns"]) == 1
|
|
assert data["turns"][0]["heard"] == "Hello"
|
|
print("PASS: History API endpoint")
|
|
|
|
# File API
|
|
async with session.get("http://127.0.0.1:9999/api/file?path=test.txt") as resp:
|
|
assert resp.status == 200
|
|
data = await resp.json()
|
|
assert data["content"] == "Hello World Content!"
|
|
print("PASS: File API endpoint")
|
|
|
|
# Set Model API
|
|
async with session.post("http://127.0.0.1:9999/api/model", json={"model": "deepseek"}) as resp:
|
|
assert resp.status == 200
|
|
data = await resp.json()
|
|
assert data["success"] is True
|
|
assert model_mgr._active_model == "ollama-cloud/deepseek-v4-flash"
|
|
print("PASS: Set Model API endpoint")
|
|
|
|
# Set Voice API
|
|
async with session.post("http://127.0.0.1:9999/api/voice", json={"voice": "am_michael"}) as resp:
|
|
assert resp.status == 200
|
|
data = await resp.json()
|
|
assert data["success"] is True
|
|
assert voice_mgr._active_voice == "am_michael"
|
|
print("PASS: Set Voice API endpoint")
|
|
|
|
# Send Message API
|
|
received = []
|
|
web_server.set_managers(workspace, model_mgr, voice_mgr, input_callback=lambda txt: received.append(txt))
|
|
async with session.post("http://127.0.0.1:9999/api/send", json={"text": "hello from web"}) as resp:
|
|
assert resp.status == 200
|
|
data = await resp.json()
|
|
assert data["success"] is True
|
|
assert "hello from web" in received
|
|
print("PASS: Send Message API endpoint")
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(test_web_server())
|
|
print("\nAll Web Server tests passed successfully!")
|