mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat(#17): add commands and jobs to poll, download and update firmware
feat(#17): add commands and jobs to poll, download and update firmware feat(#17): update firmware modal feat(#17): add tests
This commit is contained in:
parent
93aac51182
commit
87b9b57c3d
13 changed files with 567 additions and 3 deletions
116
tests/Feature/Jobs/FirmwarePollJobTest.php
Normal file
116
tests/Feature/Jobs/FirmwarePollJobTest.php
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
use App\Jobs\FirmwarePollJob;
|
||||
use App\Models\Firmware;
|
||||
use Illuminate\Http\Client\ConnectionException;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
beforeEach(function () {
|
||||
Http::preventStrayRequests();
|
||||
});
|
||||
|
||||
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)
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
expect(Firmware::where('version_tag', '1.0.0')->exists())->toBeTrue()
|
||||
->and(Firmware::where('version_tag', '1.0.0')->first())
|
||||
->url->toBe('https://example.com/firmware.bin')
|
||||
->latest->toBeTrue();
|
||||
});
|
||||
|
||||
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
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'usetrmnl.com/api/firmware/latest' => Http::response([
|
||||
'version' => '1.0.0',
|
||||
'url' => 'https://new-url.com/firmware.bin'
|
||||
], 200)
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
expect($existingFirmware->fresh())
|
||||
->url->toBe('https://new-url.com/firmware.bin')
|
||||
->latest->toBeTrue();
|
||||
});
|
||||
|
||||
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
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'usetrmnl.com/api/firmware/latest' => Http::response([
|
||||
'version' => '1.1.0',
|
||||
'url' => 'https://example.com/firmware.bin'
|
||||
], 200)
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
expect($oldFirmware->fresh()->latest)->toBeFalse()
|
||||
->and(Firmware::where('version_tag', '1.1.0')->first()->latest)->toBeTrue();
|
||||
});
|
||||
|
||||
test('it handles connection exception gracefully', function () {
|
||||
Http::fake([
|
||||
'usetrmnl.com/api/firmware/latest' => function () {
|
||||
throw new ConnectionException('Connection failed');
|
||||
}
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
// Verify no firmware records were created
|
||||
expect(Firmware::count())->toBe(0);
|
||||
});
|
||||
|
||||
test('it handles invalid response gracefully', function () {
|
||||
Http::fake([
|
||||
'usetrmnl.com/api/firmware/latest' => Http::response(null, 200)
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
// Verify no firmware records were created
|
||||
expect(Firmware::count())->toBe(0);
|
||||
});
|
||||
|
||||
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)
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
// Verify no firmware records were created
|
||||
expect(Firmware::count())->toBe(0);
|
||||
});
|
||||
|
||||
test('it handles missing url in response gracefully', function () {
|
||||
Http::fake([
|
||||
'usetrmnl.com/api/firmware/latest' => Http::response([
|
||||
'version' => '1.0.0'
|
||||
], 200)
|
||||
]);
|
||||
|
||||
(new FirmwarePollJob())->handle();
|
||||
|
||||
// Verify no firmware records were created
|
||||
expect(Firmware::count())->toBe(0);
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue