node-red-contrib-octopus-intelligent 1.0.2 → 1.0.4
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/CHANGELOG.md +209 -118
- package/README.md +477 -432
- package/__tests__/README.md +220 -0
- package/__tests__/TEST-SUMMARY.md +194 -0
- package/__tests__/charging-now-validation.test.js +495 -0
- package/__tests__/charging-timers.test.js +601 -0
- package/__tests__/data-processing.test.js +478 -0
- package/__tests__/mqtt-refresh-cooldown.test.js +420 -0
- package/__tests__/reconciliation-loop.test.js +474 -0
- package/examples/basic-monitoring.json +1 -0
- package/examples/mqtt-integration.json +1 -0
- package/examples/set-preferences.json +1 -0
- package/images/IMAGES_NEEDED.md +64 -0
- package/images/examples/basic-monitoring-flow.png +0 -0
- package/images/examples/homeassistant-device-v1.0.4.png +0 -0
- package/images/examples/homeassistant-device.png +0 -0
- package/images/examples/mqtt-integration-flow.png +0 -0
- package/octopus-intelligent.js +1163 -861
- package/package.json +58 -46
- package/test-mocks.js +1038 -0
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MQTT Refresh Button Cooldown Tests
|
|
3
|
+
*
|
|
4
|
+
* Test 1.2: MQTT Button - Rapid Clicking During Cooldown
|
|
5
|
+
* Test 4.2: Countdown Doesn't Go Negative (validated within Test 1.2)
|
|
6
|
+
*
|
|
7
|
+
* These tests verify:
|
|
8
|
+
* - MQTT button has hardcoded 30-second rate limiting
|
|
9
|
+
* - Rapid clicks are blocked during cooldown
|
|
10
|
+
* - Only first click executes
|
|
11
|
+
* - Node-RED logs warnings for blocked attempts
|
|
12
|
+
* - Countdown expires correctly and doesn't go negative
|
|
13
|
+
* - MQTT publishes correct timestamps and null at expiry
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
describe('MQTT Refresh Button Cooldown', () => {
|
|
17
|
+
let node;
|
|
18
|
+
let publishedMessages;
|
|
19
|
+
let warnings;
|
|
20
|
+
let currentTime;
|
|
21
|
+
let timers;
|
|
22
|
+
|
|
23
|
+
// Persistent state across button presses
|
|
24
|
+
let lastManualRefresh;
|
|
25
|
+
let cooldownExpiryTimer;
|
|
26
|
+
let lastKnownState;
|
|
27
|
+
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
// Reset time tracking
|
|
30
|
+
currentTime = Date.now();
|
|
31
|
+
timers = [];
|
|
32
|
+
|
|
33
|
+
// Reset persistent state
|
|
34
|
+
lastManualRefresh = 0;
|
|
35
|
+
cooldownExpiryTimer = null;
|
|
36
|
+
lastKnownState = {
|
|
37
|
+
next_start: null,
|
|
38
|
+
total_energy: 0,
|
|
39
|
+
confirmed_limit: 80,
|
|
40
|
+
confirmed_time: "04:00"
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Mock node state
|
|
44
|
+
node = {
|
|
45
|
+
broker: {
|
|
46
|
+
client: {
|
|
47
|
+
publish: jest.fn((topic, payload, options) => {
|
|
48
|
+
publishedMessages.push({
|
|
49
|
+
topic,
|
|
50
|
+
payload: JSON.parse(payload),
|
|
51
|
+
options,
|
|
52
|
+
timestamp: currentTime
|
|
53
|
+
});
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
warn: jest.fn((message) => {
|
|
58
|
+
warnings.push({
|
|
59
|
+
message,
|
|
60
|
+
timestamp: currentTime
|
|
61
|
+
});
|
|
62
|
+
}),
|
|
63
|
+
status: jest.fn(),
|
|
64
|
+
log: jest.fn()
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Reset tracking arrays
|
|
68
|
+
publishedMessages = [];
|
|
69
|
+
warnings = [];
|
|
70
|
+
|
|
71
|
+
// Mock Date.now() to control time
|
|
72
|
+
jest.spyOn(Date, 'now').mockImplementation(() => currentTime);
|
|
73
|
+
|
|
74
|
+
// Mock setTimeout to track timers
|
|
75
|
+
jest.spyOn(global, 'setTimeout').mockImplementation((callback, delay) => {
|
|
76
|
+
const timer = {
|
|
77
|
+
callback,
|
|
78
|
+
delay,
|
|
79
|
+
scheduledAt: currentTime,
|
|
80
|
+
executeAt: currentTime + delay
|
|
81
|
+
};
|
|
82
|
+
timers.push(timer);
|
|
83
|
+
return timer;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
// Mock clearTimeout
|
|
87
|
+
jest.spyOn(global, 'clearTimeout').mockImplementation((timer) => {
|
|
88
|
+
const index = timers.indexOf(timer);
|
|
89
|
+
if (index > -1) {
|
|
90
|
+
timers.splice(index, 1);
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
afterEach(() => {
|
|
96
|
+
jest.restoreAllMocks();
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Helper: Simulate MQTT refresh button press
|
|
101
|
+
*/
|
|
102
|
+
function pressMqttRefreshButton() {
|
|
103
|
+
const MANUAL_REFRESH_COOLDOWN = 30000; // 30 seconds
|
|
104
|
+
const account = "A-TEST123";
|
|
105
|
+
const stateTopic = `nodered_octopus/${account}/status`;
|
|
106
|
+
const enableMqtt = true;
|
|
107
|
+
|
|
108
|
+
// Helper functions
|
|
109
|
+
function canManualRefresh() {
|
|
110
|
+
const now = Date.now();
|
|
111
|
+
const timeSinceLastRefresh = now - lastManualRefresh;
|
|
112
|
+
return lastManualRefresh === 0 || timeSinceLastRefresh >= MANUAL_REFRESH_COOLDOWN;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function getSecondsUntilNextRefresh() {
|
|
116
|
+
const now = Date.now();
|
|
117
|
+
const timeSinceLastRefresh = now - lastManualRefresh;
|
|
118
|
+
const timeRemaining = MANUAL_REFRESH_COOLDOWN - timeSinceLastRefresh;
|
|
119
|
+
return Math.max(0, Math.ceil(timeRemaining / 1000));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function getRefreshAvailableAt() {
|
|
123
|
+
const now = Date.now();
|
|
124
|
+
const timeSinceLastRefresh = now - lastManualRefresh;
|
|
125
|
+
|
|
126
|
+
if (lastManualRefresh === 0 || timeSinceLastRefresh >= MANUAL_REFRESH_COOLDOWN) {
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return new Date(lastManualRefresh + MANUAL_REFRESH_COOLDOWN).toISOString();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function publishRefreshCooldownState() {
|
|
134
|
+
if (enableMqtt && node.broker) {
|
|
135
|
+
const fullState = {
|
|
136
|
+
...lastKnownState,
|
|
137
|
+
refresh_available_at: getRefreshAvailableAt()
|
|
138
|
+
};
|
|
139
|
+
node.broker.client.publish(stateTopic, JSON.stringify(fullState), { retain: true });
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// MQTT Refresh button handler (from actual code)
|
|
144
|
+
if (!canManualRefresh()) {
|
|
145
|
+
const secondsRemaining = getSecondsUntilNextRefresh();
|
|
146
|
+
node.status({
|
|
147
|
+
fill: "red",
|
|
148
|
+
shape: "dot",
|
|
149
|
+
text: `Cooldown: ${secondsRemaining}s`
|
|
150
|
+
});
|
|
151
|
+
node.warn(`MQTT refresh blocked. Please wait ${secondsRemaining} seconds.`);
|
|
152
|
+
return { blocked: true, secondsRemaining };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Refresh allowed - set cooldown timestamp
|
|
156
|
+
lastManualRefresh = Date.now();
|
|
157
|
+
|
|
158
|
+
// Publish immediate update with countdown timestamp
|
|
159
|
+
publishRefreshCooldownState();
|
|
160
|
+
|
|
161
|
+
// Schedule cleanup at exactly 30 seconds to clear countdown
|
|
162
|
+
if (cooldownExpiryTimer) clearTimeout(cooldownExpiryTimer);
|
|
163
|
+
cooldownExpiryTimer = setTimeout(() => {
|
|
164
|
+
publishRefreshCooldownState();
|
|
165
|
+
cooldownExpiryTimer = null;
|
|
166
|
+
}, MANUAL_REFRESH_COOLDOWN);
|
|
167
|
+
|
|
168
|
+
node.status({ fill: "yellow", shape: "ring", text: "Manual refresh..." });
|
|
169
|
+
|
|
170
|
+
return {
|
|
171
|
+
blocked: false,
|
|
172
|
+
lastManualRefresh,
|
|
173
|
+
timer: cooldownExpiryTimer
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Helper: Advance time and execute any timers that should fire
|
|
179
|
+
*/
|
|
180
|
+
function advanceTime(milliseconds) {
|
|
181
|
+
currentTime += milliseconds;
|
|
182
|
+
|
|
183
|
+
// Execute any timers that should fire
|
|
184
|
+
const timersToExecute = timers.filter(t => t.executeAt <= currentTime);
|
|
185
|
+
timersToExecute.forEach(timer => {
|
|
186
|
+
timer.callback();
|
|
187
|
+
const index = timers.indexOf(timer);
|
|
188
|
+
if (index > -1) {
|
|
189
|
+
timers.splice(index, 1);
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @test MQTT Rapid Click - Test 1.2
|
|
196
|
+
* @scenario User rapidly clicks MQTT refresh button 5 times in 2 seconds
|
|
197
|
+
* @given Fresh start, no previous refresh
|
|
198
|
+
* @expect First click works, 4 subsequent clicks blocked
|
|
199
|
+
*/
|
|
200
|
+
test('Test 1.2: Should block rapid MQTT button clicks during cooldown', () => {
|
|
201
|
+
// STEP 1: First click - should work
|
|
202
|
+
const result1 = pressMqttRefreshButton();
|
|
203
|
+
|
|
204
|
+
expect(result1.blocked).toBe(false);
|
|
205
|
+
expect(publishedMessages).toHaveLength(1);
|
|
206
|
+
expect(publishedMessages[0].payload.refresh_available_at).not.toBeNull();
|
|
207
|
+
expect(publishedMessages[0].payload.refresh_available_at).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/);
|
|
208
|
+
|
|
209
|
+
const firstClickTime = result1.lastManualRefresh;
|
|
210
|
+
const expectedExpiryTime = new Date(firstClickTime + 30000).toISOString();
|
|
211
|
+
|
|
212
|
+
// STEP 2: Advance 500ms and click again - should be blocked
|
|
213
|
+
advanceTime(500);
|
|
214
|
+
const result2 = pressMqttRefreshButton();
|
|
215
|
+
|
|
216
|
+
expect(result2.blocked).toBe(true);
|
|
217
|
+
expect(result2.secondsRemaining).toBe(30); // 29.5s rounds up to 30
|
|
218
|
+
expect(warnings).toHaveLength(1);
|
|
219
|
+
expect(warnings[0].message).toContain('MQTT refresh blocked');
|
|
220
|
+
expect(warnings[0].message).toContain('30 seconds');
|
|
221
|
+
|
|
222
|
+
// STEP 3: Advance 500ms and click again - should be blocked
|
|
223
|
+
advanceTime(500);
|
|
224
|
+
const result3 = pressMqttRefreshButton();
|
|
225
|
+
|
|
226
|
+
expect(result3.blocked).toBe(true);
|
|
227
|
+
expect(result3.secondsRemaining).toBe(29);
|
|
228
|
+
expect(warnings).toHaveLength(2);
|
|
229
|
+
|
|
230
|
+
// STEP 4: Advance 500ms and click again - should be blocked
|
|
231
|
+
advanceTime(500);
|
|
232
|
+
const result4 = pressMqttRefreshButton();
|
|
233
|
+
|
|
234
|
+
expect(result4.blocked).toBe(true);
|
|
235
|
+
expect(result4.secondsRemaining).toBe(29);
|
|
236
|
+
expect(warnings).toHaveLength(3);
|
|
237
|
+
|
|
238
|
+
// STEP 5: Advance 500ms and click again - should be blocked
|
|
239
|
+
advanceTime(500);
|
|
240
|
+
const result5 = pressMqttRefreshButton();
|
|
241
|
+
|
|
242
|
+
expect(result5.blocked).toBe(true);
|
|
243
|
+
expect(result5.secondsRemaining).toBe(28);
|
|
244
|
+
expect(warnings).toHaveLength(4);
|
|
245
|
+
|
|
246
|
+
// VERIFY: Only 1 MQTT message published (the first click)
|
|
247
|
+
expect(publishedMessages).toHaveLength(1);
|
|
248
|
+
|
|
249
|
+
// VERIFY: All blocked attempts logged warnings
|
|
250
|
+
expect(warnings).toHaveLength(4);
|
|
251
|
+
warnings.forEach(w => {
|
|
252
|
+
expect(w.message).toContain('MQTT refresh blocked');
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// VERIFY: Node status called for each blocked attempt
|
|
256
|
+
expect(node.status).toHaveBeenCalledWith({
|
|
257
|
+
fill: "red",
|
|
258
|
+
shape: "dot",
|
|
259
|
+
text: expect.stringMatching(/Cooldown: \d+s/)
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* @test Cooldown Expiry - Test 4.2 validation
|
|
265
|
+
* @scenario Wait for 30-second cooldown to expire and verify countdown doesn't go negative
|
|
266
|
+
* @given MQTT button pressed, cooldown active
|
|
267
|
+
* @expect After 30s, refresh_available_at becomes null (not negative time)
|
|
268
|
+
*/
|
|
269
|
+
test('Test 4.2: Should clear countdown at expiry and not go negative', () => {
|
|
270
|
+
// STEP 1: Press button to start cooldown
|
|
271
|
+
const result = pressMqttRefreshButton();
|
|
272
|
+
|
|
273
|
+
expect(result.blocked).toBe(false);
|
|
274
|
+
expect(timers).toHaveLength(1);
|
|
275
|
+
expect(timers[0].delay).toBe(30000);
|
|
276
|
+
|
|
277
|
+
// Initial publish with timestamp
|
|
278
|
+
expect(publishedMessages).toHaveLength(1);
|
|
279
|
+
const initialPayload = publishedMessages[0].payload;
|
|
280
|
+
expect(initialPayload.refresh_available_at).not.toBeNull();
|
|
281
|
+
expect(initialPayload.refresh_available_at).toMatch(/^\d{4}-\d{2}-\d{2}T/);
|
|
282
|
+
|
|
283
|
+
const expiryTimestamp = new Date(initialPayload.refresh_available_at);
|
|
284
|
+
const startTime = currentTime;
|
|
285
|
+
const expectedExpiry = startTime + 30000;
|
|
286
|
+
|
|
287
|
+
// Verify the timestamp is exactly 30 seconds in the future
|
|
288
|
+
expect(expiryTimestamp.getTime()).toBe(expectedExpiry);
|
|
289
|
+
|
|
290
|
+
// STEP 2: Advance time to just before expiry (29 seconds)
|
|
291
|
+
advanceTime(29000);
|
|
292
|
+
|
|
293
|
+
// No new messages should be published yet
|
|
294
|
+
expect(publishedMessages).toHaveLength(1);
|
|
295
|
+
|
|
296
|
+
// STEP 3: Advance time to exactly 30 seconds (expiry)
|
|
297
|
+
advanceTime(1000); // Total: 30s
|
|
298
|
+
|
|
299
|
+
// Timer should have fired and published null
|
|
300
|
+
expect(publishedMessages).toHaveLength(2);
|
|
301
|
+
const expiryPayload = publishedMessages[1].payload;
|
|
302
|
+
|
|
303
|
+
// CRITICAL: refresh_available_at should be null, NOT a past timestamp
|
|
304
|
+
expect(expiryPayload.refresh_available_at).toBeNull();
|
|
305
|
+
|
|
306
|
+
// STEP 4: Advance time beyond expiry (35 seconds total)
|
|
307
|
+
advanceTime(5000);
|
|
308
|
+
|
|
309
|
+
// Should still be null (not going negative like "5 seconds ago")
|
|
310
|
+
// No additional messages should be published
|
|
311
|
+
expect(publishedMessages).toHaveLength(2);
|
|
312
|
+
|
|
313
|
+
// STEP 5: Verify we can refresh again now
|
|
314
|
+
const newResult = pressMqttRefreshButton();
|
|
315
|
+
expect(newResult.blocked).toBe(false);
|
|
316
|
+
|
|
317
|
+
// New cooldown should start with new timestamp
|
|
318
|
+
expect(publishedMessages).toHaveLength(3);
|
|
319
|
+
const newPayload = publishedMessages[2].payload;
|
|
320
|
+
expect(newPayload.refresh_available_at).not.toBeNull();
|
|
321
|
+
expect(newPayload.refresh_available_at).toMatch(/^\d{4}-\d{2}-\d{2}T/);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @test MQTT Payload Values During Cooldown Lifecycle
|
|
326
|
+
* @scenario Verify exact payload values at different points in cooldown
|
|
327
|
+
* @given MQTT button pressed
|
|
328
|
+
* @expect Correct timestamp values at T+0s, T+15s, T+30s, T+31s
|
|
329
|
+
*/
|
|
330
|
+
test('Should publish correct timestamp values throughout cooldown lifecycle', () => {
|
|
331
|
+
const startTime = currentTime;
|
|
332
|
+
|
|
333
|
+
// T+0s: Press button
|
|
334
|
+
pressMqttRefreshButton();
|
|
335
|
+
|
|
336
|
+
expect(publishedMessages).toHaveLength(1);
|
|
337
|
+
const msg0 = publishedMessages[0];
|
|
338
|
+
expect(msg0.payload.refresh_available_at).toBe(new Date(startTime + 30000).toISOString());
|
|
339
|
+
expect(msg0.options.retain).toBe(true);
|
|
340
|
+
|
|
341
|
+
// T+15s: Mid-cooldown (no publish expected)
|
|
342
|
+
advanceTime(15000);
|
|
343
|
+
expect(publishedMessages).toHaveLength(1); // Still only 1 message
|
|
344
|
+
|
|
345
|
+
// T+30s: Expiry (timer fires)
|
|
346
|
+
advanceTime(15000);
|
|
347
|
+
expect(publishedMessages).toHaveLength(2);
|
|
348
|
+
const msg30 = publishedMessages[1];
|
|
349
|
+
expect(msg30.payload.refresh_available_at).toBeNull();
|
|
350
|
+
expect(msg30.options.retain).toBe(true);
|
|
351
|
+
|
|
352
|
+
// T+31s: After expiry (no publish expected)
|
|
353
|
+
advanceTime(1000);
|
|
354
|
+
expect(publishedMessages).toHaveLength(2); // Still only 2 messages
|
|
355
|
+
|
|
356
|
+
// Verify EXACTLY 2 messages were sent (start + expiry)
|
|
357
|
+
expect(publishedMessages).toHaveLength(2);
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* @test Timer Cleanup
|
|
362
|
+
* @scenario Verify cooldown timer is properly cleaned up
|
|
363
|
+
* @given MQTT button pressed
|
|
364
|
+
* @expect Timer exists initially, then is cleaned up after expiry
|
|
365
|
+
*/
|
|
366
|
+
test('Should properly clean up cooldown timer after expiry', () => {
|
|
367
|
+
// Press button
|
|
368
|
+
const result = pressMqttRefreshButton();
|
|
369
|
+
|
|
370
|
+
// Timer should be scheduled
|
|
371
|
+
expect(timers).toHaveLength(1);
|
|
372
|
+
expect(timers[0].delay).toBe(30000);
|
|
373
|
+
|
|
374
|
+
// Advance to expiry
|
|
375
|
+
advanceTime(30000);
|
|
376
|
+
|
|
377
|
+
// Timer should have been executed and removed
|
|
378
|
+
expect(timers).toHaveLength(0);
|
|
379
|
+
|
|
380
|
+
// Press button again
|
|
381
|
+
pressMqttRefreshButton();
|
|
382
|
+
|
|
383
|
+
// New timer should be created
|
|
384
|
+
expect(timers).toHaveLength(1);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* @test MQTT Message Structure
|
|
389
|
+
* @scenario Verify published MQTT messages have correct structure
|
|
390
|
+
* @given MQTT button pressed
|
|
391
|
+
* @expect Messages contain all required fields with correct types
|
|
392
|
+
*/
|
|
393
|
+
test('Should publish MQTT messages with correct structure', () => {
|
|
394
|
+
pressMqttRefreshButton();
|
|
395
|
+
|
|
396
|
+
const msg = publishedMessages[0];
|
|
397
|
+
|
|
398
|
+
// Check message structure
|
|
399
|
+
expect(msg).toHaveProperty('topic');
|
|
400
|
+
expect(msg).toHaveProperty('payload');
|
|
401
|
+
expect(msg).toHaveProperty('options');
|
|
402
|
+
expect(msg).toHaveProperty('timestamp');
|
|
403
|
+
|
|
404
|
+
// Check topic
|
|
405
|
+
expect(msg.topic).toBe('nodered_octopus/A-TEST123/status');
|
|
406
|
+
|
|
407
|
+
// Check payload structure
|
|
408
|
+
expect(msg.payload).toHaveProperty('refresh_available_at');
|
|
409
|
+
expect(msg.payload).toHaveProperty('next_start');
|
|
410
|
+
expect(msg.payload).toHaveProperty('total_energy');
|
|
411
|
+
expect(msg.payload).toHaveProperty('confirmed_limit');
|
|
412
|
+
expect(msg.payload).toHaveProperty('confirmed_time');
|
|
413
|
+
|
|
414
|
+
// Check options
|
|
415
|
+
expect(msg.options.retain).toBe(true);
|
|
416
|
+
|
|
417
|
+
// Check timestamp is ISO 8601 format
|
|
418
|
+
expect(msg.payload.refresh_available_at).toMatch(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/);
|
|
419
|
+
});
|
|
420
|
+
});
|