Files

121 lines
4.6 KiB
PHP
Raw Permalink Normal View History

2025-03-03 22:20:52 +01:00
<?php
use App\Jobs\GenerateScreenJob;
use App\Models\Device;
use App\Models\DeviceModel;
use App\Models\Plugin;
2026-04-30 13:03:32 +02:00
use App\Models\User;
2026-05-12 09:16:04 +02:00
use Bnussbau\EpaperPipeline\EpaperPipeline;
2025-03-03 22:20:52 +01:00
use Illuminate\Support\Facades\Storage;
2025-06-17 21:30:59 +02:00
uses(Illuminate\Foundation\Testing\RefreshDatabase::class);
2025-03-03 22:20:52 +01:00
2025-09-24 20:31:32 +02:00
beforeEach(function (): void {
2026-05-12 09:16:04 +02:00
EpaperPipeline::fake();
2025-03-03 22:20:52 +01:00
Storage::fake('public');
Storage::disk('public')->makeDirectory('/images/generated');
});
2025-09-24 20:31:32 +02:00
test('it generates screen images and updates device', function (): void {
2025-03-03 22:20:52 +01:00
$device = Device::factory()->create();
2025-05-12 07:19:03 +02:00
$job = new GenerateScreenJob($device->id, null, view('trmnl')->render());
2025-03-03 22:20:52 +01:00
$job->handle();
// Assert the device was updated with a new image UUID
$device->refresh();
expect($device->current_screen_image)->not->toBeNull();
// Assert both PNG and BMP files were created
$uuid = $device->current_screen_image;
Storage::disk('public')->assertExists("/images/generated/{$uuid}.png");
});
2025-03-03 22:20:52 +01:00
2025-09-24 20:31:32 +02:00
test('it cleans up unused images', function (): void {
2025-03-03 22:20:52 +01:00
// Create some test devices with images
$activeDevice = Device::factory()->create([
'current_screen_image' => 'uuid-to-be-replaced',
]);
// Create some test files
Storage::disk('public')->put('/images/generated/uuid-to-be-replaced.png', 'test');
Storage::disk('public')->put('/images/generated/uuid-to-be-replaced.bmp', 'test');
Storage::disk('public')->put('/images/generated/inactive-uuid.png', 'test');
Storage::disk('public')->put('/images/generated/inactive-uuid.bmp', 'test');
// Run a job which will trigger cleanup
2025-05-12 07:19:03 +02:00
$job = new GenerateScreenJob($activeDevice->id, null, '<div>Test</div>');
2025-03-03 22:20:52 +01:00
$job->handle();
Storage::disk('public')->assertMissing('/images/generated/uuid-to-be-replaced.png');
Storage::disk('public')->assertMissing('/images/generated/uuid-to-be-replaced.bmp');
Storage::disk('public')->assertMissing('/images/generated/inactive-uuid.png');
Storage::disk('public')->assertMissing('/images/generated/inactive-uuid.bmp');
});
2025-03-03 22:20:52 +01:00
2025-09-24 20:31:32 +02:00
test('it preserves gitignore file during cleanup', function (): void {
2025-03-03 22:20:52 +01:00
Storage::disk('public')->put('/images/generated/.gitignore', '*');
$device = Device::factory()->create();
2025-05-12 07:19:03 +02:00
$job = new GenerateScreenJob($device->id, null, '<div>Test</div>');
2025-03-03 22:20:52 +01:00
$job->handle();
Storage::disk('public')->assertExists('/images/generated/.gitignore');
});
2026-04-30 13:03:32 +02:00
test('it copies plugin current image to device for processed image plugins without pipeline', function (): void {
$user = User::factory()->create();
$device = Device::factory()->create([
'user_id' => $user->id,
'current_screen_image' => 'previous-screen',
]);
$plugin = Plugin::factory()->for($user)->imageWebhook()->create([
'current_image' => 'webhook-ready-uuid',
]);
$job = new GenerateScreenJob($device->id, $plugin->id, '<div>ignored</div>');
$job->handle();
expect($device->fresh()->current_screen_image)->toBe('webhook-ready-uuid');
expect($plugin->fresh()->current_image)->toBe('webhook-ready-uuid');
});
test('it skips device update for processed image plugin when current image is missing', function (): void {
$user = User::factory()->create();
$device = Device::factory()->create([
'user_id' => $user->id,
'current_screen_image' => 'stable-screen',
]);
$plugin = Plugin::factory()->for($user)->imageWebhook()->create([
'current_image' => null,
]);
$job = new GenerateScreenJob($device->id, $plugin->id, '<div>ignored</div>');
$job->handle();
expect($device->fresh()->current_screen_image)->toBe('stable-screen');
});
test('it saves current_image_metadata for recipe plugins', function (): void {
$deviceModel = DeviceModel::factory()->create([
'width' => 800,
'height' => 480,
'rotation' => 0,
'mime_type' => 'image/png',
'palette_id' => null,
]);
$device = Device::factory()->create(['device_model_id' => $deviceModel->id]);
$plugin = Plugin::factory()->create(['plugin_type' => 'recipe']);
$job = new GenerateScreenJob($device->id, $plugin->id, '<div>Test</div>');
$job->handle();
$plugin->refresh();
expect($plugin->current_image)->not->toBeNull();
expect($plugin->current_image_metadata)->toBeArray();
expect($plugin->current_image_metadata)->toHaveKeys(['width', 'height', 'rotation', 'palette_id', 'mime_type']);
expect($plugin->current_image_metadata['width'])->toBe(800);
expect($plugin->current_image_metadata['height'])->toBe(480);
expect($plugin->current_image_metadata['mime_type'])->toBe('image/png');
expect($plugin->data_payload_updated_at)->not->toBeNull();
});