Skip to content

Commit dc5e1bd

Browse files
committed
wip: fix tests
1 parent df50d96 commit dc5e1bd

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

Diff for: src/internal/__tests__/upgrade.spec.ts

+48-36
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
CompanionStaticUpgradeScript,
88
} from '../../module-api/upgrade.js'
99
import { runThroughUpgradeScripts } from '../upgrade.js'
10-
import { ActionInstance } from '../../host-api/api.js'
10+
import { ActionInstance, UpgradeActionInstance } from '../../host-api/api.js'
1111

1212
type MockUpgradeScript<TConfig> = Mock<CompanionStaticUpgradeScript<TConfig>>
1313

@@ -32,18 +32,18 @@ const createMockScripts = <TConfig>(count: number): MockUpgradeScript<TConfig>[]
3232
return result
3333
}
3434

35-
function makeActionsInput(...actions: ActionInstance[]): { [id: string]: ActionInstance } {
36-
const res: { [id: string]: ActionInstance } = {}
35+
function makeActionsInput(...actions: UpgradeActionInstance[]): UpgradeActionInstance[] {
36+
const res: UpgradeActionInstance[] = []
3737

3838
for (const action of actions) {
39-
if (res[action.id]) throw new Error(`Duplicate id "${action.id}"`)
40-
res[action.id] = clone(action)
39+
// if (res[action.id]) throw new Error(`Duplicate id "${action.id}"`)
40+
res.push(clone(action))
4141
}
4242

4343
return res
4444
}
4545

46-
function stripActionInstance(action: ActionInstance): CompanionMigrationAction {
46+
function stripActionInstance(action: UpgradeActionInstance): CompanionMigrationAction {
4747
return {
4848
id: action.id,
4949
controlId: action.controlId,
@@ -61,13 +61,13 @@ describe('runThroughUpgradeScripts', () => {
6161

6262
it('nothing to upgrade', () => {
6363
const scripts = createMockScripts(2)
64-
const result = runThroughUpgradeScripts({}, {}, null, scripts, {}, true)
64+
const result = runThroughUpgradeScripts([], [], null, scripts, {}, true)
6565

6666
// Check result looks sane
6767
expect(result).toBeTruthy()
6868
expect(result.updatedConfig).toBeUndefined()
69-
expect(result.updatedActions).toEqual({})
70-
expect(result.updatedFeedbacks).toEqual({})
69+
expect(result.updatedActions).toEqual([])
70+
expect(result.updatedFeedbacks).toEqual([])
7171

7272
// check scripts
7373
expect(scripts).toHaveLength(2)
@@ -81,13 +81,13 @@ describe('runThroughUpgradeScripts', () => {
8181
something: true,
8282
}
8383
const scripts = createMockScripts(2)
84-
const result = runThroughUpgradeScripts({}, {}, null, scripts, { ...configBefore }, false)
84+
const result = runThroughUpgradeScripts([], [], null, scripts, { ...configBefore }, false)
8585

8686
// Check result looks sane
8787
expect(result).toBeTruthy()
8888
expect(result.updatedConfig).toEqual(undefined)
89-
expect(result.updatedActions).toEqual({})
90-
expect(result.updatedFeedbacks).toEqual({})
89+
expect(result.updatedActions).toEqual([])
90+
expect(result.updatedFeedbacks).toEqual([])
9191

9292
// check scripts
9393
expect(scripts).toHaveLength(2)
@@ -112,15 +112,15 @@ describe('runThroughUpgradeScripts', () => {
112112
})
113113
})
114114

115-
const result = runThroughUpgradeScripts({}, {}, 0, scripts, { ...configBefore }, false)
115+
const result = runThroughUpgradeScripts([], [], 0, scripts, { ...configBefore }, false)
116116
// Check result looks sane
117117
expect(result).toBeTruthy()
118118
expect(result.updatedConfig).toEqual({
119119
...configBefore,
120120
added: 123,
121121
})
122-
expect(result.updatedActions).toEqual({})
123-
expect(result.updatedFeedbacks).toEqual({})
122+
expect(result.updatedActions).toEqual([])
123+
expect(result.updatedFeedbacks).toEqual([])
124124

125125
// check scripts
126126
expect(scripts).toHaveLength(2)
@@ -138,20 +138,26 @@ describe('runThroughUpgradeScripts', () => {
138138
})
139139

140140
it('just the actions to upgrade, from v0', () => {
141-
const action0Before: ActionInstance = {
141+
const action0Before: UpgradeActionInstance = {
142142
id: 'act0',
143143
upgradeIndex: null,
144144
disabled: false,
145145
actionId: 'my-action',
146-
options: { a: 1, b: 2 },
146+
options: {
147+
a: { value: 1, isExpression: false },
148+
b: { value: 2, isExpression: false },
149+
},
147150
controlId: 'control0',
148151
}
149-
const action1Before: ActionInstance = {
152+
const action1Before: UpgradeActionInstance = {
150153
id: 'act1',
151154
upgradeIndex: null,
152155
disabled: false,
153156
actionId: 'my-action',
154-
options: { c: 1, d: 2 },
157+
options: {
158+
c: { value: 1, isExpression: false },
159+
d: { value: 2, isExpression: false },
160+
},
155161
controlId: 'control1',
156162
}
157163

@@ -175,18 +181,18 @@ describe('runThroughUpgradeScripts', () => {
175181
})
176182

177183
const actionsInput = makeActionsInput(action0Before, action1Before)
178-
const result = runThroughUpgradeScripts(actionsInput, {}, 0, scripts, {}, true)
184+
const result = runThroughUpgradeScripts(actionsInput, [], 0, scripts, {}, true)
179185

180186
// Check result looks sane
181187
expect(result).toBeTruthy()
182188
expect(result.updatedConfig).toBeUndefined()
183-
expect(result.updatedActions).toEqual({
184-
[action0Before.id]: {
189+
expect(result.updatedActions).toEqual([
190+
{
185191
...action0Before,
186192
actionId: 'new-action',
187193
},
188-
})
189-
expect(result.updatedFeedbacks).toEqual({})
194+
])
195+
expect(result.updatedFeedbacks).toEqual([])
190196

191197
// check scripts
192198
expect(scripts).toHaveLength(2)
@@ -195,25 +201,31 @@ describe('runThroughUpgradeScripts', () => {
195201

196202
// Check input was mutated in place
197203
const expectedInput = makeActionsInput(action0Before, action1Before)
198-
expectedInput[action0Before.id].actionId = 'new-action'
204+
expectedInput[0].actionId = 'new-action'
199205
expect(actionsInput).toEqual(expectedInput)
200206
})
201207

202208
it('an actions to upgrade, from earlier than the rest', () => {
203-
const action0Before: ActionInstance = {
209+
const action0Before: UpgradeActionInstance = {
204210
id: 'act0',
205211
upgradeIndex: null,
206212
disabled: false,
207213
actionId: 'my-action',
208-
options: { a: 1, b: 2 },
214+
options: {
215+
a: { value: 1, isExpression: false },
216+
b: { value: 2, isExpression: false },
217+
},
209218
controlId: 'control0',
210219
}
211-
const action1Before: ActionInstance = {
220+
const action1Before: UpgradeActionInstance = {
212221
id: 'act1',
213222
upgradeIndex: -1,
214223
disabled: false,
215224
actionId: 'my-action',
216-
options: { c: 1, d: 2 },
225+
options: {
226+
c: { value: 1, isExpression: false },
227+
d: { value: 2, isExpression: false },
228+
},
217229
controlId: 'control1',
218230
}
219231

@@ -236,22 +248,22 @@ describe('runThroughUpgradeScripts', () => {
236248
})
237249

238250
const actionsInput = makeActionsInput(action0Before, action1Before)
239-
const result = runThroughUpgradeScripts(actionsInput, {}, 0, scripts, {}, true)
251+
const result = runThroughUpgradeScripts(actionsInput, [], 0, scripts, {}, true)
240252

241253
// Check result looks sane
242254
expect(result).toBeTruthy()
243255
expect(result.updatedConfig).toBeUndefined()
244-
expect(result.updatedActions).toEqual({
245-
[action0Before.id]: {
256+
expect(result.updatedActions).toEqual([
257+
{
246258
...action0Before,
247259
actionId: 'new-action',
248260
},
249-
[action1Before.id]: {
261+
{
250262
// Reported to confirm the upgrade
251263
...action1Before,
252264
},
253-
})
254-
expect(result.updatedFeedbacks).toEqual({})
265+
])
266+
expect(result.updatedFeedbacks).toEqual([])
255267

256268
// check scripts
257269
expect(scripts).toHaveLength(2)
@@ -261,7 +273,7 @@ describe('runThroughUpgradeScripts', () => {
261273

262274
// Check input was mutated in place
263275
const expectedInput = makeActionsInput(action0Before, action1Before)
264-
expectedInput[action0Before.id].actionId = 'new-action'
276+
expectedInput[0].actionId = 'new-action'
265277
expect(actionsInput).toEqual(expectedInput)
266278
})
267279
})

0 commit comments

Comments
 (0)