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
This commit is contained in:
@@ -26,6 +26,10 @@ add_executable(test_scheduler test_scheduler.cpp)
|
||||
target_link_libraries(test_scheduler PRIVATE schedule_core Qt6::Test schedule_warnings)
|
||||
add_test(NAME scheduler COMMAND test_scheduler)
|
||||
|
||||
add_executable(test_energy test_energy.cpp)
|
||||
target_link_libraries(test_energy PRIVATE schedule_core Qt6::Test schedule_warnings)
|
||||
add_test(NAME energy COMMAND test_energy)
|
||||
|
||||
add_executable(test_ai test_ai.cpp)
|
||||
target_link_libraries(test_ai PRIVATE schedule_ai Qt6::Test schedule_warnings)
|
||||
add_test(NAME ai COMMAND test_ai)
|
||||
@@ -47,7 +51,7 @@ endif()
|
||||
if(WIN32)
|
||||
set_target_properties(
|
||||
test_storage test_services test_grid test_mainwindow test_compactwindow
|
||||
test_config test_scheduler test_ai test_openai_provider test_sync_integration
|
||||
test_config test_scheduler test_energy test_ai test_openai_provider test_sync_integration
|
||||
PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
|
||||
)
|
||||
endif()
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "storage/SqliteStore.h"
|
||||
#include "ui/CompactWindow.h"
|
||||
#include "ui/MainWindow.h"
|
||||
#include "ui/dialogs/DailyReviewDialog.h"
|
||||
#include "ui/dialogs/SettingsDialog.h"
|
||||
#include "ui/panels/DiffPreviewCard.h"
|
||||
#include "ui/theme/ThemeManager.h"
|
||||
@@ -60,6 +61,7 @@ class TestCompactWindow : public QObject
|
||||
private slots:
|
||||
void initTestCase();
|
||||
void compactOpensScheduleAndReturns();
|
||||
void opensDailyReview();
|
||||
void aiChatShowsDiffAndAccepts();
|
||||
void aiChatShowsMultiOperationDiff();
|
||||
|
||||
@@ -217,6 +219,13 @@ void TestCompactWindow::compactOpensScheduleAndReturns()
|
||||
}
|
||||
saveWindow(scheduleWindow, QStringLiteral("schedule-day.png"));
|
||||
|
||||
if (auto* grid = scheduleWindow->findChild<ScheduleGridView*>())
|
||||
{
|
||||
grid->scrollToHour(18);
|
||||
QTest::qWait(80);
|
||||
}
|
||||
saveWindow(scheduleWindow, QStringLiteral("schedule-ghost.png"));
|
||||
|
||||
scheduleWindow->setView(MainWindow::View::Month);
|
||||
QTest::qWait(200);
|
||||
QCOMPARE(scheduleWindow->view(), MainWindow::View::Month);
|
||||
@@ -266,6 +275,59 @@ void TestCompactWindow::compactOpensScheduleAndReturns()
|
||||
QTest::mouseClick(settingsButton, Qt::LeftButton);
|
||||
}
|
||||
|
||||
void TestCompactWindow::opensDailyReview()
|
||||
{
|
||||
QTemporaryDir databaseDir;
|
||||
QVERIFY(databaseDir.isValid());
|
||||
SqliteStore store(databaseDir.filePath(QStringLiteral("schedule.db")));
|
||||
QString error;
|
||||
QVERIFY2(store.open(&error), qPrintable(error));
|
||||
|
||||
ThemeManager theme(*m_settings);
|
||||
theme.initialize();
|
||||
ScheduleService service(store, QStringLiteral("review-test"));
|
||||
NotificationService notifications(service);
|
||||
AIService aiService(service, store, *m_settings);
|
||||
createSampleData(service, QDate::currentDate());
|
||||
|
||||
CompactWindow compact(service, notifications, aiService, theme, *m_settings);
|
||||
compact.show();
|
||||
QVERIFY(QTest::qWaitForWindowExposed(&compact));
|
||||
QTest::qWait(300);
|
||||
|
||||
ElaToolButton* reviewButton = nullptr;
|
||||
for (ElaToolButton* button : compact.findChildren<ElaToolButton*>())
|
||||
{
|
||||
if (button->toolTip().startsWith(QStringLiteral("今日复盘")))
|
||||
{
|
||||
reviewButton = button;
|
||||
break;
|
||||
}
|
||||
}
|
||||
QVERIFY(reviewButton);
|
||||
|
||||
QTimer::singleShot(400, []() {
|
||||
for (QWidget* widget : QApplication::topLevelWidgets())
|
||||
{
|
||||
if (auto* dialog = qobject_cast<DailyReviewDialog*>(widget))
|
||||
{
|
||||
saveWindow(dialog, QStringLiteral("review.png"));
|
||||
dialog->reject();
|
||||
}
|
||||
}
|
||||
});
|
||||
QTimer::singleShot(2000, []() {
|
||||
for (QWidget* widget : QApplication::topLevelWidgets())
|
||||
{
|
||||
if (auto* dialog = qobject_cast<DailyReviewDialog*>(widget))
|
||||
{
|
||||
dialog->reject();
|
||||
}
|
||||
}
|
||||
});
|
||||
QTest::mouseClick(reviewButton, Qt::LeftButton);
|
||||
}
|
||||
|
||||
void TestCompactWindow::aiChatShowsDiffAndAccepts()
|
||||
{
|
||||
QTemporaryDir databaseDir;
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <QtTest>
|
||||
|
||||
#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<ScheduleItem> 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<ScheduleItem> 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"
|
||||
@@ -47,6 +47,7 @@ class TestOpenAiProvider : public QObject
|
||||
|
||||
private slots:
|
||||
void streamsSseContent();
|
||||
void streamsLocalWithoutApiKey();
|
||||
void reportsHttpError();
|
||||
};
|
||||
|
||||
@@ -92,6 +93,45 @@ void TestOpenAiProvider::streamsSseContent()
|
||||
QCOMPARE(text, QStringLiteral("你好,世界"));
|
||||
}
|
||||
|
||||
void TestOpenAiProvider::streamsLocalWithoutApiKey()
|
||||
{
|
||||
FakeSseServer server;
|
||||
QVERIFY(server.listen(QHostAddress::LocalHost, 0));
|
||||
server.response =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/event-stream\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
"data: {\"choices\":[{\"delta\":{\"content\":\"local\"}}]}\n\n"
|
||||
"data: [DONE]\n\n";
|
||||
|
||||
AiConfig config;
|
||||
config.baseUrl = QStringLiteral("http://127.0.0.1:%1/v1").arg(server.serverPort());
|
||||
config.model = QStringLiteral("local-model");
|
||||
|
||||
QJsonArray messages;
|
||||
QJsonObject message;
|
||||
message.insert(QStringLiteral("role"), QStringLiteral("user"));
|
||||
message.insert(QStringLiteral("content"), QStringLiteral("hi"));
|
||||
messages.append(message);
|
||||
|
||||
OpenAiProvider provider;
|
||||
QString text;
|
||||
QString error;
|
||||
bool finished = false;
|
||||
provider.streamCompletion(
|
||||
config,
|
||||
messages,
|
||||
{
|
||||
[&text](const QString& delta) { text += delta; },
|
||||
[&error](const QString& message) { error = message; },
|
||||
[&finished]() { finished = true; },
|
||||
});
|
||||
QTRY_VERIFY_WITH_TIMEOUT(finished, 5000);
|
||||
QCOMPARE(error, QString());
|
||||
QCOMPARE(text, QStringLiteral("local"));
|
||||
}
|
||||
|
||||
void TestOpenAiProvider::reportsHttpError()
|
||||
{
|
||||
FakeSseServer server;
|
||||
|
||||
@@ -57,6 +57,7 @@ private slots:
|
||||
void fallsBackWhenPreferredIsFull();
|
||||
void preferredWindowCanOverrideDefaultHours();
|
||||
void reportsFailureWhenNoRoom();
|
||||
void usesEnergyProfile();
|
||||
};
|
||||
|
||||
void TestScheduler::skipsBusyIntervals()
|
||||
@@ -157,5 +158,33 @@ void TestScheduler::reportsFailureWhenNoRoom()
|
||||
QCOMPARE(result.failures.size(), std::size_t(1));
|
||||
}
|
||||
|
||||
void TestScheduler::usesEnergyProfile()
|
||||
{
|
||||
scheduler::Options options;
|
||||
options.energyAt = [](SysTime value) {
|
||||
const int hour = static_cast<int>((value.time_since_epoch().count() / 3600) % 24);
|
||||
return hour < 14 ? 0 : 2;
|
||||
};
|
||||
|
||||
auto high = task(QStringLiteral("high").toStdString(), 60);
|
||||
high.energyRequired = 2;
|
||||
auto result = scheduler::schedule({high}, {}, workWindows(1), at(0, 8), at(0, 22), options);
|
||||
QCOMPARE(result.placements.size(), std::size_t(1));
|
||||
QVERIFY(result.placements[0].start == at(0, 14));
|
||||
QVERIFY(result.placements[0].end == at(0, 15));
|
||||
|
||||
auto medium = task(QStringLiteral("medium").toStdString(), 60);
|
||||
medium.energyRequired = 1;
|
||||
result = scheduler::schedule({medium}, {}, workWindows(1), at(0, 8), at(0, 22), options);
|
||||
QCOMPARE(result.placements.size(), std::size_t(1));
|
||||
QVERIFY(result.placements[0].start == at(0, 14));
|
||||
|
||||
auto strictHigh = task(QStringLiteral("strict-high").toStdString(), 60);
|
||||
strictHigh.energyRequired = 2;
|
||||
result = scheduler::schedule({strictHigh}, {}, workWindows(1), at(0, 8), at(0, 13), options);
|
||||
QVERIFY(result.placements.empty());
|
||||
QCOMPARE(result.failures.size(), std::size_t(1));
|
||||
}
|
||||
|
||||
QTEST_GUILESS_MAIN(TestScheduler)
|
||||
#include "test_scheduler.moc"
|
||||
|
||||
@@ -25,6 +25,7 @@ private slots:
|
||||
void overdueAndNotificationDedup();
|
||||
void invalidRangeRejected();
|
||||
void applyProposedCreatesOnlySelected();
|
||||
void energyProfileLearnsFromHistory();
|
||||
};
|
||||
|
||||
namespace
|
||||
@@ -313,5 +314,53 @@ void TestServices::applyProposedCreatesOnlySelected()
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user