Files
filebrowser/frontend/scripts/check-dicom-worker.mjs
T

55 lines
1.7 KiB
JavaScript

import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import vm from "node:vm";
import { fixture } from "../src/utils/__tests__/fixtures/dicom.ts";
// Exercise the shipped bundle without Node's require or module globals.
// Source-module tests alone miss broken CommonJS imports in browser workers.
const assets = path.resolve(import.meta.dirname, "../dist/assets");
const name = fs
.readdirSync(assets)
.find((n) => /^dicom\.worker-.*\.js$/.test(n));
assert.ok(name, "Production DICOM worker is missing");
const replies = [];
const self = { postMessage: (data) => replies.push(data) };
vm.runInNewContext(fs.readFileSync(path.join(assets, name), "utf8"), {
self,
console,
TextEncoder,
TextDecoder,
ArrayBuffer,
DataView,
Uint8Array,
Uint8ClampedArray,
Uint16Array,
Int16Array,
Int32Array,
Uint32Array,
Float32Array,
Float64Array,
setTimeout,
clearTimeout,
});
self.onmessage({ data: { id: 1, buffer: fixture() } });
assert.equal(replies[0]?.error, undefined);
assert.equal(replies[0]?.frames, 2);
assert.equal(replies[0]?.cols, 2);
assert.equal(replies[0]?.rows, 2);
assert.equal(replies[0]?.rgba[0], 0);
assert.equal(replies[0]?.rgba[12], 255);
self.onmessage({ data: { id: 2, frame: 1 } });
assert.equal(replies[1]?.error, undefined);
assert.equal(replies[1]?.rgba[0], 255);
self.onmessage({ data: { id: 3, mode: "metadata", buffer: fixture(32) } });
assert.equal(replies[2]?.error, undefined);
assert.ok(
replies[2]?.metadata.some(
(entry) => entry.tag === "(0010,0010)" && entry.value === "Example Patient"
)
);
assert.ok(!replies[2]?.metadata.some((entry) => entry.tag === "(7FE0,0010)"));
console.log(
"Production DICOM worker: decode, frame switching and metadata passed"
);