74 lines
2.8 KiB
Python
74 lines
2.8 KiB
Python
import os
|
|
import sys
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
|
|
import pytest
|
|
from unittest.mock import patch
|
|
import gui
|
|
import api_hooks
|
|
import urllib.request
|
|
import json
|
|
import threading
|
|
import time
|
|
|
|
def test_hooks_enabled_via_cli():
|
|
with patch.object(sys, 'argv', ['gui.py', '--enable-test-hooks']):
|
|
app = gui.App()
|
|
assert app.test_hooks_enabled is True
|
|
|
|
def test_hooks_disabled_by_default():
|
|
with patch.object(sys, 'argv', ['gui.py']):
|
|
if 'SLOP_TEST_HOOKS' in os.environ:
|
|
del os.environ['SLOP_TEST_HOOKS']
|
|
app = gui.App()
|
|
assert getattr(app, 'test_hooks_enabled', False) is False
|
|
|
|
def test_hooks_enabled_via_env():
|
|
with patch.object(sys, 'argv', ['gui.py']):
|
|
with patch.dict(os.environ, {'SLOP_TEST_HOOKS': '1'}):
|
|
app = gui.App()
|
|
assert app.test_hooks_enabled is True
|
|
|
|
def test_ipc_server_starts_and_responds():
|
|
app_mock = gui.App()
|
|
app_mock.test_hooks_enabled = True
|
|
server = api_hooks.HookServer(app_mock, port=8999)
|
|
server.start()
|
|
|
|
# Wait for server to start
|
|
time.sleep(0.5)
|
|
try:
|
|
req = urllib.request.Request("http://127.0.0.1:8999/status")
|
|
with urllib.request.urlopen(req) as response:
|
|
assert response.status == 200
|
|
data = json.loads(response.read().decode())
|
|
assert data.get("status") == "ok"
|
|
|
|
# Test project GET
|
|
req = urllib.request.Request("http://127.0.0.1:8999/api/project")
|
|
with urllib.request.urlopen(req) as response:
|
|
assert response.status == 200
|
|
data = json.loads(response.read().decode())
|
|
assert "project" in data
|
|
|
|
# Test session GET
|
|
req = urllib.request.Request("http://127.0.0.1:8999/api/session")
|
|
with urllib.request.urlopen(req) as response:
|
|
assert response.status == 200
|
|
data = json.loads(response.read().decode())
|
|
assert "session" in data
|
|
|
|
# Test project POST
|
|
req = urllib.request.Request("http://127.0.0.1:8999/api/project", method="POST", data=json.dumps({"project": {"foo": "bar"}}).encode("utf-8"), headers={'Content-Type': 'application/json'})
|
|
with urllib.request.urlopen(req) as response:
|
|
assert response.status == 200
|
|
assert app_mock.project == {"foo": "bar"}
|
|
|
|
# Test session POST
|
|
req = urllib.request.Request("http://127.0.0.1:8999/api/session", method="POST", data=json.dumps({"session": {"entries": [{"role": "User", "content": "hi"}]}}).encode("utf-8"), headers={'Content-Type': 'application/json'})
|
|
with urllib.request.urlopen(req) as response:
|
|
assert response.status == 200
|
|
assert app_mock.disc_entries == [{"role": "User", "content": "hi"}]
|
|
|
|
finally:
|
|
server.stop()
|