#include #include "core/DailyInsights.h" #include "core/EnergyProfile.h" class TestEnergy : public QObject { Q_OBJECT private slots: void defaultProfileHasCurve(); void learnsFromSamples(); void rejectsInvalidCoordinates(); void findsGhostWindows(); void buildsDailyReviewAndSuggestions(); }; void TestEnergy::defaultProfileHasCurve() { const EnergyProfile profile = EnergyProfile::defaults(); QCOMPARE(profile.level(1, 9), 2); QCOMPARE(profile.level(1, 13), 0); QCOMPARE(profile.level(7, 9), 0); QCOMPARE(profile.level(7, 10), 1); QCOMPARE(profile.level(0, 9), -1); QCOMPARE(EnergyProfile::label(2), QStringLiteral("高")); QCOMPARE(profile.levels().size(), 7 * 24); } void TestEnergy::learnsFromSamples() { EnergyProfile profile = EnergyProfile::defaults(); profile.learn({{1, 13, 2}, {1, 13, 2}, {1, 9, 0}}); QCOMPARE(profile.level(1, 13), 2); QCOMPARE(profile.level(1, 9), 0); QCOMPARE(profile.level(2, 9), 2); profile.setLevel(1, 13, 1); QCOMPARE(profile.level(1, 13), 1); } void TestEnergy::rejectsInvalidCoordinates() { EnergyProfile profile = EnergyProfile::defaults(); QVERIFY(!profile.setLevel(0, 9, 2)); QVERIFY(!profile.setLevel(8, 9, 2)); QVERIFY(!profile.setLevel(1, 24, 2)); QCOMPARE(profile.level(1, 24), -1); QCOMPARE(profile.level(QDateTime()), -1); } namespace { QDate testDate() { return QDate(2026, 1, 5); } ScheduleItem item(int startHour, int endHour, const QString& status, const QString& title) { ScheduleItem result; result.task.title = title; result.task.energyReq = 1; result.block.startAt = QDateTime(testDate(), QTime(startHour, 0), Qt::LocalTime); result.block.endAt = QDateTime(testDate(), QTime(endHour, 0), Qt::LocalTime); result.block.status = status; return result; } } // namespace void TestEnergy::findsGhostWindows() { const QVector items = { item(9, 10, QStringLiteral("done"), QStringLiteral("A")), item(13, 14, QStringLiteral("scheduled"), QStringLiteral("B")), item(11, 12, QStringLiteral("dropped"), QStringLiteral("C")), }; const auto windows = findFreeWindows(items, testDate(), QDateTime(testDate(), QTime(8, 0), Qt::LocalTime), 8, 22, 30); QCOMPARE(windows.size(), 3); QCOMPARE(windows[0].minutes, 60); QCOMPARE(windows[0].start.time(), QTime(8, 0)); QCOMPARE(windows[1].minutes, 180); QCOMPARE(windows[1].start.time(), QTime(10, 0)); QCOMPARE(windows[2].minutes, 8 * 60); QCOMPARE(windows[2].start.time(), QTime(14, 0)); } void TestEnergy::buildsDailyReviewAndSuggestions() { const QVector items = { item(9, 10, QStringLiteral("done"), QStringLiteral("完成")), item(13, 14, QStringLiteral("scheduled"), QStringLiteral("超时")), }; const QDateTime now(testDate(), QTime(15, 0), Qt::LocalTime); const DailyReview review = buildDailyReview(items, testDate(), now); QCOMPARE(review.total, 2); QCOMPARE(review.done, 1); QCOMPARE(review.active, 1); QCOMPARE(review.overdue, 1); QCOMPARE(review.focusedMinutes, 60); QCOMPARE(review.completionPercent, 50); QVERIFY(!review.ghostWindows.isEmpty()); const auto suggestions = buildSuggestions(items, now, 3); QCOMPARE(suggestions.size(), 2); QCOMPARE(suggestions[0].kind, Suggestion::Kind::Overdue); QCOMPARE(suggestions[1].kind, Suggestion::Kind::GhostTime); const auto evening = buildSuggestions(items, QDateTime(testDate(), QTime(19, 0), Qt::LocalTime), 3); QCOMPARE(evening.size(), 3); QCOMPARE(evening[2].kind, Suggestion::Kind::Review); } QTEST_GUILESS_MAIN(TestEnergy) #include "test_energy.moc"