refactor: improve readability of FetchProxyCloudResponses

This commit is contained in:
Benjamin Nussbaum 2026-01-16 13:47:01 +01:00
parent e9986b424d
commit aaefcdda49
2 changed files with 197 additions and 214 deletions

View file

@ -16,13 +16,12 @@ 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']);
});
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([
function createTestDevice(array $attributes = []): Device
{
return Device::factory()->create(array_merge([
'proxy_cloud' => true,
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
@ -30,35 +29,11 @@ test('it fetches and processes proxy cloud responses for devices', function ():
'last_battery_voltage' => 3.7,
'default_refresh_interval' => 300,
'last_firmware_version' => '1.0.0',
]);
], $attributes));
}
// Mock the API response
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'),
]);
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');
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert HTTP requests were made with correct headers
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) &&
@ -67,7 +42,22 @@ test('it fetches and processes proxy cloud responses for devices', function ():
$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
}
test('it fetches and processes proxy cloud responses for devices', function (): void {
$device = createTestDevice();
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'),
]);
(new FetchProxyCloudResponses)->handle();
assertDeviceHeaders($device);
$device->refresh();
expect($device->current_screen_image)->toBe('test-image')
@ -76,15 +66,11 @@ 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 = Device::factory()->create([
'proxy_cloud' => true,
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
$device = createTestDevice([
'last_log_request' => ['message' => 'test log'],
]);
@ -97,33 +83,24 @@ test('it handles log requests when present', function (): void {
config('services.trmnl.proxy_base_url').'/api/log' => Http::response(null, 200),
]);
$job = new FetchProxyCloudResponses;
$job->handle();
(new FetchProxyCloudResponses)->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 {
$device = Device::factory()->create([
'proxy_cloud' => true,
'mac_address' => '00:11:22:33:44:55',
]);
createTestDevice();
Http::fake([
config('services.trmnl.proxy_base_url').'/api/display' => Http::response(null, 500),
]);
$job = new FetchProxyCloudResponses;
// Job should not throw exception but log error
expect(fn () => $job->handle())->not->toThrow(Exception::class);
expect(fn () => (new FetchProxyCloudResponses)->handle())->not->toThrow(Exception::class);
});
test('it only processes proxy cloud enabled devices', function (): void {
@ -131,30 +108,15 @@ test('it only processes proxy cloud enabled devices', function (): void {
$enabledDevice = Device::factory()->create(['proxy_cloud' => true]);
$disabledDevice = Device::factory()->create(['proxy_cloud' => false]);
$job = new FetchProxyCloudResponses;
$job->handle();
(new FetchProxyCloudResponses)->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 {
config(['services.trmnl.proxy_base_url' => 'https://example.com']);
$device = createTestDevice();
// 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',
@ -163,21 +125,9 @@ 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'),
]);
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
(new FetchProxyCloudResponses)->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
assertDeviceHeaders($device);
$device->refresh();
expect($device->current_screen_image)->toBe('test-image')
@ -186,26 +136,13 @@ 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 {
config(['services.trmnl.proxy_base_url' => 'https://example.com']);
$device = createTestDevice();
// 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',
@ -214,21 +151,9 @@ 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'),
]);
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
(new FetchProxyCloudResponses)->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
assertDeviceHeaders($device);
$device->refresh();
expect($device->current_screen_image)->toBe('test-image')
@ -237,22 +162,13 @@ 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 {
config(['services.trmnl.proxy_base_url' => 'https://example.com']);
$device = createTestDevice();
// 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',
@ -261,11 +177,8 @@ test('it handles missing content type in image URL gracefully', function (): voi
'https://example.com/test-image.bmp' => Http::response('fake-image-content'),
]);
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
(new FetchProxyCloudResponses)->handle();
// Assert the device was updated
$device->refresh();
expect($device->current_screen_image)->toBe('test-image')
@ -274,7 +187,50 @@ 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();
});