diff --git a/app/Jobs/FetchProxyCloudResponses.php b/app/Jobs/FetchProxyCloudResponses.php index 3c8af13..ac23130 100644 --- a/app/Jobs/FetchProxyCloudResponses.php +++ b/app/Jobs/FetchProxyCloudResponses.php @@ -7,7 +7,6 @@ use Exception; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; -use Illuminate\Http\Client\Response; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Http; @@ -19,126 +18,100 @@ class FetchProxyCloudResponses implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; + /** + * Execute the job. + */ public function handle(): void { Device::where('proxy_cloud', true)->each(function ($device): void { - if ($device->getNextPlaylistItem()) { + if (! $device->getNextPlaylistItem()) { + try { + $response = Http::withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + 'width' => 800, + 'height' => 480, + 'rssi' => $device->last_rssi_level, + 'battery_voltage' => $device->last_battery_voltage, + 'refresh-rate' => $device->default_refresh_interval, + 'fw-version' => $device->last_firmware_version, + 'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0', + 'user-agent' => 'ESP32HTTPClient', + ])->get(config('services.trmnl.proxy_base_url').'/api/display'); + + $device->update([ + 'proxy_cloud_response' => $response->json(), + ]); + + $imageUrl = $response->json('image_url'); + $filename = $response->json('filename'); + + parse_str(parse_url($imageUrl)['query'] ?? '', $queryParams); + $imageType = urldecode($queryParams['response-content-type'] ?? 'image/bmp'); + $imageExtension = $imageType === 'image/png' ? 'png' : 'bmp'; + + if (Str::contains($imageUrl, '.png')) { + $imageExtension = 'png'; + } + + \Log::info("Response data: $imageUrl. Image Extension: $imageExtension"); + if (isset($imageUrl)) { + try { + $imageContents = Http::get($imageUrl)->body(); + if (! Storage::disk('public')->exists("images/generated/{$filename}.{$imageExtension}")) { + Storage::disk('public')->put( + "images/generated/{$filename}.{$imageExtension}", + $imageContents + ); + } + + $device->update([ + 'current_screen_image' => $filename, + ]); + } catch (Exception $e) { + Log::error("Failed to download and save image for device: {$device->mac_address}", [ + 'error' => $e->getMessage(), + ]); + } + } + + Log::info("Successfully updated proxy cloud response for device: {$device->mac_address}"); + + if ($device->last_log_request) { + try { + Http::withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + 'width' => 800, + 'height' => 480, + 'rssi' => $device->last_rssi_level, + 'battery_voltage' => $device->last_battery_voltage, + 'refresh-rate' => $device->default_refresh_interval, + 'fw-version' => $device->last_firmware_version, + 'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0', + 'user-agent' => 'ESP32HTTPClient', + ])->post(config('services.trmnl.proxy_base_url').'/api/log', $device->last_log_request); + + // Only clear the pending log request if the POST succeeded + $device->update([ + 'last_log_request' => null, + ]); + } catch (Exception $e) { + // Do not fail the entire proxy fetch if the log upload fails + Log::error("Failed to upload device log for device: {$device->mac_address}", [ + 'error' => $e->getMessage(), + ]); + } + } + + } catch (Exception $e) { + Log::error("Failed to fetch proxy cloud response for device: {$device->mac_address}", [ + 'error' => $e->getMessage(), + ]); + } + } else { Log::info("Skipping device: {$device->mac_address} as it has a pending playlist item."); - - return; - } - - try { - $response = $this->fetchDisplayResponse($device); - $device->update([ - 'proxy_cloud_response' => $response->json(), - ]); - - $this->processImage($device, $response); - $this->uploadLogRequest($device); - - Log::info("Successfully updated proxy cloud response for device: {$device->mac_address}"); - } catch (Exception $e) { - Log::error("Failed to fetch proxy cloud response for device: {$device->mac_address}", [ - 'error' => $e->getMessage(), - ]); } }); } - - private function fetchDisplayResponse(Device $device): Response - { - /** @var Response $response */ - $response = Http::withHeaders($this->getDeviceHeaders($device)) - ->get(config('services.trmnl.proxy_base_url').'/api/display'); - - return $response; - } - - private function getDeviceHeaders(Device $device): array - { - return [ - 'id' => $device->mac_address, - 'access-token' => $device->api_key, - 'width' => 800, - 'height' => 480, - 'rssi' => $device->last_rssi_level, - 'battery_voltage' => $device->last_battery_voltage, - 'refresh-rate' => $device->default_refresh_interval, - 'fw-version' => $device->last_firmware_version, - 'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0', - 'user-agent' => 'ESP32HTTPClient', - ]; - } - - private function processImage(Device $device, Response $response): void - { - $imageUrl = $response->json('image_url'); - $filename = $response->json('filename'); - - if ($imageUrl === null) { - return; - } - - $imageExtension = $this->determineImageExtension($imageUrl); - Log::info("Response data: $imageUrl. Image Extension: $imageExtension"); - - try { - $imageContents = Http::get($imageUrl)->body(); - $filePath = "images/generated/{$filename}.{$imageExtension}"; - - if (! Storage::disk('public')->exists($filePath)) { - Storage::disk('public')->put($filePath, $imageContents); - } - - $device->update([ - 'current_screen_image' => $filename, - ]); - } catch (Exception $e) { - Log::error("Failed to download and save image for device: {$device->mac_address}", [ - 'error' => $e->getMessage(), - ]); - } - } - - private function determineImageExtension(?string $imageUrl): string - { - if ($imageUrl === null) { - return 'bmp'; - } - - if (Str::contains($imageUrl, '.png')) { - return 'png'; - } - - $parsedUrl = parse_url($imageUrl); - if ($parsedUrl === false || ! isset($parsedUrl['query'])) { - return 'bmp'; - } - - parse_str($parsedUrl['query'], $queryParams); - $imageType = urldecode($queryParams['response-content-type'] ?? 'image/bmp'); - - return $imageType === 'image/png' ? 'png' : 'bmp'; - } - - private function uploadLogRequest(Device $device): void - { - if (! $device->last_log_request) { - return; - } - - try { - Http::withHeaders($this->getDeviceHeaders($device)) - ->post(config('services.trmnl.proxy_base_url').'/api/log', $device->last_log_request); - - $device->update([ - 'last_log_request' => null, - ]); - } catch (Exception $e) { - Log::error("Failed to upload device log for device: {$device->mac_address}", [ - 'error' => $e->getMessage(), - ]); - } - } } diff --git a/app/Services/QrCodeService.php b/app/Services/QrCodeService.php index e20ae42..812415b 100644 --- a/app/Services/QrCodeService.php +++ b/app/Services/QrCodeService.php @@ -81,6 +81,11 @@ class QrCodeService $this->size = 29 * $moduleSize; } + // Calculate margin: 4 modules on each side + // Module size = size / 29, so margin = (size / 29) * 4 + $moduleSize = $this->size / 29; + $margin = (int) ($moduleSize * 4); + // Map error correction level $errorCorrectionLevel = ErrorCorrectionLevel::valueOf('M'); // default if ($this->errorCorrection !== null) { @@ -94,7 +99,7 @@ class QrCodeService } // Create renderer style with size and margin - $rendererStyle = new RendererStyle($this->size, 0); + $rendererStyle = new RendererStyle($this->size, $margin); // Create SVG renderer $renderer = new ImageRenderer( diff --git a/composer.lock b/composer.lock index e97b295..52a9ed0 100644 --- a/composer.lock +++ b/composer.lock @@ -2898,20 +2898,20 @@ }, { "name": "league/uri", - "version": "7.8.0", + "version": "7.7.0", "source": { "type": "git", "url": "https://github.com/thephpleague/uri.git", - "reference": "4436c6ec8d458e4244448b069cc572d088230b76" + "reference": "8d587cddee53490f9b82bf203d3a9aa7ea4f9807" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/uri/zipball/4436c6ec8d458e4244448b069cc572d088230b76", - "reference": "4436c6ec8d458e4244448b069cc572d088230b76", + "url": "https://api.github.com/repos/thephpleague/uri/zipball/8d587cddee53490f9b82bf203d3a9aa7ea4f9807", + "reference": "8d587cddee53490f9b82bf203d3a9aa7ea4f9807", "shasum": "" }, "require": { - "league/uri-interfaces": "^7.8", + "league/uri-interfaces": "^7.7", "php": "^8.1", "psr/http-factory": "^1" }, @@ -2925,11 +2925,11 @@ "ext-gmp": "to improve IPV4 host parsing", "ext-intl": "to handle IDN host with the best performance", "ext-uri": "to use the PHP native URI class", - "jeremykendall/php-domain-parser": "to further parse the URI host and resolve its Public Suffix and Top Level Domain", - "league/uri-components": "to provide additional tools to manipulate URI objects components", - "league/uri-polyfill": "to backport the PHP URI extension for older versions of PHP", + "jeremykendall/php-domain-parser": "to resolve Public Suffix and Top Level Domain", + "league/uri-components": "Needed to easily manipulate URI objects components", + "league/uri-polyfill": "Needed to backport the PHP URI extension for older versions of PHP", "php-64bit": "to improve IPV4 host parsing", - "rowbot/url": "to handle URLs using the WHATWG URL Living Standard specification", + "rowbot/url": "to handle WHATWG URL", "symfony/polyfill-intl-idn": "to handle IDN host via the Symfony polyfill if ext-intl is not present" }, "type": "library", @@ -2984,7 +2984,7 @@ "docs": "https://uri.thephpleague.com", "forum": "https://thephpleague.slack.com", "issues": "https://github.com/thephpleague/uri-src/issues", - "source": "https://github.com/thephpleague/uri/tree/7.8.0" + "source": "https://github.com/thephpleague/uri/tree/7.7.0" }, "funding": [ { @@ -2992,20 +2992,20 @@ "type": "github" } ], - "time": "2026-01-14T17:24:56+00:00" + "time": "2025-12-07T16:02:06+00:00" }, { "name": "league/uri-interfaces", - "version": "7.8.0", + "version": "7.7.0", "source": { "type": "git", "url": "https://github.com/thephpleague/uri-interfaces.git", - "reference": "c5c5cd056110fc8afaba29fa6b72a43ced42acd4" + "reference": "62ccc1a0435e1c54e10ee6022df28d6c04c2946c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/thephpleague/uri-interfaces/zipball/c5c5cd056110fc8afaba29fa6b72a43ced42acd4", - "reference": "c5c5cd056110fc8afaba29fa6b72a43ced42acd4", + "url": "https://api.github.com/repos/thephpleague/uri-interfaces/zipball/62ccc1a0435e1c54e10ee6022df28d6c04c2946c", + "reference": "62ccc1a0435e1c54e10ee6022df28d6c04c2946c", "shasum": "" }, "require": { @@ -3018,7 +3018,7 @@ "ext-gmp": "to improve IPV4 host parsing", "ext-intl": "to handle IDN host with the best performance", "php-64bit": "to improve IPV4 host parsing", - "rowbot/url": "to handle URLs using the WHATWG URL Living Standard specification", + "rowbot/url": "to handle WHATWG URL", "symfony/polyfill-intl-idn": "to handle IDN host via the Symfony polyfill if ext-intl is not present" }, "type": "library", @@ -3068,7 +3068,7 @@ "docs": "https://uri.thephpleague.com", "forum": "https://thephpleague.slack.com", "issues": "https://github.com/thephpleague/uri-src/issues", - "source": "https://github.com/thephpleague/uri-interfaces/tree/7.8.0" + "source": "https://github.com/thephpleague/uri-interfaces/tree/7.7.0" }, "funding": [ { @@ -3076,7 +3076,7 @@ "type": "github" } ], - "time": "2026-01-15T06:54:53+00:00" + "time": "2025-12-07T16:03:21+00:00" }, { "name": "livewire/flux", diff --git a/resources/views/pages/settings/layout.blade.php b/resources/views/pages/settings/layout.blade.php index d3667d6..5cc7bce 100644 --- a/resources/views/pages/settings/layout.blade.php +++ b/resources/views/pages/settings/layout.blade.php @@ -7,6 +7,9 @@ @if(auth()?->user()?->oidc_sub === null) {{ __('Password') }} @endif + @if (Laravel\Fortify\Features::canManageTwoFactorAuthentication() && auth()?->user()?->oidc_sub === null) + {{ __('2FA') }} + @endif {{ __('Support') }} diff --git a/resources/views/pages/settings/password.blade.php b/resources/views/pages/settings/password.blade.php index 723cfed..e7c5deb 100644 --- a/resources/views/pages/settings/password.blade.php +++ b/resources/views/pages/settings/password.blade.php @@ -83,11 +83,5 @@ new class extends Component - @if (Laravel\Fortify\Features::canManageTwoFactorAuthentication() && auth()?->user()?->oidc_sub === null) - 2FA - Optionally, you can enable Two-Factor Authentication via TOTP - - {{ __('2FA Settings…') }} - @endif diff --git a/resources/views/recipes/home-assistant.blade.php b/resources/views/recipes/home-assistant.blade.php index 4e52a9e..686b33a 100644 --- a/resources/views/recipes/home-assistant.blade.php +++ b/resources/views/recipes/home-assistant.blade.php @@ -1,5 +1,5 @@ @php - $weatherEntity = collect(Arr::get($data, 'data'))->first(function($entity) { + $weatherEntity = collect($data)->first(function($entity) { return $entity['entity_id'] === 'weather.forecast_home'; }); @endphp diff --git a/tests/Feature/FetchProxyCloudResponsesTest.php b/tests/Feature/FetchProxyCloudResponsesTest.php index abd3cb9..561dc1c 100644 --- a/tests/Feature/FetchProxyCloudResponsesTest.php +++ b/tests/Feature/FetchProxyCloudResponsesTest.php @@ -16,12 +16,13 @@ beforeEach(function (): void { 'https://trmnl.app/api/log' => Http::response([], 200), 'https://example.com/api/log' => Http::response([], 200), ]); - config(['services.trmnl.proxy_base_url' => 'https://example.com']); }); -function createTestDevice(array $attributes = []): Device -{ - return Device::factory()->create(array_merge([ +test('it fetches and processes proxy cloud responses for devices', function (): void { + config(['services.trmnl.proxy_base_url' => 'https://example.com']); + + // Create a test device with proxy cloud enabled + $device = Device::factory()->create([ 'proxy_cloud' => true, 'mac_address' => '00:11:22:33:44:55', 'api_key' => 'test-api-key', @@ -29,24 +30,9 @@ function createTestDevice(array $attributes = []): Device 'last_battery_voltage' => 3.7, 'default_refresh_interval' => 300, 'last_firmware_version' => '1.0.0', - ], $attributes)); -} - -function assertDeviceHeaders(Device $device): void -{ - Http::assertSent(fn ($request): bool => $request->hasHeader('id', $device->mac_address) && - $request->hasHeader('access-token', $device->api_key) && - $request->hasHeader('width', 800) && - $request->hasHeader('height', 480) && - $request->hasHeader('rssi', $device->last_rssi_level) && - $request->hasHeader('battery_voltage', $device->last_battery_voltage) && - $request->hasHeader('refresh-rate', $device->default_refresh_interval) && - $request->hasHeader('fw-version', $device->last_firmware_version)); -} - -test('it fetches and processes proxy cloud responses for devices', function (): void { - $device = createTestDevice(); + ]); + // Mock the API response Http::fake([ config('services.trmnl.proxy_base_url').'/api/display' => Http::response([ 'image_url' => 'https://example.com/test-image.bmp', @@ -55,9 +41,33 @@ test('it fetches and processes proxy cloud responses for devices', function (): 'https://example.com/test-image.bmp' => Http::response('fake-image-content'), ]); - (new FetchProxyCloudResponses)->handle(); + Http::withHeaders([ + 'id' => $device->mac_address, + 'access-token' => $device->api_key, + 'width' => 800, + 'height' => 480, + 'rssi' => $device->last_rssi_level, + 'battery_voltage' => $device->last_battery_voltage, + 'refresh-rate' => $device->default_refresh_interval, + 'fw-version' => $device->last_firmware_version, + 'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0', + 'user-agent' => 'ESP32HTTPClient', + ])->get(config('services.trmnl.proxy_base_url').'/api/display'); - assertDeviceHeaders($device); + // Run the job + $job = new FetchProxyCloudResponses; + $job->handle(); + + // Assert HTTP requests were made with correct headers + Http::assertSent(fn ($request): bool => $request->hasHeader('id', $device->mac_address) && + $request->hasHeader('access-token', $device->api_key) && + $request->hasHeader('width', 800) && + $request->hasHeader('height', 480) && + $request->hasHeader('rssi', $device->last_rssi_level) && + $request->hasHeader('battery_voltage', $device->last_battery_voltage) && + $request->hasHeader('refresh-rate', $device->default_refresh_interval) && + $request->hasHeader('fw-version', $device->last_firmware_version)); + // Assert the device was updated $device->refresh(); expect($device->current_screen_image)->toBe('test-image') @@ -66,11 +76,15 @@ test('it fetches and processes proxy cloud responses for devices', function (): 'filename' => 'test-image', ]); + // Assert the image was saved Storage::disk('public')->assertExists('images/generated/test-image.bmp'); }); test('it handles log requests when present', function (): void { - $device = createTestDevice([ + $device = Device::factory()->create([ + 'proxy_cloud' => true, + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'test-api-key', 'last_log_request' => ['message' => 'test log'], ]); @@ -83,24 +97,33 @@ test('it handles log requests when present', function (): void { config('services.trmnl.proxy_base_url').'/api/log' => Http::response(null, 200), ]); - (new FetchProxyCloudResponses)->handle(); + $job = new FetchProxyCloudResponses; + $job->handle(); + // Assert log request was sent Http::assertSent(fn ($request): bool => $request->url() === config('services.trmnl.proxy_base_url').'/api/log' && $request->hasHeader('id', $device->mac_address) && $request->body() === json_encode(['message' => 'test log'])); + // Assert log request was cleared $device->refresh(); expect($device->last_log_request)->toBeNull(); }); test('it handles API errors gracefully', function (): void { - createTestDevice(); + $device = Device::factory()->create([ + 'proxy_cloud' => true, + 'mac_address' => '00:11:22:33:44:55', + ]); Http::fake([ config('services.trmnl.proxy_base_url').'/api/display' => Http::response(null, 500), ]); - expect(fn () => (new FetchProxyCloudResponses)->handle())->not->toThrow(Exception::class); + $job = new FetchProxyCloudResponses; + + // Job should not throw exception but log error + expect(fn () => $job->handle())->not->toThrow(Exception::class); }); test('it only processes proxy cloud enabled devices', function (): void { @@ -108,15 +131,30 @@ test('it only processes proxy cloud enabled devices', function (): void { $enabledDevice = Device::factory()->create(['proxy_cloud' => true]); $disabledDevice = Device::factory()->create(['proxy_cloud' => false]); - (new FetchProxyCloudResponses)->handle(); + $job = new FetchProxyCloudResponses; + $job->handle(); + // Assert request was only made for enabled device Http::assertSent(fn ($request) => $request->hasHeader('id', $enabledDevice->mac_address)); + Http::assertNotSent(fn ($request) => $request->hasHeader('id', $disabledDevice->mac_address)); }); test('it fetches and processes proxy cloud responses for devices with BMP images', function (): void { - $device = createTestDevice(); + config(['services.trmnl.proxy_base_url' => 'https://example.com']); + // Create a test device with proxy cloud enabled + $device = Device::factory()->create([ + 'proxy_cloud' => true, + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'test-api-key', + 'last_rssi_level' => -70, + 'last_battery_voltage' => 3.7, + 'default_refresh_interval' => 300, + 'last_firmware_version' => '1.0.0', + ]); + + // Mock the API response with BMP image Http::fake([ config('services.trmnl.proxy_base_url').'/api/display' => Http::response([ 'image_url' => 'https://example.com/test-image.bmp?response-content-type=image/bmp', @@ -125,9 +163,21 @@ test('it fetches and processes proxy cloud responses for devices with BMP images 'https://example.com/test-image.bmp?response-content-type=image/bmp' => Http::response('fake-image-content'), ]); - (new FetchProxyCloudResponses)->handle(); + // Run the job + $job = new FetchProxyCloudResponses; + $job->handle(); - assertDeviceHeaders($device); + // Assert HTTP requests were made with correct headers + Http::assertSent(fn ($request): bool => $request->hasHeader('id', $device->mac_address) && + $request->hasHeader('access-token', $device->api_key) && + $request->hasHeader('width', 800) && + $request->hasHeader('height', 480) && + $request->hasHeader('rssi', $device->last_rssi_level) && + $request->hasHeader('battery_voltage', $device->last_battery_voltage) && + $request->hasHeader('refresh-rate', $device->default_refresh_interval) && + $request->hasHeader('fw-version', $device->last_firmware_version)); + + // Assert the device was updated $device->refresh(); expect($device->current_screen_image)->toBe('test-image') @@ -136,13 +186,26 @@ test('it fetches and processes proxy cloud responses for devices with BMP images 'filename' => 'test-image', ]); + // Assert the image was saved with BMP extension expect(Storage::disk('public')->exists('images/generated/test-image.bmp'))->toBeTrue(); expect(Storage::disk('public')->exists('images/generated/test-image.png'))->toBeFalse(); }); test('it fetches and processes proxy cloud responses for devices with PNG images', function (): void { - $device = createTestDevice(); + config(['services.trmnl.proxy_base_url' => 'https://example.com']); + // Create a test device with proxy cloud enabled + $device = Device::factory()->create([ + 'proxy_cloud' => true, + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'test-api-key', + 'last_rssi_level' => -70, + 'last_battery_voltage' => 3.7, + 'default_refresh_interval' => 300, + 'last_firmware_version' => '1.0.0', + ]); + + // Mock the API response with PNG image Http::fake([ config('services.trmnl.proxy_base_url').'/api/display' => Http::response([ 'image_url' => 'https://example.com/test-image.png?response-content-type=image/png', @@ -151,9 +214,21 @@ test('it fetches and processes proxy cloud responses for devices with PNG images 'https://example.com/test-image.png?response-content-type=image/png' => Http::response('fake-image-content'), ]); - (new FetchProxyCloudResponses)->handle(); + // Run the job + $job = new FetchProxyCloudResponses; + $job->handle(); - assertDeviceHeaders($device); + // Assert HTTP requests were made with correct headers + Http::assertSent(fn ($request): bool => $request->hasHeader('id', $device->mac_address) && + $request->hasHeader('access-token', $device->api_key) && + $request->hasHeader('width', 800) && + $request->hasHeader('height', 480) && + $request->hasHeader('rssi', $device->last_rssi_level) && + $request->hasHeader('battery_voltage', $device->last_battery_voltage) && + $request->hasHeader('refresh-rate', $device->default_refresh_interval) && + $request->hasHeader('fw-version', $device->last_firmware_version)); + + // Assert the device was updated $device->refresh(); expect($device->current_screen_image)->toBe('test-image') @@ -162,13 +237,22 @@ test('it fetches and processes proxy cloud responses for devices with PNG images 'filename' => 'test-image', ]); + // Assert the image was saved with PNG extension expect(Storage::disk('public')->exists('images/generated/test-image.png'))->toBeTrue(); expect(Storage::disk('public')->exists('images/generated/test-image.bmp'))->toBeFalse(); }); test('it handles missing content type in image URL gracefully', function (): void { - $device = createTestDevice(); + config(['services.trmnl.proxy_base_url' => 'https://example.com']); + // Create a test device with proxy cloud enabled + $device = Device::factory()->create([ + 'proxy_cloud' => true, + 'mac_address' => '00:11:22:33:44:55', + 'api_key' => 'test-api-key', + ]); + + // Mock the API response with no content type in URL Http::fake([ config('services.trmnl.proxy_base_url').'/api/display' => Http::response([ 'image_url' => 'https://example.com/test-image.bmp', @@ -177,8 +261,11 @@ test('it handles missing content type in image URL gracefully', function (): voi 'https://example.com/test-image.bmp' => Http::response('fake-image-content'), ]); - (new FetchProxyCloudResponses)->handle(); + // Run the job + $job = new FetchProxyCloudResponses; + $job->handle(); + // Assert the device was updated $device->refresh(); expect($device->current_screen_image)->toBe('test-image') @@ -187,50 +274,7 @@ test('it handles missing content type in image URL gracefully', function (): voi 'filename' => 'test-image', ]); + // Assert the image was saved with default BMP extension expect(Storage::disk('public')->exists('images/generated/test-image.bmp'))->toBeTrue(); expect(Storage::disk('public')->exists('images/generated/test-image.png'))->toBeFalse(); }); - -test('it handles null image URL gracefully', function (): void { - $device = createTestDevice(); - - Http::fake([ - config('services.trmnl.proxy_base_url').'/api/display' => Http::response([ - 'image_url' => null, - 'filename' => 'test-image', - ]), - ]); - - expect(fn () => (new FetchProxyCloudResponses)->handle())->not->toThrow(TypeError::class); - - $device->refresh(); - expect($device->proxy_cloud_response)->toBe([ - 'image_url' => null, - 'filename' => 'test-image', - ]); - - expect($device->current_screen_image)->toBeNull(); - expect(Storage::disk('public')->exists('images/generated/test-image.bmp'))->toBeFalse(); - expect(Storage::disk('public')->exists('images/generated/test-image.png'))->toBeFalse(); -}); - -test('it handles malformed image URL gracefully', function (): void { - $device = createTestDevice(); - - Http::fake([ - config('services.trmnl.proxy_base_url').'/api/display' => Http::response([ - 'image_url' => 'not-a-valid-url://', - 'filename' => 'test-image', - ]), - ]); - - expect(fn () => (new FetchProxyCloudResponses)->handle())->not->toThrow(TypeError::class); - - $device->refresh(); - expect($device->proxy_cloud_response)->toBe([ - 'image_url' => 'not-a-valid-url://', - 'filename' => 'test-image', - ]); - - expect($device->current_screen_image)->toBeNull(); -});