2020-11-14 14:37:18 +02:00
|
|
|
import Setting from '@joplin/lib/models/Setting';
|
|
|
|
import BaseModel from '@joplin/lib/BaseModel';
|
|
|
|
import shim from '@joplin/lib/shim';
|
2020-12-09 14:50:51 +02:00
|
|
|
import markdownUtils from '@joplin/lib/markdownUtils';
|
2020-12-01 20:05:24 +02:00
|
|
|
const { sortedIds, createNTestNotes, setupDatabaseAndSynchronizer, switchClient, checkThrowAsync } = require('./test-utils.js');
|
2021-01-22 19:41:11 +02:00
|
|
|
import Folder from '@joplin/lib/models/Folder';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
2020-11-07 17:59:37 +02:00
|
|
|
const ArrayUtils = require('@joplin/lib/ArrayUtils.js');
|
2018-05-03 14:11:45 +02:00
|
|
|
|
2020-03-14 12:01:45 +02:00
|
|
|
async function allItems() {
|
|
|
|
const folders = await Folder.all();
|
|
|
|
const notes = await Note.all();
|
|
|
|
return folders.concat(notes);
|
|
|
|
}
|
2018-05-03 14:11:45 +02:00
|
|
|
|
2020-03-14 12:01:45 +02:00
|
|
|
describe('models_Note', function() {
|
2018-05-03 14:11:45 +02:00
|
|
|
beforeEach(async (done) => {
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should find resource and note IDs', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
2019-09-19 23:51:18 +02:00
|
|
|
let note2 = await Note.save({ title: 'ma deuxième note', body: `Lien vers première note : ${Note.markdownTag(note1)}`, parent_id: folder1.id });
|
2018-05-03 14:11:45 +02:00
|
|
|
|
|
|
|
let items = await Note.linkedItems(note2.body);
|
|
|
|
expect(items.length).toBe(1);
|
|
|
|
expect(items[0].id).toBe(note1.id);
|
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
await shim.attachFileToNote(note2, `${__dirname}/../tests/support/photo.jpg`);
|
2018-05-03 14:11:45 +02:00
|
|
|
note2 = await Note.load(note2.id);
|
|
|
|
items = await Note.linkedItems(note2.body);
|
|
|
|
expect(items.length).toBe(2);
|
|
|
|
expect(items[0].type_).toBe(BaseModel.TYPE_NOTE);
|
|
|
|
expect(items[1].type_).toBe(BaseModel.TYPE_RESOURCE);
|
2018-09-30 20:24:02 +02:00
|
|
|
|
2019-09-19 23:51:18 +02:00
|
|
|
const resource2 = await shim.createResourceFromPath(`${__dirname}/../tests/support/photo.jpg`);
|
|
|
|
const resource3 = await shim.createResourceFromPath(`${__dirname}/../tests/support/photo.jpg`);
|
|
|
|
note2.body += `<img alt="bla" src=":/${resource2.id}"/>`;
|
|
|
|
note2.body += `<img src=':/${resource3.id}' />`;
|
2018-09-30 20:24:02 +02:00
|
|
|
items = await Note.linkedItems(note2.body);
|
|
|
|
expect(items.length).toBe(4);
|
2018-05-03 14:11:45 +02:00
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should find linked items', (async () => {
|
2018-11-21 01:18:56 +02:00
|
|
|
const testCases = [
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226)', ['06894e83b8f84d3d8cbe0f1587f9e226']],
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226) [](:/06894e83b8f84d3d8cbe0f1587f9e226)', ['06894e83b8f84d3d8cbe0f1587f9e226']],
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226) [](:/06894e83b8f84d3d8cbe0f1587f9e227)', ['06894e83b8f84d3d8cbe0f1587f9e226', '06894e83b8f84d3d8cbe0f1587f9e227']],
|
|
|
|
['[](:/06894e83b8f84d3d8cbe0f1587f9e226 "some title")', ['06894e83b8f84d3d8cbe0f1587f9e226']],
|
|
|
|
];
|
|
|
|
|
|
|
|
for (let i = 0; i < testCases.length; i++) {
|
|
|
|
const t = testCases[i];
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
const input = t[0] as string;
|
2018-11-21 01:18:56 +02:00
|
|
|
const expected = t[1];
|
|
|
|
const actual = Note.linkedItemIds(input);
|
|
|
|
const contentEquals = ArrayUtils.contentEquals(actual, expected);
|
|
|
|
|
|
|
|
// console.info(contentEquals, input, expected, actual);
|
|
|
|
|
|
|
|
expect(contentEquals).toBe(true);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should change the type of notes', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
2018-10-04 19:34:30 +02:00
|
|
|
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
|
|
|
|
let changedNote = Note.changeNoteType(note1, 'todo');
|
|
|
|
expect(changedNote === note1).toBe(false);
|
|
|
|
expect(!!changedNote.is_todo).toBe(true);
|
|
|
|
await Note.save(changedNote);
|
|
|
|
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
changedNote = Note.changeNoteType(note1, 'todo');
|
|
|
|
expect(changedNote === note1).toBe(true);
|
|
|
|
expect(!!changedNote.is_todo).toBe(true);
|
|
|
|
|
|
|
|
note1 = await Note.load(note1.id);
|
|
|
|
changedNote = Note.changeNoteType(note1, 'note');
|
|
|
|
expect(changedNote === note1).toBe(false);
|
|
|
|
expect(!!changedNote.is_todo).toBe(false);
|
|
|
|
}));
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should serialize and unserialize without modifying data', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
2019-05-06 22:25:14 +02:00
|
|
|
const testCases = [
|
2020-02-05 00:11:35 +02:00
|
|
|
[{ title: '', body: 'Body and no title\nSecond line\nThird Line', parent_id: folder1.id },
|
2019-05-06 22:25:14 +02:00
|
|
|
'', 'Body and no title\nSecond line\nThird Line'],
|
2020-02-05 00:11:35 +02:00
|
|
|
[{ title: 'Note title', body: 'Body and title', parent_id: folder1.id },
|
2019-05-06 22:25:14 +02:00
|
|
|
'Note title', 'Body and title'],
|
2020-02-05 00:11:35 +02:00
|
|
|
[{ title: 'Title and no body', body: '', parent_id: folder1.id },
|
2019-05-06 22:25:14 +02:00
|
|
|
'Title and no body', ''],
|
2019-07-30 09:35:42 +02:00
|
|
|
];
|
|
|
|
|
2019-05-06 22:25:14 +02:00
|
|
|
for (let i = 0; i < testCases.length; i++) {
|
|
|
|
const t = testCases[i];
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2020-11-14 14:37:18 +02:00
|
|
|
const input: any = t[0];
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const note1 = await Note.save(input);
|
|
|
|
const serialized = await Note.serialize(note1);
|
|
|
|
const unserialized = await Note.unserialize(serialized);
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2019-05-06 22:25:14 +02:00
|
|
|
expect(unserialized.title).toBe(input.title);
|
|
|
|
expect(unserialized.body).toBe(input.body);
|
|
|
|
}
|
|
|
|
}));
|
2018-10-04 19:34:30 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should reset fields for a duplicate', (async () => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const note1 = await Note.save({ title: 'note', parent_id: folder1.id });
|
2020-02-05 13:18:14 +02:00
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const duplicatedNote = await Note.duplicate(note1.id);
|
2020-02-05 13:18:14 +02:00
|
|
|
|
|
|
|
expect(duplicatedNote !== note1).toBe(true);
|
|
|
|
expect(duplicatedNote.created_time !== note1.created_time).toBe(true);
|
|
|
|
expect(duplicatedNote.updated_time !== note1.updated_time).toBe(true);
|
|
|
|
expect(duplicatedNote.user_created_time !== note1.user_created_time).toBe(true);
|
|
|
|
expect(duplicatedNote.user_updated_time !== note1.user_updated_time).toBe(true);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should delete a set of notes', (async () => {
|
2020-03-14 12:01:45 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const noOfNotes = 20;
|
|
|
|
await createNTestNotes(noOfNotes, folder1);
|
|
|
|
|
|
|
|
const noteIds = await Folder.noteIds(folder1.id);
|
|
|
|
await Note.batchDelete(noteIds);
|
|
|
|
|
|
|
|
const all = await allItems();
|
|
|
|
expect(all.length).toBe(1);
|
|
|
|
expect(all[0].id).toBe(folder1.id);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should delete only the selected notes', (async () => {
|
2020-03-14 12:01:45 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
|
|
|
|
const noOfNotes = 20;
|
|
|
|
await createNTestNotes(noOfNotes, f1, null, 'note1');
|
|
|
|
await createNTestNotes(noOfNotes, f2, null, 'note1');
|
|
|
|
|
|
|
|
const allBeforeDelete = await allItems();
|
|
|
|
|
|
|
|
const notesInFolder1IDs = await Folder.noteIds(f1.id);
|
|
|
|
const notesInFolder2IDs = await Folder.noteIds(f2.id);
|
|
|
|
|
|
|
|
const notesToRemoveFromFolder1 = notesInFolder1IDs.slice(0, 6);
|
|
|
|
const notesToRemoveFromFolder2 = notesInFolder2IDs.slice(11, 14);
|
|
|
|
|
|
|
|
await Note.batchDelete(notesToRemoveFromFolder1);
|
|
|
|
await Note.batchDelete(notesToRemoveFromFolder2);
|
|
|
|
|
|
|
|
const allAfterDelete = await allItems();
|
|
|
|
|
|
|
|
const expectedLength = allBeforeDelete.length - notesToRemoveFromFolder1.length - notesToRemoveFromFolder2.length;
|
|
|
|
expect(allAfterDelete.length).toBe(expectedLength);
|
|
|
|
|
|
|
|
// Common elements between the to-be-deleted notes and the notes and folders remaining after the delete
|
2020-05-21 10:14:33 +02:00
|
|
|
const intersection = [...notesToRemoveFromFolder1, ...notesToRemoveFromFolder2].filter(x => allAfterDelete.includes(x));
|
2020-03-14 12:01:45 +02:00
|
|
|
// Should be empty
|
|
|
|
expect(intersection.length).toBe(0);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should delete nothing', (async () => {
|
2020-03-14 12:01:45 +02:00
|
|
|
const f1 = await Folder.save({ title: 'folder1' });
|
|
|
|
const f2 = await Folder.save({ title: 'folder2', parent_id: f1.id });
|
|
|
|
const f3 = await Folder.save({ title: 'folder3', parent_id: f2.id });
|
|
|
|
const f4 = await Folder.save({ title: 'folder4', parent_id: f1.id });
|
|
|
|
|
|
|
|
const noOfNotes = 20;
|
|
|
|
await createNTestNotes(noOfNotes, f1, null, 'note1');
|
|
|
|
await createNTestNotes(noOfNotes, f2, null, 'note2');
|
|
|
|
await createNTestNotes(noOfNotes, f3, null, 'note3');
|
|
|
|
await createNTestNotes(noOfNotes, f4, null, 'note4');
|
|
|
|
|
|
|
|
const beforeDelete = await allItems();
|
|
|
|
await Note.batchDelete([]);
|
|
|
|
const afterDelete = await allItems();
|
|
|
|
|
|
|
|
expect(sortedIds(afterDelete)).toEqual(sortedIds(beforeDelete));
|
|
|
|
}));
|
2020-03-15 23:53:49 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should not move to conflict folder', (async () => {
|
2020-03-15 23:53:49 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'Folder' });
|
|
|
|
const folder2 = await Folder.save({ title: Folder.conflictFolderTitle(), id: Folder.conflictFolderId() });
|
|
|
|
const note1 = await Note.save({ title: 'note', parent_id: folder1.id });
|
|
|
|
|
|
|
|
const hasThrown = await checkThrowAsync(async () => await Folder.moveToFolder(note1.id, folder2.id));
|
|
|
|
expect(hasThrown).toBe(true);
|
|
|
|
|
|
|
|
const note = await Note.load(note1.id);
|
|
|
|
expect(note.parent_id).toEqual(folder1.id);
|
|
|
|
}));
|
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should not copy to conflict folder', (async () => {
|
2020-03-15 23:53:49 +02:00
|
|
|
const folder1 = await Folder.save({ title: 'Folder' });
|
|
|
|
const folder2 = await Folder.save({ title: Folder.conflictFolderTitle(), id: Folder.conflictFolderId() });
|
|
|
|
const note1 = await Note.save({ title: 'note', parent_id: folder1.id });
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
const hasThrown = await checkThrowAsync(async () => await Note.copyToFolder(note1.id, folder2.id));
|
2020-03-15 23:53:49 +02:00
|
|
|
expect(hasThrown).toBe(true);
|
|
|
|
}));
|
2020-04-14 00:55:24 +02:00
|
|
|
|
2020-12-01 20:05:24 +02:00
|
|
|
it('should convert resource paths from internal to external paths', (async () => {
|
2020-04-14 00:55:24 +02:00
|
|
|
const resourceDirName = Setting.value('resourceDirName');
|
|
|
|
const resourceDir = Setting.value('resourceDir');
|
|
|
|
const r1 = await shim.createResourceFromPath(`${__dirname}/../tests/support/photo.jpg`);
|
|
|
|
const r2 = await shim.createResourceFromPath(`${__dirname}/../tests/support/photo.jpg`);
|
2020-11-14 12:59:26 +02:00
|
|
|
const r3 = await shim.createResourceFromPath(`${__dirname}/../tests/support/welcome.pdf`);
|
2020-07-23 21:38:42 +02:00
|
|
|
const note1 = await Note.save({ title: 'note1' });
|
2020-05-30 16:28:42 +02:00
|
|
|
const t1 = r1.updated_time;
|
|
|
|
const t2 = r2.updated_time;
|
2020-04-14 00:55:24 +02:00
|
|
|
|
2020-12-09 14:50:51 +02:00
|
|
|
const resourceDirE = markdownUtils.escapeLinkUrl(resourceDir);
|
|
|
|
|
2020-04-14 00:55:24 +02:00
|
|
|
const testCases = [
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
`![](:/${r1.id})`,
|
|
|
|
`![](${resourceDirName}/${r1.id}.jpg)`,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
false,
|
|
|
|
`![](:/${r1.id}) ![](:/${r1.id}) ![](:/${r2.id})`,
|
|
|
|
`![](${resourceDirName}/${r1.id}.jpg) ![](${resourceDirName}/${r1.id}.jpg) ![](${resourceDirName}/${r2.id}.jpg)`,
|
|
|
|
],
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
`![](:/${r1.id})`,
|
2020-12-09 14:50:51 +02:00
|
|
|
`![](file://${resourceDirE}/${r1.id}.jpg?t=${t1})`,
|
2020-04-14 00:55:24 +02:00
|
|
|
],
|
|
|
|
[
|
|
|
|
true,
|
|
|
|
`![](:/${r1.id}) ![](:/${r1.id}) ![](:/${r2.id})`,
|
2020-12-09 14:50:51 +02:00
|
|
|
`![](file://${resourceDirE}/${r1.id}.jpg?t=${t1}) ![](file://${resourceDirE}/${r1.id}.jpg?t=${t1}) ![](file://${resourceDirE}/${r2.id}.jpg?t=${t2})`,
|
2020-04-14 00:55:24 +02:00
|
|
|
],
|
2020-11-14 12:59:26 +02:00
|
|
|
[
|
|
|
|
true,
|
|
|
|
`![](:/${r3.id})`,
|
2020-12-09 14:50:51 +02:00
|
|
|
`![](file://${resourceDirE}/${r3.id}.pdf)`,
|
2020-11-14 12:59:26 +02:00
|
|
|
],
|
2020-04-14 00:55:24 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
for (const testCase of testCases) {
|
|
|
|
const [useAbsolutePaths, input, expected] = testCase;
|
2021-01-22 19:41:11 +02:00
|
|
|
const internalToExternal = await Note.replaceResourceInternalToExternalLinks(input as string, { useAbsolutePaths });
|
2020-11-19 20:34:53 +02:00
|
|
|
expect(internalToExternal).toBe(expected);
|
2020-04-14 00:55:24 +02:00
|
|
|
|
|
|
|
const externalToInternal = await Note.replaceResourceExternalToInternalLinks(internalToExternal, { useAbsolutePaths });
|
2020-11-19 20:34:53 +02:00
|
|
|
expect(externalToInternal).toBe(input);
|
2020-04-14 00:55:24 +02:00
|
|
|
}
|
2020-07-23 21:38:42 +02:00
|
|
|
|
|
|
|
const result = await Note.replaceResourceExternalToInternalLinks(`[](joplin://${note1.id})`);
|
2020-11-19 20:34:53 +02:00
|
|
|
expect(result).toBe(`[](:/${note1.id})`);
|
2020-04-14 00:55:24 +02:00
|
|
|
}));
|
|
|
|
|
2021-01-07 18:29:53 +02:00
|
|
|
it('should perform natural sorting', (async () => {
|
|
|
|
const folder1 = await Folder.save({});
|
|
|
|
|
|
|
|
const sortedNotes = await Note.previews(folder1.id, {
|
|
|
|
fields: ['id', 'title'],
|
|
|
|
order: [{ by: 'title', dir: 'ASC' }],
|
|
|
|
});
|
|
|
|
expect(sortedNotes.length).toBe(0);
|
|
|
|
|
2021-01-22 19:41:11 +02:00
|
|
|
const note0 = await Note.save({ title: 'A3', parent_id: folder1.id, is_todo: 0 });
|
|
|
|
const note1 = await Note.save({ title: 'A20', parent_id: folder1.id, is_todo: 0 });
|
|
|
|
const note2 = await Note.save({ title: 'A100', parent_id: folder1.id, is_todo: 0 });
|
|
|
|
const note3 = await Note.save({ title: 'égalité', parent_id: folder1.id, is_todo: 0 });
|
|
|
|
const note4 = await Note.save({ title: 'z', parent_id: folder1.id, is_todo: 0 });
|
2021-01-07 18:29:53 +02:00
|
|
|
|
|
|
|
const sortedNotes2 = await Note.previews(folder1.id, {
|
|
|
|
fields: ['id', 'title'],
|
|
|
|
order: [{ by: 'title', dir: 'ASC' }],
|
|
|
|
});
|
|
|
|
expect(sortedNotes2.length).toBe(5);
|
|
|
|
expect(sortedNotes2[0].id).toBe(note0.id);
|
|
|
|
expect(sortedNotes2[1].id).toBe(note1.id);
|
|
|
|
expect(sortedNotes2[2].id).toBe(note2.id);
|
|
|
|
expect(sortedNotes2[3].id).toBe(note3.id);
|
|
|
|
expect(sortedNotes2[4].id).toBe(note4.id);
|
|
|
|
|
|
|
|
const todo3 = Note.changeNoteType(note3, 'todo');
|
|
|
|
const todo4 = Note.changeNoteType(note4, 'todo');
|
|
|
|
await Note.save(todo3);
|
|
|
|
await Note.save(todo4);
|
|
|
|
|
|
|
|
const sortedNotes3 = await Note.previews(folder1.id, {
|
|
|
|
fields: ['id', 'title'],
|
|
|
|
order: [{ by: 'title', dir: 'ASC' }],
|
|
|
|
uncompletedTodosOnTop: true,
|
|
|
|
});
|
|
|
|
expect(sortedNotes3.length).toBe(5);
|
|
|
|
expect(sortedNotes3[0].id).toBe(note3.id);
|
|
|
|
expect(sortedNotes3[1].id).toBe(note4.id);
|
|
|
|
expect(sortedNotes3[2].id).toBe(note0.id);
|
|
|
|
expect(sortedNotes3[3].id).toBe(note1.id);
|
|
|
|
expect(sortedNotes3[4].id).toBe(note2.id);
|
|
|
|
}));
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
});
|