Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { protectString } from '@sofie-automation/corelib/dist/protectedString'
import { mongoWhereFilter } from '@sofie-automation/corelib/dist/mongo'
import { buildPastInfinitePiecesForThisPartQuery } from '@sofie-automation/corelib/dist/playout/infinites'
import { PieceLifespan } from '@sofie-automation/blueprints-integration'
import {
createResolvedPlaylistConversionContext,
findCurrentPartInstance,
Expand All @@ -14,6 +17,7 @@ import {
makeRundown,
makeSegment,
makeTestShowStyleBaseExt,
makeLargeActivePlaylist,
} from './resolvedPlaylistConversionTestUtils.js'

describe('conversionContext', () => {
Expand Down Expand Up @@ -190,3 +194,162 @@ describe('conversionContext', () => {
).toBeUndefined()
})
})

describe('conversionContext accessors', () => {
// The accessors look documents up through indexes. They must return exactly what a plain scan of the whole
// playlist would, in the same order, for any selector.
const props = makeLargeActivePlaylist({
rundowns: 2,
segmentsPerRundown: 4,
partsPerSegment: 3,
piecesPerPart: 4,
playedParts: 5,
now: 1_700_000_000_000,
})
const ctx = createResolvedPlaylistConversionContext(props)
const playlist = props.playlistState as any
const ids = (docs: Array<{ _id: unknown }> | undefined) => (docs ?? []).map((d) => String(d._id))

const segment = props.segmentsState[1]
const part = props.partsState[7]
const partInstance = props.partInstancesInPlaylistState.find((pi) => !pi.reset && pi.segmentId === segment._id)
if (!partInstance) throw new Error('Fixture has no PartInstance in the chosen segment')

it('segmentsFindOne', () => {
const reference = (selector: any) => mongoWhereFilter(props.segmentsState, selector)[0]
const selectors: any[] = [
{ _id: segment._id },
{ _id: { $eq: segment._id } },
{ _id: segment._id, rundownId: protectString('rundown_other') },
{ _id: protectString('missing') },
{ _rank: 2 },
]
for (const selector of selectors) {
expect(ctx.accessors.segmentsFindOne(selector, {})?._id).toEqual(reference(selector)?._id)
}
expect(ctx.accessors.segmentsFindOne(segment._id, {})?._id).toEqual(segment._id)
expect(ctx.accessors.segmentsFindOne(protectString('missing'), {})).toBeUndefined()
})

it('getSegmentsAndPartsSync', () => {
const reference = (rundownIds: unknown[], segmentsQuery?: any, partsQuery?: any) => {
const segments = props.segmentsState.filter((s) => rundownIds.includes(s.rundownId))
const parts = props.partsState.filter((p) => rundownIds.includes(p.rundownId))
return {
segments: segmentsQuery ? mongoWhereFilter(segments, segmentsQuery) : segments,
parts: partsQuery ? mongoWhereFilter(parts, partsQuery) : parts,
}
}
const cases: Array<[unknown[], any, any]> = [
[playlist.rundownIdsInOrder, undefined, undefined],
[playlist.rundownIdsInOrder, { _id: segment._id }, { segmentId: segment._id }],
// A segment outside of the rundowns asked for must not be returned, even when selected by id
[[playlist.rundownIdsInOrder[0]], { _id: segment._id }, { segmentId: segment._id }],
[playlist.rundownIdsInOrder, { _rank: { $gte: 2 } }, { segmentId: { $in: [segment._id, part.segmentId] } }],
]
for (const [rundownIds, segmentsQuery, partsQuery] of cases) {
const result = ctx.accessors.getSegmentsAndPartsSync(
{ ...playlist, rundownIdsInOrder: rundownIds },
segmentsQuery,
partsQuery
)
const expected = reference(rundownIds, segmentsQuery, partsQuery)
expect(ids(result.segments)).toEqual(ids(expected.segments))
expect(ids(result.parts)).toEqual(ids(expected.parts))
}
})

it('getActivePartInstances', () => {
const reference = (selector?: any) =>
mongoWhereFilter(props.partInstancesInPlaylistState, { ...selector, reset: { $ne: true } })
const selectors: any[] = [
undefined,
{ segmentId: segment._id },
{ segmentId: { $eq: segment._id } },
// The reset check wins over the selector
{ segmentId: segment._id, reset: true },
{ rundownId: segment.rundownId },
]
for (const selector of selectors) {
expect(ids(ctx.accessors.getActivePartInstances(playlist, selector))).toEqual(ids(reference(selector)))
}
expect(ids(ctx.accessors.getActivePartInstances(playlist, undefined))).not.toHaveLength(0)
})

it('piecesFind', () => {
const reference = (selector?: any) =>
selector ? mongoWhereFilter(props.piecesInPlaylistState, selector) : props.piecesInPlaylistState
const laterPart = props.partsState[11]
const previousPartIdsInSegment = props.partsState
.filter((p) => p.segmentId === laterPart.segmentId && p._rank < laterPart._rank)
.map((p) => p._id)
if (previousPartIdsInSegment.length === 0) throw new Error('Fixture part is first in its segment')
const segmentIdsInRundown = props.segmentsState
.filter((s) => s.rundownId === laterPart.rundownId)
.map((s) => s._id)
const otherLifespans = [PieceLifespan.OutOnSegmentEnd, PieceLifespan.OutOnRundownEnd]
const selectors: any[] = [
undefined,
{ startPartId: part._id },
{ startPartId: { $eq: part._id } },
// Selectors the index can't be used for
{ startPartId: { $in: [part._id, laterPart._id] } },
{ sourceLayerId: 'sl1' },
// The query for the infinites that may continue into a part, with a single fragment it is flattened
buildPastInfinitePiecesForThisPartQuery(laterPart, previousPartIdsInSegment, [], []),
// ... and with several they are under an `$or`
buildPastInfinitePiecesForThisPartQuery(laterPart, previousPartIdsInSegment, segmentIdsInRundown, [
playlist.rundownIdsInOrder[0],
]),
// Shaped like the infinites query, but may match WithinPart pieces too
{ lifespan: { $in: [PieceLifespan.WithinPart, ...otherLifespans] } },
{
$or: [
{ lifespan: { $in: otherLifespans } },
{ lifespan: { $in: [PieceLifespan.WithinPart] }, startPartId: part._id },
],
},
]
// Make sure both shapes of the infinites query are covered
expect(selectors[5]).not.toHaveProperty('$or')
expect(selectors[6]).toHaveProperty('$or')
expect(reference(selectors[6])).not.toHaveLength(0)

for (const selector of selectors) {
expect(ids(ctx.accessors.piecesFind(selector))).toEqual(ids(reference(selector)))
}
const pieceId = props.piecesInPlaylistState[3]._id
expect(ids(ctx.accessors.piecesFind(pieceId))).toEqual([String(pieceId)])
})

it('pieceInstancesFind', () => {
const reference = (selector?: any) =>
selector
? mongoWhereFilter(props.pieceInstancesInPlaylistState, selector)
: props.pieceInstancesInPlaylistState
const selectors: any[] = [
undefined,
{ partInstanceId: partInstance._id },
{ partInstanceId: { $eq: partInstance._id } },
{ partInstanceId: partInstance._id, reset: { $ne: true } },
{ partInstanceId: { $in: [partInstance._id] } },
{ rundownId: partInstance.rundownId },
]
for (const selector of selectors) {
expect(ids(ctx.accessors.pieceInstancesFind(selector))).toEqual(ids(reference(selector)))
}
const pieceInstanceId = props.pieceInstancesInPlaylistState[2]._id
expect(ids(ctx.accessors.pieceInstancesFind(pieceInstanceId))).toEqual([String(pieceInstanceId)])
})

it('results can be modified without affecting later lookups', () => {
const first = ctx.accessors.pieceInstancesFind(props.pieceInstancesInPlaylistState[2]._id)
first.length = 0
expect(ctx.accessors.pieceInstancesFind(props.pieceInstancesInPlaylistState[2]._id)).toHaveLength(1)

const all = ctx.accessors.getActivePartInstances(playlist, undefined)
const count = all.length
all.pop()
expect(ctx.accessors.getActivePartInstances(playlist, undefined)).toHaveLength(count)
})
})
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { protectString } from '@sofie-automation/corelib/dist/protectedString'
import { PlaylistTimingType } from '@sofie-automation/blueprints-integration'
import { PieceLifespan, PlaylistTimingType } from '@sofie-automation/blueprints-integration'
import { ShowStyleBaseExt } from '../../../../collections/showStyleBaseHandler.js'
import { QuickLoopMarkerType } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist/RundownPlaylist'
import type { ToResolvedPlaylistStatusProps } from '../context/conversionContext.js'

export function makeTestShowStyleBaseExt(): ShowStyleBaseExt {
return {
Expand Down Expand Up @@ -133,3 +134,170 @@ export function makeQuickLoop(): any {
end: { type: QuickLoopMarkerType.RUNDOWN, id: protectString('rundown0') },
}
}

function pieceLifespan(segmentIndex: number, partIndex: number, pieceIndex: number): PieceLifespan {
if (pieceIndex === 0) return PieceLifespan.OutOnSegmentEnd
if (pieceIndex === 1 && partIndex === 0) return PieceLifespan.OutOnRundownEnd
if (pieceIndex === 2 && partIndex === 0 && segmentIndex % 10 === 0) return PieceLifespan.OutOnShowStyleEnd
return PieceLifespan.WithinPart
}

export interface LargeActivePlaylistOptions {
rundowns: number
segmentsPerRundown: number
partsPerSegment: number
piecesPerPart: number
/** How many parts (in playout order, from the start of the first rundown) have been played */
playedParts: number
/** The time the playlist is generated relative to */
now: number
}

/**
* Build an active playlist where the first `playedParts` parts have been played, with a mix of infinite pieces and
* PartInstances / PieceInstances left over from a previous activation.
*/
export function makeLargeActivePlaylist({
rundowns: RUNDOWNS,
segmentsPerRundown: SEGMENTS_PER_RUNDOWN,
partsPerSegment: PARTS_PER_SEGMENT,
piecesPerPart: PIECES_PER_PART,
playedParts: PLAYED_PARTS,
now: FIXED_NOW,
}: LargeActivePlaylistOptions): ToResolvedPlaylistStatusProps {
const activationId = protectString('activation0')
const rundownIds = Array.from({ length: RUNDOWNS }, (_, i) => `rundown${i}`)

const showStyleBaseExt = {
...makeTestShowStyleBaseExt(),
sourceLayers: {
sl1: { _id: 'sl1', _rank: 0, name: 'Source Layer 1', type: 0 },
sl2: { _id: 'sl2', _rank: 1, name: 'Source Layer 2', type: 0 },
},
outputLayers: {
ol1: { _id: 'ol1', _rank: 0, name: 'Output Layer 1', isPGM: true },
ol2: { _id: 'ol2', _rank: 1, name: 'Output Layer 2', isPGM: false },
},
} as any

const rundowns: any[] = []
const segments: any[] = []
const parts: any[] = []
const pieces: any[] = []
const partInstances: any[] = []
const pieceInstances: any[] = []

let playoutIndex = 0
for (const [rundownIndex, rundownId] of rundownIds.entries()) {
rundowns.push(makeRundown(rundownId, rundownIndex))

for (let segmentIndex = 0; segmentIndex < SEGMENTS_PER_RUNDOWN; segmentIndex++) {
const segmentId = `${rundownId}_segment${segmentIndex}`
segments.push(makeSegment(segmentId, rundownId, segmentIndex))

for (let partIndex = 0; partIndex < PARTS_PER_SEGMENT; partIndex++) {
const partId = `${segmentId}_part${partIndex}`
const part = { ...makePart(partId, rundownId, segmentId, partIndex), autoNext: false }
parts.push(part)

const partPieces: any[] = []
for (let pieceIndex = 0; pieceIndex < PIECES_PER_PART; pieceIndex++) {
const lifespan = pieceLifespan(segmentIndex, partIndex, pieceIndex)
const piece = {
_id: protectString(`${partId}_piece${pieceIndex}`),
externalId: `ext_${partId}_piece${pieceIndex}`,
name: `Piece ${pieceIndex} of ${partId}`,
startRundownId: protectString(rundownId),
startSegmentId: protectString(segmentId),
startPartId: protectString(partId),
lifespan,
sourceLayerId: pieceIndex % 2 === 0 ? 'sl1' : 'sl2',
outputLayerId: pieceIndex % 2 === 0 ? 'ol1' : 'ol2',
enable:
lifespan === PieceLifespan.WithinPart
? { start: pieceIndex * 100, duration: 500 }
: { start: 0 },
invalid: false,
pieceType: 'normal',
content: {},
timelineObjectsString: '[]',
}
pieces.push(piece)
partPieces.push(piece)
}

if (playoutIndex <= PLAYED_PARTS) {
// An instance left over from a previous activation, which must be ignored
partInstances.push({
_id: protectString(`old_pi_${partId}`),
rundownId: protectString(rundownId),
segmentId: protectString(segmentId),
playlistActivationId: protectString('activationOld'),
takeCount: playoutIndex,
reset: true,
part,
timings: { plannedStartedPlayback: FIXED_NOW - 3_600_000, take: FIXED_NOW - 3_600_000 },
})
for (const piece of partPieces) {
pieceInstances.push({
_id: protectString(`old_pi_${partId}_${piece._id}`),
partInstanceId: protectString(`old_pi_${partId}`),
rundownId: protectString(rundownId),
playlistActivationId: protectString('activationOld'),
reset: true,
piece,
})
}

// The instance in the current activation. The last one is the next part, so hasn't started yet
const isNext = playoutIndex === PLAYED_PARTS
const partInstanceId = `pi_${partId}`
partInstances.push({
_id: protectString(partInstanceId),
rundownId: protectString(rundownId),
segmentId: protectString(segmentId),
playlistActivationId: activationId,
takeCount: playoutIndex,
part,
timings: isNext
? { setAsNext: FIXED_NOW - 60_000 }
: {
setAsNext: FIXED_NOW - 600_000 + playoutIndex * 10_000,
take: FIXED_NOW - 590_000 + playoutIndex * 10_000,
plannedStartedPlayback: FIXED_NOW - 590_000 + playoutIndex * 10_000,
},
})
for (const piece of partPieces) {
pieceInstances.push({
_id: protectString(`${partInstanceId}_${piece._id}`),
partInstanceId: protectString(partInstanceId),
rundownId: protectString(rundownId),
playlistActivationId: activationId,
piece,
})
}
}

playoutIndex++
}
}
}

const playlist = makePlaylist({
activationId,
rundownIdsInOrder: rundownIds.map((id) => protectString(id)),
currentPartInfo: { partInstanceId: partInstances.at(-3)._id },
nextPartInfo: { partInstanceId: partInstances.at(-1)._id },
})

return {
playlistState: playlist,
rundownsState: rundowns,
showStyleBaseExtState: showStyleBaseExt,
segmentsState: segments,
partsState: parts,
partInstancesInPlaylistState: partInstances,
piecesInPlaylistState: pieces,
pieceInstancesInPlaylistState: pieceInstances,
}
}
Loading
Loading