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();
|
||||
}
|
||||
});
|
||||
66
tests/Unit/Models/DeviceLogTest.php
Normal file
66
tests/Unit/Models/DeviceLogTest.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Models\DeviceLog;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
test('device log belongs to a device', function () {
|
||||
$device = Device::factory()->create();
|
||||
$log = DeviceLog::factory()->create(['device_id' => $device->id]);
|
||||
|
||||
expect($log->device)->toBeInstanceOf(Device::class)
|
||||
->and($log->device->id)->toBe($device->id);
|
||||
});
|
||||
|
||||
test('device log casts log_entry to array', function () {
|
||||
Device::factory()->create();
|
||||
$log = DeviceLog::factory()->create([
|
||||
'log_entry' => [
|
||||
'message' => 'test message',
|
||||
'level' => 'info',
|
||||
'timestamp' => time()
|
||||
]
|
||||
]);
|
||||
|
||||
expect($log->log_entry)->toBeArray()
|
||||
->and($log->log_entry['message'])->toBe('test message')
|
||||
->and($log->log_entry['level'])->toBe('info');
|
||||
});
|
||||
|
||||
test('device log casts device_timestamp to datetime', function () {
|
||||
Device::factory()->create();
|
||||
$timestamp = now();
|
||||
$log = DeviceLog::factory()->create([
|
||||
'device_timestamp' => $timestamp
|
||||
]);
|
||||
|
||||
expect($log->device_timestamp)->toBeInstanceOf(\Carbon\Carbon::class)
|
||||
->and($log->device_timestamp->timestamp)->toBe($timestamp->timestamp);
|
||||
});
|
||||
|
||||
test('device log factory creates valid data', function () {
|
||||
Device::factory()->create();
|
||||
$log = DeviceLog::factory()->create();
|
||||
|
||||
expect($log->device_id)->toBeInt()
|
||||
->and($log->device_timestamp)->toBeInstanceOf(\Carbon\Carbon::class)
|
||||
->and($log->log_entry)->toBeArray()
|
||||
->and($log->log_entry)->toHaveKeys(['creation_timestamp', 'device_status_stamp', 'log_id', 'log_message', 'log_codeline', 'log_sourcefile', 'additional_info']);
|
||||
});
|
||||
|
||||
test('device log can be created with minimal required fields', function () {
|
||||
$device = Device::factory()->create();
|
||||
$log = DeviceLog::create([
|
||||
'device_id' => $device->id,
|
||||
'device_timestamp' => now(),
|
||||
'log_entry' => [
|
||||
'message' => 'test message'
|
||||
]
|
||||
]);
|
||||
|
||||
expect($log->exists)->toBeTrue()
|
||||
->and($log->device_id)->toBe($device->id)
|
||||
->and($log->log_entry['message'])->toBe('test message');
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue