Skip to content

Commit adcd524

Browse files
justandrasJulusian
andcommitted
chore: refactor and update findForLayer tests
Co-authored-by: Julian Waller <[email protected]>
1 parent 0d9a4fa commit adcd524

File tree

10 files changed

+400
-256
lines changed

10 files changed

+400
-256
lines changed

packages/job-worker/src/playout/lookahead/__tests__/findForLayer.test.ts

Lines changed: 0 additions & 207 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { context, findLookaheadObjectsForPartMock } from './helpers/mockSetup.js'
2+
import { findLookaheadForLayer } from '../../findForLayer.js'
3+
import { expectInstancesToMatch } from '../utils.js'
4+
import { findForLayerTestConstants } from './constants.js'
5+
6+
const current = findForLayerTestConstants.current
7+
const nextFuture = findForLayerTestConstants.nextFuture
8+
const layer = findForLayerTestConstants.layer
9+
10+
describe('findLookaheadForLayer – basic behavior', () => {
11+
test('no parts', () => {
12+
const res = findLookaheadForLayer(context, {}, [], 'abc', 1, 1)
13+
14+
expect(res.timed).toHaveLength(0)
15+
expect(res.future).toHaveLength(0)
16+
})
17+
test('if the previous part is unset', () => {
18+
findLookaheadObjectsForPartMock.mockReturnValue([])
19+
20+
findLookaheadForLayer(context, { previous: undefined, current, next: nextFuture }, [], layer, 1, 1)
21+
22+
expect(findLookaheadObjectsForPartMock).toHaveBeenCalledTimes(2)
23+
expectInstancesToMatch(1, layer, current, undefined)
24+
})
25+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { getRandomString } from '@sofie-automation/corelib/dist/lib'
2+
import { PartInstanceAndPieceInstances, PartAndPieces } from '../../util.js'
3+
import { createFakePiece } from '../utils.js'
4+
5+
export const findForLayerTestConstants = {
6+
previous: {
7+
part: { _id: 'pPrev', part: 'prev' },
8+
allPieces: [createFakePiece('1'), createFakePiece('2'), createFakePiece('3')],
9+
onTimeline: true,
10+
nowInPart: 2000,
11+
} as any as PartInstanceAndPieceInstances,
12+
current: {
13+
part: { _id: 'pCur', part: 'cur' },
14+
allPieces: [createFakePiece('4'), createFakePiece('5'), createFakePiece('6')],
15+
onTimeline: true,
16+
nowInPart: 1000,
17+
} as any as PartInstanceAndPieceInstances,
18+
nextTimed: {
19+
part: { _id: 'pNextTimed', part: 'nextT' },
20+
allPieces: [createFakePiece('7'), createFakePiece('8'), createFakePiece('9')],
21+
onTimeline: true,
22+
} as any as PartInstanceAndPieceInstances,
23+
nextFuture: {
24+
part: { _id: 'pNextFuture', part: 'nextF' },
25+
allPieces: [createFakePiece('10'), createFakePiece('11'), createFakePiece('12')],
26+
onTimeline: false,
27+
} as any as PartInstanceAndPieceInstances,
28+
29+
orderedParts: [{ _id: 'p1' }, { _id: 'p2', invalid: true }, { _id: 'p3' }, { _id: 'p4' }, { _id: 'p5' }].map(
30+
(p) => ({
31+
part: p as any,
32+
usesInTransition: true,
33+
pieces: [{ _id: p._id + '_p1' } as any],
34+
})
35+
) as PartAndPieces[],
36+
37+
layer: getRandomString(),
38+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { setupDefaultJobEnvironment } from '../../../../../__mocks__/context.js'
2+
3+
jest.mock('../../../findObjects')
4+
import { findLookaheadObjectsForPart } from '../../../../../playout/lookahead/findObjects.js'
5+
6+
export type TfindLookaheadObjectsForPart = jest.MockedFunction<typeof findLookaheadObjectsForPart>
7+
8+
export const context = setupDefaultJobEnvironment()
9+
10+
const findLookaheadObjectsForPartMockBase = findLookaheadObjectsForPart as TfindLookaheadObjectsForPart
11+
export const findLookaheadObjectsForPartMock = findLookaheadObjectsForPartMockBase.mockImplementation(() => []) // Default mock
12+
13+
beforeEach(() => {
14+
findLookaheadObjectsForPartMock.mockReset()
15+
})
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { findLookaheadForLayer } from '../../findForLayer.js'
2+
import { setupDefaultJobEnvironment } from '../../../../__mocks__/context.js'
3+
4+
jest.mock('../../findObjects')
5+
import { findForLayerTestConstants } from './constants.js'
6+
import { expectPartToMatch } from '../utils.js'
7+
import { findLookaheadObjectsForPartMock } from './helpers/mockSetup.js'
8+
9+
const orderedParts = findForLayerTestConstants.orderedParts
10+
const layer = findForLayerTestConstants.layer
11+
12+
describe('findLookaheadForLayer - orderedParts', () => {
13+
beforeEach(() => {
14+
findLookaheadObjectsForPartMock.mockReset()
15+
})
16+
17+
const context = setupDefaultJobEnvironment()
18+
19+
test('finds lookahead for target index 1', () => {
20+
findLookaheadObjectsForPartMock
21+
.mockReturnValue([])
22+
.mockReturnValueOnce(['t0', 't1'] as any)
23+
.mockReturnValueOnce(['t2', 't3'] as any)
24+
.mockReturnValueOnce(['t4', 't5'] as any)
25+
.mockReturnValueOnce(['t6', 't7'] as any)
26+
.mockReturnValueOnce(['t8', 't9'] as any)
27+
28+
const res2 = findLookaheadForLayer(context, {}, orderedParts, layer, 1, 4, null)
29+
30+
expect(res2.timed).toHaveLength(0)
31+
expect(res2.future).toEqual(['t0', 't1'])
32+
expect(findLookaheadObjectsForPartMock).toHaveBeenCalledTimes(1)
33+
34+
expectPartToMatch(1, layer, orderedParts[0], undefined)
35+
})
36+
37+
test('returns nothing when target index is 0', () => {
38+
findLookaheadObjectsForPartMock.mockReturnValue([])
39+
40+
const res3 = findLookaheadForLayer(context, {}, orderedParts, layer, 0, 4, null)
41+
42+
expect(res3.timed).toHaveLength(0)
43+
expect(res3.future).toHaveLength(0)
44+
expect(findLookaheadObjectsForPartMock).toHaveBeenCalledTimes(0)
45+
})
46+
47+
test('searches across maximum search distance', () => {
48+
findLookaheadObjectsForPartMock
49+
.mockReturnValue([])
50+
.mockReturnValueOnce(['t0', 't1'] as any)
51+
.mockReturnValueOnce(['t2', 't3'] as any)
52+
.mockReturnValueOnce(['t4', 't5'] as any)
53+
.mockReturnValueOnce(['t6', 't7'] as any)
54+
.mockReturnValueOnce(['t8', 't9'] as any)
55+
56+
const res4 = findLookaheadForLayer(context, {}, orderedParts, layer, 100, 5, null)
57+
58+
expect(res4.timed).toHaveLength(0)
59+
expect(res4.future).toEqual(['t0', 't1', 't2', 't3', 't4', 't5'])
60+
61+
// Called for parts: [0], [2], [3]
62+
expect(findLookaheadObjectsForPartMock).toHaveBeenCalledTimes(3)
63+
64+
expectPartToMatch(1, layer, orderedParts[0], undefined)
65+
expectPartToMatch(2, layer, orderedParts[2], orderedParts[0].part)
66+
expectPartToMatch(3, layer, orderedParts[3], orderedParts[2].part)
67+
})
68+
})

0 commit comments

Comments
 (0)