Patrick Schurig cdd3bc88f3 test(mock): le relais sec, et le cas que le banc ne savait pas produire
La classe `dryRelay` commute et ne mesure pas. Elle ouvre le cas que
l'inventaire à froid avait identifié comme structurellement introuvable : une
charge SAINE et NON MESURÉE. Jusqu'ici `measurement.source: none` n'était
atteignable qu'avec un Thing manquant — ce qui traîne `available: false` et
mélange deux affirmations distinctes, « rien ne mesure cette charge » et
« cette charge est en panne ». Un relais sec est banal sur le terrain, et
parfaitement sain.

Le test vérifie ce que rien ne pouvait vérifier avant : deux charges, même
source: none, available opposés, faultCode sur la seule en défaut. Il
n'aurait pas pu s'écrire hier.

Et l'ajout a révélé que le mock avait SA PROPRE prémisse fausse :
executeAction() déréférençait le contrôleur HTTP de chaque Thing sans
vérifier, supposant que toute classe en possède un. Un relais sec n'en a pas
besoin. Le plantage fut immédiat et net ; l'hypothèse, elle, était invisible
tant qu'aucune classe ne la contredisait — l'homogénéité du mock la
protégeait, exactement comme elle masquait les cas qu'il ne savait pas
produire. Ajouter de la diversité au banc n'a pas seulement ouvert un test :
ça a trouvé un défaut.

AGENTS.md reçoit la règle générale sur les identités : une identité dont les
deux termes viennent de la même source ne vérifie rien. Elle est vraie par
construction et rassure là où rien ne protège — la question n'est pas
« tient-elle ? » mais « de quoi parle-t-elle vraiment ? ». Et c'est le même
défaut que la contre-épreuve qui ne mord pas, vu de l'autre côté : perturber
une grandeur qui ne pouvait rien changer, et vérifier une identité qui se
referme sur elle-même, sont deux gestes de vérification qui ne touchent rien.
Le réflexe commun : demander ce que ce contrôle aurait attrapé s'il avait
échoué. S'il n'y a pas de réponse, il ne contrôle rien.

Suite : simulation 62/62, loadmodel 22/22, charging 48/48 identique à la
référence, spotmarket 32/32, doxygen 0 avertissement.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015F7G5VeaPVSMeVNjiGj36p
2026-09-02 08:00:51 +02:00

135 lines
6.4 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (C) 2013 - 2024, nymea GmbH
* Copyright (C) 2024 - 2025, chargebyte austria GmbH
*
* This file is part of nymea-energy-plugin-nymea.
*
* nymea-energy-plugin-nymea.s free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nymea-energy-plugin-nymea.s distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with nymea-energy-plugin-nymea. If not, see <https://www.gnu.org/licenses/>.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef ENERGYTESTBASE_H
#define ENERGYTESTBASE_H
#include <QObject>
#include <QtTest>
#include <QDateTime>
#include <QTcpSocket>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QLoggingCategory>
#include <nymeatestbase.h>
#include <nymeacore.h>
#include <nymeasettings.h>
#include <servers/mocktcpserver.h>
#include <energymanager.h>
static QUuid mockMeterThingClassId = QUuid("2721a051-6e12-471a-baba-21d87c4cebc9");
static QUuid mockChargerThingClassId = QUuid("5a3ae99f-c7da-46df-9104-f477be4606b7");
static QUuid mockChargerWithPhaseSwitchingThingClassId = QUuid("9208d9f0-280c-469d-a145-106f3277470c");
static QUuid mockSimpleChargerThingClassId = QUuid("29bcf255-b654-4764-be92-399bc26fe7c3");
static QUuid mockCarThingClassId = QUuid("4513f801-836e-40a7-8784-c02650a9bdc6");
static QUuid mockEnergyStorageThingClassId = QUuid("d0d5bbf0-249c-46ed-ac6a-5f271b2b0b0f");
static QUuid mockPowerSwitchThingClassId = QUuid("841f8905-d1d7-4053-909f-01123b497747");
//! \brief Relais SEC — il commute, il ne mesure pas. Ouvre le cas « charge saine et NON
//! mesurée », que le mock ne savait pas produire : toutes les classes pilotables portaient une
//! puissance, si bien qu'aucun test ne pouvait distinguer « non mesurée » de « en défaut »
//! (docs/INVENTAIRE_MOCK.md).
static QUuid mockDryRelayThingClassId = QUuid("5b2c7e41-9a06-4d3f-b8c1-6e0f2a7d4b39");
using namespace nymeaserver;
class EnergyTestBase : public NymeaTestBase
{
Q_OBJECT
public:
explicit EnergyTestBase(QObject *parent = nullptr);
inline void verifyThingError(const QVariant &response, Thing::ThingError error = Thing::ThingErrorNoError) {
verifyError(response, "thingError", enumValueName(error));
}
inline void verifyEnergyError(const QVariant &response, EnergyManager::EnergyError error = EnergyManager::EnergyErrorNoError) {
verifyError(response, "energyError", enumValueName(error));
}
static QDateTime utcDateTime(const QDate &date, const QTime &time);
protected slots:
void initTestCase(const QString &loggingRules = QString(), bool checkExperienceLoaded = true, bool disableLogEngine = true);
void cleanupTestCase();
void init();
void cleanup();
public:
QNetworkReply *setMeterStates(const QVariantMap &phasesPower, bool connected = true, quint16 port = 26655);
QNetworkReply *setCarStates(uint batteryLevel, uint capacity, uint minChargingCurrent, uint phaseCount, quint16 port = 26656);
QNetworkReply *setChargerStates(bool connected, bool power, bool pluggedIn, const QString &phases, int maxChargingCurrent, int maxChargingCurrentMaxValue, quint16 port = 26657);
QNetworkReply *setChargerWithPhaseCountSwitchingStates(bool connected, bool power, bool pluggedIn, const QString &phases, int maxChargingCurrent, int maxChargingCurrentMaxValue, uint desiredPhaseCount, quint16 port = 26658);
QNetworkReply *setSimpleChargerStates(bool connected, bool power, bool pluggedIn, int phaseCount, int maxChargingCurrent, quint16 port = 26659);
QNetworkReply *setEnergyStorageStates(uint batteryLevel, int currentPower, quint16 port = 26660);
QNetworkReply *setPowerSwitchStates(bool power, double currentPower, quint16 port = 26661);
QNetworkReply *getActionHistory(quint16 port);
QNetworkReply *clearActionHistroy(quint16 port);
QUuid addMeter(quint16 port = 26655);
QUuid addCar(quint16 port = 26656);
QUuid addCharger(const QString &phases = "All", double maxChargingCurrentUpperLimit = 32, quint16 port = 26657);
QUuid addChargerWithPhaseCountSwitching(const QString &phases = "All", double maxChargingCurrentUpperLimit = 32, quint16 port = 26658);
QUuid addSimpleCharger(double maxChargingCurrentUpperLimit = 32, quint16 port = 26659);
QUuid addEnergyStorage(uint capacity = 10, double maxChargingPowerUpperLimit = 5000, double maxDischargingPowerUpperLimit = 11500, quint16 port = 26660);
QUuid addPowerSwitch(double nominalPower = 2000, quint16 port = 26661);
/*!
* \brief Ajoute un RELAIS SEC : il commute, il ne mesure pas.
* \param port Port d'identification (aucun contrôleur n'écoute — la classe n'en a pas besoin).
* \return L'identifiant du Thing créé.
* \note Sans cette classe, une charge « saine mais non mesurée » était impossible à
* fabriquer, et `measurement.source: none` n'était atteignable qu'avec un Thing MANQUANT —
* ce qui traîne `available: false` et mélange deux affirmations distinctes.
*/
QUuid addDryRelay(quint16 port = 26680);
//! Mock thing implémentant l'interface etmvariableload (states currentPowerW / powerSetpoint).
QUuid addEtmVariableLoad(quint16 port = 27001);
void removeDevices();
QVariant removeDevice(const QUuid &thingId);
protected:
QNetworkAccessManager *m_networkAccessManager = nullptr;
quint16 m_mockMeterDefaultPort = 26655;
quint16 m_mockCarDefaultPort = 26656;
quint16 m_mockChargerDefaultPort = 26657;
quint16 m_mockChargerWithPhaseCountSwitchingDefaultPort = 26658;
quint16 m_mockSimpleChargerDefaultPort = 26659;
quint16 m_mockEnergyStorageDefaultPort = 26660;
quint16 m_mockPowerSwitchDefaultPort = 26661;
bool verifyActionExecuted(const QVariantList &actionHistory, const QString &actionName);
QVariant getLastValueFromExecutedAction(const QVariantList &actionHistory, const QString &actionName, const QString &paramName);
};
#endif // ENERGYTESTBASE_H