-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.test.ts
More file actions
65 lines (58 loc) · 1.63 KB
/
Copy pathindex.test.ts
File metadata and controls
65 lines (58 loc) · 1.63 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
import assert from "node:assert/strict";
import test from "node:test";
import {
byCountry,
countries,
findCountry,
getByCountry,
has,
ips,
random,
} from "../src";
test("exports the generated IP list", () => {
assert.equal(ips.length, 4312);
assert.equal(countries.length, 20);
assert.equal(ips[0], "93.123.23.1");
assert.equal(ips.at(-1), "118.174.25.251");
});
test("groups IPs by country", () => {
assert.deepEqual(getByCountry("Bulgaria").slice(0, 3), [
"93.123.23.1",
"93.123.23.2",
"93.123.23.3",
]);
assert.equal(getByCountry("Taiwan").length, 1013);
assert.deepEqual(getByCountry("Unknown"), []);
});
test("looks up IP membership and country", () => {
assert.equal(has("93.123.23.1"), true);
assert.equal(has("127.0.0.1"), false);
assert.equal(has(127001), false);
assert.equal(findCountry("118.174.25.251"), "Thailand");
assert.equal(findCountry("127.0.0.1"), undefined);
assert.equal(findCountry(null), undefined);
});
test("returns random IPs from all data or a country", () => {
const originalRandom = Math.random;
Math.random = () => 0;
try {
assert.equal(random(), "93.123.23.1");
assert.equal(random("Bulgaria"), "93.123.23.1");
assert.equal(random("Unknown"), undefined);
} finally {
Math.random = originalRandom;
}
});
test("returns immutable data", () => {
assert.throws(() => {
(ips as string[]).push("127.0.0.1");
}, TypeError);
assert.throws(() => {
(byCountry.Bulgaria as string[]).push("127.0.0.1");
}, TypeError);
});
test("validates country input", () => {
assert.throws(() => {
getByCountry(123 as unknown as string);
}, TypeError);
});