1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-12 08:54:00 +02:00
joplin/CliClient/tests/synchronizer.js

292 lines
7.1 KiB
JavaScript
Raw Normal View History

2017-06-14 00:39:45 +02:00
import { time } from 'src/time-utils.js';
2017-06-18 22:19:13 +02:00
import { setupDatabase, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient } from 'test-utils.js';
2017-06-15 01:14:15 +02:00
import { createFoldersAndNotes } from 'test-data.js';
2017-06-18 01:49:52 +02:00
import { Folder } from 'src/models/folder.js';
import { Note } from 'src/models/note.js';
2017-06-19 20:58:49 +02:00
import { Setting } from 'src/models/setting.js';
2017-06-18 01:49:52 +02:00
import { BaseItem } from 'src/models/base-item.js';
2017-06-18 22:19:13 +02:00
import { BaseModel } from 'src/base-model.js';
2017-06-19 00:06:10 +02:00
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
});
2017-06-18 22:19:13 +02:00
async function localItemsSameAsRemote(locals, expect) {
try {
2017-06-19 00:06:10 +02:00
let files = await fileApi().list();
expect(locals.length).toBe(files.length);
2017-06-18 22:19:13 +02:00
for (let i = 0; i < locals.length; i++) {
let dbItem = locals[i];
let path = BaseItem.systemPath(dbItem);
let remote = await fileApi().stat(path);
expect(!!remote).toBe(true);
2017-06-19 00:06:10 +02:00
expect(remote.updated_time).toBe(dbItem.updated_time);
2017-06-18 22:19:13 +02:00
let remoteContent = await fileApi().get(path);
2017-06-19 21:26:27 +02:00
remoteContent = dbItem.type_ == BaseModel.MODEL_TYPE_NOTE ? Note.unserialize(remoteContent) : Folder.unserialize(remoteContent);
2017-06-18 22:19:13 +02:00
expect(remoteContent.title).toBe(dbItem.title);
}
} catch (error) {
console.error(error);
}
}
2017-06-15 01:14:15 +02:00
2017-06-18 01:49:52 +02:00
describe('Synchronizer', function() {
2017-06-14 00:39:45 +02:00
2017-06-18 01:49:52 +02:00
beforeEach( async (done) => {
2017-06-18 22:19:13 +02:00
await setupDatabaseAndSynchronizer(1);
await setupDatabaseAndSynchronizer(2);
switchClient(1);
2017-06-18 01:49:52 +02:00
done();
2017-06-14 00:39:45 +02:00
});
2017-06-19 21:26:27 +02:00
it('should create remote items', async (done) => {
let folder = await Folder.save({ title: "folder1" });
await Note.save({ title: "un", parent_id: folder.id });
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
let all = await Folder.all(true);
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
await localItemsSameAsRemote(all, expect);
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
done();
});
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
it('should update remote item', async (done) => {
let folder = await Folder.save({ title: "folder1" });
let note = await Note.save({ title: "un", parent_id: folder.id });
await synchronizer().start();
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
await sleep(0.1);
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
await Note.save({ title: "un UPDATE", id: note.id });
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
let all = await Folder.all(true);
await synchronizer().start();
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
await localItemsSameAsRemote(all, expect);
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
done();
});
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
it('should create local items', async (done) => {
let folder = await Folder.save({ title: "folder1" });
await Note.save({ title: "un", parent_id: folder.id });
await synchronizer().start();
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
switchClient(2);
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
let all = await Folder.all(true);
await localItemsSameAsRemote(all, expect);
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
done();
});
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
it('should update local items', async (done) => {
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
switchClient(2);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await sleep(0.1);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
let note2 = await Note.load(note1.id);
note2.title = "Updated on client 2";
await Note.save(note2);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
note2 = await Note.load(note2.id);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
let files = await fileApi().list();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
switchClient(1);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
note1 = await Note.load(note1.id);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
expect(!!note1).toBe(true);
expect(note1.title).toBe(note2.title);
expect(note1.body).toBe(note2.body);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
done();
});
2017-06-18 22:19:13 +02:00
2017-06-19 21:26:27 +02:00
it('should resolve note conflicts', async (done) => {
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
switchClient(2);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await sleep(0.1);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
let note2 = await Note.load(note1.id);
note2.title = "Updated on client 2";
await Note.save(note2);
note2 = await Note.load(note2.id);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
switchClient(1);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await sleep(0.1);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
let note2conf = await Note.load(note1.id);
note2conf.title = "Updated on client 1";
await Note.save(note2conf);
note2conf = await Note.load(note1.id);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
let conflictFolder = await Folder.conflictFolder();
let conflictedNotes = await Note.all(conflictFolder.id);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
expect(conflictedNotes.length).toBe(1);
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
// Other than the id (since the conflicted note is a duplicate), parent_id (which is now the Conflicts folder) and sync_time,
// the note must be the same in every way, to make sure no data has been lost.
let conflictedNote = conflictedNotes[0];
expect(conflictedNote.id == note2conf.id).toBe(false);
expect(conflictedNote.parent_id == note2conf.parent_id).toBe(false);
for (let n in conflictedNote) {
if (!conflictedNote.hasOwnProperty(n)) continue;
if (n == 'id' || n == 'parent_id') continue;
expect(conflictedNote[n]).toBe(note2conf[n], 'Property: ' + n);
}
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
let noteUpdatedFromRemote = await Note.load(note1.id);
for (let n in noteUpdatedFromRemote) {
if (!noteUpdatedFromRemote.hasOwnProperty(n)) continue;
if (n == 'sync_time') continue;
expect(noteUpdatedFromRemote[n]).toBe(note2[n], 'Property: ' + n);
}
2017-06-19 21:18:22 +02:00
2017-06-19 21:26:27 +02:00
done();
});
2017-06-19 21:18:22 +02:00
it('should resolve folders conflicts', async (done) => {
2017-06-18 22:19:13 +02:00
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: "un", parent_id: folder1.id });
await synchronizer().start();
2017-06-19 21:18:22 +02:00
switchClient(2); // ----------------------------------
2017-06-18 22:19:13 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
await sleep(0.1);
2017-06-19 00:06:10 +02:00
2017-06-19 21:18:22 +02:00
let folder1_modRemote = await Folder.load(folder1.id);
folder1_modRemote.title = "folder1 UPDATE CLIENT 2";
await Folder.save(folder1_modRemote);
folder1_modRemote = await Folder.load(folder1_modRemote.id);
2017-06-14 00:39:45 +02:00
2017-06-18 01:49:52 +02:00
await synchronizer().start();
2017-06-14 00:39:45 +02:00
2017-06-19 21:18:22 +02:00
switchClient(1); // ----------------------------------
2017-06-19 00:06:10 +02:00
2017-06-19 21:18:22 +02:00
await sleep(0.1);
let folder1_modLocal = await Folder.load(folder1.id);
folder1_modLocal.title = "folder1 UPDATE CLIENT 1";
await Folder.save(folder1_modLocal);
folder1_modLocal = await Folder.load(folder1.id);
2017-06-18 22:19:13 +02:00
await synchronizer().start();
2017-06-19 21:18:22 +02:00
let folder1_final = await Folder.load(folder1.id);
expect(folder1_final.title).toBe(folder1_modRemote.title);
2017-06-18 01:49:52 +02:00
done();
2017-06-14 00:39:45 +02:00
});
2017-06-19 21:18:22 +02:00
2017-06-20 00:18:24 +02:00
// it('should delete local items', async (done) => {
// let folder1 = await Folder.save({ title: "folder1" });
// let note1 = await Note.save({ title: "un", parent_id: folder1.id });
// await synchronizer().start();
// switchClient(2);
// await synchronizer().start();
// await sleep(0.1);
// await Note.delete(note1.id);
// await synchronizer().start();
// switchClient(1);
// let files = await fileApi().list();
// console.info(files);
// // await synchronizer().start();
// // note1 = await Note.load(note1.id);
// // expect(!note1).toBe(true);
// done();
// });
// it('should delete remote items', async (done) => {
// let folder1 = await Folder.save({ title: "folder1" });
// let note1 = await Note.save({ title: "un", parent_id: folder1.id });
// await synchronizer().start();
// switchClient(2);
// await synchronizer().start();
// await sleep(0.1);
// await Note.delete(note1.id);
// await synchronizer().start();
// switchClient(1);
// let files = await fileApi().list();
// console.info(files);
// await synchronizer().start();
// note1 = await Note.load(note1.id);
// expect(!note1).toBe(true);
// done();
// });
2017-06-19 00:06:10 +02:00
});