55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
|
|
//
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
import assert from "node:assert/strict";
|
|
import { readFile } from "node:fs/promises";
|
|
import test from "node:test";
|
|
import vm from "node:vm";
|
|
|
|
test("decode-history worker decodes CBOR and batches messages", async () => {
|
|
const messages = [];
|
|
const worker = {
|
|
onmessage: null,
|
|
postMessage(message) { messages.push(message); },
|
|
};
|
|
const payload = Uint8Array.from([0xa1, 0x63, 0x61, 0x69, 0x73, 0x83, 0x01, 0x02, 0x03]);
|
|
const context = vm.createContext({
|
|
self: worker,
|
|
TextDecoder,
|
|
Uint8Array,
|
|
DataView,
|
|
ArrayBuffer,
|
|
Number,
|
|
Math,
|
|
String,
|
|
Error,
|
|
fetch: async () => ({
|
|
ok: true,
|
|
status: 200,
|
|
arrayBuffer: async () => payload.buffer,
|
|
}),
|
|
});
|
|
const source = await readFile(
|
|
new URL("../../assets/web/generated/decode-history-worker.js", import.meta.url),
|
|
"utf8",
|
|
);
|
|
new vm.Script(source).runInContext(context);
|
|
worker.onmessage({ data: { type: "fetch-history", url: "/decode/history", batchLimit: 2 } });
|
|
await new Promise((resolve) => { setTimeout(resolve, 0); });
|
|
|
|
assert.deepEqual(
|
|
messages.map(({ type, phase, total, processed }) => ({ type, phase, total, processed })),
|
|
[
|
|
{ type: "status", phase: "fetching", total: undefined, processed: undefined },
|
|
{ type: "status", phase: "decoding", total: undefined, processed: undefined },
|
|
{ type: "start", phase: undefined, total: 3, processed: undefined },
|
|
{ type: "group", phase: undefined, total: 3, processed: 2 },
|
|
{ type: "group", phase: undefined, total: 3, processed: 3 },
|
|
{ type: "done", phase: undefined, total: 3, processed: undefined },
|
|
],
|
|
);
|
|
assert.deepEqual(Array.from(messages[3].messages), [1, 2]);
|
|
assert.deepEqual(Array.from(messages[4].messages), [3]);
|
|
});
|