-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.test.ts
More file actions
576 lines (483 loc) · 16.5 KB
/
context.test.ts
File metadata and controls
576 lines (483 loc) · 16.5 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
import { EventEmitter } from 'node:events';
import { createSimpleContext, SimpleContext } from '..';
describe('SimpleContext', () => {
it('should create a context', () => {
const context = createSimpleContext();
expect(context).toBeInstanceOf(SimpleContext);
});
it('should get a value from a context', () => {
const context = createSimpleContext();
context.set('A', 10);
const res = context.get('A');
expect(res).toBe(10);
});
it('should display value in order with fork', async () => {
const context = createSimpleContext();
const func = (): string => {
const foo = context.get('foo');
return `foo=${foo}`;
};
context.fork();
context.set('foo', 'bar');
const res = await Promise.all([
new Promise((resolve) => {
context.fork().set('foo', 'tata');
setTimeout(() => {
resolve(func());
}, 400);
}),
new Promise((resolve) => {
context.fork().set('foo', 'toto');
setTimeout(() => {
resolve(func());
}, 200);
}),
new Promise((resolve) => {
context.fork().set('foo', 'titi');
setTimeout(() => {
resolve(func());
}, 100);
}),
new Promise((resolve) => {
context.fork().set('foo', 'tutu');
setTimeout(() => {
resolve(func());
}, 600);
}),
]);
expect(res).toStrictEqual(['foo=tata', 'foo=toto', 'foo=titi', 'foo=tutu']);
});
it('should display value in order with multiple context', async () => {
const contextA = createSimpleContext();
const contextB = createSimpleContext();
const contextC = createSimpleContext();
const contextD = createSimpleContext();
const func = (context: SimpleContext): string => {
const foo = context.get('foo');
return `foo=${foo}`;
};
const res = await Promise.all([
new Promise((resolve) => {
contextA.set('foo', 'tata');
setTimeout(() => {
resolve(func(contextA));
}, 400);
}),
new Promise((resolve) => {
contextB.set('foo', 'toto');
setTimeout(() => {
resolve(func(contextB));
}, 200);
}),
new Promise((resolve) => {
contextC.set('foo', 'titi');
setTimeout(() => {
resolve(func(contextC));
}, 100);
}),
new Promise((resolve) => {
contextD.set('foo', 'tutu');
setTimeout(() => {
resolve(func(contextD));
}, 600);
}),
]);
expect(res).toStrictEqual(['foo=tata', 'foo=toto', 'foo=titi', 'foo=tutu']);
});
it('should display value in order with multiple value', async () => {
const context = createSimpleContext();
const func = (key: string): string => {
const foo = context.get(key);
return `foo=${foo}`;
};
const res = await Promise.all([
new Promise((resolve) => {
context.set('foo1', 'tata');
setTimeout(() => {
resolve(func('foo1'));
}, 400);
}),
new Promise((resolve) => {
context.set('foo2', 'toto');
setTimeout(() => {
resolve(func('foo2'));
}, 200);
}),
new Promise((resolve) => {
context.set('foo3', 'titi');
setTimeout(() => {
resolve(func('foo3'));
}, 100);
}),
new Promise((resolve) => {
context.set('foo4', 'tutu');
setTimeout(() => {
resolve(func('foo4'));
}, 600);
}),
]);
expect(res).toStrictEqual(['foo=tata', 'foo=toto', 'foo=titi', 'foo=tutu']);
});
it('should work if the values are wrapped in a callback run', async () => {
function Deferred<T>() {
let resolve!: (value: T) => void;
let reject!: (reason?: unknown) => void;
const promise = new Promise<T>((thisResolve, thisReject) => {
resolve = thisResolve;
reject = thisReject;
});
return Object.assign(promise, { resolve, reject });
}
const emitter = new EventEmitter();
const context = createSimpleContext();
const deferredTiti = Deferred<string>();
const deferredToto = Deferred<string>();
emitter.on('titi', () => {
context.fork(() => {
context.set('foo', 'titi2');
});
});
emitter.on('toto', () => {
context.fork(() => {
context.set('foo', 'toto2');
});
});
setTimeout(() => {
context.fork(() => {
context.set('foo', 'titi');
emitter.emit('titi');
setTimeout(() => {
const foo = context.get('foo') as string;
deferredTiti.resolve(foo ?? 'titi');
}, 50);
});
}, 100);
setTimeout(() => {
context.fork(() => {
context.set('foo', 'toto');
emitter.emit('toto');
setTimeout(() => {
const foo = context.get('foo') as string;
deferredToto.resolve(foo ?? 'toto');
}, 50);
});
}, 200);
expect([await deferredTiti, await deferredToto]).toStrictEqual([
'titi',
'toto',
]);
});
it('should return good value with the callback', async () => {
const context = createSimpleContext();
const string = context
.fork(() => {
return 'hello';
})
.toUpperCase();
expect(string).toBe('HELLO');
});
// New tests for added methods
describe('delete method', () => {
it('should delete a value from context', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
expect(context.get('foo')).toBe('bar');
const result = context.delete('foo');
expect(result).toBe(true);
expect(context.get('foo')).toBeUndefined();
});
it('should return false when deleting non-existent key', () => {
const context = createSimpleContext();
const result = context.delete('nonexistent');
expect(result).toBe(false);
});
it('should work with forked contexts', () => {
const context = createSimpleContext();
context.set('foo', 'parent');
context.fork(() => {
context.set('bar', 'child');
expect(context.delete('bar')).toBe(true);
expect(context.has('bar')).toBe(false);
});
});
});
describe('has method', () => {
it('should return true for existing key', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
expect(context.has('foo')).toBe(true);
});
it('should return false for non-existent key', () => {
const context = createSimpleContext();
expect(context.has('foo')).toBe(false);
});
it('should work with forked contexts', () => {
const context = createSimpleContext();
context.set('foo', 'parent');
context.fork(() => {
context.set('bar', 'child');
expect(context.has('foo')).toBe(true);
expect(context.has('bar')).toBe(true);
});
});
});
describe('clear method', () => {
it('should clear all values from context', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
context.set('baz', 'qux');
expect(context.size()).toBe(2);
context.clear();
expect(context.size()).toBe(0);
expect(context.get('foo')).toBeUndefined();
expect(context.get('baz')).toBeUndefined();
});
it('should work with forked contexts without affecting parent', () => {
const context = createSimpleContext();
context.set('foo', 'parent');
context.set('bar', 'parent');
context.fork(() => {
context.set('child', 'value');
context.clear();
expect(context.size()).toBe(0);
});
expect(context.size()).toBe(2);
expect(context.get('foo')).toBe('parent');
});
});
describe('getAll method', () => {
it('should return all key-value pairs', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
context.set('baz', 'qux');
const all = context.getAll();
expect(all).toEqual({ foo: 'bar', baz: 'qux' });
});
it('should return a copy, not the original object', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
const all = context.getAll();
(all as Record<string, unknown>).foo = 'modified';
expect(context.get('foo')).toBe('bar');
});
it('should work with forked contexts', () => {
const context = createSimpleContext();
context.set('foo', 'parent');
context.fork(() => {
context.set('bar', 'child');
const all = context.getAll();
expect(all).toEqual({ foo: 'parent', bar: 'child' });
});
});
});
describe('keys method', () => {
it('should return all keys', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
context.set('baz', 'qux');
const keys = context.keys();
expect(keys).toEqual(expect.arrayContaining(['foo', 'baz']));
expect(keys.length).toBe(2);
});
it('should return empty array when no keys', () => {
const context = createSimpleContext();
expect(context.keys()).toEqual([]);
});
});
describe('size method', () => {
it('should return the number of entries', () => {
const context = createSimpleContext();
expect(context.size()).toBe(0);
context.set('foo', 'bar');
expect(context.size()).toBe(1);
context.set('baz', 'qux');
expect(context.size()).toBe(2);
});
it('should decrease when deleting keys', () => {
const context = createSimpleContext();
context.set('foo', 'bar');
context.set('baz', 'qux');
expect(context.size()).toBe(2);
context.delete('foo');
expect(context.size()).toBe(1);
});
});
describe('validation', () => {
it('should throw TypeError for non-string key in get', () => {
const context = createSimpleContext();
expect(() => context.get(123 as unknown as string)).toThrow(TypeError);
expect(() => context.get(null as unknown as string)).toThrow(TypeError);
});
it('should throw TypeError for empty string key in get', () => {
const context = createSimpleContext();
expect(() => context.get('')).toThrow(TypeError);
});
it('should throw TypeError for non-string key in set', () => {
const context = createSimpleContext();
expect(() => context.set(123 as unknown as string, 'value')).toThrow(
TypeError,
);
});
it('should throw TypeError for empty string key in set', () => {
const context = createSimpleContext();
expect(() => context.set('', 'value')).toThrow(TypeError);
});
it('should throw TypeError for non-string key in delete', () => {
const context = createSimpleContext();
expect(() => context.delete(123 as unknown as string)).toThrow(TypeError);
});
it('should throw TypeError for non-string key in has', () => {
const context = createSimpleContext();
expect(() => context.has(123 as unknown as string)).toThrow(TypeError);
});
});
describe('fork improvements', () => {
it('should inherit properties from forked context', () => {
const context = createSimpleContext();
context.set('parentKey', 'parentValue');
context.fork(() => {
expect(context.get('parentKey')).toBe('parentValue');
context.set('childKey', 'childValue');
expect(context.get('childKey')).toBe('childValue');
});
expect(context.has('childKey')).toBe(false);
});
it('should not modify parent context from fork', () => {
const context = createSimpleContext();
context.set('key', 'original');
context.fork(() => {
context.set('key', 'modified');
expect(context.get('key')).toBe('modified');
});
expect(context.get('key')).toBe('original');
});
it('should isolate nested forks (depth > 2)', () => {
const context = createSimpleContext();
context.set('level', 'root');
context.fork(() => {
context.set('level', 'child');
context.fork(() => {
context.set('level', 'grandchild');
expect(context.get('level')).toBe('grandchild');
});
expect(context.get('level')).toBe('child');
});
expect(context.get('level')).toBe('root');
});
it('should not leak deleted keys from fork to parent', () => {
const context = createSimpleContext();
context.set('foo', 'parent');
context.fork(() => {
expect(context.has('foo')).toBe(true);
context.delete('foo');
expect(context.has('foo')).toBe(false);
});
expect(context.has('foo')).toBe(true);
expect(context.get('foo')).toBe('parent');
});
});
describe('edge cases', () => {
it('should not report prototype keys via has()', () => {
const context = createSimpleContext();
expect(context.has('toString')).toBe(false);
expect(context.has('hasOwnProperty')).toBe(false);
expect(context.has('constructor')).toBe(false);
});
it('should not report prototype keys via has() inside a fork', () => {
const context = createSimpleContext();
context.fork(() => {
expect(context.has('toString')).toBe(false);
expect(context.has('constructor')).toBe(false);
});
});
it('should distinguish undefined value from missing key', () => {
const context = createSimpleContext();
context.set('key', undefined);
expect(context.has('key')).toBe(true);
expect(context.get('key')).toBeUndefined();
});
it('should return a shallow copy from getAll (nested objects are shared)', () => {
const context = createSimpleContext();
const nested = { inner: 'original' };
context.set('obj', nested);
const all = context.getAll();
(all as Record<string, { inner: string }>).obj.inner = 'mutated';
const retrieved = context.get('obj') as { inner: string };
expect(retrieved?.inner).toBe('mutated');
});
it('should handle concurrent fork callbacks without interference', async () => {
const context = createSimpleContext();
const [resultA, resultB] = await Promise.all([
new Promise<unknown>((resolve) => {
context.fork(() => {
context.set('val', 'A');
setTimeout(() => resolve(context.get('val')), 50);
});
}),
new Promise<unknown>((resolve) => {
context.fork(() => {
context.set('val', 'B');
setTimeout(() => resolve(context.get('val')), 50);
});
}),
]);
expect(resultA).toBe('A');
expect(resultB).toBe('B');
});
});
describe('typed context', () => {
type UserSchema = {
userId: string;
count: number;
active: boolean;
};
it('should create a typed context', () => {
const context = createSimpleContext<UserSchema>();
expect(context).toBeInstanceOf(SimpleContext);
});
it('should set and get typed values', () => {
const context = createSimpleContext<UserSchema>();
context.set('userId', 'abc123');
context.set('count', 42);
context.set('active', true);
expect(context.get('userId')).toBe('abc123');
expect(context.get('count')).toBe(42);
expect(context.get('active')).toBe(true);
});
it('should return typed keys', () => {
const context = createSimpleContext<UserSchema>();
context.set('userId', 'abc');
context.set('count', 1);
const keys = context.keys();
expect(keys).toEqual(expect.arrayContaining(['userId', 'count']));
});
it('should return typed partial from getAll', () => {
const context = createSimpleContext<UserSchema>();
context.set('userId', 'abc');
context.set('count', 1);
const all = context.getAll();
expect(all).toEqual({ userId: 'abc', count: 1 });
});
it('should work with fork and preserve types', () => {
const context = createSimpleContext<UserSchema>();
context.set('userId', 'parent');
context.fork(() => {
context.set('userId', 'child');
expect(context.get('userId')).toBe('child');
});
expect(context.get('userId')).toBe('parent');
});
it('should support has/delete/clear with typed keys', () => {
const context = createSimpleContext<UserSchema>();
context.set('userId', 'abc');
context.set('count', 5);
expect(context.has('userId')).toBe(true);
expect(context.size()).toBe(2);
context.delete('count');
expect(context.has('count')).toBe(false);
expect(context.size()).toBe(1);
context.clear();
expect(context.size()).toBe(0);
});
});
});