Skip to content

Commit 61e48fc

Browse files
committed
test: 💍 Added tests for NodeRequestManager
1 parent b515fe6 commit 61e48fc

File tree

1 file changed

+193
-0
lines changed

1 file changed

+193
-0
lines changed

test/node.requestManager.spec.ts

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
import './setup.js';
2+
import { expect } from 'chai';
3+
import { stringify } from 'viem';
4+
import { NodeRequestManager } from '../src/index.js';
5+
import { parseSeconds } from '../src/utils/time.js';
6+
7+
describe('Node.NodeRequestManager', () => {
8+
const defaultNoncePeriod = 1;
9+
let nodeRequestManager: NodeRequestManager;
10+
11+
beforeEach(() => {
12+
nodeRequestManager = new NodeRequestManager({
13+
noncePeriod: defaultNoncePeriod,
14+
});
15+
});
16+
17+
describe('#constructor', () => {
18+
it('should construct with correct noncePeriod', () => {
19+
expect(nodeRequestManager).to.have.property('noncePeriod', 1);
20+
});
21+
});
22+
23+
describe('#setNoncePeriod', () => {
24+
it('should set new nonce period with string value', () => {
25+
nodeRequestManager.setNoncePeriod('5s');
26+
expect(nodeRequestManager).to.have.property('noncePeriod', 5);
27+
});
28+
29+
it('should set new nonce period with number value', () => {
30+
nodeRequestManager.setNoncePeriod(7000);
31+
expect(nodeRequestManager).to.have.property('noncePeriod', 7000);
32+
});
33+
34+
it('should throw of invalid nonce period provided', () => {
35+
expect(() => {
36+
nodeRequestManager.setNoncePeriod('50X');
37+
}).to.throw('Unknown duration time format');
38+
});
39+
});
40+
41+
describe('#add', () => {
42+
it('should add request to cache', () => {
43+
const requestTopic = 'testTopic';
44+
const data = stringify({
45+
id: '1',
46+
nonce: 1,
47+
expire: Math.floor(Date.now() / 1000) + 20,
48+
});
49+
50+
nodeRequestManager.add(requestTopic, data);
51+
expect(nodeRequestManager['cache'].has('1')).to.be.true;
52+
});
53+
54+
it('should not add expired request to cache', () => {
55+
const requestTopic = 'testTopic';
56+
const data = stringify({
57+
id: '1',
58+
nonce: 1,
59+
expire: Math.floor(Date.now() / 1000) - 20,
60+
});
61+
62+
nodeRequestManager.add(requestTopic, data);
63+
expect(nodeRequestManager['cache'].has('1')).to.be.false;
64+
});
65+
66+
it('should not add request that will expire before it can be processed', () => {
67+
const requestTopic = 'testTopic';
68+
const data = stringify({
69+
id: '1',
70+
nonce: 1,
71+
expire:
72+
Math.floor(Date.now() / 1000) +
73+
Number(parseSeconds(defaultNoncePeriod)),
74+
});
75+
76+
nodeRequestManager.add(requestTopic, data);
77+
expect(nodeRequestManager['cache'].has('1')).to.be.false;
78+
});
79+
80+
it('should update request in cache if nonce is higher', () => {
81+
const requestTopic = 'testTopic';
82+
const data1 = stringify({
83+
id: '1',
84+
nonce: 1,
85+
expire: Math.floor(Date.now() / 1000) + 20,
86+
});
87+
const data2 = stringify({
88+
id: '1',
89+
nonce: 2,
90+
expire: Math.floor(Date.now() / 1000) + 20,
91+
});
92+
93+
nodeRequestManager.add(requestTopic, data1);
94+
nodeRequestManager.add(requestTopic, data2);
95+
expect(nodeRequestManager['cache'].get('1')?.data.nonce).to.equal(2);
96+
});
97+
98+
it('should not update request in cache if nonce is lower', () => {
99+
const requestTopic = 'testTopic';
100+
const data1 = stringify({
101+
id: '1',
102+
nonce: 2,
103+
expire: Math.floor(Date.now() / 1000) + 20,
104+
});
105+
const data2 = stringify({
106+
id: '1',
107+
nonce: 1,
108+
expire: Math.floor(Date.now() / 1000) + 20,
109+
});
110+
111+
nodeRequestManager.add(requestTopic, data1);
112+
nodeRequestManager.add(requestTopic, data2);
113+
expect(nodeRequestManager['cache'].get('1')?.data.nonce).to.equal(2);
114+
});
115+
116+
it('should not add request if data is not valid JSON', (done) => {
117+
const requestTopic = 'testTopic';
118+
const data = 'invalid JSON';
119+
const sizeBefore = nodeRequestManager['cache'].size;
120+
121+
nodeRequestManager.addEventListener(
122+
'error',
123+
() => {
124+
expect(nodeRequestManager['cache'].size).to.eq(sizeBefore);
125+
done();
126+
},
127+
{ once: true },
128+
);
129+
130+
nodeRequestManager.add(requestTopic, data);
131+
});
132+
133+
it('should emit request event after nonce period', (done) => {
134+
const requestTopic = 'testTopic';
135+
const data = stringify({
136+
id: '1',
137+
nonce: 1,
138+
expire: Math.floor(Date.now() / 1000) + 20,
139+
});
140+
141+
nodeRequestManager.addEventListener(
142+
'request',
143+
(event) => {
144+
expect(event.detail.topic).to.equal(requestTopic);
145+
expect(event.detail.data).to.deep.equal(JSON.parse(data));
146+
done();
147+
},
148+
{ once: true },
149+
);
150+
151+
nodeRequestManager.add(requestTopic, data);
152+
});
153+
});
154+
155+
describe('#prune', () => {
156+
it('should remove expired requests from cache', () => {
157+
const requestTopic = 'testTopic';
158+
const data1 = JSON.stringify({
159+
id: '1',
160+
nonce: 1,
161+
expire: Math.floor(Date.now() / 1000) + 20,
162+
});
163+
const data2 = JSON.stringify({
164+
id: '2',
165+
nonce: 1,
166+
expire: Math.floor(Date.now() / 1000) - 20,
167+
});
168+
169+
nodeRequestManager.add(requestTopic, data1);
170+
nodeRequestManager.add(requestTopic, data2);
171+
nodeRequestManager.prune();
172+
173+
expect(nodeRequestManager['cache'].has('1')).to.be.true;
174+
expect(nodeRequestManager['cache'].has('2')).to.be.false;
175+
});
176+
});
177+
178+
describe('#clear', () => {
179+
it('should clear the cache', () => {
180+
const requestTopic = 'testTopic';
181+
const data = JSON.stringify({
182+
id: '1',
183+
nonce: 1,
184+
expire: Math.floor(Date.now() / 1000) + 20,
185+
});
186+
187+
nodeRequestManager.add(requestTopic, data);
188+
nodeRequestManager.clear();
189+
190+
expect(nodeRequestManager['cache'].size).to.equal(0);
191+
});
192+
});
193+
});

0 commit comments

Comments
 (0)