-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils_test.ts
230 lines (214 loc) · 7.33 KB
/
utils_test.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import { assert, assertEquals } from "@std/assert";
import { check_running } from "./pid_check.ts";
import {
add_suffix_to_path,
asyncEvery,
asyncFilter,
asyncForEach,
calFileMd5,
calFileSha1,
compareNum,
filterFilename,
getHashFromUrl,
map,
parseBigInt,
promiseState,
PromiseStatus,
sleep,
sure_dir,
toJSON,
} from "./utils.ts";
import { md5 } from "lifegpc-md5";
import { sha1 } from "@lifegpc/sha1";
Deno.test("promiseState_test", async () => {
const p1 = new Promise((res) => setTimeout(() => res(100), 100));
const p2 = new Promise((res) => setTimeout(() => res(200), 200));
const p3 = new Promise((_res, rej) => setTimeout(() => rej(300), 100));
async function getStates() {
console.log(await promiseState(p1));
console.log(await promiseState(p2));
console.log(await promiseState(p3));
}
console.log("Immediately after initiation:");
await getStates();
await sleep(100);
console.log("After waiting for 100ms:");
await getStates();
await Promise.allSettled([p1, p2, p3]);
});
Deno.test("Pid_Test", async () => {
if (Deno.build.os == "windows" || Deno.build.os == "linux") {
assertEquals(await check_running(Deno.pid), true);
}
});
Deno.test("asyncFilter_test", async () => {
const t = [3, 5];
const r = await asyncFilter(t, async (t) => {
await sleep(10);
return t === 3;
});
assertEquals(r, [3]);
const e = [new Promise<number>((res) => setTimeout(() => res(100), 100))];
const v = await asyncFilter(e, async function (d) {
assertEquals(this, t);
const s = await promiseState(d);
return s.status === PromiseStatus.Pending;
}, t);
assertEquals(v, e);
const e2 = { 0: e[0], length: 1 };
const v2 = await asyncFilter(e2, async function (d) {
assertEquals(this, t);
const s = await promiseState(d);
return s.status === PromiseStatus.Pending;
}, t);
assertEquals(v, v2);
await Promise.allSettled(e);
});
Deno.test("asyncForEach_test", async () => {
const e = [new Promise<number>((res) => setTimeout(() => res(100), 100))];
const t = { test: 2 };
await asyncForEach(e, async function (e) {
assertEquals(this, t);
await e;
}, t);
});
Deno.test("filterFilename_test", () => {
assertEquals(filterFilename("abcdef.ts", 5), "ab.ts");
assertEquals(filterFilename("\x00df\t\r.ts"), "df.ts");
assertEquals(filterFilename("a\u200bd.ts"), "ad.ts");
assertEquals(filterFilename("中文.ts"), "中文.ts");
assertEquals(filterFilename("d\\s/.ts"), "d_s_.ts");
if (Deno.build.os == "windows") {
assertEquals(filterFilename("d|?ad.ts"), "d__ad.ts");
}
assertEquals(filterFilename("t\\.ts", 0), "t_.ts");
});
Deno.test("add_suffix_to_path_test", () => {
assertEquals(add_suffix_to_path("test.ts", "ok"), "test-ok.ts");
assertEquals(add_suffix_to_path("test", "ok"), "test-ok");
const t = add_suffix_to_path("test.ts");
console.log(t);
assert(t.startsWith("test-"));
assert(t.endsWith(".ts"));
});
Deno.test("calFileMd5_test", async () => {
await sure_dir();
const text = `Hello World.te${Math.random()}`;
await Deno.writeTextFile("./test/test.txt", text);
assertEquals(await calFileMd5("./test/test.txt"), md5(text));
});
Deno.test("calFileSha1_test", async () => {
await sure_dir();
const text = `Hello World.te${Math.random()}dsadasd`;
await Deno.writeTextFile("./test/testsha1.txt", text);
assertEquals(await calFileSha1("./test/testsha1.txt"), sha1(text));
});
Deno.test("asyncEvery_test", async () => {
// @ts-ignore: FUCKED UP
const list = [];
// @ts-ignore: FUCKED UP
function timeout(fun, time) {
list.push(setTimeout(fun, time));
}
const e = [new Promise<number>((res) => timeout(() => res(100), 100))];
const e2 = [
new Promise<number>((res) => timeout(() => res(100), 100)),
new Promise<number>((res) => timeout(() => res(200), 100)),
];
const e3 = [
new Promise<number>((res) => timeout(() => res(100), 100)),
new Promise<number>((res) => timeout(() => res(200), 100)),
new Promise<number>((res) => timeout(() => res(150), 100)),
];
const t = { test: 2 };
assertEquals(
await asyncEvery(e, async function (e) {
assertEquals(this, t);
return (await e) === 100;
}, t),
true,
);
assertEquals(
await asyncEvery(e2, async function (e) {
assertEquals(this, t);
const d = await e;
return d === 100;
}, t),
false,
);
assertEquals(
await asyncEvery(e3, async function (e) {
assertEquals(this, t);
const d = await e;
if (d === 150) throw Error("Should exited.");
return d === 100;
}, t),
false,
);
// @ts-ignore: FUCKED UP
for (const i of list) {
clearTimeout(i);
}
});
Deno.test("map_test", () => {
const arr = { 0: 1, 1: 2, length: 2 };
const d = { d: 1 };
const re = map(arr, function (n) {
assertEquals(this, d);
return n + 2;
}, d);
assertEquals(re, [3, 4]);
});
Deno.test("toJSON_test", () => {
assertEquals(toJSON({ a: 3n }), '{"a":3}');
assertEquals(
toJSON([1099511627776n, { a: 45n }]),
'[1099511627776,{"a":45}]',
);
assertEquals(toJSON([9007199254740992n]), '["9007199254740992"]');
});
Deno.test("compareNum_test", () => {
assertEquals([3, 4n, 1, 2n, 11n, 5n].sort(compareNum), [
1,
2n,
3,
4n,
5n,
11n,
]);
});
Deno.test("parseBigInt_test", () => {
assertEquals(parseBigInt("1.jpg"), 1);
assertEquals(parseBigInt("9007199254740992.png"), 9007199254740992n);
assertEquals(parseBigInt("+3_3"), 3);
assertEquals(parseBigInt("+9007199254740992"), 9007199254740992n);
assertEquals(parseBigInt("-9007199254740992.3"), -9007199254740992n);
assertEquals(parseBigInt("--9007199254740992"), NaN);
assertEquals(parseBigInt("--3"), NaN);
});
Deno.test("getHashFromUrl_test", () => {
assertEquals(
getHashFromUrl(
"https://tgozcdr.gmhufzljgpgr.hath.network/h/5ad98a65fedb4bbe374200127bf59122d790b01a-561706-2400-3455-jpg/keystamp=1717653000-d7f10a5a37;fileindex=148307765;xres=2400/3.jpg",
),
"5ad98a65fedb4bbe374200127bf59122d790b01a",
);
assertEquals(
getHashFromUrl(
"https://shioxowwjqzzmcmcjtcc.hath.network/om/148307800/fdf168f74a048b4d8ac00fd8679ebd37495d4225-12779336-5709-4063-png/eda2e519f8a8d08af91c9e69aca958bad4287fc7-442362-2400-1708-jpg/2400/jxd25rgrogit4j1d6bp/13_14.jpg",
),
"eda2e519f8a8d08af91c9e69aca958bad4287fc7",
);
assertEquals(
getHashFromUrl(
"https://wodmrfmkcovmstjztpdj.hath.network/om/148307765/b17d7a49b90ec7f8e586dd9847d22027b0d56d6b-5720430-2822-4063-png/x/0/erpgfojcsyhu9v1d6b2/3.png",
),
"b17d7a49b90ec7f8e586dd9847d22027b0d56d6b",
);
assertEquals(
getHashFromUrl(
"https://asmrsqqftvbbqqaocggo.hath.network/om/98603353/1f48eb617ea10c4b48ef0485d43f339985119d7f-13101261-4893-3446-png/4038f0c078b59736aeaa5b1ce38a44b701238363-330283-2400-1690-jpg/2400/g101vh47s3q5al1d6ax/18.jpg",
),
"4038f0c078b59736aeaa5b1ce38a44b701238363",
);
});