#include #include #include #include #include #include #include "ElaApplication.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/DailyReviewDialog.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 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 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(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 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))); 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()) { 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(widget)) { scheduleWindow = candidate; break; } } QVERIFY(scheduleWindow); QVERIFY(scheduleWindow->isVisible()); QVERIFY(!compact.isVisible()); if (auto* grid = scheduleWindow->findChild()) { grid->scrollToHour(8); QTest::qWait(80); } saveWindow(scheduleWindow, QStringLiteral("schedule-day.png")); if (auto* grid = scheduleWindow->findChild()) { 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()) { if (button->toolTip().startsWith(QStringLiteral("设置"))) { settingsButton = button; break; } } QVERIFY(settingsButton); QTimer::singleShot(400, []() { for (QWidget* widget : QApplication::topLevelWidgets()) { if (auto* dialog = qobject_cast(widget)) { saveWindow(dialog, QStringLiteral("settings.png")); dialog->reject(); } } }); QTimer::singleShot(2000, []() { for (QWidget* widget : QApplication::topLevelWidgets()) { if (auto* dialog = qobject_cast(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()) { if (button->toolTip().startsWith(QStringLiteral("今日复盘"))) { reviewButton = button; break; } } QVERIFY(reviewButton); QTimer::singleShot(400, []() { for (QWidget* widget : QApplication::topLevelWidgets()) { if (auto* dialog = qobject_cast(widget)) { saveWindow(dialog, QStringLiteral("review.png")); dialog->reject(); } } }); QTimer::singleShot(2000, []() { for (QWidget* widget : QApplication::topLevelWidgets()) { if (auto* dialog = qobject_cast(widget)) { dialog->reject(); } } }); QTest::mouseClick(reviewButton, Qt::LeftButton); } 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* 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(); QVERIFY(input); input->setText(QStringLiteral("明天下午写周报 2 小时")); ElaPushButton* sendButton = nullptr; for (ElaPushButton* button : compact.findChildren()) { if (button->text() == QStringLiteral("发送")) { sendButton = button; break; } } QVERIFY(sendButton); QTest::mouseClick(sendButton, Qt::LeftButton); QTest::qWait(150); auto* card = compact.findChild(); QVERIFY(card); saveWindow(&compact, QStringLiteral("chat-diff.png")); ElaPushButton* acceptButton = nullptr; for (ElaPushButton* button : card->findChildren()) { if (button->text() == QStringLiteral("接受选中")) { acceptButton = button; break; } } QVERIFY(acceptButton); QTest::mouseClick(acceptButton, Qt::LeftButton); QTest::qWait(150); const QVector 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* 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(); QVERIFY(input); input->setText(QStringLiteral("明天下午两点插入一小时任务,并删掉四点的会议")); ElaPushButton* sendButton = nullptr; for (ElaPushButton* button : compact.findChildren()) { if (button->text() == QStringLiteral("发送")) { sendButton = button; break; } } QVERIFY(sendButton); QTest::mouseClick(sendButton, Qt::LeftButton); QTest::qWait(180); auto* card = compact.findChild(); QVERIFY(card); saveWindow(&compact, QStringLiteral("chat-multi-diff.png")); ElaPushButton* acceptButton = nullptr; for (ElaPushButton* button : card->findChildren()) { 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 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"