From 731a0922bb9bf3fc2fa03299cfd74f151b9e8bdd Mon Sep 17 00:00:00 2001 From: Julian Waller Date: Wed, 13 Nov 2024 09:51:44 +0000 Subject: [PATCH] wip: tests --- packages/job-worker/src/__mocks__/context.ts | 12 +++- .../src/__mocks__/defaultCollectionObjects.ts | 4 +- .../src/blueprints/__tests__/config.test.ts | 16 ++--- .../src/blueprints/__tests__/context.test.ts | 3 +- .../src/ingest/__tests__/ingest.test.ts | 7 +- .../__tests__/selectShowStyleVariant.test.ts | 18 +++--- .../mosDevice/__tests__/mosIngest.test.ts | 2 +- .../src/playout/__tests__/playout.test.ts | 7 +- .../lookahead/__tests__/lookahead.test.ts | 8 +-- .../playout/lookahead/__tests__/util.test.ts | 2 +- .../__tests__/StudioRouteSetUpdater.spec.ts | 64 +++++++++++++------ 11 files changed, 87 insertions(+), 56 deletions(-) diff --git a/packages/job-worker/src/__mocks__/context.ts b/packages/job-worker/src/__mocks__/context.ts index 57a861bb9f..d06fac5154 100644 --- a/packages/job-worker/src/__mocks__/context.ts +++ b/packages/job-worker/src/__mocks__/context.ts @@ -42,6 +42,7 @@ import { IDirectCollections } from '../db' import { ApmSpan, JobContext, + JobStudio, ProcessedShowStyleBase, ProcessedShowStyleCompound, ProcessedShowStyleVariant, @@ -56,6 +57,7 @@ import { JSONBlobStringify } from '@sofie-automation/shared-lib/dist/lib/JSONBlo import { removeRundownPlaylistFromDb } from '../ingest/__tests__/lib' import { processShowStyleBase, processShowStyleVariant } from '../jobs/showStyle' import { defaultStudio } from './defaultCollectionObjects' +import { convertStudioToJobStudio } from '../jobs/studio' export function setupDefaultJobEnvironment(studioId?: StudioId): MockJobContext { const { mockCollections, jobCollections } = getMockCollections() @@ -75,6 +77,7 @@ export class MockJobContext implements JobContext { #jobCollections: Readonly #mockCollections: Readonly #studio: ReadonlyDeep + #jobStudio: ReadonlyDeep #studioBlueprint: ReadonlyDeep #showStyleBlueprint: ReadonlyDeep @@ -87,6 +90,7 @@ export class MockJobContext implements JobContext { this.#jobCollections = jobCollections this.#mockCollections = mockCollections this.#studio = studio + this.#jobStudio = convertStudioToJobStudio(clone(studio)) this.#studioBlueprint = MockStudioBlueprint() this.#showStyleBlueprint = MockShowStyleBlueprint() @@ -103,7 +107,10 @@ export class MockJobContext implements JobContext { get studioId(): StudioId { return this.#studio._id } - get studio(): ReadonlyDeep { + get studio(): ReadonlyDeep { + return this.#jobStudio + } + get rawStudio(): ReadonlyDeep { return this.#studio } @@ -219,7 +226,7 @@ export class MockJobContext implements JobContext { } } getShowStyleBlueprintConfig(showStyle: ReadonlyDeep): ProcessedShowStyleConfig { - return preprocessShowStyleConfig(showStyle, this.#showStyleBlueprint, this.#studio.settings) + return preprocessShowStyleConfig(showStyle, this.#showStyleBlueprint, this.studio.settings) } hackPublishTimelineToFastTrack(_newTimeline: TimelineComplete): void { @@ -244,6 +251,7 @@ export class MockJobContext implements JobContext { setStudio(studio: ReadonlyDeep): void { this.#studio = clone(studio) + this.#jobStudio = convertStudioToJobStudio(clone(studio)) } setShowStyleBlueprint(blueprint: ReadonlyDeep): void { this.#showStyleBlueprint = blueprint diff --git a/packages/job-worker/src/__mocks__/defaultCollectionObjects.ts b/packages/job-worker/src/__mocks__/defaultCollectionObjects.ts index 5f13ba6285..752b8f9eb6 100644 --- a/packages/job-worker/src/__mocks__/defaultCollectionObjects.ts +++ b/packages/job-worker/src/__mocks__/defaultCollectionObjects.ts @@ -102,12 +102,12 @@ export function defaultStudio(_id: StudioId): DBStudio { mappingsWithOverrides: wrapDefaultObject({}), supportedShowStyleBase: [], blueprintConfigWithOverrides: wrapDefaultObject({}), - settings: { + settingsWithOverrides: wrapDefaultObject({ frameRate: 25, mediaPreviewsUrl: '', minimumTakeSpan: DEFAULT_MINIMUM_TAKE_SPAN, allowAdlibTestingSegment: true, - }, + }), routeSetsWithOverrides: wrapDefaultObject({}), routeSetExclusivityGroupsWithOverrides: wrapDefaultObject({}), packageContainersWithOverrides: wrapDefaultObject({}), diff --git a/packages/job-worker/src/blueprints/__tests__/config.test.ts b/packages/job-worker/src/blueprints/__tests__/config.test.ts index 2e77bd5dd8..5e142a6bec 100644 --- a/packages/job-worker/src/blueprints/__tests__/config.test.ts +++ b/packages/job-worker/src/blueprints/__tests__/config.test.ts @@ -10,12 +10,12 @@ describe('Test blueprint config', () => { test('compileStudioConfig', () => { const jobContext = setupDefaultJobEnvironment() jobContext.setStudio({ - ...jobContext.studio, - settings: { + ...jobContext.rawStudio, + settingsWithOverrides: wrapDefaultObject({ mediaPreviewsUrl: '', frameRate: 25, minimumTakeSpan: DEFAULT_MINIMUM_TAKE_SPAN, - }, + }), blueprintConfigWithOverrides: wrapDefaultObject({ sdfsdf: 'one', another: 5 }), }) jobContext.updateStudioBlueprint({ @@ -33,12 +33,12 @@ describe('Test blueprint config', () => { test('compileStudioConfig with function', () => { const jobContext = setupDefaultJobEnvironment() jobContext.setStudio({ - ...jobContext.studio, - settings: { + ...jobContext.rawStudio, + settingsWithOverrides: wrapDefaultObject({ mediaPreviewsUrl: '', frameRate: 25, minimumTakeSpan: DEFAULT_MINIMUM_TAKE_SPAN, - }, + }), blueprintConfigWithOverrides: wrapDefaultObject({ sdfsdf: 'one', another: 5 }), }) jobContext.updateStudioBlueprint({ @@ -136,7 +136,7 @@ describe('Test blueprint config', () => { const studioId = jobContext.studioId jobContext.setStudio({ - ...jobContext.studio, + ...jobContext.rawStudio, blueprintConfigWithOverrides: wrapDefaultObject({ two: 'abc', number: 99, @@ -183,7 +183,7 @@ describe('Test blueprint config', () => { }, }) jobContext.setStudio({ - ...jobContext.studio, + ...jobContext.rawStudio, supportedShowStyleBase: [showStyle._id], }) jobContext.updateShowStyleBlueprint({ diff --git a/packages/job-worker/src/blueprints/__tests__/context.test.ts b/packages/job-worker/src/blueprints/__tests__/context.test.ts index 307289c2df..5388b21303 100644 --- a/packages/job-worker/src/blueprints/__tests__/context.test.ts +++ b/packages/job-worker/src/blueprints/__tests__/context.test.ts @@ -1,6 +1,5 @@ import { getHash } from '@sofie-automation/corelib/dist/lib' import { unprotectString } from '@sofie-automation/corelib/dist/protectedString' -import { applyAndValidateOverrides } from '@sofie-automation/corelib/dist/settings/objectWithOverrides' import { MockJobContext, setupDefaultJobEnvironment } from '../../__mocks__/context' import { getShowStyleConfigRef, getStudioConfigRef } from '../configRefs' import { CommonContext } from '../context/CommonContext' @@ -81,7 +80,7 @@ describe('Test blueprint api context', () => { expect(context.studio).toBe(studio) expect(context.getStudioConfig()).toBe(studioConfig) - expect(context.getStudioMappings()).toEqual(applyAndValidateOverrides(studio.mappingsWithOverrides).obj) + expect(context.getStudioMappings()).toEqual(studio.mappings) }) test('getStudioConfigRef', () => { const context = new StudioContext( diff --git a/packages/job-worker/src/ingest/__tests__/ingest.test.ts b/packages/job-worker/src/ingest/__tests__/ingest.test.ts index e46a0f827b..a9d96c0c97 100644 --- a/packages/job-worker/src/ingest/__tests__/ingest.test.ts +++ b/packages/job-worker/src/ingest/__tests__/ingest.test.ts @@ -47,6 +47,7 @@ import { UserErrorMessage } from '@sofie-automation/corelib/dist/error' import { PlayoutPartInstanceModel } from '../../playout/model/PlayoutPartInstanceModel' import { NrcsIngestCacheType } from '@sofie-automation/corelib/dist/dataModel/NrcsIngestDataCache' import { wrapGenericIngestJob, wrapGenericIngestJobWithPrecheck } from '../jobWrappers' +import { wrapDefaultObject } from '@sofie-automation/corelib/dist/settings/objectWithOverrides' const handleRemovedRundownWrapped = wrapGenericIngestJob(handleRemovedRundown) const handleUpdatedRundownWrapped = wrapGenericIngestJob(handleUpdatedRundown) @@ -121,11 +122,11 @@ describe('Test ingest actions for rundowns and segments', () => { const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, - settings: { + ...context.rawStudio, + settingsWithOverrides: wrapDefaultObject({ ...context.studio.settings, minimumTakeSpan: 0, - }, + }), supportedShowStyleBase: [showStyleCompound._id], }) diff --git a/packages/job-worker/src/ingest/__tests__/selectShowStyleVariant.test.ts b/packages/job-worker/src/ingest/__tests__/selectShowStyleVariant.test.ts index 2c6e7d8ce4..cb63251f09 100644 --- a/packages/job-worker/src/ingest/__tests__/selectShowStyleVariant.test.ts +++ b/packages/job-worker/src/ingest/__tests__/selectShowStyleVariant.test.ts @@ -35,7 +35,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) @@ -57,7 +57,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [], }) @@ -76,7 +76,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) @@ -118,7 +118,7 @@ describe('selectShowStyleVariant', () => { const showStyleCompoundVariant2 = await setupMockShowStyleVariant(context, showStyleCompound._id) const showStyleCompound2 = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id, showStyleCompound2._id], }) @@ -153,7 +153,7 @@ describe('selectShowStyleVariant', () => { test('no show style bases', async () => { const context = setupDefaultJobEnvironment() context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [protectString('fakeId')], }) @@ -176,7 +176,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) @@ -201,7 +201,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) @@ -226,7 +226,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) @@ -251,7 +251,7 @@ describe('selectShowStyleVariant', () => { const context = setupDefaultJobEnvironment() const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) diff --git a/packages/job-worker/src/ingest/mosDevice/__tests__/mosIngest.test.ts b/packages/job-worker/src/ingest/mosDevice/__tests__/mosIngest.test.ts index c96a18890d..d6e675ba1f 100644 --- a/packages/job-worker/src/ingest/mosDevice/__tests__/mosIngest.test.ts +++ b/packages/job-worker/src/ingest/mosDevice/__tests__/mosIngest.test.ts @@ -87,7 +87,7 @@ describe('Test recieved mos ingest payloads', () => { const showStyleCompound = await setupMockShowStyleCompound(context) context.setStudio({ - ...context.studio, + ...context.rawStudio, supportedShowStyleBase: [showStyleCompound._id], }) diff --git a/packages/job-worker/src/playout/__tests__/playout.test.ts b/packages/job-worker/src/playout/__tests__/playout.test.ts index 01c63b2d17..93ab3ef753 100644 --- a/packages/job-worker/src/playout/__tests__/playout.test.ts +++ b/packages/job-worker/src/playout/__tests__/playout.test.ts @@ -47,6 +47,7 @@ import { PlayoutChangedType } from '@sofie-automation/shared-lib/dist/peripheral import { ProcessedShowStyleCompound } from '../../jobs' import { handleOnPlayoutPlaybackChanged } from '../timings' import { sleep } from '@sofie-automation/shared-lib/dist/lib/lib' +import { wrapDefaultObject } from '@sofie-automation/corelib/dist/settings/objectWithOverrides' // const mockGetCurrentTime = jest.spyOn(lib, 'getCurrentTime') const mockExecutePeripheralDeviceFunction = jest @@ -98,11 +99,11 @@ describe('Playout API', () => { context = setupDefaultJobEnvironment() context.setStudio({ - ...context.studio, - settings: { + ...context.rawStudio, + settingsWithOverrides: wrapDefaultObject({ ...context.studio.settings, minimumTakeSpan: 0, - }, + }), }) // Ignore event jobs diff --git a/packages/job-worker/src/playout/lookahead/__tests__/lookahead.test.ts b/packages/job-worker/src/playout/lookahead/__tests__/lookahead.test.ts index 0256447f51..1a925310bb 100644 --- a/packages/job-worker/src/playout/lookahead/__tests__/lookahead.test.ts +++ b/packages/job-worker/src/playout/lookahead/__tests__/lookahead.test.ts @@ -51,7 +51,7 @@ describe('Lookahead', () => { } } context.setStudio({ - ...context.studio, + ...context.rawStudio, mappingsWithOverrides: wrapDefaultObject(mappings), }) @@ -222,7 +222,7 @@ describe('Lookahead', () => { // Set really low { - const studio = clone(context.studio) + const studio = clone(context.rawStudio) studio.mappingsWithOverrides.defaults['WHEN_CLEAR'].lookaheadMaxSearchDistance = 0 studio.mappingsWithOverrides.defaults['PRELOAD'].lookaheadMaxSearchDistance = 0 context.setStudio(studio) @@ -236,7 +236,7 @@ describe('Lookahead', () => { // really high getOrderedPartsAfterPlayheadMock.mockClear() { - const studio = clone(context.studio) + const studio = clone(context.rawStudio) studio.mappingsWithOverrides.defaults['WHEN_CLEAR'].lookaheadMaxSearchDistance = -1 studio.mappingsWithOverrides.defaults['PRELOAD'].lookaheadMaxSearchDistance = 2000 context.setStudio(studio) @@ -250,7 +250,7 @@ describe('Lookahead', () => { // unset getOrderedPartsAfterPlayheadMock.mockClear() { - const studio = clone(context.studio) + const studio = clone(context.rawStudio) studio.mappingsWithOverrides.defaults['WHEN_CLEAR'].lookaheadMaxSearchDistance = undefined studio.mappingsWithOverrides.defaults['PRELOAD'].lookaheadMaxSearchDistance = -1 context.setStudio(studio) diff --git a/packages/job-worker/src/playout/lookahead/__tests__/util.test.ts b/packages/job-worker/src/playout/lookahead/__tests__/util.test.ts index 4c3cc76bd8..5807e8b6c1 100644 --- a/packages/job-worker/src/playout/lookahead/__tests__/util.test.ts +++ b/packages/job-worker/src/playout/lookahead/__tests__/util.test.ts @@ -37,7 +37,7 @@ describe('getOrderedPartsAfterPlayhead', () => { } } context.setStudio({ - ...context.studio, + ...context.rawStudio, mappingsWithOverrides: wrapDefaultObject(mappings), }) diff --git a/packages/job-worker/src/workers/context/__tests__/StudioRouteSetUpdater.spec.ts b/packages/job-worker/src/workers/context/__tests__/StudioRouteSetUpdater.spec.ts index 77692f4072..e6ef0fe40d 100644 --- a/packages/job-worker/src/workers/context/__tests__/StudioRouteSetUpdater.spec.ts +++ b/packages/job-worker/src/workers/context/__tests__/StudioRouteSetUpdater.spec.ts @@ -6,11 +6,15 @@ import { wrapDefaultObject } from '@sofie-automation/corelib/dist/settings/objec function setupTest(routeSets: Record) { const context = setupDefaultJobEnvironment() - const mockCache: Pick = { - studio: { - ...context.studio, + const mockCache: Pick = { + rawStudio: { + ...context.rawStudio, routeSetsWithOverrides: wrapDefaultObject(routeSets), }, + jobStudio: { + ...context.studio, + routeSets: routeSets, + }, } const mockCollection = context.mockCollections.Studios const routeSetHelper = new StudioRouteSetUpdater(context.directCollections, mockCache) @@ -197,11 +201,13 @@ describe('StudioRouteSetUpdater', () => { routeSetHelper.setRouteSetActive('one', true) - expect(routeSetHelper.studioWithChanges).toBeTruthy() + expect(routeSetHelper.rawStudioWithChanges).toBeTruthy() + expect(routeSetHelper.jobStudioWithChanges).toBeTruthy() routeSetHelper.discardRouteSetChanges() - expect(routeSetHelper.studioWithChanges).toBeFalsy() + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() expect(mockCollection.operations).toHaveLength(0) await routeSetHelper.saveRouteSetChanges() @@ -211,54 +217,70 @@ describe('StudioRouteSetUpdater', () => { it('save should update mockCache', async () => { const { mockCache, mockCollection, routeSetHelper } = setupTest(SINGLE_ROUTESET) - const studioBefore = mockCache.studio - expect(routeSetHelper.studioWithChanges).toBeFalsy() + const rawStudioBefore = mockCache.rawStudio + const jobStudioBefore = mockCache.jobStudio + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() routeSetHelper.setRouteSetActive('one', true) - expect(routeSetHelper.studioWithChanges).toBeTruthy() + expect(routeSetHelper.rawStudioWithChanges).toBeTruthy() + expect(routeSetHelper.jobStudioWithChanges).toBeTruthy() expect(mockCollection.operations).toHaveLength(0) await routeSetHelper.saveRouteSetChanges() expect(mockCollection.operations).toHaveLength(1) // Object should have changed - expect(mockCache.studio).not.toBe(studioBefore) + expect(mockCache.rawStudio).not.toBe(rawStudioBefore) + expect(mockCache.jobStudio).not.toBe(jobStudioBefore) // Object should not be equal - expect(mockCache.studio).not.toEqual(studioBefore) - expect(routeSetHelper.studioWithChanges).toBeFalsy() + expect(mockCache.rawStudio).not.toEqual(rawStudioBefore) + expect(mockCache.jobStudio).not.toEqual(jobStudioBefore) + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() }) it('no changes should not update mockCache', async () => { const { mockCache, mockCollection, routeSetHelper } = setupTest(SINGLE_ROUTESET) - const studioBefore = mockCache.studio - expect(routeSetHelper.studioWithChanges).toBeFalsy() + const rawStudioBefore = mockCache.rawStudio + const jobStudioBefore = mockCache.jobStudio + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() expect(mockCollection.operations).toHaveLength(0) await routeSetHelper.saveRouteSetChanges() expect(mockCollection.operations).toHaveLength(0) - expect(mockCache.studio).toBe(studioBefore) - expect(routeSetHelper.studioWithChanges).toBeFalsy() + expect(mockCache.rawStudio).toBe(rawStudioBefore) + expect(mockCache.jobStudio).toBe(jobStudioBefore) + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() }) it('discard changes should not update mockCache', async () => { const { mockCache, mockCollection, routeSetHelper } = setupTest(SINGLE_ROUTESET) - const studioBefore = mockCache.studio - expect(routeSetHelper.studioWithChanges).toBeFalsy() + const rawStudioBefore = mockCache.rawStudio + const jobStudioBefore = mockCache.jobStudio + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() routeSetHelper.setRouteSetActive('one', true) - expect(routeSetHelper.studioWithChanges).toBeTruthy() + expect(routeSetHelper.rawStudioWithChanges).toBeTruthy() + expect(routeSetHelper.jobStudioWithChanges).toBeTruthy() routeSetHelper.discardRouteSetChanges() - expect(routeSetHelper.studioWithChanges).toBeFalsy() + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() expect(mockCollection.operations).toHaveLength(0) await routeSetHelper.saveRouteSetChanges() expect(mockCollection.operations).toHaveLength(0) - expect(mockCache.studio).toBe(studioBefore) - expect(routeSetHelper.studioWithChanges).toBeFalsy() + expect(mockCache.rawStudio).toBe(rawStudioBefore) + expect(mockCache.jobStudio).toBe(jobStudioBefore) + expect(routeSetHelper.rawStudioWithChanges).toBeFalsy() + expect(routeSetHelper.jobStudioWithChanges).toBeFalsy() }) it('ACTIVATE_ONLY routeset can be activated', async () => {