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,186 @@
|
||||
#include <QCoreApplication>
|
||||
#include <QElapsedTimer>
|
||||
#include <QFileInfo>
|
||||
#include <QProcess>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
#include <QTemporaryDir>
|
||||
#include <QThread>
|
||||
#include <QtTest>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "services/ScheduleService.h"
|
||||
#include "storage/SqliteStore.h"
|
||||
#include "sync/SyncService.h"
|
||||
#include "utils/AppSettings.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
int freePort()
|
||||
{
|
||||
QTcpServer probe;
|
||||
if (!probe.listen(QHostAddress::LocalHost, 0))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
const int port = probe.serverPort();
|
||||
probe.close();
|
||||
return port;
|
||||
}
|
||||
|
||||
bool waitForServer(int port, int timeoutMs)
|
||||
{
|
||||
QElapsedTimer timer;
|
||||
timer.start();
|
||||
while (timer.elapsed() < timeoutMs)
|
||||
{
|
||||
QTcpSocket socket;
|
||||
socket.connectToHost(QHostAddress::LocalHost, static_cast<quint16>(port));
|
||||
if (socket.waitForConnected(150))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
QCoreApplication::processEvents();
|
||||
QThread::msleep(50);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
class TestSyncIntegration : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
private slots:
|
||||
void twoClientsConverge();
|
||||
};
|
||||
|
||||
void TestSyncIntegration::twoClientsConverge()
|
||||
{
|
||||
const QString serverBinary = qEnvironmentVariable("SCHEDULE_SYNC_SERVER_BIN");
|
||||
if (serverBinary.isEmpty() || !QFileInfo::exists(serverBinary))
|
||||
{
|
||||
QSKIP("sync server binary is not available");
|
||||
}
|
||||
const int port = freePort();
|
||||
QVERIFY(port > 0);
|
||||
QTemporaryDir serverDir;
|
||||
QVERIFY(serverDir.isValid());
|
||||
QProcess serverProcess;
|
||||
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
|
||||
env.insert(QStringLiteral("SYNC_LISTEN_ADDR"), QStringLiteral("127.0.0.1:%1").arg(port));
|
||||
env.insert(QStringLiteral("SYNC_DB_PATH"), serverDir.filePath(QStringLiteral("sync.db")));
|
||||
env.insert(QStringLiteral("SYNC_TOKEN_PEPPER"), QStringLiteral("integration-pepper"));
|
||||
serverProcess.setProcessEnvironment(env);
|
||||
serverProcess.setProcessChannelMode(QProcess::MergedChannels);
|
||||
serverProcess.setStandardOutputFile(serverDir.filePath(QStringLiteral("server.log")));
|
||||
serverProcess.start(serverBinary, {});
|
||||
QVERIFY2(serverProcess.waitForStarted(5000), qPrintable(serverProcess.errorString()));
|
||||
QVERIFY(waitForServer(port, 10000));
|
||||
|
||||
QTemporaryDir clientADir;
|
||||
QTemporaryDir clientBDir;
|
||||
QVERIFY(clientADir.isValid());
|
||||
QVERIFY(clientBDir.isValid());
|
||||
AppSettings settingsA(clientADir.filePath(QStringLiteral("config.toml")));
|
||||
AppSettings settingsB(clientBDir.filePath(QStringLiteral("config.toml")));
|
||||
SyncConfig syncConfig;
|
||||
syncConfig.enabled = true;
|
||||
syncConfig.serverUrl = QStringLiteral("http://127.0.0.1:%1").arg(port);
|
||||
settingsA.setSyncConfig(syncConfig);
|
||||
settingsB.setSyncConfig(syncConfig);
|
||||
|
||||
SqliteStore storeA(clientADir.filePath(QStringLiteral("schedule.db")));
|
||||
SqliteStore storeB(clientBDir.filePath(QStringLiteral("schedule.db")));
|
||||
QString error;
|
||||
QVERIFY2(storeA.open(&error), qPrintable(error));
|
||||
QVERIFY2(storeB.open(&error), qPrintable(error));
|
||||
ScheduleService serviceA(storeA, settingsA.deviceId());
|
||||
ScheduleService serviceB(storeB, settingsB.deviceId());
|
||||
SyncService syncA(serviceA, storeA, settingsA);
|
||||
SyncService syncB(serviceB, storeB, settingsB);
|
||||
syncA.start();
|
||||
syncB.start();
|
||||
QTRY_VERIFY_WITH_TIMEOUT(syncA.statusText().contains(QStringLiteral("同步")) ||
|
||||
syncA.statusText().contains(QStringLiteral("已连接")), 12000);
|
||||
QTRY_VERIFY_WITH_TIMEOUT(syncB.statusText().contains(QStringLiteral("同步")) ||
|
||||
syncB.statusText().contains(QStringLiteral("已连接")), 12000);
|
||||
|
||||
const QDate tomorrow = QDate::currentDate().addDays(1);
|
||||
const QDateTime start(tomorrow, QTime(9, 0), Qt::LocalTime);
|
||||
const QDateTime end(tomorrow, QTime(10, 0), Qt::LocalTime);
|
||||
QVERIFY2(serviceA.createBlock(QStringLiteral("跨设备任务"), start, end, &error), qPrintable(error));
|
||||
|
||||
QTRY_VERIFY_WITH_TIMEOUT(serviceB.itemsForDay(tomorrow).size() == 1, 20000);
|
||||
QCOMPARE(serviceB.itemsForDay(tomorrow).first().task.title, QStringLiteral("跨设备任务"));
|
||||
|
||||
QVERIFY2(serviceA.updateBlock(serviceA.itemsForDay(tomorrow).first().block.id,
|
||||
QStringLiteral("跨设备任务 v2"),
|
||||
start.addSecs(3600),
|
||||
end.addSecs(3600),
|
||||
&error),
|
||||
qPrintable(error));
|
||||
QTRY_VERIFY_WITH_TIMEOUT(!serviceB.itemsForDay(tomorrow).isEmpty() &&
|
||||
serviceB.itemsForDay(tomorrow).first().task.title ==
|
||||
QStringLiteral("跨设备任务 v2"),
|
||||
20000);
|
||||
|
||||
QVERIFY2(serviceB.createBlock(QStringLiteral("反向任务"),
|
||||
start.addSecs(2 * 3600),
|
||||
end.addSecs(2 * 3600),
|
||||
&error),
|
||||
qPrintable(error));
|
||||
QTRY_VERIFY_WITH_TIMEOUT(serviceA.itemsForDay(tomorrow).size() == 2, 20000);
|
||||
bool sawReverse = false;
|
||||
for (const ScheduleItem& item : serviceA.itemsForDay(tomorrow))
|
||||
{
|
||||
if (item.task.title == QStringLiteral("反向任务"))
|
||||
{
|
||||
sawReverse = true;
|
||||
}
|
||||
}
|
||||
QVERIFY(sawReverse);
|
||||
|
||||
for (const ScheduleItem& item : serviceA.itemsForDay(tomorrow))
|
||||
{
|
||||
if (item.task.title == QStringLiteral("跨设备任务 v2"))
|
||||
{
|
||||
QVERIFY2(serviceA.deleteBlock(item.block.id, &error), qPrintable(error));
|
||||
break;
|
||||
}
|
||||
}
|
||||
QTRY_VERIFY_WITH_TIMEOUT(serviceB.itemsForDay(tomorrow).size() == 1, 20000);
|
||||
QCOMPARE(serviceB.itemsForDay(tomorrow).first().task.title, QStringLiteral("反向任务"));
|
||||
|
||||
serverProcess.terminate();
|
||||
QVERIFY(serverProcess.waitForFinished(5000));
|
||||
QVERIFY2(serviceA.createBlock(QStringLiteral("离线任务"),
|
||||
start.addSecs(4 * 3600),
|
||||
end.addSecs(4 * 3600),
|
||||
&error),
|
||||
qPrintable(error));
|
||||
QTest::qWait(1000);
|
||||
|
||||
serverProcess.start(serverBinary, {});
|
||||
QVERIFY2(serverProcess.waitForStarted(5000), qPrintable(serverProcess.errorString()));
|
||||
QVERIFY(waitForServer(port, 10000));
|
||||
QTRY_VERIFY_WITH_TIMEOUT(serviceB.itemsForDay(tomorrow).size() == 2, 30000);
|
||||
bool sawOffline = false;
|
||||
for (const ScheduleItem& item : serviceB.itemsForDay(tomorrow))
|
||||
{
|
||||
if (item.task.title == QStringLiteral("离线任务"))
|
||||
{
|
||||
sawOffline = true;
|
||||
}
|
||||
}
|
||||
QVERIFY(sawOffline);
|
||||
|
||||
syncA.stop();
|
||||
syncB.stop();
|
||||
serverProcess.terminate();
|
||||
serverProcess.waitForFinished(5000);
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(TestSyncIntegration)
|
||||
#include "test_sync_integration.moc"
|
||||
Reference in New Issue
Block a user