Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions autotests/dfm-search-tests/tst_content_retriever.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
#include <future>
#include <vector>

#include <dfm-search/contentretriever.h>

Check warning on line 13 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-search/contentretriever.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 13 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-search/contentretriever.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <dfm-search/field_names.h>

Check warning on line 14 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <dfm-search/field_names.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 14 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <dfm-search/field_names.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "utils/contenthighlighter.h"

Check warning on line 16 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "utils/contenthighlighter.h" not found.

Check warning on line 16 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "utils/contenthighlighter.h" not found.

#include <lucene++/Document.h>

Check warning on line 18 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <lucene++/Document.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 18 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <lucene++/Document.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lucene++/Field.h>

Check warning on line 19 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <lucene++/Field.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 19 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <lucene++/Field.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lucene++/FSDirectory.h>

Check warning on line 20 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <lucene++/FSDirectory.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 20 in autotests/dfm-search-tests/tst_content_retriever.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <lucene++/FSDirectory.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <lucene++/IndexWriter.h>
#include <lucene++/KeywordAnalyzer.h>
#include <lucene++/LuceneHeaders.h>
Expand Down Expand Up @@ -91,6 +93,11 @@
void fetchContent_semanticRoutingFailsWhenNoDConfig();
void fetchHighlight_semanticRoutingFailsWhenNoDConfig();
void concurrentFetch_sharedRetriever();
void fetchPreview_noKeyword();
void fetchPreview_withKeyword();
void fetchPreview_keywordNotFound();
void fetchPreview_offsetBeyondContent();
void previewSnippet_basic();
};

void tst_ContentRetriever::fetchContent_single()
Expand Down Expand Up @@ -225,6 +232,129 @@
QVERIFY(!failed.load());
}

void tst_ContentRetriever::fetchPreview_noKeyword()
{
QTemporaryDir tempDir;
QVERIFY(tempDir.isValid());

const QString contentIndexDir = tempDir.path() + "/content-index";
createIndex(contentIndexDir, SearchType::Content);

ContentRetriever retriever;
retriever.setIndexDirectory(SearchType::Content, contentIndexDir);

PreviewOptions options;
options.setKeyword(QString());
options.setOffset(6); // skip "hello "
options.setMaxLength(5); // expect "world"

const PreviewResult result = retriever.fetchPreview("/tmp/doc-a.txt",
SearchType::Content,
options);
QCOMPARE(result.content(), QString("world"));
QCOMPARE(result.keywordOffset(), -1);
}

void tst_ContentRetriever::fetchPreview_withKeyword()
{
QTemporaryDir tempDir;
QVERIFY(tempDir.isValid());

const QString contentIndexDir = tempDir.path() + "/content-index";
createIndex(contentIndexDir, SearchType::Content);

ContentRetriever retriever;
retriever.setIndexDirectory(SearchType::Content, contentIndexDir);

// Content of doc-a.txt is "hello world from content index"
// "world" starts at position 6
PreviewOptions options;
options.setKeyword("world");
options.setOffset(0);
options.setMaxLength(10);

const PreviewResult result = retriever.fetchPreview("/tmp/doc-a.txt",
SearchType::Content,
options);
QCOMPARE(result.content(), QString("world from"));
QCOMPARE(result.keywordOffset(), 6);
}

void tst_ContentRetriever::fetchPreview_keywordNotFound()
{
QTemporaryDir tempDir;
QVERIFY(tempDir.isValid());

const QString contentIndexDir = tempDir.path() + "/content-index";
createIndex(contentIndexDir, SearchType::Content);

ContentRetriever retriever;
retriever.setIndexDirectory(SearchType::Content, contentIndexDir);

// Content of doc-a.txt is "hello world from content index" (30 chars)
// offset=300 is beyond the content, so keyword won't be found
PreviewOptions options;
options.setKeyword("world");
options.setOffset(300);
options.setMaxLength(10);

const PreviewResult result = retriever.fetchPreview("/tmp/doc-a.txt",
SearchType::Content,
options);
QVERIFY(result.content().isEmpty());
QCOMPARE(result.keywordOffset(), -1);
}

void tst_ContentRetriever::fetchPreview_offsetBeyondContent()
{
QTemporaryDir tempDir;
QVERIFY(tempDir.isValid());

const QString contentIndexDir = tempDir.path() + "/content-index";
createIndex(contentIndexDir, SearchType::Content);

ContentRetriever retriever;
retriever.setIndexDirectory(SearchType::Content, contentIndexDir);

// No keyword, offset beyond content length → empty content
PreviewOptions options;
options.setKeyword(QString());
options.setOffset(1000);
options.setMaxLength(50);

const PreviewResult result = retriever.fetchPreview("/tmp/doc-a.txt",
SearchType::Content,
options);
QVERIFY(result.content().isEmpty());
QCOMPARE(result.keywordOffset(), -1);
}

void tst_ContentRetriever::previewSnippet_basic()
{
using DFMSEARCH::ContentHighlighter::previewSnippet;

const QString content = QStringLiteral("hello world from content index");

// No keyword: simple offset + maxLength slice
int kwOff = 42;
QCOMPARE(previewSnippet(content, 6, 5, QString(), &kwOff), QString("world"));
QCOMPARE(kwOff, -1);

// With keyword: search from offset, return from match position
QCOMPARE(previewSnippet(content, 0, 10, "world", &kwOff), QString("world from"));
QCOMPARE(kwOff, 6);

// Keyword not found
QVERIFY(previewSnippet(content, 0, 10, "nonexistent", &kwOff).isEmpty());
QCOMPARE(kwOff, -1);

// Offset beyond content
QVERIFY(previewSnippet(content, 1000, 10, QString(), &kwOff).isEmpty());

// Negative offset → empty
QVERIFY(previewSnippet(content, -1, 10, QString(), &kwOff).isEmpty());
}

QObject *create_tst_ContentRetriever()
{
return new tst_ContentRetriever();
Expand Down
Loading
Loading