Skip to content

A self-hosted media server and file management system

feat(testing): subtitle scanner test

aminnausineefec6cffb0b
diff --git a/tests/Unit/Services/Subtitles/SubtitlePathTest.php b/tests/Unit/Services/Subtitles/SubtitlePathTest.php
index ab09da89..67d06474 100644
--- a/tests/Unit/Services/Subtitles/SubtitlePathTest.php
+++ b/tests/Unit/Services/Subtitles/SubtitlePathTest.php
@@ -2,8 +2,6 @@
namespace Tests\Unit\Services\Subtitles;
-use App\Models\Metadata;
-use App\Models\Subtitle;
use App\Services\Subtitles\SubtitlePath;
use Illuminate\Support\Str;
use Tests\TestCase;
@@ -11,7 +9,6 @@ use Tests\TestCase;
class SubtitlePathTest extends TestCase {
public function test_get_file_path_returns_correct_path() {
$uuid = Str::uuid()->toString();
-
$path = SubtitlePath::file($uuid, 2, 'vtt');
$this->assertEquals('metadata/media/' . substr($uuid, 0, 2) . "/{$uuid}/subtitles/2.vtt", $path);
diff --git a/tests/Unit/Services/Subtitles/SubtitleScannerTest.php b/tests/Unit/Services/Subtitles/SubtitleScannerTest.php
new file mode 100644
index 00000000..80496e3f
--- /dev/null
+++ b/tests/Unit/Services/Subtitles/SubtitleScannerTest.php
@@ -0,0 +1,83 @@
+<?php
+
+namespace Tests\Unit\Services\Subtitles;
+
+use App\Services\Subtitles\SubtitleScanner;
+use Illuminate\Support\Facades\Log;
+use Tests\TestCase;
+
+class SubtitleScannerTest extends TestCase {
+ private SubtitleScanner $scanner;
+
+ protected function setUp(): void {
+ parent::setUp();
+ $this->scanner = new SubtitleScanner;
+ }
+
+ public function test_extracts_only_subtitle_streams_from_metadata() {
+ $metadata = [
+ 'streams' => [
+ ['codec_type' => 'video', 'index' => 0],
+ ['codec_type' => 'subtitle', 'index' => 2, 'codec_name' => 'subrip'],
+ ['codec_type' => 'audio', 'index' => 1],
+ ],
+ ];
+
+ $streams = $this->scanner->extractSubtitleStreams($metadata);
+
+ $this->assertCount(1, $streams);
+ foreach ($streams as $stream) {
+ $this->assertSame('subtitle', $stream['codec_type']);
+ }
+ }
+
+ public function test_builds_subtitle_transactions_correctly() {
+ $streams = [
+ [
+ 'index' => 2,
+ 'codec_name' => 'subrip',
+ 'tags' => ['language' => 'eng'],
+ 'disposition' => ['default' => 1, 'forced' => 0],
+ ],
+ ];
+
+ $result = $this->scanner->buildSubtitleTransactions('uuid', $streams);
+
+ $this->assertSame([
+ [
+ 'metadata_uuid' => 'uuid',
+ 'track_id' => 2,
+ 'language' => 'eng',
+ 'codec' => 'subrip',
+ 'is_default' => true,
+ 'is_forced' => false,
+ ],
+ ], $result);
+ }
+
+ public function test_defaults_language_to_und_when_missing() {
+ $streams = [
+ [
+ 'index' => 3,
+ 'codec_name' => 'ass',
+ 'disposition' => [],
+ ],
+ ];
+
+ $result = $this->scanner->buildSubtitleTransactions('uuid', $streams);
+
+ $this->assertSame('und', $result[0]['language']);
+ }
+
+ public function test_skips_invalid_streams_and_logs_warning() {
+ Log::spy();
+
+ $streams = [
+ ['codec_name' => 'subrip'],
+ ];
+
+ $result = $this->scanner->buildSubtitleTransactions('uuid', $streams);
+ $this->assertEmpty($result);
+ Log::shouldHaveReceived('warning')->once();
+ }
+}