2022-11-01 17:28:14 +02:00
|
|
|
const BaseCommand = require('./base-command').default;
|
2021-09-17 11:59:10 +02:00
|
|
|
import { reg } from '@joplin/lib/registry';
|
|
|
|
import Note from '@joplin/lib/models/Note';
|
|
|
|
import uuid from '@joplin/lib/uuid';
|
|
|
|
import populateDatabase from '@joplin/lib/services/debug/populateDatabase';
|
2021-10-31 19:36:54 +02:00
|
|
|
import { readCredentialFile } from '@joplin/lib/utils/credentialFiles';
|
|
|
|
import JoplinServerApi from '@joplin/lib/JoplinServerApi';
|
2021-09-17 11:59:10 +02:00
|
|
|
|
|
|
|
function randomElement(array: any[]): any {
|
|
|
|
if (!array.length) return null;
|
|
|
|
return array[Math.floor(Math.random() * array.length)];
|
|
|
|
}
|
|
|
|
|
|
|
|
function itemCount(args: any) {
|
|
|
|
const count = Number(args.arg0);
|
|
|
|
if (!count || isNaN(count)) throw new Error('Note count must be specified');
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Command extends BaseCommand {
|
2023-03-06 16:22:01 +02:00
|
|
|
public usage() {
|
2021-09-17 11:59:10 +02:00
|
|
|
return 'testing <command> [arg0]';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public description() {
|
2021-09-17 11:59:10 +02:00
|
|
|
return 'testing';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public enabled() {
|
2021-09-17 11:59:10 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public options(): any[] {
|
2021-09-17 11:59:10 +02:00
|
|
|
return [
|
|
|
|
['--folder-count <count>', 'Folders to create'],
|
|
|
|
['--note-count <count>', 'Notes to create'],
|
|
|
|
['--tag-count <count>', 'Tags to create'],
|
|
|
|
['--tags-per-note <count>', 'Tags per note'],
|
|
|
|
['--silent', 'Silent'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public async action(args: any) {
|
2021-09-17 11:59:10 +02:00
|
|
|
const { command, options } = args;
|
|
|
|
|
|
|
|
if (command === 'populate') {
|
|
|
|
await populateDatabase(reg.db(), {
|
|
|
|
folderCount: options['folder-count'],
|
|
|
|
noteCount: options['note-count'],
|
|
|
|
tagCount: options['tag-count'],
|
|
|
|
tagsPerNote: options['tags-per-note'],
|
|
|
|
silent: options['silent'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const promises: any[] = [];
|
|
|
|
|
|
|
|
if (command === 'createRandomNotes') {
|
|
|
|
const noteCount = itemCount(args);
|
|
|
|
|
|
|
|
for (let i = 0; i < noteCount; i++) {
|
|
|
|
promises.push(Note.save({
|
|
|
|
title: `Note ${uuid.createNano()}`,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command === 'updateRandomNotes') {
|
|
|
|
const noteCount = itemCount(args);
|
|
|
|
|
|
|
|
const noteIds = await Note.allIds();
|
|
|
|
|
|
|
|
for (let i = 0; i < noteCount; i++) {
|
|
|
|
const noteId = randomElement(noteIds);
|
|
|
|
promises.push(Note.save({
|
|
|
|
id: noteId,
|
|
|
|
title: `Note ${uuid.createNano()}`,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (command === 'deleteRandomNotes') {
|
|
|
|
const noteCount = itemCount(args);
|
|
|
|
const noteIds = await Note.allIds();
|
|
|
|
|
|
|
|
for (let i = 0; i < noteCount; i++) {
|
|
|
|
const noteId = randomElement(noteIds);
|
2024-03-09 12:33:05 +02:00
|
|
|
promises.push(Note.delete(noteId, { sourceDescription: 'command-testing' }));
|
2021-09-17 11:59:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-31 19:36:54 +02:00
|
|
|
if (command === 'joplinServerParallelItemUpdate') {
|
|
|
|
const randomContent = () => {
|
|
|
|
const charCount = Math.random() * 1000;
|
|
|
|
return 'a'.repeat(charCount);
|
|
|
|
};
|
|
|
|
|
|
|
|
const joplinServerAuth = JSON.parse(await readCredentialFile('joplin-server-test.json'));
|
|
|
|
|
|
|
|
const api = new JoplinServerApi({
|
|
|
|
baseUrl: () => joplinServerAuth.baseUrl,
|
|
|
|
userContentBaseUrl: () => joplinServerAuth.userContentBaseUrl,
|
|
|
|
username: () => joplinServerAuth.email,
|
|
|
|
password: () => joplinServerAuth.password,
|
|
|
|
});
|
|
|
|
|
|
|
|
const apiPut = async () => {
|
|
|
|
await api.exec('PUT', 'api/items/root:/testing:/content', {}, randomContent(), {
|
|
|
|
'Content-Type': 'application/octet-stream',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
await apiPut();
|
|
|
|
|
|
|
|
const promises = [];
|
|
|
|
for (let i = 0; i < 100; i++) {
|
|
|
|
promises.push(void apiPut());
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
2023-02-16 12:55:24 +02:00
|
|
|
// eslint-disable-next-line no-console
|
2021-10-31 19:36:54 +02:00
|
|
|
console.info(await api.exec('GET', 'api/items/root:/testing:'));
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:59:10 +02:00
|
|
|
await Promise.all(promises);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Command;
|