Files
Enelix-EMS/tests/PrognoseFormular/manager_checks.php
T
dh 74a2d6c685
Tests / test (push) Successful in 1m1s
feat(workflow): publish verified forecast controls and consolidated docs
Approved by Daniel Haefliger for develop and beta. Author dh_Agent, authenticated account dh. Preserve published battery, charging and overall Energy Pie changes. No deployment or plant control authorization.
2026-10-08 10:02:58 +00:00

136 lines
7.2 KiB
PHP

<?php
declare(strict_types=1);
if (function_exists('IPS_GetKernelVersion')) {
throw new RuntimeException('Isolated CLI test only; never run in the Symcon kernel.');
}
class IPSModule
{
public int $InstanceID = 17004;
public array $props = [], $attrs = [], $values = [], $fields = [];
protected function ReadPropertyBoolean(string $key): bool { return (bool) ($this->props[$key] ?? false); }
protected function ReadPropertyInteger(string $key): int { return (int) ($this->props[$key] ?? 0); }
protected function ReadPropertyString(string $key): string { return (string) ($this->props[$key] ?? ''); }
protected function ReadAttributeString(string $key): string { return (string) ($this->attrs[$key] ?? ''); }
protected function ReadAttributeInteger(string $key): int { return (int) ($this->attrs[$key] ?? 0); }
protected function GetValue(string $key) { return $this->values[$key] ?? false; }
protected function UpdateFormField(string $name, string $field, $value): void { $this->fields[$name][$field] = $value; }
}
function IPS_GetInstance(int $id): array { return ['InstanceStatus' => $GLOBALS['instanceStatus']]; }
require_once __DIR__ . '/../../Manager/module.php';
final class ManagerFormHarness extends Manager
{
public array $calls = [];
public function V4ManagerAktivtestSchalten(bool $aktiv): void
{
$this->calls[] = $aktiv;
throw new RuntimeException('The normal form must never invoke the legacy trial controller.');
}
}
$count = 0;
function check(bool $ok, string $name): void
{
global $count;
if (!$ok) { throw new RuntimeException('FAIL ' . $name); }
$count++;
echo 'PASS ' . $name . PHP_EOL;
}
function fixture(): ManagerFormHarness
{
$h = new ManagerFormHarness();
$h->props = array_fill_keys(['PrognoseAktiv', 'NetzfahrplanV4SchattenAktiv',
'NetzfahrplanV4EmpfangAktiv', 'NetzfahrplanV4MessdatenAktiv', 'NetzfahrplanV4RegeltestErlaubt',
'NetzfahrplanV4AktivtestErlaubt', 'NetzfahrplanV4WatchdogVerzichtErlaubt'], true);
$h->props['NetzfahrplanAktiv'] = false;
$h->props['NetzfahrplanRegelungAktiv'] = false;
// Synthetic fixture, not a license or credential from an installation.
$h->props['Lizenzcode'] = 'ENX' . str_repeat('A', 16);
$h->attrs['LizenzcodeHash'] = hash('sha256', $h->props['Lizenzcode']);
$h->attrs['LizenzInstallationID'] = '10000000-0000-4000-8000-000000000001';
$h->attrs['LizenzLease'] = json_encode([
'status' => 'active', 'development' => true, 'installationId' => $h->attrs['LizenzInstallationID'],
'issuedAt' => gmdate('c', time() - 100), 'refreshAfter' => gmdate('c', time() + 100),
'offlineUntil' => gmdate('c', time() + 3600),
'entitlements' => [
['catalog_key' => 'manager_peak', 'quantity' => 1, 'status' => 'active'],
['catalog_key' => 'grid_schedule', 'quantity' => 1, 'status' => 'active'],
],
], JSON_THROW_ON_ERROR);
$h->attrs['NetzfahrplanV4AktivtestEnde'] = time() + 600;
$h->attrs['PrognoseV4Serverstatus'] = json_encode(['receivedAt' => time(),
'workflow' => ['planningReady' => false, 'state' => 'collecting', 'reason' => 'Messdaten werden gesammelt.']], JSON_THROW_ON_ERROR);
$h->values = ['Aktiv' => true, 'NetzfahrplanV4Aktivtest' => false];
$GLOBALS['instanceStatus'] = 102;
return $h;
}
function rejected(ManagerFormHarness $h, $value, string $name): void
{
$before = [$h->props, $h->attrs, $h->values];
try {
$h->RequestAction('FormNetzfahrplanSchalten', $value);
} catch (RuntimeException | InvalidArgumentException $e) {
check($h->calls === [] && [$h->props, $h->attrs, $h->values] === $before, $name);
return;
}
check(false, $name);
}
rejected(fixture(), true, 'old test permissions cannot bypass missing workflow readiness');
foreach ([1, 0, 'true', 'false', null, []] as $value) {
rejected(fixture(), $value, 'nonboolean compatibility action rejected: ' . gettype($value));
}
$h = fixture();
$before = [$h->props, $h->attrs, $h->values];
$h->RequestAction('FormNetzfahrplanSchalten', false);
check($h->fields['NetzfahrplanRegelungAktiv']['value'] === false, 'compatibility disable updates the saved-property form field');
check($h->calls === [], 'compatibility form action invokes no legacy trial controller');
check([$h->props, $h->attrs, $h->values] === $before, 'form preview changes no properties, authority or device values');
$h = fixture(); $h->props['Lizenzcode'] = '';
rejected($h, true, 'missing license cannot be bypassed through the old action');
$h->RequestAction('FormNetzfahrplanSchalten', false);
check($h->fields['NetzfahrplanRegelungAktiv']['value'] === false && $h->calls === [], 'disable preview remains possible after license loss');
$h = fixture(); $lease = json_decode($h->attrs['LizenzLease'], true);
$lease['entitlements'][1]['quantity'] = 0; $h->attrs['LizenzLease'] = json_encode($lease);
rejected($h, true, 'manager license alone does not grant grid schedule');
$h = fixture(); $h->props['PrognoseAktiv'] = false;
rejected($h, true, 'stored learning consent is required');
$h = fixture(); $h->attrs['PrognoseV4Serverstatus'] = '{}';
rejected($h, true, 'missing server workflow is not ready');
$h = fixture(); $h->attrs['PrognoseV4Serverstatus'] = json_encode(['receivedAt' => time() - 100,
'workflow' => ['planningReady' => true]], JSON_THROW_ON_ERROR);
rejected($h, true, 'stale server readiness cannot start control');
$h = fixture(); $h->attrs['PrognoseV4Einrichtungsfehler'] = 'Batteriezuordnung unvollstaendig.';
rejected($h, true, 'setup errors block the compatibility start action');
$h = fixture();
$h->RequestAction('FormPrognoseAktiv', false);
check(!$h->fields['NetzfahrplanRegelungAktiv']['enabled'] && $h->calls === [], 'unsaved learning disable prevents a new control request without dispatch');
check($h->fields['NetzfahrplanRegelungAktiv']['value'] === false, 'learning disable clears the form control request');
$h = fixture(); $h->props['PrognoseAktiv'] = false;
$h->RequestAction('FormPrognoseAktiv', true);
check(!$h->fields['NetzfahrplanRegelungAktiv']['enabled'] && $h->calls === [], 'unsaved learning enable does not grant stored readiness');
foreach (['license', 'setup', 'learning'] as $failure) {
$h = fixture(); $h->props['NetzfahrplanRegelungAktiv'] = true;
if ($failure === 'license') $h->props['Lizenzcode'] = '';
if ($failure === 'setup') $h->attrs['PrognoseV4Einrichtungsfehler'] = 'Zuordnung fehlt.';
if ($failure === 'learning') $h->props['PrognoseAktiv'] = false;
$h->RequestAction('FormPrognoseAktiv', false);
check($h->fields['NetzfahrplanRegelungAktiv']['enabled'] === true && $h->calls === [],
'already requested control remains switchable off after ' . $failure . ' failure');
}
$h = fixture(); $h->RequestAction('FormPrognoseAktiv', true);
check(str_contains($h->fields['PrognoseNetzfahrplanStatus']['caption'], 'Messdaten werden gesammelt.'), 'manager form displays the current workflow reason');
foreach (['PrognoseNetzfahrplanStart', 'PrognoseNetzfahrplanStop', 'PrognoseNetzfahrplanPruefen', 'NetzfahrplanV4EmpfangAktiv'] as $name) {
check(!isset($h->fields[$name]), 'form refresh no longer updates removed field: ' . $name);
}
echo "Total: $count checks. Real RequestAction and license checks; mocked IPS, no device calls.\n";