|
| 1 | +#include <QtTest> |
| 2 | + |
| 3 | +#include <QComboBox> |
| 4 | +#include <QCoreApplication> |
| 5 | +#include <QJsonDocument> |
| 6 | +#include <QPlainTextEdit> |
| 7 | +#include <QPushButton> |
| 8 | +#include <QSettings> |
| 9 | +#include <QTemporaryDir> |
| 10 | + |
| 11 | +#include "AcpAgentRegistry.h" |
| 12 | +#include "ApplicationSettings.h" |
| 13 | +#include "GoalAgentSettings.h" |
| 14 | +#include "GoalConfigWidget.h" |
| 15 | +#include "SendWithGoalDialog.h" |
| 16 | + |
| 17 | +class TestSendWithGoalDialog : public QObject |
| 18 | +{ |
| 19 | + Q_OBJECT |
| 20 | + |
| 21 | +private slots: |
| 22 | + void initTestCase(); |
| 23 | + void init(); |
| 24 | + |
| 25 | + void changedAgent_isRestoredAfterCancel(); |
| 26 | + void changedAgent_isRestoredAfterEscape(); |
| 27 | + void changedAgent_isRestoredAfterAccept(); |
| 28 | + void removedStoredAgent_fallsBackAndNormalizesSetting(); |
| 29 | + |
| 30 | +private: |
| 31 | + static QComboBox *agentCombo(SendWithGoalDialog &dialog); |
| 32 | + static QPushButton *dialogButton(SendWithGoalDialog &dialog, const QString &text); |
| 33 | + static QString storedAgentId(ApplicationSettings &settings); |
| 34 | + static void selectCodex(SendWithGoalDialog &dialog); |
| 35 | + |
| 36 | + QTemporaryDir m_settingsDir; |
| 37 | +}; |
| 38 | + |
| 39 | +void TestSendWithGoalDialog::initTestCase() |
| 40 | +{ |
| 41 | + QVERIFY(m_settingsDir.isValid()); |
| 42 | + QCoreApplication::setOrganizationName(QStringLiteral("NotepadNextTest")); |
| 43 | + QCoreApplication::setApplicationName(QStringLiteral("NotepadNextTest_SendWithGoalDialog")); |
| 44 | + QSettings::setDefaultFormat(QSettings::IniFormat); |
| 45 | + QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, m_settingsDir.path()); |
| 46 | +} |
| 47 | + |
| 48 | +void TestSendWithGoalDialog::init() |
| 49 | +{ |
| 50 | + ApplicationSettings settings; |
| 51 | + settings.clear(); |
| 52 | + settings.sync(); |
| 53 | +} |
| 54 | + |
| 55 | +QComboBox *TestSendWithGoalDialog::agentCombo(SendWithGoalDialog &dialog) |
| 56 | +{ |
| 57 | + const auto combos = dialog.findChildren<QComboBox *>(); |
| 58 | + for (QComboBox *combo : combos) { |
| 59 | + if (combo->findData(AcpAgentRegistry::builtinClaudeCodeId()) >= 0 |
| 60 | + && combo->findData(AcpAgentRegistry::builtinCodexId()) >= 0) { |
| 61 | + return combo; |
| 62 | + } |
| 63 | + } |
| 64 | + return nullptr; |
| 65 | +} |
| 66 | + |
| 67 | +QPushButton *TestSendWithGoalDialog::dialogButton(SendWithGoalDialog &dialog, const QString &text) |
| 68 | +{ |
| 69 | + for (QPushButton *button : dialog.findChildren<QPushButton *>()) { |
| 70 | + if (button->text() == text) |
| 71 | + return button; |
| 72 | + } |
| 73 | + return nullptr; |
| 74 | +} |
| 75 | + |
| 76 | +QString TestSendWithGoalDialog::storedAgentId(ApplicationSettings &settings) |
| 77 | +{ |
| 78 | + const QString json = settings.get("Ai/GoalAgentSettings", QString()); |
| 79 | + return GoalAgentSettings::fromJson(QJsonDocument::fromJson(json.toUtf8()).object()).agentId; |
| 80 | +} |
| 81 | + |
| 82 | +void TestSendWithGoalDialog::selectCodex(SendWithGoalDialog &dialog) |
| 83 | +{ |
| 84 | + QComboBox *combo = agentCombo(dialog); |
| 85 | + QVERIFY(combo); |
| 86 | + const int codexIndex = combo->findData(AcpAgentRegistry::builtinCodexId()); |
| 87 | + QVERIFY(codexIndex >= 0); |
| 88 | + combo->setCurrentIndex(codexIndex); |
| 89 | +} |
| 90 | + |
| 91 | +void TestSendWithGoalDialog::changedAgent_isRestoredAfterCancel() |
| 92 | +{ |
| 93 | + ApplicationSettings settings; |
| 94 | + AcpAgentRegistry registry(&settings); |
| 95 | + |
| 96 | + SendWithGoalDialog first(®istry, &settings); |
| 97 | + selectCodex(first); |
| 98 | + QCOMPARE(storedAgentId(settings), AcpAgentRegistry::builtinCodexId()); |
| 99 | + QVERIFY(dialogButton(first, QStringLiteral("Cancel"))); |
| 100 | + QTest::mouseClick(dialogButton(first, QStringLiteral("Cancel")), Qt::LeftButton); |
| 101 | + QCOMPARE(first.result(), QDialog::Rejected); |
| 102 | + |
| 103 | + SendWithGoalDialog reopened(®istry, &settings); |
| 104 | + QCOMPARE(agentCombo(reopened)->currentData().toString(), AcpAgentRegistry::builtinCodexId()); |
| 105 | +} |
| 106 | + |
| 107 | +void TestSendWithGoalDialog::changedAgent_isRestoredAfterEscape() |
| 108 | +{ |
| 109 | + ApplicationSettings settings; |
| 110 | + AcpAgentRegistry registry(&settings); |
| 111 | + |
| 112 | + SendWithGoalDialog first(®istry, &settings); |
| 113 | + selectCodex(first); |
| 114 | + QCOMPARE(storedAgentId(settings), AcpAgentRegistry::builtinCodexId()); |
| 115 | + QTest::keyClick(&first, Qt::Key_Escape); |
| 116 | + QCOMPARE(first.result(), QDialog::Rejected); |
| 117 | + |
| 118 | + SendWithGoalDialog reopened(®istry, &settings); |
| 119 | + QCOMPARE(agentCombo(reopened)->currentData().toString(), AcpAgentRegistry::builtinCodexId()); |
| 120 | +} |
| 121 | + |
| 122 | +void TestSendWithGoalDialog::changedAgent_isRestoredAfterAccept() |
| 123 | +{ |
| 124 | + ApplicationSettings settings; |
| 125 | + AcpAgentRegistry registry(&settings); |
| 126 | + |
| 127 | + SendWithGoalDialog first(®istry, &settings); |
| 128 | + selectCodex(first); |
| 129 | + QCOMPARE(storedAgentId(settings), AcpAgentRegistry::builtinCodexId()); |
| 130 | + QVERIFY(dialogButton(first, QStringLiteral("Start Goal"))); |
| 131 | + const auto criteria = first.findChildren<QPlainTextEdit *>(); |
| 132 | + QVERIFY(!criteria.isEmpty()); |
| 133 | + criteria.first()->setPlainText(QStringLiteral("Confirm selected agent")); |
| 134 | + QTest::mouseClick(dialogButton(first, QStringLiteral("Start Goal")), Qt::LeftButton); |
| 135 | + QCOMPARE(first.result(), QDialog::Accepted); |
| 136 | + |
| 137 | + SendWithGoalDialog reopened(®istry, &settings); |
| 138 | + QCOMPARE(agentCombo(reopened)->currentData().toString(), AcpAgentRegistry::builtinCodexId()); |
| 139 | +} |
| 140 | + |
| 141 | +void TestSendWithGoalDialog::removedStoredAgent_fallsBackAndNormalizesSetting() |
| 142 | +{ |
| 143 | + ApplicationSettings settings; |
| 144 | + GoalAgentSettings goalSettings; |
| 145 | + goalSettings.agentId = QStringLiteral("removed-agent"); |
| 146 | + settings.setValue( |
| 147 | + QStringLiteral("Ai/GoalAgentSettings"), |
| 148 | + QString::fromUtf8(QJsonDocument(goalSettings.toJson()).toJson(QJsonDocument::Compact))); |
| 149 | + AcpAgentRegistry registry(&settings); |
| 150 | + |
| 151 | + SendWithGoalDialog dialog(®istry, &settings); |
| 152 | + QCOMPARE(agentCombo(dialog)->currentData().toString(), AcpAgentRegistry::builtinClaudeCodeId()); |
| 153 | + QCOMPARE(storedAgentId(settings), AcpAgentRegistry::builtinClaudeCodeId()); |
| 154 | +} |
| 155 | + |
| 156 | +QTEST_MAIN(TestSendWithGoalDialog) |
| 157 | + |
| 158 | +#include "test_send_with_goal_dialog.moc" |
0 commit comments