|
| 1 | +/** |
| 2 | + * Type discriminator slicing demo — ExampleTypedBundle profile. |
| 3 | + * |
| 4 | + * The profile slices Bundle.entry[] by resource type: |
| 5 | + * - PatientEntry (min: 1, max: 1) — entry where resource is Patient |
| 6 | + * - OrganizationEntry (min: 0, max: *) — entry where resource is Organization |
| 7 | + */ |
| 8 | + |
| 9 | +import { describe, expect, test } from "bun:test"; |
| 10 | +import { ExampleTypedBundleProfile } from "./fhir-types/example-folder-structures/profiles/Bundle_ExampleTypedBundle"; |
| 11 | +import type { Organization } from "./fhir-types/hl7-fhir-r4-core/Organization"; |
| 12 | +import type { Patient } from "./fhir-types/hl7-fhir-r4-core/Patient"; |
| 13 | + |
| 14 | +const createBundle = () => ExampleTypedBundleProfile.create({ type: "collection" }); |
| 15 | + |
| 16 | +const smithPatient: Patient = { resourceType: "Patient", name: [{ family: "Smith" }] }; |
| 17 | +const jonesPatient: Patient = { resourceType: "Patient", name: [{ family: "Jones" }] }; |
| 18 | +const activePatient: Patient = { resourceType: "Patient", active: true }; |
| 19 | +const acmeOrg: Organization = { resourceType: "Organization", name: "Acme Corp" }; |
| 20 | +const clinicOrg: Organization = { resourceType: "Organization", name: "Clinic" }; |
| 21 | + |
| 22 | +describe("type-discriminated bundle slices", () => { |
| 23 | + test("create() auto-populates a PatientEntry stub (min: 1)", () => { |
| 24 | + const bundle = createBundle(); |
| 25 | + const entry = bundle.toResource().entry; |
| 26 | + expect(entry).toHaveLength(1); |
| 27 | + expect(entry![0]!.resource).toEqual({ resourceType: "Patient" }); |
| 28 | + }); |
| 29 | + |
| 30 | + test("setPatientEntry inserts a typed patient entry", () => { |
| 31 | + const bundle = createBundle(); |
| 32 | + bundle.setPatientEntry({ resource: smithPatient }); |
| 33 | + |
| 34 | + const entry = bundle.getPatientEntry()!; |
| 35 | + expect(entry.resource).toEqual(smithPatient); |
| 36 | + }); |
| 37 | + |
| 38 | + test("setPatientEntry replaces existing patient entry (no duplicates)", () => { |
| 39 | + const bundle = createBundle(); |
| 40 | + bundle.setPatientEntry({ resource: smithPatient }); |
| 41 | + bundle.setPatientEntry({ resource: jonesPatient }); |
| 42 | + |
| 43 | + const patients = bundle.toResource().entry!.filter((e) => e.resource?.resourceType === "Patient"); |
| 44 | + expect(patients).toHaveLength(1); |
| 45 | + expect(bundle.getPatientEntry()!.resource).toEqual(jonesPatient); |
| 46 | + }); |
| 47 | + |
| 48 | + test("setOrganizationEntry adds an organization entry", () => { |
| 49 | + const bundle = createBundle(); |
| 50 | + bundle.setOrganizationEntry({ resource: acmeOrg }); |
| 51 | + |
| 52 | + expect(bundle.getOrganizationEntry()!.resource).toEqual(acmeOrg); |
| 53 | + }); |
| 54 | + |
| 55 | + test("getPatientEntry('flat') returns the entry as-is (no keys stripped)", () => { |
| 56 | + const bundle = createBundle(); |
| 57 | + bundle.setPatientEntry({ fullUrl: "urn:uuid:patient-1", resource: activePatient }); |
| 58 | + |
| 59 | + const flat = bundle.getPatientEntry("flat")!; |
| 60 | + expect(flat.fullUrl).toBe("urn:uuid:patient-1"); |
| 61 | + expect(flat.resource).toEqual(activePatient); |
| 62 | + }); |
| 63 | + |
| 64 | + test("validate() checks PatientEntry cardinality", () => { |
| 65 | + const bundle = ExampleTypedBundleProfile.apply({ |
| 66 | + resourceType: "Bundle", |
| 67 | + type: "collection", |
| 68 | + }); |
| 69 | + const { errors } = bundle.validate(); |
| 70 | + expect(errors).toEqual(["ExampleTypedBundle.entry: slice 'PatientEntry' requires at least 1 item(s), found 0"]); |
| 71 | + }); |
| 72 | + |
| 73 | + test("fluent chaining across slice setters", () => { |
| 74 | + const bundle = createBundle() |
| 75 | + .setPatientEntry({ resource: activePatient }) |
| 76 | + .setOrganizationEntry({ resource: clinicOrg }); |
| 77 | + |
| 78 | + expect(bundle.getPatientEntry()!.resource).toEqual(activePatient); |
| 79 | + expect(bundle.getOrganizationEntry()!.resource).toEqual(clinicOrg); |
| 80 | + expect(bundle.toResource().entry).toHaveLength(2); |
| 81 | + }); |
| 82 | + |
| 83 | + test("set/get PatientEntry with full BundleEntry input", () => { |
| 84 | + const bundle = createBundle(); |
| 85 | + bundle.setPatientEntry({ fullUrl: "urn:uuid:p1", resource: smithPatient }); |
| 86 | + |
| 87 | + const raw = bundle.getPatientEntry("raw")!; |
| 88 | + expect(raw.fullUrl).toBe("urn:uuid:p1"); |
| 89 | + expect(raw.resource).toEqual(smithPatient); |
| 90 | + |
| 91 | + const flat = bundle.getPatientEntry("flat")!; |
| 92 | + expect(flat.fullUrl).toBe("urn:uuid:p1"); |
| 93 | + expect(flat.resource).toEqual(smithPatient); |
| 94 | + }); |
| 95 | + |
| 96 | + test("set/get OrganizationEntry with full BundleEntry input", () => { |
| 97 | + const bundle = createBundle(); |
| 98 | + bundle.setOrganizationEntry({ fullUrl: "urn:uuid:o1", resource: acmeOrg }); |
| 99 | + |
| 100 | + const raw = bundle.getOrganizationEntry("raw")!; |
| 101 | + expect(raw.fullUrl).toBe("urn:uuid:o1"); |
| 102 | + expect(raw.resource).toEqual(acmeOrg); |
| 103 | + |
| 104 | + const flat = bundle.getOrganizationEntry("flat")!; |
| 105 | + expect(flat.fullUrl).toBe("urn:uuid:o1"); |
| 106 | + expect(flat.resource).toEqual(acmeOrg); |
| 107 | + }); |
| 108 | +}); |
0 commit comments