Skip to content

Commit 3600f3d

Browse files
committed
WIP - Validation utils tests
1 parent 347b8a4 commit 3600f3d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/validation/utils.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { utils, errors } from '@/validation';
2+
import * as nodesUtils from '@/nodes/utils';
3+
4+
describe('Validation utils', () => {
5+
describe('parseSeedNodes', () => {
6+
const nodeIdEncoded1 = 'vrsc24a1er424epq77dtoveo93meij0pc8ig4uvs9jbeld78n9nl0';
7+
const nodeId1 = nodesUtils.decodeNodeId(nodeIdEncoded1)!;
8+
const nodeIdEncoded2 = 'vrcacp9vsb4ht25hds6s4lpp2abfaso0mptcfnh499n35vfcn2gkg';
9+
const nodeId2 = nodesUtils.decodeNodeId(nodeIdEncoded2)!;
10+
const nodeIdEncoded3 = 'vi3et1hrpv2m2lrplcm7cu913kr45v51cak54vm68anlbvuf83ra0';
11+
const nodeId3 = nodesUtils.decodeNodeId(nodeIdEncoded3)!;
12+
const hostname = 'testnet.polykey.io';
13+
const hostIPv4 = '127.0.0.1';
14+
const hostIPv6 = '[2001:db8:85a3:8d3:1319:8a2e:370:7348]';
15+
const port1 = 1314;
16+
const port2 = 1315;
17+
const port3 = 1316;
18+
test('valid seed nodes (using hostname, IPv4, IPv6)', () => {
19+
const rawSeedNodes =
20+
`${nodeIdEncoded1}@${hostname}:${port1};` +
21+
`${nodeIdEncoded2}@${hostIPv4}:${port2};` +
22+
`${nodeIdEncoded3}@${hostIPv6}:${port3};`;
23+
const parsed = utils.parseSeedNodes(rawSeedNodes);
24+
const seeds = parsed[0];
25+
console.log(seeds);
26+
expect(seeds[nodeId1]).toStrictEqual({
27+
host: hostname,
28+
port: port1,
29+
});
30+
expect(seeds[nodeId2]).toStrictEqual({
31+
host: hostIPv4,
32+
port: port2,
33+
});
34+
expect(seeds[nodeId3]).toStrictEqual({
35+
host: hostIPv6.replace(/\[|\]/g, ''),
36+
port: port3,
37+
});
38+
expect(parsed[1]).toBeFalsy();
39+
});
40+
test('invalid node ID', () => {
41+
const rawSeedNodes = `INVALIDNODEID@${hostname}:${port1}`;
42+
expect(() => utils.parseSeedNodes(rawSeedNodes)).toThrow(errors.ErrorParse);
43+
});
44+
test('invalid hostname', () => {
45+
const rawSeedNodes = `${nodeIdEncoded1}@$invalidHost:${port1}`;
46+
expect(() => utils.parseSeedNodes(rawSeedNodes)).toThrow(errors.ErrorParse);
47+
});
48+
test('invalid port', () => {
49+
const rawSeedNodes = `${nodeIdEncoded1}@${hostname}$:NOTAPORT`;
50+
// TODO: currently failing here
51+
expect(() => utils.parseSeedNodes(rawSeedNodes)).toThrow(errors.ErrorParse);
52+
});
53+
test('invalid structure', () => {
54+
const rawSeedNodes = ``
55+
});
56+
// TODO: remove - just testing this weird behaviour
57+
test.only('invalid', () => {
58+
expect(() => {
59+
new URL('bad URL');
60+
}).toThrowError(TypeError)
61+
// try {
62+
// new URL('bad url');
63+
// } catch (e) {
64+
// if (e instanceof TypeError) {
65+
// console.log('TypeError')
66+
// }
67+
// }
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)