From ff6eb2c608c5bb921a1fe929cbce6200a92ce93d Mon Sep 17 00:00:00 2001 From: Sam Stenvall Date: Thu, 19 Oct 2023 13:12:10 +0300 Subject: [PATCH] Add more tests around config resolving --- tests/config.test.ts | 50 ++++++++++++++++++++++++++-- tests/testConfigs.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 tests/testConfigs.ts diff --git a/tests/config.test.ts b/tests/config.test.ts index 07cd7a4..cd675bb 100644 --- a/tests/config.test.ts +++ b/tests/config.test.ts @@ -1,7 +1,7 @@ -import { resolveAndValidateConfig } from '../src/config' -import { SensorType, ShellySensor, ShellyType } from '../src/sensor' -import { Config } from '../src/config' +import { Config, resolveAndValidateConfig } from '../src/config' +import { SensorType, ShellySensor, ShellyType, UnmeteredSensor, VirtualSensor } from '../src/sensor' import { CircuitType } from '../src/circuit' +import { createParentChildConfig, createUnmeteredParentChildrenConfig, createVirtualSensorConfig } from './testConfigs' test('defaults are applied', () => { const config = resolveAndValidateConfig({ @@ -25,3 +25,47 @@ test('defaults are applied', () => { const sensor = config.circuits[0].sensor as ShellySensor expect(sensor.shelly.type).toEqual(ShellyType.Gen1) }) + +test('parent and child circuit is resolved', () => { + const config = resolveAndValidateConfig(createParentChildConfig()) + + expect(config.circuits[1].parent).toEqual(config.circuits[0]) + expect(config.circuits[0].children).toEqual([config.circuits[1]]) +}) + +test('throws error when parent circuit cannot be resolved', () => { + const rawConfig = createParentChildConfig() + rawConfig.circuits[1].parent = 'Some unknown parent' + + expect(() => resolveAndValidateConfig(rawConfig)).toThrow('Failed to resolve circuit') +}) + +test('virtual sensor children are resolved', () => { + const config = resolveAndValidateConfig(createVirtualSensorConfig()) + + const virtualSensor = config.circuits[1].sensor as VirtualSensor + expect(virtualSensor.virtual.children).toEqual([config.circuits[0]]) +}) + +test('unmetered sensor parent and children are resolved', () => { + const rawConfig = createUnmeteredParentChildrenConfig() + const config = resolveAndValidateConfig(rawConfig) + + const sensor = config.circuits[3].sensor as UnmeteredSensor + expect(sensor.unmetered.parent).toEqual(config.circuits[0]) + expect(sensor.unmetered.children).toEqual([config.circuits[1], config.circuits[2]]) +}) + +test('throws when unmetered sensor parent or children cannot be resolved', () => { + // Unknown parent + const unknownParentConfig = createUnmeteredParentChildrenConfig() + unknownParentConfig.circuits[0].name = 'Unknown parent' + + expect(() => resolveAndValidateConfig(unknownParentConfig)).toThrow('Failed to resolve') + + // Unknown child + const unknownChildConfig = createUnmeteredParentChildrenConfig() + unknownChildConfig.circuits[2].name = 'Unknown child' + + expect(() => resolveAndValidateConfig(unknownChildConfig)).toThrow('Failed to resolve') +}) diff --git a/tests/testConfigs.ts b/tests/testConfigs.ts new file mode 100644 index 0000000..9a684a2 --- /dev/null +++ b/tests/testConfigs.ts @@ -0,0 +1,79 @@ +import { SensorType } from '../src/sensor' +import { Config } from '../src/config' + +export const createParentChildConfig = (): Config => { + return { + circuits: [ + { + name: 'The parent', + sensor: { + type: SensorType.Dummy, + }, + }, + { + name: 'The child', + parent: 'The parent', + sensor: { + type: SensorType.Dummy, + }, + }, + ], + } as unknown as Config +} + +export const createVirtualSensorConfig = (): Config => { + return { + circuits: [ + { + name: 'Some circuit', + sensor: { + type: SensorType.Dummy, + }, + }, + { + name: 'Some virtual circuit', + sensor: { + type: SensorType.Virtual, + virtual: { + children: ['Some circuit'], + }, + }, + }, + ], + } as unknown as Config +} + +export const createUnmeteredParentChildrenConfig = (): Config => { + return { + circuits: [ + { + name: 'Total', + sensor: { + type: SensorType.Dummy, + }, + }, + { + name: 'Some sub-circuit', + sensor: { + type: SensorType.Dummy, + }, + }, + { + name: 'Some other sub-circuit', + sensor: { + type: SensorType.Dummy, + }, + }, + { + name: 'Unmetered', + sensor: { + type: SensorType.Unmetered, + unmetered: { + parent: 'Total', + children: ['Some sub-circuit', 'Some other sub-circuit'], + }, + }, + }, + ], + } as unknown as Config +}