Repository navigation
Expand file tree
/
Copy pathtestassistantcontroller.cpp
More file actions
422 lines (342 loc) · 18 KB
/
Copy pathtestassistantcontroller.cpp
File metadata and controls
422 lines (342 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "account.h"
#include "assistant/assistantcontroller.h"
#include "assistant/fakeassistantclient.h"
#include "testhelper.h"
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonValue>
#include <QtTest>
using namespace OCC;
namespace
{
QJsonDocument ocsResponse(const QJsonValue &data)
{
return QJsonDocument{QJsonObject{
{QStringLiteral("ocs"), QJsonObject{{QStringLiteral("data"), data}}},
}};
}
QJsonObject message(qint64 id, qint64 conversationId, const QString &role, const QString &content)
{
return {
{QStringLiteral("id"), id},
{QStringLiteral("sessionId"), conversationId},
{QStringLiteral("role"), role},
{QStringLiteral("content"), content},
{QStringLiteral("timestamp"), 1},
};
}
QJsonDocument humanMessageResponse(qint64 conversationId, bool enveloped = false)
{
const auto messages = QJsonArray{message(1, conversationId, QStringLiteral("human"), QStringLiteral("Question"))};
return enveloped ? ocsResponse(messages) : QJsonDocument{messages};
}
QJsonDocument chatTaskTypesResponse()
{
const auto chatType = QJsonObject{
{QStringLiteral("name"), QStringLiteral("Chat")},
{QStringLiteral("description"), QStringLiteral("A conversation")},
};
return ocsResponse(QJsonObject{
{QStringLiteral("types"), QJsonObject{{QStringLiteral("core:text2text:chat"), chatType}}},
});
}
QJsonDocument conversationsResponse(std::initializer_list<QJsonObject> conversations)
{
auto array = QJsonArray{};
for (const auto &conversation : conversations) {
array.append(conversation);
}
return ocsResponse(array);
}
QJsonObject conversation(qint64 id, const QString &title)
{
return {
{QStringLiteral("id"), id},
{QStringLiteral("title"), title},
{QStringLiteral("timestamp"), 1},
};
}
QJsonDocument completedTaskResponse(qint64 taskId, const QString &taskType, const QString &input)
{
const auto task = QJsonObject{
{QStringLiteral("id"), taskId},
{QStringLiteral("type"), taskType},
{QStringLiteral("appId"), QStringLiteral("assistant")},
{QStringLiteral("input"), QJsonObject{{QStringLiteral("input"), input}}},
{QStringLiteral("output"), QJsonObject{{QStringLiteral("output"), QStringLiteral("Done")}}},
{QStringLiteral("status"), QStringLiteral("STATUS_SUCCESSFUL")},
};
return ocsResponse(QJsonObject{{QStringLiteral("tasks"), QJsonArray{task}}});
}
struct ControllerFixture {
ControllerFixture()
: account(Account::create())
, accountState(new FakeAccountState(account))
, client(new FakeAssistantClient)
, controller(accountState, client, nullptr)
{
account->setCapabilities({
{QStringLiteral("assistant"),
QVariantMap{
{QStringLiteral("enabled"), true},
{QStringLiteral("version"), QStringLiteral("1.0.9")},
}},
});
}
AccountPtr account;
AccountStatePtr accountState;
FakeAssistantClient *client;
AssistantController controller;
};
}
class TestAssistantController : public QObject
{
Q_OBJECT
private Q_SLOTS:
void loadDataLoadsChatTypeAndWrappedConversations()
{
ControllerFixture fixture;
fixture.controller.loadData();
QCOMPARE(fixture.client->fetchTaskTypesCount, 1);
QVERIFY(fixture.controller.requestInProgress());
const auto requestGeneration = fixture.client->lastTaskTypesGeneration;
fixture.client->deliverTaskTypes(requestGeneration, chatTaskTypesResponse());
QCOMPARE(fixture.controller.taskTypes()->rowCount(), 1);
QCOMPARE(fixture.controller.selectedTaskTypeId(), QStringLiteral("core:text2text:chat"));
QCOMPARE(fixture.controller.selectedTaskTypeName(), QStringLiteral("Chat"));
QCOMPARE(fixture.client->fetchChatConversationsCount, 1);
const auto conversations = QJsonArray{
QJsonObject{
{QStringLiteral("id"), 42},
{QStringLiteral("title"), QStringLiteral("Conversation")},
{QStringLiteral("timestamp"), 1},
},
};
fixture.client->deliverChatConversations(requestGeneration, ocsResponse(conversations));
QCOMPARE(fixture.controller.chatConversations()->rowCount(), 1);
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(fixture.controller.error().isEmpty());
}
void loadDataRejectsMalformedTaskTypes()
{
ControllerFixture fixture;
fixture.controller.loadData();
fixture.client->deliverTaskTypes(fixture.client->lastTaskTypesGeneration, ocsResponse(QJsonObject{}));
QCOMPARE(fixture.controller.taskTypes()->rowCount(), 0);
QVERIFY(fixture.controller.selectedTaskTypeId().isEmpty());
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(!fixture.controller.error().isEmpty());
QCOMPARE(fixture.client->fetchChatConversationsCount, 0);
fixture.controller.submitQuestion(QStringLiteral("Question"));
QCOMPARE(fixture.client->fetchTaskTypesCount, 2);
fixture.client->deliverTaskTypes(fixture.client->lastTaskTypesGeneration, ocsResponse(QJsonObject{}));
QVERIFY(fixture.controller.selectedTaskTypeId().isEmpty());
QCOMPARE(fixture.client->createChatConversationCount, 0);
QCOMPARE(fixture.client->scheduleTaskCount, 0);
QVERIFY(!fixture.controller.requestInProgress());
}
void wrappedMessagesUpdateConversationState()
{
ControllerFixture fixture;
fixture.controller.loadData();
const auto loadGeneration = fixture.client->lastTaskTypesGeneration;
fixture.client->deliverTaskTypes(loadGeneration, chatTaskTypesResponse());
fixture.client->deliverChatConversations(loadGeneration, conversationsResponse({conversation(42, QStringLiteral("Original title"))}));
fixture.controller.selectChatConversation(42);
const auto requestGeneration = fixture.client->lastChatMessagesGeneration;
fixture.client->deliverChatMessages(requestGeneration, humanMessageResponse(42, true));
QCOMPARE(fixture.controller.messages()->rowCount(), 1);
QCOMPARE(fixture.client->chatSessionCheckCount, 1);
fixture.client->deliverChatSessionCheck(requestGeneration,
ocsResponse(QJsonObject{{QStringLiteral("sessionTitle"), QStringLiteral("Renamed conversation")}}));
QCOMPARE(fixture.controller.selectedChatConversationTitle(), QStringLiteral("Renamed conversation"));
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(fixture.controller.showRetryResponseGeneration());
fixture.controller.startNewChat();
fixture.controller.selectChatConversation(42);
QCOMPARE(fixture.controller.selectedChatConversationTitle(), QStringLiteral("Renamed conversation"));
}
void reloadClearsConversationMissingFromServer()
{
ControllerFixture fixture;
fixture.controller.loadData();
const auto initialGeneration = fixture.client->lastTaskTypesGeneration;
fixture.client->deliverTaskTypes(initialGeneration, chatTaskTypesResponse());
fixture.client->deliverChatConversations(initialGeneration, conversationsResponse({conversation(42, QStringLiteral("Conversation"))}));
fixture.controller.selectChatConversation(42);
fixture.client->deliverChatMessages(fixture.client->lastChatMessagesGeneration, humanMessageResponse(42, true));
fixture.client->deliverChatSessionCheck(fixture.client->lastChatSessionCheckGeneration, ocsResponse(QJsonObject{}));
QCOMPARE(fixture.controller.messages()->rowCount(), 1);
QCOMPARE(fixture.controller.selectedChatConversationId(), 42);
fixture.controller.loadData();
const auto reloadGeneration = fixture.client->lastTaskTypesGeneration;
fixture.client->deliverTaskTypes(reloadGeneration, chatTaskTypesResponse());
fixture.client->deliverChatConversations(reloadGeneration, conversationsResponse({}));
QCOMPARE(fixture.controller.selectedChatConversationId(), -1);
QVERIFY(fixture.controller.selectedChatConversationTitle().isEmpty());
QCOMPARE(fixture.controller.messages()->rowCount(), 0);
QVERIFY(!fixture.controller.showRetryResponseGeneration());
}
void selectingConversationClearsPreviousError()
{
ControllerFixture fixture;
fixture.controller.selectChatConversation(41);
fixture.client->deliverChatMessages(fixture.client->lastChatMessagesGeneration, {}, 500);
QVERIFY(!fixture.controller.error().isEmpty());
fixture.controller.selectChatConversation(42);
QVERIFY(fixture.controller.error().isEmpty());
QCOMPARE(fixture.client->lastChatMessagesConversationId, 42);
}
void wrappedResponsesCompleteNewChatWorkflow()
{
ControllerFixture fixture;
fixture.controller.submitQuestion(QStringLiteral(" Question "));
QCOMPARE(fixture.client->createChatConversationCount, 1);
QCOMPARE(fixture.client->lastChatConversationTitle, QStringLiteral("Question"));
const auto requestGeneration = fixture.client->lastChatConversationCreateGeneration;
const auto session = QJsonObject{
{QStringLiteral("id"), 42},
{QStringLiteral("title"), QStringLiteral("Question")},
{QStringLiteral("timestamp"), 1},
};
fixture.client->deliverChatConversationCreated(requestGeneration, ocsResponse(QJsonObject{{QStringLiteral("session"), session}}));
QCOMPARE(fixture.controller.selectedChatConversationId(), 42);
QCOMPARE(fixture.client->createChatMessageCount, 1);
QCOMPARE(fixture.client->lastChatMessageSessionId, 42);
QCOMPARE(fixture.client->lastChatMessageRole, QStringLiteral("human"));
QCOMPARE(fixture.client->lastChatMessageContent, QStringLiteral("Question"));
QVERIFY(fixture.client->lastChatMessageWasFirst);
fixture.client->deliverChatMessageCreated(requestGeneration, ocsResponse(message(1, 42, QStringLiteral("human"), QStringLiteral("Question"))));
QCOMPARE(fixture.controller.messages()->rowCount(), 1);
QCOMPARE(fixture.client->generateChatSessionCount, 1);
QCOMPARE(fixture.client->lastGeneratedConversationId, 42);
fixture.client->deliverChatSessionGenerationStarted(requestGeneration, ocsResponse(QJsonObject{{QStringLiteral("taskId"), 99}}));
QVERIFY(fixture.controller.requestInProgress());
QVERIFY(fixture.controller.thinking());
fixture.client->deliverChatGenerationCheck(requestGeneration, ocsResponse(message(2, 42, QStringLiteral("assistant"), QStringLiteral("Answer"))), 200);
QCOMPARE(fixture.controller.messages()->rowCount(), 2);
const auto answerIndex = fixture.controller.messages()->index(1, 0);
QCOMPARE(fixture.controller.messages()->data(answerIndex, AssistantMessageModel::TextRole).toString(), QStringLiteral("Answer"));
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(!fixture.controller.thinking());
QVERIFY(fixture.controller.error().isEmpty());
}
void nonChatTaskWorkflowSchedulesAndLoadsResult()
{
ControllerFixture fixture;
const auto taskType = QStringLiteral("core:text2text:text_generation");
fixture.controller.selectTaskType(taskType);
QCOMPARE(fixture.client->fetchTasksCount, 1);
fixture.client->deliverTasks(fixture.client->lastTasksGeneration, ocsResponse(QJsonObject{{QStringLiteral("tasks"), QJsonArray{}}}));
fixture.controller.submitQuestion(QStringLiteral(" Summarize this "));
QCOMPARE(fixture.client->scheduleTaskCount, 1);
QCOMPARE(fixture.client->lastTaskInput, QStringLiteral("Summarize this"));
QCOMPARE(fixture.client->lastTaskType, taskType);
const auto requestGeneration = fixture.client->lastTaskScheduleGeneration;
const auto scheduledTask = QJsonObject{{QStringLiteral("task"), QJsonObject{{QStringLiteral("id"), 7}}}};
fixture.client->deliverTaskScheduled(requestGeneration, ocsResponse(scheduledTask));
QCOMPARE(fixture.client->fetchTasksCount, 2);
fixture.client->deliverTasks(requestGeneration, completedTaskResponse(7, taskType, QStringLiteral("Summarize this")));
QCOMPARE(fixture.controller.tasks()->rowCount(), 1);
const auto taskIndex = fixture.controller.tasks()->index(0, 0);
QCOMPARE(fixture.controller.tasks()->data(taskIndex, AssistantTaskModel::OutputRole).toString(), QStringLiteral("Done"));
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(fixture.controller.error().isEmpty());
}
void clearRejectsLateMessageReply()
{
ControllerFixture fixture;
fixture.controller.selectChatConversation(41);
const auto clearedGeneration = fixture.client->lastChatMessagesGeneration;
fixture.controller.clear();
fixture.client->deliverChatMessages(clearedGeneration, humanMessageResponse(41));
QCOMPARE(fixture.controller.messages()->rowCount(), 0);
QCOMPARE(fixture.client->chatSessionCheckCount, 0);
QCOMPARE(fixture.controller.selectedChatConversationId(), -1);
}
void newerConversationRejectsSupersededReply()
{
ControllerFixture fixture;
fixture.controller.selectChatConversation(41);
const auto supersededGeneration = fixture.client->lastChatMessagesGeneration;
fixture.controller.selectChatConversation(42);
const auto currentGeneration = fixture.client->lastChatMessagesGeneration;
fixture.client->deliverChatMessages(supersededGeneration, humanMessageResponse(41));
QCOMPARE(fixture.controller.messages()->rowCount(), 0);
QCOMPARE(fixture.client->chatSessionCheckCount, 0);
fixture.client->deliverChatMessages(currentGeneration, humanMessageResponse(42));
QCOMPARE(fixture.controller.messages()->rowCount(), 1);
QCOMPARE(fixture.client->chatSessionCheckCount, 1);
QCOMPARE(fixture.client->lastChatSessionCheckConversationId, 42);
QCOMPARE(fixture.client->lastChatSessionCheckGeneration, currentGeneration);
}
void chatGenerationPollingStopsAtLimit()
{
ControllerFixture fixture;
fixture.controller.selectChatConversation(42);
const auto requestGeneration = fixture.client->lastChatMessagesGeneration;
fixture.client->deliverChatMessages(requestGeneration, humanMessageResponse(42));
fixture.client->deliverChatSessionCheck(requestGeneration, QJsonDocument{QJsonObject{{QStringLiteral("messageTaskId"), 99}}});
fixture.client->replyToGenerationChecksAsPending = true;
for (auto attempt = 0; attempt < 31; ++attempt) {
QVERIFY(QMetaObject::invokeMethod(&fixture.controller, "pollChatGeneration", Qt::DirectConnection));
}
QCOMPARE(fixture.client->chatGenerationCheckCount, 30);
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(!fixture.controller.thinking());
QVERIFY(fixture.controller.showRetryResponseGeneration());
QVERIFY(!fixture.controller.error().isEmpty());
}
void pendingChatGenerationKeepsRequestActive()
{
ControllerFixture fixture;
fixture.controller.selectChatConversation(42);
const auto requestGeneration = fixture.client->lastChatMessagesGeneration;
fixture.client->deliverChatMessages(requestGeneration, humanMessageResponse(42));
fixture.client->deliverChatSessionCheck(requestGeneration, QJsonDocument{QJsonObject{{QStringLiteral("messageTaskId"), 99}}});
fixture.client->deliverChatGenerationCheck(requestGeneration, {}, 417);
QVERIFY(fixture.controller.requestInProgress());
QVERIFY(fixture.controller.thinking());
QVERIFY(fixture.controller.error().isEmpty());
}
void unexpectedChatGenerationStatusFailsRequest()
{
ControllerFixture fixture;
fixture.controller.selectChatConversation(42);
const auto requestGeneration = fixture.client->lastChatMessagesGeneration;
fixture.client->deliverChatMessages(requestGeneration, humanMessageResponse(42));
fixture.client->deliverChatSessionCheck(requestGeneration, QJsonDocument{QJsonObject{{QStringLiteral("messageTaskId"), 99}}});
fixture.client->deliverChatGenerationCheck(requestGeneration, {}, 418);
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(!fixture.controller.thinking());
QVERIFY(!fixture.controller.error().isEmpty());
}
void successfulGenerationRequiresAssistantMessage_data()
{
QTest::addColumn<QJsonDocument>("response");
QTest::newRow("empty-object") << ocsResponse(QJsonObject{});
QTest::newRow("human-message") << ocsResponse(message(2, 42, QStringLiteral("human"), QStringLiteral("Not an assistant response")));
QTest::newRow("missing-content") << ocsResponse(QJsonObject{{QStringLiteral("id"), 2}, {QStringLiteral("role"), QStringLiteral("assistant")}});
}
void successfulGenerationRequiresAssistantMessage()
{
QFETCH(QJsonDocument, response);
ControllerFixture fixture;
fixture.controller.selectChatConversation(42);
const auto requestGeneration = fixture.client->lastChatMessagesGeneration;
fixture.client->deliverChatMessages(requestGeneration, humanMessageResponse(42));
fixture.client->deliverChatSessionCheck(requestGeneration, ocsResponse(QJsonObject{{QStringLiteral("messageTaskId"), 99}}));
fixture.client->deliverChatGenerationCheck(requestGeneration, response, 200);
QCOMPARE(fixture.controller.messages()->rowCount(), 1);
QVERIFY(!fixture.controller.requestInProgress());
QVERIFY(!fixture.controller.thinking());
QVERIFY(fixture.controller.showRetryResponseGeneration());
QVERIFY(!fixture.controller.error().isEmpty());
}
};
QTEST_GUILESS_MAIN(TestAssistantController)
#include "testassistantcontroller.moc"