Skip to content

Commit

Permalink
Add more tests around config resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Oct 19, 2023
1 parent 482e652 commit ff6eb2c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 3 deletions.
50 changes: 47 additions & 3 deletions tests/config.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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')
})
79 changes: 79 additions & 0 deletions tests/testConfigs.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit ff6eb2c

Please sign in to comment.