Skip to content

Commit 5cb987e

Browse files
authored
Merge pull request Sofie-Automation#1164 from nrkno/feat/model-unit-tests
2 parents 1eff236 + d2f12b6 commit 5cb987e

File tree

8 files changed

+897
-33
lines changed

8 files changed

+897
-33
lines changed

packages/job-worker/src/__mocks__/collection.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
175175
async remove(selector: MongoQuery<TDoc> | TDoc['_id']): Promise<number> {
176176
this.#ops.push({ type: 'remove', args: [selector] })
177177

178-
return this.removeInner(selector)
179-
}
180-
private async removeInner(selector: MongoQuery<TDoc> | TDoc['_id']): Promise<number> {
181178
const docs: Pick<TDoc, '_id'>[] = await this.findFetchInner(selector, { projection: { _id: 1 } })
182179
for (const doc of docs) {
183180
this.#documents.delete(doc._id)
@@ -186,15 +183,15 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
186183
return docs.length
187184
}
188185
async update(selector: MongoQuery<TDoc> | TDoc['_id'], modifier: MongoModifier<TDoc>): Promise<number> {
189-
this.#ops.push({ type: 'update', args: [selector, modifier] })
190-
191186
return this.updateInner(selector, modifier, false)
192187
}
193188
private async updateInner(
194189
selector: MongoQuery<TDoc> | TDoc['_id'],
195190
modifier: MongoModifier<TDoc>,
196191
single: boolean
197192
) {
193+
this.#ops.push({ type: 'update', args: [selector, modifier] })
194+
198195
const docs = await this.findFetchInner(selector)
199196

200197
for (const doc of docs) {
@@ -210,9 +207,6 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
210207
async replace(doc: TDoc | ReadonlyDeep<TDoc>): Promise<boolean> {
211208
this.#ops.push({ type: 'replace', args: [doc._id] })
212209

213-
return this.replaceInner(doc)
214-
}
215-
private async replaceInner(doc: TDoc | ReadonlyDeep<TDoc>): Promise<boolean> {
216210
if (!doc._id) throw new Error(`replace requires document to have an _id`)
217211

218212
const exists = this.#documents.has(doc._id)
@@ -228,9 +222,9 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
228222
} else if ('updateOne' in op) {
229223
await this.updateInner(op.updateOne.filter, op.updateOne.update, true)
230224
} else if ('replaceOne' in op) {
231-
await this.replaceInner(op.replaceOne.replacement as any)
225+
await this.replace(op.replaceOne.replacement as any)
232226
} else if ('deleteMany' in op) {
233-
await this.removeInner(op.deleteMany.filter)
227+
await this.remove(op.deleteMany.filter)
234228
} else {
235229
// Note: implement more as we start using them
236230
throw new Error(`Unknown mongo Bulk Operation: ${JSON.stringify(op)}`)

packages/job-worker/src/ingest/commit.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -729,16 +729,6 @@ async function removeSegments(
729729

730730
async function validateScratchpad(_context: JobContext, playoutModel: PlayoutModel) {
731731
for (const rundown of playoutModel.rundowns) {
732-
const scratchpadSegment = rundown.getScratchpadSegment()
733-
734-
if (scratchpadSegment) {
735-
// Ensure the _rank is just before the real content
736-
const otherSegmentsInRundown = rundown.segments.filter(
737-
(s) => s.segment.orphaned !== SegmentOrphanedReason.SCRATCHPAD
738-
)
739-
const minNormalRank = Math.min(0, ...otherSegmentsInRundown.map((s) => s.segment._rank))
740-
741-
rundown.setScratchpadSegmentRank(minNormalRank - 1)
742-
}
732+
rundown.updateScratchpadSegmentRank()
743733
}
744734
}

packages/job-worker/src/playout/model/PlayoutRundownModel.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ export interface PlayoutRundownModel {
6363
*/
6464
getScratchpadSegment(): PlayoutSegmentModel | undefined
6565
/**
66-
* Set the rank of the Scratchpad Segment in this Rundown
67-
* Throws if the segment does not exists
68-
* @param rank New rank
66+
* Update the rank of the Scratchpad Segment in this Rundown, if it exists
6967
*/
70-
setScratchpadSegmentRank(rank: number): void
68+
updateScratchpadSegmentRank(): void
7169
}

packages/job-worker/src/playout/model/implementation/PlayoutRundownModelImpl.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,12 @@ export class PlayoutRundownModelImpl implements PlayoutRundownModel {
6767
const existingSegment = this.segments.find((s) => s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD)
6868
if (existingSegment) throw UserError.create(UserErrorMessage.ScratchpadAlreadyActive)
6969

70-
const minSegmentRank = Math.min(0, ...this.segments.map((s) => s.segment._rank))
71-
7270
const segmentId: SegmentId = getRandomId()
7371
this.#segments.unshift(
7472
new PlayoutSegmentModelImpl(
7573
{
7674
_id: segmentId,
77-
_rank: minSegmentRank - 1,
75+
_rank: calculateRankForScratchpadSegment(this.#segments),
7876
externalId: '__scratchpad__',
7977
externalModified: getCurrentTime(),
8078
rundownId: this.rundown._id,
@@ -105,13 +103,25 @@ export class PlayoutRundownModelImpl implements PlayoutRundownModel {
105103
return this.#segments.find((s) => s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD)
106104
}
107105

108-
setScratchpadSegmentRank(rank: number): void {
106+
updateScratchpadSegmentRank(): void {
109107
const segment = this.#segments.find((s) => s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD)
110-
if (!segment) throw new Error('Scratchpad segment does not exist!')
108+
if (!segment) return
111109

112-
segment.setScratchpadRank(rank)
113-
this.#segments.sort((a, b) => a.segment._rank - b.segment._rank)
110+
const changed = segment.setScratchpadRank(calculateRankForScratchpadSegment(this.#segments))
111+
if (!changed) return
114112

113+
this.#segments.sort((a, b) => a.segment._rank - b.segment._rank)
115114
this.#scratchPadSegmentHasChanged = true
116115
}
117116
}
117+
118+
function calculateRankForScratchpadSegment(segments: readonly PlayoutSegmentModel[]) {
119+
// Ensure the _rank is just before the real content
120+
121+
return (
122+
Math.min(
123+
0,
124+
...segments.map((s) => (s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD ? 0 : s.segment._rank))
125+
) - 1
126+
)
127+
}

packages/job-worker/src/playout/model/implementation/PlayoutSegmentModelImpl.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ export class PlayoutSegmentModelImpl implements PlayoutSegmentModel {
3232
* This segment belongs to Playout, so is allowed to be modified in this way
3333
* @param rank New rank for the segment
3434
*/
35-
setScratchpadRank(rank: number): void {
35+
setScratchpadRank(rank: number): boolean {
3636
if (this.#segment.orphaned !== SegmentOrphanedReason.SCRATCHPAD)
3737
throw new Error('setScratchpadRank can only be used on a SCRATCHPAD segment')
3838

39+
if (this.#segment._rank == rank) return false
40+
3941
this.#segment._rank = rank
42+
return true
4043
}
4144
}

0 commit comments

Comments
 (0)