2021-05-21 15:17:21 +02:00
|
|
|
import { setupDatabaseAndSynchronizer, db, switchClient } from '../../testing/test-utils.js';
|
2024-01-05 16:15:47 +02:00
|
|
|
import SearchEngine from './SearchEngine.js';
|
|
|
|
import SearchEngineUtils from './SearchEngineUtils.js';
|
|
|
|
import Setting from '../../models/Setting.js';
|
2021-05-21 15:17:21 +02:00
|
|
|
const Note = require('../../models/Note').default;
|
2021-05-13 14:23:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
let searchEngine: any = null;
|
|
|
|
|
2024-03-02 16:25:27 +02:00
|
|
|
describe('SearchEngineUtils', () => {
|
2022-11-15 12:23:50 +02:00
|
|
|
beforeEach(async () => {
|
2021-05-13 14:23:17 +02:00
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
searchEngine = new SearchEngine();
|
|
|
|
searchEngine.setDb(db());
|
|
|
|
});
|
|
|
|
|
2023-02-20 17:02:29 +02:00
|
|
|
describe('filter todos based on showCompletedTodos', () => {
|
2021-05-13 14:23:17 +02:00
|
|
|
it('show completed', (async () => {
|
|
|
|
const note1 = await Note.save({ title: 'abcd', body: 'body 1' });
|
|
|
|
const todo1 = await Note.save({ title: 'abcd', body: 'todo 1', is_todo: 1 });
|
|
|
|
const todo2 = await Note.save({ title: 'abcd', body: 'todo 2', is_todo: 1, todo_completed: 1590085027710 });
|
|
|
|
await Note.save({ title: 'qwer', body: 'body 2' });
|
|
|
|
await searchEngine.syncTables();
|
|
|
|
|
|
|
|
Setting.setValue('showCompletedTodos', true);
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
const rows = (await SearchEngineUtils.notesForQuery('abcd', true, null, searchEngine)).notes;
|
2021-05-13 14:23:17 +02:00
|
|
|
|
|
|
|
expect(rows.length).toBe(3);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(note1.id);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(todo1.id);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(todo2.id);
|
2021-06-07 11:21:24 +02:00
|
|
|
|
|
|
|
const options: any = {};
|
|
|
|
options.fields = ['id', 'title'];
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
const rows2 = (await SearchEngineUtils.notesForQuery('abcd', true, options, searchEngine)).notes;
|
2021-06-07 11:21:24 +02:00
|
|
|
expect(rows2.length).toBe(3);
|
|
|
|
expect(rows2.map(r=>r.id)).toContain(note1.id);
|
|
|
|
expect(rows2.map(r=>r.id)).toContain(todo1.id);
|
|
|
|
expect(rows2.map(r=>r.id)).toContain(todo2.id);
|
2021-05-13 14:23:17 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('hide completed', (async () => {
|
|
|
|
const note1 = await Note.save({ title: 'abcd', body: 'body 1' });
|
|
|
|
const todo1 = await Note.save({ title: 'abcd', body: 'todo 1', is_todo: 1 });
|
|
|
|
await Note.save({ title: 'qwer', body: 'body 2' });
|
|
|
|
await Note.save({ title: 'abcd', body: 'todo 2', is_todo: 1, todo_completed: 1590085027710 });
|
|
|
|
await searchEngine.syncTables();
|
|
|
|
|
|
|
|
Setting.setValue('showCompletedTodos', false);
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
const rows = (await SearchEngineUtils.notesForQuery('abcd', true, null, searchEngine)).notes;
|
2021-05-13 14:23:17 +02:00
|
|
|
|
|
|
|
expect(rows.length).toBe(2);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(note1.id);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(todo1.id);
|
2021-06-07 11:21:24 +02:00
|
|
|
|
|
|
|
const options: any = {};
|
|
|
|
options.fields = ['id', 'title'];
|
2023-12-13 21:24:58 +02:00
|
|
|
const rows2 = (await SearchEngineUtils.notesForQuery('abcd', true, options, searchEngine)).notes;
|
2021-06-07 11:21:24 +02:00
|
|
|
expect(rows2.length).toBe(2);
|
|
|
|
expect(rows2.map(r=>r.id)).toContain(note1.id);
|
|
|
|
expect(rows2.map(r=>r.id)).toContain(todo1.id);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('show completed (!applyUserSettings)', (async () => {
|
|
|
|
const note1 = await Note.save({ title: 'abcd', body: 'body 1' });
|
|
|
|
const todo1 = await Note.save({ title: 'abcd', body: 'todo 1', is_todo: 1 });
|
|
|
|
await Note.save({ title: 'qwer', body: 'body 2' });
|
|
|
|
const todo2 = await Note.save({ title: 'abcd', body: 'todo 2', is_todo: 1, todo_completed: 1590085027710 });
|
|
|
|
await searchEngine.syncTables();
|
|
|
|
|
|
|
|
Setting.setValue('showCompletedTodos', false);
|
|
|
|
|
2023-12-13 21:24:58 +02:00
|
|
|
const rows = (await SearchEngineUtils.notesForQuery('abcd', false, null, searchEngine)).notes;
|
2021-06-07 11:21:24 +02:00
|
|
|
|
|
|
|
expect(rows.length).toBe(3);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(note1.id);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(todo1.id);
|
|
|
|
expect(rows.map(r=>r.id)).toContain(todo2.id);
|
2021-05-13 14:23:17 +02:00
|
|
|
}));
|
|
|
|
});
|
2021-06-11 01:24:50 +02:00
|
|
|
|
|
|
|
it('remove auto added fields', (async () => {
|
|
|
|
await Note.save({ title: 'abcd', body: 'body 1' });
|
|
|
|
await searchEngine.syncTables();
|
|
|
|
|
|
|
|
const testCases = [
|
|
|
|
['title', 'todo_due'],
|
|
|
|
['title', 'todo_completed'],
|
|
|
|
['title'],
|
|
|
|
['title', 'todo_completed', 'todo_due'],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const testCase of testCases) {
|
2023-12-13 21:24:58 +02:00
|
|
|
const rows = (await SearchEngineUtils.notesForQuery('abcd', false, { fields: [...testCase] }, searchEngine)).notes;
|
2021-06-11 01:24:50 +02:00
|
|
|
testCase.push('type_');
|
|
|
|
expect(Object.keys(rows[0]).length).toBe(testCase.length);
|
|
|
|
for (const field of testCase) {
|
|
|
|
expect(rows[0]).toHaveProperty(field);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}));
|
2021-05-13 14:23:17 +02:00
|
|
|
});
|