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

587 lines
20 KiB
C++

#include <QApplication>
#include <QDir>
#include <QListWidget>
#include <QTemporaryDir>
#include <QTimer>
#include <QtTest>
#include <memory>
#include "ElaApplication.h"
#include "ElaComboBox.h"
#include "ElaLineEdit.h"
#include "ElaPushButton.h"
#include "ElaToolButton.h"
#include "ai/AIService.h"
#include "services/NotificationService.h"
#include "services/ScheduleService.h"
#include "storage/SqliteStore.h"
#include "ui/CompactWindow.h"
#include "ui/MainWindow.h"
#include "ui/dialogs/BlockEditDialog.h"
#include "ui/dialogs/DailyReviewDialog.h"
#include "ui/dialogs/ProjectDialog.h"
#include "ui/dialogs/SearchDialog.h"
#include "ui/dialogs/SettingsDialog.h"
#include "ui/panels/DiffPreviewCard.h"
#include "ui/theme/ThemeManager.h"
#include "ui/views/ScheduleGridView.h"
#include "ui/theme/WindowEffects.h"
#include "utils/AppSettings.h"
class MockChatProvider : public ai::IAIProvider
{
public:
QString response;
QString name() const override
{
return QStringLiteral("mock");
}
void cancel() override
{
}
void streamCompletion(const AiConfig&,
const QJsonArray&,
ai::CompletionCallbacks callbacks) override
{
for (int i = 0; i < response.size(); i += 7)
{
if (callbacks.onDelta)
{
callbacks.onDelta(response.mid(i, 7));
}
}
if (callbacks.onFinished)
{
callbacks.onFinished();
}
}
};
class TestCompactWindow : public QObject
{
Q_OBJECT
private slots:
void initTestCase();
void compactOpensScheduleAndReturns();
void opensDailyReview();
void searchDialogFindsSchedules();
void repeatDialogShowsOptions();
void projectDialogShowsFields();
void aiChatShowsDiffAndAccepts();
void aiChatShowsMultiOperationDiff();
private:
void createSampleData(ScheduleService& service, const QDate& today);
static void saveWindow(QWidget* widget, const QString& name);
QTemporaryDir m_tempDir;
std::unique_ptr<AppSettings> m_settings;
bool m_mica = false;
};
void TestCompactWindow::initTestCase()
{
QVERIFY(m_tempDir.isValid());
QCoreApplication::setOrganizationName(QStringLiteral("ScheduleTest"));
QCoreApplication::setApplicationName(QStringLiteral("ScheduleTest"));
QApplication::setStyle(QStringLiteral("Fusion"));
eApp->init();
m_settings = std::make_unique<AppSettings>(m_tempDir.filePath(QStringLiteral("config.toml")));
m_settings->setThemePreference(qEnvironmentVariable("SCHEDULE_TEST_THEME", QStringLiteral("system")));
m_mica = qEnvironmentVariableIsSet("SCHEDULE_TEST_MICA");
m_settings->setMicaEnabled(m_mica);
}
void TestCompactWindow::createSampleData(ScheduleService& service, const QDate& today)
{
const auto create = [&service](const QString& title,
const QDateTime& start,
const QDateTime& end,
const QString& status = QStringLiteral("scheduled")) {
QString error;
if (!service.createBlock(title, start, end, &error))
{
qWarning() << error;
return false;
}
if (status != QStringLiteral("scheduled"))
{
const QVector<ScheduleItem> items = service.itemsForDay(start.date());
for (const ScheduleItem& item : items)
{
if (item.task.title == title)
{
service.setBlockStatus(item.block.id, status, &error);
break;
}
}
}
return true;
};
QVERIFY(create(QStringLiteral("写周报"),
QDateTime(today, QTime(9, 0), Qt::LocalTime),
QDateTime(today, QTime(10, 30), Qt::LocalTime)));
QString projectError;
QVERIFY2(service.createProject(QStringLiteral("飞行控制器项目"),
QStringLiteral("完成固件、测试与文档"),
today.addDays(30),
&projectError),
qPrintable(projectError));
QVERIFY(create(QStringLiteral("团队同步"),
QDateTime(today, QTime(10, 0), Qt::LocalTime),
QDateTime(today, QTime(11, 0), Qt::LocalTime)));
QVERIFY(create(QStringLiteral("设计评审"),
QDateTime(today, QTime(14, 0), Qt::LocalTime),
QDateTime(today, QTime(15, 0), Qt::LocalTime),
QStringLiteral("done")));
QVERIFY(create(QStringLiteral("整理邮件"),
QDateTime(today, QTime(16, 0), Qt::LocalTime),
QDateTime(today, QTime(16, 30), Qt::LocalTime),
QStringLiteral("dropped")));
QVERIFY(create(QStringLiteral("项目周会"),
QDateTime(today.addDays(-2), QTime(10, 0), Qt::LocalTime),
QDateTime(today.addDays(-2), QTime(11, 0), Qt::LocalTime)));
QVERIFY(create(QStringLiteral("深度阅读"),
QDateTime(today.addDays(3), QTime(15, 0), Qt::LocalTime),
QDateTime(today.addDays(3), QTime(16, 30), Qt::LocalTime)));
const QDate nextMonth = QDate(today.year(), today.month(), 1).addMonths(1);
QVERIFY(create(QStringLiteral("月度复盘"),
QDateTime(nextMonth.addDays(4), QTime(9, 0), Qt::LocalTime),
QDateTime(nextMonth.addDays(4), QTime(10, 30), Qt::LocalTime)));
const QDate earlier = QDate(today.year(), today.month(), 1).addMonths(-2);
QVERIFY(create(QStringLiteral("季度目标"),
QDateTime(earlier.addDays(9), QTime(13, 0), Qt::LocalTime),
QDateTime(earlier.addDays(9), QTime(15, 0), Qt::LocalTime)));
}
void TestCompactWindow::saveWindow(QWidget* widget, const QString& name)
{
const QString directory = qEnvironmentVariable("SCHEDULE_TEST_SCREENSHOTS");
if (directory.isEmpty() || !widget)
{
return;
}
QDir().mkpath(directory);
widget->grab().save(directory + QLatin1Char('/') + name);
}
void TestCompactWindow::compactOpensScheduleAndReturns()
{
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("compact-test"));
NotificationService notifications(service);
AIService aiService(service, store, *m_settings);
const QDate today = QDate::currentDate();
createSampleData(service, today);
CompactWindow compact(service, notifications, aiService, theme, *m_settings);
compact.show();
QVERIFY(QTest::qWaitForWindowExposed(&compact));
if (m_mica)
{
WindowEffects::applyMica(true);
QTest::qWait(4500);
}
else
{
QTest::qWait(300);
}
saveWindow(&compact, QStringLiteral("compact.png"));
ElaPushButton* openButton = nullptr;
for (ElaPushButton* button : compact.findChildren<ElaPushButton*>())
{
if (button->text() == QStringLiteral("详细日程"))
{
openButton = button;
break;
}
}
QVERIFY(openButton);
QTest::mouseClick(openButton, Qt::LeftButton);
QTest::qWait(300);
MainWindow* scheduleWindow = nullptr;
for (QWidget* widget : QApplication::topLevelWidgets())
{
if (auto* candidate = qobject_cast<MainWindow*>(widget))
{
scheduleWindow = candidate;
break;
}
}
QVERIFY(scheduleWindow);
QVERIFY(scheduleWindow->isVisible());
QVERIFY(!compact.isVisible());
if (auto* grid = scheduleWindow->findChild<ScheduleGridView*>())
{
grid->scrollToHour(8);
QTest::qWait(80);
}
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);
saveWindow(scheduleWindow, QStringLiteral("schedule-month.png"));
scheduleWindow->setView(MainWindow::View::Year);
QTest::qWait(200);
QCOMPARE(scheduleWindow->view(), MainWindow::View::Year);
saveWindow(scheduleWindow, QStringLiteral("schedule-year.png"));
scheduleWindow->setView(MainWindow::View::Day);
QTest::qWait(120);
scheduleWindow->close();
QTest::qWait(200);
QVERIFY(compact.isVisible());
ElaToolButton* settingsButton = nullptr;
for (ElaToolButton* button : compact.findChildren<ElaToolButton*>())
{
if (button->toolTip().startsWith(QStringLiteral("设置")))
{
settingsButton = button;
break;
}
}
QVERIFY(settingsButton);
QTimer::singleShot(400, []() {
for (QWidget* widget : QApplication::topLevelWidgets())
{
if (auto* dialog = qobject_cast<SettingsDialog*>(widget))
{
saveWindow(dialog, QStringLiteral("settings.png"));
dialog->reject();
}
}
});
QTimer::singleShot(2000, []() {
for (QWidget* widget : QApplication::topLevelWidgets())
{
if (auto* dialog = qobject_cast<SettingsDialog*>(widget))
{
dialog->reject();
}
}
});
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::searchDialogFindsSchedules()
{
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("search-test"));
createSampleData(service, QDate::currentDate());
SearchDialog dialog(service, theme.tokens(), QStringLiteral("周报"));
dialog.show();
QVERIFY(QTest::qWaitForWindowExposed(&dialog));
QTest::qWait(300);
auto* results = dialog.findChild<QListWidget*>();
QVERIFY(results);
QVERIFY(results->count() > 0);
saveWindow(&dialog, QStringLiteral("search.png"));
dialog.close();
}
void TestCompactWindow::repeatDialogShowsOptions()
{
ThemeManager theme(*m_settings);
theme.initialize();
BlockEditDialog dialog(theme.tokens());
dialog.setCreateMode(true);
const QDateTime start(QDate::currentDate(), QTime(9, 0), Qt::LocalTime);
dialog.setValues(QStringLiteral("每日复盘"), start, start.addSecs(60 * 60));
auto* combo = dialog.findChild<ElaComboBox*>();
QVERIFY(combo);
combo->setCurrentIndex(1);
dialog.show();
QVERIFY(QTest::qWaitForWindowExposed(&dialog));
QTest::qWait(300);
saveWindow(&dialog, QStringLiteral("block-edit-repeat.png"));
dialog.accept();
QCOMPARE(dialog.recurrenceFrequency(), RecurrenceFrequency::Daily);
QCOMPARE(dialog.recurrenceUntil(), start.date().addMonths(3));
}
void TestCompactWindow::projectDialogShowsFields()
{
ThemeManager theme(*m_settings);
theme.initialize();
ProjectDialog dialog(theme.tokens());
const QDate deadline = QDate::currentDate().addDays(30);
dialog.setValues(QStringLiteral("飞行控制器项目"),
QStringLiteral("完成固件、测试与文档"),
deadline);
dialog.show();
QVERIFY(QTest::qWaitForWindowExposed(&dialog));
QTest::qWait(300);
saveWindow(&dialog, QStringLiteral("project-dialog.png"));
QCOMPARE(dialog.titleValue(), QStringLiteral("飞行控制器项目"));
QCOMPARE(dialog.deadlineValue(), deadline);
dialog.accept();
}
void TestCompactWindow::aiChatShowsDiffAndAccepts()
{
QTemporaryDir databaseDir;
QVERIFY(databaseDir.isValid());
SqliteStore store(databaseDir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
AiConfig config;
config.baseUrl = QStringLiteral("http://localhost/v1");
config.apiKey = QStringLiteral("test-key");
config.model = QStringLiteral("test-model");
m_settings->setAiConfig(config);
auto provider = std::make_unique<MockChatProvider>();
MockChatProvider* mock = provider.get();
const QDate tomorrow = QDate::currentDate().addDays(1);
mock->response = QStringLiteral(
"好的,我为你安排。\n@@DIFF@@\n"
"{\"tasks\":[{\"title\":\"写周报\",\"duration_min\":120,"
"\"priority\":2,\"preferred_start\":\"%1T14:00:00\","
"\"preferred_end\":\"%1T16:00:00\"}]}")
.arg(tomorrow.toString(QStringLiteral("yyyy-MM-dd")));
ThemeManager theme(*m_settings);
theme.initialize();
ScheduleService service(store, QStringLiteral("chat-ui-test"));
NotificationService notifications(service);
AIService aiService(service, store, *m_settings, std::move(provider));
CompactWindow compact(service, notifications, aiService, theme, *m_settings);
compact.show();
QVERIFY(QTest::qWaitForWindowExposed(&compact));
QTest::qWait(100);
ElaLineEdit* input = compact.findChild<ElaLineEdit*>();
QVERIFY(input);
input->setText(QStringLiteral("明天下午写周报 2 小时"));
ElaPushButton* sendButton = nullptr;
for (ElaPushButton* button : compact.findChildren<ElaPushButton*>())
{
if (button->text() == QStringLiteral("发送"))
{
sendButton = button;
break;
}
}
QVERIFY(sendButton);
QTest::mouseClick(sendButton, Qt::LeftButton);
QTest::qWait(150);
auto* card = compact.findChild<DiffPreviewCard*>();
QVERIFY(card);
saveWindow(&compact, QStringLiteral("chat-diff.png"));
ElaPushButton* acceptButton = nullptr;
for (ElaPushButton* button : card->findChildren<ElaPushButton*>())
{
if (button->text() == QStringLiteral("接受选中"))
{
acceptButton = button;
break;
}
}
QVERIFY(acceptButton);
QTest::mouseClick(acceptButton, Qt::LeftButton);
QTest::qWait(150);
const QVector<ScheduleItem> items = service.itemsForDay(tomorrow);
QCOMPARE(items.size(), 1);
QCOMPARE(items.first().task.title, QStringLiteral("写周报"));
QCOMPARE(items.first().task.source, QStringLiteral("ai_chat"));
saveWindow(&compact, QStringLiteral("chat-applied.png"));
}
void TestCompactWindow::aiChatShowsMultiOperationDiff()
{
QTemporaryDir databaseDir;
QVERIFY(databaseDir.isValid());
SqliteStore store(databaseDir.filePath(QStringLiteral("schedule.db")));
QString error;
QVERIFY2(store.open(&error), qPrintable(error));
AiConfig config;
config.baseUrl = QStringLiteral("http://localhost/v1");
config.apiKey = QStringLiteral("test-key");
config.model = QStringLiteral("test-model");
m_settings->setAiConfig(config);
ThemeManager theme(*m_settings);
theme.initialize();
ScheduleService service(store, QStringLiteral("chat-multi-test"));
NotificationService notifications(service);
const QDate tomorrow = QDate::currentDate().addDays(1);
QVERIFY(service.createBlock(QStringLiteral("已有日程"),
QDateTime(tomorrow, QTime(14, 0), Qt::LocalTime),
QDateTime(tomorrow, QTime(15, 0), Qt::LocalTime),
&error));
QVERIFY(service.createBlock(QStringLiteral("待删除"),
QDateTime(tomorrow, QTime(16, 0), Qt::LocalTime),
QDateTime(tomorrow, QTime(17, 0), Qt::LocalTime),
&error));
auto provider = std::make_unique<MockChatProvider>();
MockChatProvider* mock = provider.get();
mock->response = QStringLiteral(
"好的,我插入并清理一下。\n@@DIFF@@\n"
"{\"operations\":["
"{\"type\":\"create\",\"title\":\"插入任务\","
"\"duration_min\":60,\"preferred_start\":\"%1T14:00:00\","
"\"preferred_end\":\"%1T15:00:00\"},"
"{\"type\":\"delete\",\"target_ref\":\"b2\"}]}")
.arg(tomorrow.toString(QStringLiteral("yyyy-MM-dd")));
AIService aiService(service, store, *m_settings, std::move(provider));
CompactWindow compact(service, notifications, aiService, theme, *m_settings);
compact.show();
QVERIFY(QTest::qWaitForWindowExposed(&compact));
QTest::qWait(100);
ElaLineEdit* input = compact.findChild<ElaLineEdit*>();
QVERIFY(input);
input->setText(QStringLiteral("明天下午两点插入一小时任务,并删掉四点的会议"));
ElaPushButton* sendButton = nullptr;
for (ElaPushButton* button : compact.findChildren<ElaPushButton*>())
{
if (button->text() == QStringLiteral("发送"))
{
sendButton = button;
break;
}
}
QVERIFY(sendButton);
QTest::mouseClick(sendButton, Qt::LeftButton);
QTest::qWait(180);
auto* card = compact.findChild<DiffPreviewCard*>();
QVERIFY(card);
saveWindow(&compact, QStringLiteral("chat-multi-diff.png"));
ElaPushButton* acceptButton = nullptr;
for (ElaPushButton* button : card->findChildren<ElaPushButton*>())
{
if (button->text() == QStringLiteral("接受选中"))
{
acceptButton = button;
break;
}
}
QVERIFY(acceptButton);
QTest::mouseClick(acceptButton, Qt::LeftButton);
QTest::qWait(150);
saveWindow(&compact, QStringLiteral("chat-multi-applied.png"));
const QVector<ScheduleItem> items = service.itemsForDay(tomorrow);
QCOMPARE(items.size(), 2);
bool sawInsert = false;
bool sawMoved = false;
for (const ScheduleItem& item : items)
{
QVERIFY(item.task.title != QStringLiteral("待删除"));
if (item.task.title == QStringLiteral("插入任务"))
{
sawInsert = true;
QCOMPARE(item.block.startAt, QDateTime(tomorrow, QTime(14, 0), Qt::LocalTime));
}
if (item.task.title == QStringLiteral("已有日程"))
{
sawMoved = true;
QCOMPARE(item.block.startAt, QDateTime(tomorrow, QTime(15, 0), Qt::LocalTime));
}
}
QVERIFY(sawInsert);
QVERIFY(sawMoved);
}
QTEST_MAIN(TestCompactWindow)
#include "test_compactwindow.moc"