-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathtests.js
More file actions
453 lines (373 loc) · 17 KB
/
Copy pathtests.js
File metadata and controls
453 lines (373 loc) · 17 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* tests.js
*
* Unit tests for helpers.js and osc-feedback.js
*
* Framework: Mocha
* Assertions: Chai
* Stubs/Mocks: Sinon
* Require mocking: Proxyquire
*/
const { expect } = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire').noCallThru();
describe('helpers.js', () => {
describe('resolveHostname()', () => {
it('resolves IPv4 address and logs an info message', async () => {
// Description: When dns.lookup succeeds, resolveHostname should resolve with the address and log.
const dnsMock = {
lookup: sinon.stub().callsFake((hostname, opts, cb) => cb(null, '1.2.3.4', 4)),
};
const helpers = proxyquire('./helpers.js', {
dns: dnsMock,
});
const root = { log: sinon.stub() };
const result = await helpers.resolveHostname(root, 'example.com');
expect(result).to.equal('1.2.3.4');
expect(root.log.called).to.equal(true);
const [level, msg] = root.log.firstCall.args;
expect(level).to.equal('info');
expect(msg).to.include('Resolved example.com to 1.2.3.4');
});
it('rejects when dns.lookup fails', async () => {
// Description: When dns.lookup errors, resolveHostname should reject with the same error.
const err = new Error('DNS failure');
const dnsMock = {
lookup: sinon.stub().callsFake((hostname, opts, cb) => cb(err)),
};
const helpers = proxyquire('./helpers.js', {
dns: dnsMock,
});
const root = { log: sinon.stub() };
let caught;
try {
await helpers.resolveHostname(root, 'example.com');
} catch (e) {
caught = e;
}
expect(caught).to.equal(err);
expect(root.log.called).to.equal(false);
});
});
describe('isValidIPAddress()', () => {
it('returns true for a valid IPv4 address', () => {
// Description: net.isIP returns 4 for IPv4, which should map to true.
const helpers = require('./helpers.js');
expect(helpers.isValidIPAddress('192.168.1.10')).to.equal(true);
});
it('returns true for a valid IPv6 address', () => {
// Description: net.isIP returns 6 for IPv6, which should map to true.
const helpers = require('./helpers.js');
expect(helpers.isValidIPAddress('2001:db8::1')).to.equal(true);
});
it('returns false for an invalid IP string', () => {
// Description: net.isIP returns 0 for invalid input, which should map to false.
const helpers = require('./helpers.js');
expect(helpers.isValidIPAddress('not-an-ip')).to.equal(false);
});
});
describe('parseArguments()', () => {
const helpers = require('./helpers.js');
it('parses ints and floats correctly', () => {
// Description: Whole numbers become ints, decimals become floats.
const { args, error } = helpers.parseArguments('1 2 3.5 -7 -8.25 0');
expect(error).to.equal(undefined);
expect(args).to.deep.equal([1, 2, 3.5, -7, -8.25, 0]);
});
it('parses unquoted strings and strips quotes/apostrophes', () => {
// Description: Non-numeric tokens remain strings; quotes and apostrophes are removed.
const { args, error } = helpers.parseArguments('hello \'world\' "test"');
expect(error).to.equal(undefined);
expect(args).to.deep.equal(['hello', 'world', 'test']);
});
it('parses quoted strings with spaces as a single argument', () => {
// Description: A token starting with " should be combined until a closing " is found.
const { args, error } = helpers.parseArguments('"hello world" 123');
expect(error).to.equal(undefined);
expect(args).to.deep.equal(['hello world', 123]);
});
it('supports smart quotes by converting them to normal quotes', () => {
// Description: “ ” should be converted to " so quoted parsing works.
const { args, error } = helpers.parseArguments('“hello world” 5');
expect(error).to.equal(undefined);
expect(args).to.deep.equal(['hello world', 5]);
});
it('returns an error on unmatched quotes', () => {
// Description: If a quoted string never closes, parseArguments returns {error}.
const { args, error } = helpers.parseArguments('"hello world 123');
expect(args).to.equal(undefined);
expect(error).to.be.a('string');
expect(error).to.include('Unmatched quote');
});
it('ignores extra whitespace tokens', () => {
// Description: Multiple spaces should not create empty args.
const { args, error } = helpers.parseArguments('1 2 "a b" c');
expect(error).to.equal(undefined);
expect(args).to.deep.equal([1, 2, 'a b', 'c']);
});
});
describe('evaluateComparison()', () => {
const helpers = require('./helpers.js');
it('supports equal', () => {
// Description: Strict equality.
expect(helpers.evaluateComparison(5, 5, 'equal')).to.equal(true);
expect(helpers.evaluateComparison(5, 6, 'equal')).to.equal(false);
});
it('supports notequal', () => {
// Description: Strict inequality.
expect(helpers.evaluateComparison(5, 6, 'notequal')).to.equal(true);
expect(helpers.evaluateComparison(5, 5, 'notequal')).to.equal(false);
});
it('supports greaterthan / lessthan', () => {
// Description: Numeric comparisons.
expect(helpers.evaluateComparison(10, 5, 'greaterthan')).to.equal(true);
expect(helpers.evaluateComparison(1, 5, 'greaterthan')).to.equal(false);
expect(helpers.evaluateComparison(1, 5, 'lessthan')).to.equal(true);
expect(helpers.evaluateComparison(10, 5, 'lessthan')).to.equal(false);
});
it('supports greaterthanequal / lessthanequal', () => {
// Description: Inclusive numeric comparisons.
expect(helpers.evaluateComparison(5, 5, 'greaterthanequal')).to.equal(true);
expect(helpers.evaluateComparison(4, 5, 'greaterthanequal')).to.equal(false);
expect(helpers.evaluateComparison(5, 5, 'lessthanequal')).to.equal(true);
expect(helpers.evaluateComparison(6, 5, 'lessthanequal')).to.equal(false);
});
it('returns false for unknown comparisons', () => {
// Description: Default branch should be false.
expect(helpers.evaluateComparison(1, 1, 'doesnotexist')).to.equal(false);
});
});
describe('integer and range validation helpers', () => {
const helpers = require('./helpers.js');
describe('clampInt()', () => {
it('accepts numeric input, truncates decimals, and returns value within bounds', () => {
// Description: clampInt normalizes numeric input and enforces inclusive bounds.
expect(helpers.clampInt('5', 0, 10)).to.equal(5);
expect(helpers.clampInt(5.9, 0, 10)).to.equal(5);
expect(helpers.clampInt(-3.1, -10, 0)).to.equal(-3);
});
it('rejects non-numeric, infinite, or out-of-range values', () => {
// Description: clampInt returns null for invalid or out-of-range values.
expect(helpers.clampInt('nope', 0, 10)).to.equal(null);
expect(helpers.clampInt(Infinity, 0, 10)).to.equal(null);
expect(helpers.clampInt(-1, 0, 10)).to.equal(null);
expect(helpers.clampInt(11, 0, 10)).to.equal(null);
});
});
});
describe('hexadecimal parsing helpers (used for MIDI + blob handling)', () => {
const helpers = require('./helpers.js');
describe('parseHexByte()', () => {
it('parses a single hexadecimal byte with optional 0x prefix', () => {
// Description: Accepts 1–2 hex digits and optional 0x prefix.
expect(helpers.parseHexByte('A')).to.equal(0x0a);
expect(helpers.parseHexByte('0A')).to.equal(0x0a);
expect(helpers.parseHexByte('0x0a')).to.equal(0x0a);
expect(helpers.parseHexByte('ff')).to.equal(0xff);
});
it('rejects invalid hexadecimal byte representations', () => {
// Description: Rejects empty, oversized, or non-hex strings.
expect(helpers.parseHexByte('')).to.equal(null);
expect(helpers.parseHexByte('0x')).to.equal(null);
expect(helpers.parseHexByte('100')).to.equal(null);
expect(helpers.parseHexByte('GG')).to.equal(null);
expect(helpers.parseHexByte('0xGG')).to.equal(null);
});
});
describe('parseHexBytes()', () => {
it('parses multiple hex bytes into a Buffer when length matches', () => {
// Description: Returns a Buffer only when the number of bytes matches expectedLen.
const buf = helpers.parseHexBytes('00 90 45 65', 4);
expect(Buffer.isBuffer(buf)).to.equal(true);
expect(buf.equals(Buffer.from([0x00, 0x90, 0x45, 0x65]))).to.equal(true);
});
it('accepts comma-separated and irregularly spaced hex byte lists', () => {
// Description: Normalizes commas and whitespace before parsing.
const buf = helpers.parseHexBytes('00, 90, 45 65', 4);
expect(buf.equals(Buffer.from([0x00, 0x90, 0x45, 0x65]))).to.equal(true);
});
it('rejects input when byte count does not match expected length', () => {
// Description: Ensures exact byte length for fixed-size MIDI messages.
expect(helpers.parseHexBytes('00 90 45', 4)).to.equal(null);
expect(helpers.parseHexBytes('00 90 45 65 01', 4)).to.equal(null);
});
it('rejects input when any token is not a valid hex byte', () => {
// Description: Any invalid byte invalidates the entire sequence.
expect(helpers.parseHexBytes('00 90 GG 65', 4)).to.equal(null);
});
});
});
describe('MIDI message classification helpers', () => {
const helpers = require('./helpers.js');
describe('midiTypeFromStatus()', () => {
it('maps MIDI status byte high nibble to a semantic message type', () => {
// Description: Identifies MIDI message types independent of channel.
expect(helpers.midiTypeFromStatus(0x90)).to.equal('noteon');
expect(helpers.midiTypeFromStatus(0x80)).to.equal('noteoff');
expect(helpers.midiTypeFromStatus(0xb3)).to.equal('cc');
expect(helpers.midiTypeFromStatus(0xc0)).to.equal('program');
expect(helpers.midiTypeFromStatus(0xe0)).to.equal('pitchbend');
expect(helpers.midiTypeFromStatus(0xa0)).to.equal('polyaftertouch');
expect(helpers.midiTypeFromStatus(0xd0)).to.equal('channelpressure');
});
it('returns "unknown" for unsupported or system status bytes', () => {
// Description: System Common / System Realtime messages are not mapped here.
expect(helpers.midiTypeFromStatus(0x00)).to.equal('unknown');
expect(helpers.midiTypeFromStatus(0xf0)).to.equal('unknown');
});
});
});
describe('setupOSC()', () => {
it('creates an OSCUDPClient when protocol is udp', () => {
// setupOSC should instantiate OSCUDPClient with expected args.
const OSCUDPClientStub = sinon.stub();
const helpers = proxyquire('./helpers.js', {
'./osc-udp.js': OSCUDPClientStub,
'./osc-tcp.js': sinon.stub(),
'./osc-raw.js': sinon.stub(),
});
const instance = {
config: { protocol: 'udp', targetPort: 8000, feedbackPort: 8001, listen: true },
targetHost: '1.2.3.4',
updateStatus: sinon.stub(),
};
helpers.setupOSC(instance);
expect(OSCUDPClientStub.calledOnce).to.equal(true);
const [rootArg, hostArg, remotePortArg, localPortArg, listenArg] = OSCUDPClientStub.firstCall.args;
expect(rootArg).to.equal(instance);
expect(hostArg).to.equal('1.2.3.4');
expect(remotePortArg).to.equal(8000); // destination
expect(localPortArg).to.equal(8001); // bound local port when listening
expect(listenArg).to.equal(true);
expect(instance.client).to.be.ok;
expect(instance.updateStatus.called).to.equal(false);
});
it('creates an OSCTCPClient when protocol is tcp', () => {
// Description: setupOSC should instantiate OSCTCPClient with expected args.
const OSCTCPClientStub = sinon.stub();
const helpers = proxyquire('./helpers.js', {
'./osc-udp.js': sinon.stub(),
'./osc-tcp.js': OSCTCPClientStub,
'./osc-raw.js': sinon.stub(),
});
const instance = {
config: { protocol: 'tcp', targetPort: 10000, listen: false },
targetHost: 'example.local',
updateStatus: sinon.stub(),
};
helpers.setupOSC(instance);
expect(OSCTCPClientStub.calledOnce).to.equal(true);
const args = OSCTCPClientStub.firstCall.args;
expect(args[1]).to.equal('example.local');
expect(args[2]).to.equal(10000);
expect(args[3]).to.equal(false);
});
it('creates an OSCRawClient when protocol is tcp-raw', () => {
// Description: setupOSC should instantiate OSCRawClient with expected args.
const OSCRawClientStub = sinon.stub();
const helpers = proxyquire('./helpers.js', {
'./osc-udp.js': sinon.stub(),
'./osc-tcp.js': sinon.stub(),
'./osc-raw.js': OSCRawClientStub,
});
const instance = {
config: { protocol: 'tcp-raw', targetPort: 7777, listen: true },
targetHost: '10.0.0.1',
updateStatus: sinon.stub(),
};
helpers.setupOSC(instance);
expect(OSCRawClientStub.calledOnce).to.equal(true);
const args = OSCRawClientStub.firstCall.args;
expect(args[1]).to.equal('10.0.0.1');
expect(args[2]).to.equal(7777);
expect(args[3]).to.equal(true);
});
it('sets client null and marks bad_config for unknown protocol', () => {
// Description: For unknown protocol, setupOSC should set instance.client = null and call updateStatus("bad_config").
const helpers = require('./helpers.js');
const instance = {
config: { protocol: 'nope' },
targetHost: 'x',
updateStatus: sinon.stub(),
};
helpers.setupOSC(instance);
expect(instance.client).to.equal(null);
expect(instance.updateStatus.calledOnce).to.equal(true);
expect(instance.updateStatus.firstCall.args[0]).to.equal('bad_config');
});
});
});
describe('osc-feedback.js', () => {
describe('onDataHandler()', () => {
it('handles OSC bundle packets including int/float/string/blob/midi/bool', async () => {
// Description: Bundle elements should be stored in onDataReceived and trigger feedback/variable updates per element.
const oscMock = {
readPacket: sinon.stub(),
writePacket: sinon.stub(),
};
const blobBuf = Buffer.from([0x63, 0x61, 0x74, 0x21]); // "cat!"
const midiBuf = Buffer.from([0x00, 0x90, 0x45, 0x65]);
const bundle = {
packets: [
{ address: '/a', args: [{ type: 'i', value: 10 }] },
{ address: '/f', args: [{ type: 'f', value: 1.5 }] },
{ address: '/b', args: [{ type: 's', value: 'hi' }] },
// blob + midi
{ address: '/blob', args: [{ type: 'b', value: blobBuf }] },
{ address: '/midiMessage', args: [{ type: 'm', value: midiBuf }] },
// bools (depending on osc lib metadata: often T/F)
{ address: '/boolTrue', args: [{ type: 'T', value: true }] },
{ address: '/boolFalse', args: [{ type: 'F', value: false }] },
],
};
oscMock.readPacket.returns(bundle);
oscMock.writePacket.returns(Buffer.alloc(4));
const { onDataHandler } = proxyquire('./osc-feedback.js', { osc: oscMock });
const root = {
log: sinon.stub(),
onDataReceived: {},
checkFeedbacks: sinon.stub().resolves(),
setVariableValues: sinon.stub(),
};
await onDataHandler(root, Buffer.alloc(4));
// Existing types
expect(root.onDataReceived['/a']).to.deep.equal([{ type: 'i', value: 10 }]);
expect(root.onDataReceived['/f']).to.deep.equal([{ type: 'f', value: 1.5 }]);
expect(root.onDataReceived['/b']).to.deep.equal([{ type: 's', value: 'hi' }]);
// Blob: verify raw Buffer bytes
expect(root.onDataReceived['/blob']).to.have.lengthOf(1);
expect(root.onDataReceived['/blob'][0].type).to.equal('b');
expect(Buffer.isBuffer(root.onDataReceived['/blob'][0].value)).to.equal(true);
expect(root.onDataReceived['/blob'][0].value.equals(blobBuf)).to.equal(true);
// Midi: verify raw Buffer bytes
expect(root.onDataReceived['/midiMessage']).to.have.lengthOf(1);
expect(root.onDataReceived['/midiMessage'][0].type).to.equal('m');
expect(Buffer.isBuffer(root.onDataReceived['/midiMessage'][0].value)).to.equal(true);
expect(root.onDataReceived['/midiMessage'][0].value.equals(midiBuf)).to.equal(true);
// Bools
expect(root.onDataReceived['/boolTrue']).to.deep.equal([{ type: 'T', value: true }]);
expect(root.onDataReceived['/boolFalse']).to.deep.equal([{ type: 'F', value: false }]);
// Called per element
expect(root.checkFeedbacks.callCount).to.equal(7);
expect(root.setVariableValues.callCount).to.equal(7);
// Spot-check that latest_received_args uses the raw value list (buffers and bools included)
// Find the setVariableValues call corresponding to /blob
const blobCall = root.setVariableValues.getCalls().find((c) => c.args[0]?.latest_received_path === '/blob');
expect(blobCall).to.exist;
expect(blobCall.args[0].latest_received_args).to.have.lengthOf(1);
expect(Buffer.isBuffer(blobCall.args[0].latest_received_args[0])).to.equal(true);
expect(blobCall.args[0].latest_received_args[0].equals(blobBuf)).to.equal(true);
const midiCall = root.setVariableValues
.getCalls()
.find((c) => c.args[0]?.latest_received_path === '/midiMessage');
expect(midiCall).to.exist;
expect(Buffer.isBuffer(midiCall.args[0].latest_received_args[0])).to.equal(true);
expect(midiCall.args[0].latest_received_args[0].equals(midiBuf)).to.equal(true);
const trueCall = root.setVariableValues.getCalls().find((c) => c.args[0]?.latest_received_path === '/boolTrue');
expect(trueCall).to.exist;
expect(trueCall.args[0].latest_received_args).to.deep.equal([true]);
const falseCall = root.setVariableValues.getCalls().find((c) => c.args[0]?.latest_received_path === '/boolFalse');
expect(falseCall).to.exist;
expect(falseCall.args[0].latest_received_args).to.deep.equal([false]);
});
});
});