diff --git a/lib/tests/config/bad_config.json b/lib/tests/config/bad_config.json new file mode 100644 index 00000000..97e9d71f --- /dev/null +++ b/lib/tests/config/bad_config.json @@ -0,0 +1,87 @@ +{ + "http": { + "params": { + "port": 3000 + }, + "triggers": [ + { + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "ws": { + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "osc": { + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "midi": { + "params": { + "virtualInputName": "showbridge Input", + "virtualOutputName": "showbridge Output" + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "udp": { + "params": { + "port": 8000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "tcp": { + "params": { + "port": 8000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "mqtt": { + "params": { + "broker": "", + "topics": [] + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "cloud": { + "params": { + "url": "", + "rooms": [] + } + } +} diff --git a/lib/tests/config/cloud_single_room_config.json b/lib/tests/config/cloud_single_room_config.json new file mode 100644 index 00000000..338b844a --- /dev/null +++ b/lib/tests/config/cloud_single_room_config.json @@ -0,0 +1,88 @@ +{ + "http": { + "params": { + "port": 3000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "ws": { + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "osc": { + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "midi": { + "params": { + "virtualInputName": "showbridge Input", + "virtualOutputName": "showbridge Output" + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "udp": { + "params": { + "port": 8000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "tcp": { + "params": { + "port": 8000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "mqtt": { + "params": { + "broker": "", + "topics": [] + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "cloud": { + "params": { + "url": "", + "room": "test" + } + } +} diff --git a/lib/tests/config/config.test.js b/lib/tests/config/config.test.js new file mode 100644 index 00000000..4d233796 --- /dev/null +++ b/lib/tests/config/config.test.js @@ -0,0 +1,63 @@ +import assert from 'node:assert'; +import { readFileSync } from 'node:fs'; +import path from 'node:path'; +import { describe, test } from 'node:test'; +import Config from '../../src/config.js'; +describe('Config', () => { + // TODO(jwetzell): find better way to load schema + + test('create', () => { + const schema = JSON.parse(readFileSync(path.join(import.meta.dirname, '../../../schema/config.schema.json'))); + const goodConfig = JSON.parse(readFileSync(path.join(import.meta.dirname, './good_config.json'))); + const config = new Config(goodConfig, schema); + + assert.notStrictEqual(config, undefined); + }); + + test('bad config', () => { + const schema = JSON.parse(readFileSync(path.join(import.meta.dirname, '../../../schema/config.schema.json'))); + const badConfig = JSON.parse(readFileSync(path.join(import.meta.dirname, './bad_config.json'))); + + assert.throws(() => { + const config = new Config(badConfig, schema); + }); + }); + + test('cloud room migration', () => { + const schema = JSON.parse(readFileSync(path.join(import.meta.dirname, '../../../schema/config.schema.json'))); + const cloudSingleRoomConfig = JSON.parse( + readFileSync(path.join(import.meta.dirname, './cloud_single_room_config.json')) + ); + + const config = new Config(cloudSingleRoomConfig, schema); + assert.deepStrictEqual(config.cloud.params.rooms, ['test']); + }); + + test('getTriggers', () => { + const schema = JSON.parse(readFileSync(path.join(import.meta.dirname, '../../../schema/config.schema.json'))); + const goodConfig = JSON.parse(readFileSync(path.join(import.meta.dirname, './good_config.json'))); + const config = new Config(goodConfig, schema); + + assert.notStrictEqual(config, undefined); + assert.strictEqual(config.getTriggers('http')[0].type, 'any'); + assert.strictEqual(config.getTriggers('udp')[0].type, 'any'); + assert.strictEqual(config.getTriggers('osc')[0], undefined); + }); + + test('toJSON', () => { + const schema = JSON.parse(readFileSync(path.join(import.meta.dirname, '../../../schema/config.schema.json'))); + const goodConfig = JSON.parse(readFileSync(path.join(import.meta.dirname, './good_config.json'))); + const config = new Config(goodConfig, schema); + + assert.notStrictEqual(config, undefined); + const toJSON = config.toJSON(); + console.log(toJSON); + assert.strictEqual(toJSON.http.triggers[0].comment, undefined); + assert.strictEqual(toJSON.http.triggers[0].type, 'any'); + assert.strictEqual(toJSON.osc.triggers[0], undefined); + assert.strictEqual(toJSON.ws.triggers[0].actions[0].type, 'log'); + assert.strictEqual(toJSON.ws.triggers[0].actions[0].comment, undefined); + assert.strictEqual(toJSON.ws.triggers[0].actions[0].transforms.length, 0); + assert.strictEqual(toJSON.$schema, undefined); + }); +}); diff --git a/lib/tests/config/good_config.json b/lib/tests/config/good_config.json new file mode 100644 index 00000000..19a5477f --- /dev/null +++ b/lib/tests/config/good_config.json @@ -0,0 +1,83 @@ +{ + "$schema": "./config.schema.json", + "http": { + "params": { + "port": 3000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "ws": { + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "osc": { + "triggers": [] + }, + "midi": { + "params": { + "virtualInputName": "showbridge Input", + "virtualOutputName": "showbridge Output" + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "udp": { + "params": { + "port": 8000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "tcp": { + "params": { + "port": 8000 + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "mqtt": { + "params": { + "broker": "", + "topics": [] + }, + "triggers": [ + { + "type": "any", + "enabled": true, + "actions": [{ "type": "log", "enabled": true }] + } + ] + }, + "cloud": { + "params": { + "url": "", + "rooms": [] + } + } +}