86 lines
2.7 KiB
JavaScript
86 lines
2.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)"));
|
|
self.onmessage({
|
|
data: {
|
|
id: 4,
|
|
mode: "metadata",
|
|
buffer: fixture(16, {
|
|
charset: "ISO_IR 192",
|
|
patientBytes: [0xe3, 0xcd, 0xe3, 0xcf],
|
|
}),
|
|
},
|
|
});
|
|
assert.equal(replies[3]?.encoding, "windows-1256");
|
|
assert.ok(replies[3]?.metadata.some((entry) => entry.value === "محمد"));
|
|
self.onmessage({
|
|
data: {
|
|
id: 5,
|
|
mode: "metadata",
|
|
buffer: fixture(16, {
|
|
charset: "WINDOWS-1256",
|
|
patientBytes: Array.from(new TextEncoder().encode("Иван^Петров")),
|
|
}),
|
|
},
|
|
});
|
|
assert.equal(replies[4]?.encoding, "utf-8");
|
|
assert.ok(replies[4]?.metadata.some((entry) => entry.value === "Иван Петров"));
|
|
self.onmessage({
|
|
data: { id: 6, mode: "volumeSlice", buffer: fixture(16, { slice: 2 }) },
|
|
});
|
|
assert.equal(replies[5]?.error, undefined);
|
|
assert.deepEqual(Array.from(replies[5].slice.position), [0, 0, 2]);
|
|
assert.deepEqual(Array.from(replies[5].slice.orientation), [1, 0, 0, 0, 1, 0]);
|
|
assert.equal(replies[5].slice.pixels[0], -2048);
|
|
console.log(
|
|
"Production DICOM worker: decode, metadata, text encodings and volume slices passed"
|
|
);
|