UvosSmartHomeInterface/tests/unit/items/test_mqttitem.cpp

188 lines
5.6 KiB
C++

#include <QtTest/QtTest>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QFile>
#include <QDebug>
#include <QTextStream>
#include "items/mqttitem.h"
#include "mqttclient.h"
#include "programmode.h"
class TestMqttItem : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
// Setup for all tests
// Try to load config and connect to MQTT broker if configured
QString settingsPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json";
QJsonObject json;
if (QFile::exists(settingsPath)) {
QFile file(settingsPath);
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
file.close();
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
if (error.error == QJsonParseError::NoError) {
json = doc.object();
}
}
}
QJsonObject mqttJson = json["Mqtt"].toObject();
QString host = mqttJson["Host"].toString();
int port = mqttJson["Port"].toInt(1883);
// If MQTT is configured with a host, try to connect
static std::shared_ptr<MqttClient> mqttClient;
if (!host.isEmpty()) {
qDebug() << "MQTT configured:" << host << port;
mqttClient = std::make_shared<MqttClient>();
mqttClient->start(mqttJson);
// Give it a moment to connect
QTest::qWait(1000);
// Check if connected or connecting
auto qClient = mqttClient->getClient();
if (qClient && (qClient->state() == QMqttClient::Connected ||
qClient->state() == QMqttClient::Connecting)) {
qDebug() << "MQTT connected/connecting, using client";
MqttItem::client = mqttClient;
} else {
qDebug() << "MQTT connection failed, using UI_ONLY mode";
programMode = PROGRAM_MODE_UI_ONLY;
}
} else {
qDebug() << "No MQTT host configured, using UI_ONLY mode";
programMode = PROGRAM_MODE_UI_ONLY;
}
}
void testMqttItemCreation()
{
MqttItem item("test_mqtt", 0);
QCOMPARE(item.getName(), QString("test_mqtt"));
QVERIFY(item.getTopic().isEmpty());
QVERIFY(item.getValueKey() == "state");
}
void testMqttItemSetTopic()
{
MqttItem item("test_mqtt", 0);
item.setTopic("my_device");
item.setValueKey("state");
QVERIFY(item.getTopic() == "my_device");
QVERIFY(item.getValueKey() == "state");
}
void testMqttItemSetValueType()
{
MqttItem item("test_mqtt", 0);
// Default should be BOOL
QVERIFY(item.getValueType() == ITEM_VALUE_BOOL);
// Set to UINT
item.setValueType(ITEM_VALUE_UINT);
QVERIFY(item.getValueType() == ITEM_VALUE_UINT);
// Set to ENUM
item.setValueType(ITEM_VALUE_ENUM);
QVERIFY(item.getValueType() == ITEM_VALUE_ENUM);
}
void testMqttItemValueNames()
{
MqttItem item("test_mqtt", 0);
// Initially empty
QVERIFY(item.getValueNames().empty());
// Set value names
std::vector<QString> names = {"off", "heat", "cool"};
item.setValueNames(names);
auto storedNames = item.getValueNames();
QVERIFY(storedNames.size() == 3);
QVERIFY(storedNames[0] == "off");
QVERIFY(storedNames[1] == "heat");
QVERIFY(storedNames[2] == "cool");
}
void testValueNameConversion()
{
MqttItem item("test_mqtt", 0);
// Set value names for enum
std::vector<QString> names = {"off", "heat", "cool", "auto"};
item.setValueNames(names);
item.setValueType(ITEM_VALUE_ENUM);
// Test name to index
QVERIFY(item.valueNameToIndex("heat") == 1);
QVERIFY(item.valueNameToIndex("cool") == 2);
QVERIFY(item.valueNameToIndex("unknown") == -1);
// Test index to name
QVERIFY(item.indexToValueName(0) == "off");
QVERIFY(item.indexToValueName(3) == "auto");
QVERIFY(item.indexToValueName(99).isEmpty()); // Out of bounds
}
void testJsonSerialization()
{
MqttItem item("test_mqtt", 1);
item.setTopic("my_device");
item.setValueKey("state");
item.setValueOn("ON");
item.setValueOff("OFF");
QJsonObject json;
item.store(json);
QVERIFY(json["Type"] == "Mqtt");
QVERIFY(json["Topic"] == "my_device");
QVERIFY(json["ValueKey"] == "state");
QVERIFY(json["ValueOn"] == "ON");
QVERIFY(json["ValueOff"] == "OFF");
}
void testJsonDeserialization()
{
QJsonObject json;
json["Type"] = "Mqtt";
json["ItemId"] = 100;
json["Name"] = "loaded_mqtt";
json["Topic"] = "test_device";
json["ValueKey"] = "state";
json["ValueOn"] = "ON";
json["ValueOff"] = "OFF";
json["Value"] = 1;
MqttItem item;
item.load(json);
QVERIFY(item.getTopic() == "test_device");
QVERIFY(item.getValueKey() == "state");
QVERIFY(item.getValueOn() == "ON");
QVERIFY(item.getValueOff() == "OFF");
}
void cleanupTestCase()
{
// Cleanup after all tests
}
};
QTEST_APPLESS_MAIN(TestMqttItem)
#include "test_mqttitem.moc"