-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
84 lines (65 loc) · 2.15 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const os = require('os')
const fs = require('fs')
const path = require('node:path')
const assert = require('node:assert/strict')
const { describe, before, beforeEach, afterEach, it } = require('node:test')
const Transmission = require('./index')
const tmpDir = path.join(os.tmpdir(), 'transmission-native')
const APP_NAME = 'transmission'
describe('transmission-native tests', async () => {
let tr
before(() => removeTmpDir()) // In case tmp folder already exist)
beforeEach(() => {
tr = new Transmission(tmpDir, APP_NAME)
})
afterEach(() => {
tr.close()
removeTmpDir()
})
it('request should succeed with callback', () => {
return new Promise((resolve, reject) => {
const req = { method: 'session-get' }
tr.request(req, (err, json) => {
if (err) return reject(err)
assert.equal(json.result, 'success')
resolve()
})
})
})
it('request should succeed with promise', async () => {
const req = { method: 'session-get' }
const json = await tr.request(req)
assert.equal(json.result, 'success')
})
it('request should return error', async () => {
const req = { method: 'unknown' }
const res = await tr.request(req)
assert.equal(res.result, 'method name not recognized')
})
it('one async request', async () => {
const req = { method: 'port-test' }
const json = await tr.request(req)
assert.equal(json.result, 'success')
})
it('two async requests', async () => {
const req = { method: 'port-test' }
const promises = [tr.request(req), tr.request(req)]
const results = await Promise.all(promises)
assert.equal(results[0].result, 'success')
assert.equal(results[1].result, 'success')
})
it('settings should be saved', async () => {
let exists = fs.existsSync(path.join(tmpDir, 'settings.json'))
assert.equal(exists, false)
tr.saveSettings()
// Settings.json should now be saved
exists = fs.existsSync(path.join(tmpDir, 'settings.json'))
assert.equal(exists, true)
})
})
const removeTmpDir = () => {
if (fs.existsSync(tmpDir)) {
console.log('Removing tmp folder...')
fs.rmSync(tmpDir, { recursive: true })
}
}