File size: 1,429 Bytes
0b2210a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import pytest
import asyncio
import websockets
import json
import os

PORT = 8765
URL = f"ws://127.0.0.1:{PORT}"

@pytest.mark.asyncio
async def test_message():
    async with websockets.connect(URL) as ws:
        await ws.send(json.dumps({"message": "Hello"}))
        response = await ws.recv()
        data = json.loads(response)
        assert "response" in data
        assert isinstance(data["response"], str)

@pytest.mark.asyncio
async def test_directive():
    async with websockets.connect(URL) as ws:
        cmd = {"directive": {"action": "shell", "tool": "shell", "parameters": {"command": "echo MCP_TEST"}}}
        await ws.send(json.dumps(cmd))
        response = await ws.recv()
        data = json.loads(response)
        assert data["type"] == "execution_result"
        assert "MCP_TEST" in data["result"]["output"]

@pytest.mark.asyncio
async def test_invalid_keys():
    async with websockets.connect(URL) as ws:
        await ws.send(json.dumps({"message": "Hi", "extra": "field"}))
        response = await ws.recv()
        data = json.loads(response)
        assert data["type"] == "error"
        assert "Invalid request format" in data["message"]

@pytest.mark.asyncio
async def test_text_key():
    async with websockets.connect(URL) as ws:
        await ws.send(json.dumps({"text": "Hello"}))
        response = await ws.recv()
        data = json.loads(response)
        assert "response" in data