feat: add tests, chore: update readme

This commit is contained in:
Benjamin Nussbaum 2025-03-03 22:20:52 +01:00
parent 715e6a2562
commit e6a2bdb3bc
27 changed files with 1179 additions and 299 deletions

View file

@ -0,0 +1,158 @@
<?php
use App\Models\Device;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Laravel\Sanctum\Sanctum;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
beforeEach(function () {
Storage::fake('public');
});
test('device can fetch display data with valid credentials', function () {
$device = Device::factory()->create([
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
'current_screen_image' => 'test-image',
]);
$response = $this->withHeaders([
'id' => $device->mac_address,
'access-token' => $device->api_key,
'rssi' => -70,
'battery_voltage' => 3.8,
'fw-version' => '1.0.0',
])->get('/api/display');
$response->assertOk()
->assertJson([
'status' => '0',
'filename' => 'test-image.bmp',
'refresh_rate' => 900,
'reset_firmware' => false,
'update_firmware' => false,
'firmware_url' => null,
'special_function' => 'sleep',
]);
expect($device->fresh())
->last_rssi_level->toBe(-70)
->last_battery_voltage->toBe(3.8)
->last_firmware_version->toBe('1.0.0');
});
test('new device is auto-assigned to user with auto-assign enabled', function () {
$user = User::factory()->create(['assign_new_devices' => true]);
$response = $this->withHeaders([
'id' => '00:11:22:33:44:55',
'access-token' => 'new-device-key',
'rssi' => -70,
'battery_voltage' => 3.8,
'fw-version' => '1.0.0',
])->get('/api/display');
$response->assertOk();
$device = Device::where('mac_address', '00:11:22:33:44:55')->first();
expect($device)
->not->toBeNull()
->user_id->toBe($user->id)
->api_key->toBe('new-device-key');
});
test('device setup endpoint returns correct data', function () {
$device = Device::factory()->create([
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
'friendly_id' => 'test-device',
]);
$response = $this->withHeaders([
'id' => $device->mac_address,
])->get('/api/setup');
$response->assertOk()
->assertJson([
'api_key' => 'test-api-key',
'friendly_id' => 'test-device',
'message' => 'Welcome to TRMNL BYOS',
]);
});
test('device can submit logs', function () {
$device = Device::factory()->create([
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
]);
$logData = [
'log' => [
'logs_array' => [
['message' => 'Test log message', 'level' => 'info'],
],
],
];
$response = $this->withHeaders([
'id' => $device->mac_address,
'access-token' => $device->api_key,
])->postJson('/api/log', $logData);
$response->assertOk()
->assertJson(['status' => '0']);
expect($device->fresh()->last_log_request)
->toBe($logData);
});
// test('authenticated user can update device display', function () {
// $user = User::factory()->create();
// $device = Device::factory()->create(['user_id' => $user->id]);
//
// Sanctum::actingAs($user, ['update-screen']);
//
// $response = $this->postJson('/api/display/update', [
// 'device_id' => $device->id,
// 'markup' => '<div>Test markup</div>'
// ]);
//
// $response->assertOk();
// });
test('user cannot update display for devices they do not own', function () {
$user = User::factory()->create();
$otherUser = User::factory()->create();
$device = Device::factory()->create(['user_id' => $otherUser->id]);
Sanctum::actingAs($user, ['update-screen']);
$response = $this->postJson('/api/display/update', [
'device_id' => $device->id,
'markup' => '<div>Test markup</div>',
]);
$response->assertForbidden();
});
test('invalid device credentials return error', function () {
$response = $this->withHeaders([
'id' => 'invalid-mac',
'access-token' => 'invalid-token',
])->get('/api/display');
$response->assertNotFound()
->assertJson(['message' => 'MAC Address not registered or invalid access token']);
});
test('log endpoint requires valid device credentials', function () {
$response = $this->withHeaders([
'id' => 'invalid-mac',
'access-token' => 'invalid-token',
])->postJson('/api/log', ['log' => []]);
$response->assertNotFound()
->assertJson(['message' => 'Device not found or invalid access token']);
});

View file

@ -0,0 +1,15 @@
<?php
use App\Jobs\FetchProxyCloudResponses;
use Illuminate\Support\Facades\Bus;
test('it dispatches fetch proxy cloud responses job', function () {
// Prevent the job from actually running
Bus::fake();
// Run the command
$this->artisan('trmnl:cloud:proxy')->assertSuccessful();
// Assert that the job was dispatched
Bus::assertDispatched(FetchProxyCloudResponses::class);
});

View file

@ -0,0 +1,13 @@
<?php
use App\Jobs\GenerateScreenJob;
use Illuminate\Support\Facades\Bus;
test('it generates screen with default parameters', function () {
Bus::fake();
$this->artisan('trmnl:screen:generate')
->assertSuccessful();
Bus::assertDispatched(GenerateScreenJob::class);
});

View 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');
});

View 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();
});

View file

@ -0,0 +1,140 @@
<?php
use App\Jobs\FetchProxyCloudResponses;
use App\Models\Device;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
beforeEach(function () {
Storage::fake('public');
Storage::disk('public')->makeDirectory('/images/generated');
});
test('it fetches and processes proxy cloud responses for devices', function () {
config(['services.trmnl.proxy_base_url' => 'https://example.com']);
// Create a test device with proxy cloud enabled
$device = Device::factory()->create([
'proxy_cloud' => true,
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
'last_rssi_level' => -70,
'last_battery_voltage' => 3.7,
'default_refresh_interval' => 300,
'last_firmware_version' => '1.0.0',
]);
// Mock the API response
Http::fake([
config('services.trmnl.proxy_base_url').'/api/display' => Http::response([
'image_url' => 'https://example.com/test-image.bmp',
'filename' => 'test-image',
]),
'https://example.com/test-image.bmp' => Http::response('fake-image-content'),
]);
Http::withHeaders([
'id' => $device->mac_address,
'access-token' => $device->api_key,
'width' => 800,
'height' => 480,
'rssi' => $device->last_rssi_level,
'battery_voltage' => $device->last_battery_voltage,
'refresh-rate' => $device->default_refresh_interval,
'fw-version' => $device->last_firmware_version,
'accept-encoding' => 'identity;q=1,chunked;q=0.1,*;q=0',
'user-agent' => 'ESP32HTTPClient',
])->get(config('services.trmnl.proxy_base_url').'/api/display');
// Run the job
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert HTTP requests were made with correct headers
Http::assertSent(function ($request) use ($device) {
return $request->hasHeader('id', $device->mac_address) &&
$request->hasHeader('access-token', $device->api_key) &&
$request->hasHeader('width', 800) &&
$request->hasHeader('height', 480) &&
$request->hasHeader('rssi', $device->last_rssi_level) &&
$request->hasHeader('battery_voltage', $device->last_battery_voltage) &&
$request->hasHeader('refresh-rate', $device->default_refresh_interval) &&
$request->hasHeader('fw-version', $device->last_firmware_version);
});
// Assert the device was updated
$device->refresh();
expect($device->current_screen_image)->toBe('test-image')
->and($device->proxy_cloud_response)->toBe('{"image_url":"https:\\/\\/example.com\\/test-image.bmp","filename":"test-image"}');
// Assert the image was saved
Storage::disk('public')->assertExists('images/generated/test-image.bmp');
});
test('it handles log requests when present', function () {
$device = Device::factory()->create([
'proxy_cloud' => true,
'mac_address' => '00:11:22:33:44:55',
'api_key' => 'test-api-key',
'last_log_request' => ['message' => 'test log'],
]);
Http::fake([
config('services.trmnl.proxy_base_url').'/api/display' => Http::response([
'image_url' => 'https://example.com/test-image.bmp',
'filename' => 'test-image',
]),
'https://example.com/test-image.bmp' => Http::response('fake-image-content'),
config('services.trmnl.proxy_base_url').'/api/log' => Http::response(null, 200),
]);
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert log request was sent
Http::assertSent(function ($request) use ($device) {
return $request->url() === config('services.trmnl.proxy_base_url').'/api/log' &&
$request->hasHeader('id', $device->mac_address) &&
$request->body() === json_encode(['message' => 'test log']);
});
// Assert log request was cleared
$device->refresh();
expect($device->last_log_request)->toBeNull();
});
test('it handles API errors gracefully', function () {
$device = Device::factory()->create([
'proxy_cloud' => true,
'mac_address' => '00:11:22:33:44:55',
]);
Http::fake([
config('services.trmnl.proxy_base_url').'/api/display' => Http::response(null, 500),
]);
$job = new FetchProxyCloudResponses;
// Job should not throw exception but log error
expect(fn () => $job->handle())->not->toThrow(Exception::class);
});
test('it only processes proxy cloud enabled devices', function () {
Http::fake();
$enabledDevice = Device::factory()->create(['proxy_cloud' => true]);
$disabledDevice = Device::factory()->create(['proxy_cloud' => false]);
$job = new FetchProxyCloudResponses;
$job->handle();
// Assert request was only made for enabled device
Http::assertSent(function ($request) use ($enabledDevice) {
return $request->hasHeader('id', $enabledDevice->mac_address);
});
Http::assertNotSent(function ($request) use ($disabledDevice) {
return $request->hasHeader('id', $disabledDevice->mac_address);
});
});

View file

@ -0,0 +1,59 @@
<?php
use App\Jobs\GenerateScreenJob;
use App\Models\Device;
use Illuminate\Support\Facades\Storage;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
beforeEach(function () {
Storage::fake('public');
Storage::disk('public')->makeDirectory('/images/generated');
});
test('it generates screen images and updates device', function () {
$device = Device::factory()->create();
$job = new GenerateScreenJob($device->id, view('trmnl')->render());
$job->handle();
// Assert the device was updated with a new image UUID
$device->refresh();
expect($device->current_screen_image)->not->toBeNull();
// Assert both PNG and BMP files were created
$uuid = $device->current_screen_image;
Storage::disk('public')->assertExists("/images/generated/{$uuid}.png");
Storage::disk('public')->assertExists("/images/generated/{$uuid}.bmp");
})->skipOnGitHubActions();
test('it cleans up unused images', function () {
// Create some test devices with images
$activeDevice = Device::factory()->create([
'current_screen_image' => 'uuid-to-be-replaced',
]);
// Create some test files
Storage::disk('public')->put('/images/generated/uuid-to-be-replaced.png', 'test');
Storage::disk('public')->put('/images/generated/uuid-to-be-replaced.bmp', 'test');
Storage::disk('public')->put('/images/generated/inactive-uuid.png', 'test');
Storage::disk('public')->put('/images/generated/inactive-uuid.bmp', 'test');
// Run a job which will trigger cleanup
$job = new GenerateScreenJob($activeDevice->id, '<div>Test</div>');
$job->handle();
Storage::disk('public')->assertMissing('/images/generated/uuid-to-be-replaced.png');
Storage::disk('public')->assertMissing('/images/generated/uuid-to-be-replaced.bmp');
Storage::disk('public')->assertMissing('/images/generated/inactive-uuid.png');
Storage::disk('public')->assertMissing('/images/generated/inactive-uuid.bmp');
})->skipOnGitHubActions();
test('it preserves gitignore file during cleanup', function () {
Storage::disk('public')->put('/images/generated/.gitignore', '*');
$device = Device::factory()->create();
$job = new GenerateScreenJob($device->id, '<div>Test</div>');
$job->handle();
Storage::disk('public')->assertExists('/images/generated/.gitignore');
})->skipOnGitHubActions();

View file

@ -15,6 +15,7 @@ pest()->extend(Tests\TestCase::class)
->use(Illuminate\Foundation\Testing\RefreshDatabase::class)
->in('Feature');
registerSpatiePestHelpers();
/*
|--------------------------------------------------------------------------
| Expectations
@ -26,9 +27,9 @@ pest()->extend(Tests\TestCase::class)
|
*/
expect()->extend('toBeOne', function () {
return $this->toBe(1);
});
// expect()->extend('toBeOne', function () {
// return $this->toBe(1);
// });
/*
|--------------------------------------------------------------------------
@ -41,7 +42,7 @@ expect()->extend('toBeOne', function () {
|
*/
function something()
{
// ..
}
// function something()
// {
// // ..
// }