-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcache.test.js
More file actions
103 lines (92 loc) · 2.99 KB
/
Copy pathcache.test.js
File metadata and controls
103 lines (92 loc) · 2.99 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
const { test } = require('node:test');
const assert = require('node:assert/strict');
const { createCache } = require('../cache');
test('set/get round trip', () => {
const c = createCache();
c.set('foo', 'bar');
assert.equal(c.get('foo'), 'bar');
});
test('get returns undefined for missing key', () => {
const c = createCache();
assert.equal(c.get('nope'), undefined);
});
test('expired entries are evicted on read', async () => {
const c = createCache();
c.set('short', 'x', 20);
await new Promise(r => setTimeout(r, 50));
assert.equal(c.get('short'), undefined);
const s = c.getStats();
assert.equal(s.expired, 1);
});
test('ttl of null means no expiry', () => {
const c = createCache({ defaultTtlMs: 10 });
c.set('forever', 'yes', null);
// Even after the default TTL would have expired
assert.equal(c.get('forever'), 'yes');
});
test('delete removes key', () => {
const c = createCache();
c.set('k', 'v');
assert.equal(c.delete('k'), true);
assert.equal(c.get('k'), undefined);
});
test('clear empties all entries', () => {
const c = createCache();
c.set('a', 1);
c.set('b', 2);
c.clear();
assert.equal(c.get('a'), undefined);
assert.equal(c.get('b'), undefined);
assert.equal(c.getStats().size, 0);
});
test('hit/miss stats are tracked', () => {
const c = createCache();
c.set('k', 'v');
c.get('k'); // hit
c.get('k'); // hit
c.get('missing'); // miss
const s = c.getStats();
assert.equal(s.hits, 2);
assert.equal(s.misses, 1);
assert.ok(Math.abs(s.hitRate - 2 / 3) < 0.001);
});
test('maxKeys triggers LRU eviction of oldest', () => {
const c = createCache({ maxKeys: 3 });
c.set('a', 1);
c.set('b', 2);
c.set('c', 3);
c.set('d', 4); // should evict 'a'
assert.equal(c.get('a'), undefined);
assert.equal(c.get('d'), 4);
assert.equal(c.getStats().evictions, 1);
});
test('accessing key promotes it (LRU behavior)', () => {
const c = createCache({ maxKeys: 3 });
c.set('a', 1);
c.set('b', 2);
c.set('c', 3);
c.get('a'); // touch 'a' — now 'b' is oldest
c.set('d', 4); // should evict 'b'
assert.equal(c.get('a'), 1, "'a' should still be present");
assert.equal(c.get('b'), undefined, "'b' should have been evicted");
});
test('remember: computes on miss, caches on subsequent', async () => {
const c = createCache();
let callCount = 0;
const producer = async () => { callCount += 1; return 'computed'; };
const v1 = await c.remember('k', producer);
const v2 = await c.remember('k', producer);
assert.equal(v1, 'computed');
assert.equal(v2, 'computed');
assert.equal(callCount, 1, 'producer should only run once');
});
test('remember: honors explicit ttl', async () => {
const c = createCache();
let callCount = 0;
const producer = async () => { callCount += 1; return callCount; };
await c.remember('k', producer, 20);
await new Promise(r => setTimeout(r, 50));
const v2 = await c.remember('k', producer, 20);
assert.equal(callCount, 2);
assert.equal(v2, 2);
});