@dcmjs-org/fhir 1.0.0-beta.3 → 1.0.0-beta.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,313 +0,0 @@
1
- // packages/fhir/test/toFhir.test.js
2
- //
3
- // @dcmjs-org/fhir — the FHIR sink. Parses the committed sample-dicom.dcm
4
- // fixture with @dcmjs-org/parser, naturalizes it, and asserts the FHIR
5
- // output against the fixture's known ground truth (MR study, patient
6
- // "Fall 3", PatientID 11791306742903).
7
- //
8
- // The v2 original read test/sample-dicom.dcm through the dcmjs umbrella;
9
- // here the fixture comes from packages/fixtures/dicom (same bytes, but
10
- // its upstream provenance is still being traced) and the parse goes
11
- // through the event-stream reader so the package tests stay off
12
- // @dcmjs-org/legacy. The facade half of the v2 "dcmjs.fhir umbrella
13
- // namespace" tests (an ArrayBuffer straight to FHIR) landed with the
14
- // event-stream slice as DicomEventStream.fromPart10(bytes).toFhir() —
15
- // see test/eventStream/fhirPdfEventStream.test.js at the repo root.
16
-
17
- import fs from "fs";
18
- import path from "path";
19
-
20
- import { fromPart10, NaturalizedListener } from "@dcmjs-org/parser";
21
- import {
22
- toFhir,
23
- toBundle,
24
- patientFromDataset,
25
- imagingStudyFromDatasets,
26
- parsePersonName,
27
- dicomDateTimeToIso,
28
- sexToGender
29
- } from "../src/index.js";
30
-
31
- const FIXTURE = path.join(
32
- __dirname,
33
- "..",
34
- "..",
35
- "fixtures",
36
- "dicom",
37
- "sample-dicom.dcm"
38
- );
39
- const STUDY_UID = "1.2.276.0.50.192168001092.11156604.14547392.4";
40
-
41
- async function loadNaturalizedFixture() {
42
- const buffer = fs.readFileSync(FIXTURE);
43
- const arrayBuffer = buffer.buffer.slice(
44
- buffer.byteOffset,
45
- buffer.byteOffset + buffer.byteLength
46
- );
47
- const listener = new NaturalizedListener();
48
- await fromPart10(arrayBuffer, listener);
49
- return listener.result;
50
- }
51
-
52
- describe("@dcmjs-org/fhir helpers", () => {
53
- test("parsePersonName handles naturalized PN, raw strings, and empties", () => {
54
- expect(parsePersonName([{ Alphabetic: "Doe^John^Q^Dr^Jr" }])).toEqual({
55
- family: "Doe",
56
- given: "John",
57
- middle: "Q",
58
- prefix: "Dr",
59
- suffix: "Jr",
60
- text: "Doe John Q Dr Jr"
61
- });
62
- expect(parsePersonName("Fall 3")).toMatchObject({
63
- family: "Fall 3",
64
- text: "Fall 3"
65
- });
66
- expect(parsePersonName(null)).toBeNull();
67
- expect(parsePersonName([])).toBeNull();
68
- });
69
-
70
- test("dicomDateTimeToIso writes the time only with a timezone", () => {
71
- expect(dicomDateTimeToIso("20010101")).toBe("2001-01-01");
72
- expect(dicomDateTimeToIso("20010101", "102231")).toBe("2001-01-01");
73
- expect(dicomDateTimeToIso("20010101", "102231", "+0130")).toBe(
74
- "2001-01-01T10:22:31+01:30"
75
- );
76
- expect(dicomDateTimeToIso(null)).toBeNull();
77
- // Legacy ACR-NEMA forms, and values that are not a date
78
- expect(dicomDateTimeToIso("1980.05.17", "10:22:31", "+0100")).toBe(
79
- "1980-05-17T10:22:31+01:00"
80
- );
81
- expect(dicomDateTimeToIso("1980-5-17")).toBeNull();
82
- expect(dicomDateTimeToIso("20010101", "10h22", "+0100")).toBe(
83
- "2001-01-01"
84
- );
85
- });
86
-
87
- test("sexToGender maps DICOM codes", () => {
88
- expect(sexToGender("M")).toBe("male");
89
- expect(sexToGender("F")).toBe("female");
90
- expect(sexToGender("O")).toBe("other");
91
- expect(sexToGender(undefined)).toBe("unknown");
92
- });
93
- });
94
-
95
- describe("@dcmjs-org/fhir sink", () => {
96
- let dataset;
97
- beforeAll(async () => {
98
- dataset = await loadNaturalizedFixture();
99
- });
100
-
101
- test("patientFromDataset builds a FHIR Patient from the patient module", () => {
102
- const patient = patientFromDataset(dataset);
103
-
104
- expect(patient.resourceType).toBe("Patient");
105
- expect(patient.identifier[0].value).toBe("11791306742903");
106
- expect(patient.identifier[0].type.coding[0].code).toBe("MR");
107
- expect(patient.name[0].family).toBe("Fall 3");
108
- expect(["male", "female", "other", "unknown"]).toContain(
109
- patient.gender
110
- );
111
- // No deployment-specific fields ever, and no sex extension that
112
- // DICOM does not record (FHIR-USR-3)
113
- expect(patient.id).toBeUndefined();
114
- expect(patient.meta).toBeUndefined();
115
- expect(patient.extension).toBeUndefined();
116
- });
117
-
118
- test("toFhir returns { patient, imagingStudy } with DICOM identity intact", () => {
119
- const { patient, imagingStudy } = toFhir(dataset);
120
-
121
- expect(patient.resourceType).toBe("Patient");
122
- expect(imagingStudy.resourceType).toBe("ImagingStudy");
123
- expect(imagingStudy.status).toBe("available");
124
- expect(imagingStudy.identifier[0]).toEqual({
125
- use: "official",
126
- system: "urn:dicom:uid",
127
- value: `urn:oid:${STUDY_UID}`
128
- });
129
- expect(imagingStudy.numberOfSeries).toBe(1);
130
- expect(imagingStudy.numberOfInstances).toBe(1);
131
- expect(imagingStudy.modality[0].code).toBe("MR");
132
- // The fixture has no TimezoneOffsetFromUTC, so only the date.
133
- expect(imagingStudy.started).toBe("2001-01-01");
134
-
135
- const series = imagingStudy.series[0];
136
- expect(series.modality.code).toBe("MR");
137
- expect(series.modality.display).toBe("Magnetic Resonance");
138
- expect(series.number).toBe(2101);
139
- expect(series.instance[0].number).toBe(10);
140
- expect(series.instance[0].sopClass.system).toBe("urn:ietf:rfc:3986");
141
- expect(series.instance[0].sopClass.code).toMatch(/^urn:oid:/);
142
- });
143
-
144
- test("without options.subject, the ImagingStudy references the returned patient", () => {
145
- const { imagingStudy } = toFhir(dataset);
146
- expect(imagingStudy.subject).toEqual({ reference: "./patient" });
147
- });
148
-
149
- test("options.subject passes through as ImagingStudy.subject", () => {
150
- const { imagingStudy } = toFhir(dataset, {
151
- subject: { reference: "Patient/abc", display: "Fall 3" }
152
- });
153
- expect(imagingStudy.subject).toEqual({
154
- reference: "Patient/abc",
155
- display: "Fall 3"
156
- });
157
- });
158
-
159
- test("imagingStudyFromDatasets aggregates multiple instances into one study", () => {
160
- const study = imagingStudyFromDatasets([dataset, dataset]);
161
- expect(study.numberOfSeries).toBe(1);
162
- expect(study.numberOfInstances).toBe(2);
163
- expect(study.series[0].instance).toHaveLength(2);
164
- });
165
-
166
- test("imagingStudyFromDatasets skips a dataset of another study", () => {
167
- const other = {
168
- ...dataset,
169
- StudyInstanceUID: "9.9.9",
170
- SeriesInstanceUID: "9.9.9.1",
171
- SOPInstanceUID: "9.9.9.1.1"
172
- };
173
- const study = imagingStudyFromDatasets([dataset, other]);
174
- expect(study.identifier[0].value).toBe(`urn:oid:${STUDY_UID}`);
175
- expect(study.numberOfSeries).toBe(1);
176
- expect(study.numberOfInstances).toBe(1);
177
- });
178
-
179
- test("toBundle emits one Patient and one aggregated ImagingStudy", () => {
180
- const bundle = toBundle([dataset, dataset]);
181
-
182
- expect(bundle.resourceType).toBe("Bundle");
183
- expect(bundle.type).toBe("collection");
184
- expect(bundle.total).toBe(2);
185
-
186
- const types = bundle.entry.map(entry => entry.resource.resourceType);
187
- expect(types).toEqual(["Patient", "ImagingStudy"]);
188
- expect(bundle.entry[1].resource.numberOfInstances).toBe(2);
189
- // Relative fullUrls link the ImagingStudy to the Patient entry.
190
- expect(bundle.entry.map(entry => entry.fullUrl)).toEqual([
191
- "./entry/0",
192
- "./entry/1"
193
- ]);
194
- expect(bundle.entry[1].resource.subject).toEqual({
195
- reference: "./entry/0"
196
- });
197
- });
198
-
199
- test("series and instances hold only valid FHIR values", () => {
200
- const base = {
201
- StudyInstanceUID: "1.2.3",
202
- SOPClassUID: "1.2.840.10008.5.1.4.1.1.4"
203
- };
204
- const study = imagingStudyFromDatasets([
205
- {
206
- ...base,
207
- SeriesInstanceUID: "1.2.3.1",
208
- SOPInstanceUID: "1.2.3.1.1",
209
- Modality: "MR",
210
- SeriesNumber: -1,
211
- InstanceNumber: 2.5,
212
- BodyPartExamined: "CHEST",
213
- Laterality: "L"
214
- },
215
- // No Modality: series.modality is 1..1, so the dataset is skipped.
216
- {
217
- ...base,
218
- SeriesInstanceUID: "1.2.3.2",
219
- SOPInstanceUID: "1.2.3.2.1"
220
- }
221
- ]);
222
- expect(study.numberOfSeries).toBe(1);
223
- const [series] = study.series;
224
- expect(series.number).toBeUndefined();
225
- expect(series.instance[0].number).toBeUndefined();
226
- expect(series.bodySite).toEqual({ display: "CHEST" });
227
- expect(series.laterality).toEqual({
228
- system: "http://snomed.info/sct",
229
- code: "7771000",
230
- display: "Left"
231
- });
232
- });
233
-
234
- test("unsupported fhirVersion throws (strict-out)", () => {
235
- expect(() => toFhir(dataset, { fhirVersion: "R5" })).toThrow(
236
- /unsupported fhirVersion/
237
- );
238
- expect(() => toBundle([dataset], { fhirVersion: "STU3" })).toThrow(
239
- /unsupported fhirVersion/
240
- );
241
- });
242
-
243
- test("empty input degrades gracefully", () => {
244
- expect(patientFromDataset({})).toBeNull();
245
- expect(imagingStudyFromDatasets([])).toBeNull();
246
- const { patient, imagingStudy } = toFhir({});
247
- expect(patient).toBeNull();
248
- expect(imagingStudy).toBeNull();
249
- });
250
-
251
- // Regression for review finding 17: a dataset with no SOPClassUID used
252
- // to fabricate the CT Image Storage UID (1.2.840.10008.5.1.4.1.1.2),
253
- // asserting an unknown instance is a CT image. FHIR R4B makes
254
- // instance.sopClass 1..1, so the instance is skipped with a warning
255
- // instead of emitted with invented identity.
256
- test("missing SOPClassUID skips the instance, never fabricates CT (finding 17)", () => {
257
- const study = imagingStudyFromDatasets([
258
- {
259
- StudyInstanceUID: "1.2.3",
260
- SeriesInstanceUID: "1.2.3.1",
261
- SOPInstanceUID: "1.2.3.4",
262
- Modality: "OT"
263
- }
264
- ]);
265
- const json = JSON.stringify(study);
266
- expect(json).not.toContain("1.2.840.10008.5.1.4.1.1.2");
267
- expect(study.numberOfInstances).toBe(0);
268
- expect(study.series[0].instance).toBeUndefined();
269
- });
270
-
271
- // Regression for review finding 18: a dataset with no SeriesInstanceUID
272
- // used to produce series.uid: null (invalid — ImagingStudy.series.uid is
273
- // 1..1 in R4B), and every such dataset merged under one "unknown" key.
274
- // Those datasets are now skipped with a warning, consistent with 17.
275
- test("missing SeriesInstanceUID skips the dataset, no null-uid merge (finding 18)", () => {
276
- const study = imagingStudyFromDatasets([
277
- {
278
- StudyInstanceUID: "1.2.3",
279
- SeriesInstanceUID: "1.2.3.1",
280
- SOPClassUID: "1.2.840.10008.5.1.4.1.1.4",
281
- SOPInstanceUID: "1.2.3.4",
282
- Modality: "MR"
283
- },
284
- // Two datasets from genuinely different series, both without
285
- // SeriesInstanceUID — these must not merge into one series.
286
- {
287
- StudyInstanceUID: "1.2.3",
288
- SOPClassUID: "1.2.840.10008.5.1.4.1.1.4",
289
- SOPInstanceUID: "1.2.3.5"
290
- },
291
- {
292
- StudyInstanceUID: "1.2.3",
293
- SOPClassUID: "1.2.840.10008.5.1.4.1.1.4",
294
- SOPInstanceUID: "1.2.3.6"
295
- }
296
- ]);
297
- expect(study.numberOfSeries).toBe(1);
298
- expect(study.series).toHaveLength(1);
299
- expect(study.series[0].uid).toBe("1.2.3.1");
300
- expect(study.series.every(entry => entry.uid !== null)).toBe(true);
301
- expect(study.numberOfInstances).toBe(1);
302
- });
303
- });
304
-
305
- // The v2 suite ends with a "dcmjs.fhir umbrella namespace" block
306
- // (dcmjs.fhir.fromPart10 straight to FHIR, and the namespace re-exports).
307
- // The facade integration landed with the event-stream slice: the
308
- // bytes-to-FHIR composition is DicomEventStream.fromPart10(x).toFhir(),
309
- // pinned in test/eventStream/fhirPdfEventStream.test.js at the repo root.
310
- // The dcmjs.fhir NAMESPACE itself (the spread of this package's exports
311
- // plus the fromPart10 convenience onto the dcmjs default export) is the
312
- // step-7 wrapper package's surface — the two namespace tests return when
313
- // that wrapper assembles the scoped packages.