mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-03-14 12:23:33 +00:00
85 lines
3.3 KiB
PHP
85 lines
3.3 KiB
PHP
<?php
|
|
|
|
use App\Jobs\GenerateScreenJob;
|
|
use App\Models\Device;
|
|
use App\Models\DeviceModel;
|
|
use App\Models\Plugin;
|
|
use Bnussbau\TrmnlPipeline\TrmnlPipeline;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
uses(Illuminate\Foundation\Testing\RefreshDatabase::class);
|
|
|
|
beforeEach(function (): void {
|
|
TrmnlPipeline::fake();
|
|
Storage::fake('public');
|
|
Storage::disk('public')->makeDirectory('/images/generated');
|
|
});
|
|
|
|
test('it generates screen images and updates device', function (): void {
|
|
$device = Device::factory()->create();
|
|
$job = new GenerateScreenJob($device->id, null, view('trmnl')->render());
|
|
$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");
|
|
});
|
|
|
|
test('it cleans up unused images', function (): void {
|
|
// 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
|
|
$job = new GenerateScreenJob($activeDevice->id, null, '<div>Test</div>');
|
|
$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');
|
|
});
|
|
|
|
test('it preserves gitignore file during cleanup', function (): void {
|
|
Storage::disk('public')->put('/images/generated/.gitignore', '*');
|
|
|
|
$device = Device::factory()->create();
|
|
$job = new GenerateScreenJob($device->id, null, '<div>Test</div>');
|
|
$job->handle();
|
|
|
|
Storage::disk('public')->assertExists('/images/generated/.gitignore');
|
|
});
|
|
|
|
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');
|
|
});
|