2021-08-07 13:22:37 +02:00
|
|
|
import { syncDir, synchronizer, supportDir, loadEncryptionMasterKey, setupDatabaseAndSynchronizer, switchClient } from '../testing/test-utils';
|
|
|
|
import Setting from '../models/Setting';
|
|
|
|
import Folder from '../models/Folder';
|
|
|
|
import Note from '../models/Note';
|
|
|
|
import Tag from '../models/Tag';
|
|
|
|
import Resource from '../models/Resource';
|
|
|
|
import markdownUtils from '../markdownUtils';
|
|
|
|
import shim from '../shim';
|
|
|
|
import * as fs from 'fs-extra';
|
2020-07-25 12:38:43 +02:00
|
|
|
|
2021-05-21 15:17:21 +02:00
|
|
|
const snapshotBaseDir = `${supportDir}/syncTargetSnapshots`;
|
2020-08-02 13:28:50 +02:00
|
|
|
|
2020-07-25 12:38:43 +02:00
|
|
|
const testData = {
|
|
|
|
folder1: {
|
|
|
|
subFolder1: {},
|
|
|
|
subFolder2: {
|
|
|
|
note1: {
|
|
|
|
resource: true,
|
|
|
|
tags: ['tag1'],
|
|
|
|
},
|
|
|
|
note2: {},
|
|
|
|
},
|
|
|
|
note3: {
|
|
|
|
tags: ['tag1', 'tag2'],
|
|
|
|
},
|
|
|
|
note4: {
|
|
|
|
tags: ['tag2'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
folder2: {},
|
|
|
|
folder3: {
|
|
|
|
note5: {
|
|
|
|
resource: true,
|
|
|
|
tags: ['tag2'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-08-07 13:22:37 +02:00
|
|
|
async function createTestData(data: any) {
|
|
|
|
async function recurseStruct(s: any, parentId: string = '') {
|
2020-07-25 12:38:43 +02:00
|
|
|
for (const n in s) {
|
|
|
|
if (n.toLowerCase().includes('folder')) {
|
|
|
|
const folder = await Folder.save({ title: n, parent_id: parentId });
|
|
|
|
await recurseStruct(s[n], folder.id);
|
|
|
|
} else {
|
|
|
|
const note = await Note.save({ title: n, parent_id: parentId });
|
|
|
|
if (s[n].resource) {
|
2021-05-21 15:17:21 +02:00
|
|
|
await shim.attachFileToNote(note, `${supportDir}/photo.jpg`);
|
2020-07-25 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s[n].tags) {
|
|
|
|
for (const tagTitle of s[n].tags) {
|
|
|
|
await Tag.addNoteTagByTitle(note.id, tagTitle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await recurseStruct(data);
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:22:37 +02:00
|
|
|
async function checkTestData(data: any) {
|
|
|
|
async function recurseCheck(s: any) {
|
2020-07-25 12:38:43 +02:00
|
|
|
for (const n in s) {
|
|
|
|
const obj = s[n];
|
|
|
|
|
|
|
|
if (n.toLowerCase().includes('folder')) {
|
|
|
|
const folder = await Folder.loadByTitle(n);
|
|
|
|
if (!folder) throw new Error(`Cannot load folder: ${n}`);
|
|
|
|
await recurseCheck(obj);
|
|
|
|
} else {
|
|
|
|
const note = await Note.loadByTitle(n);
|
|
|
|
if (!note) throw new Error(`Cannot load note: ${n}`);
|
|
|
|
|
|
|
|
const parent = await Folder.load(note.parent_id);
|
|
|
|
if (!parent) throw new Error(`Cannot load note parent: ${n}`);
|
|
|
|
|
|
|
|
if (obj.resource) {
|
|
|
|
const urls = markdownUtils.extractImageUrls(note.body);
|
|
|
|
const resourceId = urls[0].substr(2);
|
|
|
|
const resource = await Resource.load(resourceId);
|
|
|
|
if (!resource) throw new Error(`Cannot load note resource: ${n}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (obj.tags) {
|
|
|
|
for (const tagTitle of obj.tags) {
|
|
|
|
const tag = await Tag.loadByTitle(tagTitle);
|
|
|
|
if (!tag) throw new Error(`Cannot load note tag: ${tagTitle}`);
|
|
|
|
const hasNote = await Tag.hasNote(tag.id, note.id);
|
|
|
|
if (!hasNote) throw new Error(`Tag not associated with note: ${tagTitle}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await recurseCheck(data);
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:22:37 +02:00
|
|
|
async function deploySyncTargetSnapshot(syncTargetType: string, syncVersion: number) {
|
2020-08-02 13:28:50 +02:00
|
|
|
const sourceDir = `${snapshotBaseDir}/${syncVersion}/${syncTargetType}`;
|
|
|
|
await fs.remove(syncDir);
|
|
|
|
await fs.copy(sourceDir, syncDir);
|
|
|
|
}
|
|
|
|
|
2021-08-07 13:22:37 +02:00
|
|
|
async function main(syncTargetType: string) {
|
2020-07-25 12:38:43 +02:00
|
|
|
const validSyncTargetTypes = ['normal', 'e2ee'];
|
2021-05-21 15:17:21 +02:00
|
|
|
if (!validSyncTargetTypes.includes(syncTargetType)) throw new Error(`Sync target type must be: ${validSyncTargetTypes.join(', ')}`);
|
2020-07-25 12:38:43 +02:00
|
|
|
|
|
|
|
await setupDatabaseAndSynchronizer(1);
|
|
|
|
await switchClient(1);
|
|
|
|
await createTestData(testData);
|
|
|
|
|
|
|
|
if (syncTargetType === 'e2ee') {
|
|
|
|
Setting.setValue('encryption.enabled', true);
|
|
|
|
await loadEncryptionMasterKey();
|
|
|
|
}
|
|
|
|
|
|
|
|
await synchronizer().start();
|
|
|
|
|
|
|
|
if (!Setting.value('syncVersion')) throw new Error('syncVersion is not set');
|
2020-08-02 13:28:50 +02:00
|
|
|
const destDir = `${snapshotBaseDir}/${Setting.value('syncVersion')}/${syncTargetType}`;
|
2020-07-25 12:38:43 +02:00
|
|
|
await fs.mkdirp(destDir); // Create intermediate directories
|
|
|
|
await fs.remove(destDir);
|
|
|
|
await fs.mkdirp(destDir);
|
|
|
|
await fs.copy(syncDir, destDir);
|
|
|
|
|
2021-05-21 15:17:21 +02:00
|
|
|
console.info(`Sync target snapshot created in: ${destDir}`);
|
2020-07-25 12:38:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-07 13:22:37 +02:00
|
|
|
export {
|
2020-07-25 12:38:43 +02:00
|
|
|
checkTestData,
|
|
|
|
main,
|
|
|
|
testData,
|
2020-08-02 13:28:50 +02:00
|
|
|
deploySyncTargetSnapshot,
|
2021-05-21 15:17:21 +02:00
|
|
|
};
|