Files
lc-mcp/tests/test_jev_client.py
T

58 lines
3.1 KiB
Python

import io
import json
import sys
import unittest
import urllib.error
from pathlib import Path
from unittest import mock
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'scripts'))
import jev_client as client
class JevClientTest(unittest.TestCase):
def test_configured_model_endpoint_session_and_instructions_reach_api(self):
cfg = {'apiKey': 'test-key', 'model': 'test-model',
'baseUrl': 'https://example.test/go/v1/', 'sessionId': 'session-1'}
response = io.StringIO(json.dumps({'answers': {'action': {'type': 'choice', 'choice': 'inspect'}}}))
with mock.patch.object(client.urllib.request, 'urlopen', return_value=response) as post:
choice, _ = client.request_decision(cfg, 'main story map', {'inspect': 'inspect team'}, 'story rules')
request = post.call_args.args[0]
self.assertEqual(request.full_url, 'https://example.test/go/v1/systemone')
self.assertEqual(request.get_header('X-opencode-session'), 'session-1')
self.assertEqual(request.get_header('User-agent'), 'lc-mcp/0.1')
body = json.loads(request.data)
self.assertEqual(body['model'], 'test-model')
self.assertEqual(body['questions']['action']['instructions'], 'story rules')
self.assertEqual(choice, 'inspect')
def test_curl_credentials_only_on_stdin_and_http_errors_not_retried(self):
cfg = {'apiKey': 'private-test-key', 'transport': 'curl', 'sessionId': 'session-1'}
response = mock.Mock(returncode=0, stdout='{"error":"rate limit"}\n429')
with mock.patch.object(client.subprocess, 'run', return_value=response) as run, \
mock.patch.object(client.time, 'sleep') as sleep:
with self.assertRaises(urllib.error.HTTPError) as error:
client.request_decision(cfg, 'state', {'wait': 'wait'}, 'choose')
self.assertEqual(error.exception.code, 429)
self.assertNotIn('private-test-key', ' '.join(run.call_args.args[0]))
self.assertIn('Authorization: Bearer private-test-key', run.call_args.kwargs['input'])
self.assertEqual(run.call_count, 1)
sleep.assert_not_called()
def test_choice_outside_permitted_actions_is_rejected(self):
with mock.patch.object(client, '_post', return_value={
'answers': {'action': {'type': 'choice', 'choice': 'spend_currency'}}}):
with self.assertRaisesRegex(ValueError, 'unsupported candidate'):
client.request_decision({'apiKey': 'test'}, 'state', {'wait': 'wait'}, 'choose')
def test_session_id_stays_stable_across_decisions(self):
cfg = {'apiKey': 'test'}
def response(*args, **kwargs):
return io.StringIO('{"answers":{"action":{"type":"choice","choice":"wait"}}}')
with mock.patch.object(client.urllib.request, 'urlopen', side_effect=response) as post:
client.request_decision(cfg, 'state', {'wait': 'wait'}, 'choose')
client.request_decision(cfg, 'state', {'wait': 'wait'}, 'choose')
ids = [call.args[0].get_header('X-opencode-session') for call in post.call_args_list]
self.assertTrue(ids[0])
self.assertEqual(ids[0], ids[1])