9bafa71667
- 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
191 lines
7.1 KiB
C++
191 lines
7.1 KiB
C++
#include <QtTest>
|
|
|
|
#include <chrono>
|
|
|
|
#include "core/SchedulerEngine.h"
|
|
|
|
using namespace std::chrono;
|
|
using namespace std::chrono_literals;
|
|
using scheduler::SysTime;
|
|
|
|
namespace
|
|
{
|
|
SysTime at(int day, int hour, int minute = 0)
|
|
{
|
|
return SysTime{sys_days{year{2026} / January / 5} + days{day} + hours{hour} + minutes{minute}};
|
|
}
|
|
|
|
std::vector<scheduler::TimeRange> workWindows(int days = 2)
|
|
{
|
|
std::vector<scheduler::TimeRange> windows;
|
|
for (int day = 0; day < days; ++day)
|
|
{
|
|
windows.push_back({at(day, 8), at(day, 22)});
|
|
}
|
|
return windows;
|
|
}
|
|
|
|
scheduler::TaskInput task(const std::string& id,
|
|
int minutes,
|
|
int priority = 1,
|
|
std::optional<SysTime> deadline = std::nullopt,
|
|
std::optional<SysTime> preferredStart = std::nullopt,
|
|
std::optional<SysTime> preferredEnd = std::nullopt)
|
|
{
|
|
scheduler::TaskInput input;
|
|
input.id = id;
|
|
input.title = id;
|
|
input.durationMinutes = minutes;
|
|
input.priority = priority;
|
|
input.deadline = deadline;
|
|
input.preferredStart = preferredStart;
|
|
input.preferredEnd = preferredEnd;
|
|
return input;
|
|
}
|
|
} // namespace
|
|
|
|
class TestScheduler : public QObject
|
|
{
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
void skipsBusyIntervals();
|
|
void honorsPreferredWindow();
|
|
void alignsToGranularity();
|
|
void respectsDeadline();
|
|
void prioritizesHigherPriorityFirst();
|
|
void fallsBackWhenPreferredIsFull();
|
|
void preferredWindowCanOverrideDefaultHours();
|
|
void reportsFailureWhenNoRoom();
|
|
void usesEnergyProfile();
|
|
};
|
|
|
|
void TestScheduler::skipsBusyIntervals()
|
|
{
|
|
const auto result = scheduler::schedule({task("a", 60)},
|
|
{{at(0, 8), at(0, 10)}},
|
|
workWindows(),
|
|
at(0, 8),
|
|
at(1, 22));
|
|
QCOMPARE(result.placements.size(), std::size_t(1));
|
|
QVERIFY(result.placements[0].start == at(0, 10));
|
|
QVERIFY(result.placements[0].end == at(0, 11));
|
|
QVERIFY(result.failures.empty());
|
|
}
|
|
|
|
void TestScheduler::honorsPreferredWindow()
|
|
{
|
|
const auto result = scheduler::schedule({task("a", 60, 1, std::nullopt, at(0, 14), at(0, 17))},
|
|
{},
|
|
workWindows(),
|
|
at(0, 8),
|
|
at(1, 22));
|
|
QCOMPARE(result.placements.size(), std::size_t(1));
|
|
QVERIFY(result.placements[0].start == at(0, 14));
|
|
QVERIFY(result.placements[0].end == at(0, 15));
|
|
}
|
|
|
|
void TestScheduler::alignsToGranularity()
|
|
{
|
|
const auto result = scheduler::schedule({task("a", 30)},
|
|
{},
|
|
workWindows(),
|
|
at(0, 8, 7),
|
|
at(0, 22));
|
|
QCOMPARE(result.placements.size(), std::size_t(1));
|
|
QVERIFY(result.placements[0].start == at(0, 8, 15));
|
|
QVERIFY(result.placements[0].end == at(0, 8, 45));
|
|
}
|
|
|
|
void TestScheduler::respectsDeadline()
|
|
{
|
|
const auto result = scheduler::schedule({task("a", 60, 1, at(0, 9))},
|
|
{{at(0, 8), at(0, 10)}},
|
|
workWindows(),
|
|
at(0, 8),
|
|
at(1, 22));
|
|
QVERIFY(result.placements.empty());
|
|
QCOMPARE(result.failures.size(), std::size_t(1));
|
|
QCOMPARE(QString::fromStdString(result.failures[0].taskId), QStringLiteral("a"));
|
|
}
|
|
|
|
void TestScheduler::prioritizesHigherPriorityFirst()
|
|
{
|
|
const auto result = scheduler::schedule({task("low", 60, 1), task("high", 60, 3)},
|
|
{},
|
|
workWindows(1),
|
|
at(0, 8),
|
|
at(0, 22));
|
|
QCOMPARE(result.placements.size(), std::size_t(2));
|
|
QCOMPARE(QString::fromStdString(result.placements[0].taskId), QStringLiteral("high"));
|
|
QVERIFY(result.placements[0].start == at(0, 8));
|
|
QCOMPARE(QString::fromStdString(result.placements[1].taskId), QStringLiteral("low"));
|
|
QVERIFY(result.placements[1].start == at(0, 9));
|
|
}
|
|
|
|
void TestScheduler::fallsBackWhenPreferredIsFull()
|
|
{
|
|
const auto result = scheduler::schedule({task("a", 60, 1, std::nullopt, at(0, 14), at(0, 16))},
|
|
{{at(0, 14), at(0, 16)}},
|
|
workWindows(),
|
|
at(0, 8),
|
|
at(1, 22));
|
|
QCOMPARE(result.placements.size(), std::size_t(1));
|
|
QVERIFY(result.placements[0].start == at(0, 8));
|
|
QVERIFY(result.placements[0].end == at(0, 9));
|
|
}
|
|
|
|
void TestScheduler::preferredWindowCanOverrideDefaultHours()
|
|
{
|
|
const auto result = scheduler::schedule({task("late", 60, 1, std::nullopt, at(0, 23), at(1, 1))},
|
|
{},
|
|
workWindows(),
|
|
at(0, 8),
|
|
at(1, 22));
|
|
QCOMPARE(result.placements.size(), std::size_t(1));
|
|
QVERIFY(result.placements[0].start == at(0, 23));
|
|
QVERIFY(result.placements[0].end == at(1, 0));
|
|
}
|
|
|
|
void TestScheduler::reportsFailureWhenNoRoom()
|
|
{
|
|
const auto result = scheduler::schedule({task("a", 120)},
|
|
{{at(0, 8), at(0, 22)}},
|
|
workWindows(1),
|
|
at(0, 8),
|
|
at(0, 22));
|
|
QVERIFY(result.placements.empty());
|
|
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"
|