Files
oh-my-pi/python/omp-rpc/tests/test_protocol.py
T
can1357 67e12b39fe feat(omp-rpc): introduced RpcClient with subprocess communication and event listeners
- Added RpcClient class with subprocess-based RPC communication, thread-safe request/response handling, and 30+ public methods for agent operations.
- Defined 65+ public API symbols including protocol types, dataclasses, parser functions, and custom exception types for RPC communication.
- Implemented event listener system with on_event, on_ui_request, and on_extension_error callbacks for asynchronous RPC event handling.
- Added comprehensive test suite with integration tests for RpcClient and unit tests for protocol parsing functions.
- Configured Python package with setuptools, PEP 561 type hints support, and documentation for usage and API reference.
2026-04-08 04:20:06 +02:00

124 lines
4.2 KiB
Python

from __future__ import annotations
import unittest
from omp_rpc import (
AgentEndEvent,
ExtensionUiRequest,
SessionState,
assistant_text,
parse_notification,
parse_session_state,
)
class ProtocolParsingTests(unittest.TestCase):
def test_parse_session_state(self) -> None:
state = parse_session_state(
{
"model": {
"id": "claude-sonnet-4-5",
"name": "Claude Sonnet 4.5",
"api": "anthropic-messages",
"provider": "anthropic",
"baseUrl": "https://api.anthropic.com",
"reasoning": True,
"input": ["text", "image"],
"cost": {
"input": 1.0,
"output": 2.0,
"cacheRead": 0.1,
"cacheWrite": 0.2,
},
"contextWindow": 200000,
"maxTokens": 8192,
"thinking": {
"minLevel": "minimal",
"maxLevel": "high",
"mode": "effort",
},
},
"thinkingLevel": "medium",
"isStreaming": False,
"isCompacting": False,
"steeringMode": "one-at-a-time",
"followUpMode": "all",
"interruptMode": "immediate",
"sessionFile": "/tmp/test.jsonl",
"sessionId": "session-123",
"sessionName": "Scratchpad",
"autoCompactionEnabled": True,
"messageCount": 4,
"queuedMessageCount": 1,
"systemPrompt": "You are useful.",
"dumpTools": [
{
"name": "read",
"description": "Read files",
"parameters": {"type": "object"},
}
],
}
)
self.assertIsInstance(state, SessionState)
self.assertEqual(state.session_id, "session-123")
self.assertEqual(state.follow_up_mode, "all")
self.assertEqual(state.model.id if state.model else None, "claude-sonnet-4-5")
self.assertEqual(state.dump_tools[0].name, "read")
def test_parse_agent_end_notification(self) -> None:
notification = parse_notification(
{
"type": "agent_end",
"messages": [
{
"role": "assistant",
"content": [{"type": "text", "text": "hello"}],
"api": "anthropic-messages",
"provider": "anthropic",
"model": "claude-sonnet-4-5",
"usage": {
"input": 1,
"output": 1,
"cacheRead": 0,
"cacheWrite": 0,
"totalTokens": 2,
"cost": {
"input": 0.0,
"output": 0.0,
"cacheRead": 0.0,
"cacheWrite": 0.0,
"total": 0.0,
},
},
"stopReason": "stop",
"timestamp": 1,
}
],
}
)
self.assertIsInstance(notification, AgentEndEvent)
self.assertEqual(assistant_text(notification.messages[0]), "hello")
def test_parse_extension_ui_request(self) -> None:
notification = parse_notification(
{
"type": "extension_ui_request",
"id": "ui-1",
"method": "confirm",
"title": "Confirm",
"message": "Continue?",
"timeout": 1000,
}
)
self.assertIsInstance(notification, ExtensionUiRequest)
self.assertEqual(notification.method, "confirm")
self.assertEqual(notification.message, "Continue?")
if __name__ == "__main__":
unittest.main()