Skip to content

Commit

Permalink
Merge pull request nrkno#1164 from nrkno/feat/model-unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Mar 22, 2024
2 parents 1eff236 + d2f12b6 commit 5cb987e
Show file tree
Hide file tree
Showing 8 changed files with 897 additions and 33 deletions.
14 changes: 4 additions & 10 deletions packages/job-worker/src/__mocks__/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
async remove(selector: MongoQuery<TDoc> | TDoc['_id']): Promise<number> {
this.#ops.push({ type: 'remove', args: [selector] })

return this.removeInner(selector)
}
private async removeInner(selector: MongoQuery<TDoc> | TDoc['_id']): Promise<number> {
const docs: Pick<TDoc, '_id'>[] = await this.findFetchInner(selector, { projection: { _id: 1 } })
for (const doc of docs) {
this.#documents.delete(doc._id)
Expand All @@ -186,15 +183,15 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
return docs.length
}
async update(selector: MongoQuery<TDoc> | TDoc['_id'], modifier: MongoModifier<TDoc>): Promise<number> {
this.#ops.push({ type: 'update', args: [selector, modifier] })

return this.updateInner(selector, modifier, false)
}
private async updateInner(
selector: MongoQuery<TDoc> | TDoc['_id'],
modifier: MongoModifier<TDoc>,
single: boolean
) {
this.#ops.push({ type: 'update', args: [selector, modifier] })

const docs = await this.findFetchInner(selector)

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

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

const exists = this.#documents.has(doc._id)
Expand All @@ -228,9 +222,9 @@ export class MockMongoCollection<TDoc extends { _id: ProtectedString<any> }> imp
} else if ('updateOne' in op) {
await this.updateInner(op.updateOne.filter, op.updateOne.update, true)
} else if ('replaceOne' in op) {
await this.replaceInner(op.replaceOne.replacement as any)
await this.replace(op.replaceOne.replacement as any)
} else if ('deleteMany' in op) {
await this.removeInner(op.deleteMany.filter)
await this.remove(op.deleteMany.filter)
} else {
// Note: implement more as we start using them
throw new Error(`Unknown mongo Bulk Operation: ${JSON.stringify(op)}`)
Expand Down
12 changes: 1 addition & 11 deletions packages/job-worker/src/ingest/commit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,16 +729,6 @@ async function removeSegments(

async function validateScratchpad(_context: JobContext, playoutModel: PlayoutModel) {
for (const rundown of playoutModel.rundowns) {
const scratchpadSegment = rundown.getScratchpadSegment()

if (scratchpadSegment) {
// Ensure the _rank is just before the real content
const otherSegmentsInRundown = rundown.segments.filter(
(s) => s.segment.orphaned !== SegmentOrphanedReason.SCRATCHPAD
)
const minNormalRank = Math.min(0, ...otherSegmentsInRundown.map((s) => s.segment._rank))

rundown.setScratchpadSegmentRank(minNormalRank - 1)
}
rundown.updateScratchpadSegmentRank()
}
}
6 changes: 2 additions & 4 deletions packages/job-worker/src/playout/model/PlayoutRundownModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export interface PlayoutRundownModel {
*/
getScratchpadSegment(): PlayoutSegmentModel | undefined
/**
* Set the rank of the Scratchpad Segment in this Rundown
* Throws if the segment does not exists
* @param rank New rank
* Update the rank of the Scratchpad Segment in this Rundown, if it exists
*/
setScratchpadSegmentRank(rank: number): void
updateScratchpadSegmentRank(): void
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ export class PlayoutRundownModelImpl implements PlayoutRundownModel {
const existingSegment = this.segments.find((s) => s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD)
if (existingSegment) throw UserError.create(UserErrorMessage.ScratchpadAlreadyActive)

const minSegmentRank = Math.min(0, ...this.segments.map((s) => s.segment._rank))

const segmentId: SegmentId = getRandomId()
this.#segments.unshift(
new PlayoutSegmentModelImpl(
{
_id: segmentId,
_rank: minSegmentRank - 1,
_rank: calculateRankForScratchpadSegment(this.#segments),
externalId: '__scratchpad__',
externalModified: getCurrentTime(),
rundownId: this.rundown._id,
Expand Down Expand Up @@ -105,13 +103,25 @@ export class PlayoutRundownModelImpl implements PlayoutRundownModel {
return this.#segments.find((s) => s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD)
}

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

segment.setScratchpadRank(rank)
this.#segments.sort((a, b) => a.segment._rank - b.segment._rank)
const changed = segment.setScratchpadRank(calculateRankForScratchpadSegment(this.#segments))
if (!changed) return

this.#segments.sort((a, b) => a.segment._rank - b.segment._rank)
this.#scratchPadSegmentHasChanged = true
}
}

function calculateRankForScratchpadSegment(segments: readonly PlayoutSegmentModel[]) {
// Ensure the _rank is just before the real content

return (
Math.min(
0,
...segments.map((s) => (s.segment.orphaned === SegmentOrphanedReason.SCRATCHPAD ? 0 : s.segment._rank))
) - 1
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ export class PlayoutSegmentModelImpl implements PlayoutSegmentModel {
* This segment belongs to Playout, so is allowed to be modified in this way
* @param rank New rank for the segment
*/
setScratchpadRank(rank: number): void {
setScratchpadRank(rank: number): boolean {
if (this.#segment.orphaned !== SegmentOrphanedReason.SCRATCHPAD)
throw new Error('setScratchpadRank can only be used on a SCRATCHPAD segment')

if (this.#segment._rank == rank) return false

this.#segment._rank = rank
return true
}
}
Loading

0 comments on commit 5cb987e

Please sign in to comment.