Skip to content

A self-hosted media server and file management system

feat: storyboard tests

aminnausin8878d36740a4
diff --git a/resources/js/components/video/PlayerStoryboard.vue b/resources/js/components/video/PlayerStoryboard.vue
index 15fe0f93..1646364e 100644
--- a/resources/js/components/video/PlayerStoryboard.vue
+++ b/resources/js/components/video/PlayerStoryboard.vue
@@ -1,23 +1,15 @@
<script setup lang="ts">
import type { StoryboardResource } from '@/contracts/media';
import type { HTMLAttributes } from 'vue';
+import type { StoryboardCue } from '@/service/storyboard/types';
+import { buildStoryboardCues } from '@/service/storyboard';
import { useContentStore } from '@/stores/ContentStore';
import { useBreakpoints } from '@vueuse/core';
import { storeToRefs } from 'pinia';
import { computed } from 'vue';
import { cn } from '@aminnausin/cedar-ui';
-interface ThumbnailCue {
- start: number;
- end: number;
- image: string;
- x: number;
- y: number;
- w: number;
- h: number;
-}
-
const { stateVideo } = storeToRefs(useContentStore());
const breakpoints = useBreakpoints({ xs: 320 });
@@ -29,7 +21,7 @@ const props = defineProps<{
const storyboard = computed<StoryboardResource | undefined>(() => stateVideo.value.storyboard);
-const thumbnailCues = computed<ThumbnailCue[]>(() => {
+const storyboardCues = computed<StoryboardCue[]>(() => {
const uuid = stateVideo.value.metadata?.uuid;
const durationSeconds = stateVideo.value.duration;
@@ -37,33 +29,10 @@ const thumbnailCues = computed<ThumbnailCue[]>(() => {
return [];
}
- const { tile_rows: rows, tile_cols: cols, tile_height: tileHeight, tile_width: tileWidth, interval_seconds: interval } = storyboard.value;
-
- const tilesPerSheet = rows * cols;
- const cues = [];
-
- for (let t = 0; t < durationSeconds; t += interval) {
- const index = Math.floor(t / interval);
- const sheet = Math.floor(index / tilesPerSheet) + 1;
- const tileIndex = index % tilesPerSheet;
- const col = tileIndex % cols;
- const row = Math.floor(tileIndex / cols);
-
- cues.push({
- start: t,
- end: Math.min(t + interval, durationSeconds),
- image: `/storage/metadata/${uuid.slice(0, 2)}/${uuid}/storyboard/${sheet}.jpg`,
- x: col * tileWidth,
- y: row * tileHeight,
- w: tileWidth,
- h: tileHeight,
- });
- }
-
- return cues;
+ return buildStoryboardCues(uuid, durationSeconds, storyboard.value);
});
-const activeCue = computed<ThumbnailCue | undefined>(() => thumbnailCues.value.find((c) => props.tooltipTime >= c.start && props.tooltipTime < c.end) ?? thumbnailCues.value[0]);
+const activeCue = computed<StoryboardCue | undefined>(() => storyboardCues.value.find((c) => props.tooltipTime >= c.start && props.tooltipTime < c.end) ?? storyboardCues.value[0]);
const spriteStyle = computed<HTMLAttributes['style']>(() => {
const c = activeCue.value;
diff --git a/resources/js/service/__test__/generateStoryboardCues.test.ts b/resources/js/service/__test__/generateStoryboardCues.test.ts
new file mode 100644
index 00000000..27c3ff40
--- /dev/null
+++ b/resources/js/service/__test__/generateStoryboardCues.test.ts
@@ -0,0 +1,68 @@
+import type { StoryboardResource } from '@/contracts/media';
+
+import { describe, it, expect } from 'vitest';
+import { buildStoryboardCues } from '@/service/storyboard';
+
+const makeStoryboard = (overrides: Partial<StoryboardResource> = {}): StoryboardResource => ({
+ tile_rows: 10,
+ tile_cols: 10,
+ tile_width: 320,
+ tile_height: 180,
+ interval_seconds: 10,
+ tile_count: 0, // Irrelevant
+ ...overrides,
+});
+
+const UUID = 'abc12345-uuid';
+
+describe('buildStoryboardCues', () => {
+ it('generates correct number of cues', () => {
+ const cues = buildStoryboardCues(UUID, 100, makeStoryboard());
+ expect(cues.length).toBe(10);
+ });
+
+ it('first cue starts at 0', () => {
+ const cues = buildStoryboardCues(UUID, 100, makeStoryboard());
+ expect(cues[0].start).toBe(0);
+ });
+
+ it('last cue end does not exceed duration', () => {
+ const cues = buildStoryboardCues(UUID, 95, makeStoryboard());
+ expect(cues[cues.length - 1].end).toBeLessThanOrEqual(95);
+ });
+
+ it('first tile has x=0 y=0', () => {
+ const cues = buildStoryboardCues(UUID, 100, makeStoryboard());
+ expect(cues[0].x).toBe(0);
+ expect(cues[0].y).toBe(0);
+ });
+
+ it('second tile has correct x offset', () => {
+ const cues = buildStoryboardCues(UUID, 200, makeStoryboard());
+ expect(cues[1].x).toBe(320);
+ expect(cues[1].y).toBe(0);
+ });
+
+ it('eleventh tile wraps to second row', () => {
+ const cues = buildStoryboardCues(UUID, 200, makeStoryboard());
+ expect(cues[10].x).toBe(0);
+ expect(cues[10].y).toBe(180);
+ });
+
+ it('rolls over to next sheet after 100 tiles', () => {
+ const cues = buildStoryboardCues(UUID, 1100, makeStoryboard());
+ expect(cues[99].image).toContain('/1.jpg');
+ expect(cues[100].image).toContain('/2.jpg');
+ });
+
+ it('image path uses first two chars of uuid', () => {
+ const cues = buildStoryboardCues(UUID, 10, makeStoryboard());
+ expect(cues[0].image).toContain('/ab/');
+ });
+
+ it('cue dimensions match storyboard tile size', () => {
+ const cues = buildStoryboardCues(UUID, 10, makeStoryboard());
+ expect(cues[0].w).toBe(320);
+ expect(cues[0].h).toBe(180);
+ });
+});
diff --git a/resources/js/service/storyboard/index.ts b/resources/js/service/storyboard/index.ts
new file mode 100644
index 00000000..ffcfaf7c
--- /dev/null
+++ b/resources/js/service/storyboard/index.ts
@@ -0,0 +1,29 @@
+import type { StoryboardResource } from '@/contracts/media';
+import type { StoryboardCue } from './types';
+
+export function buildStoryboardCues(uuid: string, durationSeconds: number, storyboard: StoryboardResource): StoryboardCue[] {
+ const { tile_rows: rows, tile_cols: cols, tile_height: tileHeight, tile_width: tileWidth, interval_seconds: interval } = storyboard;
+
+ const tilesPerSheet = rows * cols;
+ const cues: StoryboardCue[] = [];
+
+ for (let t = 0; t < durationSeconds; t += interval) {
+ const index = Math.floor(t / interval);
+ const sheet = Math.floor(index / tilesPerSheet) + 1;
+ const tileIndex = index % tilesPerSheet;
+ const col = tileIndex % cols;
+ const row = Math.floor(tileIndex / cols);
+
+ cues.push({
+ start: t,
+ end: Math.min(t + interval, durationSeconds),
+ image: `/storage/metadata/${uuid.slice(0, 2)}/${uuid}/storyboard/${sheet}.jpg`,
+ x: col * tileWidth,
+ y: row * tileHeight,
+ w: tileWidth,
+ h: tileHeight,
+ });
+ }
+
+ return cues;
+}
diff --git a/resources/js/service/storyboard/types.ts b/resources/js/service/storyboard/types.ts
new file mode 100644
index 00000000..00d759a3
--- /dev/null
+++ b/resources/js/service/storyboard/types.ts
@@ -0,0 +1,9 @@
+export interface StoryboardCue {
+ start: number;
+ end: number;
+ image: string;
+ x: number;
+ y: number;
+ w: number;
+ h: number;
+}
diff --git a/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.php b/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.php
new file mode 100644
index 00000000..53fb4e12
--- /dev/null
+++ b/tests/Unit/Services/FFmpeg/FFmpegCommandBuilderTest.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Tests\Unit\Services\FFmpeg;
+
+use App\Enums\HardwareType;
+use App\Services\Ffmpeg\FFmpegCommandBuilder;
+use App\Services\Hardware\HardwareDetectionService;
+use App\Services\Hardware\HardwareProfile;
+use App\Services\Images\Storyboard\StoryboardOptions;
+use Tests\TestCase;
+
+class FFmpegCommandBuilderTest extends TestCase {
+ private function makeBuilder(HardwareType $type): FFmpegCommandBuilder {
+ $profile = new HardwareProfile(
+ cuda: $type === HardwareType::CUDA,
+ qsv: $type === HardwareType::QSV,
+ vaapi: $type === HardwareType::VAAPI,
+ );
+
+ $detector = $this->createMock(HardwareDetectionService::class);
+ $detector->method('detect')->willReturn($profile);
+
+ return new FFmpegCommandBuilder($detector);
+ }
+
+ private function makeOptions(): StoryboardOptions {
+ return new StoryboardOptions(
+ width: 320,
+ height: 180,
+ cols: 10,
+ rows: 10,
+ fps: 0.1,
+ tile: true,
+ sourceWidth: 1920,
+ sourceHeight: 1080,
+ duration: 600,
+ );
+ }
+
+ public function test_cpu_command_has_no_hwaccel(): void {
+ $command = $this->makeBuilder(HardwareType::CPU)->storyboard('/input.mkv', '/output/%d.jpg', $this->makeOptions());
+
+ $this->assertNotContains('-hwaccel', $command);
+ $this->assertContains('mjpeg', $command);
+ }
+
+ public function test_cuda_command_has_hwaccel(): void {
+ $command = $this->makeBuilder(HardwareType::CUDA)
+ ->storyboard('/input.mkv', '/output/%d.jpg', $this->makeOptions());
+
+ $this->assertContains('-hwaccel', $command);
+ $this->assertContains('cuda', $command);
+ }
+
+ public function test_qsv_command_uses_mjpeg_qsv_encoder(): void {
+ $command = $this->makeBuilder(HardwareType::QSV)
+ ->storyboard('/input.mkv', '/output/%d.jpg', $this->makeOptions());
+
+ $this->assertContains('mjpeg_qsv', $command);
+ $this->assertContains('-global_quality:v', $command);
+ $this->assertNotContains('-q:v', $command);
+ }
+
+ public function test_command_contains_skip_frame_nokey(): void {
+ $command = $this->makeBuilder(HardwareType::CPU)
+ ->storyboard('/input.mkv', '/output/%d.jpg', $this->makeOptions());
+
+ $this->assertContains('-skip_frame', $command);
+ $this->assertContains('nokey', $command);
+ }
+
+ public function test_command_contains_correct_fps(): void {
+ $command = $this->makeBuilder(HardwareType::CPU)
+ ->storyboard('/input.mkv', '/output/%d.jpg', $this->makeOptions());
+
+ $vfIndex = array_search('-vf', $command);
+ $this->assertStringContainsString('fps=0.1', $command[$vfIndex + 1]);
+ }
+
+ public function test_command_contains_tile_filter(): void {
+ $command = $this->makeBuilder(HardwareType::CPU)
+ ->storyboard('/input.mkv', '/output/%d.jpg', $this->makeOptions());
+
+ $vfIndex = array_search('-vf', $command);
+ $this->assertStringContainsString('tile=10x10', $command[$vfIndex + 1]);
+ }
+}
diff --git a/tests/Unit/Services/Storyboard/StoryboardOptionsTest.php b/tests/Unit/Services/Images/StoryboardOptionsTest.php
similarity index 97%
rename from tests/Unit/Services/Storyboard/StoryboardOptionsTest.php
rename to tests/Unit/Services/Images/StoryboardOptionsTest.php
index 5d0cc221..1e3fa1a7 100644
--- a/tests/Unit/Services/Storyboard/StoryboardOptionsTest.php
+++ b/tests/Unit/Services/Images/StoryboardOptionsTest.php
@@ -1,6 +1,6 @@
<?php
-namespace Tests\Unit\Services\Storyboard;
+namespace Tests\Unit\Services\Images;
use App\Exceptions\StoryboardNotSupportedException;
use App\Models\Metadata;