-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
181 lines (155 loc) · 4.45 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
var test = require('tape');
var net = require('net');
var SmartViewClient = require('.');
test('block splitting', function (t) {
var client = new SmartViewClient();
var blocks = [];
client._parseBlock = block => blocks.push(block);
client._onData('A\n\n');
t.deepEqual(blocks, ['A']);
client._onData('B\nC\n');
t.deepEqual(blocks, ['A']);
client._onData('\n');
t.deepEqual(blocks, ['A', 'B\nC']);
client._onData('D\n\nE\n\nF');
t.deepEqual(blocks, ['A', 'B\nC', 'D', 'E']);
client._onData('G\n\n');
t.deepEqual(blocks, ['A', 'B\nC', 'D', 'E', 'FG']);
t.end();
});
test('block parsing', function (t) {
var client = new SmartViewClient();
var ackReceived = false;
client._timeouts.push(null);
client._callbacks.push(() => ackReceived = true);
client._parseBlock([
'MONITOR A:',
'Brightness: 255',
'Contrast: 127',
'Saturation: 126',
'Identify: false',
'Border: None',
'WidescreenSD: off',
'ScopeMode: ParadeRGB',
'AudioChannel: 0',
].join('\n'));
t.deepEqual(client.get('MONITOR A'), {
Brightness: '255',
Contrast: '127',
Saturation: '126',
Identify: 'false',
Border: 'None',
WidescreenSD: 'off',
ScopeMode: 'ParadeRGB',
AudioChannel: '0'
});
client._parseBlock([
'MONITOR A:',
'Brightness: 8',
'ScopeMode: ParadeYUV',
'AudioChannel: 0',
].join('\n'));
t.deepEqual(client.get('MONITOR A'), {
Brightness: '8',
Contrast: '127',
Saturation: '126',
Identify: 'false',
Border: 'None',
WidescreenSD: 'off',
ScopeMode: 'ParadeYUV',
AudioChannel: '0'
});
t.ok(!ackReceived);
client._parseBlock('ACK');
t.ok(ackReceived);
t.end();
});
test('with fake server', function (t) {
t.timeoutAfter(1000);
var server = net.createServer();
server.listen(9992, 'localhost', () => {
var client = new SmartViewClient();
client.connect('localhost');
t.equal(client.isConnected, false);
var clientOnConnect = false;
client.on('connect', () => clientOnConnect = true);
server.once('connection', connection => {
t.equal(client.isConnected, true);
t.ok(clientOnConnect, 'connect event was emitted on client');
connection.setEncoding('utf8');
connection.write([
'MONITOR A:',
'Brightness: 8',
'ScopeMode: ParadeYUV',
'AudioChannel: 0',
'\n'
].join('\n'));
client.once('change', () => {
t.deepEqual(client.get('MONITOR A'), {
Brightness: '8',
ScopeMode: 'ParadeYUV',
AudioChannel: '0'
});
var ackSent = false;
client.set('MONITOR A', {Identify: 'true'}, function callback() {
t.ok(ackSent, 'server has sent ACK');
client.close();
connection.on('end', () => {
t.ok(!client.isConnected);
server.close();
t.end();
});
});
t.deepEqual(client.get('MONITOR A'), {
Brightness: '8',
ScopeMode: 'ParadeYUV',
AudioChannel: '0',
Identify: 'true'
});
connection.once('data', data => {
t.equal(data, 'MONITOR A:\nIdentify: true\n\n');
connection.write('ACK\n\n');
ackSent = true;
});
});
});
});
});
test('callbacks & reset', function (t) {
t.timeoutAfter(1000);
var server = net.createServer();
server.listen(9992, 'localhost', () => {
var client = new SmartViewClient();
client.connect('localhost');
server.once('connection', connection => {
connection.setEncoding('utf8');
client.timeout = 20;
client.set('MONITOR A', {Identify: 'true'}, () => t.fail('this callback is never called'));
client.on('error', () => {
t.ok(!client.isConnected);
client.connect('localhost');
server.once('connection', connection => {
connection.setEncoding('utf8');
t.deepEqual(client.get('MONITOR A'), {}, 'state was reset');
client.set('MONITOR A', {Contrast: '127'}, () => {
t.pass('callback is called');
client.close();
connection.end();
server.close();
t.end();
});
connection.once('data', data => {
connection.write('ACK\n\n');
});
});
});
});
});
});
test('error when not connected', function (t) {
var client = new SmartViewClient();
t.throws(() => {
client.set('MONITOR A', {Identify: 'true'});
});
t.end();
});