33
44import { describe , it , expect } from "vitest" ;
55import { BundleParser } from "./bundle-parser.js" ;
6- import type { Bundle } from "./types.js" ;
6+ import { hasId } from "./types.js" ;
7+ import type { Bundle , WithId } from "./types.js" ;
78
89describe ( "BundleParser" , ( ) => {
910 const patient1 = { resourceType : "Patient" as const , id : "1" , name : [ { family : "Smith" } ] } ;
@@ -22,14 +23,14 @@ describe("BundleParser", () => {
2223
2324 describe ( "getResourcesByType" , ( ) => {
2425 it ( "filters resources by resourceType" , ( ) => {
25- const patients = BundleParser . getResourcesByType < fhir4 . Patient > ( bundle , "Patient" ) ;
26+ const patients = BundleParser . getResourcesByType ( bundle , "Patient" ) ;
2627 expect ( patients ) . toHaveLength ( 2 ) ;
27- expect ( patients [ 0 ] . id ) . toBe ( "1" ) ;
28- expect ( patients [ 1 ] . id ) . toBe ( "2" ) ;
28+ expect ( patients [ 0 ] ? .id ) . toBe ( "1" ) ;
29+ expect ( patients [ 1 ] ? .id ) . toBe ( "2" ) ;
2930 } ) ;
3031
3132 it ( "returns empty array for non-matching type" , ( ) => {
32- const conditions = BundleParser . getResourcesByType < fhir4 . Condition > ( bundle , "Condition" ) ;
33+ const conditions = BundleParser . getResourcesByType ( bundle , "Condition" ) ;
3334 expect ( conditions ) . toEqual ( [ ] ) ;
3435 } ) ;
3536
@@ -46,11 +47,27 @@ describe("BundleParser", () => {
4647 } ;
4748 expect ( BundleParser . getResourcesByType ( noRes , "Patient" ) ) . toEqual ( [ ] ) ;
4849 } ) ;
50+
51+ it ( "keeps resources that have no id, and hasId drops them" , ( ) => {
52+ const posting : Bundle < fhir4 . Resource > = {
53+ resourceType : "Bundle" ,
54+ type : "transaction" ,
55+ entry : [
56+ { resource : { resourceType : "Patient" } as fhir4 . Resource , request : { method : "POST" , url : "Patient" } } ,
57+ { resource : patient1 as fhir4 . Resource } ,
58+ ] ,
59+ } ;
60+ expect ( BundleParser . getResourcesByType ( posting , "Patient" ) ) . toHaveLength ( 2 ) ;
61+
62+ const withIds : WithId < fhir4 . Patient > [ ] = BundleParser . getResourcesByType ( posting , "Patient" ) . filter ( hasId ) ;
63+ expect ( withIds ) . toHaveLength ( 1 ) ;
64+ expect ( withIds [ 0 ] ?. id ) . toBe ( "1" ) ;
65+ } ) ;
4966 } ) ;
5067
5168 describe ( "getFirstResourceByType" , ( ) => {
5269 it ( "returns first matching resource" , ( ) => {
53- const first = BundleParser . getFirstResourceByType < fhir4 . Patient > ( bundle , "Patient" ) ;
70+ const first = BundleParser . getFirstResourceByType ( bundle , "Patient" ) ;
5471 expect ( first ?. id ) . toBe ( "1" ) ;
5572 } ) ;
5673
@@ -65,6 +82,17 @@ describe("BundleParser", () => {
6582 expect ( all ) . toHaveLength ( 3 ) ;
6683 } ) ;
6784
85+ it ( "preserves the bundle's element type" , ( ) => {
86+ const searchset : Bundle < WithId < fhir4 . Patient > > = {
87+ resourceType : "Bundle" ,
88+ type : "searchset" ,
89+ entry : [ { resource : patient1 } , { resource : patient2 } ] ,
90+ } ;
91+ // No cast: what the reader knew is what comes back.
92+ const patients : WithId < fhir4 . Patient > [ ] = BundleParser . getAllResources ( searchset ) ;
93+ expect ( patients . map ( p => p . id ) ) . toEqual ( [ "1" , "2" ] ) ;
94+ } ) ;
95+
6896 it ( "returns empty array for empty bundle" , ( ) => {
6997 const empty : Bundle < fhir4 . Resource > = { resourceType : "Bundle" , type : "searchset" } ;
7098 expect ( BundleParser . getAllResources ( empty ) ) . toEqual ( [ ] ) ;
@@ -96,26 +124,23 @@ describe("BundleParser", () => {
96124 } ;
97125
98126 it ( "resolves by exact fullUrl match" , ( ) => {
99- const result = BundleParser . resolveReference < fhir4 . Patient > (
127+ const result = BundleParser . resolveReference (
100128 { reference : "https://fhir.example.com/Patient/1" } ,
101129 bundleWithFullUrls ,
102130 ) ;
103131 expect ( result ?. id ) . toBe ( "1" ) ;
104132 } ) ;
105133
106134 it ( "resolves by urn:uuid fullUrl match" , ( ) => {
107- const result = BundleParser . resolveReference < fhir4 . Patient > (
135+ const result = BundleParser . resolveReference (
108136 { reference : "urn:uuid:abc-123" } ,
109137 bundleWithFullUrls ,
110138 ) ;
111139 expect ( result ?. id ) . toBe ( "2" ) ;
112140 } ) ;
113141
114142 it ( "resolves by ResourceType/id relative reference" , ( ) => {
115- const result = BundleParser . resolveReference < fhir4 . Patient > (
116- { reference : "Patient/1" } ,
117- bundleWithFullUrls ,
118- ) ;
143+ const result = BundleParser . resolveReference ( { reference : "Patient/1" } , bundleWithFullUrls ) ;
119144 expect ( result ?. id ) . toBe ( "1" ) ;
120145 } ) ;
121146
@@ -127,10 +152,7 @@ describe("BundleParser", () => {
127152 { fullUrl : "https://fhir.example.com/Patient/99" , resource : { resourceType : "Patient" } as fhir4 . Resource } ,
128153 ] ,
129154 } ;
130- const result = BundleParser . resolveReference < fhir4 . Patient > (
131- { reference : "Patient/99" } ,
132- bundleNoId ,
133- ) ;
155+ const result = BundleParser . resolveReference ( { reference : "Patient/99" } , bundleNoId ) ;
134156 expect ( result ?. resourceType ) . toBe ( "Patient" ) ;
135157 } ) ;
136158
@@ -151,5 +173,47 @@ describe("BundleParser", () => {
151173 bundleWithFullUrls ,
152174 ) ) . toBeUndefined ( ) ;
153175 } ) ;
176+
177+ it ( "preserves the bundle's element type" , ( ) => {
178+ const searchset : Bundle < WithId < fhir4 . Patient > > = {
179+ resourceType : "Bundle" ,
180+ type : "searchset" ,
181+ entry : [ { fullUrl : "https://fhir.example.com/Patient/1" , resource : patient1 } ] ,
182+ } ;
183+ const resolved : WithId < fhir4 . Patient > | undefined = BundleParser . resolveReference (
184+ { reference : "Patient/1" } ,
185+ searchset ,
186+ ) ;
187+ expect ( resolved ?. id ) . toBe ( "1" ) ;
188+ } ) ;
189+ } ) ;
190+
191+ describe ( "resolveReferenceOfType" , ( ) => {
192+ const bundleWithFullUrls : Bundle < fhir4 . Resource > = {
193+ resourceType : "Bundle" ,
194+ type : "searchset" ,
195+ entry : [
196+ { fullUrl : "https://fhir.example.com/Patient/1" , resource : patient1 as fhir4 . Resource } ,
197+ { fullUrl : "https://fhir.example.com/Observation/obs-1" , resource : observation as fhir4 . Resource } ,
198+ ] ,
199+ } ;
200+
201+ it ( "resolves a reference of the requested type" , ( ) => {
202+ const result = BundleParser . resolveReferenceOfType (
203+ { reference : "Patient/1" } ,
204+ bundleWithFullUrls ,
205+ "Patient" ,
206+ ) ;
207+ expect ( result ?. id ) . toBe ( "1" ) ;
208+ } ) ;
209+
210+ it ( "returns undefined when the reference resolves to another type" , ( ) => {
211+ const result = BundleParser . resolveReferenceOfType (
212+ { reference : "https://fhir.example.com/Observation/obs-1" } ,
213+ bundleWithFullUrls ,
214+ "Patient" ,
215+ ) ;
216+ expect ( result ) . toBeUndefined ( ) ;
217+ } ) ;
154218 } ) ;
155219} ) ;
0 commit comments