@cuppet/core 1.2.1 → 1.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -249,6 +249,35 @@ class MqttManager {
249
249
  });
250
250
  }
251
251
 
252
+ /** Wait for specific message on a topic with timeout. IMPORTANT: Message is position,case and whitespace sensitive.
253
+ * @param {string} topic - Topic to wait for message on
254
+ * @param {string} message - Message to wait for
255
+ * @param {number} timeoutSeconds - Timeout in seconds
256
+ * @returns {Promise<Object>} - Resolves with message when received
257
+ */
258
+ async waitForSpecificMessage(topic, message, timeoutSeconds = 10) {
259
+ const timeoutMs = timeoutSeconds * 1000;
260
+ const startTime = Date.now();
261
+
262
+ while (Date.now() - startTime < timeoutMs) {
263
+ const latestMessage = this.getLatestMessage(topic);
264
+ if (latestMessage) {
265
+ const latestMessageString = JSON.stringify(latestMessage.message);
266
+ const expectedMessageString = JSON.stringify(message);
267
+ if (latestMessageString === expectedMessageString) {
268
+ return latestMessage;
269
+ } else {
270
+ throw new Error(
271
+ `Message: ${latestMessageString} on topic ${topic} does not match expected: ${expectedMessageString}`
272
+ );
273
+ }
274
+ }
275
+ await new Promise((resolve) => setTimeout(resolve, 100));
276
+ }
277
+
278
+ throw new Error(`Timeout waiting for message on topic: ${topic} after ${timeoutSeconds} seconds`);
279
+ }
280
+
252
281
  /**
253
282
  * Check if client is connected
254
283
  * @returns {boolean}
@@ -69,6 +69,20 @@ Then('I should receive a message on MQTT topic {string}', async function (topic)
69
69
  await mqttFunctions.validateMessageReceived(this.mqttManager, topic, 10);
70
70
  });
71
71
 
72
+ /**
73
+ * Wait for a specific message on a topic with timeout
74
+ * @example Then I should receive the message "Hello World" on MQTT topic "test/response" within 5 seconds
75
+ */
76
+ Then(
77
+ 'I should receive the message {string} on MQTT topic {string} within {int} seconds',
78
+ async function (message, topic, timeout) {
79
+ await mqttFunctions.waitForSpecificMessage(this.mqttManager, topic, message, timeout);
80
+ }
81
+ );
82
+
83
+ Then('I should receive the message {string} on MQTT topic {string}', async function (message, topic) {
84
+ await mqttFunctions.waitForSpecificMessage(this.mqttManager, topic, message, 10);
85
+ });
72
86
  /**
73
87
  * Validate the content of the latest message on a topic
74
88
  * @example Then the MQTT message on topic "test/echo" should equal "Hello World"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuppet/core",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Core testing framework components for Cuppet - BDD framework based on Cucumber and Puppeteer",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -98,6 +98,20 @@ module.exports = {
98
98
  return await mqttManager.waitForMessage(resolvedTopic, timeoutSeconds);
99
99
  },
100
100
 
101
+ /**
102
+ * Wait for a specific message on a topic with timeout
103
+ * @param {object} mqttManager - MQTT manager instance
104
+ * @param {string} topic - Topic to wait for message on
105
+ * @param {string} message - Message to wait for
106
+ * @param {number} timeoutSeconds - Timeout in seconds
107
+ * @returns {Promise<Object>} - Message object
108
+ */
109
+ waitForSpecificMessage: async function (mqttManager, topic, message, timeoutSeconds = 10) {
110
+ const resolvedTopic = await this.prepareTopic(topic);
111
+ const resolvedMessage = await this.prepareMessage(message);
112
+ return await mqttManager.waitForSpecificMessage(resolvedTopic, resolvedMessage, timeoutSeconds);
113
+ },
114
+
101
115
  /**
102
116
  * Get the latest message from a topic
103
117
  * @param {object} mqttManager - MQTT manager instance