Skip to content

Commit a5ecaf2

Browse files
committed
chore: add tests for lookaheadOffset
1 parent adcd524 commit a5ecaf2

File tree

12 files changed

+948
-39
lines changed

12 files changed

+948
-39
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
import { context, findLookaheadObjectsForPartMock } from './helpers/mockSetup.js'
1+
jest.mock('../../findObjects')
2+
import { context, TfindLookaheadObjectsForPart } from './helpers/mockSetup.js'
23
import { findLookaheadForLayer } from '../../findForLayer.js'
34
import { expectInstancesToMatch } from '../utils.js'
45
import { findForLayerTestConstants } from './constants.js'
6+
import { findLookaheadObjectsForPart } from '../../findObjects.js'
7+
8+
const findLookaheadObjectsForPartMockBase = findLookaheadObjectsForPart as TfindLookaheadObjectsForPart
9+
const findLookaheadObjectsForPartMock = findLookaheadObjectsForPartMockBase.mockImplementation(() => []) // Default mock
10+
11+
beforeEach(() => {
12+
findLookaheadObjectsForPartMock.mockReset()
13+
})
514

615
const current = findForLayerTestConstants.current
716
const nextFuture = findForLayerTestConstants.nextFuture
@@ -20,6 +29,6 @@ describe('findLookaheadForLayer – basic behavior', () => {
2029
findLookaheadForLayer(context, { previous: undefined, current, next: nextFuture }, [], layer, 1, 1)
2130

2231
expect(findLookaheadObjectsForPartMock).toHaveBeenCalledTimes(2)
23-
expectInstancesToMatch(1, layer, current, undefined)
32+
expectInstancesToMatch(findLookaheadObjectsForPartMock, 1, layer, current, undefined)
2433
})
2534
})

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

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
11
import { getRandomString } from '@sofie-automation/corelib/dist/lib'
22
import { PartInstanceAndPieceInstances, PartAndPieces } from '../../util.js'
33
import { createFakePiece } from '../utils.js'
4+
import { protectString } from '@sofie-automation/corelib/dist/protectedString'
5+
import { PieceTimelineObjectsBlob } from '@sofie-automation/corelib/dist/dataModel/Piece'
6+
7+
const layer: string = getRandomString()
8+
9+
const generateFakeObectsString = (
10+
pieceId: string,
11+
layer: string,
12+
beforeStart: number,
13+
afterStart: number,
14+
enableWhile: boolean = false
15+
) =>
16+
protectString<PieceTimelineObjectsBlob>(
17+
JSON.stringify([
18+
// At piece start
19+
{
20+
id: pieceId + 'tl_pieceStart' + (enableWhile ? '_while' : ''),
21+
layer: layer,
22+
enable: !enableWhile ? { start: 0 } : { while: 1 },
23+
content: { deviceType: 0, type: 'none' },
24+
},
25+
//beforeOffsetObj except if it's piece starts later than the offset.
26+
{
27+
id: pieceId + 'tl_beforeOffset' + (enableWhile ? '_while' : ''),
28+
layer: layer,
29+
enable: !enableWhile ? { start: beforeStart } : { while: beforeStart },
30+
content: { deviceType: 0, type: 'none' },
31+
},
32+
//afterOffsetObj except if it's piece starts later than the offset.
33+
{
34+
id: pieceId + 'tl_afterOffset' + (enableWhile ? '_while' : ''),
35+
layer: layer,
36+
enable: !enableWhile ? { start: afterStart } : { while: afterStart },
37+
content: { deviceType: 0, type: 'none' },
38+
},
39+
])
40+
)
441

542
export const findForLayerTestConstants = {
643
previous: {
@@ -34,5 +71,193 @@ export const findForLayerTestConstants = {
3471
})
3572
) as PartAndPieces[],
3673

37-
layer: getRandomString(),
74+
layer,
75+
}
76+
77+
const partDuration = 3000
78+
79+
const lookaheadOffsetLayerIds = {
80+
partStart: 'partStart_' + layer,
81+
beforeOffset: 'beforeOffset_' + layer,
82+
afterOffset: 'afterOffset_' + layer,
83+
}
84+
85+
export const lookaheadOffsetTestConstants = {
86+
multiLayerPart: {
87+
part: { _id: 'pLookahead_ml', part: 'lookahead_ml' },
88+
89+
pieces: [
90+
// piece1 — At Part Start - lookaheadOffset should equal nextTimeOffset
91+
createFakePiece('ml_piece1', {
92+
enable: { start: 0, duration: partDuration - 0 },
93+
sourceLayerId: lookaheadOffsetLayerIds.partStart,
94+
// We generate three objects, one at the piece's start, one 700 ms after the piece's start, one 1700 ms after the piece's start.
95+
// We need to check if all offsets are calculated correctly. (1000, 300 and no offset)
96+
timelineObjectsString: generateFakeObectsString(
97+
'ml_piece1',
98+
lookaheadOffsetLayerIds.partStart,
99+
700,
100+
1700
101+
),
102+
}),
103+
104+
// piece2 — Before Offset — lookaheadOffset should equal nextTimeOffset - the piece's start - the timeline object's start.
105+
createFakePiece('ml_piece2', {
106+
enable: { start: 500, duration: partDuration - 500 },
107+
sourceLayerId: lookaheadOffsetLayerIds.beforeOffset,
108+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 1200 ms after the piece's start.
109+
// We need to check if all offsets are calculated correctly. (500, 300 and no offset)
110+
timelineObjectsString: generateFakeObectsString(
111+
'ml_piece2',
112+
lookaheadOffsetLayerIds.beforeOffset,
113+
200,
114+
1200
115+
),
116+
}),
117+
118+
// piece3 — After Offset — no lookahead offset
119+
createFakePiece('ml_piece3', {
120+
enable: { start: 1500, duration: partDuration - 1500 },
121+
sourceLayerId: lookaheadOffsetLayerIds.afterOffset,
122+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 400 ms after the piece's start.
123+
// We need to check if all offsets are calculated correctly.
124+
// for reference no offset should be calculated for all of it's objects.
125+
timelineObjectsString: generateFakeObectsString(
126+
'ml_piece3',
127+
lookaheadOffsetLayerIds.afterOffset,
128+
200,
129+
400
130+
),
131+
}),
132+
],
133+
134+
onTimeline: true,
135+
} as any as PartAndPieces,
136+
multiLayerPartWhile: {
137+
part: { _id: 'pLookahead_ml_while', part: 'lookaheadFull_ml_while' },
138+
139+
pieces: [
140+
// piece1 — At Part Start - lookaheadOffset should equal nextTimeOffset
141+
createFakePiece('ml_piece1_while', {
142+
enable: { start: 0, duration: partDuration - 0 },
143+
sourceLayerId: lookaheadOffsetLayerIds.partStart,
144+
// We generate three objects, one at the piece's start, one 700 ms after the piece's start, one 1700 ms after the piece's start.
145+
// We need to check if all offsets are calculated correctly. (1000, 300 and no offset)
146+
timelineObjectsString: generateFakeObectsString(
147+
'ml_piece1_while',
148+
lookaheadOffsetLayerIds.partStart,
149+
700,
150+
1700,
151+
true
152+
),
153+
}),
154+
155+
// piece2 — Before Offset — lookaheadOffset should equal nextTimeOffset - the piece's start - the timeline object's start
156+
createFakePiece('ml_piece2_while', {
157+
enable: { start: 500, duration: partDuration - 500 },
158+
sourceLayerId: lookaheadOffsetLayerIds.beforeOffset,
159+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 1200 ms after the piece's start.
160+
// We need to check if all offsets are calculated correctly. (500, 300 and no offset)
161+
timelineObjectsString: generateFakeObectsString(
162+
'ml_piece2_while',
163+
lookaheadOffsetLayerIds.beforeOffset,
164+
200,
165+
1200,
166+
true
167+
),
168+
}),
169+
170+
// piece3 — After Offset — no lookahead offset
171+
createFakePiece('ml_piece3_while', {
172+
enable: { start: 1500, duration: partDuration - 1500 },
173+
sourceLayerId: lookaheadOffsetLayerIds.afterOffset,
174+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 400 ms after the piece's start.
175+
// We need to check if all offsets are calculated correctly. (no offset should be calculated)
176+
timelineObjectsString: generateFakeObectsString(
177+
'ml_piece3_while',
178+
lookaheadOffsetLayerIds.afterOffset,
179+
200,
180+
400,
181+
true
182+
),
183+
}),
184+
],
185+
186+
onTimeline: true,
187+
} as any as PartAndPieces,
188+
singleLayerPart: {
189+
part: { _id: 'pLookahead_sl', part: 'lookahead_sl' },
190+
191+
pieces: [
192+
// piece1 — At Part Start - should be ignored
193+
createFakePiece('sl_piece1', {
194+
enable: { start: 0, duration: partDuration - 0 },
195+
sourceLayerId: layer,
196+
// We generate three objects, one at the piece's start, one 700 ms after the piece's start, one 1700 ms after the piece's start.
197+
// If the piece is not ignored (which shouldn't happen, it would mean that the logic is wrong)
198+
// for reference the calculated offset values should be 1000, 300 and no offset
199+
timelineObjectsString: generateFakeObectsString('sl_piece1', layer, 700, 1700),
200+
}),
201+
202+
// piece2 — Before Offset — lookaheadOffset should equal nextTimeOffset - the piece's start - the timeline object's start
203+
createFakePiece('sl_piece2', {
204+
enable: { start: 500, duration: partDuration - 500 },
205+
sourceLayerId: layer,
206+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 1200 ms after the piece's start.
207+
// We need to check if all offsets are calculated correctly. (500, 300 and no offset)
208+
timelineObjectsString: generateFakeObectsString('sl_piece2', layer, 200, 1200),
209+
}),
210+
211+
// piece3 — After Offset — should be ignored
212+
createFakePiece('sl_piece3', {
213+
enable: { start: 1500, duration: partDuration - 1500 },
214+
sourceLayerId: layer,
215+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 400 ms after the piece's start.
216+
// If the piece is not ignored (which shouldn't happen, it would mean that the logic is wrong)
217+
// for reference no offset should be calculated for all of it's objects.
218+
timelineObjectsString: generateFakeObectsString('sl_piece3', layer, 200, 400),
219+
}),
220+
],
221+
onTimeline: true,
222+
} as any as PartAndPieces,
223+
singleLayerPartWhile: {
224+
part: { _id: 'pLookahead_sl_while', part: 'lookahead_sl_while' },
225+
226+
pieces: [
227+
// piece1 — At Part Start - should be ignored
228+
createFakePiece('sl_piece1_while', {
229+
enable: { start: 0, duration: partDuration - 0 },
230+
sourceLayerId: layer,
231+
// We generate three objects, one at the piece's start, one 700 ms after the piece's start, one 1700 ms after the piece's start.
232+
// If the piece is not ignored (which shouldn't happen, it would mean that the logic is wrong)
233+
// for reference the calculated offset values should be 1000, 300 and no offset
234+
timelineObjectsString: generateFakeObectsString('sl_piece1_while', layer, 700, 1700, true),
235+
}),
236+
237+
// piece2 — Before Offset — lookaheadOffset should equal nextTimeOffset - the piece's start - the timeline object's start
238+
createFakePiece('sl_piece2_while', {
239+
enable: { start: 500, duration: partDuration - 500 },
240+
sourceLayerId: layer,
241+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 1200 ms after the piece's start.
242+
// We need to check if all offsets are calculated correctly. (500, 300 and no offset)
243+
timelineObjectsString: generateFakeObectsString('sl_piece2_while', layer, 200, 1200, true),
244+
}),
245+
246+
// piece3 — After Offset — should be ignored
247+
createFakePiece('sl_piece3_while', {
248+
enable: { start: 1500, duration: partDuration - 1500 },
249+
sourceLayerId: layer,
250+
// We generate three objects, one at the piece's start, one 200 ms after the piece's start, one 400 ms after the piece's start.
251+
// If the piece is not ignored (which shouldn't happen, it would mean that the logic is wrong)
252+
// for reference no offset should be calculated for all of it's objects.
253+
timelineObjectsString: generateFakeObectsString('sl_piece3_while', layer, 200, 400, true),
254+
}),
255+
],
256+
onTimeline: true,
257+
} as any as PartAndPieces,
258+
259+
nextTimeOffset: 1000,
260+
261+
layer,
262+
lookaheadOffsetLayerIds,
38263
}
Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,6 @@
11
import { setupDefaultJobEnvironment } from '../../../../../__mocks__/context.js'
2-
3-
jest.mock('../../../findObjects')
42
import { findLookaheadObjectsForPart } from '../../../../../playout/lookahead/findObjects.js'
53

64
export type TfindLookaheadObjectsForPart = jest.MockedFunction<typeof findLookaheadObjectsForPart>
75

86
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-
})

0 commit comments

Comments
 (0)