@cuppet/core 1.3.0 → 1.3.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.
package/README.md CHANGED
@@ -132,8 +132,8 @@ For a detailed configuration and step definitions guide, see [GUIDE.MD](./GUIDE.
132
132
 
133
133
  This package requires the following peer dependencies:
134
134
 
135
- - `@cucumber/cucumber` ^11.0.0
136
- - `config` ^3.3.9
135
+ - `@cucumber/cucumber` ^12.0.0
136
+ - `config` ^4.1.0
137
137
 
138
138
  Make sure to install these in your project:
139
139
 
@@ -27,6 +27,11 @@ 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
+ const message = JSON.stringify(docString);
32
+ await mqttFunctions.prepareMessage(message, true);
33
+ });
34
+
30
35
  /**
31
36
  * Publish a message to an MQTT topic
32
37
  * @example When I publish "Hello World" to MQTT topic "test/message"
@@ -53,7 +58,15 @@ When(
53
58
  * @example When I publish JSON '{"temperature": 25, "humidity": 60}' to MQTT topic "sensors/room1"
54
59
  */
55
60
  When('I publish JSON {string} to MQTT topic {string}', async function (jsonMessage, topic) {
56
- 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);
57
70
  });
58
71
 
59
72
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cuppet/core",
3
- "version": "1.3.0",
3
+ "version": "1.3.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": [
@@ -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,6 +7,9 @@ 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,
12
+
10
13
  /**
11
14
  * Prepare topic by replacing variables
12
15
  * @param {string} topic - Topic with potential variables
@@ -21,8 +24,17 @@ module.exports = {
21
24
  * @param {string} message - Message with potential variables
22
25
  * @returns {Promise<string>} - Resolved message
23
26
  */
24
- prepareMessage: async function (message) {
25
- return await storage.checkForMultipleVariables(message);
27
+ prepareMessage: async function (message, json = false) {
28
+ const resolvedMessage = await storage.checkForMultipleVariables(message);
29
+ if (json) {
30
+ try {
31
+ this.messageObject = JSON.parse(resolvedMessage);
32
+ return this.messageObject;
33
+ } catch (error) {
34
+ throw new Error(`Invalid JSON message: ${error.message}`);
35
+ }
36
+ }
37
+ return resolvedMessage;
26
38
  },
27
39
 
28
40
  /**
@@ -66,23 +78,17 @@ module.exports = {
66
78
  /**
67
79
  * Publish a JSON message to a topic
68
80
  * @param {object} mqttManager - MQTT manager instance
69
- * @param {string} jsonString - JSON string to publish
70
81
  * @param {string} topic - Topic to publish to
82
+ * @param {string} jsonString - JSON string to publish
71
83
  * @param {number} qos - Quality of Service level
72
84
  * @param {boolean} retain - Whether to retain the message
73
85
  * @returns {Promise<void>}
74
86
  */
75
- publishJsonMessage: async function (mqttManager, jsonString, topic, qos = 0, retain = false) {
87
+ publishJsonMessage: async function (mqttManager, topic, jsonString = null, qos = 0, retain = false) {
76
88
  const resolvedTopic = await this.prepareTopic(topic);
77
- const resolvedJson = await this.prepareMessage(jsonString);
78
-
79
- // Validate JSON
80
- try {
81
- JSON.parse(resolvedJson);
82
- } catch (error) {
83
- throw new Error(`Invalid JSON message: ${error.message}`);
84
- }
85
-
89
+ const resolvedJson = jsonString ? await this.prepareMessage(jsonString, true) : this.messageObject;
90
+ // Delete the message object before the message is published to avoid conflicts if the request fails.
91
+ delete this.messageObject;
86
92
  await mqttManager.publish(resolvedTopic, resolvedJson, { qos, retain });
87
93
  },
88
94