-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
101 lines (81 loc) · 2.58 KB
/
test.js
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
import test from "node:test";
import { readFileSync } from "node:fs";
import { pack } from "./src/packer.js";
import { instantiate } from "./zopfli/index.js";
import { inflateRawSync } from "node:zlib";
import { binEscape, ISO_TO_UTF } from "./src/escaper.js";
import { DECODER_BIG, DECODER_SMALL } from "./src/templates.js";
let testScript = readFileSync("./index.js", "utf8");
let testFile = readFileSync("./package-lock.json");
function compareArrays(a, b) {
if (b.length != a.length) {
throw new Error("Different length of arrays.");
}
for (let i = 0; i < a.length; i++) {
if (b[i] !== a[i]) {
throw new Error(`Different byte at ${i}. ${b[i]} != ${a[i]}.`);
}
}
}
test("Escape and unescape", () => {
let testData = [];
for (let i = 0; i < 10; i++) {
for (let j = 0; j < 256; j++) {
testData.push(j);
}
// this should trigger byte swapping
testData.push(92); // "\\"
testData.push(92); // "\\"
testData.push(92); // "\\"
testData.push(13); // "\r"
testData.push(13); // "\r"
testData.push(92); // "\\"
testData.push(13); // "\r"
testData.push(13); // "\r"
testData.push(13); // "\r"
// and HTML comment closing tag
testData.push(45); // "-"
testData.push(45); // "-"
testData.push(62); // ">"
}
let testEscaped = binEscape(testData);
let testStr = "";
testEscaped.payload.forEach((b) => {
testStr += String.fromCharCode(ISO_TO_UTF[b] ?? b);
});
let smallDec = new Function(
"b",
"u",
DECODER_SMALL.replace("$PLACEHOLDER_MAPPING", "u")
);
let bigDec = new Function(
"b",
"u",
DECODER_BIG.replace("$PLACEHOLDER_MAPPING", "u")
);
let res;
res = smallDec(testStr, testEscaped.decodeMap);
compareArrays(testData, res);
res = bigDec(testStr, testEscaped.decodeMap);
compareArrays(testData, res);
testEscaped.payload.charCodeAt = (i) => testEscaped.payload[i];
res = smallDec(testEscaped.payload, testEscaped.swapMap);
compareArrays(testData, res);
res = bigDec(testEscaped.payload, testEscaped.swapMap);
compareArrays(testData, res);
});
test("Compress and decompress", async () => {
let ect = await instantiate();
let compressed = ect(testFile, 2);
let decompressed = inflateRawSync(compressed);
compareArrays(testFile, decompressed);
});
// Unfortunately I don't know how to test that generated HTML file really
// works properly. But at least we can test that function does not throw.
test("Package HTML", async () => {
return pack({
script: testScript,
compressionlevel: 2,
files: { "data.json": { content: testFile } },
});
});