feat: implement AI schedule app with multi-device sync
- Qt 6 desktop client with day/month/year views, AI scheduling, and TOML settings\n- Go sync server with SQLite change stream, WebSocket notifications, and Docker deployment\n- Client outbox/LWW sync, offline retry, and dual-client integration tests\n- Linux fcitx5 setup, validation reports, and Windows MinGW preset
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QTimer>
|
||||
#include <QtTest>
|
||||
|
||||
#include "ai/IAIProvider.h"
|
||||
#include "ai/OpenAiProvider.h"
|
||||
|
||||
class FakeSseServer : public QTcpServer
|
||||
{
|
||||
public:
|
||||
QByteArray response;
|
||||
|
||||
FakeSseServer()
|
||||
{
|
||||
connect(this, &QTcpServer::newConnection, this, [this]() {
|
||||
while (hasPendingConnections())
|
||||
{
|
||||
QTcpSocket* socket = nextPendingConnection();
|
||||
connect(socket, &QTcpSocket::readyRead, socket, [this, socket]() {
|
||||
if (socket->property("responded").toBool())
|
||||
{
|
||||
return;
|
||||
}
|
||||
QByteArray buffer = socket->property("buffer").toByteArray();
|
||||
buffer += socket->readAll();
|
||||
socket->setProperty("buffer", buffer);
|
||||
if (!buffer.contains("\r\n\r\n"))
|
||||
{
|
||||
return;
|
||||
}
|
||||
socket->setProperty("responded", true);
|
||||
socket->write(response);
|
||||
socket->flush();
|
||||
QTimer::singleShot(30, socket, [socket]() { socket->disconnectFromHost(); });
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
class TestOpenAiProvider : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void streamsSseContent();
|
||||
void reportsHttpError();
|
||||
};
|
||||
|
||||
void TestOpenAiProvider::streamsSseContent()
|
||||
{
|
||||
FakeSseServer server;
|
||||
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
|
||||
server.response =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/event-stream\r\n"
|
||||
"Cache-Control: no-cache\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
"data: {\"choices\":[{\"delta\":{\"content\":\"你好\"}}]}\n\n"
|
||||
"data: {\"choices\":[{\"delta\":{\"content\":\",世界\"}}]}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
|
||||
AiConfig config;
|
||||
config.baseUrl = QStringLiteral("http://127.0.0.1:%1/v1").arg(server.serverPort());
|
||||
config.apiKey = QStringLiteral("test-key");
|
||||
config.model = QStringLiteral("test-model");
|
||||
|
||||
QJsonArray messages;
|
||||
QJsonObject message;
|
||||
message.insert(QStringLiteral("role"), QStringLiteral("user"));
|
||||
message.insert(QStringLiteral("content"), QStringLiteral("hi"));
|
||||
messages.append(message);
|
||||
|
||||
OpenAiProvider provider;
|
||||
QString text;
|
||||
QString error;
|
||||
bool finished = false;
|
||||
provider.streamCompletion(
|
||||
config,
|
||||
messages,
|
||||
{
|
||||
[&text](const QString& delta) { text += delta; },
|
||||
[&error](const QString& message) { error = message; },
|
||||
[&finished]() { finished = true; },
|
||||
});
|
||||
QTRY_VERIFY_WITH_TIMEOUT(finished, 5000);
|
||||
QCOMPARE(error, QString());
|
||||
QCOMPARE(text, QStringLiteral("你好,世界"));
|
||||
}
|
||||
|
||||
void TestOpenAiProvider::reportsHttpError()
|
||||
{
|
||||
FakeSseServer server;
|
||||
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
|
||||
server.response =
|
||||
"HTTP/1.1 401 Unauthorized\r\n"
|
||||
"Content-Type: application/json\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
"{\"error\":{\"message\":\"Invalid API key\"}}";
|
||||
|
||||
AiConfig config;
|
||||
config.baseUrl = QStringLiteral("http://127.0.0.1:%1/v1").arg(server.serverPort());
|
||||
config.apiKey = QStringLiteral("bad-key");
|
||||
config.model = QStringLiteral("test-model");
|
||||
|
||||
QJsonArray messages;
|
||||
QJsonObject message;
|
||||
message.insert(QStringLiteral("role"), QStringLiteral("user"));
|
||||
message.insert(QStringLiteral("content"), QStringLiteral("hi"));
|
||||
messages.append(message);
|
||||
|
||||
OpenAiProvider provider;
|
||||
QString error;
|
||||
bool finished = false;
|
||||
provider.streamCompletion(
|
||||
config,
|
||||
messages,
|
||||
{
|
||||
[](const QString&) {},
|
||||
[&error](const QString& message) { error = message; },
|
||||
[&finished]() { finished = true; },
|
||||
});
|
||||
QTRY_VERIFY_WITH_TIMEOUT(!error.isEmpty(), 5000);
|
||||
QVERIFY(!finished);
|
||||
QVERIFY(error.contains(QStringLiteral("Invalid API key")));
|
||||
QVERIFY(error.contains(QStringLiteral("401")));
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(TestOpenAiProvider)
|
||||
#include "test_openai_provider.moc"
|
||||
Reference in New Issue
Block a user