homeos/tests/Feature/DeviceInputRoleTest.php

56 lines
2.0 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\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;
class DeviceInputRoleTest extends TestCase
{
use RefreshDatabase;
private function inputDevice(): array
{
$device = Device::create(['name' => 'Shelly', 'vendor' => 'Shelly', 'protocol' => 'mqtt', 'status' => 'active', 'config' => ['mqtt_prefix' => 'shelly1-x']]);
$entity = Entity::create(['device_id' => $device->id, 'type' => 'input', 'key' => 'input:0']);
DeviceState::create(['entity_id' => $entity->id, 'state' => ['on' => true]]); // raw input = true
return [$device, $entity];
}
public function test_assigning_and_inverting_a_contact_role_is_reversible(): void
{
$this->actingAs(User::factory()->create());
[$device, $entity] = $this->inputDevice();
$page = Livewire::test(Show::class, ['device' => $device]);
// Promote to a window contact (raw on=true, no invert → open).
$page->call('setInputRole', $entity->id, 'window');
$entity->refresh()->load('state');
$this->assertSame('contact', $entity->type);
$this->assertTrue($entity->state->state['open']);
// Invert → closed (raw true, inverted).
$page->call('setInputInvert', $entity->id, true);
$this->assertFalse($entity->refresh()->state->state['open']);
// Un-invert → open again: the raw value was preserved, so this round-trips.
$page->call('setInputInvert', $entity->id, false);
$this->assertTrue($entity->refresh()->state->state['open']);
// Demote back to a plain input → raw on restored.
$page->call('setInputRole', $entity->id, '');
$entity->refresh();
$this->assertSame('input', $entity->type);
$this->assertSame(['on' => true], $entity->state->state);
$this->assertNull(data_get($device->fresh()->config, 'input_roles.0'));
}
}