<?php

app_require('modules/selfserve/classes/selfserve_machine_signal.php');

use modules\selfserve\classes\selfserve_machine_signal;

it('normalizes Shelly input toggle ON events as machine start signals', function (): void {
    $signal = (new selfserve_machine_signal())->normalizeShellyPayload([
        'event' => 'input.toggle_on',
        'component' => 'input:0',
        'state' => true,
        'relay_id' => 'M-1',
    ]);

    expect($signal['recognized'])->toBeTrue()
        ->and($signal['on'])->toBeTrue()
        ->and($signal['component'])->toBe('input')
        ->and($signal['relay_id'])->toBe('M-1');
});

it('normalizes Shelly switch ON events and nested status payloads', function (): void {
    $service = new selfserve_machine_signal();

    $switchSignal = $service->normalizeShellyPayload([
        'event' => 'switch.on',
        'component' => 'switch:0',
        'output' => true,
    ]);
    $statusSignal = $service->normalizeShellyPayload([
        'status' => [
            'input:0' => ['state' => true],
        ],
    ]);

    expect($switchSignal['recognized'])->toBeTrue()
        ->and($switchSignal['on'])->toBeTrue()
        ->and($switchSignal['component'])->toBe('switch')
        ->and($statusSignal['recognized'])->toBeTrue()
        ->and($statusSignal['on'])->toBeTrue();
});

it('recognizes Shelly OFF events but does not treat them as billable machine starts', function (): void {
    $signal = (new selfserve_machine_signal())->normalizeShellyPayload([
        'event' => 'input.toggle_off',
        'component' => 'input:0',
        'state' => false,
    ]);

    expect($signal['recognized'])->toBeTrue()
        ->and($signal['on'])->toBeFalse();
});
