Files
Schedule/client/tests/test_services.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

520 lines
21 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <QJsonArray>
#include <QJsonObject>
#include <QSignalSpy>
#include <QTemporaryDir>
#include <QtTest>
#include <memory>
#include "core/DateTimeUtils.h"
#include "core/ProposedDiff.h"
#include "services/NotificationService.h"
#include "services/ScheduleService.h"
#include "storage/SqliteStore.h"
class TestServices : public QObject
{
Q_OBJECT
private slots:
void createAndQueryDay();
void queryMonthAndYearRange();
void updateAndMove();
void statusAndPostpone();
void deleteCascadesTask();
void overdueAndNotificationDedup();
void invalidRangeRejected();
void applyProposedCreatesOnlySelected();
void energyProfileLearnsFromHistory();
void searchFindsSchedules();
void createsRecurringSchedules();
void startAndLeadReminders();
void projectCrudAndPersistence();
};
namespace
{
struct Context
{
QTemporaryDir dir;
std::unique_ptr<SqliteStore> store;
std::unique_ptr<ScheduleService> service;
bool open()
{
if (!dir.isValid())
{
return false;
}
store = std::make_unique<SqliteStore>(dir.filePath(QStringLiteral("schedule.db")));
QString error;
if (!store->open(&error))
{
qWarning() << error;
return false;
}
service = std::make_unique<ScheduleService>(*store, QStringLiteral("test-device"));
return true;
}
};
} // namespace
void TestServices::createAndQueryDay()
{
Context context;
QVERIFY(context.open());
const QDateTime start(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
const QDateTime end = start.addSecs(90 * 60);
QString error;
QVERIFY2(context.service->createBlock(QStringLiteral("写周报"), start, end, &error), qPrintable(error));
const QVector<ScheduleItem> items = context.service->itemsForDay(QDate::currentDate());
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().task.title, QStringLiteral("写周报"));
QCOMPARE(items.first().block.startAt, start);
QCOMPARE(items.first().block.endAt, end);
QCOMPARE(items.first().task.source, QStringLiteral("manual"));
}
void TestServices::queryMonthAndYearRange()
{
Context context;
QVERIFY(context.open());
const QDate today = QDate::currentDate();
const QDate first = QDate(today.year(), today.month(), 1);
const QDate last = QDate(today.year(), today.month(), first.daysInMonth());
QString error;
QVERIFY(context.service->createBlock(QStringLiteral("月初"),
QDateTime(first, QTime(9, 0), Qt::LocalTime),
QDateTime(first, QTime(10, 0), Qt::LocalTime),
&error));
QVERIFY(context.service->createBlock(QStringLiteral("月末"),
QDateTime(last, QTime(9, 0), Qt::LocalTime),
QDateTime(last, QTime(10, 0), Qt::LocalTime),
&error));
const QDate outside = QDate(today.year() + 1, 6, 15);
QVERIFY(context.service->createBlock(QStringLiteral("范围外"),
QDateTime(outside, QTime(9, 0), Qt::LocalTime),
QDateTime(outside, QTime(10, 0), Qt::LocalTime),
&error));
const QVector<ScheduleItem> monthItems = context.service->itemsForRange(first, last);
QCOMPARE(monthItems.size(), 2);
QCOMPARE(monthItems.first().task.title, QStringLiteral("月初"));
QCOMPARE(monthItems.last().task.title, QStringLiteral("月末"));
const QVector<ScheduleItem> yearItems = context.service->itemsForRange(
QDate(today.year(), 1, 1), QDate(today.year(), 12, 31));
QCOMPARE(yearItems.size(), 2);
QVERIFY(context.service->itemsForRange(last, first).isEmpty());
}
void TestServices::updateAndMove()
{
Context context;
QVERIFY(context.open());
const QDateTime start(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
QString error;
QVERIFY2(context.service->createBlock(QStringLiteral("旧标题"),
start,
start.addSecs(60 * 60),
&error),
qPrintable(error));
QVector<ScheduleItem> items = context.service->itemsForDay(QDate::currentDate());
QCOMPARE(items.size(), 1);
const QString blockId = items.first().block.id;
const QDateTime newStart = start.addSecs(2 * 60 * 60);
const QDateTime newEnd = newStart.addSecs(45 * 60);
QVERIFY2(context.service->updateBlock(blockId,
QStringLiteral("新标题"),
newStart,
newEnd,
&error),
qPrintable(error));
items = context.service->itemsForDay(QDate::currentDate());
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().task.title, QStringLiteral("新标题"));
QCOMPARE(items.first().block.startAt, newStart);
QCOMPARE(items.first().block.endAt, newEnd);
QVERIFY(items.first().block.version >= 1);
QCOMPARE(items.first().block.deviceId, QStringLiteral("test-device"));
const QDateTime movedStart = newStart.addSecs(15 * 60);
const QDateTime movedEnd = newEnd.addSecs(15 * 60);
QVERIFY2(context.service->moveBlock(blockId, movedStart, movedEnd, &error), qPrintable(error));
items = context.service->itemsForDay(QDate::currentDate());
QCOMPARE(items.first().block.startAt, movedStart);
QCOMPARE(items.first().block.endAt, movedEnd);
}
void TestServices::statusAndPostpone()
{
Context context;
QVERIFY(context.open());
const QDateTime start(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
QString error;
QVERIFY(context.service->createBlock(QStringLiteral("任务"),
start,
start.addSecs(60 * 60),
&error));
QVector<ScheduleItem> items = context.service->itemsForDay(QDate::currentDate());
const QString blockId = items.first().block.id;
QVERIFY2(context.service->setBlockStatus(blockId, QStringLiteral("done"), &error), qPrintable(error));
items = context.service->itemsForDay(QDate::currentDate());
QCOMPARE(items.first().block.status, QStringLiteral("done"));
QVERIFY(items.first().block.doneAt.has_value());
QVERIFY2(context.service->postponeBlock(blockId, 15, &error), qPrintable(error));
items = context.service->itemsForDay(QDate::currentDate());
QCOMPARE(items.first().block.status, QStringLiteral("scheduled"));
QVERIFY(!items.first().block.doneAt.has_value());
QCOMPARE(items.first().block.startAt, start.addSecs(15 * 60));
QCOMPARE(items.first().block.endAt, start.addSecs(75 * 60));
QVERIFY(!context.service->setBlockStatus(blockId, QStringLiteral("unknown"), &error));
QVERIFY(!error.isEmpty());
}
void TestServices::deleteCascadesTask()
{
Context context;
QVERIFY(context.open());
const QDateTime start(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
QString error;
QVERIFY(context.service->createBlock(QStringLiteral("待删除"),
start,
start.addSecs(60 * 60),
&error));
QVector<ScheduleItem> items = context.service->itemsForDay(QDate::currentDate());
const QString blockId = items.first().block.id;
const QString taskId = items.first().task.id;
QVERIFY(context.store->taskById(taskId).has_value());
QVERIFY2(context.service->deleteBlock(blockId, &error), qPrintable(error));
QVERIFY(context.service->itemsForDay(QDate::currentDate()).isEmpty());
QVERIFY(!context.store->taskById(taskId).has_value());
}
void TestServices::overdueAndNotificationDedup()
{
Context context;
QVERIFY(context.open());
const QDateTime now = QDateTime::currentDateTime();
const QDateTime start = now.addSecs(-2 * 60 * 60);
const QDateTime end = now.addSecs(-60 * 60);
QString error;
QVERIFY(context.service->createBlock(QStringLiteral("已超时"), start, end, &error));
const QVector<ScheduleItem> overdue = context.service->overdueItems(now);
QCOMPARE(overdue.size(), 1);
const QString overdueId = overdue.first().block.id;
NotificationService notifications(*context.service);
QSignalSpy detectedSpy(&notifications, &NotificationService::overdueDetected);
QSignalSpy listSpy(&notifications, &NotificationService::overdueListChanged);
notifications.checkNow();
QCOMPARE(detectedSpy.count(), 1);
QCOMPARE(listSpy.count(), 1);
QCOMPARE(notifications.overdueItems().size(), 1);
notifications.checkNow();
QCOMPARE(detectedSpy.count(), 1);
QVERIFY(context.service->setBlockStatus(overdueId, QStringLiteral("done"), &error));
QCOMPARE(context.service->overdueItems(QDateTime::currentDateTime()).size(), 0);
QCOMPARE(notifications.overdueItems().size(), 0);
}
void TestServices::invalidRangeRejected()
{
Context context;
QVERIFY(context.open());
const QDateTime start(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
QString error;
QVERIFY(!context.service->createBlock(QStringLiteral("倒序"),
start,
start.addSecs(-60 * 60),
&error));
QVERIFY(!error.isEmpty());
error.clear();
QVERIFY(!context.service->createBlock(QStringLiteral("太短"),
start,
start.addSecs(60),
&error));
QVERIFY(!error.isEmpty());
QVERIFY(context.service->itemsForDay(QDate::currentDate()).isEmpty());
}
void TestServices::applyProposedCreatesOnlySelected()
{
Context context;
QVERIFY(context.open());
const QDate today = QDate::currentDate();
const QDateTime startA(today, QTime(9, 0), Qt::LocalTime);
const QDateTime endA = startA.addSecs(60 * 60);
const QDateTime startB(today, QTime(11, 0), Qt::LocalTime);
const QDateTime endB = startB.addSecs(30 * 60);
const auto makeCreate = [](const QString& tempId,
const QString& title,
const QDateTime& start,
const QDateTime& end) {
QJsonObject task;
task.insert(QStringLiteral("title"), title);
task.insert(QStringLiteral("description"), QStringLiteral("AI 生成"));
task.insert(QStringLiteral("priority"), 2);
task.insert(QStringLiteral("energy_req"), 1);
QJsonObject block;
block.insert(QStringLiteral("start_at"), DateTimeUtils::toDb(start));
block.insert(QStringLiteral("end_at"), DateTimeUtils::toDb(end));
block.insert(QStringLiteral("granularity"), 15);
QJsonObject create;
create.insert(QStringLiteral("temp_id"), tempId);
create.insert(QStringLiteral("task"), task);
create.insert(QStringLiteral("block"), block);
return create;
};
ProposedDiff diff;
diff.id = QStringLiteral("diff-2");
diff.createdAt = DateTimeUtils::toDb(QDateTime::currentDateTimeUtc());
diff.explanation = QStringLiteral("新增两个日程");
diff.state = QStringLiteral("pending");
diff.payload = QJsonObject{
{QStringLiteral("creates"),
QJsonArray{makeCreate(QStringLiteral("a"), QStringLiteral("AI 任务 A"), startA, endA),
makeCreate(QStringLiteral("b"), QStringLiteral("AI 任务 B"), startB, endB)}},
{QStringLiteral("updates"), QJsonArray{}},
{QStringLiteral("deletes"), QJsonArray{}}};
QString error;
QVERIFY2(context.service->applyProposedCreates(diff, {QStringLiteral("a")}, &error), qPrintable(error));
QVector<ScheduleItem> items = context.service->itemsForDay(today);
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().task.title, QStringLiteral("AI 任务 A"));
QCOMPARE(items.first().task.source, QStringLiteral("ai_chat"));
QCOMPARE(items.first().block.startAt, startA);
QCOMPARE(items.first().block.endAt, endA);
QVERIFY2(context.service->applyProposedCreates(diff, {QStringLiteral("b")}, &error), qPrintable(error));
items = context.service->itemsForDay(today);
QCOMPARE(items.size(), 2);
QVERIFY(!context.service->applyProposedCreates(diff, {}, &error));
}
void TestServices::energyProfileLearnsFromHistory()
{
Context context;
QVERIFY(context.open());
const QDate date = QDate::currentDate();
QJsonObject task;
task.insert(QStringLiteral("title"), QStringLiteral("高精力任务"));
task.insert(QStringLiteral("priority"), 2);
task.insert(QStringLiteral("energy_req"), 2);
QJsonObject block;
block.insert(QStringLiteral("start_at"),
DateTimeUtils::toDb(QDateTime(date, QTime(13, 0), Qt::LocalTime)));
block.insert(QStringLiteral("end_at"),
DateTimeUtils::toDb(QDateTime(date, QTime(14, 0), Qt::LocalTime)));
QJsonObject create;
create.insert(QStringLiteral("temp_id"), QStringLiteral("energy"));
create.insert(QStringLiteral("task"), task);
create.insert(QStringLiteral("block"), block);
ProposedDiff diff;
diff.id = QStringLiteral("energy-diff");
diff.createdAt = DateTimeUtils::toDb(QDateTime::currentDateTimeUtc());
diff.state = QStringLiteral("pending");
diff.payload = QJsonObject{
{QStringLiteral("creates"), QJsonArray{create}},
{QStringLiteral("updates"), QJsonArray{}},
{QStringLiteral("deletes"), QJsonArray{}},
};
QString error;
QVERIFY2(context.service->applyProposedCreates(diff, {QStringLiteral("energy")}, &error),
qPrintable(error));
const QVector<ScheduleItem> items = context.service->itemsForDay(date);
QCOMPARE(items.size(), 1);
const QString blockId = items.first().block.id;
QCOMPARE(context.service->energyLevelFor(QDateTime(date, QTime(13, 0), Qt::LocalTime)), 0);
QVERIFY2(context.service->setBlockStatus(blockId, QStringLiteral("done"), &error), qPrintable(error));
QCOMPARE(context.service->energyLevelFor(QDateTime(date, QTime(13, 0), Qt::LocalTime)), 2);
QVERIFY(context.service->dailyReview(date).energySummary.contains(QStringLiteral("13:00–14:00")));
ScheduleService reloaded(*context.store, QStringLiteral("test-device"));
QCOMPARE(reloaded.energyLevelFor(QDateTime(date, QTime(13, 0), Qt::LocalTime)), 2);
QVERIFY2(context.service->setBlockStatus(blockId, QStringLiteral("scheduled"), &error), qPrintable(error));
QCOMPARE(context.service->energyLevelFor(QDateTime(date, QTime(13, 0), Qt::LocalTime)), 0);
}
void TestServices::searchFindsSchedules()
{
Context context;
QVERIFY(context.open());
const QDate day = QDate::currentDate();
QString error;
QVERIFY(context.service->createBlock(QStringLiteral("写周报"),
QDateTime(day, QTime(9, 0), Qt::LocalTime),
QDateTime(day, QTime(10, 0), Qt::LocalTime),
&error));
QVERIFY(context.service->createBlock(QStringLiteral("团队会议"),
QDateTime(day, QTime(11, 0), Qt::LocalTime),
QDateTime(day, QTime(12, 0), Qt::LocalTime),
&error));
const QVector<ScheduleItem> weekly = context.service->search(QStringLiteral("周报"));
QCOMPARE(weekly.size(), 1);
QCOMPARE(weekly.first().task.title, QStringLiteral("写周报"));
const QVector<ScheduleItem> all = context.service->search(QString());
QCOMPARE(all.size(), 2);
QStringList titles;
for (const ScheduleItem& item : all)
{
titles.append(item.task.title);
}
QVERIFY(titles.contains(QStringLiteral("写周报")));
QVERIFY(titles.contains(QStringLiteral("团队会议")));
QCOMPARE(context.service->search(QStringLiteral("%")).size(), 0);
}
void TestServices::createsRecurringSchedules()
{
Context context;
QVERIFY(context.open());
const QDate first = QDate::currentDate().addDays(10);
const QDateTime start(first, QTime(9, 0), Qt::LocalTime);
const QDateTime end(first, QTime(10, 0), Qt::LocalTime);
int count = 0;
QString error;
QVERIFY2(context.service->createRecurringBlock(QStringLiteral("每日复盘"),
start,
end,
RecurrenceFrequency::Daily,
first.addDays(4),
&count,
&error),
qPrintable(error));
QCOMPARE(count, 5);
for (int offset = 0; offset < 5; ++offset)
{
const QVector<ScheduleItem> items = context.service->itemsForDay(first.addDays(offset));
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().task.title, QStringLiteral("每日复盘"));
QCOMPARE(items.first().block.startAt.time(), QTime(9, 0));
}
const QString taskId = context.service->itemsForDay(first).first().task.id;
const auto recurrence = context.store->recurrenceForTask(taskId);
QVERIFY(recurrence.has_value());
QCOMPARE(recurrence->rule, QStringLiteral("FREQ=DAILY"));
QCOMPARE(recurrence->until.value(), first.addDays(4));
QVERIFY(!context.service->createRecurringBlock(QStringLiteral("错误"),
start,
end,
RecurrenceFrequency::Weekly,
first.addDays(-1),
&count,
&error));
QVERIFY(!error.isEmpty());
}
void TestServices::startAndLeadReminders()
{
Context context;
QVERIFY(context.open());
const QDateTime now = QDateTime::currentDateTime();
QString error;
QVERIFY(context.service->createBlock(QStringLiteral("即将开始"),
now.addSecs(5 * 60),
now.addSecs(65 * 60),
&error));
QVERIFY(context.service->createBlock(QStringLiteral("已经开始"),
now.addSecs(-5 * 60),
now.addSecs(55 * 60),
&error));
NotificationService notifications(*context.service);
QSignalSpy leadSpy(&notifications, &NotificationService::leadReminderDue);
QSignalSpy startSpy(&notifications, &NotificationService::startReminderDue);
notifications.checkNow();
QCOMPARE(leadSpy.count(), 1);
QCOMPARE(startSpy.count(), 1);
QCOMPARE(leadSpy.first().at(0).value<ScheduleItem>().task.title, QStringLiteral("即将开始"));
QCOMPARE(leadSpy.first().at(1).toInt(), 10);
QCOMPARE(startSpy.first().at(0).value<ScheduleItem>().task.title, QStringLiteral("已经开始"));
notifications.checkNow();
QCOMPARE(leadSpy.count(), 1);
QCOMPARE(startSpy.count(), 1);
notifications.setLeadMinutes(0);
QCOMPARE(leadSpy.count(), 1);
}
void TestServices::projectCrudAndPersistence()
{
Context context;
QVERIFY(context.open());
const QDate deadline = QDate::currentDate().addMonths(1);
QString error;
QVERIFY2(context.service->createProject(QStringLiteral("飞行控制器项目"),
QStringLiteral("完成飞控板固件与测试"),
deadline,
&error),
qPrintable(error));
QVector<Project> projects = context.service->projects();
QCOMPARE(projects.size(), 1);
QCOMPARE(projects.first().title, QStringLiteral("飞行控制器项目"));
QCOMPARE(projects.first().deadline.value(), deadline);
const QString id = projects.first().id;
QVERIFY2(context.service->updateProject(id,
QStringLiteral("飞行控制器项目 v2"),
QStringLiteral("完成固件、测试与文档"),
deadline.addDays(7),
&error),
qPrintable(error));
auto updated = context.service->projectById(id);
QVERIFY(updated.has_value());
QCOMPARE(updated->title, QStringLiteral("飞行控制器项目 v2"));
QCOMPARE(updated->deadline.value(), deadline.addDays(7));
ScheduleService reloaded(*context.store, QStringLiteral("test-device"));
QCOMPARE(reloaded.projects().size(), 1);
QVERIFY2(context.service->setProjectStatus(id, QStringLiteral("done"), &error), qPrintable(error));
QVERIFY(context.service->projects().isEmpty());
QCOMPARE(context.store->outboxCount() > 0, true);
QVERIFY2(context.service->deleteProject(id, &error), qPrintable(error));
QVERIFY(!context.service->projectById(id).has_value());
}
QTEST_GUILESS_MAIN(TestServices)
#include "test_services.moc"