|
| 1 | +import { SaveableChannel } from '@tunarr/types'; |
| 2 | +import { FastifyInstance } from 'fastify'; |
| 3 | +import { afterAll, beforeAll, describe, expect, test } from 'vitest'; |
| 4 | +import { v4 } from 'uuid'; |
| 5 | +import { container } from '../src/container.ts'; |
| 6 | +import { ChannelDB } from '../src/db/ChannelDB.ts'; |
| 7 | +import { TranscodeConfigDB } from '../src/db/TranscodeConfigDB.ts'; |
| 8 | +import { KEYS } from '../src/types/inject.ts'; |
| 9 | +import { getAvailablePort } from '../src/util/net.ts'; |
| 10 | +import { initTestApp } from './testServer.js'; |
| 11 | + |
| 12 | +let app: FastifyInstance; |
| 13 | +let validTranscodeConfigId: string; |
| 14 | + |
| 15 | +const NON_EXISTENT_UUID = '00000000-0000-0000-0000-000000000000'; |
| 16 | + |
| 17 | +function makeChannelPayload( |
| 18 | + transcodeConfigId: string, |
| 19 | +): Partial<SaveableChannel> { |
| 20 | + return { |
| 21 | + name: 'Test Channel', |
| 22 | + number: 8001, |
| 23 | + duration: 60000, |
| 24 | + groupTitle: 'test', |
| 25 | + guideMinimumDuration: 30000, |
| 26 | + icon: { |
| 27 | + path: '', |
| 28 | + width: 0, |
| 29 | + duration: 0, |
| 30 | + position: 'bottom-right', |
| 31 | + }, |
| 32 | + id: NON_EXISTENT_UUID, |
| 33 | + startTime: 0, |
| 34 | + stealth: false, |
| 35 | + offline: { mode: 'pic' }, |
| 36 | + streamMode: 'hls', |
| 37 | + transcodeConfigId, |
| 38 | + disableFillerOverlay: false, |
| 39 | + subtitlesEnabled: false, |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +beforeAll(async () => { |
| 44 | + app = await initTestApp(await getAvailablePort()); |
| 45 | + const transcodeConfigDB = container.get(TranscodeConfigDB); |
| 46 | + const defaultConfig = await transcodeConfigDB.getDefaultConfig(); |
| 47 | + if (!defaultConfig) { |
| 48 | + throw new Error('Default transcode config not found after bootstrap'); |
| 49 | + } |
| 50 | + validTranscodeConfigId = defaultConfig.uuid; |
| 51 | +}); |
| 52 | + |
| 53 | +afterAll(async () => { |
| 54 | + await app?.close(); |
| 55 | +}); |
| 56 | + |
| 57 | +async function createChannel(): Promise<string> { |
| 58 | + const channelDB = container.get<ChannelDB>(KEYS.ChannelDB); |
| 59 | + const result = await channelDB.saveChannel({ |
| 60 | + ...makeChannelPayload(validTranscodeConfigId), |
| 61 | + name: 'Programming Validation Channel', |
| 62 | + } as SaveableChannel); |
| 63 | + return result.channel.uuid; |
| 64 | +} |
| 65 | + |
| 66 | +describe('POST /channels/:id/programming - slot group validation on the save path', () => { |
| 67 | + test('rejects a schedule whose slots share an iterationGroup with mismatched ordering', async () => { |
| 68 | + const channelId = await createChannel(); |
| 69 | + const groupId = v4(); |
| 70 | + |
| 71 | + const res = await app.inject({ |
| 72 | + method: 'POST', |
| 73 | + url: `/api/channels/${channelId}/programming`, |
| 74 | + payload: { |
| 75 | + type: 'time', |
| 76 | + programs: [], |
| 77 | + schedule: { |
| 78 | + type: 'time', |
| 79 | + flexPreference: 'distribute', |
| 80 | + latenessMs: 0, |
| 81 | + maxDays: 1, |
| 82 | + padMs: 0, |
| 83 | + period: 'day', |
| 84 | + timeZoneOffset: 0, |
| 85 | + slots: [ |
| 86 | + { |
| 87 | + type: 'movie', |
| 88 | + id: v4(), |
| 89 | + startTime: 0, |
| 90 | + order: 'next', |
| 91 | + direction: 'asc', |
| 92 | + iterationGroup: groupId, |
| 93 | + }, |
| 94 | + { |
| 95 | + type: 'movie', |
| 96 | + id: v4(), |
| 97 | + startTime: 0, |
| 98 | + order: 'shuffle', |
| 99 | + direction: 'asc', |
| 100 | + iterationGroup: groupId, |
| 101 | + }, |
| 102 | + ], |
| 103 | + }, |
| 104 | + }, |
| 105 | + }); |
| 106 | + |
| 107 | + // The two slots share an iterationGroup but disagree on ordering — the |
| 108 | + // schedule must be rejected on the save path just like the preview |
| 109 | + // endpoints reject it, instead of being persisted and regenerated badly. |
| 110 | + expect(res.statusCode).toBe(400); |
| 111 | + expect(res.body).toContain('mismatched orderings'); |
| 112 | + }); |
| 113 | +}); |
0 commit comments