35cabb37b2
- Add project model, schema v4, sync triggers, and server support\n- Add project panel, deadline countdown, manual create/edit/complete/delete\n- Parse AI project operations and show them in the proposal diff\n- Sync projects to Android and display them read-only\n- Rebuild AppImage and update validation docs and screenshots
220 lines
8.7 KiB
C++
220 lines
8.7 KiB
C++
#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("跨设备任务"));
|
|
|
|
const QString sharedBlockId = serviceA.itemsForDay(tomorrow).first().block.id;
|
|
QVERIFY2(serviceA.setBlockStatus(sharedBlockId, QStringLiteral("done"), &error), qPrintable(error));
|
|
QTRY_VERIFY_WITH_TIMEOUT(!serviceB.itemsForDay(tomorrow).isEmpty() &&
|
|
serviceB.itemsForDay(tomorrow).first().block.status ==
|
|
QStringLiteral("done"),
|
|
20000);
|
|
QVERIFY2(serviceB.setBlockStatus(sharedBlockId, QStringLiteral("scheduled"), &error),
|
|
qPrintable(error));
|
|
QTRY_VERIFY_WITH_TIMEOUT(!serviceA.itemsForDay(tomorrow).isEmpty() &&
|
|
serviceA.itemsForDay(tomorrow).first().block.status ==
|
|
QStringLiteral("scheduled"),
|
|
20000);
|
|
|
|
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);
|
|
|
|
const QDate projectDeadline = QDate::currentDate().addMonths(1);
|
|
QVERIFY2(serviceA.createProject(QStringLiteral("长期项目"),
|
|
QStringLiteral("跨月目标"),
|
|
projectDeadline,
|
|
&error),
|
|
qPrintable(error));
|
|
QTRY_VERIFY_WITH_TIMEOUT(serviceB.projects().size() == 1, 20000);
|
|
QCOMPARE(serviceB.projects().first().title, QStringLiteral("长期项目"));
|
|
QVERIFY2(serviceB.updateProject(serviceB.projects().first().id,
|
|
QStringLiteral("长期项目 v2"),
|
|
QStringLiteral("跨月目标 v2"),
|
|
projectDeadline.addDays(14),
|
|
&error),
|
|
qPrintable(error));
|
|
QTRY_VERIFY_WITH_TIMEOUT(!serviceA.projects().isEmpty() &&
|
|
serviceA.projects().first().title == QStringLiteral("长期项目 v2"),
|
|
20000);
|
|
QVERIFY2(serviceA.deleteProject(serviceA.projects().first().id, &error), qPrintable(error));
|
|
QTRY_VERIFY_WITH_TIMEOUT(serviceB.projects().isEmpty(), 20000);
|
|
|
|
syncA.stop();
|
|
syncB.stop();
|
|
serverProcess.terminate();
|
|
serverProcess.waitForFinished(5000);
|
|
}
|
|
|
|
QTEST_GUILESS_MAIN(TestSyncIntegration)
|
|
#include "test_sync_integration.moc"
|