2019-07-30 09:35:42 +02:00
'use strict' ;
2017-07-11 20:41:18 +02:00
2017-11-03 02:09:34 +02:00
const fs = require ( 'fs-extra' ) ;
2020-11-07 17:59:37 +02:00
const Logger = require ( '@joplin/lib/Logger' ) . default ;
const { dirname } = require ( '@joplin/lib/path-utils' ) ;
const { DatabaseDriverNode } = require ( '@joplin/lib/database-driver-node.js' ) ;
const { JoplinDatabase } = require ( '@joplin/lib/joplin-database.js' ) ;
const BaseModel = require ( '@joplin/lib/BaseModel' ) . default ;
const Folder = require ( '@joplin/lib/models/Folder.js' ) ;
const Note = require ( '@joplin/lib/models/Note.js' ) ;
const Setting = require ( '@joplin/lib/models/Setting' ) . default ;
2017-11-03 02:09:34 +02:00
const { sprintf } = require ( 'sprintf-js' ) ;
2019-07-30 09:35:42 +02:00
const exec = require ( 'child_process' ) . exec ;
2017-07-11 20:41:18 +02:00
2019-09-19 23:51:18 +02:00
const baseDir = ` ${ dirname ( _ _dirname ) } /tests/cli-integration ` ;
const joplinAppPath = ` ${ _ _dirname } /main.js ` ;
2017-07-11 20:41:18 +02:00
const logger = new Logger ( ) ;
logger . addTarget ( 'console' ) ;
logger . setLevel ( Logger . LEVEL _ERROR ) ;
const dbLogger = new Logger ( ) ;
dbLogger . addTarget ( 'console' ) ;
dbLogger . setLevel ( Logger . LEVEL _INFO ) ;
const db = new JoplinDatabase ( new DatabaseDriverNode ( ) ) ;
db . setLogger ( dbLogger ) ;
function createClient ( id ) {
return {
2019-07-30 09:35:42 +02:00
id : id ,
2019-09-19 23:51:18 +02:00
profileDir : ` ${ baseDir } /client ${ id } ` ,
2017-07-11 20:41:18 +02:00
} ;
}
const client = createClient ( 1 ) ;
2019-09-13 00:16:42 +02:00
function execCommand ( client , command ) {
2020-03-14 01:46:14 +02:00
const exePath = ` node ${ joplinAppPath } ` ;
const cmd = ` ${ exePath } --update-geolocation-disabled --env dev --profile ${ client . profileDir } ${ command } ` ;
2019-09-19 23:51:18 +02:00
logger . info ( ` ${ client . id } : ${ command } ` ) ;
2017-07-11 20:41:18 +02:00
return new Promise ( ( resolve , reject ) => {
exec ( cmd , ( error , stdout , stderr ) => {
if ( error ) {
logger . error ( stderr ) ;
reject ( error ) ;
} else {
resolve ( stdout . trim ( ) ) ;
}
} ) ;
} ) ;
}
function assertTrue ( v ) {
if ( ! v ) throw new Error ( sprintf ( 'Expected "true", got "%s"."' , v ) ) ;
process . stdout . write ( '.' ) ;
}
function assertFalse ( v ) {
if ( v ) throw new Error ( sprintf ( 'Expected "false", got "%s"."' , v ) ) ;
process . stdout . write ( '.' ) ;
}
function assertEquals ( expected , real ) {
if ( expected !== real ) throw new Error ( sprintf ( 'Expecting "%s", got "%s"' , expected , real ) ) ;
process . stdout . write ( '.' ) ;
}
async function clearDatabase ( ) {
2019-07-30 09:35:42 +02:00
await db . transactionExecBatch ( [ 'DELETE FROM folders' , 'DELETE FROM notes' , 'DELETE FROM tags' , 'DELETE FROM note_tags' , 'DELETE FROM resources' , 'DELETE FROM deleted_items' ] ) ;
2017-07-11 20:41:18 +02:00
}
const testUnits = { } ;
testUnits . testFolders = async ( ) => {
await execCommand ( client , 'mkbook nb1' ) ;
let folders = await Folder . all ( ) ;
assertEquals ( 1 , folders . length ) ;
assertEquals ( 'nb1' , folders [ 0 ] . title ) ;
await execCommand ( client , 'mkbook nb1' ) ;
folders = await Folder . all ( ) ;
assertEquals ( 1 , folders . length ) ;
assertEquals ( 'nb1' , folders [ 0 ] . title ) ;
await execCommand ( client , 'rm -r -f nb1' ) ;
folders = await Folder . all ( ) ;
assertEquals ( 0 , folders . length ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:41:18 +02:00
testUnits . testNotes = async ( ) => {
await execCommand ( client , 'mkbook nb1' ) ;
await execCommand ( client , 'mknote n1' ) ;
let notes = await Note . all ( ) ;
assertEquals ( 1 , notes . length ) ;
assertEquals ( 'n1' , notes [ 0 ] . title ) ;
await execCommand ( client , 'rm -f n1' ) ;
notes = await Note . all ( ) ;
assertEquals ( 0 , notes . length ) ;
await execCommand ( client , 'mknote n1' ) ;
await execCommand ( client , 'mknote n2' ) ;
notes = await Note . all ( ) ;
assertEquals ( 2 , notes . length ) ;
2019-07-30 09:35:42 +02:00
await execCommand ( client , 'rm -f \'blabla*\'' ) ;
2017-07-11 20:41:18 +02:00
notes = await Note . all ( ) ;
assertEquals ( 2 , notes . length ) ;
2019-07-30 09:35:42 +02:00
await execCommand ( client , 'rm -f \'n*\'' ) ;
2017-07-11 20:41:18 +02:00
notes = await Note . all ( ) ;
assertEquals ( 0 , notes . length ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:41:18 +02:00
testUnits . testCat = async ( ) => {
await execCommand ( client , 'mkbook nb1' ) ;
await execCommand ( client , 'mknote mynote' ) ;
2020-03-14 01:46:14 +02:00
const folder = await Folder . loadByTitle ( 'nb1' ) ;
const note = await Note . loadFolderNoteByField ( folder . id , 'title' , 'mynote' ) ;
2017-07-11 20:41:18 +02:00
let r = await execCommand ( client , 'cat mynote' ) ;
assertTrue ( r . indexOf ( 'mynote' ) >= 0 ) ;
assertFalse ( r . indexOf ( note . id ) >= 0 ) ;
r = await execCommand ( client , 'cat -v mynote' ) ;
assertTrue ( r . indexOf ( note . id ) >= 0 ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:41:18 +02:00
2017-07-11 20:58:01 +02:00
testUnits . testConfig = async ( ) => {
await execCommand ( client , 'config editor vim' ) ;
await Setting . load ( ) ;
assertEquals ( 'vim' , Setting . value ( 'editor' ) ) ;
await execCommand ( client , 'config editor subl' ) ;
await Setting . load ( ) ;
assertEquals ( 'subl' , Setting . value ( 'editor' ) ) ;
2020-03-14 01:46:14 +02:00
const r = await execCommand ( client , 'config' ) ;
2017-07-11 20:58:01 +02:00
assertTrue ( r . indexOf ( 'editor' ) >= 0 ) ;
assertTrue ( r . indexOf ( 'subl' ) >= 0 ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:58:01 +02:00
testUnits . testCp = async ( ) => {
await execCommand ( client , 'mkbook nb2' ) ;
await execCommand ( client , 'mkbook nb1' ) ;
await execCommand ( client , 'mknote n1' ) ;
await execCommand ( client , 'cp n1' ) ;
2020-03-14 01:46:14 +02:00
const f1 = await Folder . loadByTitle ( 'nb1' ) ;
const f2 = await Folder . loadByTitle ( 'nb2' ) ;
2017-07-11 20:58:01 +02:00
let notes = await Note . previews ( f1 . id ) ;
assertEquals ( 2 , notes . length ) ;
await execCommand ( client , 'cp n1 nb2' ) ;
2020-03-14 01:46:14 +02:00
const notesF1 = await Note . previews ( f1 . id ) ;
2017-07-11 20:58:01 +02:00
assertEquals ( 2 , notesF1 . length ) ;
notes = await Note . previews ( f2 . id ) ;
assertEquals ( 1 , notes . length ) ;
assertEquals ( notesF1 [ 0 ] . title , notes [ 0 ] . title ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:58:01 +02:00
testUnits . testLs = async ( ) => {
await execCommand ( client , 'mkbook nb1' ) ;
await execCommand ( client , 'mknote note1' ) ;
await execCommand ( client , 'mknote note2' ) ;
2020-03-14 01:46:14 +02:00
const r = await execCommand ( client , 'ls' ) ;
2017-07-11 20:58:01 +02:00
assertTrue ( r . indexOf ( 'note1' ) >= 0 ) ;
assertTrue ( r . indexOf ( 'note2' ) >= 0 ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:58:01 +02:00
testUnits . testMv = async ( ) => {
await execCommand ( client , 'mkbook nb2' ) ;
await execCommand ( client , 'mkbook nb1' ) ;
await execCommand ( client , 'mknote n1' ) ;
await execCommand ( client , 'mv n1 nb2' ) ;
2020-03-14 01:46:14 +02:00
const f1 = await Folder . loadByTitle ( 'nb1' ) ;
const f2 = await Folder . loadByTitle ( 'nb2' ) ;
2017-07-11 20:58:01 +02:00
let notes1 = await Note . previews ( f1 . id ) ;
let notes2 = await Note . previews ( f2 . id ) ;
assertEquals ( 0 , notes1 . length ) ;
assertEquals ( 1 , notes2 . length ) ;
await execCommand ( client , 'mknote note1' ) ;
await execCommand ( client , 'mknote note2' ) ;
await execCommand ( client , 'mknote note3' ) ;
await execCommand ( client , 'mknote blabla' ) ;
2019-07-30 09:35:42 +02:00
await execCommand ( client , 'mv \'note*\' nb2' ) ;
2017-07-11 20:58:01 +02:00
notes1 = await Note . previews ( f1 . id ) ;
notes2 = await Note . previews ( f2 . id ) ;
assertEquals ( 1 , notes1 . length ) ;
assertEquals ( 4 , notes2 . length ) ;
2019-07-30 09:35:42 +02:00
} ;
2017-07-11 20:58:01 +02:00
2019-09-13 00:16:42 +02:00
async function main ( ) {
2017-07-11 20:41:18 +02:00
await fs . remove ( baseDir ) ;
logger . info ( await execCommand ( client , 'version' ) ) ;
2019-09-19 23:51:18 +02:00
await db . open ( { name : ` ${ client . profileDir } /database.sqlite ` } ) ;
2020-03-16 04:30:54 +02:00
BaseModel . setDb ( db ) ;
2017-07-11 20:41:18 +02:00
await Setting . load ( ) ;
2017-07-11 20:58:01 +02:00
let onlyThisTest = 'testMv' ;
2017-07-11 20:41:18 +02:00
onlyThisTest = '' ;
2020-03-14 01:46:14 +02:00
for ( const n in testUnits ) {
2017-07-11 20:41:18 +02:00
if ( ! testUnits . hasOwnProperty ( n ) ) continue ;
if ( onlyThisTest && n != onlyThisTest ) continue ;
await clearDatabase ( ) ;
2020-03-14 01:46:14 +02:00
const testName = n . substr ( 4 ) . toLowerCase ( ) ;
2019-09-19 23:51:18 +02:00
process . stdout . write ( ` ${ testName } : ` ) ;
2017-07-11 20:41:18 +02:00
await testUnits [ n ] ( ) ;
console . info ( '' ) ;
}
}
2020-05-21 10:14:33 +02:00
main ( process . argv ) . catch ( error => {
2017-07-11 20:41:18 +02:00
console . info ( '' ) ;
logger . error ( error ) ;
2019-07-30 09:35:42 +02:00
} ) ;