Files
Schedule/client/tests/test_storage.cpp
T
Misaki 35cabb37b2 feat: add long-term projects with AI creation
- 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
2026-10-31 15:52:48 +08:00

328 lines
12 KiB
C++

#include <QJsonObject>
#include <QTemporaryDir>
#include <QtTest>
#include "core/DateTimeUtils.h"
#include "core/ProposedDiff.h"
#include "storage/SqliteStore.h"
class TestStorage : public QObject
{
Q_OBJECT
private slots:
void migrationCreatesSchema();
void taskAndBlockRoundTrip();
void blockRangeIncludesOverlap();
void proposedDiffRoundTrip();
void outboxCapturesLocalChanges();
void remoteApplySuppressesOutboxAndUsesLww();
void remoteCompletionWinsOverOlderTimestamp();
};
void TestStorage::migrationCreatesSchema()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
QCOMPARE(store.schemaVersion(), 4);
store.close();
QVERIFY2(store.open(&error), qPrintable(error));
QCOMPARE(store.schemaVersion(), 4);
}
void TestStorage::taskAndBlockRoundTrip()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
const QDateTime now = QDateTime::currentDateTime();
Task task;
task.id = QStringLiteral("task-1");
task.title = QStringLiteral("写周报");
task.createdAt = now;
task.updatedAt = now;
task.deviceId = QStringLiteral("device-1");
QVERIFY2(store.insertTask(task), qPrintable(store.lastError()));
TimeBlock block;
block.id = QStringLiteral("block-1");
block.taskId = task.id;
block.startAt = QDateTime(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
block.endAt = block.startAt.addSecs(90 * 60);
block.createdAt = now;
block.updatedAt = now;
block.deviceId = QStringLiteral("device-1");
QVERIFY2(store.insertTimeBlock(block), qPrintable(store.lastError()));
const QVector<ScheduleItem> items = store.blocksInRange(
DateTimeUtils::localDayStartUtc(QDate::currentDate()),
DateTimeUtils::localDayEndUtc(QDate::currentDate()));
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().task.title, QStringLiteral("写周报"));
QCOMPARE(items.first().block.startAt, block.startAt);
QCOMPARE(items.first().block.endAt, block.endAt);
QCOMPARE(items.first().block.granularity, 15);
QVERIFY(items.first().block.splittable);
QVERIFY(items.first().block.breakable);
const auto loadedTask = store.taskById(task.id);
QVERIFY(loadedTask.has_value());
QCOMPARE(loadedTask->title, task.title);
QVERIFY2(store.deleteTimeBlock(block.id), qPrintable(store.lastError()));
QVERIFY(store.blocksInRange(DateTimeUtils::localDayStartUtc(QDate::currentDate()),
DateTimeUtils::localDayEndUtc(QDate::currentDate()))
.isEmpty());
}
void TestStorage::blockRangeIncludesOverlap()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
const QDate today = QDate::currentDate();
const QDateTime now = QDateTime::currentDateTime();
Task task;
task.id = QStringLiteral("task-2");
task.title = QStringLiteral("跨午夜");
task.createdAt = now;
task.updatedAt = now;
task.deviceId = QStringLiteral("device-1");
QVERIFY(store.insertTask(task));
TimeBlock block;
block.id = QStringLiteral("block-2");
block.taskId = task.id;
block.startAt = QDateTime(today.addDays(-1), QTime(23, 30), Qt::LocalTime);
block.endAt = QDateTime(today, QTime(0, 30), Qt::LocalTime);
block.createdAt = now;
block.updatedAt = now;
block.deviceId = QStringLiteral("device-1");
QVERIFY(store.insertTimeBlock(block));
const QVector<ScheduleItem> items =
store.blocksInRange(DateTimeUtils::localDayStartUtc(today), DateTimeUtils::localDayEndUtc(today));
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().block.id, block.id);
}
void TestStorage::proposedDiffRoundTrip()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
ProposedDiff diff;
diff.id = QStringLiteral("diff-1");
diff.createdAt = QStringLiteral("2026-10-26T00:00:00Z");
diff.explanation = QStringLiteral("新增一个日程");
diff.state = QStringLiteral("pending");
diff.payload = QJsonObject{{QStringLiteral("creates"), QJsonArray{}}};
QVERIFY2(store.insertProposedDiff(diff), qPrintable(store.lastError()));
const auto loaded = store.proposedDiffById(diff.id);
QVERIFY(loaded.has_value());
QCOMPARE(loaded->explanation, diff.explanation);
QCOMPARE(loaded->state, QStringLiteral("pending"));
QVERIFY(loaded->payload.contains(QStringLiteral("creates")));
QVERIFY2(store.updateProposedDiffState(diff.id, QStringLiteral("applied"), QStringLiteral("2026-10-26T01:00:00Z")),
qPrintable(store.lastError()));
const auto updated = store.proposedDiffById(diff.id);
QVERIFY(updated.has_value());
QCOMPARE(updated->state, QStringLiteral("applied"));
QCOMPARE(updated->resolvedAt, QStringLiteral("2026-10-26T01:00:00Z"));
}
void TestStorage::outboxCapturesLocalChanges()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
QCOMPARE(store.outboxCount(), 0);
const QDateTime now = QDateTime::currentDateTime();
Task task;
task.id = QStringLiteral("sync-task");
task.title = QStringLiteral("同步任务");
task.createdAt = now;
task.updatedAt = now;
task.deviceId = QStringLiteral("device-1");
QVERIFY(store.insertTask(task));
TimeBlock block;
block.id = QStringLiteral("sync-block");
block.taskId = task.id;
block.startAt = QDateTime(QDate::currentDate(), QTime(10, 0), Qt::LocalTime);
block.endAt = block.startAt.addSecs(3600);
block.createdAt = now;
block.updatedAt = now;
block.deviceId = QStringLiteral("device-1");
QVERIFY(store.insertTimeBlock(block));
QCOMPARE(store.outboxCount(), 2);
const QVector<SyncOutboxItem> items = store.outboxBatch(10);
QCOMPARE(items.size(), 2);
QCOMPARE(items.first().entityType, QStringLiteral("task"));
QCOMPARE(items.first().op, QStringLiteral("upsert"));
QCOMPARE(items.first().payload.value(QStringLiteral("title")).toString(), QStringLiteral("同步任务"));
task.title = QStringLiteral("同步任务 v2");
task.updatedAt = now.addSecs(1);
task.version = 1;
QVERIFY(store.updateTask(task));
QCOMPARE(store.outboxCount(), 3);
QVERIFY(store.deleteTimeBlock(block.id));
QVERIFY(store.deleteTask(task.id));
QCOMPARE(store.outboxCount(), 5);
const qint64 maxId = store.outboxBatch(100).last().id;
QVERIFY(store.clearOutboxThrough(maxId));
QCOMPARE(store.outboxCount(), 0);
}
void TestStorage::remoteApplySuppressesOutboxAndUsesLww()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
QVERIFY(store.setSyncCursor(QStringLiteral("device-1"), 42));
QCOMPARE(store.syncCursor(QStringLiteral("device-1")), 42);
QVERIFY(store.beginTransaction());
QVERIFY(store.setRemoteApplySuppressed(true));
RemoteChange taskChange;
taskChange.seq = 1;
taskChange.deviceId = QStringLiteral("remote-device");
taskChange.entityType = QStringLiteral("task");
taskChange.entityId = QStringLiteral("remote-task");
taskChange.op = QStringLiteral("upsert");
taskChange.version = 1;
taskChange.updatedAt = QStringLiteral("2026-10-26T10:00:00Z");
taskChange.payload = QJsonObject{
{QStringLiteral("id"), QStringLiteral("remote-task")},
{QStringLiteral("title"), QStringLiteral("远程任务")},
{QStringLiteral("status"), QStringLiteral("active")},
{QStringLiteral("priority"), 1},
{QStringLiteral("created_at"), QStringLiteral("2026-10-26T09:00:00Z")},
{QStringLiteral("updated_at"), QStringLiteral("2026-10-26T10:00:00Z")},
};
QVERIFY(store.applyRemoteChange(taskChange));
QVERIFY(store.setRemoteApplySuppressed(false));
QVERIFY(store.commit());
QCOMPARE(store.outboxCount(), 0);
QVERIFY(store.taskById(QStringLiteral("remote-task")).has_value());
// 较旧的远程版本应被 LWW 忽略。
taskChange.updatedAt = QStringLiteral("2026-10-26T09:30:00Z");
taskChange.payload.insert(QStringLiteral("title"), QStringLiteral("旧标题"));
QVERIFY(store.beginTransaction());
QVERIFY(store.setRemoteApplySuppressed(true));
QVERIFY(store.applyRemoteChange(taskChange));
QVERIFY(store.setRemoteApplySuppressed(false));
QVERIFY(store.commit());
QCOMPARE(store.taskById(QStringLiteral("remote-task"))->title, QStringLiteral("远程任务"));
// 毫秒级时间戳必须可比较,且能覆盖同秒的无毫秒旧时间戳。
taskChange.updatedAt = QStringLiteral("2026-10-26T10:00:00.500Z");
taskChange.payload.insert(QStringLiteral("title"), QStringLiteral("毫秒版本"));
QVERIFY(store.beginTransaction());
QVERIFY(store.setRemoteApplySuppressed(true));
QVERIFY(store.applyRemoteChange(taskChange));
QVERIFY(store.setRemoteApplySuppressed(false));
QVERIFY(store.commit());
QCOMPARE(store.taskById(QStringLiteral("remote-task"))->title, QStringLiteral("毫秒版本"));
// 同时间戳删除优先,避免墓碑被等时间戳的 upsert 覆盖。
taskChange.op = QStringLiteral("delete");
taskChange.updatedAt = QStringLiteral("2026-10-26T10:00:00.500Z");
QVERIFY(store.beginTransaction());
QVERIFY(store.setRemoteApplySuppressed(true));
QVERIFY(store.applyRemoteChange(taskChange));
QVERIFY(store.setRemoteApplySuppressed(false));
QVERIFY(store.commit());
QVERIFY(!store.taskById(QStringLiteral("remote-task")).has_value());
QCOMPARE(store.outboxCount(), 0);
}
void TestStorage::remoteCompletionWinsOverOlderTimestamp()
{
QTemporaryDir dir;
QVERIFY(dir.isValid());
SqliteStore store(dir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
const QDateTime now = QDateTime::currentDateTime();
Task task;
task.id = QStringLiteral("completion-task");
task.title = QStringLiteral("完成同步");
task.createdAt = now;
task.updatedAt = now;
task.deviceId = QStringLiteral("device-a");
QVERIFY(store.insertTask(task));
TimeBlock block;
block.id = QStringLiteral("completion-block");
block.taskId = task.id;
block.startAt = now.addSecs(3600);
block.endAt = now.addSecs(7200);
block.createdAt = now;
block.updatedAt = now;
block.deviceId = QStringLiteral("device-a");
QVERIFY(store.insertTimeBlock(block));
RemoteChange change;
change.seq = 2;
change.deviceId = QStringLiteral("device-b");
change.entityType = QStringLiteral("timeblock");
change.entityId = block.id;
change.op = QStringLiteral("upsert");
change.version = 1;
change.updatedAt = DateTimeUtils::toDb(now.addSecs(-60));
change.payload = QJsonObject{
{QStringLiteral("id"), block.id},
{QStringLiteral("task_id"), task.id},
{QStringLiteral("start_at"), DateTimeUtils::toDb(block.startAt)},
{QStringLiteral("end_at"), DateTimeUtils::toDb(block.endAt)},
{QStringLiteral("granularity"), 15},
{QStringLiteral("splittable"), 1},
{QStringLiteral("breakable"), 1},
{QStringLiteral("status"), QStringLiteral("done")},
{QStringLiteral("done_at"), DateTimeUtils::toDb(now.addSecs(-60))},
{QStringLiteral("created_at"), DateTimeUtils::toDb(block.createdAt)},
{QStringLiteral("updated_at"), DateTimeUtils::toDb(now.addSecs(-60))},
{QStringLiteral("version"), 1},
{QStringLiteral("device_id"), QStringLiteral("device-b")},
};
QVERIFY(store.beginTransaction());
QVERIFY(store.setRemoteApplySuppressed(true));
QVERIFY(store.applyRemoteChange(change));
QVERIFY(store.setRemoteApplySuppressed(false));
QVERIFY(store.commit());
const auto updated = store.timeBlockById(block.id);
QVERIFY(updated.has_value());
QCOMPARE(updated->status, QStringLiteral("done"));
QVERIFY(updated->doneAt.has_value());
}
QTEST_GUILESS_MAIN(TestStorage)
#include "test_storage.moc"