Sonde v1.1.0
Embeddable Model Context Protocol server for Qt applications
Loading...
Searching...
No Matches
testsupport.h
Go to the documentation of this file.
1#pragma once
2
6
7#include <QtCore/QByteArray>
8#include <QtCore/QCoreApplication>
9#include <QtCore/QElapsedTimer>
10#include <QtCore/QEventLoop>
11#include <QtCore/QHash>
12#include <QtCore/QJsonArray>
13#include <QtCore/QJsonDocument>
14#include <QtCore/QJsonObject>
15#include <QtCore/QList>
16#include <QtCore/QTimer>
17#include <QtNetwork/QLocalSocket>
18#include <QtNetwork/QTcpSocket>
19
20#include <functional>
21#include <memory>
22
23namespace SondeTest {
24
30
33bool waitUntil(const std::function<bool()> &predicate, int timeoutMs = 5000);
34
36void spin(int ms);
37
39QString uniqueName(const QString &prefix);
40
42QString resultText(const QJsonObject &toolResult);
43
49{
50public:
52 explicit LocalClient(const QString &name);
53
55 bool isConnected() const;
56
59 QJsonObject request(const QString &method, const QJsonObject &params = { },
60 int timeoutMs = 5000);
61
63 int sendRequest(const QString &method, const QJsonObject &params = { });
64
66 QJsonObject waitForResponse(int id, int timeoutMs = 5000);
67
69 void notify(const QString &method, const QJsonObject &params = { });
70
72 void sendRaw(const QByteArray &data);
73
75 QJsonObject initialize(const QString &protocolVersion = QStringLiteral("2025-06-18"));
76
78 QJsonObject callTool(const QString &name, const QJsonObject &arguments = { },
79 int timeoutMs = 5000);
80
82 QString callToolText(const QString &name, const QJsonObject &arguments = { },
83 int timeoutMs = 5000);
84
86 bool waitForNotification(const QString &method, int timeoutMs = 3000, qsizetype from = 0);
87
89 const QList<QJsonObject> &notifications() const { return m_notifications; }
90
92 const QList<QJsonArray> &batches() const { return m_batches; }
93
95 QLocalSocket &socket() { return m_socket; }
96
97private:
99 void onReadyRead();
100
101 QLocalSocket m_socket;
102 QByteArray m_buffer;
103 QHash<int, QJsonObject> m_responses;
104 QList<QJsonObject> m_notifications;
105 QList<QJsonArray> m_batches;
106 int m_nextId = 1;
107};
108
111{
112 int status = 0;
113 QHash<QByteArray, QByteArray> headers;
114 QByteArray body;
115
117 QJsonObject json() const { return QJsonDocument::fromJson(body).object(); }
119 QByteArray header(const char *name) const { return headers.value(QByteArray(name)); }
120};
121
123QList<HttpResponse> rawHttp(quint16 port, const QByteArray &request, int count = 1,
124 int timeoutMs = 5000);
125
127HttpResponse http(quint16 port, const QByteArray &method, const QByteArray &path,
128 const QList<QPair<QByteArray, QByteArray>> &headers = { },
129 const QByteArray &body = { }, int timeoutMs = 5000);
130
133{
134public:
136 explicit HttpClient(quint16 port) : m_port(port) { }
137
139 QJsonObject initialize();
141 QJsonObject request(const QString &method, const QJsonObject &params = { });
143 HttpResponse post(const QJsonValue &body,
144 const QList<QPair<QByteArray, QByteArray>> &extraHeaders = { });
146 QByteArray sessionId() const { return m_session; }
147
148private:
149 quint16 m_port;
150 QByteArray m_session;
151 int m_nextId = 1;
152};
153
156{
157public:
159 SseStream(quint16 port, const QByteArray &session);
160
162 int waitForStatus(int timeoutMs = 3000);
164 bool waitForMessage(const QString &method, int timeoutMs = 3000);
166 const QList<QJsonObject> &messages() const { return m_messages; }
168 bool isClosed() const { return m_socket.state() == QAbstractSocket::UnconnectedState; }
169
170private:
172 void onReadyRead();
173
174 QTcpSocket m_socket;
175 QByteArray m_buffer;
176 int m_status = 0;
177 QList<QJsonObject> m_messages;
178};
179
180} // namespace SondeTest
QJsonObject request(const QString &method, const QJsonObject &params={ })
POSTs a JSON-RPC request with the session header.
Definition testsupport.cpp:275
QJsonObject initialize()
Sends initialize and stores the returned session id.
Definition testsupport.cpp:261
QByteArray sessionId() const
The session id from initialize.
Definition testsupport.h:146
HttpResponse post(const QJsonValue &body, const QList< QPair< QByteArray, QByteArray > > &extraHeaders={ })
POSTs any JSON body with the session header.
Definition testsupport.cpp:285
QByteArray m_session
Mcp-Session-Id from initialize.
Definition testsupport.h:150
HttpClient(quint16 port)
Creates a client for the server on 127.0.0.1:port.
Definition testsupport.h:136
int m_nextId
Next request id.
Definition testsupport.h:151
quint16 m_port
Server port.
Definition testsupport.h:149
QLocalSocket m_socket
Connection to the server.
Definition testsupport.h:101
QList< QJsonArray > m_batches
Array responses.
Definition testsupport.h:105
QJsonObject request(const QString &method, const QJsonObject &params={ }, int timeoutMs=5000)
Sends a request and waits for its response.
Definition testsupport.cpp:121
bool waitForNotification(const QString &method, int timeoutMs=3000, qsizetype from=0)
Waits until a notification with method arrives (counting from from).
Definition testsupport.cpp:168
bool isConnected() const
Whether the socket connected.
Definition testsupport.cpp:77
void sendRaw(const QByteArray &data)
Writes raw bytes to the socket.
Definition testsupport.cpp:135
int sendRequest(const QString &method, const QJsonObject &params={ })
Sends a request without waiting; returns the id used.
Definition testsupport.cpp:103
QJsonObject initialize(const QString &protocolVersion=QStringLiteral("2025-06-18"))
Performs initialize + notifications/initialized.
Definition testsupport.cpp:141
QHash< int, QJsonObject > m_responses
Responses not yet collected, by id.
Definition testsupport.h:103
QList< QJsonObject > m_notifications
Messages without an id.
Definition testsupport.h:104
void notify(const QString &method, const QJsonObject &params={ })
Sends a notification (no id).
Definition testsupport.cpp:126
int m_nextId
Next request id.
Definition testsupport.h:106
const QList< QJsonArray > & batches() const
Responses that arrived as JSON arrays (legacy batches).
Definition testsupport.h:92
QJsonObject waitForResponse(int id, int timeoutMs=5000)
Waits for the response to request id.
Definition testsupport.cpp:115
QLocalSocket & socket()
The underlying socket.
Definition testsupport.h:95
const QList< QJsonObject > & notifications() const
All notifications received so far.
Definition testsupport.h:89
LocalClient(const QString &name)
Connects to the local server name; check isConnected().
Definition testsupport.cpp:70
QString callToolText(const QString &name, const QJsonObject &arguments={ }, int timeoutMs=5000)
Calls a tool and returns the concatenated text of its result.
Definition testsupport.cpp:163
void onReadyRead()
Parses complete lines from the read buffer.
Definition testsupport.cpp:82
QByteArray m_buffer
Received bytes not yet split into lines.
Definition testsupport.h:102
QJsonObject callTool(const QString &name, const QJsonObject &arguments={ }, int timeoutMs=5000)
Calls a tool and returns its result object (empty on JSON-RPC error).
Definition testsupport.cpp:154
QByteArray m_buffer
Received bytes not yet parsed.
Definition testsupport.h:175
SseStream(quint16 port, const QByteArray &session)
Opens the stream for session on port.
Definition testsupport.cpp:300
bool waitForMessage(const QString &method, int timeoutMs=3000)
Waits until a message with method has arrived.
Definition testsupport.cpp:338
QList< QJsonObject > m_messages
Parsed data: payloads.
Definition testsupport.h:177
int waitForStatus(int timeoutMs=3000)
Waits for the response head; returns the status code.
Definition testsupport.cpp:332
void onReadyRead()
Parses received SSE data.
Definition testsupport.cpp:311
const QList< QJsonObject > & messages() const
Messages received so far.
Definition testsupport.h:166
QTcpSocket m_socket
Stream connection.
Definition testsupport.h:174
bool isClosed() const
Whether the server closed the stream.
Definition testsupport.h:168
int m_status
Response status, 0 until the head arrives.
Definition testsupport.h:176
A parsed HTTP response.
Definition testsupport.h:111
int status
Status code, 0 if no response arrived.
Definition testsupport.h:112
QHash< QByteArray, QByteArray > headers
Header fields, names lower-cased.
Definition testsupport.h:113
QByteArray header(const char *name) const
A header value by lower-case name.
Definition testsupport.h:119
QByteArray body
Response body.
Definition testsupport.h:114
QJsonObject json() const
The body parsed as a JSON object.
Definition testsupport.h:117
QString uniqueName(const QString &prefix)
Returns a process-unique name with the given prefix (for sockets).
Definition testsupport.cpp:49
void spin(int ms)
Runs the event loop for ms milliseconds.
Definition testsupport.cpp:42
bool waitUntil(const std::function< bool()> &predicate, int timeoutMs)
Runs the event loop until predicate is true or timeoutMs expires.
Definition testsupport.cpp:28
void reportCrashesOnStderr()
For the tests' main(): on Windows, sends the C runtime's reports (qFatal() and failed assertions in a...
Definition testsupport.cpp:17
QString resultText(const QJsonObject &toolResult)
Concatenates the text blocks of a tool result's content array.
Definition testsupport.cpp:58