From deb443baae02ebf6b79f72955539446261136455 Mon Sep 17 00:00:00 2001 From: Adrien Castex Date: Wed, 28 Jun 2017 15:28:28 +0200 Subject: [PATCH] Added test for the persistence [v2] --- .../persistence/.createPersistenceContext.ts | 55 +++++++++++++++++ .../tests.ts/persistence/autoLoadVirtual.ts | 61 +++++++++++++++++++ .../tests.ts/persistence/autoSaveVirtual.ts | 24 ++++++++ 3 files changed, 140 insertions(+) create mode 100644 test/v2/tests.ts/persistence/.createPersistenceContext.ts create mode 100644 test/v2/tests.ts/persistence/autoLoadVirtual.ts create mode 100644 test/v2/tests.ts/persistence/autoSaveVirtual.ts diff --git a/test/v2/tests.ts/persistence/.createPersistenceContext.ts b/test/v2/tests.ts/persistence/.createPersistenceContext.ts new file mode 100644 index 00000000..d5e45e7b --- /dev/null +++ b/test/v2/tests.ts/persistence/.createPersistenceContext.ts @@ -0,0 +1,55 @@ +import { TestCallback, TestInfo } from '../Type' +import { v2 } from '../../../../lib/index.js' +import * as path from 'path' +import * as fs from 'fs' + +let index = 0; +function getPath(index : number, subName ?: string) : string +{ + const rootPath = path.join(__dirname, 'persistence' + index.toString()); + if(subName) + return path.join(rootPath, subName); + else + return rootPath; +} + +export function starter(info : TestInfo, isValid : TestCallback, callback : (server : v2.WebDAVServer, folder : string, file : string, fileTemp : string) => void) : void +{ + const currentIndex = ++index; + + const root = getPath(currentIndex); + const folder = getPath(currentIndex, 'data'); + const file = getPath(currentIndex, 'save.json'); + const fileTemp = getPath(currentIndex, 'save.tmp.json'); + + const server = info.startServer({ + autoSave: { + treeFilePath: file, + tempTreeFilePath: fileTemp + } + }) + server.rootFileSystem().addSubTree(v2.RequestContext.createExternal(server), { + 'emptyFolder1': v2.ResourceType.Directory, + 'folder1': { + 'emptyFolder2': v2.ResourceType.Directory, + 'file2': v2.ResourceType.File, + 'folder2': { + 'emptyFolder3': v2.ResourceType.Directory, + 'file3': v2.ResourceType.File + } + }, + 'file1': v2.ResourceType.File + }, (e) => { + if(e) return isValid(false, 'Cannot call "addSubTree(...)".', e); + + fs.rmdir(folder, () => { + fs.unlink(file, () => { + fs.unlink(fileTemp, () => { + fs.mkdir(root, (e) => { + callback(server, folder, file, fileTemp); + }) + }) + }) + }) + }) +} diff --git a/test/v2/tests.ts/persistence/autoLoadVirtual.ts b/test/v2/tests.ts/persistence/autoLoadVirtual.ts new file mode 100644 index 00000000..2ca1798c --- /dev/null +++ b/test/v2/tests.ts/persistence/autoLoadVirtual.ts @@ -0,0 +1,61 @@ +import { Test, TestCallback } from '../Type' +import { v2 } from '../../../../lib/index.js' +import { starter } from './.createPersistenceContext' +import * as fs from 'fs' + +function prob(server : v2.WebDAVServer, isValid : TestCallback, path : string | v2.Path, expectedType : v2.ResourceType, callback : () => void) : void +{ + const ctx = server.createExternalContext(); + server.getResource(ctx, path, (e, r) => { + if(e) + return isValid(false, 'Could not find the resource ' + path, e); + + r.type((e, type) => { + if(e) + return isValid(false, 'Error with the resource ' + path, e); + if(type !== expectedType) + return isValid(false, 'Wrong type for the resource ' + path); + + callback(); + }) + }) +} + +export default ((info, isValid) => +{ + info.init(1); + + starter(info, isValid, (server, folder, file, fileTmp) => { + info.req({ + url: 'http://localhost:' + server.options.port + '/file1', + method: 'PUT', + body: 'This is my content!' + }, () => { + setTimeout(() => { + const server2 = info.startServer(server.options); + server2.autoLoad((e) => { + isValid(!e, 'Could not autoLoad the supposed saved server state.', e); + + prob(server2, isValid, '/folder1', v2.ResourceType.Directory, () => { + prob(server2, isValid, '/folder1/emptyFolder2', v2.ResourceType.Directory, () => { + prob(server2, isValid, '/folder1/file2', v2.ResourceType.File, () => { + prob(server2, isValid, '/folder1/folder2', v2.ResourceType.Directory, () => { + prob(server2, isValid, '/folder1/folder2/emptyFolder3', v2.ResourceType.Directory, () => { + prob(server2, isValid, '/folder1/folder2/file3', v2.ResourceType.File, () => { + prob(server2, isValid, '/emptyFolder1', v2.ResourceType.Directory, () => { + prob(server2, isValid, '/file1', v2.ResourceType.File, () => { + isValid(true); + }) + }) + }) + }) + }) + }) + }) + }) + }) + }, 1000); + }) + }) + +}) as Test; diff --git a/test/v2/tests.ts/persistence/autoSaveVirtual.ts b/test/v2/tests.ts/persistence/autoSaveVirtual.ts new file mode 100644 index 00000000..3fca173d --- /dev/null +++ b/test/v2/tests.ts/persistence/autoSaveVirtual.ts @@ -0,0 +1,24 @@ +import { Test } from '../Type' +import { v2 } from '../../../../lib/index.js' +import { starter } from './.createPersistenceContext' +import * as fs from 'fs' + +export default ((info, isValid) => +{ + info.init(1); + + starter(info, isValid, (server, folder, file, fileTmp) => { + info.req({ + url: 'http://localhost:' + server.options.port + '/file1', + method: 'PUT', + body: 'This is my content!' + }, () => { + setTimeout(() => { + fs.exists(file, (exists) => { + isValid(exists, 'The save file is not created.'); + }) + }, 1000); + }) + }) + +}) as Test;