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
77 changes: 77 additions & 0 deletions meteor/server/api/rest/v1/__tests__/playlists.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { registerRoutes } from '../playlists'
import { ClientAPI } from '@sofie-automation/meteor-lib/dist/api/client'
import { protectString } from '@sofie-automation/corelib/dist/protectedString'
import { PlaylistsRestAPI } from '../../../../lib/rest/v1'

describe('Playlists REST API Routes', () => {
let mockRegisterRoute: jest.Mock
let mockServerAPI: jest.Mocked<PlaylistsRestAPI>

beforeEach(() => {
mockRegisterRoute = jest.fn()
mockServerAPI = {
tTimerStartCountdown: jest.fn().mockResolvedValue(ClientAPI.responseSuccess(undefined)),
tTimerStartFreeRun: jest.fn().mockResolvedValue(ClientAPI.responseSuccess(undefined)),
tTimerPause: jest.fn().mockResolvedValue(ClientAPI.responseSuccess(undefined)),
tTimerResume: jest.fn().mockResolvedValue(ClientAPI.responseSuccess(undefined)),
tTimerRestart: jest.fn().mockResolvedValue(ClientAPI.responseSuccess(undefined)),
} as any

registerRoutes(mockRegisterRoute)
})

test('should register T-timer countdown route', () => {
const countdownRoute = mockRegisterRoute.mock.calls.find(
(call) => call[1] === '/playlists/:playlistId/t-timers/:timerIndex/countdown'
)
expect(countdownRoute).toBeDefined()
expect(countdownRoute[0]).toBe('post')
})

test('T-timer countdown handler should call serverAPI.tTimerStartCountdown', async () => {
const countdownRoute = mockRegisterRoute.mock.calls.find(
(call) => call[1] === '/playlists/:playlistId/t-timers/:timerIndex/countdown'
)
const handler = countdownRoute[4]

const params = { playlistId: 'playlist0', timerIndex: '1' }
const body = { duration: 60, stopAtZero: true, startPaused: false }
const connection = {} as any
const event = 'test-event'

await handler(mockServerAPI, connection, event, params, body)

expect(mockServerAPI.tTimerStartCountdown).toHaveBeenCalledWith(
connection,
event,
protectString('playlist0'),
1,
60,
true,
false
)
})

test('should register T-timer pause route', () => {
const pauseRoute = mockRegisterRoute.mock.calls.find(
(call) => call[1] === '/playlists/:playlistId/t-timers/:timerIndex/pause'
)
expect(pauseRoute).toBeDefined()
expect(pauseRoute[0]).toBe('post')
})

test('T-timer pause handler should call serverAPI.tTimerPause', async () => {
const pauseRoute = mockRegisterRoute.mock.calls.find(
(call) => call[1] === '/playlists/:playlistId/t-timers/:timerIndex/pause'
)
const handler = pauseRoute[4]

const params = { playlistId: 'playlist0', timerIndex: '2' }
const connection = {} as any
const event = 'test-event'

await handler(mockServerAPI, connection, event, params, {})

expect(mockServerAPI.tTimerPause).toHaveBeenCalledWith(connection, event, protectString('playlist0'), 2)
})
})
226 changes: 226 additions & 0 deletions meteor/server/api/rest/v1/playlists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
RundownPlaylistId,
SegmentId,
} from '@sofie-automation/corelib/dist/dataModel/Ids'
import { RundownTTimerIndex } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
import { Match, check } from '../../../lib/check'
import { PlaylistsRestAPI } from '../../../lib/rest/v1'
import { Meteor } from 'meteor/meteor'
Expand Down Expand Up @@ -544,6 +545,133 @@ class PlaylistsServerAPI implements PlaylistsRestAPI {
}
)
}

async tTimerStartCountdown(
connection: Meteor.Connection,
event: string,
rundownPlaylistId: RundownPlaylistId,
timerIndex: RundownTTimerIndex,
duration: number,
stopAtZero?: boolean,
startPaused?: boolean
): Promise<ClientAPI.ClientResponse<void>> {
return ServerClientAPI.runUserActionInLogForPlaylistOnWorker(
this.context.getMethodContext(connection),
event,
getCurrentTime(),
rundownPlaylistId,
() => {
check(rundownPlaylistId, String)
check(timerIndex, Number)
check(duration, Number)
check(stopAtZero, Match.Optional(Boolean))
check(startPaused, Match.Optional(Boolean))
},
StudioJobs.TTimerStartCountdown,
{
playlistId: rundownPlaylistId,
timerIndex,
duration,
stopAtZero: !!stopAtZero,
startPaused: !!startPaused,
}
)
}

async tTimerStartFreeRun(
connection: Meteor.Connection,
event: string,
rundownPlaylistId: RundownPlaylistId,
timerIndex: RundownTTimerIndex,
startPaused?: boolean
): Promise<ClientAPI.ClientResponse<void>> {
return ServerClientAPI.runUserActionInLogForPlaylistOnWorker(
this.context.getMethodContext(connection),
event,
getCurrentTime(),
rundownPlaylistId,
() => {
check(rundownPlaylistId, String)
check(timerIndex, Number)
check(startPaused, Match.Optional(Boolean))
},
StudioJobs.TTimerStartFreeRun,
{
playlistId: rundownPlaylistId,
timerIndex,
startPaused: !!startPaused,
}
)
}

async tTimerPause(
connection: Meteor.Connection,
event: string,
rundownPlaylistId: RundownPlaylistId,
timerIndex: RundownTTimerIndex
): Promise<ClientAPI.ClientResponse<void>> {
return ServerClientAPI.runUserActionInLogForPlaylistOnWorker(
this.context.getMethodContext(connection),
event,
getCurrentTime(),
rundownPlaylistId,
() => {
check(rundownPlaylistId, String)
check(timerIndex, Number)
},
StudioJobs.TTimerPause,
{
playlistId: rundownPlaylistId,
timerIndex,
}
)
}

async tTimerResume(
connection: Meteor.Connection,
event: string,
rundownPlaylistId: RundownPlaylistId,
timerIndex: RundownTTimerIndex
): Promise<ClientAPI.ClientResponse<void>> {
return ServerClientAPI.runUserActionInLogForPlaylistOnWorker(
this.context.getMethodContext(connection),
event,
getCurrentTime(),
rundownPlaylistId,
() => {
check(rundownPlaylistId, String)
check(timerIndex, Number)
},
StudioJobs.TTimerResume,
{
playlistId: rundownPlaylistId,
timerIndex,
}
)
}

async tTimerRestart(
connection: Meteor.Connection,
event: string,
rundownPlaylistId: RundownPlaylistId,
timerIndex: RundownTTimerIndex
): Promise<ClientAPI.ClientResponse<void>> {
return ServerClientAPI.runUserActionInLogForPlaylistOnWorker(
this.context.getMethodContext(connection),
event,
getCurrentTime(),
rundownPlaylistId,
() => {
check(rundownPlaylistId, String)
check(timerIndex, Number)
},
StudioJobs.TTimerRestart,
{
playlistId: rundownPlaylistId,
timerIndex,
}
)
}
}

class PlaylistsAPIFactory implements APIFactory<PlaylistsRestAPI> {
Expand Down Expand Up @@ -877,4 +1005,102 @@ export function registerRoutes(registerRoute: APIRegisterHook<PlaylistsRestAPI>)
return await serverAPI.recallStickyPiece(connection, event, playlistId, sourceLayerId)
}
)

registerRoute<
{ playlistId: string; timerIndex: string },
{ duration: number; stopAtZero?: boolean; startPaused?: boolean },
void
>(
'post',
'/playlists/:playlistId/t-timers/:timerIndex/countdown',
new Map([[404, [UserErrorMessage.RundownPlaylistNotFound]]]),
playlistsAPIFactory,
async (serverAPI, connection, event, params, body) => {
const rundownPlaylistId = protectString<RundownPlaylistId>(params.playlistId)
const timerIndex = Number.parseInt(params.timerIndex) as RundownTTimerIndex
logger.info(`API POST: t-timer countdown ${rundownPlaylistId} ${timerIndex}`)

check(rundownPlaylistId, String)
check(timerIndex, Number)
return await serverAPI.tTimerStartCountdown(
connection,
event,
rundownPlaylistId,
timerIndex,
body.duration,
body.stopAtZero,
body.startPaused
)
}
)

registerRoute<{ playlistId: string; timerIndex: string }, { startPaused?: boolean }, void>(
'post',
'/playlists/:playlistId/t-timers/:timerIndex/free-run',
new Map([[404, [UserErrorMessage.RundownPlaylistNotFound]]]),
playlistsAPIFactory,
async (serverAPI, connection, event, params, body) => {
const rundownPlaylistId = protectString<RundownPlaylistId>(params.playlistId)
const timerIndex = Number.parseInt(params.timerIndex) as RundownTTimerIndex
logger.info(`API POST: t-timer free-run ${rundownPlaylistId} ${timerIndex}`)

check(rundownPlaylistId, String)
check(timerIndex, Number)
return await serverAPI.tTimerStartFreeRun(
connection,
event,
rundownPlaylistId,
timerIndex,
body.startPaused
)
}
)

registerRoute<{ playlistId: string; timerIndex: string }, never, void>(
'post',
'/playlists/:playlistId/t-timers/:timerIndex/pause',
new Map([[404, [UserErrorMessage.RundownPlaylistNotFound]]]),
playlistsAPIFactory,
async (serverAPI, connection, event, params, _) => {
const rundownPlaylistId = protectString<RundownPlaylistId>(params.playlistId)
const timerIndex = Number.parseInt(params.timerIndex) as RundownTTimerIndex
logger.info(`API POST: t-timer pause ${rundownPlaylistId} ${timerIndex}`)

check(rundownPlaylistId, String)
check(timerIndex, Number)
return await serverAPI.tTimerPause(connection, event, rundownPlaylistId, timerIndex)
}
)

registerRoute<{ playlistId: string; timerIndex: string }, never, void>(
'post',
'/playlists/:playlistId/t-timers/:timerIndex/resume',
new Map([[404, [UserErrorMessage.RundownPlaylistNotFound]]]),
playlistsAPIFactory,
async (serverAPI, connection, event, params, _) => {
const rundownPlaylistId = protectString<RundownPlaylistId>(params.playlistId)
const timerIndex = Number.parseInt(params.timerIndex) as RundownTTimerIndex
logger.info(`API POST: t-timer resume ${rundownPlaylistId} ${timerIndex}`)

check(rundownPlaylistId, String)
check(timerIndex, Number)
return await serverAPI.tTimerResume(connection, event, rundownPlaylistId, timerIndex)
}
)

registerRoute<{ playlistId: string; timerIndex: string }, never, void>(
'post',
'/playlists/:playlistId/t-timers/:timerIndex/restart',
new Map([[404, [UserErrorMessage.RundownPlaylistNotFound]]]),
playlistsAPIFactory,
async (serverAPI, connection, event, params, _) => {
const rundownPlaylistId = protectString<RundownPlaylistId>(params.playlistId)
const timerIndex = Number.parseInt(params.timerIndex) as RundownTTimerIndex
logger.info(`API POST: t-timer restart ${rundownPlaylistId} ${timerIndex}`)

check(rundownPlaylistId, String)
check(timerIndex, Number)
return await serverAPI.tTimerRestart(connection, event, rundownPlaylistId, timerIndex)
}
)
}
Loading
Loading