-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorScope.test.js
More file actions
307 lines (259 loc) · 11.1 KB
/
MonitorScope.test.js
File metadata and controls
307 lines (259 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import { assert } from './test-utils/deps-node.js';
import { MonitorScope } from '../ts/index.ts';
// Mock connection that simulates Pryv API responses
function createMockConnection (responses = {}) {
return {
apiCalls: [],
async api (calls) {
this.apiCalls.push(calls);
return calls.map(call => {
const _key = call.method + ':' + JSON.stringify(call.params);
// Check for specific response handlers
if (responses[call.method]) {
return responses[call.method](call.params);
}
// Default empty responses
if (call.method === 'events.get') return { events: [] };
if (call.method === 'streams.get') return { streams: [] };
return {};
});
}
};
}
// Since MonitorScope creates a real Pryv.Monitor internally, and we can't
// easily mock that without the full pryv module, we test the pre-Monitor logic
// by stopping before Monitor starts.
describe('[MSC] MonitorScope', () => {
const now = Date.now() / 1000;
const oneDay = 86400;
const thirtyDaysAgo = now - 30 * oneDay;
it('[MSC1] constructor sets config correctly', () => {
const conn = createMockConnection();
const config = { pageSize: 100, fromTime: thirtyDaysAgo, toTime: now };
const ms = new MonitorScope(conn, config, { onEvent: () => {} });
assert.strictEqual(ms.hasMoreOlder, false);
});
it('[MSC2] loadMore returns empty when hasMoreOlder is false', async () => {
const conn = createMockConnection();
const config = { pageSize: 100, fromTime: thirtyDaysAgo };
const ms = new MonitorScope(conn, config, { onEvent: () => {} });
const result = await ms.loadMore();
assert.deepStrictEqual(result, { events: [], hasMore: false });
});
it('[MSC3] stop prevents start from proceeding', async () => {
const conn = createMockConnection();
const config = { pageSize: 100, fromTime: thirtyDaysAgo };
const events = [];
const ms = new MonitorScope(conn, config, { onEvent: (e) => events.push(e) });
ms.stop();
await ms.start();
assert.strictEqual(events.length, 0);
assert.strictEqual(conn.apiCalls.length, 0);
});
it('[MSC4] stop is idempotent', () => {
const conn = createMockConnection();
const config = { pageSize: 100, fromTime: thirtyDaysAgo };
const ms = new MonitorScope(conn, config, { onEvent: () => {} });
ms.stop();
ms.stop(); // should not throw
});
describe('[MSC-LOAD] event loading logic (mocked, no Monitor)', () => {
// For these tests, we test the batch API call and event processing
// by checking what API calls are made and what events are delivered
it('[MSC5] Step 1 delivers events and streams via callbacks', async () => {
const mockEvents = [
{ id: 'e1', time: now - 100, modified: now - 100, streamIds: ['s1'], type: 'note/txt', content: 'a' },
{ id: 'e2', time: now - 200, modified: now - 200, streamIds: ['s1'], type: 'note/txt', content: 'b' },
];
const mockStreams = [{ id: 's1', name: 'Stream 1', children: [] }];
const conn = createMockConnection({
'events.get': (params) => {
if (params.limit) return { events: mockEvents };
return { events: [] };
},
'streams.get': () => ({ streams: mockStreams }),
});
const receivedEvents = [];
let receivedStreams = null;
const config = { pageSize: 100, fromTime: thirtyDaysAgo, toTime: now };
const ms = new MonitorScope(conn, config, {
onEvent: (e) => receivedEvents.push(e),
onStreams: (s) => { receivedStreams = s; },
});
// Stop immediately after start to prevent Monitor creation
// We need to test the pre-Monitor logic
// Since start() awaits the API call before creating Monitor,
// we can't easily intercept. Instead, just verify the API calls structure.
// The Monitor constructor will fail without pryv setup, but events will be delivered.
try {
await ms.start();
} catch (_e) {
// Monitor creation may fail in test env — that's OK, we test the loading logic
}
ms.stop();
assert.strictEqual(receivedEvents.length, 2);
assert.strictEqual(receivedEvents[0].id, 'e1');
assert.strictEqual(receivedEvents[1].id, 'e2');
assert.deepStrictEqual(receivedStreams, mockStreams);
// Verify the API batch call structure
assert.strictEqual(conn.apiCalls.length >= 1, true);
const firstBatch = conn.apiCalls[0];
assert.strictEqual(firstBatch[0].method, 'events.get');
assert.strictEqual(firstBatch[0].params.limit, 100);
assert.strictEqual(firstBatch[1].method, 'streams.get');
});
it('[MSC6] pages backwards progressively when full pages returned', async () => {
const pageSize = 3;
let callCount = 0;
const conn = createMockConnection({
'events.get': () => {
callCount++;
if (callCount === 1) {
// First page: 3 events (full page)
return {
events: [
{ id: 'e1', time: now - 100, modified: now - 100, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e2', time: now - 200, modified: now - 200, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e3', time: now - 300, modified: now - 300, streamIds: ['s1'], type: 'note/txt' },
]
};
}
if (callCount === 2) {
// Second page: older events (partial = last page)
return {
events: [
{ id: 'e4', time: now - 400, modified: now - 400, streamIds: ['s1'], type: 'note/txt' },
]
};
}
return { events: [] };
},
'streams.get': () => ({ streams: [] }),
});
const receivedEvents = [];
const config = { pageSize, fromTime: now - 10000, toTime: now };
const ms = new MonitorScope(conn, config, {
onEvent: (e) => receivedEvents.push(e),
});
try { await ms.start(); } catch (_e) { /* Monitor may fail */ }
ms.stop();
// First page (3) + second page (1) = 4 events total
assert.strictEqual(receivedEvents.length, 4);
assert.strictEqual(ms.hasMoreOlder, true); // optimistic
});
it('[MSC7] hasMoreOlder is true when events exist (optimistic until loadMore proves otherwise)', async () => {
const mockEvents = [
{ id: 'e1', time: now - 100, modified: now - 100, streamIds: ['s1'], type: 'note/txt' },
];
const conn = createMockConnection({
'events.get': () => ({ events: mockEvents }),
'streams.get': () => ({ streams: [] }),
});
const receivedEvents = [];
const config = { pageSize: 100, fromTime: thirtyDaysAgo, toTime: now };
const ms = new MonitorScope(conn, config, {
onEvent: (e) => receivedEvents.push(e),
});
try { await ms.start(); } catch (_e) { /* Monitor may fail */ }
// hasMoreOlder is true optimistically — loadMore will set false if no older events found
assert.strictEqual(ms.hasMoreOlder, true);
ms.stop();
});
it('[MSC8] tracks oldest event time correctly', async () => {
const mockEvents = [
{ id: 'e1', time: now - 500, modified: now - 500, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e2', time: now - 100, modified: now - 100, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e3', time: now - 1000, modified: now - 1000, streamIds: ['s1'], type: 'note/txt' },
];
const conn = createMockConnection({
'events.get': () => ({ events: mockEvents }),
'streams.get': () => ({ streams: [] }),
});
const receivedEvents = [];
const config = { pageSize: 100, fromTime: thirtyDaysAgo, toTime: now };
const ms = new MonitorScope(conn, config, {
onEvent: (e) => receivedEvents.push(e),
});
try { await ms.start(); } catch (_e) { /* Monitor may fail */ }
ms.stop();
// All 3 events should be received
assert.strictEqual(receivedEvents.length, 3);
});
it('[MSC-BATCH] onEvents receives batches instead of individual events', async () => {
const mockEvents = [
{ id: 'e1', time: now - 100, modified: now - 100, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e2', time: now - 200, modified: now - 200, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e3', time: now - 300, modified: now - 300, streamIds: ['s1'], type: 'note/txt' },
];
const conn = createMockConnection({
'events.get': (params) => {
if (params.limit) return { events: mockEvents };
return { events: [] };
},
'streams.get': () => ({ streams: [] }),
});
const batches = [];
const config = { pageSize: 100, fromTime: thirtyDaysAgo, toTime: now };
const ms = new MonitorScope(conn, config, {
onEvents: (events) => batches.push([...events]),
});
try { await ms.start(); } catch (_e) { /* Monitor may fail */ }
ms.stop();
// Should receive one batch with all 3 events
assert.strictEqual(batches.length, 1);
assert.strictEqual(batches[0].length, 3);
assert.strictEqual(batches[0][0].id, 'e1');
});
it('[MSC-PAGE] pages backwards until fromTime boundary', async () => {
const pageSize = 2;
let callCount = 0;
const conn = createMockConnection({
'events.get': (params) => {
callCount++;
if (callCount === 1) {
// First page: 2 events (full page)
return {
events: [
{ id: 'e1', time: now - 100, modified: now - 100, streamIds: ['s1'], type: 'note/txt' },
{ id: 'e2', time: now - 200, modified: now - 200, streamIds: ['s1'], type: 'note/txt' },
]
};
}
if (callCount === 2) {
// Second page: 1 event (partial = last page)
return {
events: [
{ id: 'e3', time: now - 300, modified: now - 300, streamIds: ['s1'], type: 'note/txt' },
]
};
}
return { events: [] };
},
'streams.get': () => ({ streams: [] }),
});
const allReceived = [];
const batchCount = { n: 0 };
const config = { pageSize, fromTime: now - 10000, toTime: now };
const ms = new MonitorScope(conn, config, {
onEvents: (events) => { batchCount.n++; allReceived.push(...events); },
});
try { await ms.start(); } catch (_e) { /* Monitor may fail */ }
ms.stop();
// Should have made 2 events.get calls (first page + one gap-fill page)
assert.strictEqual(allReceived.length, 3);
assert.strictEqual(batchCount.n, 2); // 2 batches delivered progressively
});
it('[MSC9] onError callback receives errors', () => {
const conn = createMockConnection();
let receivedError = null;
const config = { pageSize: 100, fromTime: thirtyDaysAgo };
const ms = new MonitorScope(conn, config, {
onEvent: () => {},
onError: (e) => { receivedError = e; },
});
// onError is wired to Monitor — just verify it's accepted without error
assert.strictEqual(receivedError, null);
ms.stop();
});
});
});