This repository was archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathkodi.test.js
141 lines (108 loc) · 2.81 KB
/
kodi.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const test = require('ava')
const sinon = require('sinon')
const kodi = require('./kodi')
// SETUP BEFORE TESTS
test.cb.beforeEach(t => {
kodi.setOptions({
host: 'pi',
port: 9090,
reconnect: false,
reconnectSleep: 3000,
connectionTimeout: 10000,
sendTimeout: 3000
})
t.deepEqual(kodi.eventNames(), [])
kodi.on('error', console.log)
t.end()
})
test.cb.afterEach(t => {
kodi.close({ reconnect: false })
setTimeout(() => {
kodi.removeAllListeners()
t.end()
}, 1000)
})
// TESTS
test.serial.cb('connection to kodi', t => {
t.plan(1)
const connectFunc = sinon.spy()
kodi.on('connect', connectFunc)
kodi.connect()
setTimeout(() => {
t.is(connectFunc.callCount, 1)
t.end()
}, 3000)
})
test.serial.cb('connection timeout', t => {
t.plan(1)
kodi.on('error', error => {
t.is(error.message, 'Was not able to connect before reaching timeout.')
t.end()
})
kodi.connect({ connectionTimeout: 10 })
})
test.serial.cb('sending simple commands', t => {
t.plan(3)
kodi.connect()
kodi.on('connect', async () => {
const promises = [
kodi.send('Input.Up'),
kodi.send('Input.Down')
]
const results = await Promise.all(promises)
t.is(results.length, 2)
t.is(results[0], 'OK')
t.is(results[1], 'OK')
t.end()
})
})
// Doesn't work on fast networks - not sure if it's possible to test this..
test.skip.serial.cb('receive response for timed out message', t => {
t.plan(1)
kodi.connect({ sendTimeout: 1 })
kodi.on('connect', async () => {
const error = await t.throws(kodi.send('Input.Up'))
t.is(error, 'Failed to send message. No response within timeout.')
t.end()
})
})
test.serial.cb('sending advanced commands', t => {
t.plan(2)
kodi.connect()
kodi.on('connect', async () => {
const result1 = await kodi.send(
'Application.GetProperties',
{ properties: [ 'volume' ] }
)
function isNumeric (n) {
return !isNaN(parseFloat(n)) && isFinite(n)
}
t.true(isNumeric(result1.volume))
const result2 = await kodi.send(
'Application.SetVolume',
{ volume: result1.volume }
)
t.is(result1.volume, result2)
t.end()
})
})
test.serial('sending while not connected', async t => {
t.plan(2)
const error = await t.throws(kodi.send('Input.Up'))
t.is(error.message, 'Failed to send message. No connection to kodi.')
})
test.serial.cb('receives notifications', t => {
t.plan(3)
kodi.connect()
kodi.on('notification', ({ method, params }) => {
t.is(method, 'Application.OnVolumeChanged')
t.is(params.data.volume, 99)
})
kodi.on('Application.OnVolumeChanged', (params) => {
t.is(params.data.volume, 99)
})
kodi.on('connect', async () => {
kodi.send('Application.SetVolume', { volume: 99 })
})
setTimeout(() => t.end(), 2000)
})