#pragma once #include #include #include "mqtt/MqttClient.h" // MqttClient for unit tests: records what is published and subscribed; // the test drives the connection state and incoming messages. class FakeMqttClient : public MqttClient { Q_OBJECT public: struct Message { QString topic; QByteArray payload; bool retain = false; }; using MqttClient::MqttClient; void connectToBroker() override { ++connectCalls; } bool disconnectFromBroker(int) override { setState(State::Disconnected); return true; } bool publish(const QString& topic, const QByteArray& payload, bool retain) override { if (m_state != State::Connected) return false; published.append({topic, payload, retain}); return true; } bool subscribe(const QString& topic) override { if (m_state != State::Connected) return false; subscriptions.append(topic); return true; } bool waitForPublished(int) override { return true; } State state() const override { return m_state; } // Test side. void setState(State state, const QString& error = {}) { m_state = state; emit connectionChanged(state, error); } void deliver(const QString& topic, const QByteArray& payload) { emit messageReceived(topic, payload); } // The messages published to `topic`, oldest first. QList to(const QString& topic) const { QList result; for (const Message& message : published) if (message.topic == topic) result.append(message); return result; } QList published; QStringList subscriptions; int connectCalls = 0; private: State m_state = State::Disconnected; };