diff --git a/app/Services/ImageGenerationService.php b/app/Services/ImageGenerationService.php index a7bd3c6..d0ecddc 100644 --- a/app/Services/ImageGenerationService.php +++ b/app/Services/ImageGenerationService.php @@ -353,20 +353,27 @@ class ImageGenerationService public static function resetIfNotCacheable(?Plugin $plugin): void { if ($plugin?->id) { - // Check if any devices have custom dimensions or use DeviceModels + // Check if any devices have custom dimensions or use non-standard DeviceModels $hasCustomDimensions = Device::query() ->where(function ($query) { $query->where('width', '!=', 800) ->orWhere('height', '!=', 480) ->orWhere('rotate', '!=', 0); }) - ->orWhereNotNull('device_model_id') + ->orWhereHas('deviceModel', function ($query) { + // Only allow caching if all device models have standard dimensions (800x480, rotation=0) + $query->where(function ($subQuery) { + $subQuery->where('width', '!=', 800) + ->orWhere('height', '!=', 480) + ->orWhere('rotation', '!=', 0); + }); + }) ->exists(); if ($hasCustomDimensions) { // TODO cache image per device $plugin->update(['current_image' => null]); - Log::debug('Skip cache as devices with custom dimensions or DeviceModels exist'); + Log::debug('Skip cache as devices with custom dimensions or non-standard DeviceModels exist'); } } } diff --git a/tests/Unit/Services/ImageGenerationServiceTest.php b/tests/Unit/Services/ImageGenerationServiceTest.php index 4941c3c..b254243 100644 --- a/tests/Unit/Services/ImageGenerationServiceTest.php +++ b/tests/Unit/Services/ImageGenerationServiceTest.php @@ -226,6 +226,59 @@ it('reset_if_not_cacheable preserves cache for standard devices', function (): v expect($plugin->current_image)->toBe('test-uuid'); })->skipOnGitHubActions(); +it('reset_if_not_cacheable preserves cache for og_png and og_plus device models', function (): void { + // Create a plugin + $plugin = App\Models\Plugin::factory()->create(['current_image' => 'test-uuid']); + + // Create og_png device model + $ogPngModel = DeviceModel::factory()->create([ + 'name' => 'test_og_png', + 'width' => 800, + 'height' => 480, + 'rotation' => 0, + ]); + + // Create og_plus device model + $ogPlusModel = DeviceModel::factory()->create([ + 'name' => 'test_og_plus', + 'width' => 800, + 'height' => 480, + 'rotation' => 0, + ]); + + // Create devices with og_png and og_plus device models + Device::factory()->create(['device_model_id' => $ogPngModel->id]); + Device::factory()->create(['device_model_id' => $ogPlusModel->id]); + + // Test that the method preserves cache for standard device models + ImageGenerationService::resetIfNotCacheable($plugin); + + $plugin->refresh(); + expect($plugin->current_image)->toBe('test-uuid'); +})->skipOnGitHubActions(); + +it('reset_if_not_cacheable resets cache for non-standard device models', function (): void { + // Create a plugin + $plugin = App\Models\Plugin::factory()->create(['current_image' => 'test-uuid']); + + // Create a non-standard device model (e.g., kindle) + $kindleModel = DeviceModel::factory()->create([ + 'name' => 'test_amazon_kindle_2024', + 'width' => 1400, + 'height' => 840, + 'rotation' => 90, + ]); + + // Create a device with the non-standard device model + Device::factory()->create(['device_model_id' => $kindleModel->id]); + + // Test that the method resets cache for non-standard device models + ImageGenerationService::resetIfNotCacheable($plugin); + + $plugin->refresh(); + expect($plugin->current_image)->toBeNull(); +})->skipOnGitHubActions(); + it('reset_if_not_cacheable handles null plugin', function (): void { // Test that the method handles null plugin gracefully expect(fn () => ImageGenerationService::resetIfNotCacheable(null))->not->toThrow(Exception::class);