aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/miscs
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2017-12-28 13:59:22 +0100
committerChocobozzz <me@florianbigard.com>2017-12-28 13:59:22 +0100
commitc5d31dba56d669c0df0209761c43c5a6ac7cec4a (patch)
treefe72b56a1c0e7beb6e092c393a00ddfe93a5d71f /server/tests/utils/miscs
parentdb799da3d2b2ea465165df78ff71effa653b6309 (diff)
downloadPeerTube-c5d31dba56d669c0df0209761c43c5a6ac7cec4a.tar.gz
PeerTube-c5d31dba56d669c0df0209761c43c5a6ac7cec4a.tar.zst
PeerTube-c5d31dba56d669c0df0209761c43c5a6ac7cec4a.zip
Tests directories refractor
Diffstat (limited to 'server/tests/utils/miscs')
-rw-r--r--server/tests/utils/miscs/miscs.ts52
1 files changed, 52 insertions, 0 deletions
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 @@
1import * as WebTorrent from 'webtorrent'
2import { readFile, readdir } from 'fs'
3
4let webtorrent = new WebTorrent()
5
6function readFilePromise (path: string) {
7 return new Promise<Buffer>((res, rej) => {
8 readFile(path, (err, data) => {
9 if (err) return rej(err)
10
11 return res(data)
12 })
13 })
14}
15
16function readdirPromise (path: string) {
17 return new Promise<string[]>((res, rej) => {
18 readdir(path, (err, files) => {
19 if (err) return rej(err)
20
21 return res(files)
22 })
23 })
24}
25
26 // Default interval -> 2 minutes
27function dateIsValid (dateString: string, interval = 120000) {
28 const dateToCheck = new Date(dateString)
29 const now = new Date()
30
31 return Math.abs(now.getTime() - dateToCheck.getTime()) <= interval
32}
33
34function wait (milliseconds: number) {
35 return new Promise(resolve => setTimeout(resolve, milliseconds))
36}
37
38function webtorrentAdd (torrent: string, refreshWebTorrent = false) {
39 if (refreshWebTorrent === true) webtorrent = new WebTorrent()
40
41 return new Promise<WebTorrent.Torrent>(res => webtorrent.add(torrent, res))
42}
43
44// ---------------------------------------------------------------------------
45
46export {
47 readFilePromise,
48 readdirPromise,
49 dateIsValid,
50 wait,
51 webtorrentAdd
52}