mirror of
https://github.com/jwetzell/osc-js.git
synced 2026-08-24 00:09:01 +00:00
bring in sendosc and convert to npm workspaces
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
const { deepEqual } = require('assert');
|
||||
const { describe, it } = require('node:test');
|
||||
const osc = require('../dist/index');
|
||||
|
||||
const tests = [
|
||||
{
|
||||
description: 'simple contents single message',
|
||||
expected: {
|
||||
timeTag: [32, 0],
|
||||
contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }],
|
||||
},
|
||||
bundle: Buffer.concat([
|
||||
Buffer.from('#bundle', 'ascii'),
|
||||
Buffer.from([0x00]),
|
||||
Buffer.from([0, 0, 0, 32, 0, 0, 0, 0]),
|
||||
Buffer.from('00000020', 'hex'),
|
||||
Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
|
||||
]),
|
||||
},
|
||||
];
|
||||
|
||||
describe('OSC Bundle Decoding', () => {
|
||||
tests.forEach((bundleTest) => {
|
||||
it(bundleTest.description, () => {
|
||||
const encoded = osc.bundleFromBuffer(bundleTest.bundle);
|
||||
deepEqual(encoded, bundleTest.expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
const { deepEqual, throws } = require('assert');
|
||||
const { describe, it } = require('node:test');
|
||||
const osc = require('../dist/index');
|
||||
|
||||
const tests = [
|
||||
{
|
||||
description: 'simple contents single message',
|
||||
bundle: { timeTag: [32, 0], contents: [{ address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] }] },
|
||||
expected: Buffer.concat([
|
||||
Buffer.from('#bundle', 'ascii'),
|
||||
Buffer.from([0x00]),
|
||||
Buffer.from([0, 0, 0, 32, 0, 0, 0, 0]),
|
||||
Buffer.from('00000020', 'hex'),
|
||||
Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
|
||||
]),
|
||||
},
|
||||
];
|
||||
|
||||
describe('OSC Bundle Encoding', () => {
|
||||
tests.forEach((bundleTest) => {
|
||||
it(bundleTest.description, () => {
|
||||
const encoded = osc.bundleToBuffer(bundleTest.bundle);
|
||||
deepEqual(encoded, bundleTest.expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
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 }] },
|
||||
},
|
||||
{
|
||||
description: 'osc 1.0 spec example 1',
|
||||
bytes: Buffer.from('2f6f7363696c6c61746f722f342f6672657175656e6379002c66000043dc0000', 'hex'),
|
||||
expected: { address: '/oscillator/4/frequency', args: [{ type: 'f', value: 440 }] },
|
||||
},
|
||||
{
|
||||
description: 'osc 1.0 spec example 2',
|
||||
bytes: Buffer.from('2f666f6f000000002c69697366660000000003e8ffffffff68656c6c6f0000003f9df3b640b5b22d', 'hex'),
|
||||
expected: {
|
||||
address: '/foo',
|
||||
args: [
|
||||
{ type: 'i', value: 1000 },
|
||||
{ type: 'i', value: -1 },
|
||||
{ type: 's', value: 'hello' },
|
||||
// thanks IEEE 754
|
||||
{ type: 'f', value: 1.2339999675750732421875 },
|
||||
{ type: 'f', value: 5.677999973297119140625 },
|
||||
],
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
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/ }
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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$/ }
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user