|
| 1 | +import { describe, expect, it } from "bun:test"; |
| 2 | +import type { Observation } from "./fhir-types/hl7-fhir-r4-core/Observation"; |
| 3 | +import type { Patient } from "./fhir-types/hl7-fhir-r4-core/Patient"; |
| 4 | +import { |
| 5 | + USCoreBloodPressureProfileProfile as usBpProfile, |
| 6 | + USCorePatientProfileProfile as usPatientProfile, |
| 7 | +} from "./fhir-types/hl7-fhir-us-core/profiles"; |
| 8 | + |
| 9 | +const createPatient = (): Patient => ({ resourceType: "Patient" }); |
| 10 | +const createObservation = (): Observation => ({ resourceType: "Observation", status: "final", code: {} }); |
| 11 | + |
| 12 | +describe("Profile Getter Methods", () => { |
| 13 | + describe("Extension getters", () => { |
| 14 | + it("returns simplified object via getRace()", () => { |
| 15 | + const profile = new usPatientProfile(createPatient()); |
| 16 | + profile.setRace({ |
| 17 | + ombCategory: { system: "urn:oid:2.16.840.1.113883.6.238", code: "2106-3", display: "White" }, |
| 18 | + text: "White", |
| 19 | + }); |
| 20 | + |
| 21 | + const result = profile.getRace(); |
| 22 | + expect(result).toBeDefined(); |
| 23 | + expect(result?.ombCategory?.code).toBe("2106-3"); |
| 24 | + expect(result?.text).toBe("White"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("returns raw Extension via getRaceExtension()", () => { |
| 28 | + const profile = new usPatientProfile(createPatient()); |
| 29 | + profile.setRace({ |
| 30 | + ombCategory: { system: "urn:oid:2.16.840.1.113883.6.238", code: "2106-3", display: "White" }, |
| 31 | + text: "White", |
| 32 | + }); |
| 33 | + |
| 34 | + const raw = profile.getRaceExtension(); |
| 35 | + expect(raw).toBeDefined(); |
| 36 | + expect(raw?.url).toBe("http://hl7.org/fhir/us/core/StructureDefinition/us-core-race"); |
| 37 | + expect(raw?.extension).toBeArray(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("returns undefined when extension not set", () => { |
| 41 | + const profile = new usPatientProfile(createPatient()); |
| 42 | + expect(profile.getRace()).toBeUndefined(); |
| 43 | + }); |
| 44 | + |
| 45 | + it("simple extension getter returns value directly", () => { |
| 46 | + const profile = new usPatientProfile(createPatient()); |
| 47 | + profile.setSex({ system: "http://hl7.org/fhir/administrative-gender", code: "male" }); |
| 48 | + |
| 49 | + const result = profile.getSex(); |
| 50 | + expect(result).toBeDefined(); |
| 51 | + expect(result?.code).toBe("male"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("simple extension getSexExtension() returns raw Extension", () => { |
| 55 | + const profile = new usPatientProfile(createPatient()); |
| 56 | + profile.setSex({ system: "http://hl7.org/fhir/administrative-gender", code: "male" }); |
| 57 | + |
| 58 | + const raw = profile.getSexExtension(); |
| 59 | + expect(raw).toBeDefined(); |
| 60 | + expect(raw?.url).toBe("http://hl7.org/fhir/us/core/StructureDefinition/us-core-individual-sex"); |
| 61 | + expect(raw?.valueCoding?.code).toBe("male"); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("Slice getters", () => { |
| 66 | + it("returns simplified slice without discriminator via getSystolic()", () => { |
| 67 | + const profile = new usBpProfile(createObservation()); |
| 68 | + profile.setSystolic({ |
| 69 | + valueQuantity: { value: 120, unit: "mmHg", system: "http://unitsofmeasure.org", code: "mm[Hg]" }, |
| 70 | + }); |
| 71 | + |
| 72 | + const result = profile.getSystolic(); |
| 73 | + expect(result).toBeDefined(); |
| 74 | + expect(result?.valueQuantity?.value).toBe(120); |
| 75 | + // Should NOT include the code discriminator in simplified form |
| 76 | + expect((result as Record<string, unknown>)?.code).toBeUndefined(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("returns full slice with discriminator via getSystolicRaw()", () => { |
| 80 | + const profile = new usBpProfile(createObservation()); |
| 81 | + profile.setSystolic({ |
| 82 | + valueQuantity: { value: 120, unit: "mmHg", system: "http://unitsofmeasure.org", code: "mm[Hg]" }, |
| 83 | + }); |
| 84 | + |
| 85 | + const raw = profile.getSystolicRaw(); |
| 86 | + expect(raw).toBeDefined(); |
| 87 | + expect(raw?.valueQuantity?.value).toBe(120); |
| 88 | + // Raw should include the code discriminator |
| 89 | + expect(raw?.code?.coding?.[0]?.code).toBe("8480-6"); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns undefined when slice not set", () => { |
| 93 | + const profile = new usBpProfile(createObservation()); |
| 94 | + expect(profile.getSystolic()).toBeUndefined(); |
| 95 | + expect(profile.getDiastolic()).toBeUndefined(); |
| 96 | + }); |
| 97 | + |
| 98 | + it("can get multiple slices independently", () => { |
| 99 | + const profile = new usBpProfile(createObservation()); |
| 100 | + profile.setSystolic({ valueQuantity: { value: 120, unit: "mmHg" } }); |
| 101 | + profile.setDiastolic({ valueQuantity: { value: 80, unit: "mmHg" } }); |
| 102 | + |
| 103 | + const systolic = profile.getSystolic(); |
| 104 | + const diastolic = profile.getDiastolic(); |
| 105 | + |
| 106 | + expect(systolic?.valueQuantity?.value).toBe(120); |
| 107 | + expect(diastolic?.valueQuantity?.value).toBe(80); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe("Round-trip: set and get", () => { |
| 112 | + it("can set and get extension values", () => { |
| 113 | + const profile = new usPatientProfile(createPatient()); |
| 114 | + |
| 115 | + // Set values |
| 116 | + profile.setRace({ |
| 117 | + ombCategory: { code: "2106-3", display: "White" }, |
| 118 | + detailed: [{ code: "2108-9", display: "European" }], |
| 119 | + text: "White European", |
| 120 | + }); |
| 121 | + |
| 122 | + // Get and verify |
| 123 | + const race = profile.getRace(); |
| 124 | + expect(race?.text).toBe("White European"); |
| 125 | + |
| 126 | + // Verify the resource has the extension |
| 127 | + const resource = profile.toResource(); |
| 128 | + expect(resource.extension).toBeArray(); |
| 129 | + expect( |
| 130 | + resource.extension?.some( |
| 131 | + (e) => e.url === "http://hl7.org/fhir/us/core/StructureDefinition/us-core-race", |
| 132 | + ), |
| 133 | + ).toBe(true); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments