100 lines
5.0 KiB
C++
100 lines
5.0 KiB
C++
// Verifica locale del nucleo di harbour-aira, senza SDK Sailfish.
|
|
// Copre la parte pura (src/airajson.cpp): normalizzazione dell'indirizzo,
|
|
// lettura del riassunto, etichette di stato, corpo dei comandi, messaggi
|
|
// d'errore. Il networking e la UI si verificano sul device.
|
|
//
|
|
// Build: ~/Qt/5.15.2/gcc_64/bin/qmake tests/core_test.pro && make && ./core_test
|
|
#include <QCoreApplication>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QDebug>
|
|
#include <QStringList>
|
|
|
|
#include "../src/airajson.h"
|
|
|
|
static int failures = 0;
|
|
static int checks = 0;
|
|
|
|
static void check(bool cond, const QString &what)
|
|
{
|
|
++checks;
|
|
if (!cond) {
|
|
++failures;
|
|
qWarning() << "FALLITO:" << what;
|
|
} else {
|
|
qInfo() << "ok:" << what;
|
|
}
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication app(argc, argv);
|
|
|
|
// ---- normalizeBaseUrl ------------------------------------------------- //
|
|
check(AiraJson::normalizeBaseUrl("192.168.2.50:8790") == "http://192.168.2.50:8790",
|
|
"host:porta riceve lo schema http");
|
|
check(AiraJson::normalizeBaseUrl("http://192.168.2.50:8790") == "http://192.168.2.50:8790",
|
|
"schema http preservato");
|
|
check(AiraJson::normalizeBaseUrl("https://aira.example.tld/bridge/") == "https://aira.example.tld/bridge",
|
|
"https preservato, slash finale tolto");
|
|
check(AiraJson::normalizeBaseUrl(" 192.168.2.50:8790// ") == "http://192.168.2.50:8790",
|
|
"spazi e slash ripetuti normalizzati");
|
|
check(AiraJson::normalizeBaseUrl("").isEmpty(), "stringa vuota resta vuota (nessuna richiesta)");
|
|
check(AiraJson::normalizeBaseUrl(" ").isEmpty(), "solo spazi = vuoto");
|
|
|
|
// ---- parseSummary ----------------------------------------------------- //
|
|
const QByteArray good = R"({"ok":true,"summary":{"outdoor_temperature":18.1,"dhw_current":51.0,"dhw_target":55.0,"operating_status":"OPERATING_STATUS_AUTO","away_mode":false,"errors_count":0}})";
|
|
bool ok = false;
|
|
QVariantMap m = AiraJson::parseSummary(good, &ok);
|
|
check(ok, "riassunto valido riconosciuto");
|
|
check(qFuzzyCompare(m.value("dhw_current").toDouble(), 51.0), "dhw_current letto");
|
|
check(m.value("operating_status").toString() == "OPERATING_STATUS_AUTO", "operating_status letto");
|
|
|
|
QVariantMap bad = AiraJson::parseSummary("non sono json", &ok);
|
|
check(!ok && bad.isEmpty(), "JSON non valido -> vuoto con ok=false");
|
|
|
|
QVariantMap err = AiraJson::parseSummary(R"({"ok":false,"error":"token errato"})", &ok);
|
|
check(!ok && err.isEmpty(), "risposta di errore -> vuoto");
|
|
check(AiraJson::parseSummary(R"({"ok":true})", &ok).isEmpty() && !ok,
|
|
"ok=true senza summary -> vuoto");
|
|
|
|
// ---- modeLabel -------------------------------------------------------- //
|
|
check(AiraJson::modeLabel("OPERATING_STATUS_AUTO") == "Automatico", "AUTO -> Automatico");
|
|
check(AiraJson::modeLabel("OPERATING_STATUS_HOT_WATER") == "Acqua calda", "acqua calda");
|
|
check(AiraJson::modeLabel("") == "—", "stato vuoto -> trattino");
|
|
check(AiraJson::modeLabel("PUMP_MODE_STATE_HEATING") == "Riscaldamento",
|
|
"stato zona: riscaldamento");
|
|
check(AiraJson::modeLabel("PUMP_MODE_STATE_IDLE") == "In attesa",
|
|
"stato zona: in attesa");
|
|
check(AiraJson::modeLabel("PUMP_MODE_STATE_IGNOTO") == "ignoto",
|
|
"stato zona sconosciuto -> senza prefisso");
|
|
check(AiraJson::modeLabel("OPERATING_STATUS_DEFROST") == "defrost",
|
|
"stato sconosciuto: prefisso tolto, minuscolo");
|
|
|
|
// ---- formatValue ------------------------------------------------------ //
|
|
check(AiraJson::formatValue(QVariant(51.0)) == "51.0", "numero a un decimale");
|
|
check(AiraJson::formatValue(QVariant(18.14)) == "18.1", "arrotondamento a un decimale");
|
|
check(AiraJson::formatValue(QVariant()) == "—", "valore assente -> trattino");
|
|
check(AiraJson::formatValue(QVariant(QString())) == "—", "stringa vuota -> trattino");
|
|
|
|
// ---- commandPayload --------------------------------------------------- //
|
|
const QJsonObject c1 = QJsonDocument::fromJson(
|
|
AiraJson::commandPayload("set_hot_water_target", 55)).object();
|
|
check(c1.value("id").toString() == "set_hot_water_target", "id nel corpo del comando");
|
|
check(c1.value("value").toInt() == 55, "valore numerico nel corpo");
|
|
|
|
const QJsonObject c2 = QJsonDocument::fromJson(
|
|
AiraJson::commandPayload("hot_water_boost_on", QVariant())).object();
|
|
check(c2.contains("value") == false, "comando senza parametri: nessun campo value");
|
|
|
|
// ---- errorMessage ----------------------------------------------------- //
|
|
check(AiraJson::errorMessage(R"({"ok":false,"error":"token mancante o errato"})", 401)
|
|
== "token mancante o errato", "messaggio d'errore dal corpo");
|
|
check(AiraJson::errorMessage("", 502) == "errore HTTP 502", "fallback sullo stato HTTP");
|
|
check(AiraJson::errorMessage("", 0) == "server non raggiungibile", "stato 0 = irraggiungibile");
|
|
|
|
qInfo() << "----";
|
|
qInfo() << checks - failures << "/" << checks << "verifiche superate";
|
|
return failures == 0 ? 0 : 1;
|
|
}
|