forked from polkadot-js/apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
152 lines (129 loc) · 5.03 KB
/
index.spec.ts
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
// Copyright 2017-2024 @polkadot/apps-config authors & contributors
// SPDX-License-Identifier: Apache-2.0
/// <reference types="@polkadot/dev-test/globals.d.ts" />
import { strict as assert } from 'node:assert';
import { isNumber, isString } from '@polkadot/util';
import { createWsEndpoints } from './index.js';
interface Endpoint {
name: string;
provider: string;
value: string;
}
const allEndpoints = createWsEndpoints(undefined, false, false);
const INVALID_CHARS = ['%'];
describe('WS urls are all valid', (): void => {
const endpoints = allEndpoints
.filter(({ value }) =>
value &&
isString(value) &&
!value.includes('127.0.0.1')
)
.map(({ text, textBy, value }): Endpoint => ({
name: text as string,
provider: textBy,
value
}));
for (const { name, provider, value } of endpoints) {
it(`${name}:: ${provider}`, (): void => {
assert(value.startsWith('wss://') || value.startsWith('light://substrate-connect/'), `${name}:: ${provider} -> ${value} should start with wss:// or light://`);
assert(!INVALID_CHARS.some((c) => value.includes(c)), `${value} should not contain invalid characters such as ${INVALID_CHARS.join(', ')}`);
});
}
});
describe('urls are sorted', (): void => {
let hasDevelopment = false;
let lastHeader = '';
const filtered = allEndpoints.filter(({ isHeader, text }): boolean => {
hasDevelopment = hasDevelopment || (!!isHeader && text === 'Development');
return !hasDevelopment;
});
filtered.forEach(({ isHeader, paraId, text, textBy }, index): void => {
if (isHeader) {
lastHeader = text as string;
} else {
it(`${lastHeader}:: ${text as string}:: ${textBy}`, (): void => {
const item = filtered[index - 1];
assert((
item.isHeader ||
item.linked ||
(
isNumber(item.paraId) &&
(
item.paraId < 2000
? isNumber(paraId) && paraId >= 2000
: false
)
) ||
item.text === '' ||
text === item.text ||
(text as string).localeCompare(item.text as string) === 1
), `${lastHeader}:: ${text as string} needs to be before ${item.text as string}`);
});
}
});
});
describe('urls are not duplicated', (): void => {
let hasDevelopment = false;
let lastHeader = '';
const map = allEndpoints
.filter(({ isDisabled, isHeader, isUnreachable, text }): boolean => {
hasDevelopment = hasDevelopment || (!!isHeader && text === 'Development');
return !hasDevelopment && !isDisabled && !isUnreachable;
})
.reduce((map, { isHeader, text, value }): Record<string, string[]> => {
if (isHeader) {
lastHeader = text as string;
} else {
const path = `${lastHeader} -> ${text as string}`;
const key = value.endsWith('/')
? value.substring(0, value.length - 1)
: value;
map[key] ||= [];
map[key].push(path);
}
return map;
}, {} as Record<string, string[]>);
for (const [url, paths] of Object.entries<string[]>(map)) {
it(`${url}`, (): void => {
assert(paths.length === 1, `${url} appears multiple times - ${paths.map((p) => `\n\t"${p}"`).join('')}`);
});
}
});
describe('endpopints naming', (): void => {
const emoji = /(\u00a9|\u00ae|[\u2000-\u3300]|\ud83c[\ud000-\udfff]|\ud83d[\ud000-\udfff]|\ud83e[\ud000-\udfff])/;
const endpoints: Record<string, Endpoint> = allEndpoints
.filter(({ value }) =>
value &&
isString(value) &&
!value.includes('127.0.0.1')
)
.map(({ text, textBy, value }): Endpoint => ({
name: text as string,
provider: textBy,
value
}))
.reduce((all, e) => ({
...all,
[`${e.name}:: ${e.provider}`]: e
}), {});
for (const [key, { name, provider }] of Object.entries<Endpoint>(endpoints)) {
describe(`${key}`, (): void => {
it(`[${key}] has no emojis`, (): void => {
assert(!emoji.test(name), `${name} should not contain any emojis`);
assert(!emoji.test(provider), `${name}:: ${provider} should not contain any emojis`);
});
it(`[${key}] not all uppercase`, (): void => {
assert(!provider.includes(' ') || (provider.toLocaleUpperCase() !== provider), `${name}:: ${provider} should not be all uppercase`);
});
it(`[${key}] does not contain "Parachain`, (): void => {
assert(!name.includes('Parachain'), `${name} should not contain "Parachain" (redundant)`);
});
it(`[${key}] does not contain a relay name`, (): void => {
assert(!name.includes(' ') || !name.includes('Kusama'), `${name} should not contain "Kusama" (redundant)`);
assert(!name.includes(' ') || !name.includes('Polkadot'), `${name} should not contain "Polkadot" (redundant)`);
assert(!name.includes(' ') || !name.includes('Rococo'), `${name} should not contain "Rococo" (redundant)`);
assert(!name.includes(' ') || !name.includes('Westend'), `${name} should not contain "Westend" (redundant)`);
});
});
}
});