@rootintf/protocol-2phw 2.2.2-api.2 → 2.3.7-api.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.
@@ -1,239 +0,0 @@
1
-
2
- import { EventLog, logr as logr_ } from '../src/EventLog-mem.mjs';
3
- import { LOGR } from '@knev/bitlogr';
4
-
5
- //-------------------------------------------------------------------------------------------------
6
-
7
- const LOGR_= LOGR.get_instance();
8
- const l_= logr_.l;
9
-
10
- //-------------------------------------------------------------------------------------------------
11
-
12
- // Helper to mock Date.now for consistent timestamps
13
- function mockDateNow(fixedTimestamp) {
14
- spyOn(Date, 'now').and.returnValue(fixedTimestamp);
15
- }
16
-
17
- describe('EventLog', () => {
18
- let eventLog;
19
-
20
- beforeEach(() => {
21
- eventLog = new EventLog();
22
- // Mock BitLogr.log to avoid actual logging during tests
23
- // spyOn(eventLog.logr, 'log');
24
- });
25
-
26
- afterEach(() => {
27
- // Reset Date.now mock if used
28
- jasmine.clock().uninstall();
29
- });
30
-
31
- describe('constructor', () => {
32
- it('should initialize with an empty events array', () => {
33
- expect(eventLog.arr_events).toEqual([]);
34
- });
35
-
36
- });
37
-
38
- describe('addEvent;', () => {
39
- beforeEach(() => {
40
- // Use Jasmine's clock to control time
41
- jasmine.clock().install();
42
- jasmine.clock().mockDate(new Date(1000000)); // Fixed base time: 1,000,000 ms
43
- });
44
-
45
- it('add 1 event', () => {
46
- const { idx, evt } = eventLog.addEvent('TEST_EVENT', 'msg1', { key: 'value' });
47
- expect(typeof evt.dt).toBe('number');
48
- expect(eventLog.arr_events.length).toBe(1);
49
- expect(eventLog.arr_events[0]).toBe(evt);
50
- });
51
-
52
- it('add 3 events', () => {
53
- const { idx: idx0, evt: evt0 } = eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
54
- jasmine.clock().tick(1000); // Advance 1 second
55
- const { idx: idx1, evt: evt1 } = eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,001,000
56
- jasmine.clock().tick(1000); // Advance another second
57
- const { idx: idx2, evt: evt2 } = eventLog.addEvent('EVENT3', 'msg2', { data: 3 }); // t=1,002,000
58
-
59
- expect(eventLog.arr_events.length).toBe(3);
60
- expect(eventLog.arr_events[0]).toBe(evt0);
61
- expect(eventLog.arr_events[1]).toBe(evt1);
62
- expect(eventLog.arr_events[2]).toBe(evt2);
63
- });
64
-
65
- it('should return the added event', () => {
66
- const { idx, evt } = eventLog.addEvent('TEST_EVENT', 'msg1', {});
67
- expect(evt).toBeDefined();
68
- expect(evt).toBe(eventLog.arr_events[0]);
69
- });
70
- });
71
-
72
- describe('getRecentEvents;', () => {
73
- beforeEach(() => {
74
- // Use Jasmine's clock to control time
75
- jasmine.clock().install();
76
- jasmine.clock().mockDate(new Date(1000000)); // Fixed base time: 1,000,000 ms
77
- });
78
-
79
- it('should return events matching msg_id predicate within lookback_ms', () => {
80
- eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
81
- jasmine.clock().tick(1000);
82
- eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,001,000
83
- jasmine.clock().tick(1000);
84
- eventLog.addEvent('EVENT3', 'msg2', { data: 3 }); // t=1,002,000
85
-
86
- let arr_evts;
87
-
88
- arr_evts = eventLog.getEvents(1500, event => event.id === 'msg1');
89
- expect(arr_evts.length).toBe(1); // Only one event within 1500 ms
90
- expect(arr_evts[0].evt.dt).toBe(1001000); // Newest (and only) match
91
- expect(arr_evts[0].evt.data).toEqual({ data: 2 });
92
-
93
- arr_evts = eventLog.getEventsById('msg1', 1500);
94
- expect(arr_evts.length).toBe(1); // Only one event within 1500 ms
95
- expect(arr_evts[0].evt.dt).toBe(1001000); // Newest (and only) match
96
- expect(arr_evts[0].evt.data).toEqual({ data: 2 });
97
- });
98
-
99
-
100
- it('should return events matching msg_id within lookback period', () => {
101
- eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
102
- jasmine.clock().tick(1000); // Advance 1 second
103
- eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,001,000
104
- jasmine.clock().tick(1000); // Advance another second
105
- eventLog.addEvent('EVENT3', 'msg2', { data: 3 }); // t=1,002,000
106
-
107
- const arr_evts = eventLog.getEvents(1500, event => event.type === 'EVENT3'); // Look back 1.5s from t=1,002,000
108
- expect(arr_evts.length).toBe(1);
109
- expect(arr_evts[0].evt.type).toBe('EVENT3');
110
- // expect(recent[1].evt.type).toBe('EVENT2');
111
- });
112
-
113
- /*
114
- it('should exclude events outside lookback period', () => {
115
- eventLog.addEvent('EVENT1', 'msg1', { data: 1 }); // t=1,000,000
116
- jasmine.clock().tick(2000); // Advance 2 seconds
117
- eventLog.addEvent('EVENT2', 'msg1', { data: 2 }); // t=1,002,000
118
-
119
- const recent = eventLog.getRecentEvents('msg1', 1000); // Look back 1s from t=1,002,000
120
- expect(recent.length).toBe(1);
121
- expect(recent[0].type).toBe('EVENT2');
122
- });
123
-
124
- it('should return empty array if no matching events', () => {
125
- eventLog.addEvent('EVENT1', 'msg2', { data: 1 });
126
- const recent = eventLog.getRecentEvents('msg1', 1000);
127
- expect(recent).toEqual([]);
128
- });
129
- */
130
-
131
- });
132
-
133
- describe('addEvent = timeouts;', () => {
134
- let eventLog;
135
-
136
- beforeEach(() => {
137
- eventLog = new EventLog();
138
- jasmine.clock().install(); // Mock the clock for timeout testing
139
- jasmine.clock().mockDate(new Date(1000000)); // Fixed base time: 1,000,000 ms
140
- });
141
-
142
- afterEach(() => {
143
- jasmine.clock().uninstall();
144
- });
145
-
146
- it('should add event without timeout', () => {
147
- const { idx, evt } = eventLog.addEvent('test', 'id1', { value: 1 });
148
- expect(eventLog.arr_events.length).toBe(1);
149
- expect(evt.type).toBe('test');
150
- expect(evt.id).toBe('id1');
151
- expect(evt.data).toEqual({ value: 1 });
152
- expect(evt.timeout_id).toBeNull();
153
- });
154
-
155
- it('should add event with timeout', () => {
156
- const timeout_callback = jasmine.createSpy('timeout_callback');
157
- const { idx, evt } = eventLog.addEvent('test', 'id1', { value: 1 }, 5000, timeout_callback);
158
-
159
- expect(eventLog.arr_events.length).toBe(1);
160
- expect(evt.timeout_id).not.toBeNull();
161
- expect(eventLog.map_timeouts.size).toBe(1);
162
- });
163
-
164
-
165
- it('should trigger timeout callback after specified time', () => {
166
- const timeout_callback = jasmine.createSpy('timeout_callback');
167
- const { idx, evt } = eventLog.addEvent('test', 'id1', { value: 1 }, 5000, timeout_callback);
168
-
169
- jasmine.clock().tick(5001);
170
-
171
- expect(timeout_callback).toHaveBeenCalledWith({ idx, evt});
172
- expect(eventLog.map_timeouts.size).toBe(0); // Timeout should be cleaned up
173
- });
174
-
175
- it('should trigger all timeouts after specified time', () => {
176
- LOGR_.toggle(l_, {
177
- EVENTS : true
178
- });
179
- const cb1 = jasmine.createSpy('callback1');
180
- const cb2 = jasmine.createSpy('callback2');
181
- eventLog.addEvent('test1', 'id1', { value: 1 }, 5000, cb1);
182
- eventLog.addEvent('test2', 'id2', { value: 2 }, 3000, cb2);
183
-
184
- jasmine.clock().tick(2999);
185
- expect(cb1).not.toHaveBeenCalled();
186
- expect(cb2).not.toHaveBeenCalled();
187
-
188
- jasmine.clock().tick(2); // 3001
189
- expect(cb1).not.toHaveBeenCalled();
190
- expect(cb2).toHaveBeenCalled();
191
- expect(eventLog.map_timeouts.size).toBe(1);
192
-
193
- jasmine.clock().tick(1998); // 4999
194
- expect(cb2).toHaveBeenCalled();
195
-
196
- jasmine.clock().tick(2000); // 5001
197
- expect(cb1).toHaveBeenCalled();
198
- expect(cb2).toHaveBeenCalled();
199
- expect(eventLog.map_timeouts.size).toBe(0);
200
- });
201
-
202
- it('should cancel all timeouts', () => {
203
- const cb1 = jasmine.createSpy('callback1');
204
- const cb2 = jasmine.createSpy('callback2');
205
- eventLog.addEvent('test1', 'id1', { value: 1 }, 5000, cb1);
206
- eventLog.addEvent('test2', 'id2', { value: 2 }, 5000, cb2);
207
-
208
- eventLog.cancelAllTimeouts();
209
- jasmine.clock().tick(5001);
210
-
211
- expect(cb1).not.toHaveBeenCalled();
212
- expect(cb2).not.toHaveBeenCalled();
213
- expect(eventLog.map_timeouts.size).toBe(0);
214
- });
215
-
216
- });
217
-
218
- /*
219
- describe('clear', () => {
220
- it('should remove all events from the log', () => {
221
- eventLog.addEvent('EVENT1', 'msg1', { data: 1 });
222
- eventLog.addEvent('EVENT2', 'msg1', { data: 2 });
223
- expect(eventLog.events.length).toBe(2);
224
-
225
- eventLog.clear();
226
- expect(eventLog.events.length).toBe(0);
227
- expect(eventLog.events).toEqual([]);
228
- });
229
-
230
- it('should not affect logr configuration', () => {
231
- const mockLogrConfig = { enabled: true };
232
- eventLog.setLogr(mockLogrConfig);
233
- eventLog.addEvent('EVENT1', 'msg1', {});
234
- eventLog.clear();
235
- expect(eventLog.logr.toggled).toBe(mockLogrConfig);
236
- });
237
- });
238
- */
239
- });