2021-05-21 15:17:21 +02:00
import BaseModel from '../BaseModel' ;
2021-08-12 17:54:10 +02:00
import { fileApi } from '../testing/../testing/test-utils' ;
2021-05-21 15:17:21 +02:00
import Folder from '../models/Folder' ;
import Note from '../models/Note' ;
import BaseItem from '../models/BaseItem' ;
2024-03-02 16:25:27 +02:00
import { FolderEntity , NoteEntity } from '../services/database/types' ;
2020-12-01 20:05:24 +02:00
export async function allNotesFolders() {
const folders = await Folder . all ( ) ;
const notes = await Note . all ( ) ;
return folders . concat ( notes ) ;
}
async function remoteItemsByTypes ( types : number [ ] ) {
const list = await fileApi ( ) . list ( '' , { includeDirs : false , syncItemsOnly : true } ) ;
2021-11-03 14:26:26 +02:00
if ( list . hasMore ) throw new Error ( 'Not implemented!!!' ) ;
2020-12-01 20:05:24 +02:00
const files = list . items ;
const output = [ ] ;
for ( const file of files ) {
const remoteContent = await fileApi ( ) . get ( file . path ) ;
const content = await BaseItem . unserialize ( remoteContent ) ;
if ( types . indexOf ( content . type_ ) < 0 ) continue ;
output . push ( content ) ;
}
return output ;
}
2024-03-02 16:25:27 +02:00
export async function remoteNotesAndFolders ( ) : Promise < ( NoteEntity | FolderEntity ) [ ] > {
2020-12-01 20:05:24 +02:00
return remoteItemsByTypes ( [ BaseModel . TYPE_NOTE , BaseModel . TYPE_FOLDER ] ) ;
}
export async function remoteNotesFoldersResources() {
return remoteItemsByTypes ( [ BaseModel . TYPE_NOTE , BaseModel . TYPE_FOLDER , BaseModel . TYPE_RESOURCE ] ) ;
}
export async function remoteResources() {
return remoteItemsByTypes ( [ BaseModel . TYPE_RESOURCE ] ) ;
}
2024-04-05 13:16:49 +02:00
// eslint-disable-next-line @typescript-eslint/ban-types, @typescript-eslint/no-explicit-any -- Old code before rule was applied, Old code before rule was applied
2020-12-01 20:05:24 +02:00
export async function localNotesFoldersSameAsRemote ( locals : any [ ] , expect : Function ) {
let error = null ;
try {
const nf = await remoteNotesAndFolders ( ) ;
expect ( locals . length ) . toBe ( nf . length ) ;
for ( let i = 0 ; i < locals . length ; i ++ ) {
const dbItem = locals [ i ] ;
const path = BaseItem . systemPath ( dbItem ) ;
const remote = await fileApi ( ) . stat ( path ) ;
expect ( ! ! remote ) . toBe ( true ) ;
if ( ! remote ) continue ;
let remoteContent = await fileApi ( ) . get ( path ) ;
2022-07-23 11:33:12 +02:00
remoteContent = dbItem . type_ === BaseModel . TYPE_NOTE ? await Note . unserialize ( remoteContent ) : await Folder . unserialize ( remoteContent ) ;
2020-12-01 20:05:24 +02:00
expect ( remoteContent . title ) . toBe ( dbItem . title ) ;
}
} catch ( e ) {
error = e ;
}
expect ( error ) . toBe ( null ) ;
}