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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2026-06-10
* Description : End-to-end pipeline test:
* hardcoded query -> (mock) LLM -> parse -> resolve.
* Validates the resolution pipeline before any real
* model is integrated. The intent -> Search XML
* serialization is not asserted here.
*
* SPDX-FileCopyrightText: 2026 by Srirupa Datta <srirupa dot sps at gmail dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
// Qt includes
#include <QTest>
#include <QSignalSpy>
#include <QFileInfo>
// Local includes
#include "searchqueryengine.h"
#include "searchmockbackend.h"
#include "searchpromptbuilder.h"
#include "searchintentparser.h"
#include "searchcapabilitydictionary.h"
#include "searchintentresolver.h"
#include "searchquerycache.h"
#include "digikam_config.h"
#include "searchlanguagebackend.h"
#ifdef HAVE_LLAMACPP
# include "searchllamabackend.h"
# include "searchnlmodelmanager.h"
#endif
using namespace Digikam;
class NlSearchPipelineTest : public QObject
{
Q_OBJECT
private:
void buildEngine(SearchQueryEngine& engine,
SearchMockBackend& backend,
SearchPromptBuilder& prompts,
SearchIntentParser& parser,
SearchCapabilityDictionary& dict,<--- Parameter 'dict' can be declared as reference to const
SearchIntentResolver& resolver,
SearchQueryCache& cache)
{
// The mock backend ignores the path, loadModel() only flips the
// "ready" flag that SearchQueryEngine::isReady() checks. There is no
// real model in this test. It validates the backend-independent pipeline.
// Model loading is done by the llama backend (ENABLE_NLSEARCH_LLAMACPP).
backend.loadModel(QLatin1String("mock-no-model"));
engine.setBackend(&backend);
engine.setPromptBuilder(&prompts);
engine.setParser(&parser);
engine.setResolver(&resolver);
engine.setCache(&cache);
}
private Q_SLOTS:
void initTestCase()
{
qRegisterMetaType<SearchQueryIntent>("SearchQueryIntent");
qRegisterMetaType<SearchQueryConstraint>("SearchQueryConstraint");
}
void testCompositeQueryResolves()
{
SearchQueryEngine engine;
SearchMockBackend backend;
SearchPromptBuilder prompts;
SearchIntentParser parser;
SearchCapabilityDictionary dict;
SearchIntentResolver resolver(&dict);
SearchQueryCache cache;
buildEngine(engine, backend, prompts, parser, dict, resolver, cache);
QSignalSpy readySpy(&engine, &SearchQueryEngine::signalIntentReady);
engine.slotInterpretQuery(QLatin1String("sunset photos with red labels"),
QString());
QVERIFY(readySpy.wait(2000));
const auto intent = readySpy.takeFirst().at(0).value<SearchQueryIntent>();
QCOMPARE(intent.constraints.size(), 2);
QCOMPARE(intent.constraints.at(0).field, QLatin1String("tag"));
QCOMPARE(intent.constraints.at(1).field, QLatin1String("colorlabel"));
QCOMPARE(intent.constraints.at(1).value, QLatin1String("red"));
}
// "best photos from last summer" => clarification, never a silent guess.
void testAmbiguousQueryAsksForClarification()
{
SearchQueryEngine engine;
SearchMockBackend backend;
SearchPromptBuilder prompts;
SearchIntentParser parser;
SearchCapabilityDictionary dict;
SearchIntentResolver resolver(&dict);
SearchQueryCache cache;
buildEngine(engine, backend, prompts, parser, dict, resolver, cache);
QSignalSpy clarSpy(&engine, &SearchQueryEngine::signalClarificationRequired);
QSignalSpy readySpy(&engine, &SearchQueryEngine::signalIntentReady);
engine.slotInterpretQuery(QLatin1String("best photos from last summer"),
QString());
QVERIFY(clarSpy.wait(2000));
QCOMPARE(readySpy.count(), 0);
const auto intent = clarSpy.takeFirst().at(0).value<SearchQueryIntent>();
QVERIFY(intent.requiresClarification);
QVERIFY(!intent.clarificationChoices.isEmpty());
}
// Unknown query => user-visible error, nothing invented.
void testUnknownQueryProducesErrorNotGuess()
{
SearchQueryEngine engine;
SearchMockBackend backend;
SearchPromptBuilder prompts;
SearchIntentParser parser;
SearchCapabilityDictionary dict;
SearchIntentResolver resolver(&dict);
SearchQueryCache cache;
buildEngine(engine, backend, prompts, parser, dict, resolver, cache);
QSignalSpy errorSpy(&engine, &SearchQueryEngine::signalErrorOccurred);
engine.slotInterpretQuery(QLatin1String("my happiest photos"), QString());
QVERIFY(errorSpy.wait(2000));
}
void testCacheHitSkipsInference()
{
SearchQueryEngine engine;
SearchMockBackend backend;
SearchPromptBuilder prompts;
SearchIntentParser parser;
SearchCapabilityDictionary dict;
SearchIntentResolver resolver(&dict);
SearchQueryCache cache;
buildEngine(engine, backend, prompts, parser, dict, resolver, cache);
QSignalSpy readySpy(&engine, &SearchQueryEngine::signalIntentReady);
engine.slotInterpretQuery(QLatin1String("photos from Paris in 2023"), QString());
QVERIFY(readySpy.wait(2000));
QCOMPARE(cache.size(), 1);
// Unload model: a cache hit must still answer instantly.
backend.unloadModel();
engine.slotInterpretQuery(QLatin1String("Photos from PARIS in 2023 "), QString());
QVERIFY(readySpy.wait(2000));
QCOMPARE(readySpy.count(), 2);
}
// Malformed model output is rejected, never applied.
void testMalformedOutputRejected()
{
SearchIntentParser parser;
const auto intent = parser.parse("not json at all { broken",
QLatin1String("q"), QLatin1String("q"));
QVERIFY(!intent.parseSucceeded);
const auto intent2 = parser.parse(
R"({ "constraints": [ { "field": "droptable", "op": "eq", "value": "x" } ] })",
QLatin1String("q"), QLatin1String("q"));
QVERIFY(!intent2.parseSucceeded);
}
void testNumericJsonValueParsed()
{
// Regression: the model may emit numeric values as JSON numbers
// (e.g. rating: 5) rather than strings ("5").
SearchIntentParser parser;
const QByteArray json =
QByteArrayLiteral("{\"constraints\":[{\"field\":\"rating\","
"\"op\":\"eq\",\"value\":5}]}");
const SearchQueryIntent intent = parser.parse(json,
QLatin1String("five star photos"),
QLatin1String("five star photos"));
QVERIFY(intent.parseSucceeded);
QCOMPARE(intent.constraints.size(), 1);
QCOMPARE(intent.constraints.first().field, QLatin1String("rating"));
QCOMPARE(intent.constraints.first().op, QLatin1String("eq"));
QCOMPARE(intent.constraints.first().value, QLatin1String("5"));
}
void testDateRangeValuePreserved()
{
// Regression: date ranges must survive parsing intact in the
// "start..end" form the search UI expects.
SearchIntentParser parser;
const QByteArray json =
QByteArrayLiteral("{\"constraints\":[{\"field\":\"daterange\","
"\"op\":\"between\","
"\"value\":\"2023-01-01..2023-12-31\"}]}");
const SearchQueryIntent intent = parser.parse(json,
QLatin1String("photos from 2023"),
QLatin1String("photos from 2023"));
QVERIFY(intent.parseSucceeded);
QCOMPARE(intent.constraints.size(), 1);
QCOMPARE(intent.constraints.first().op, QLatin1String("between"));
QCOMPARE(intent.constraints.first().value,
QLatin1String("2023-01-01..2023-12-31"));
}
void testVideoDurationParses()
{
// Regression: videoduration must accept the "between" operator with a
// numeric range. Previously "between" was restricted to daterange and
// rating, so video-property queries were rejected by the parser.
SearchIntentParser parser;
const QByteArray json =
QByteArrayLiteral("{\"constraints\":[{\"field\":\"videoduration\","
"\"op\":\"between\","
"\"value\":\"300..99999\"}]}");
const SearchQueryIntent intent = parser.parse(json,
QLatin1String("videos longer than 5 minutes"),
QLatin1String("videos longer than 5 minutes"));
QVERIFY(intent.parseSucceeded);
QCOMPARE(intent.constraints.size(), 1);
QCOMPARE(intent.constraints.first().field, QLatin1String("videoduration"));
QCOMPARE(intent.constraints.first().op, QLatin1String("between"));
QCOMPARE(intent.constraints.first().value, QLatin1String("300..99999"));
}
void testFileFormatParses()
{
// File format is a plain string field matched with "eq".
SearchIntentParser parser;
const QByteArray json =
QByteArrayLiteral("{\"constraints\":[{\"field\":\"format\","
"\"op\":\"eq\","
"\"value\":\"raw\"}]}");
const SearchQueryIntent intent = parser.parse(json,
QLatin1String("raw files"),
QLatin1String("raw files"));
QVERIFY(intent.parseSucceeded);
QCOMPARE(intent.constraints.size(), 1);
QCOMPARE(intent.constraints.first().field, QLatin1String("format"));
QCOMPARE(intent.constraints.first().value, QLatin1String("raw"));
}
void testRealInferenceProducesConstraints()
{
#ifdef HAVE_LLAMACPP
QString modelPath = SearchNlModelManager::defaultModelPath();
if (modelPath.isEmpty() || !QFileInfo::exists(modelPath))
{
// Allow pointing the test at a model explicitly, e.g. for local runs:
modelPath = QString::fromLocal8Bit(qgetenv("DIGIKAM_NLSEARCH_TEST_MODEL"));
}
if (modelPath.isEmpty() || !QFileInfo::exists(modelPath))
{
QSKIP("Language model not installed; skipping real-inference test.");
}
SearchQueryEngine engine;
SearchLlamaBackend backend;
SearchPromptBuilder prompts;
SearchIntentParser parser;
SearchCapabilityDictionary dict;
SearchIntentResolver resolver(&dict);
SearchQueryCache cache;
QSignalSpy loadedSpy(&backend, &SearchLanguageBackend::signalModelLoaded);
backend.loadModel(modelPath);
// Model loading runs on a worker thread and reports completion via
// signalModelLoaded(bool); wait for it before running inference.
QVERIFY(loadedSpy.wait(30000));
QVERIFY(loadedSpy.takeFirst().at(0).toBool());
engine.setBackend(&backend);
engine.setPromptBuilder(&prompts);
engine.setParser(&parser);
engine.setResolver(&resolver);
engine.setCache(&cache);
QSignalSpy readySpy(&engine, &SearchQueryEngine::signalIntentReady);
engine.slotInterpretQuery(QLatin1String("photos from 2023 rated 5 stars"),
QString());
// Real CPU inference is slow; allow a generous timeout.
QVERIFY(readySpy.wait(60000));
const auto intent = readySpy.takeFirst().at(0).value<SearchQueryIntent>();
QVERIFY(!intent.constraints.isEmpty());
// The query names a year and a rating. Assert both fields appear,
// without over-fitting to the model's exact values (real output is
// not bit-for-bit deterministic across model or prompt changes).
bool hasDate = false;
bool hasRating = false;
for (const SearchQueryConstraint& c : intent.constraints)
{
if (c.field == QLatin1String("daterange")) { hasDate = true; }
if (c.field == QLatin1String("rating")) { hasRating = true; }
}
QVERIFY(hasDate);
QVERIFY(hasRating);
#else
QSKIP("Built without llama.cpp; real-inference test not applicable.");
#endif
}
};
QTEST_GUILESS_MAIN(NlSearchPipelineTest)
#include "nlsearchpipeline_utest.moc"
|