Skip to content

Commit

Permalink
fix: abstract device to handle undefined old state
Browse files Browse the repository at this point in the history
  • Loading branch information
mint-dewit committed Oct 10, 2023
1 parent 8ae3d54 commit 5f656bd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe('Abstract device', () => {
const LAYERNAME = 'myLayer0'

async function compareStates(
oldDevState: AbstractDeviceState,
oldDevState: AbstractDeviceState | undefined,
newDevState: AbstractDeviceState,
expCommands: AbstractCommandWithContext[]
) {
Expand All @@ -54,6 +54,18 @@ describe('Abstract device', () => {
expect(commands).toEqual(expCommands)
}

test('From undefined', async () => {
await compareStates(
undefined,
{
time: 20,
nextEvents: [],
layers: {},
},
[]
)
})

test('Empty states', async () => {
await compareStates(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ export class AbstractDevice extends Device<AbstractOptions, AbstractDeviceState,
* @param oldAbstractState
* @param newAbstractState
*/
diffStates(oldAbstractState: AbstractDeviceState, newAbstractState: AbstractDeviceState) {
diffStates(oldAbstractState: AbstractDeviceState | undefined, newAbstractState: AbstractDeviceState) {
// in this abstract class, let's just cheat:

const commands: Array<AbstractCommandWithContext> = []

for (const [layerKey, newLayer] of Object.entries<Timeline.ResolvedTimelineObjectInstance<any>>(
newAbstractState.layers
)) {
const oldLayer = oldAbstractState.layers[layerKey]
const oldLayer = oldAbstractState?.layers[layerKey]
if (!oldLayer) {
// added!
commands.push({
Expand All @@ -102,7 +102,7 @@ export class AbstractDevice extends Device<AbstractOptions, AbstractDeviceState,

// removed
for (const [layerKey, oldLayer] of Object.entries<Timeline.ResolvedTimelineObjectInstance<any>>(
oldAbstractState.layers
oldAbstractState?.layers || {}
)) {
const newLayer = newAbstractState.layers[layerKey]
if (!newLayer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('HTTP-Send', () => {

describe('diffState', () => {
async function compareStates(
oldDevState: HttpSendDeviceState,
oldDevState: HttpSendDeviceState | undefined,
newDevState: HttpSendDeviceState,
expCommands: HttpSendDeviceCommand[]
) {
Expand All @@ -63,6 +63,18 @@ describe('HTTP-Send', () => {
expect(commands).toEqual(expCommands)
}

test('From undefined', async () => {
await compareStates(
undefined,
{
time: 20,
nextEvents: [],
layers: {},
},
[]
)
})

test('empty states', async () => {
await compareStates(createTimelineState({}), createTimelineState({}), [])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('OSC Device', () => {

describe('diffState', () => {
async function compareStates(
oldDevState: OscDeviceState,
oldDevState: OscDeviceState | undefined,
newDevState: OscDeviceState,
expCommands: OscCommandWithContext[]
) {
Expand All @@ -178,6 +178,10 @@ describe('OSC Device', () => {
expect(commands).toEqual(expCommands)
}

test('From undefined', async () => {
await compareStates(undefined, {}, [])
})

test('Empty states', async () => {
await compareStates({}, {}, [])
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ describe('Shotoku Device', () => {

describe('diffState', () => {
async function compareStates(
oldDevState: ShotokuDeviceState,
oldDevState: ShotokuDeviceState | undefined,
newDevState: ShotokuDeviceState,
expCommands: ShotokuCommandWithContext[]
) {
Expand All @@ -138,6 +138,17 @@ describe('Shotoku Device', () => {
expect(commands).toEqual(expCommands)
}

test('From undefined', async () => {
await compareStates(
undefined,
{
shots: {},
sequences: {},
},
[]
)
})

test('Empty states', async () => {
await compareStates(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ describe('TCP-Send', () => {
})

describe('diffState', () => {
test('From undefined', async () => {
const device = await getInitializedTcpDevice()
const commands = device.diffStates(undefined, createTimelineState({}))
expect(commands).toEqual([])
await device.terminate()
})
test('Empty states', async () => {
const device = await getInitializedTcpDevice()
const commands = device.diffStates(createTimelineState({}), createTimelineState({}))
Expand Down

0 comments on commit 5f656bd

Please sign in to comment.