Skip to content

Commit ddba586

Browse files
Julusianjstarpl
authored andcommitted
chore: refactor syncChangesToPartInstances and add some skeleton unit tests
1 parent a757a66 commit ddba586

File tree

2 files changed

+456
-198
lines changed

2 files changed

+456
-198
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
/* eslint-disable @typescript-eslint/unbound-method */
3+
import { setupDefaultJobEnvironment } from '../../__mocks__/context'
4+
import { setupMockShowStyleCompound } from '../../__mocks__/presetCollections'
5+
import { findInstancesToSync, PartInstanceToSync, SyncChangesToPartInstancesWorker } from '../syncChangesToPartInstance'
6+
import { mock } from 'jest-mock-extended'
7+
import type { PlayoutModel } from '../../playout/model/PlayoutModel'
8+
import type { IngestModelReadonly } from '../model/IngestModel'
9+
import type { PlayoutRundownModel } from '../../playout/model/PlayoutRundownModel'
10+
import type { PlayoutPartInstanceModel } from '../../playout/model/PlayoutPartInstanceModel'
11+
import type { DBPart } from '@sofie-automation/corelib/dist/dataModel/Part'
12+
import { protectString } from '@sofie-automation/corelib/dist/protectedString'
13+
14+
jest.mock('../../playout/adlibTesting')
15+
import { validateAdlibTestingPartInstanceProperties } from '../../playout/adlibTesting'
16+
17+
const mockOptions = {
18+
fallbackMockImplementation: () => {
19+
throw new Error('not mocked')
20+
},
21+
funcPropSupport: true,
22+
}
23+
24+
function createMockPart(id: string): DBPart {
25+
return {
26+
_id: protectString(id),
27+
_rank: 1,
28+
externalId: 'mockPartExternalId',
29+
segmentId: protectString('mockSegmentId'),
30+
rundownId: protectString('mockRundownId'),
31+
title: 'mockPartTitle',
32+
expectedDurationWithTransition: undefined,
33+
userEditOperations: undefined,
34+
}
35+
}
36+
37+
describe('SyncChangesToPartInstancesWorker', () => {
38+
describe('findInstancesToSync', () => {
39+
function createMockPlayoutModel(): PlayoutModel {
40+
return mock<PlayoutModel>(
41+
{
42+
currentPartInstance: null,
43+
nextPartInstance: null,
44+
previousPartInstance: null,
45+
},
46+
mockOptions
47+
)
48+
}
49+
function createMockPlayoutRundownModel(): PlayoutRundownModel {
50+
return mock<PlayoutRundownModel>({}, mockOptions)
51+
}
52+
function createMockIngestModelReadonly(): IngestModelReadonly {
53+
return mock<IngestModelReadonly>({}, mockOptions)
54+
}
55+
56+
test('No partInstances', async () => {
57+
const context = setupDefaultJobEnvironment()
58+
59+
const playoutModel = createMockPlayoutModel()
60+
const ingestModel = createMockIngestModelReadonly()
61+
const rundownModel = createMockPlayoutRundownModel()
62+
63+
const instancesToSync = findInstancesToSync(context, playoutModel, ingestModel, rundownModel)
64+
expect(instancesToSync).toHaveLength(0)
65+
})
66+
67+
// TODO - this needs a lot more fleshing out
68+
})
69+
70+
describe('syncChangesToPartInstance', () => {
71+
function createMockPlayoutModel(partialModel?: Partial<Pick<PlayoutModel, 'nextPartInstance'>>) {
72+
return mock<PlayoutModel>(
73+
{
74+
currentPartInstance: null,
75+
nextPartInstance: partialModel?.nextPartInstance ?? null,
76+
previousPartInstance: null,
77+
78+
clearAllNotifications: jest.fn(),
79+
// setPartInstanceAsNext: jest.fn(),
80+
// removeUntakenPartInstances: jest.fn(),
81+
},
82+
mockOptions
83+
)
84+
}
85+
function createMockPlayoutRundownModel(): PlayoutRundownModel {
86+
return mock<PlayoutRundownModel>({}, mockOptions)
87+
}
88+
function createMockIngestModelReadonly(): IngestModelReadonly {
89+
return mock<IngestModelReadonly>(
90+
{
91+
findPart: jest.fn(() => undefined),
92+
getGlobalPieces: jest.fn(() => []),
93+
},
94+
mockOptions
95+
)
96+
}
97+
98+
function createMockPartInstance(id: string): PlayoutPartInstanceModel {
99+
return mock<PlayoutPartInstanceModel>(
100+
{
101+
partInstance: {
102+
_id: protectString(id),
103+
part: createMockPart(id),
104+
segmentId: protectString('mockSegmentId'),
105+
rundownId: protectString('mockRundownId'),
106+
takeCount: 0,
107+
rehearsal: false,
108+
playlistActivationId: protectString('mockPlaylistActivationId'),
109+
segmentPlayoutId: protectString('mockSegmentPlayoutId'),
110+
} satisfies PlayoutPartInstanceModel['partInstance'],
111+
pieceInstances: [] satisfies PlayoutPartInstanceModel['pieceInstances'],
112+
113+
recalculateExpectedDurationWithTransition: jest.fn(),
114+
snapshotMakeCopy: jest.fn(() => Date.now() as any),
115+
snapshotRestore: jest.fn(() => {
116+
throw new Error('snapshotRestore not expected')
117+
}),
118+
},
119+
mockOptions
120+
)
121+
}
122+
123+
beforeEach(() => {
124+
jest.clearAllMocks()
125+
})
126+
127+
test('successful with empty blueprint method', async () => {
128+
const context = setupDefaultJobEnvironment()
129+
const showStyleCompound = await setupMockShowStyleCompound(context)
130+
131+
const syncIngestUpdateToPartInstanceFn = jest.fn()
132+
context.updateShowStyleBlueprint({
133+
syncIngestUpdateToPartInstance: syncIngestUpdateToPartInstanceFn,
134+
})
135+
const blueprint = await context.getShowStyleBlueprint(showStyleCompound._id)
136+
137+
const playoutModel = createMockPlayoutModel()
138+
const ingestModel = createMockIngestModelReadonly()
139+
const rundownModel = createMockPlayoutRundownModel()
140+
141+
const worker = new SyncChangesToPartInstancesWorker(
142+
context,
143+
playoutModel,
144+
ingestModel,
145+
showStyleCompound,
146+
blueprint
147+
)
148+
149+
const partInstance = createMockPartInstance('mockPartInstanceId')
150+
const part = createMockPart('mockPartId')
151+
152+
const instanceToSync: PartInstanceToSync = {
153+
playoutRundownModel: rundownModel,
154+
existingPartInstance: partInstance,
155+
previousPartInstance: null,
156+
playStatus: 'next',
157+
newPart: part,
158+
proposedPieceInstances: Promise.resolve([]),
159+
}
160+
161+
await worker.syncChangesToPartInstance(instanceToSync)
162+
163+
expect(partInstance.snapshotMakeCopy).toHaveBeenCalledTimes(1)
164+
expect(partInstance.snapshotRestore).toHaveBeenCalledTimes(0)
165+
expect(syncIngestUpdateToPartInstanceFn).toHaveBeenCalledTimes(1)
166+
expect(validateAdlibTestingPartInstanceProperties).toHaveBeenCalledTimes(1)
167+
})
168+
})
169+
})

0 commit comments

Comments
 (0)