feat(headless): Implement Phase 4 - Session & Context Management via API
This commit is contained in:
@@ -9,7 +9,7 @@ auto_add = true
|
||||
|
||||
[discussions.main]
|
||||
git_commit = ""
|
||||
last_updated = "2026-02-25T13:08:45"
|
||||
last_updated = "2026-02-25T13:18:14"
|
||||
history = [
|
||||
"@1772042443.1159382\nUser:\nStress test entry 0 Stress test entry 0 Stress test entry 0 Stress test entry 0 Stress test entry 0",
|
||||
"@1772042443.1159382\nUser:\nStress test entry 1 Stress test entry 1 Stress test entry 1 Stress test entry 1 Stress test entry 1",
|
||||
|
||||
@@ -2,21 +2,21 @@ import unittest
|
||||
from fastapi.testclient import TestClient
|
||||
import gui_2
|
||||
from unittest.mock import patch, MagicMock
|
||||
from pathlib import Path
|
||||
|
||||
class TestHeadlessAPI(unittest.TestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
def setUp(self):
|
||||
# We need an App instance to initialize the API, but we want to avoid GUI stuff
|
||||
with patch('gui_2.session_logger.open_session'), \
|
||||
patch('gui_2.ai_client.set_provider'), \
|
||||
patch('gui_2.session_logger.close_session'):
|
||||
cls.app_instance = gui_2.App()
|
||||
# We will implement create_api method in App
|
||||
if hasattr(cls.app_instance, 'create_api'):
|
||||
cls.api = cls.app_instance.create_api()
|
||||
else:
|
||||
cls.api = MagicMock()
|
||||
cls.client = TestClient(cls.api)
|
||||
self.app_instance = gui_2.App()
|
||||
# Clear any leftover state
|
||||
self.app_instance._pending_actions = {}
|
||||
self.app_instance._pending_dialog = None
|
||||
|
||||
self.api = self.app_instance.create_api()
|
||||
self.client = TestClient(self.api)
|
||||
|
||||
def test_health_endpoint(self):
|
||||
response = self.client.get("/health")
|
||||
@@ -58,5 +58,53 @@ class TestHeadlessAPI(unittest.TestCase):
|
||||
self.assertIn("metadata", data)
|
||||
self.assertEqual(data["usage"]["input_tokens"], 10)
|
||||
|
||||
def test_pending_actions_endpoint(self):
|
||||
# Manually add a pending action
|
||||
with patch('gui_2.uuid.uuid4', return_value="test-action-id"):
|
||||
dialog = gui_2.ConfirmDialog("dir", ".")
|
||||
self.app_instance._pending_actions[dialog._uid] = dialog
|
||||
|
||||
response = self.client.get("/api/v1/pending_actions")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data = response.json()
|
||||
self.assertEqual(len(data), 1)
|
||||
self.assertEqual(data[0]["action_id"], "test-action-id")
|
||||
|
||||
def test_confirm_action_endpoint(self):
|
||||
# Manually add a pending action
|
||||
with patch('gui_2.uuid.uuid4', return_value="test-confirm-id"):
|
||||
dialog = gui_2.ConfirmDialog("dir", ".")
|
||||
self.app_instance._pending_actions[dialog._uid] = dialog
|
||||
|
||||
payload = {"approved": True}
|
||||
response = self.client.post("/api/v1/confirm/test-confirm-id", json=payload)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertTrue(dialog._done)
|
||||
self.assertTrue(dialog._approved)
|
||||
|
||||
def test_list_sessions_endpoint(self):
|
||||
# Ensure logs directory exists
|
||||
Path("logs").mkdir(exist_ok=True)
|
||||
# Create a dummy log
|
||||
dummy_log = Path("logs/test_session_api.log")
|
||||
dummy_log.write_text("dummy content")
|
||||
|
||||
try:
|
||||
response = self.client.get("/api/v1/sessions")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data = response.json()
|
||||
self.assertIn("test_session_api.log", data)
|
||||
finally:
|
||||
if dummy_log.exists():
|
||||
dummy_log.unlink()
|
||||
|
||||
def test_get_context_endpoint(self):
|
||||
response = self.client.get("/api/v1/context")
|
||||
self.assertEqual(response.status_code, 200)
|
||||
data = response.json()
|
||||
self.assertIn("files", data)
|
||||
self.assertIn("screenshots", data)
|
||||
self.assertIn("files_base_dir", data)
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user