A self-hosted media server and file management system
Vue ffmpegfile-serverjellyfinlaravelmediamedia-playermedia-servermediabrowsermediaservermusicmusic-librarymusic-playermusicplayerself-hostedvuejs
feat: tests for playback progress and mini optimisation
aminnausin817d6cf3c459
diff --git a/app/Http/Controllers/Api/V1/PlaybackProgressController.php b/app/Http/Controllers/Api/V1/PlaybackProgressController.phpindex 49d1bd04..c40b83ca 100644--- a/app/Http/Controllers/Api/V1/PlaybackProgressController.php+++ b/app/Http/Controllers/Api/V1/PlaybackProgressController.php@@ -10,16 +10,15 @@ use App\Traits\HttpResponses;use Illuminate\Http\JsonResponse;use Illuminate\Http\Response;use Illuminate\Support\Facades\Auth;-use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\Log;class PlaybackProgressController extends Controller {use HttpResponses;public function show(Metadata $metadata) {- $progress = PlaybackProgress::where('user_id', Auth::id())->where('metadata_id', $metadata->id)->value('progress_offset');+ $progress = PlaybackProgress::where('user_id', Auth::id())->where('metadata_id', $metadata->id)->first(['progress_offset', 'progress_percentage']);- return response()->json(['progress_offset' => $progress ?? 0]);+ return response()->json(['progress_offset' => $progress?->progress_offset ?? 0, 'progress_percentage' => $progress?->progress_percentage ?? 0]);}/**@@ -30,33 +29,40 @@ class PlaybackProgressController extends Controller {$user_id = Auth::id();$validated = $request->validated();- DB::transaction(function () use ($user_id, $validated, $metadata) {- $progress = PlaybackProgress::firstOrNew([- 'user_id' => $user_id,- 'metadata_id' => $metadata->id,- ]);+ $duration = $metadata->duration ?? 0;+ $progressOffset = min($validated['progress_offset'], $duration);+ $progressPct = $duration ? round($progressOffset * 100 / $duration) : 0;+ $threshold = config('playback.completion_percentage_threshold', 95);- $duration = $metadata->duration ?? 0;- $progress_offset = min($validated['progress_offset'], $duration);- $progress_pct = $duration ? round($progress_offset * 100 / $duration) : 0;- $threshold = config('playback.completion_percentage_threshold', 95);+ if ($progressPct >= $threshold) {+ $progress = PlaybackProgress::firstOrNew(['user_id' => $user_id, 'metadata_id' => $metadata->id]);- $is_newly_completed = ($progress_pct >= $threshold && $progress->progress_percentage < $threshold);- $is_completed = $progress_pct >= $threshold;+ $isNewlyCompleted = $progress->progress_percentage < $threshold;- $progress->progress_offset = $is_completed ? $metadata->duration : $progress_offset;- $progress->progress_percentage = $is_completed ? 100 : $progress_pct;+ $progress->progress_offset = $duration;+ $progress->progress_percentage = 100;+ $progress->record_id = $validated['record_id'] ?? $progress->record_id;- if ($is_newly_completed) {+ if ($isNewlyCompleted) {$progress->last_completed_at = now();$progress->completion_count++;}- if (isset($validated['record_id'])) {- $progress->record_id = $validated['record_id'];- }$progress->save();- });+ } else {+ PlaybackProgress::upsert(+ [+ 'user_id' => $user_id,+ 'metadata_id' => $metadata->id,+ 'progress_offset' => $progressOffset,+ 'progress_percentage' => $progressPct,+ 'record_id' => $validated['record_id'] ?? null, // Overwrites existing+ 'updated_at' => now(),+ ],+ ['user_id', 'metadata_id'],+ ['progress_offset', 'progress_percentage', 'record_id', 'updated_at']+ );+ }return response()->noContent();} catch (\Throwable $th) {diff --git a/app/Models/PlaybackProgress.php b/app/Models/PlaybackProgress.phpindex f44332af..31360b2f 100644--- a/app/Models/PlaybackProgress.php+++ b/app/Models/PlaybackProgress.php@@ -2,10 +2,12 @@namespace App\Models;+use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\BelongsTo;class PlaybackProgress extends Model {+ use HasFactory;/*** id -> int8 (pk) (index)* user_id -> int8 (fk) (index) (unique with metadata_id)diff --git a/database/factories/PlaybackProgressFactory.php b/database/factories/PlaybackProgressFactory.phpnew file mode 100644index 00000000..097b3f0b--- /dev/null+++ b/database/factories/PlaybackProgressFactory.php@@ -0,0 +1,29 @@+<?php++namespace Database\Factories;++use App\Models\Metadata;+use App\Models\User;+use Illuminate\Database\Eloquent\Factories\Factory;++/**+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\PlaybackProgress>+ */+class PlaybackProgressFactory extends Factory {+ /**+ * Define the model's default state.+ *+ * @return array<string, mixed>+ */+ public function definition(): array {+ return [+ 'user_id' => User::factory(),+ 'metadata_id' => Metadata::factory(),+ 'record_id' => null,+ 'progress_offset' => $this->faker->numberBetween(0, 100),+ 'progress_percentage' => $this->faker->numberBetween(0, 100),+ 'completion_count' => 0,+ 'last_completed_at' => null,+ ];+ }+}diff --git a/tests/Feature/Playback/PlaybackProgressControllerTest.php b/tests/Feature/Playback/PlaybackProgressControllerTest.phpnew file mode 100644index 00000000..14c6c555--- /dev/null+++ b/tests/Feature/Playback/PlaybackProgressControllerTest.php@@ -0,0 +1,205 @@+<?php++namespace Tests\Feature\Playback;++use App\Models\Metadata;+use App\Models\PlaybackProgress;+use App\Models\User;+use Illuminate\Foundation\Testing\RefreshDatabase;+use Tests\TestCase;++class PlaybackProgressControllerTest extends TestCase {+ use RefreshDatabase;++ private User $user;+ private Metadata $metadata;++ protected function setUp(): void {+ parent::setUp();+ $this->user = User::factory()->create();+ $this->metadata = Metadata::factory()->create(['duration' => 100]);+ }++ private function actingAsUser(): static {+ return $this->actingAs($this->user);+ }++ // -------------------------+ // show+ // -------------------------++ public function test_show_returns_zero_when_no_progress_exists(): void {+ $response = $this->actingAsUser()->getJson("/api/metadata/{$this->metadata->id}/progress");++ $response->assertOk()->assertJson([+ 'progress_offset' => 0,+ 'progress_percentage' => 0,+ ]);+ }++ public function test_show_returns_existing_progress(): void {+ PlaybackProgress::factory()->create([+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 40,+ 'progress_percentage' => 40,+ ]);++ $response = $this->actingAsUser()->getJson("/api/metadata/{$this->metadata->id}/progress");++ $response->assertOk()->assertJson([+ 'progress_offset' => 40,+ 'progress_percentage' => 40,+ ]);+ }++ public function test_show_only_returns_authenticated_users_progress(): void {+ $otherUser = User::factory()->create();++ PlaybackProgress::factory()->create([+ 'user_id' => $otherUser->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 60,+ 'progress_percentage' => 60,+ ]);++ $response = $this->actingAsUser()->getJson("/api/metadata/{$this->metadata->id}/progress");++ $response->assertOk()->assertJson([+ 'progress_offset' => 0,+ 'progress_percentage' => 0,+ ]);+ }++ public function test_show_requires_authentication(): void {+ $this->getJson("/api/metadata/{$this->metadata->id}/progress")+ ->assertUnauthorized();+ }++ // -------------------------+ // upsert+ // -------------------------++ public function test_upsert_creates_new_progress_record(): void {+ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 40,+ ])->assertNoContent();++ $this->assertDatabaseHas('playback_progress', [+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 40,+ 'progress_percentage' => 40,+ ]);+ }++ public function test_upsert_updates_existing_progress_record(): void {+ PlaybackProgress::factory()->create([+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 40,+ 'progress_percentage' => 40,+ ]);++ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 60,+ ])->assertNoContent();++ $this->assertDatabaseHas('playback_progress', [+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 60,+ 'progress_percentage' => 60,+ ]);++ $this->assertDatabaseCount('playback_progress', 1);+ }++ public function test_upsert_clamps_offset_to_duration(): void {+ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 999,+ ])->assertNoContent();++ $this->assertDatabaseHas('playback_progress', [+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 100,+ ]);+ }++ public function test_upsert_marks_completion_at_threshold(): void {+ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 96,+ ])->assertNoContent();++ $progress = PlaybackProgress::where('user_id', $this->user->id)+ ->where('metadata_id', $this->metadata->id)+ ->first();++ $this->assertEquals(100, $progress->progress_offset);+ $this->assertEquals(100, $progress->progress_percentage);+ $this->assertEquals(1, $progress->completion_count);+ $this->assertNotNull($progress->last_completed_at);+ }++ public function test_upsert_does_not_increment_completion_count_if_already_completed(): void {+ PlaybackProgress::factory()->create([+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 100,+ 'progress_percentage' => 100,+ 'completion_count' => 1,+ 'last_completed_at' => now()->subDay(),+ ]);++ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 98,+ ])->assertNoContent();++ $this->assertDatabaseHas('playback_progress', [+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'completion_count' => 1,+ ]);+ }++ public function test_upsert_increments_completion_count_on_rewatch(): void {+ PlaybackProgress::factory()->create([+ 'user_id' => $this->user->id,+ 'metadata_id' => $this->metadata->id,+ 'progress_offset' => 100,+ 'progress_percentage' => 100,+ 'completion_count' => 1,+ ]);++ // progress gets set to below the threshold and then completes again+ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 10,+ ])->assertNoContent();++ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 97,+ ])->assertNoContent();++ $this->assertDatabaseHas('playback_progress', [+ 'completion_count' => 2,+ ]);+ }++ public function test_upsert_requires_authentication(): void {+ $this->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 40,+ ])->assertUnauthorized();+ }++ public function test_upsert_requires_progress_offset(): void {+ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [])+ ->assertUnprocessable();+ }++ public function test_upsert_requires_progress_offset_to_be_integer(): void {+ $this->actingAsUser()->putJson("/api/metadata/{$this->metadata->id}/progress", [+ 'progress_offset' => 'not-an-integer',+ ])->assertUnprocessable();+ }+}