From c5d31dba56d669c0df0209761c43c5a6ac7cec4a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 28 Dec 2017 13:59:22 +0100 Subject: Tests directories refractor --- server/tests/utils/miscs/miscs.ts | 52 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 server/tests/utils/miscs/miscs.ts (limited to 'server/tests/utils/miscs/miscs.ts') diff --git a/server/tests/utils/miscs/miscs.ts b/server/tests/utils/miscs/miscs.ts new file mode 100644 index 000000000..424b0db98 --- /dev/null +++ b/server/tests/utils/miscs/miscs.ts @@ -0,0 +1,52 @@ +import * as WebTorrent from 'webtorrent' +import { readFile, readdir } from 'fs' + +let webtorrent = new WebTorrent() + +function readFilePromise (path: string) { + return new Promise((res, rej) => { + readFile(path, (err, data) => { + if (err) return rej(err) + + return res(data) + }) + }) +} + +function readdirPromise (path: string) { + return new Promise((res, rej) => { + readdir(path, (err, files) => { + if (err) return rej(err) + + return res(files) + }) + }) +} + + // Default interval -> 2 minutes +function dateIsValid (dateString: string, interval = 120000) { + const dateToCheck = new Date(dateString) + const now = new Date() + + return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval +} + +function wait (milliseconds: number) { + return new Promise(resolve => setTimeout(resolve, milliseconds)) +} + +function webtorrentAdd (torrent: string, refreshWebTorrent = false) { + if (refreshWebTorrent === true) webtorrent = new WebTorrent() + + return new Promise(res => webtorrent.add(torrent, res)) +} + +// --------------------------------------------------------------------------- + +export { + readFilePromise, + readdirPromise, + dateIsValid, + wait, + webtorrentAdd +} -- cgit v1.2.3