@cuppet/core 1.3.1 → 1.3.3

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.
@@ -27,8 +27,9 @@ When('I unsubscribe from MQTT topic {string}', async function (topic) {
27
27
  await mqttFunctions.unsubscribeFromTopic(this.mqttManager, topic);
28
28
  });
29
29
 
30
- When('I prepare MQTT message as JSON', async function (docString) {
31
- await mqttFunctions.prepareMessage(docString, true);
30
+ When('I prepare a JSON as MQTT message', async function (docString) {
31
+ const message = JSON.stringify(docString);
32
+ await mqttFunctions.prepareMessage(message, true);
32
33
  });
33
34
 
34
35
  /**
@@ -57,7 +58,15 @@ When(
57
58
  * @example When I publish JSON '{"temperature": 25, "humidity": 60}' to MQTT topic "sensors/room1"
58
59
  */
59
60
  When('I publish JSON {string} to MQTT topic {string}', async function (jsonMessage, topic) {
60
- await mqttFunctions.publishJsonMessage(this.mqttManager, jsonMessage, topic, 0, false);
61
+ await mqttFunctions.publishJsonMessage(this.mqttManager, topic, jsonMessage);
62
+ });
63
+
64
+ /**
65
+ * Publish an already prepared JSON message to an MQTT topic
66
+ * @example When I publish the prepared JSON message to MQTT topic "sensors/room1"
67
+ */
68
+ When('I publish the prepared JSON message to MQTT topic {string}', async function (topic) {
69
+ await mqttFunctions.publishJsonMessage(this.mqttManager, topic);
61
70
  });
62
71
 
63
72
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuppet/core",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Core testing framework components for Cuppet - BDD framework based on Cucumber and Puppeteer",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -126,6 +126,9 @@ module.exports = {
126
126
  const regex = /%([^%]+)%/g;
127
127
  const allVariables = this.getJsonFile();
128
128
  // The convention dictates if function argument is not used, it can be replaced by "_".
129
+ if (typeof text !== 'string') {
130
+ throw new Error(`The value passed to checkForMultipleVariables is not a string: ${text}`);
131
+ }
129
132
  const result = text.replace(regex, (_, group) => {
130
133
  return allVariables[group];
131
134
  });
@@ -7,8 +7,8 @@ const helper = require('./helperFunctions');
7
7
  * Provides core MQTT testing operations following the same pattern as Puppeteer and Appium functions
8
8
  */
9
9
  module.exports = {
10
- /** @type {Object} */
11
- messageObject: null,
10
+ /** @type {string} */
11
+ messageString: null,
12
12
 
13
13
  /**
14
14
  * Prepare topic by replacing variables
@@ -28,8 +28,11 @@ module.exports = {
28
28
  const resolvedMessage = await storage.checkForMultipleVariables(message);
29
29
  if (json) {
30
30
  try {
31
- this.messageObject = JSON.parse(resolvedMessage);
32
- return this.messageObject;
31
+ // Validate JSON (do not assign the value to the message object)
32
+ JSON.parse(resolvedMessage);
33
+ // Pass it as a string
34
+ this.messageString = resolvedMessage;
35
+ return this.messageString;
33
36
  } catch (error) {
34
37
  throw new Error(`Invalid JSON message: ${error.message}`);
35
38
  }
@@ -71,33 +74,25 @@ module.exports = {
71
74
  */
72
75
  publishMessage: async function (mqttManager, message, topic, qos = 0, retain = false) {
73
76
  const resolvedTopic = await this.prepareTopic(topic);
74
- const resolvedMessage = this.messageObject || (await this.prepareMessage(message));
77
+ const resolvedMessage = await this.prepareMessage(message);
75
78
  await mqttManager.publish(resolvedTopic, resolvedMessage, { qos, retain });
76
- delete this.messageObject;
77
79
  },
78
80
 
79
81
  /**
80
82
  * Publish a JSON message to a topic
81
83
  * @param {object} mqttManager - MQTT manager instance
82
- * @param {string} jsonString - JSON string to publish
83
84
  * @param {string} topic - Topic to publish to
85
+ * @param {string} jsonString - JSON string to publish
84
86
  * @param {number} qos - Quality of Service level
85
87
  * @param {boolean} retain - Whether to retain the message
86
88
  * @returns {Promise<void>}
87
89
  */
88
- publishJsonMessage: async function (mqttManager, jsonString, topic, qos = 0, retain = false) {
90
+ publishJsonMessage: async function (mqttManager, topic, jsonString = null, qos = 0, retain = false) {
89
91
  const resolvedTopic = await this.prepareTopic(topic);
90
- const resolvedJson = this.messageObject || (await this.prepareMessage(jsonString));
91
-
92
- // Validate JSON
93
- try {
94
- JSON.parse(resolvedJson);
95
- } catch (error) {
96
- throw new Error(`Invalid JSON message: ${error.message}`);
97
- }
98
-
92
+ const resolvedJson = jsonString ? await this.prepareMessage(jsonString, true) : this.messageString;
93
+ // Delete the message object before the message is published to avoid conflicts if the request fails.
94
+ delete this.messageString;
99
95
  await mqttManager.publish(resolvedTopic, resolvedJson, { qos, retain });
100
- delete this.messageObject;
101
96
  },
102
97
 
103
98
  /**