mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 23:18:10 +00:00
feat: add tests, chore: update readme
This commit is contained in:
parent
715e6a2562
commit
e6a2bdb3bc
27 changed files with 1179 additions and 299 deletions
76
tests/Feature/Devices/DeviceTest.php
Normal file
76
tests/Feature/Devices/DeviceTest.php
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Device;
|
||||
|
||||
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
|
||||
|
||||
test('device can be created with basic attributes', function () {
|
||||
$device = Device::factory()->create([
|
||||
'name' => 'Test Device',
|
||||
]);
|
||||
|
||||
expect($device)->toBeInstanceOf(Device::class)
|
||||
->and($device->name)->toBe('Test Device');
|
||||
});
|
||||
|
||||
test('battery percentage is calculated correctly', function () {
|
||||
$cases = [
|
||||
['voltage' => 3.0, 'expected' => 0], // Min voltage
|
||||
['voltage' => 4.2, 'expected' => 100], // Max voltage
|
||||
['voltage' => 2.9, 'expected' => 0], // Below min
|
||||
['voltage' => 4.3, 'expected' => 100], // Above max
|
||||
['voltage' => 3.6, 'expected' => 50.0], // Middle voltage
|
||||
['voltage' => 3.3, 'expected' => 25.0], // Quarter voltage
|
||||
];
|
||||
|
||||
foreach ($cases as $case) {
|
||||
$device = Device::factory()->create([
|
||||
'last_battery_voltage' => $case['voltage'],
|
||||
]);
|
||||
|
||||
expect($device->battery_percent)->toBe($case['expected'])
|
||||
->and($device->last_battery_voltage)->toBe($case['voltage']);
|
||||
}
|
||||
});
|
||||
|
||||
test('wifi strength is determined correctly', function () {
|
||||
$cases = [
|
||||
['rssi' => 0, 'expected' => 0], // No signal
|
||||
['rssi' => -90, 'expected' => 1], // Weak signal
|
||||
['rssi' => -70, 'expected' => 2], // Moderate signal
|
||||
['rssi' => -50, 'expected' => 3], // Strong signal
|
||||
];
|
||||
|
||||
foreach ($cases as $case) {
|
||||
$device = Device::factory()->create([
|
||||
'last_rssi_level' => $case['rssi'],
|
||||
]);
|
||||
|
||||
expect($device->wifi_strengh)->toBe($case['expected'])
|
||||
->and($device->last_rssi_level)->toBe($case['rssi']);
|
||||
}
|
||||
});
|
||||
|
||||
test('proxy cloud attribute is properly cast to boolean', function () {
|
||||
$device = Device::factory()->create([
|
||||
'proxy_cloud' => true,
|
||||
]);
|
||||
|
||||
expect($device->proxy_cloud)->toBeTrue();
|
||||
|
||||
$device->update(['proxy_cloud' => false]);
|
||||
expect($device->proxy_cloud)->toBeFalse();
|
||||
});
|
||||
|
||||
test('last log request is properly cast to json', function () {
|
||||
$logData = ['status' => 'success', 'timestamp' => '2024-03-04 12:00:00'];
|
||||
|
||||
$device = Device::factory()->create([
|
||||
'last_log_request' => $logData,
|
||||
]);
|
||||
|
||||
expect($device->last_log_request)
|
||||
->toBeArray()
|
||||
->toHaveKey('status')
|
||||
->toHaveKey('timestamp');
|
||||
});
|
||||
106
tests/Feature/Devices/ManageTest.php
Normal file
106
tests/Feature/Devices/ManageTest.php
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
use App\Models\Device;
|
||||
use App\Models\User;
|
||||
use Livewire\Volt\Volt;
|
||||
|
||||
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
|
||||
|
||||
test('device management page can be rendered', function () {
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)
|
||||
->get('/devices');
|
||||
|
||||
$response->assertOk();
|
||||
});
|
||||
|
||||
test('user can create a new device', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$deviceData = [
|
||||
'name' => 'Test Device',
|
||||
'mac_address' => '00:11:22:33:44:55',
|
||||
'api_key' => 'test-api-key',
|
||||
'default_refresh_interval' => 900,
|
||||
'friendly_id' => 'test-device-1',
|
||||
];
|
||||
|
||||
$response = Volt::test('devices.manage')
|
||||
->set('name', $deviceData['name'])
|
||||
->set('mac_address', $deviceData['mac_address'])
|
||||
->set('api_key', $deviceData['api_key'])
|
||||
->set('default_refresh_interval', $deviceData['default_refresh_interval'])
|
||||
->set('friendly_id', $deviceData['friendly_id'])
|
||||
->call('createDevice');
|
||||
|
||||
$response->assertHasNoErrors();
|
||||
|
||||
expect(Device::count())->toBe(1);
|
||||
|
||||
$device = Device::first();
|
||||
expect($device->name)->toBe($deviceData['name']);
|
||||
expect($device->mac_address)->toBe($deviceData['mac_address']);
|
||||
expect($device->api_key)->toBe($deviceData['api_key']);
|
||||
expect($device->default_refresh_interval)->toBe($deviceData['default_refresh_interval']);
|
||||
expect($device->friendly_id)->toBe($deviceData['friendly_id']);
|
||||
expect($device->user_id)->toBe($user->id);
|
||||
});
|
||||
|
||||
test('device creation requires required fields', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$response = Volt::test('devices.manage')
|
||||
->set('name', '')
|
||||
->set('mac_address', '')
|
||||
->set('api_key', '')
|
||||
->set('default_refresh_interval', '')
|
||||
->set('friendly_id', '')
|
||||
->call('createDevice');
|
||||
|
||||
$response->assertHasErrors([
|
||||
'mac_address',
|
||||
'api_key',
|
||||
'default_refresh_interval',
|
||||
]);
|
||||
});
|
||||
|
||||
test('user can toggle proxy cloud for their device', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
$device = Device::factory()->create([
|
||||
'user_id' => $user->id,
|
||||
'proxy_cloud' => false,
|
||||
]);
|
||||
|
||||
$response = Volt::test('devices.manage')
|
||||
->call('toggleProxyCloud', $device);
|
||||
|
||||
$response->assertHasNoErrors();
|
||||
expect($device->fresh()->proxy_cloud)->toBeTrue();
|
||||
|
||||
// Toggle back to false
|
||||
$response = Volt::test('devices.manage')
|
||||
->call('toggleProxyCloud', $device);
|
||||
|
||||
expect($device->fresh()->proxy_cloud)->toBeFalse();
|
||||
});
|
||||
|
||||
test('user cannot toggle proxy cloud for other users devices', function () {
|
||||
$user = User::factory()->create();
|
||||
$this->actingAs($user);
|
||||
|
||||
$otherUser = User::factory()->create();
|
||||
$device = Device::factory()->create([
|
||||
'user_id' => $otherUser->id,
|
||||
'proxy_cloud' => false,
|
||||
]);
|
||||
|
||||
$response = Volt::test('devices.manage')
|
||||
->call('toggleProxyCloud', $device);
|
||||
|
||||
$response->assertStatus(403);
|
||||
expect($device->fresh()->proxy_cloud)->toBeFalse();
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue