homeos/tests/Feature/HomeTest.php

88 lines
3.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Devices\Show;
use App\Models\Device;
use App\Models\DeviceState;
use App\Models\Entity;
use App\Models\Room;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class HomeTest extends TestCase
{
use RefreshDatabase;
public function test_host_page_renders_for_authenticated_user(): void
{
$this->actingAs(User::factory()->create())
->get('/host')
->assertOk()
->assertSee(__('host.title'))
->assertSee(__('host.services_title'));
}
public function test_dashboard_shows_rooms_and_device_state(): void
{
$room = Room::create(['name' => 'Testraum']);
$device = Device::create([
'name' => 'Testlampe', 'vendor' => 'Shelly', 'model' => 'Plug S',
'protocol' => 'mqtt', 'room_id' => $room->id, 'status' => 'active', 'last_seen_at' => now(),
]);
$entity = Entity::create(['device_id' => $device->id, 'type' => 'switch', 'key' => 'switch:0', 'name' => 'Testlampe']);
DeviceState::create(['entity_id' => $entity->id, 'state' => ['on' => true]]);
$this->actingAs(User::factory()->create())
->get('/dashboard')
->assertOk()
->assertSee('Testraum')
->assertSee('Testlampe');
}
public function test_dashboard_surfaces_a_low_battery_warning(): void
{
$room = Room::create(['name' => 'Testraum']);
$device = Device::create([
'name' => 'Türsensor', 'room_id' => $room->id, 'status' => 'active', 'last_seen_at' => now(),
]);
$battery = Entity::create(['device_id' => $device->id, 'type' => 'battery', 'key' => 'battery:0', 'name' => 'Batterie']);
DeviceState::create(['entity_id' => $battery->id, 'state' => ['percent' => 5]]);
$this->actingAs(User::factory()->create())
->get('/dashboard')
->assertOk()
->assertSee(__('dashboard.warnings_title'))
->assertSee('Türsensor');
}
public function test_room_less_device_appears_in_home_status(): void
{
$device = Device::create(['name' => 'Heimloser Sensor', 'status' => 'active', 'last_seen_at' => now(), 'room_id' => null]);
$battery = Entity::create(['device_id' => $device->id, 'type' => 'battery', 'key' => 'battery:0', 'name' => 'Batterie']);
DeviceState::create(['entity_id' => $battery->id, 'state' => ['percent' => 5]]);
$this->actingAs(User::factory()->create())
->get('/dashboard')
->assertOk()
->assertSee('Heimloser Sensor')
->assertSee(__('dashboard.no_room'));
}
public function test_device_room_update_rejects_invalid_room(): void
{
$device = Device::create(['name' => 'Lampe', 'status' => 'active', 'last_seen_at' => now()]);
$this->actingAs(User::factory()->create());
Livewire::test(Show::class, ['device' => $device])
->set('roomId', 999999)
->call('saveRoom')
->assertHasErrors('roomId');
$this->assertNull($device->fresh()->room_id);
}
}