Files
Schedule/client/tests/test_services.cpp
T
Misaki 9bafa71667 feat: add energy-aware scheduling and daily insights
- 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
2026-10-26 19:08:09 +08:00

367 lines
15 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();
};
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);
}
QTEST_GUILESS_MAIN(TestServices)
#include "test_services.moc"