webpack/test/BinaryMiddleware.unittest.js

135 lines
3.2 KiB
JavaScript
Raw Normal View History

"use strict";
2020-12-14 06:16:38 +08:00
const BinaryMiddleware = require("../lib/serialization/BinaryMiddleware");
2020-12-14 23:43:29 +08:00
const SerializerMiddleware = require("../lib/serialization/SerializerMiddleware");
2020-12-14 06:16:38 +08:00
const cont = (base, count) => {
const result = [];
for (let i = 0; i < count; i++) {
result.push(base[i % base.length]);
}
return result;
};
2020-12-14 23:43:29 +08:00
const mw = new BinaryMiddleware();
const other = { other: true };
const resolveLazy = (item) => {
2020-12-14 23:43:29 +08:00
if (SerializerMiddleware.isLazy(item)) {
const data = item();
if (Array.isArray(data)) return { resolvesTo: data.map(resolveLazy) };
return { resolvesTo: resolveLazy(data) };
}
return item;
};
2020-12-14 06:16:38 +08:00
describe("BinaryMiddleware", () => {
2020-12-14 06:20:05 +08:00
const items = [
true,
false,
null,
"",
"hi",
"hi".repeat(200),
"😀",
"😀".repeat(200),
Buffer.from("hello"),
1,
11,
0x100,
-1,
-11,
-0x100,
2020-12-14 23:43:29 +08:00
-1.25,
2021-02-03 18:52:32 +08:00
SerializerMiddleware.createLazy([5], other)
];
const itemsWithLazy = [
...items,
2020-12-14 23:43:29 +08:00
SerializerMiddleware.createLazy(
[SerializerMiddleware.createLazy([5], other)],
mw
),
SerializerMiddleware.createLazy(
[
1,
SerializerMiddleware.createLazy([2], mw),
SerializerMiddleware.createLazy([5], other),
4
],
mw
)
2020-12-14 06:20:05 +08:00
];
2025-07-03 17:06:45 +08:00
itemsWithLazy.push(SerializerMiddleware.createLazy([...itemsWithLazy], mw));
2021-02-03 18:52:32 +08:00
itemsWithLazy.push(
2025-07-03 17:06:45 +08:00
SerializerMiddleware.createLazy([...itemsWithLazy], other)
2021-02-03 18:52:32 +08:00
);
2020-12-14 23:43:29 +08:00
items.push(undefined);
2020-12-14 06:20:05 +08:00
2020-12-14 06:16:38 +08:00
const cases = [
...itemsWithLazy.map((item) => [item]),
2020-12-14 23:43:29 +08:00
[(true, true)],
2020-12-14 06:16:38 +08:00
[false, true],
[true, false],
[false, false],
[false, false, false],
[false, true, false, true],
[true, true, true],
[false, false, false],
cont([false, true, false, true], 5),
cont([true], 5),
cont([false], 5),
cont([false, true, false, true], 6),
cont([true], 6),
cont([false], 6),
cont([false, true, false, true], 7),
cont([false, true, false, true], 8),
cont([false, true, false, true], 9),
cont([false, true, false, true], 132),
cont([false, true, false, true], 133),
cont([false, true, false, true], 134),
cont([false, true, false, true], 135),
2021-02-03 18:52:32 +08:00
cont([false, true, false, true], 10000),
2020-12-14 06:20:05 +08:00
cont([true], 135),
[null],
[null, null],
[null, null, null],
cont([null], 4),
cont([null], 100),
cont([null], 300),
cont([-20], 20),
cont([400], 20),
cont([5.5], 20)
2020-12-14 06:16:38 +08:00
];
2021-02-03 18:52:32 +08:00
for (const c of [1, 100]) {
for (const caseData of cases) {
for (const prepend of items) {
for (const append of items) {
if (c > 1 && append !== undefined) continue;
2024-07-31 04:09:42 +08:00
const data = [prepend, ...caseData, append].filter(
(x) => x !== undefined
2021-02-03 18:52:32 +08:00
);
if (data.length * c > 200000) continue;
if (data.length === 0) continue;
let key = JSON.stringify(data.map(resolveLazy));
if (key.length > 100) {
2024-07-31 10:39:30 +08:00
key = `${key.slice(0, 50)} ... ${key.slice(-50)}`;
}
2021-02-03 18:52:32 +08:00
it(`should serialize ${c} x ${key} (${data.length}) correctly`, () => {
// process.stderr.write(
// `${c} x ${key.slice(0, 20)} (${data.length})\n`
// );
const realData = cont(data, data.length * c);
const serialized = mw.serialize(realData, {});
const newData = mw.deserialize(serialized, {});
expect(newData.map(resolveLazy)).toEqual(realData.map(resolveLazy));
});
}
2020-12-14 06:16:38 +08:00
}
}
}
});