9bafa71667
- Learn a per-device 7x24 energy profile from completed tasks\n- Constrain high-energy scheduling and expose energy peaks to AI\n- Add ghost-time overlays, daily review dialog, and proactive suggestions\n- Cover energy/insights logic and local OpenAI-compatible providers with tests\n- Update validation docs and screenshots
176 lines
5.4 KiB
C++
176 lines
5.4 KiB
C++
#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 streamsLocalWithoutApiKey();
|
|
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::streamsLocalWithoutApiKey()
|
|
{
|
|
FakeSseServer server;
|
|
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
|
|
server.response =
|
|
"HTTP/1.1 200 OK\r\n"
|
|
"Content-Type: text/event-stream\r\n"
|
|
"Connection: close\r\n"
|
|
"\r\n"
|
|
"data: {\"choices\":[{\"delta\":{\"content\":\"local\"}}]}\n\n"
|
|
"data: [DONE]\n\n";
|
|
|
|
AiConfig config;
|
|
config.baseUrl = QStringLiteral("http://127.0.0.1:%1/v1").arg(server.serverPort());
|
|
config.model = QStringLiteral("local-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("local"));
|
|
}
|
|
|
|
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"
|