diff --git a/tests/validation/utils.test.ts b/tests/validation/utils.test.ts new file mode 100644 index 0000000000..e9a0ed3ba4 --- /dev/null +++ b/tests/validation/utils.test.ts @@ -0,0 +1,70 @@ +import { utils, errors } from '@/validation'; +import * as nodesUtils from '@/nodes/utils'; + +describe('Validation utils', () => { + describe('parseSeedNodes', () => { + const nodeIdEncoded1 = 'vrsc24a1er424epq77dtoveo93meij0pc8ig4uvs9jbeld78n9nl0'; + const nodeId1 = nodesUtils.decodeNodeId(nodeIdEncoded1)!; + const nodeIdEncoded2 = 'vrcacp9vsb4ht25hds6s4lpp2abfaso0mptcfnh499n35vfcn2gkg'; + const nodeId2 = nodesUtils.decodeNodeId(nodeIdEncoded2)!; + const nodeIdEncoded3 = 'vi3et1hrpv2m2lrplcm7cu913kr45v51cak54vm68anlbvuf83ra0'; + const nodeId3 = nodesUtils.decodeNodeId(nodeIdEncoded3)!; + const hostname = 'testnet.polykey.io'; + const hostIPv4 = '127.0.0.1'; + const hostIPv6 = '[2001:db8:85a3:8d3:1319:8a2e:370:7348]'; + const port1 = 1314; + const port2 = 1315; + const port3 = 1316; + test('valid seed nodes (using hostname, IPv4, IPv6)', () => { + const rawSeedNodes = + `${nodeIdEncoded1}@${hostname}:${port1};` + + `${nodeIdEncoded2}@${hostIPv4}:${port2};` + + `${nodeIdEncoded3}@${hostIPv6}:${port3};`; + const parsed = utils.parseSeedNodes(rawSeedNodes); + const seeds = parsed[0]; + console.log(seeds); + expect(seeds[nodeId1]).toStrictEqual({ + host: hostname, + port: port1, + }); + expect(seeds[nodeId2]).toStrictEqual({ + host: hostIPv4, + port: port2, + }); + expect(seeds[nodeId3]).toStrictEqual({ + host: hostIPv6.replace(/\[|\]/g, ''), + port: port3, + }); + expect(parsed[1]).toBeFalsy(); + }); + test('invalid node ID', () => { + const rawSeedNodes = `INVALIDNODEID@${hostname}:${port1}`; + expect(() => utils.parseSeedNodes(rawSeedNodes)).toThrow(errors.ErrorParse); + }); + test('invalid hostname', () => { + const rawSeedNodes = `${nodeIdEncoded1}@$invalidHost:${port1}`; + expect(() => utils.parseSeedNodes(rawSeedNodes)).toThrow(errors.ErrorParse); + }); + test('invalid port', () => { + const rawSeedNodes = `${nodeIdEncoded1}@${hostname}$:NOTAPORT`; + // TODO: currently failing here + expect(() => utils.parseSeedNodes(rawSeedNodes)).toThrow(errors.ErrorParse); + }); + test('invalid structure', () => { + const rawSeedNodes = `` + }); + // TODO: remove - just testing this weird behaviour + test.only('invalid', () => { + expect(() => { + new URL('bad URL'); + }).toThrowError(TypeError) + // try { + // new URL('bad url'); + // } catch (e) { + // if (e instanceof TypeError) { + // console.log('TypeError') + // } + // } + }); + }); +});