From ece1d988f52888351fe237f4a70aad0c06ab1cac Mon Sep 17 00:00:00 2001 From: Joel Wetzell Date: Mon, 7 Oct 2024 12:59:31 -0500 Subject: [PATCH] add basic tests --- tests/message-decode.test.js | 104 +++++++++++++++++++++++++++++++++++ tests/message-encode.test.js | 94 +++++++++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 tests/message-decode.test.js create mode 100644 tests/message-encode.test.js diff --git a/tests/message-decode.test.js b/tests/message-decode.test.js new file mode 100644 index 0000000..f1910fb --- /dev/null +++ b/tests/message-decode.test.js @@ -0,0 +1,104 @@ +const { deepEqual, throws } = require('assert'); +const { describe, it } = require('node:test'); +const osc = require('../dist/index'); + +const tests = [ + { + description: 'simple address no args', + bytes: Buffer.from('2f68656c6c6f00002c000000', 'hex'), + expected: { address: '/hello', args: [] }, + }, + { + description: 'simple address string arg', + bytes: Buffer.from('2f68656c6c6f00002c7300006172673100000000', 'hex'), + expected: { address: '/hello', args: [{ type: 's', value: 'arg1' }] }, + }, + { + description: 'simple address integer arg', + bytes: Buffer.from('2f68656c6c6f00002c69000000000023', 'hex'), + expected: { address: '/hello', args: [{ type: 'i', value: 35 }] }, + }, + { + description: 'simple address float arg', + bytes: Buffer.from('2f68656c6c6f00002c660000420a0000', 'hex'), + expected: { address: '/hello', args: [{ type: 'f', value: 34.5 }] }, + }, + { + description: 'simple address blob arg', + bytes: Buffer.from('2f68656c6c6f00002c62000000000004626c6f62', 'hex'), + expected: { address: '/hello', args: [{ type: 'b', value: Buffer.from('blob') }] }, + }, + { + description: 'simple address True arg', + bytes: Buffer.from('2f68656c6c6f00002c540000', 'hex'), + expected: { address: '/hello', args: [{ type: 'T', value: true }] }, + }, + { + description: 'simple address False arg', + bytes: Buffer.from('2f68656c6c6f00002c460000', 'hex'), + expected: { address: '/hello', args: [{ type: 'F', value: false }] }, + }, +]; + +describe('OSC Message Decoding', () => { + tests.forEach((messageTest) => { + it(messageTest.description, () => { + const decoded = osc.messageFromBuffer(messageTest.bytes); + deepEqual(decoded, messageTest.expected); + }); + }); + + it('bad address', () => { + throws( + () => { + osc.messageFromBuffer(Buffer.from('68656c6c6f00002c660000420a0000', 'hex')); + }, + { name: /^Error$/, message:/must start with/ } + ); + }); + + it('bad type string', () => { + throws( + () => { + osc.messageFromBuffer(Buffer.from('2f68656c6c6f000066000000420a00', 'hex')); + }, + { name: /^Error$/, message:/type string must start with/ } + ); + }); + + it('unknown type', () => { + throws( + () => { + osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c7a0000420a0000', 'hex')); + }, + { name: /^Error$/, message:/unknown/ } + ); + }); + + it('float arg missing bytes', () => { + throws( + () => { + osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c660000420a00', 'hex')); + }, + { name: /^Error$/, message:/not enough bytes/ } + ); + }); + + it('int arg missing bytes', () => { + throws( + () => { + osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c690000000000', 'hex')); + }, + { name: /^Error$/, message:/not enough bytes/ } + ); + }); + + it('blob bytes too small', () => { + throws( + () => { + osc.messageFromBuffer(Buffer.from('2f68656c6c6f00002c62000000000004626c6f', 'hex')); + }, + { name: /^Error$/, message:/not enough bytes/ } + ); + }); +}); diff --git a/tests/message-encode.test.js b/tests/message-encode.test.js new file mode 100644 index 0000000..70efb19 --- /dev/null +++ b/tests/message-encode.test.js @@ -0,0 +1,94 @@ +const { deepEqual, throws } = require('assert'); +const { describe, it } = require('node:test'); +const osc = require('../dist/index'); + +const tests = [ + { + description: 'simple address no args', + message: { address: '/hello', args: [] }, + expected: Buffer.from('2f68656c6c6f00002c000000', 'hex'), + }, + { + description: 'simple address string arg', + message: { address: '/hello', args: [{ type: 's', value: 'arg1' }] }, + expected: Buffer.from('2f68656c6c6f00002c7300006172673100000000', 'hex'), + }, + { + description: 'simple address integer arg', + message: { address: '/hello', args: [{ type: 'i', value: 35 }] }, + expected: Buffer.from('2f68656c6c6f00002c69000000000023', 'hex'), + }, + { + description: 'simple address float arg', + message: { address: '/hello', args: [{ type: 'f', value: 34.5 }] }, + expected: Buffer.from('2f68656c6c6f00002c660000420a0000', 'hex'), + }, + { + description: 'simple address blob arg', + message: { address: '/hello', args: [{ type: 'b', value: Buffer.from('blob') }] }, + expected: Buffer.from('2f68656c6c6f00002c62000000000004626c6f62', 'hex'), + }, + { + description: 'simple address True arg', + message: { address: '/hello', args: [{ type: 'T', value: true }] }, + expected: Buffer.from('2f68656c6c6f00002c540000', 'hex'), + }, + { + description: 'simple address False arg', + message: { address: '/hello', args: [{ type: 'F', value: false }] }, + expected: Buffer.from('2f68656c6c6f00002c460000', 'hex'), + }, +]; + +describe('OSC Message Encoding', () => { + tests.forEach((messageTest) => { + it(messageTest.description, () => { + const encoded = osc.messageToBuffer(messageTest.message); + deepEqual(encoded, messageTest.expected); + }); + }); + it('bad string arg', () => { + throws( + () => { + osc.messageToBuffer({ address: '/address', args: [{ type: 's', value: 123 }] }); + }, + { name: /^TypeError$/ } + ); + }); + + it('bad integer arg', () => { + throws( + () => { + osc.messageToBuffer({ address: '/address', args: [{ type: 'i', value: 'hi' }] }); + }, + { name: /^TypeError$/ } + ); + }); + + it('bad float arg', () => { + throws( + () => { + osc.messageToBuffer({ address: '/address', args: [{ type: 'f', value: 'hi' }] }); + }, + { name: /^TypeError$/ } + ); + }); + + it('bad blob arg', () => { + throws( + () => { + osc.messageToBuffer({ address: '/address', args: [{ type: 'b', value: 123 }] }); + }, + { name: /^TypeError$/ } + ); + }); + + it('unknown arg type', () => { + throws( + () => { + osc.messageToBuffer({ address: '/address', args: [{ type: 'z', value: 123 }] }); + }, + { name: /^TypeError$/ } + ); + }); +});