import { expect , use } from "chai" ; import "mocha" ; import { Client , DownloadFolderCommand , DownloadFolderCommandOptions , UploadFilesCommand , SourceTargetFileNames , UploadFolderCommand , FileSystemFolder , IFileNameFormats , CommandStatus , UploadFilesCommandOptions , UploadFolderCommandOptions , File , Folder , GetFilesRecursivelyCommandOptions , CommandAlreadyExecutedError , } from "../client" ; import { getNextcloudClient } from "./testUtils" ; import GetFilesRecursivelyCommand from "../command/getFilesRecursivelyCommand" ; let client ; describe ( "30-NEXCLOUD-NODE-COMMAND" , function ( ) { beforeEach ( async function ( ) { if ( this . currentTest && this . currentTest . parent ) { client = await getNextcloudClient ( this . currentTest . parent . title + "/" + this . currentTest . title ) ; } } ) ; this . timeout ( 1 * 60 * 10000 ) ; it ( "01 execute upload folder command" , async ( ) => { const sourceFolderName = "./src/test/data/Borstenson" ; const targetFolderName = "/test/30/02/uploadFilesCommand" ; const fsf = new FileSystemFolder ( sourceFolderName ) ; const folderFileNames = await fsf . getFileNames ( ) ; const getTargetFileNameBeforeUpload = ( fileNames ) : string => { return `${ targetFolderName }${ fileNames . targetFileName }` ; } ; const options = { folderName : sourceFolderName , getTargetFileNameBeforeUpload } ; const uc = new UploadFolderCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( uc . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . greaterThan ( 0 ) ; expect ( uc . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; const bytesUploaded = uc . getBytesUploaded ( ) ; expect ( bytesUploaded , "expect a certain number of bytesUploaded" ) . to . be . greaterThan ( 3000000 ) ; } ) ; it ( "02 execute upload files command" , async ( ) => { const sourceFolderName = "./src/test/data/Borstenson" ; const targetFolderName = "/test/30/02/uploadFilesCommand" ; const fsf = new FileSystemFolder ( sourceFolderName ) ; const fileNames = await fsf . getFileNames ( ) ; const files = [ ] ; for ( const fileNameFormat of fileNames ) { files . push ( { sourceFileName : fileNameFormat . absolute , targetFileName : `${ targetFolderName }${ fileNameFormat . relative }` } ) ; } const options = { files } ; const uc = new UploadFilesCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( uc . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . greaterThan ( 0 ) ; expect ( uc . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; } ) ; it ( "03 execute upload files command fails with invalid source file name" , async ( ) => { const sourceFileName = "./does/not/exist.txt" ; const targetFileName = "/test/30/03/uploadFilesCommand" ; const files = [ ] ; files . push ( { sourceFileName , targetFileName } ) ; const options = { files } ; const uc = new UploadFilesCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } let alreadyCalled = false ; while ( uc . isFinished ( ) !== true ) { if ( ! alreadyCalled ) { uc . execute ( ) ; alreadyCalled = true ; } await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain an error" ) . to . be . equal ( 1 ) ; expect ( uc . getStatus ( ) , "command should fail" ) . to . be . equal ( CommandStatus . failed ) ; } ) ; it ( "04 execute upload files command fails with invalid target file name" , async ( ) => { const sourceFileName = "./src/test/data/Borstenson/Company/Borstenson Company Profile.pdf" ; const targetFileName = " " ; const files = [ ] ; files . push ( { sourceFileName , targetFileName } ) ; const options = { files } ; const uc = new UploadFilesCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain an error" ) . to . be . equal ( 1 ) ; expect ( uc . getStatus ( ) , "command should fail" ) . to . be . equal ( CommandStatus . failed ) ; } ) ; it ( "05 execute upload files command with file processing callback after upload" , async ( ) => { const baseName = "Borstenson Company Profile.pdf" ; const sourceFileName = "./src/test/data/Borstenson/Company/" + baseName ; const targetFileName = "/test/30/05/uploadFilesCommand/" + baseName ; const processFileAfterUpload = async ( file ) : Promise < void > => { expect ( file . baseName ) . to . be . equal ( baseName ) ; } ; const files = [ ] ; files . push ( { sourceFileName , targetFileName } ) ; const options = { files , processFileAfterUpload } ; const uc = new UploadFilesCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( uc . getResultMetaData ( ) . messages . length , "result should contain one message" ) . to . be . equal ( 1 ) ; expect ( uc . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; } ) ; it ( "06 execute upload files command should fail in callback function" , async ( ) => { const baseName = "Borstenson Company Profile.pdf" ; const sourceFileName = "./src/test/data/Borstenson/Company/" + baseName ; const targetFileName = "/test/30/06/uploadFilesCommand/" + baseName ; const errorMessage = "This is an error messsage" ; const processFileAfterUpload = async ( file ) : Promise < void > => { throw new Error ( errorMessage ) ; } ; const files = [ ] ; files . push ( { sourceFileName , targetFileName } ) ; const options = { files , processFileAfterUpload } ; const uc = new UploadFilesCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain one error" ) . to . be . equal ( 1 ) ; expect ( uc . getResultMetaData ( ) . errors [ 0 ] ) . to . be . equal ( errorMessage ) ; expect ( uc . getStatus ( ) , "command should be fail" ) . to . be . equal ( CommandStatus . failed ) ; } ) ; it ( "07 execute upload folder command with default target file name processing" , async ( ) => { const sourceFolderName = "./src/test/data/Borstenson" ; const targetFolderName = "/test/30/07/uploadFilesCommand" ; const fsf = new FileSystemFolder ( sourceFolderName ) ; const folderFileNames = await fsf . getFileNames ( ) ; const options = { folderName : sourceFolderName } ; const uc = new UploadFolderCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( uc . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . greaterThan ( 0 ) ; expect ( uc . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; } ) ; it ( "08 execute upload folder command filtering all target names" , async ( ) => { const sourceFolderName = "./src/test/data/Borstenson" ; const targetFolderName = "/test/30/08/uploadFilesCommand" ; const fsf = new FileSystemFolder ( sourceFolderName ) ; const folderFileNames = await fsf . getFileNames ( ) ; const getTargetFileNameBeforeUpload = ( fileNames ) : string => { return "" ; } ; const options = { folderName : sourceFolderName , getTargetFileNameBeforeUpload } ; const uc = new UploadFolderCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( uc . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . equal ( 0 ) ; expect ( uc . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; } ) ; it ( "09 execute upload folder command with non exisiting folder" , async ( ) => { const sourceFolderName = "./this/folder/does/not/exist" ; const targetFolderName = "/test/30/09/uploadFolderCommand" ; const options = { folderName : sourceFolderName } ; const uc = new UploadFolderCommand ( client , options ) ; uc . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( uc . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( uc . getResultMetaData ( ) . errors . length , "result should contain one error" ) . to . be . equal ( 1 ) ; expect ( uc . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . equal ( 0 ) ; expect ( uc . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . failed ) ; } ) ; it ( "10 get files recursively" , async ( ) => { const sourceFolderName = "./src/test/data/Borstenson" ; const targetFolderName = "/test/30/10/GetFilesRecursivelyCommand" ; let sourceFolder = await client . getFolder ( targetFolderName ) ; if ( sourceFolder ) { await sourceFolder . delete ( ) ; } const getTargetFileNameBeforeUpload = ( fileNames ) : string => { return `${ targetFolderName }${ fileNames . targetFileName }` ; } ; const ucfOptions = { folderName : sourceFolderName , getTargetFileNameBeforeUpload } ; const uc = new UploadFolderCommand ( client , ucfOptions ) ; await uc . execute ( ) ; sourceFolder = await client . getFolder ( targetFolderName ) ; expect ( sourceFolder ) . not . to . be . equal ( null ) ; const options = { sourceFolder : sourceFolder ! } ; const command = new GetFilesRecursivelyCommand ( client , options ) ; command . execute ( ) ; async function sleep ( seconds ) { return new Promise ( resolve => setTimeout ( resolve , seconds * 1000 ) ) ; } while ( command . isFinished ( ) !== true ) { await sleep ( 0.1 ) ; } expect ( command . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( command . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . equal ( 1 ) ; expect ( command . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; const files = command . getFiles ( ) ; expect ( files . length ) . to . be . equal ( 14 ) ; let error = null ; try { await command . execute ( ) ; } catch ( e ) { error = e ; } expect ( error , "it should not be possible to execute a command again" ) . to . be . instanceOf ( CommandAlreadyExecutedError ) ; const fileFilterFunction = ( file ) : File | null => { if ( file . mime === "application/pdf" ) { return file ; } if ( file . mime === "image/jpeg" ) { return file ; } return null ; } ; const options2 = { sourceFolder : sourceFolder ! , filterFile : fileFilterFunction , } ; const command2 = new GetFilesRecursivelyCommand ( client , options2 ) ; await command2 . execute ( ) ; const filteredFiles = command2 . getFiles ( ) ; expect ( filteredFiles . length , "expect the number of filtered files is 3" ) . to . be . equal ( 3 ) ; await sourceFolder ! . delete ( ) ; } ) ; it ( "11 get files recursively fails" , async ( ) => { const notARealFolder = { } ; const options = { sourceFolder : notARealFolder as Folder } ; const command = new GetFilesRecursivelyCommand ( client , options ) ; await command . execute ( ) ; expect ( command . getResultMetaData ( ) . errors . length , "result should contain an error" ) . to . be . equal ( 1 ) ; expect ( command . getResultMetaData ( ) . messages . length , "result should contain no messages" ) . to . be . equal ( 0 ) ; expect ( command . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . failed ) ; } ) ; it ( "12 download files recursively" , async ( ) => { const sourceFolderName = "./src/test/data/Borstenson" ; const targetFolderName = "/test/30/12/DownloadFilesRecursivelyCommand" ; let sourceFolder = await client . getFolder ( targetFolderName ) ; if ( sourceFolder ) { await sourceFolder . delete ( ) ; } const getTargetFileNameBeforeUpload = ( fileNames ) : string => { return `${ targetFolderName }${ fileNames . targetFileName }` ; } ; const ucfOptions = { folderName : sourceFolderName , getTargetFileNameBeforeUpload } ; const uc = new UploadFolderCommand ( client , ucfOptions ) ; await uc . execute ( ) ; sourceFolder = await client . getFolder ( targetFolderName ) ; expect ( sourceFolder ) . not . to . be . equal ( null ) ; const fileFilterFunction = ( file ) : File | null => { if ( file . mime === "application/pdf" || file . mime === "image/jpeg" ) { return file ; } return null ; } ; const options = { sourceFolder : sourceFolder ! , filterFile : fileFilterFunction , getTargetFileNameBeforeDownload : ( fileNames ) : string => { return "./tmp/" + fileNames . targetFileName ; } } ; const command = new DownloadFolderCommand ( client , options ) ; command . execute ( ) ; while ( command . isFinished ( ) !== true ) { await ( async ( ) => { return new Promise ( resolve => setTimeout ( resolve , 100 ) ) ; } ) ( ) ; } expect ( command . getResultMetaData ( ) . errors . length , "result should contain no errors" ) . to . be . equal ( 0 ) ; expect ( command . getResultMetaData ( ) . messages . length , "result should contain messages" ) . to . be . equal ( 1 ) ; expect ( command . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . success ) ; expect ( command . getBytesDownloaded ( ) ) . to . be . greaterThan ( 100 ) ; const options2 = { sourceFolder : sourceFolder ! , filterFile : fileFilterFunction , getTargetFileNameBeforeDownload : ( fileNames ) : string => "tt:/\0/invalid filename/" + fileNames . targetFileName } ; const command2 = new DownloadFolderCommand ( client , options2 ) ; await command2 . execute ( ) ; expect ( command2 . getResultMetaData ( ) . errors . length , "result should contain errors" ) . to . be . equal ( 1 ) ; expect ( command2 . getResultMetaData ( ) . messages . length , "result should contain no messages" ) . to . be . equal ( 0 ) ; expect ( command2 . getStatus ( ) , "command should be successfull" ) . to . be . equal ( CommandStatus . failed ) ; await sourceFolder ! . delete ( ) ; } ) ; } ) ;