mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 15:07:49 +00:00
feat: inspect device logs
feat: create DeviceLog model on log request feat: implement logs route, logs view feat: implement details on device log timezone, latest 50 log items sort by latest device timestamp cleanup job add tests
This commit is contained in:
parent
04d089c445
commit
c045dc1e85
12 changed files with 425 additions and 0 deletions
44
tests/Feature/Jobs/CleanupDeviceLogsJobTest.php
Normal file
44
tests/Feature/Jobs/CleanupDeviceLogsJobTest.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
use App\Jobs\CleanupDeviceLogsJob;
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceLog;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
test('it keeps only the 50 most recent logs per device', function () {
|
||||
// Create two devices
|
||||
$device1 = Device::factory()->create();
|
||||
$device2 = Device::factory()->create();
|
||||
|
||||
// Create 60 logs for each device with different timestamps
|
||||
for ($i = 0; $i < 60; $i++) {
|
||||
DeviceLog::factory()->create([
|
||||
'device_id' => $device1->id,
|
||||
'device_timestamp' => now()->subMinutes($i),
|
||||
]);
|
||||
|
||||
DeviceLog::factory()->create([
|
||||
'device_id' => $device2->id,
|
||||
'device_timestamp' => now()->subMinutes($i),
|
||||
]);
|
||||
}
|
||||
|
||||
// Run the cleanup job
|
||||
CleanupDeviceLogsJob::dispatchSync();
|
||||
|
||||
// Assert each device has exactly 50 logs
|
||||
expect($device1->logs()->count())->toBe(50)
|
||||
->and($device2->logs()->count())->toBe(50);
|
||||
|
||||
// Assert the remaining logs are the most recent ones
|
||||
$device1Logs = $device1->logs()->orderByDesc('device_timestamp')->get();
|
||||
$device2Logs = $device2->logs()->orderByDesc('device_timestamp')->get();
|
||||
|
||||
// Check that the timestamps are in descending order
|
||||
for ($i = 0; $i < 49; $i++) {
|
||||
expect($device1Logs[$i]->device_timestamp->gt($device1Logs[$i + 1]->device_timestamp))->toBeTrue()
|
||||
->and($device2Logs[$i]->device_timestamp->gt($device2Logs[$i + 1]->device_timestamp))->toBeTrue();
|
||||
}
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue