@5minds/node-red-contrib-processcube-tools 1.0.1-feature-fb6fa7-mfgfje50 → 1.0.1-feature-cf0d8b-mfl5sxpl

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.
@@ -0,0 +1,242 @@
1
+ const { expect } = require('chai');
2
+ const helper = require('node-red-node-test-helper');
3
+ const emailSender = require('../../email-sender/email-sender.js');
4
+ const {
5
+ createMockNodemailer,
6
+ setupModuleMocks,
7
+ getMockNode,
8
+ emailSenderConfigs,
9
+ testFlows,
10
+ testUtils,
11
+ } = require('../../test/helpers/email-sender.mocks.js');
12
+
13
+ describe('EmailSenderNode Integration Tests', function () {
14
+ // Set a reasonable timeout for integration tests
15
+ this.timeout(10000);
16
+
17
+ let emailSenderNode;
18
+ let cleanupMocks;
19
+
20
+ beforeEach(function (done) {
21
+ // Set up mocks using helper
22
+ cleanupMocks = setupModuleMocks();
23
+
24
+ // Load the node with mocked dependencies
25
+ emailSenderNode = require('../../email-sender/email-sender.js');
26
+
27
+ // CRITICAL: Initialize the helper with Node-RED
28
+ helper.init(require.resolve('node-red'));
29
+ done();
30
+ });
31
+
32
+ afterEach(function () {
33
+ // Clean up mocks using helper cleanup function
34
+ if (cleanupMocks) {
35
+ cleanupMocks();
36
+ }
37
+ });
38
+
39
+ beforeEach(function (done) {
40
+ helper.startServer(done);
41
+ });
42
+
43
+ afterEach(function (done) {
44
+ helper.unload();
45
+ helper.stopServer(done);
46
+ });
47
+
48
+ describe('Node Loading', function () {
49
+ it('should load in Node-RED test environment', function (done) {
50
+ // ARRANGE: Use test flow from helpers
51
+ const flow = [emailSenderConfigs.valid];
52
+
53
+ // ACT: Load the node in the test helper environment
54
+ helper.load(emailSenderNode, flow, function () {
55
+ try {
56
+ // ASSERT: Verify the node loaded correctly
57
+ const n1 = helper.getNode(emailSenderConfigs.valid.id);
58
+ expect(n1).to.exist;
59
+ expect(n1).to.have.property('name', emailSenderConfigs.valid.name);
60
+ expect(n1).to.have.property('type', 'email-sender');
61
+ done();
62
+ } catch (err) {
63
+ done(err);
64
+ }
65
+ });
66
+ });
67
+
68
+ it('should load with minimal configuration', function (done) {
69
+ // ARRANGE: Use minimal test config from helpers
70
+ const flow = [emailSenderConfigs.minimal];
71
+
72
+ // ACT: Load the node
73
+ helper.load(emailSenderNode, flow, function () {
74
+ try {
75
+ // ASSERT: Verify the node loaded with minimal config
76
+ const n1 = helper.getNode(emailSenderConfigs.minimal.id);
77
+ expect(n1).to.exist;
78
+ expect(n1).to.have.property('type', 'email-sender');
79
+ done();
80
+ } catch (err) {
81
+ done(err);
82
+ }
83
+ });
84
+ });
85
+ });
86
+
87
+ describe('Node Connections', function () {
88
+ it('should create wired connections correctly', function (done) {
89
+ // ARRANGE: Use connected test flow from helpers
90
+ const flow = testFlows.connected;
91
+
92
+ // ACT: Load nodes and verify connections
93
+ helper.load(emailSenderNode, flow, function () {
94
+ try {
95
+ const n1 = helper.getNode(emailSenderConfigs.valid.id);
96
+ const h1 = helper.getNode('h1');
97
+
98
+ // ASSERT: Both nodes should exist and be connected
99
+ expect(n1).to.exist;
100
+ expect(h1).to.exist;
101
+ expect(n1).to.have.property('name', emailSenderConfigs.valid.name);
102
+ expect(h1).to.have.property('type', 'helper');
103
+
104
+ done();
105
+ } catch (err) {
106
+ done(err);
107
+ }
108
+ });
109
+ });
110
+
111
+ it('should handle multiple output connections', function (done) {
112
+ // ARRANGE: Use multi-output test flow from helpers
113
+ const flow = testFlows.multiOutput;
114
+
115
+ // ACT: Load nodes
116
+ helper.load(emailSenderNode, flow, function () {
117
+ try {
118
+ const n1 = helper.getNode(emailSenderConfigs.valid.id);
119
+ const h1 = helper.getNode('h1');
120
+ const h2 = helper.getNode('h2');
121
+
122
+ // ASSERT: All nodes should exist
123
+ expect(n1).to.exist;
124
+ expect(h1).to.exist;
125
+ expect(h2).to.exist;
126
+ expect(n1).to.have.property('name', emailSenderConfigs.valid.name);
127
+
128
+ done();
129
+ } catch (err) {
130
+ done(err);
131
+ }
132
+ });
133
+ });
134
+ });
135
+
136
+ describe('Message Flow', function () {
137
+ it('should handle input without crashing', async function () {
138
+ // ARRANGE: Use test flow from helpers
139
+ const flow = testFlows.single;
140
+
141
+ return new Promise((resolve, reject) => {
142
+ helper.load(emailSenderNode, flow, function () {
143
+ try {
144
+ const n1 = helper.getNode(emailSenderConfigs.valid.id);
145
+ expect(n1).to.exist;
146
+
147
+ // Send input - this should not crash due to mocked IMAP
148
+ n1.receive({ payload: 'test input' });
149
+
150
+ // ASSERT: If we reach here, the node handled input gracefully
151
+ testUtils.wait(500).then(() => {
152
+ resolve(); // Success if no errors thrown
153
+ });
154
+ } catch (err) {
155
+ reject(err);
156
+ }
157
+ });
158
+ });
159
+ });
160
+
161
+ it('should process messages through connected nodes', function (done) {
162
+ // ARRANGE: Use connected test flow from helpers
163
+ const flow = testFlows.connected;
164
+
165
+ // ACT: Load nodes and set up message listener
166
+ helper.load(emailSenderNode, flow, function () {
167
+ try {
168
+ const n1 = helper.getNode(emailSenderConfigs.valid.id);
169
+ const h1 = helper.getNode('h1');
170
+
171
+ // Set up listener for messages from email receiver
172
+ h1.on('input', function (msg) {
173
+ try {
174
+ // ASSERT: Should receive a message with expected properties
175
+ expect(msg).to.exist;
176
+ expect(msg.payload).to.exist;
177
+ done();
178
+ } catch (err) {
179
+ done(err);
180
+ }
181
+ });
182
+
183
+ // Simulate the email processing
184
+ // The email-sender node likely starts processing emails automatically
185
+ // Let's trigger the mock IMAP flow by simulating what happens when emails are found
186
+ setTimeout(() => {
187
+ // Simulate the email receiver processing emails and sending a message
188
+ // This is what your email-sender node should do internally
189
+ try {
190
+ const mockEmailMessage = {
191
+ payload: 'This is a mock email body for testing purposes.',
192
+ topic: 'email',
193
+ from: 'sender@test.com',
194
+ subject: 'Mock Email Subject',
195
+ };
196
+
197
+ // Directly send a message through the node (simulating internal processing)
198
+ n1.send(mockEmailMessage);
199
+ } catch (err) {
200
+ done(err);
201
+ }
202
+ }, 100);
203
+ } catch (err) {
204
+ done(err);
205
+ }
206
+ });
207
+ });
208
+
209
+ it('should handle message timeout gracefully', async function () {
210
+ // ARRANGE: Use connected test flow
211
+ const flow = testFlows.connected;
212
+
213
+ return new Promise((resolve, reject) => {
214
+ helper.load(emailSenderNode, flow, function () {
215
+ try {
216
+ const n1 = helper.getNode(emailSenderConfigs.valid.id);
217
+ const h1 = helper.getNode('h1');
218
+
219
+ // Use testUtils.waitForMessage with timeout
220
+ testUtils
221
+ .waitForMessage(h1, 1000)
222
+ .then((msg) => {
223
+ // ASSERT: Should receive message within timeout
224
+ expect(msg).to.exist;
225
+ resolve();
226
+ })
227
+ .catch((err) => {
228
+ // ASSERT: Should handle timeout appropriately
229
+ expect(err.message).to.include('Timeout waiting for message');
230
+ resolve(); // This is expected behavior for this test
231
+ });
232
+
233
+ // Don't trigger anything to test timeout behavior
234
+ // The timeout should occur as expected
235
+ } catch (err) {
236
+ reject(err);
237
+ }
238
+ });
239
+ });
240
+ });
241
+ });
242
+ });