From cb9e1e4fc5eae054aa69faf92cea056dffc647ee Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Wed, 9 Oct 2024 12:04:47 -0400 Subject: [PATCH] Add tests for segment utils --- .../playback/utils/mediaSegments.test.ts | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/apps/stable/features/playback/utils/mediaSegments.test.ts diff --git a/src/apps/stable/features/playback/utils/mediaSegments.test.ts b/src/apps/stable/features/playback/utils/mediaSegments.test.ts new file mode 100644 index 000000000000..17dc9e73834e --- /dev/null +++ b/src/apps/stable/features/playback/utils/mediaSegments.test.ts @@ -0,0 +1,68 @@ +import type { MediaSegmentDto } from '@jellyfin/sdk/lib/generated-client/models/media-segment-dto'; +import { MediaSegmentType } from '@jellyfin/sdk/lib/generated-client/models/media-segment-type'; +import { describe, expect, it } from 'vitest'; + +import { findCurrentSegment } from './mediaSegments'; + +const TEST_SEGMENTS: MediaSegmentDto[] = [ + { + Id: 'intro', + Type: MediaSegmentType.Intro, + StartTicks: 0, + EndTicks: 10 + }, + { + Id: 'preview', + Type: MediaSegmentType.Preview, + StartTicks: 20, + EndTicks: 30 + }, + { + Id: 'recap', + Type: MediaSegmentType.Recap, + StartTicks: 30, + EndTicks: 40 + }, + { + Id: 'commercial', + Type: MediaSegmentType.Commercial, + StartTicks: 40, + EndTicks: 50 + }, + { + Id: 'outro', + Type: MediaSegmentType.Outro, + StartTicks: 50, + EndTicks: 60 + } +]; + +describe('findCurrentSegment()', () => { + it('Should return the current segment', () => { + let segmentDetails = findCurrentSegment(TEST_SEGMENTS, 23); + expect(segmentDetails).toBeDefined(); + expect(segmentDetails?.index).toBe(1); + expect(segmentDetails?.segment?.Id).toBe('preview'); + + segmentDetails = findCurrentSegment(TEST_SEGMENTS, 5, 1); + expect(segmentDetails).toBeDefined(); + expect(segmentDetails?.index).toBe(0); + expect(segmentDetails?.segment?.Id).toBe('intro'); + + segmentDetails = findCurrentSegment(TEST_SEGMENTS, 42, 3); + expect(segmentDetails).toBeDefined(); + expect(segmentDetails?.index).toBe(3); + expect(segmentDetails?.segment?.Id).toBe('commercial'); + }); + + it('Should return undefined if not in a segment', () => { + let segmentDetails = findCurrentSegment(TEST_SEGMENTS, 16); + expect(segmentDetails).toBeUndefined(); + + segmentDetails = findCurrentSegment(TEST_SEGMENTS, 10, 1); + expect(segmentDetails).toBeUndefined(); + + segmentDetails = findCurrentSegment(TEST_SEGMENTS, 100); + expect(segmentDetails).toBeUndefined(); + }); +});