feat: add support for png

feat: add tests

chore: update dependencies
This commit is contained in:
Benjamin Nussbaum 2025-04-24 19:31:57 +02:00
parent f1fafe7ef0
commit b4dea89fad
6 changed files with 250 additions and 94 deletions

View file

@ -141,3 +141,146 @@ test('it only processes proxy cloud enabled devices', function () {
return $request->hasHeader('id', $disabledDevice->mac_address);
});
});
test('it fetches and processes proxy cloud responses for devices with BMP images', function () {
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',
'filename' => 'test-image',
]),
'https://example.com/test-image.bmp?response-content-type=image/bmp' => Http::response('fake-image-content'),
]);
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert HTTP requests were made with correct headers
Http::assertSent(function ($request) use ($device) {
return $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')
->and($device->proxy_cloud_response)->toBe([
'image_url' => 'https://example.com/test-image.bmp?response-content-type=image/bmp',
'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 () {
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',
'filename' => 'test-image',
]),
'https://example.com/test-image.png?response-content-type=image/png' => Http::response('fake-image-content'),
]);
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert HTTP requests were made with correct headers
Http::assertSent(function ($request) use ($device) {
return $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')
->and($device->proxy_cloud_response)->toBe([
'image_url' => 'https://example.com/test-image.png?response-content-type=image/png',
'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 () {
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',
'filename' => 'test-image',
]),
'https://example.com/test-image.bmp' => Http::response('fake-image-content'),
]);
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert the device was updated
$device->refresh();
expect($device->current_screen_image)->toBe('test-image')
->and($device->proxy_cloud_response)->toBe([
'image_url' => 'https://example.com/test-image.bmp',
'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();
});