diff --git a/app/Console/Commands/FirmwareCheckCommand.php b/app/Console/Commands/FirmwareCheckCommand.php index be36824..f407314 100644 --- a/app/Console/Commands/FirmwareCheckCommand.php +++ b/app/Console/Commands/FirmwareCheckCommand.php @@ -5,6 +5,7 @@ namespace App\Console\Commands; use App\Jobs\FirmwarePollJob; use App\Models\Firmware; use Illuminate\Console\Command; + use function Laravel\Prompts\spin; use function Laravel\Prompts\table; diff --git a/app/Console/Commands/FirmwareUpdateCommand.php b/app/Console/Commands/FirmwareUpdateCommand.php index 66f3640..97d9d58 100644 --- a/app/Console/Commands/FirmwareUpdateCommand.php +++ b/app/Console/Commands/FirmwareUpdateCommand.php @@ -45,27 +45,25 @@ class FirmwareUpdateCommand extends Command ...Device::all()->mapWithKeys(function ($device) { // without _ returns index return ["_$device->id" => "$device->name (Current version: $device->last_firmware_version)"]; - })->toArray() + })->toArray(), ], scroll: 10 ); - - if (empty($devices)) { $this->error('No devices selected. Aborting.'); + return; } if (in_array('all', $devices)) { $devices = Device::pluck('id')->toArray(); } else { - $devices = array_map(function($selected) { + $devices = array_map(function ($selected) { return (int) str_replace('_', '', $selected); }, $devices); } - foreach ($devices as $deviceId) { Device::find($deviceId)->update(['update_firmware_id' => $firmwareVersion]); diff --git a/app/Jobs/CleanupDeviceLogsJob.php b/app/Jobs/CleanupDeviceLogsJob.php index c1a2096..b49f507 100644 --- a/app/Jobs/CleanupDeviceLogsJob.php +++ b/app/Jobs/CleanupDeviceLogsJob.php @@ -8,7 +8,6 @@ use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\DB; class CleanupDeviceLogsJob implements ShouldQueue { diff --git a/app/Jobs/FirmwareDownloadJob.php b/app/Jobs/FirmwareDownloadJob.php index 39cf199..9db7c82 100644 --- a/app/Jobs/FirmwareDownloadJob.php +++ b/app/Jobs/FirmwareDownloadJob.php @@ -44,4 +44,4 @@ class FirmwareDownloadJob implements ShouldQueue Log::error('An unexpected error occurred: '.$e->getMessage()); } } -} \ No newline at end of file +} diff --git a/app/Jobs/FirmwarePollJob.php b/app/Jobs/FirmwarePollJob.php index d2d7a12..9d6f169 100644 --- a/app/Jobs/FirmwarePollJob.php +++ b/app/Jobs/FirmwarePollJob.php @@ -27,8 +27,9 @@ class FirmwarePollJob implements ShouldQueue try { $response = Http::get('https://usetrmnl.com/api/firmware/latest')->json(); - if (!is_array($response) || !isset($response['version']) || !isset($response['url'])) { + if (! is_array($response) || ! isset($response['version']) || ! isset($response['url'])) { \Log::error('Invalid firmware response format received'); + return; } diff --git a/database/factories/DeviceLogFactory.php b/database/factories/DeviceLogFactory.php index 1250efc..10871d0 100644 --- a/database/factories/DeviceLogFactory.php +++ b/database/factories/DeviceLogFactory.php @@ -14,7 +14,7 @@ class DeviceLogFactory extends Factory public function definition(): array { return [ - 'log_entry' => ["creation_timestamp"=>fake()->dateTimeBetween('-1 month', 'now')->getTimestamp(),"device_status_stamp"=>["wifi_rssi_level"=>-65,"wifi_status"=>"connected","refresh_rate"=>900,"time_since_last_sleep_start"=>901,"current_fw_version"=>"1.5.5","special_function"=>"none","battery_voltage"=>4.052,"wakeup_reason"=>"timer","free_heap_size"=>215128,"max_alloc_size"=>192500],"log_id"=>17,"log_message"=>"Error fetching API display: 7, detail: HTTP Client failed with error: connection refused(-1)","log_codeline"=>586,"log_sourcefile"=>"src\/bl.cpp","additional_info"=>["filename_current"=>"UUID.png","filename_new"=>null,"retry_attempt"=>5]], + 'log_entry' => ['creation_timestamp' => fake()->dateTimeBetween('-1 month', 'now')->getTimestamp(), 'device_status_stamp' => ['wifi_rssi_level' => -65, 'wifi_status' => 'connected', 'refresh_rate' => 900, 'time_since_last_sleep_start' => 901, 'current_fw_version' => '1.5.5', 'special_function' => 'none', 'battery_voltage' => 4.052, 'wakeup_reason' => 'timer', 'free_heap_size' => 215128, 'max_alloc_size' => 192500], 'log_id' => 17, 'log_message' => 'Error fetching API display: 7, detail: HTTP Client failed with error: connection refused(-1)', 'log_codeline' => 586, 'log_sourcefile' => "src\/bl.cpp", 'additional_info' => ['filename_current' => 'UUID.png', 'filename_new' => null, 'retry_attempt' => 5]], 'device_timestamp' => fake()->dateTimeBetween('-1 month', 'now'), 'created_at' => Carbon::now(), 'updated_at' => Carbon::now(), diff --git a/database/migrations/2025_06_01_195732_create_device_logs_table.php b/database/migrations/2025_06_01_195732_create_device_logs_table.php index 1fe3122..ef89f3e 100644 --- a/database/migrations/2025_06_01_195732_create_device_logs_table.php +++ b/database/migrations/2025_06_01_195732_create_device_logs_table.php @@ -5,7 +5,8 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration { +return new class extends Migration +{ public function up(): void { Schema::create('device_logs', function (Blueprint $table) { diff --git a/tests/Feature/DeviceConfigureTest.php b/tests/Feature/DeviceConfigureTest.php index 95e3d2b..85b1fd3 100644 --- a/tests/Feature/DeviceConfigureTest.php +++ b/tests/Feature/DeviceConfigureTest.php @@ -5,6 +5,7 @@ namespace Tests\Feature; use App\Models\Device; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; + use function Pest\Laravel\actingAs; uses(RefreshDatabase::class); diff --git a/tests/Feature/Jobs/FirmwareDownloadJobTest.php b/tests/Feature/Jobs/FirmwareDownloadJobTest.php index 30d9e29..4f5fd79 100644 --- a/tests/Feature/Jobs/FirmwareDownloadJobTest.php +++ b/tests/Feature/Jobs/FirmwareDownloadJobTest.php @@ -2,7 +2,6 @@ use App\Jobs\FirmwareDownloadJob; use App\Models\Firmware; -use Illuminate\Http\Client\ConnectionException; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Storage; diff --git a/tests/Feature/Jobs/FirmwarePollJobTest.php b/tests/Feature/Jobs/FirmwarePollJobTest.php index 4b91180..27e91b5 100644 --- a/tests/Feature/Jobs/FirmwarePollJobTest.php +++ b/tests/Feature/Jobs/FirmwarePollJobTest.php @@ -13,11 +13,11 @@ test('it creates new firmware record when polling', function () { Http::fake([ 'usetrmnl.com/api/firmware/latest' => Http::response([ 'version' => '1.0.0', - 'url' => 'https://example.com/firmware.bin' - ], 200) + 'url' => 'https://example.com/firmware.bin', + ], 200), ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); expect(Firmware::where('version_tag', '1.0.0')->exists())->toBeTrue() ->and(Firmware::where('version_tag', '1.0.0')->first()) @@ -29,17 +29,17 @@ test('it updates existing firmware record when polling', function () { $existingFirmware = Firmware::factory()->create([ 'version_tag' => '1.0.0', 'url' => 'https://old-url.com/firmware.bin', - 'latest' => true + 'latest' => true, ]); Http::fake([ 'usetrmnl.com/api/firmware/latest' => Http::response([ 'version' => '1.0.0', - 'url' => 'https://new-url.com/firmware.bin' - ], 200) + 'url' => 'https://new-url.com/firmware.bin', + ], 200), ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); expect($existingFirmware->fresh()) ->url->toBe('https://new-url.com/firmware.bin') @@ -49,17 +49,17 @@ test('it updates existing firmware record when polling', function () { test('it marks previous firmware as not latest when new version is found', function () { $oldFirmware = Firmware::factory()->create([ 'version_tag' => '1.0.0', - 'latest' => true + 'latest' => true, ]); Http::fake([ 'usetrmnl.com/api/firmware/latest' => Http::response([ 'version' => '1.1.0', - 'url' => 'https://example.com/firmware.bin' - ], 200) + 'url' => 'https://example.com/firmware.bin', + ], 200), ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); expect($oldFirmware->fresh()->latest)->toBeFalse() ->and(Firmware::where('version_tag', '1.1.0')->first()->latest)->toBeTrue(); @@ -69,10 +69,10 @@ test('it handles connection exception gracefully', function () { Http::fake([ 'usetrmnl.com/api/firmware/latest' => function () { throw new ConnectionException('Connection failed'); - } + }, ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); // Verify no firmware records were created expect(Firmware::count())->toBe(0); @@ -80,10 +80,10 @@ test('it handles connection exception gracefully', function () { test('it handles invalid response gracefully', function () { Http::fake([ - 'usetrmnl.com/api/firmware/latest' => Http::response(null, 200) + 'usetrmnl.com/api/firmware/latest' => Http::response(null, 200), ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); // Verify no firmware records were created expect(Firmware::count())->toBe(0); @@ -92,11 +92,11 @@ test('it handles invalid response gracefully', function () { test('it handles missing version in response gracefully', function () { Http::fake([ 'usetrmnl.com/api/firmware/latest' => Http::response([ - 'url' => 'https://example.com/firmware.bin' - ], 200) + 'url' => 'https://example.com/firmware.bin', + ], 200), ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); // Verify no firmware records were created expect(Firmware::count())->toBe(0); @@ -105,11 +105,11 @@ test('it handles missing version in response gracefully', function () { test('it handles missing url in response gracefully', function () { Http::fake([ 'usetrmnl.com/api/firmware/latest' => Http::response([ - 'version' => '1.0.0' - ], 200) + 'version' => '1.0.0', + ], 200), ]); - (new FirmwarePollJob())->handle(); + (new FirmwarePollJob)->handle(); // Verify no firmware records were created expect(Firmware::count())->toBe(0); diff --git a/tests/Unit/Models/DeviceLogTest.php b/tests/Unit/Models/DeviceLogTest.php index 545641f..d34e8d7 100644 --- a/tests/Unit/Models/DeviceLogTest.php +++ b/tests/Unit/Models/DeviceLogTest.php @@ -20,8 +20,8 @@ test('device log casts log_entry to array', function () { 'log_entry' => [ 'message' => 'test message', 'level' => 'info', - 'timestamp' => time() - ] + 'timestamp' => time(), + ], ]); expect($log->log_entry)->toBeArray() @@ -33,7 +33,7 @@ test('device log casts device_timestamp to datetime', function () { Device::factory()->create(); $timestamp = now(); $log = DeviceLog::factory()->create([ - 'device_timestamp' => $timestamp + 'device_timestamp' => $timestamp, ]); expect($log->device_timestamp)->toBeInstanceOf(\Carbon\Carbon::class) @@ -56,8 +56,8 @@ test('device log can be created with minimal required fields', function () { 'device_id' => $device->id, 'device_timestamp' => now(), 'log_entry' => [ - 'message' => 'test message' - ] + 'message' => 'test message', + ], ]); expect($log->exists)->toBeTrue()