homeos/tests/Feature/DeviceManagementTest.php

71 lines
2.8 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Devices\Show;
use App\Livewire\Discovery\Index as DiscoveryIndex;
use App\Models\Device;
use App\Models\DiscoveryFinding;
use App\Models\User;
use App\Support\HostAddress;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class DeviceManagementTest extends TestCase
{
use RefreshDatabase;
public function test_host_address_uses_the_override_when_set(): void
{
config()->set('homeos.mqtt.device_host', '10.0.0.5:1883');
$this->assertSame('10.0.0.5:1883', HostAddress::broker());
// Override without a port gets the configured port appended.
config()->set('homeos.mqtt.device_host', '10.0.0.5');
config()->set('homeos.mqtt.port', 1883);
$this->assertSame('10.0.0.5:1883', HostAddress::broker());
}
public function test_host_address_falls_back_to_the_request_host(): void
{
config()->set('homeos.mqtt.device_host', null);
config()->set('homeos.mqtt.port', 1883);
$this->actingAs(User::factory()->create());
Livewire::test(\App\Livewire\Settings\Index::class)
->assertViewHas('deviceMqtt', fn ($m) => str_ends_with($m['host'], ':1883') && $m['host'] !== ':1883');
}
public function test_added_device_is_hidden_from_new_discovery_findings(): void
{
$this->actingAs(User::factory()->create());
DiscoveryFinding::create(['identifier' => 'shelly1-abc', 'name' => 'shelly1-abc', 'source' => 'mdns', 'status' => 'new']);
DiscoveryFinding::create(['identifier' => 'shelly1-new', 'name' => 'shelly1-new', 'source' => 'mdns', 'status' => 'new']);
// A device already exists for the first finding's prefix.
Device::create(['name' => 'Lampe', 'vendor' => 'Shelly', 'protocol' => 'mqtt', 'config' => ['mqtt_prefix' => 'shelly1-abc']]);
Livewire::test(DiscoveryIndex::class)
->assertViewHas('new', fn ($new) => $new->count() === 1 && $new->first()->identifier === 'shelly1-new');
}
public function test_deleting_a_device_frees_its_discovery_finding(): void
{
$this->actingAs(User::factory()->create());
$device = Device::create(['name' => 'Lampe', 'vendor' => 'Shelly', 'protocol' => 'mqtt', 'config' => ['mqtt_prefix' => 'shelly1-abc']]);
$finding = DiscoveryFinding::create(['identifier' => 'shelly1-abc', 'name' => 'shelly1-abc', 'source' => 'mdns', 'status' => 'assigned', 'device_id' => $device->id]);
Livewire::test(Show::class, ['device' => $device])
->call('deleteDevice')
->assertRedirect(route('devices.index'));
$this->assertModelMissing($device);
$finding->refresh();
$this->assertSame('new', $finding->status);
$this->assertNull($finding->device_id);
}
}