-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprofile-bp.test.ts
More file actions
115 lines (99 loc) · 4.93 KB
/
profile-bp.test.ts
File metadata and controls
115 lines (99 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* FHIR R4 Blood Pressure Profile Class API Tests
*/
import { describe, expect, test } from "bun:test";
import type { Observation } from "./fhir-types/hl7-fhir-r4-core/Observation";
import { observation_bpProfile as bpProfile } from "./fhir-types/hl7-fhir-r4-core/profiles/Observation_observation_bp";
describe("demo: create a blood pressure observation", () => {
test("build a valid BP resource step by step", () => {
// Create a new BP profile — auto-sets code, category, and component stubs
const profile = bpProfile.create({
status: "final",
subject: { reference: "Patient/pt-1" },
});
// Not yet valid: effective[x] and component valueQuantity are required
expect(profile.validate().errors).toEqual([
"observation-bp: at least one of effectiveDateTime, effectivePeriod is required",
"observation-bp.component[SystolicBP].valueQuantity is required",
"observation-bp.component[DiastolicBP].valueQuantity is required",
]);
// Fill in the remaining required fields
profile
.setEffectiveDateTime("2024-06-15")
.setSystolicBP({ value: 120, unit: "mmHg", system: "http://unitsofmeasure.org", code: "mm[Hg]" })
.setDiastolicBP({ value: 80, unit: "mmHg", system: "http://unitsofmeasure.org", code: "mm[Hg]" });
// Now the resource is fully valid
expect(profile.validate().errors).toEqual([]);
expect(profile.toResource()).toMatchSnapshot();
});
});
describe("demo: apply BP profile to an existing Observation", () => {
test("wrap a plain Observation and populate profiled fields", () => {
// Start with an existing Observation — e.g. received from another system
const obs: Observation = {
resourceType: "Observation",
status: "preliminary",
code: { coding: [{ code: "85354-9", system: "http://loinc.org" }] },
subject: { reference: "Patient/pt-1" },
};
// apply() adds meta.profile, sets fixed values (code), and auto-populates required slices
const profile = bpProfile.apply(obs);
// Not yet valid: effective[x] and component valueQuantity are required
expect(profile.validate().errors).toEqual([
"observation-bp: at least one of effectiveDateTime, effectivePeriod is required",
"observation-bp.component[SystolicBP].valueQuantity is required",
"observation-bp.component[DiastolicBP].valueQuantity is required",
]);
// The profile mutates the original resource — no copy is made
expect(profile.toResource()).toBe(obs);
expect(obs.meta?.profile).toContain("http://hl7.org/fhir/StructureDefinition/bp");
// Fill in the remaining fields via fluent setters
profile
.setStatus("final")
.setEffectiveDateTime("2024-06-15")
.setSystolicBP({ value: 120, unit: "mmHg" })
.setDiastolicBP({ value: 80, unit: "mmHg" });
expect(profile.validate().errors).toEqual([]);
});
});
// Note: from() demo is omitted for R4 BP because the R4 component slice discriminator stores
// code.coding as a plain object (not an array) — a known upstream issue in the R4 FHIR schema.
// See the US Core BP test for a from() demo that works with correct array-shaped discriminators.
describe("component slice replacement", () => {
test("setSystolicBP replaces existing stub, component stays at 2", () => {
const profile = bpProfile.create({ status: "final", subject: { reference: "Patient/pt-1" } });
expect(profile.toResource().component).toHaveLength(2);
profile.setSystolicBP({ value: 130, unit: "mmHg" });
expect(profile.toResource().component).toHaveLength(2);
expect(profile.getSystolicBP()!.value).toBe(130);
});
});
describe("category auto-population", () => {
test("custom category is preserved, VSCat is appended", () => {
const profile = bpProfile.create({
status: "final",
subject: { reference: "Patient/pt-1" },
category: [{ text: "My Category" }],
});
const obs = profile.toResource();
expect(obs.category).toHaveLength(2);
expect(obs.category![0]!.text).toBe("My Category");
expect(obs.category![1]!.coding).toEqual([
{ code: "vital-signs", system: "http://terminology.hl7.org/CodeSystem/observation-category" },
]);
});
test("existing VSCat is not duplicated", () => {
const profile = bpProfile.create({
status: "final",
subject: { reference: "Patient/pt-1" },
category: [
{
coding: [
{ code: "vital-signs", system: "http://terminology.hl7.org/CodeSystem/observation-category" },
],
},
],
});
expect(profile.toResource().category).toHaveLength(1);
});
});