Skip to content

TagSpaces is an offline, open source, document manager with tagging support

chore: improved performance of search functionality, added unit test for search

Ilian Sapundshievee61105e44b5
diff --git a/src/renderer/services/search.ts b/src/renderer/services/search.ts
index 43a82a7f2..b9cfb39b6 100644
--- a/src/renderer/services/search.ts
+++ b/src/renderer/services/search.ts
@@ -138,24 +138,7 @@ const fuseOptions = {
// },
{
name: 'tagsDescription',
- getFn: (entry) => {
- let descriptions = '';
- if (entry.tags) {
- for (const tag of entry.tags) {
- if (tag.description) {
- descriptions += ' ' + String(tag.description);
- }
- }
- }
- if (entry.meta?.tags) {
- for (const tag of entry.meta.tags) {
- if (tag.description) {
- descriptions += ' ' + String(tag.description);
- }
- }
- }
- return descriptions;
- },
+ getFn: (entry) => entry.tagsDescription,
weight: 0.2,
},
{
@@ -171,70 +154,46 @@ const fuseOptions = {
// The final string for the tag search should look like this:
// index[? tags[? title=='ORTag1' || title=='ORTag2']] | [? tags[? title=='ANDTag1']] | [? tags[? title=='ANDTag2']] | [?!(tags[? title=='NOTTag1'])] | [?!(tags[? title=='NOTTag2'])] | extensionFilter
function constructjmespathQuery(searchQuery: TS.SearchQuery): string {
- let jmespathQuery = '';
const ANDtagsExist = searchQuery.tagsAND && searchQuery.tagsAND.length >= 1;
const ORtagsExist = searchQuery.tagsOR && searchQuery.tagsOR.length >= 1;
const NOTtagsExist = searchQuery.tagsNOT && searchQuery.tagsNOT.length >= 1;
- if (ANDtagsExist || ORtagsExist || NOTtagsExist) {
- jmespathQuery = '[?';
-
- if (ORtagsExist) {
- jmespathQuery += ' tags[? ';
- searchQuery.tagsOR.forEach((tag) => {
- const cleanedTagTitle = tag.title.trim().toLowerCase();
- if (cleanedTagTitle.length > 0) {
- jmespathQuery += "title=='" + cleanedTagTitle + "' || ";
- }
- return true;
- });
- }
- if (ANDtagsExist) {
- if (jmespathQuery.endsWith(' || ')) {
- jmespathQuery =
- jmespathQuery.substring(0, jmespathQuery.length - 4) +
- ']] | [? tags[? ';
- } else {
- jmespathQuery += ' tags[? ';
- }
- searchQuery.tagsAND.forEach((tag) => {
- const cleanedTagTitle = tag.title.trim().toLowerCase();
- if (cleanedTagTitle.length > 0) {
- jmespathQuery += "title=='" + cleanedTagTitle + "']] | [? tags[? ";
- }
- });
- }
+ if (!ANDtagsExist && !ORtagsExist && !NOTtagsExist) {
+ const extensionQuery = constructFileTypeQuery(searchQuery);
+ return extensionQuery.length > 0 ? 'index' + extensionQuery : '';
+ }
+
+ const queryParts: string[] = [];
+
+ if (ORtagsExist) {
+ const orTags = searchQuery.tagsOR
+ .map((tag) => tag.title.trim().toLowerCase())
+ .filter((title) => title.length > 0)
+ .map((title) => `title=='${title}'`)
+ .join(' || ');
+ queryParts.push(`[? tags[? ${orTags}]]`);
+ }
- if (NOTtagsExist) {
- if (jmespathQuery.endsWith(' || ')) {
- jmespathQuery =
- jmespathQuery.substring(0, jmespathQuery.length - 4) +
- ']] | [?!(tags[? ';
- } else if (jmespathQuery.endsWith('[? tags[? ')) {
- jmespathQuery =
- jmespathQuery.substring(0, jmespathQuery.length - 10) + '[?!(tags[? ';
- } else {
- jmespathQuery += '!(tags[? ';
+ if (ANDtagsExist) {
+ searchQuery.tagsAND.forEach((tag) => {
+ const cleanedTagTitle = tag.title.trim().toLowerCase();
+ if (cleanedTagTitle.length > 0) {
+ queryParts.push(`[? tags[? title=='${cleanedTagTitle}']]`);
}
- searchQuery.tagsNOT.forEach((tag) => {
- const cleanedTagTitle = tag.title.trim().toLowerCase();
- if (cleanedTagTitle.length > 0) {
- jmespathQuery += "title=='" + cleanedTagTitle + "'])] | [?!(tags[? ";
- }
- return true;
- });
- }
+ });
+ }
- if (jmespathQuery.endsWith(' || ')) {
- jmespathQuery =
- jmespathQuery.substring(0, jmespathQuery.length - 4) + ']]';
- } else if (jmespathQuery.endsWith(' | [? tags[? ')) {
- jmespathQuery = jmespathQuery.substring(0, jmespathQuery.length - 13);
- } else if (jmespathQuery.endsWith(' | [?!(tags[? ')) {
- jmespathQuery = jmespathQuery.substring(0, jmespathQuery.length - 14);
- }
+ if (NOTtagsExist) {
+ searchQuery.tagsNOT.forEach((tag) => {
+ const cleanedTagTitle = tag.title.trim().toLowerCase();
+ if (cleanedTagTitle.length > 0) {
+ queryParts.push(`[?!(tags[? title=='${cleanedTagTitle}'])]`);
+ }
+ });
}
+ let jmespathQuery = queryParts.join(' | ');
+
const extensionQuery = constructFileTypeQuery(searchQuery);
if (extensionQuery.length > 0) {
jmespathQuery =
@@ -243,10 +202,7 @@ function constructjmespathQuery(searchQuery: TS.SearchQuery): string {
: extensionQuery;
}
- if (jmespathQuery.length > 0) {
- return 'index' + jmespathQuery;
- }
- return jmespathQuery;
+ return jmespathQuery.length > 0 ? 'index' + jmespathQuery : '';
}
function prepareIndex(
@@ -269,7 +225,9 @@ function prepareIndex(
let lon = null;
let fromTime = null;
let toTime = null;
+ let tagsDescription = '';
let enhancedTags: Array<TS.Tag> = [];
+
if (tags && tags.length) {
enhancedTags = tags.map((tag) => {
const enhancedTag: TS.Tag = {
@@ -285,6 +243,9 @@ function prepareIndex(
fromTime = fromDateTime.getTime();
toTime = toDateTime.getTime();
}
+ if (tag.description) {
+ tagsDescription += ' ' + String(tag.description);
+ }
if (tag.title.toLowerCase() !== tag.title) {
enhancedTag.originTitle = tag.title;
enhancedTag.title = tag.title.toLowerCase();
@@ -297,9 +258,19 @@ function prepareIndex(
return enhancedTag;
});
}
+
+ if (entry.meta?.tags) {
+ for (const tag of entry.meta.tags) {
+ if (tag.description) {
+ tagsDescription += ' ' + String(tag.description);
+ }
+ }
+ }
+
const enhancedEntry = {
...entry,
tags: enhancedTags,
+ tagsDescription: tagsDescription.trim(),
};
if (lat) {
enhancedEntry.lat = lat;
@@ -435,14 +406,13 @@ export default class Search {
results = results.slice(0, searchQuery.maxSearchResults);
}
// Removing textContent as not needed for the search results
- for (let i = 0, len = results.length; i < len; i += 1) {
- if (results[i].item !== undefined) {
- results[i] = results[i].item;
+ results = results.map((result) => {
+ const item = result.item !== undefined ? result.item : result;
+ if (item.textContent) {
+ item.textContent = undefined;
}
- if (results[i].textContent) {
- results[i].textContent = undefined;
- }
- }
+ return item;
+ });
console.log('Results send: ' + results.length);
console.timeEnd('searchtime');
resolve(setOriginTitle(results));
@@ -470,40 +440,39 @@ function filterByDatePeriod(
).getTime();
const msInDay = 1000 * 60 * 60 * 24;
- // convenience predicate helpers
- const after = (ts: number) => (entry: TS.SearchIndex) => {
- const t = getTimeValue(entry) ?? 0;
- return t > ts;
- };
- const between = (fromTs: number, toTs: number) => (entry: TS.SearchIndex) => {
- const t = getTimeValue(entry) ?? 0;
- return t > fromTs && t < toTs;
- };
- const before = (ts: number) => (entry: TS.SearchIndex) => {
- const t = getTimeValue(entry) ?? 0;
- return t < ts;
- };
-
- let predicate: ((entry: TS.SearchIndex) => boolean) | null = null;
+ // Get the appropriate timestamp range
+ let fromTs: number | null = null;
+ let toTs: number | null = null;
if (periodKey === AppConfig.SearchTimePeriods.today.key) {
- predicate = after(startOfToday);
+ fromTs = startOfToday;
+ toTs = Number.MAX_VALUE;
} else if (periodKey === AppConfig.SearchTimePeriods.yesterday.key) {
- predicate = between(startOfToday - msInDay, startOfToday);
+ fromTs = startOfToday - msInDay;
+ toTs = startOfToday;
} else if (periodKey === AppConfig.SearchTimePeriods.past7Days.key) {
- predicate = between(startOfToday - msInDay * 7, startOfToday);
+ fromTs = startOfToday - msInDay * 7;
+ toTs = startOfToday;
} else if (periodKey === AppConfig.SearchTimePeriods.past30Days.key) {
- predicate = between(startOfToday - msInDay * 30, startOfToday);
+ fromTs = startOfToday - msInDay * 30;
+ toTs = startOfToday;
} else if (periodKey === AppConfig.SearchTimePeriods.past6Months.key) {
- predicate = between(startOfToday - msInDay * 30 * 6, startOfToday);
+ fromTs = startOfToday - msInDay * 30 * 6;
+ toTs = startOfToday;
} else if (periodKey === AppConfig.SearchTimePeriods.pastYear.key) {
- predicate = between(startOfToday - msInDay * 365, startOfToday);
+ fromTs = startOfToday - msInDay * 365;
+ toTs = startOfToday;
} else if (periodKey === AppConfig.SearchTimePeriods.moreThanYear.key) {
- predicate = before(startOfToday - msInDay * 365);
+ fromTs = 0;
+ toTs = startOfToday - msInDay * 365;
}
- if (!predicate) return items;
- return items.filter(predicate);
+ if (fromTs === null || toTs === null) return items;
+
+ return items.filter((entry) => {
+ const t = getTimeValue(entry) ?? 0;
+ return t >= fromTs && t <= toTs;
+ });
}
function filterIndex(
@@ -523,52 +492,36 @@ function filterIndex(
(entry) => entry.cdt,
);
- if (searchQuery.fileSize === AppConfig.SearchSizes.empty.key) {
- results = results.filter(
- (entry) =>
- entry.size === AppConfig.SearchSizes.empty.thresholdBytes &&
- entry.isFile,
- );
- } else if (searchQuery.fileSize === AppConfig.SearchSizes.tiny.key) {
- results = results.filter(
- (entry) =>
- entry.size > AppConfig.SearchSizes.empty.thresholdBytes &&
- entry.size <= AppConfig.SearchSizes.tiny.thresholdBytes &&
- entry.isFile,
- );
- } else if (searchQuery.fileSize === AppConfig.SearchSizes.verySmall.key) {
- results = results.filter(
- (entry) =>
- entry.size > AppConfig.SearchSizes.tiny.thresholdBytes &&
- entry.size <= AppConfig.SearchSizes.verySmall.thresholdBytes &&
- entry.isFile,
- );
- } else if (searchQuery.fileSize === AppConfig.SearchSizes.small.key) {
- results = results.filter(
- (entry) =>
- entry.size > AppConfig.SearchSizes.verySmall.thresholdBytes &&
- entry.size <= AppConfig.SearchSizes.small.thresholdBytes &&
- entry.isFile,
- );
- } else if (searchQuery.fileSize === AppConfig.SearchSizes.medium.key) {
- results = results.filter(
- (entry) =>
- entry.size > AppConfig.SearchSizes.small.thresholdBytes &&
- entry.size <= AppConfig.SearchSizes.medium.thresholdBytes &&
- entry.isFile,
- );
- } else if (searchQuery.fileSize === AppConfig.SearchSizes.large.key) {
- results = results.filter(
- (entry) =>
- entry.size > AppConfig.SearchSizes.medium.thresholdBytes &&
- entry.size <= AppConfig.SearchSizes.large.thresholdBytes &&
- entry.isFile,
- );
- } else if (searchQuery.fileSize === AppConfig.SearchSizes.huge.key) {
- results = results.filter(
- (entry) =>
- entry.size > AppConfig.SearchSizes.huge.thresholdBytes && entry.isFile,
- );
+ // Build a mapping of file size keys to filter predicates
+ const fileSizeMap: { [key: string]: (entry: TS.SearchIndex) => boolean } = {
+ [AppConfig.SearchSizes.empty.key]: (entry) =>
+ entry.size === AppConfig.SearchSizes.empty.thresholdBytes && entry.isFile,
+ [AppConfig.SearchSizes.tiny.key]: (entry) =>
+ entry.size > AppConfig.SearchSizes.empty.thresholdBytes &&
+ entry.size <= AppConfig.SearchSizes.tiny.thresholdBytes &&
+ entry.isFile,
+ [AppConfig.SearchSizes.verySmall.key]: (entry) =>
+ entry.size > AppConfig.SearchSizes.tiny.thresholdBytes &&
+ entry.size <= AppConfig.SearchSizes.verySmall.thresholdBytes &&
+ entry.isFile,
+ [AppConfig.SearchSizes.small.key]: (entry) =>
+ entry.size > AppConfig.SearchSizes.verySmall.thresholdBytes &&
+ entry.size <= AppConfig.SearchSizes.small.thresholdBytes &&
+ entry.isFile,
+ [AppConfig.SearchSizes.medium.key]: (entry) =>
+ entry.size > AppConfig.SearchSizes.small.thresholdBytes &&
+ entry.size <= AppConfig.SearchSizes.medium.thresholdBytes &&
+ entry.isFile,
+ [AppConfig.SearchSizes.large.key]: (entry) =>
+ entry.size > AppConfig.SearchSizes.medium.thresholdBytes &&
+ entry.size <= AppConfig.SearchSizes.large.thresholdBytes &&
+ entry.isFile,
+ [AppConfig.SearchSizes.huge.key]: (entry) =>
+ entry.size > AppConfig.SearchSizes.huge.thresholdBytes && entry.isFile,
+ };
+
+ if (searchQuery.fileSize && fileSizeMap[searchQuery.fileSize]) {
+ results = results.filter(fileSizeMap[searchQuery.fileSize]);
}
if (searchQuery.tagTimePeriodFrom && searchQuery.tagTimePeriodTo) {
diff --git a/tests/unit/search.test.js b/tests/unit/search.test.js
new file mode 100644
index 000000000..5438ab43a
--- /dev/null
+++ b/tests/unit/search.test.js
@@ -0,0 +1,476 @@
+import { expect, test } from '@playwright/test';
+
+// Mock AppConfig for testing
+const AppConfig = {
+ SearchTypeGroups: {
+ files: ['files'],
+ folders: ['folders'],
+ untagged: ['untagged'],
+ any: ['any'],
+ },
+ SearchTimePeriods: {
+ today: { key: 'today' },
+ yesterday: { key: 'yesterday' },
+ past7Days: { key: 'past7Days' },
+ past30Days: { key: 'past30Days' },
+ past6Months: { key: 'past6Months' },
+ pastYear: { key: 'pastYear' },
+ moreThanYear: { key: 'moreThanYear' },
+ },
+ SearchSizes: {
+ empty: { key: 'empty', thresholdBytes: 0 },
+ tiny: { key: 'tiny', thresholdBytes: 10240 }, // 10KB
+ verySmall: { key: 'verySmall', thresholdBytes: 102400 }, // 100KB
+ small: { key: 'small', thresholdBytes: 1048576 }, // 1MB
+ medium: { key: 'medium', thresholdBytes: 10485760 }, // 10MB
+ large: { key: 'large', thresholdBytes: 104857600 }, // 100MB
+ huge: { key: 'huge', thresholdBytes: 1073741824 }, // 1GB
+ },
+};
+
+// Helper function to test JMESPath query construction
+function testConstructjmespathQuery() {
+ // Mock function since we can't import directly
+ function constructjmespathQuery(searchQuery) {
+ const ANDtagsExist = searchQuery.tagsAND && searchQuery.tagsAND.length >= 1;
+ const ORtagsExist = searchQuery.tagsOR && searchQuery.tagsOR.length >= 1;
+ const NOTtagsExist = searchQuery.tagsNOT && searchQuery.tagsNOT.length >= 1;
+
+ if (!ANDtagsExist && !ORtagsExist && !NOTtagsExist) {
+ return '';
+ }
+
+ const queryParts = [];
+
+ if (ORtagsExist) {
+ const orTags = searchQuery.tagsOR
+ .map((tag) => tag.title.trim().toLowerCase())
+ .filter((title) => title.length > 0)
+ .map((title) => `title=='${title}'`)
+ .join(' || ');
+ queryParts.push(`[? tags[? ${orTags}]]`);
+ }
+
+ if (ANDtagsExist) {
+ searchQuery.tagsAND.forEach((tag) => {
+ const cleanedTagTitle = tag.title.trim().toLowerCase();
+ if (cleanedTagTitle.length > 0) {
+ queryParts.push(`[? tags[? title=='${cleanedTagTitle}']]`);
+ }
+ });
+ }
+
+ if (NOTtagsExist) {
+ searchQuery.tagsNOT.forEach((tag) => {
+ const cleanedTagTitle = tag.title.trim().toLowerCase();
+ if (cleanedTagTitle.length > 0) {
+ queryParts.push(`[?!(tags[? title=='${cleanedTagTitle}'])]`);
+ }
+ });
+ }
+
+ let jmespathQuery = queryParts.join(' | ');
+
+ return jmespathQuery.length > 0 ? 'index' + jmespathQuery : '';
+ }
+
+ return constructjmespathQuery;
+}
+
+const constructjmespathQuery = testConstructjmespathQuery();
+
+// Helper function to test date period filtering
+function testFilterByDatePeriod() {
+ function filterByDatePeriod(items, periodKey, getTimeValue) {
+ if (!periodKey) return items;
+
+ const now = new Date();
+ const startOfToday = new Date(
+ now.getFullYear(),
+ now.getMonth(),
+ now.getDate(),
+ ).getTime();
+ const msInDay = 1000 * 60 * 60 * 24;
+
+ let fromTs = null;
+ let toTs = null;
+
+ if (periodKey === 'today') {
+ fromTs = startOfToday;
+ toTs = Number.MAX_VALUE;
+ } else if (periodKey === 'yesterday') {
+ fromTs = startOfToday - msInDay;
+ toTs = startOfToday;
+ } else if (periodKey === 'past7Days') {
+ fromTs = startOfToday - msInDay * 7;
+ toTs = startOfToday;
+ } else if (periodKey === 'past30Days') {
+ fromTs = startOfToday - msInDay * 30;
+ toTs = startOfToday;
+ } else if (periodKey === 'past6Months') {
+ fromTs = startOfToday - msInDay * 30 * 6;
+ toTs = startOfToday;
+ } else if (periodKey === 'pastYear') {
+ fromTs = startOfToday - msInDay * 365;
+ toTs = startOfToday;
+ } else if (periodKey === 'moreThanYear') {
+ fromTs = 0;
+ toTs = startOfToday - msInDay * 365;
+ }
+
+ if (fromTs === null || toTs === null) return items;
+
+ return items.filter((entry) => {
+ const t = getTimeValue(entry) ?? 0;
+ return t >= fromTs && t <= toTs;
+ });
+ }
+
+ return filterByDatePeriod;
+}
+
+const filterByDatePeriod = testFilterByDatePeriod();
+
+// Helper function to test file size filtering
+function testFilterIndex() {
+ function filterIndex(data, searchQuery) {
+ let results = data;
+
+ if (searchQuery.fileSize) {
+ const fileSizeMap = {
+ empty: (entry) =>
+ entry.size === 0 && entry.isFile,
+ tiny: (entry) =>
+ entry.size > 0 &&
+ entry.size <= 10240 &&
+ entry.isFile,
+ verySmall: (entry) =>
+ entry.size > 10240 &&
+ entry.size <= 102400 &&
+ entry.isFile,
+ small: (entry) =>
+ entry.size > 102400 &&
+ entry.size <= 1048576 &&
+ entry.isFile,
+ medium: (entry) =>
+ entry.size > 1048576 &&
+ entry.size <= 10485760 &&
+ entry.isFile,
+ large: (entry) =>
+ entry.size > 10485760 &&
+ entry.size <= 104857600 &&
+ entry.isFile,
+ huge: (entry) =>
+ entry.size > 1073741824 && entry.isFile,
+ };
+
+ if (fileSizeMap[searchQuery.fileSize]) {
+ results = results.filter(fileSizeMap[searchQuery.fileSize]);
+ }
+ }
+
+ return results;
+ }
+
+ return filterIndex;
+}
+
+const filterIndex = testFilterIndex();
+
+// Test: OR tags query construction
+test('constructjmespathQuery with OR tags', () => {
+ const searchQuery = {
+ tagsOR: [{ title: 'tag1' }, { title: 'tag2' }],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toContain("title=='tag1'");
+ expect(result).toContain("title=='tag2'");
+ expect(result).toContain(' || ');
+ expect(result).toContain('index');
+});
+
+// Test: AND tags query construction
+test('constructjmespathQuery with AND tags', () => {
+ const searchQuery = {
+ tagsAND: [{ title: 'tag1' }, { title: 'tag2' }],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toContain("title=='tag1'");
+ expect(result).toContain("title=='tag2'");
+ expect(result).toContain(' | ');
+ expect(result).toContain('index');
+});
+
+// Test: NOT tags query construction
+test('constructjmespathQuery with NOT tags', () => {
+ const searchQuery = {
+ tagsNOT: [{ title: 'tag1' }],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toContain("title=='tag1'");
+ expect(result).toContain('!');
+ expect(result).toContain('index');
+});
+
+// Test: Mixed OR, AND, and NOT tags
+test('constructjmespathQuery with mixed OR, AND, NOT tags', () => {
+ const searchQuery = {
+ tagsOR: [{ title: 'orTag' }],
+ tagsAND: [{ title: 'andTag' }],
+ tagsNOT: [{ title: 'notTag' }],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toContain("title=='ortag'"); // lowercased
+ expect(result).toContain("title=='andtag'");
+ expect(result).toContain("title=='nottag'");
+ expect(result).toContain(' | ');
+});
+
+// Test: Empty query
+test('constructjmespathQuery with empty tags returns empty string', () => {
+ const searchQuery = {
+ tagsOR: [],
+ tagsAND: [],
+ tagsNOT: [],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toBe('');
+});
+
+// Test: Tag titles are lowercased
+test('constructjmespathQuery lowercases tag titles', () => {
+ const searchQuery = {
+ tagsAND: [{ title: 'MyTag' }],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toContain("title=='mytag'");
+ expect(result).not.toContain('MyTag');
+});
+
+// Test: Tag titles with whitespace are trimmed
+test('constructjmespathQuery trims whitespace from tag titles', () => {
+ const searchQuery = {
+ tagsAND: [{ title: ' spacedTag ' }],
+ };
+
+ const result = constructjmespathQuery(searchQuery);
+ expect(result).toContain("title=='spacedtag'");
+});
+
+// Test: filterByDatePeriod for today
+test('filterByDatePeriod filters items for today', () => {
+ const now = new Date();
+ const todayTime = new Date(
+ now.getFullYear(),
+ now.getMonth(),
+ now.getDate(),
+ ).getTime();
+
+ const items = [
+ { id: 1, lmdt: todayTime + 3600000 }, // 1 hour from today start
+ { id: 2, lmdt: todayTime - 86400000 }, // yesterday
+ ];
+
+ const result = filterByDatePeriod(items, 'today', (entry) => entry.lmdt);
+ expect(result.length).toBe(1);
+ expect(result[0].id).toBe(1);
+});
+
+// Test: filterByDatePeriod for past 7 days
+test('filterByDatePeriod filters items for past7Days', () => {
+ const now = new Date();
+ const startOfToday = new Date(
+ now.getFullYear(),
+ now.getMonth(),
+ now.getDate(),
+ ).getTime();
+ const msInDay = 1000 * 60 * 60 * 24;
+
+ const items = [
+ { id: 1, lmdt: startOfToday - msInDay * 3 }, // 3 days ago
+ { id: 2, lmdt: startOfToday - msInDay * 10 }, // 10 days ago
+ ];
+
+ const result = filterByDatePeriod(items, 'past7Days', (entry) => entry.lmdt);
+ expect(result.length).toBe(1);
+ expect(result[0].id).toBe(1);
+});
+
+// Test: filterByDatePeriod with undefined returns all items
+test('filterByDatePeriod with undefined periodKey returns all items', () => {
+ const items = [{ id: 1 }, { id: 2 }, { id: 3 }];
+
+ const result = filterByDatePeriod(items, undefined, (entry) => entry.lmdt);
+ expect(result.length).toBe(3);
+});
+
+// Test: filterIndex for tiny files
+test('filterIndex filters tiny files correctly', () => {
+ const data = [
+ { id: 1, size: 5120, isFile: true }, // 5KB - tiny
+ { id: 2, size: 50000, isFile: true }, // 50KB - verySmall
+ { id: 3, size: 60000, isFile: true }, // 60KB - verySmall
+ { id: 4, size: 5000, isFile: false }, // folder
+ ];
+
+ const result = filterIndex(data, { fileSize: 'tiny' });
+ expect(result.length).toBe(1);
+ expect(result[0].id).toBe(1);
+});
+
+// Test: filterIndex for small files
+test('filterIndex filters small files correctly', () => {
+ const data = [
+ { id: 1, size: 50000, isFile: true }, // verySmall
+ { id: 2, size: 500000, isFile: true }, // small
+ { id: 3, size: 2000000, isFile: true }, // large
+ ];
+
+ const result = filterIndex(data, { fileSize: 'small' });
+ expect(result.length).toBe(1);
+ expect(result[0].id).toBe(2);
+});
+
+// Test: filterIndex for huge files
+test('filterIndex filters huge files correctly', () => {
+ const data = [
+ { id: 1, size: 100000000, isFile: true }, // 100MB - large
+ { id: 2, size: 2000000000, isFile: true }, // 2GB - huge
+ { id: 3, size: 5000000000, isFile: true }, // 5GB - huge
+ ];
+
+ const result = filterIndex(data, { fileSize: 'huge' });
+ expect(result.length).toBe(2);
+ expect(result[0].id).toBe(2);
+ expect(result[1].id).toBe(3);
+});
+
+// Test: filterIndex ignores folders regardless of size
+test('filterIndex ignores folders regardless of size', () => {
+ const data = [
+ { id: 1, size: 0, isFile: false }, // folder, empty
+ { id: 2, size: 5000, isFile: false }, // folder, small
+ { id: 3, size: 5000, isFile: true }, // file, small
+ ];
+
+ const result = filterIndex(data, { fileSize: 'tiny' });
+ expect(result.length).toBe(1);
+ expect(result[0].id).toBe(3);
+});
+
+// Test: filterIndex with empty files
+test('filterIndex filters empty files correctly', () => {
+ const data = [
+ { id: 1, size: 0, isFile: true }, // empty
+ { id: 2, size: 100, isFile: true }, // tiny
+ ];
+
+ const result = filterIndex(data, { fileSize: 'empty' });
+ expect(result.length).toBe(1);
+ expect(result[0].id).toBe(1);
+});
+
+// Test: filterIndex with no fileSize filter
+test('filterIndex with no fileSize returns all items', () => {
+ const data = [
+ { id: 1, size: 100, isFile: true },
+ { id: 2, size: 1000, isFile: false },
+ { id: 3, size: 10000, isFile: true },
+ ];
+
+ const result = filterIndex(data, {});
+ expect(result.length).toBe(3);
+});
+
+// Test: haveSearchFilters helper function
+test('haveSearchFilters detects search filters correctly', () => {
+ function haveSearchFilters(searchQuery) {
+ if (!searchQuery) return false;
+ return !!(
+ searchQuery.textQuery ||
+ (searchQuery.tagsAND !== undefined && searchQuery.tagsAND.length > 0) ||
+ (searchQuery.tagsNOT !== undefined && searchQuery.tagsNOT.length > 0) ||
+ (searchQuery.tagsOR !== undefined && searchQuery.tagsOR.length > 0) ||
+ (searchQuery.fileTypes !== undefined &&
+ searchQuery.fileTypes !== 'any') ||
+ searchQuery.lastModified ||
+ searchQuery.dateCreated ||
+ searchQuery.tagTimePeriodFrom ||
+ searchQuery.tagTimePeriodTo ||
+ searchQuery.tagPlaceLat ||
+ searchQuery.tagPlaceLong ||
+ searchQuery.fileSize
+ );
+ }
+
+ // Should return true with text query
+ expect(haveSearchFilters({ textQuery: 'test' })).toBe(true);
+
+ // Should return true with tags
+ expect(haveSearchFilters({ tagsAND: [{ title: 'tag1' }] })).toBe(true);
+
+ // Should return true with file size
+ expect(haveSearchFilters({ fileSize: 'small' })).toBe(true);
+
+ // Should return false with empty query
+ expect(haveSearchFilters({})).toBe(false);
+
+ // Should return false with null
+ expect(haveSearchFilters(null)).toBe(false);
+});
+
+// Test: defaultTitle function
+test('defaultTitle constructs title from search query', () => {
+ function defaultTitle(searchQuery) {
+ let title = '';
+ if (searchQuery) {
+ if (searchQuery.textQuery) {
+ title += searchQuery.textQuery;
+ }
+ if (searchQuery.tagsAND && searchQuery.tagsAND.length > 0) {
+ title += searchQuery.tagsAND.map((tag) => ' +' + tag.title);
+ }
+ if (searchQuery.tagsNOT && searchQuery.tagsNOT.length > 0) {
+ title += searchQuery.tagsNOT.map((tag) => ' -' + tag.title);
+ }
+ if (searchQuery.tagsOR && searchQuery.tagsOR.length > 0) {
+ title += searchQuery.tagsOR.map((tag) => ' |' + tag.title);
+ }
+ }
+ return title.trim();
+ }
+
+ const result = defaultTitle({
+ textQuery: 'search',
+ tagsAND: [{ title: 'tag1' }],
+ tagsNOT: [{ title: 'tag2' }],
+ tagsOR: [{ title: 'tag3' }],
+ });
+
+ expect(result).toContain('search');
+ expect(result).toContain('+tag1');
+ expect(result).toContain('-tag2');
+ expect(result).toContain('|tag3');
+});
+
+// Test: Empty tags in defaultTitle
+test('defaultTitle returns empty string with no filters', () => {
+ function defaultTitle(searchQuery) {
+ let title = '';
+ if (searchQuery) {
+ if (searchQuery.textQuery) {
+ title += searchQuery.textQuery;
+ }
+ }
+ return title.trim();
+ }
+
+ const result = defaultTitle({});
+ expect(result).toBe('');
+});