Skip to main content

haste_testscript_runner/
lib.rs

1use base64::{Engine as _, engine::general_purpose::STANDARD as BASE64};
2use haste_fhir_client::{
3    FHIRClient,
4    http::{
5        FHIRHttpExchange, HeaderMap, HeaderName, HeaderValue, HttpResponseMetadata, Method,
6        StatusCode, WithRequestHeaders, header,
7    },
8    request::{
9        DeleteRequest, DeleteResponse, FHIRBatchRequest, FHIRConditionalUpdateRequest,
10        FHIRCreateRequest, FHIRDeleteInstanceRequest, FHIRDeleteSystemRequest,
11        FHIRDeleteTypeRequest, FHIRHistoryInstanceRequest, FHIRHistorySystemRequest,
12        FHIRHistoryTypeRequest, FHIRInvokeInstanceRequest, FHIRInvokeSystemRequest,
13        FHIRInvokeTypeRequest, FHIRPatchRequest, FHIRReadRequest, FHIRRequest, FHIRResponse,
14        FHIRSearchTypeRequest, FHIRTransactionRequest, FHIRUpdateInstanceRequest,
15        FHIRVersionReadRequest, HistoryRequest, HistoryResponse, InvocationRequest, InvokeResponse,
16        Operation, SearchRequest, SearchResponse, UpdateRequest,
17    },
18    url::ParsedParameters,
19};
20use haste_fhir_model::r4::generated::{
21    resources::{
22        Resource, ResourceType, TestReport, TestReportSetup, TestReportSetupAction,
23        TestReportSetupActionAssert, TestReportSetupActionOperation, TestReportTeardown,
24        TestReportTeardownAction, TestReportTest, TestReportTestAction, TestScript,
25        TestScriptFixture, TestScriptSetup, TestScriptSetupAction, TestScriptSetupActionAssert,
26        TestScriptSetupActionOperation, TestScriptTeardown, TestScriptTeardownAction,
27        TestScriptTest, TestScriptTestAction, TestScriptVariable,
28    },
29    terminology::{
30        AssertDirectionCodes, AssertOperatorCodes, BoundCode, BundleType, IssueType,
31        ReportActionResultCodes, ReportResultCodes, ReportStatusCodes, TestscriptOperationCodes,
32    },
33    types::{ExtensionValueTypeChoice, FHIRId, FHIRMarkdown, FHIRString, Reference},
34};
35use haste_fhir_operation_error::OperationOutcomeError;
36use haste_pointer::{Key, TypedPointer};
37use haste_reflect::MetaValue;
38use regex::Regex;
39use std::{
40    any::Any,
41    collections::HashMap,
42    sync::{Arc, LazyLock},
43    time::Duration,
44};
45use tokio::sync::Mutex;
46
47use crate::conversion::ConvertedValue;
48
49mod conversion;
50
51#[derive(Debug)]
52pub enum TestScriptError {
53    ExecutionError(String),
54    ValidationError(String),
55    FixtureNotFound,
56    InvalidFixture,
57    OperationError(OperationOutcomeError),
58}
59
60#[derive(Debug, Clone)]
61enum Response {
62    Interaction(Box<FHIRResponse>),
63    OperationError(Arc<OperationOutcomeError>),
64    /// The response to a request that is not a FHIR interaction (an operation
65    /// with a `url` and no `type`): its body, when that is a FHIR resource.
66    Http(Option<Box<Resource>>),
67}
68
69/// A response as fixtures, variables and assertions see it: the FHIR
70/// response, plus the HTTP status and headers it came with (`None` when the
71/// request failed before a response arrived).
72#[derive(Debug, Clone)]
73struct RecordedResponse {
74    response: Response,
75    http: Option<HttpResponseMetadata>,
76}
77
78#[derive(Debug)]
79enum Fixtures {
80    Resource(Resource),
81    Request(FHIRRequest),
82    Response(RecordedResponse),
83}
84
85// Internal structure to hold current test result and testing fixtures.
86struct TestState {
87    fp_engine: haste_fhirpath::FPEngine,
88    fixtures: HashMap<String, Fixtures>,
89    latest_request: Option<FHIRRequest>,
90    latest_response: Option<RecordedResponse>,
91    result: BoundCode<ReportResultCodes>,
92    /// Writes not yet confirmed visible to search. All are waited for, since a
93    /// search (a chained one especially) may filter on any of them.
94    unindexed_writes: Vec<IndexedWrite>,
95}
96
97/// A write that searches must see before they run. Search indexing is
98/// asynchronous, so a search sent straight after a write can miss it. The
99/// worker indexes a tenant's writes in order, so once the latest write is
100/// visible every earlier one is too.
101#[derive(Debug, Clone)]
102struct IndexedWrite {
103    resource_type: ResourceType,
104    id: String,
105    /// The version search should return; `None` for a delete, which search
106    /// should no longer return at all.
107    version_id: Option<String>,
108}
109
110impl TestState {
111    fn new() -> Self {
112        TestState {
113            fp_engine: haste_fhirpath::FPEngine::new(),
114            fixtures: HashMap::new(),
115            latest_request: None,
116            latest_response: None,
117            result: ReportResultCodes::pending(),
118            unindexed_writes: Vec::new(),
119        }
120    }
121    fn resolve_fixture<'a>(
122        &'a self,
123        fixture_id: &str,
124    ) -> Result<&'a dyn MetaValue, TestScriptError> {
125        let fixture = self
126            .fixtures
127            .get(fixture_id)
128            .ok_or(TestScriptError::FixtureNotFound)?;
129
130        match fixture {
131            Fixtures::Resource(res) => Ok(res),
132            Fixtures::Request(req) => {
133                request_to_meta_value(req).ok_or_else(|| TestScriptError::InvalidFixture)
134            }
135            Fixtures::Response(recorded) => response_to_meta_value(&recorded.response)
136                .ok_or_else(|| TestScriptError::InvalidFixture),
137        }
138    }
139
140    /// The response fixture `fixture_id` names, or the latest response.
141    fn resolve_response(
142        &self,
143        fixture_id: Option<&str>,
144    ) -> Result<&RecordedResponse, TestScriptError> {
145        match fixture_id {
146            Some(fixture_id) => match self.fixtures.get(fixture_id) {
147                Some(Fixtures::Response(recorded)) => Ok(recorded),
148                Some(_) => Err(TestScriptError::ExecutionError(format!(
149                    "Fixture '{fixture_id}' is not a response."
150                ))),
151                None => Err(TestScriptError::FixtureNotFound),
152            },
153            None => self.latest_response.as_ref().ok_or_else(|| {
154                TestScriptError::ExecutionError("No response to evaluate yet.".to_string())
155            }),
156        }
157    }
158}
159
160struct TestResult<T> {
161    pub state: Arc<Mutex<TestState>>,
162    pub value: T,
163}
164
165fn response_to_meta_value(response: &Response) -> Option<&dyn MetaValue> {
166    match response {
167        Response::Interaction(fhir_response) => match &**fhir_response {
168            FHIRResponse::Create(res) => Some(&res.resource),
169            FHIRResponse::Read(res) => Some(&res.resource),
170            FHIRResponse::VersionRead(res) => Some(&res.resource),
171            FHIRResponse::Update(res) => Some(&res.resource),
172            FHIRResponse::Patch(res) => Some(&res.resource),
173            FHIRResponse::Batch(res) => Some(&res.resource),
174            FHIRResponse::Transaction(res) => Some(&res.resource),
175
176            FHIRResponse::Capabilities(res) => Some(&res.capabilities),
177            FHIRResponse::Search(res) => match res {
178                SearchResponse::Type(res) => Some(&res.bundle),
179                SearchResponse::System(res) => Some(&res.bundle),
180            },
181            FHIRResponse::History(res) => match res {
182                HistoryResponse::Instance(res) => Some(&res.bundle),
183                HistoryResponse::Type(res) => Some(&res.bundle),
184                HistoryResponse::System(res) => Some(&res.bundle),
185            },
186            FHIRResponse::Invoke(res) => match res {
187                InvokeResponse::Instance(res) => Some(&res.resource),
188                InvokeResponse::Type(res) => Some(&res.resource),
189                InvokeResponse::System(res) => Some(&res.resource),
190            },
191
192            FHIRResponse::Delete(_) | FHIRResponse::Accepted(_) => None,
193        },
194        Response::OperationError(op_error) => {
195            let outcome = op_error.outcome();
196            Some(outcome)
197        }
198        Response::Http(resource) => resource
199            .as_deref()
200            .map(|resource| resource as &dyn MetaValue),
201    }
202}
203
204fn request_to_meta_value(request: &FHIRRequest) -> Option<&dyn MetaValue> {
205    match request {
206        FHIRRequest::Create(req) => Some(&req.resource),
207
208        FHIRRequest::Update(update_request) => match update_request {
209            UpdateRequest::Conditional(req) => Some(&req.resource),
210            UpdateRequest::Instance(req) => Some(&req.resource),
211        },
212
213        FHIRRequest::Batch(req) => Some(&req.resource),
214        FHIRRequest::Transaction(req) => Some(&req.resource),
215        FHIRRequest::Invocation(req) => match req {
216            haste_fhir_client::request::InvocationRequest::Instance(req) => Some(&req.parameters),
217            haste_fhir_client::request::InvocationRequest::Type(req) => Some(&req.parameters),
218            haste_fhir_client::request::InvocationRequest::System(req) => Some(&req.parameters),
219        },
220        FHIRRequest::Read(_)
221        | FHIRRequest::VersionRead(_)
222        | FHIRRequest::Compartment(_)
223        | FHIRRequest::Patch(_)
224        | FHIRRequest::Delete(_)
225        | FHIRRequest::Capabilities
226        | FHIRRequest::Search(_)
227        | FHIRRequest::History(_) => None,
228    }
229}
230
231fn associate_request_response_variables(
232    state: &mut TestState,
233    operation: &TestScriptSetupActionOperation,
234    request: Option<FHIRRequest>,
235    response: RecordedResponse,
236) {
237    if let Some(request) = request.as_ref()
238        && let Some(request_var) = operation
239            .requestId
240            .as_ref()
241            .and_then(|id| id.value.as_ref())
242    {
243        // Associate request variable in state
244        state
245            .fixtures
246            .insert(request_var.clone(), Fixtures::Request(request.clone()));
247    }
248
249    if let Some(response_var) = operation
250        .responseId
251        .as_ref()
252        .and_then(|id| id.value.as_ref())
253    {
254        // Associate response variable in state
255        state
256            .fixtures
257            .insert(response_var.clone(), Fixtures::Response(response.clone()));
258    }
259
260    state.latest_request = request;
261    state.latest_response = Some(response);
262}
263
264/// Derive the resource type from operation or from the metavalue if not present on operation.
265fn derive_resource_type(
266    operation: &TestScriptSetupActionOperation,
267    target: Option<&dyn MetaValue>,
268    path: &str,
269) -> Result<ResourceType, TestScriptError> {
270    if let Some(operation_resource_type) = operation.resource.as_ref() {
271        let string_type = operation_resource_type.as_str();
272        ResourceType::try_from(string_type.unwrap_or_default()).map_err(|_| {
273            TestScriptError::ExecutionError(format!(
274                "Unsupported resource type '{operation_resource_type:?}' for operation at '{path}'."
275            ))
276        })
277    } else if let Some(target) = target {
278        ResourceType::try_from(target.fhir_type()).map_err(|_| {
279            TestScriptError::ExecutionError(format!(
280                "Unsupported resource type '{}' for operation at '{path}'.",
281                target.fhir_type()
282            ))
283        })
284    } else {
285        Err(TestScriptError::ExecutionError(format!(
286            "Failed to derive resource type for operation at '{path}'.",
287        )))
288    }
289}
290
291static EXPRESSION_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\$\{([^}]*)\}").unwrap());
292
293async fn get_variable(
294    state: &TestState,
295    variables: &[TestScriptVariable],
296    variable_id: &str,
297) -> Result<ConvertedValue, TestScriptError> {
298    let Some(variable) = variables
299        .iter()
300        .find(|v| v.name.value.as_deref() == Some(variable_id))
301    else {
302        return Err(TestScriptError::ExecutionError(format!(
303            "Variable with id '{variable_id}' not found."
304        )));
305    };
306
307    if let Some(field) = variable
308        .headerField
309        .as_ref()
310        .and_then(|field| field.value.as_deref())
311    {
312        let source_id = variable
313            .sourceId
314            .as_ref()
315            .and_then(|id| id.value.as_deref());
316        let recorded = state.resolve_response(source_id)?;
317
318        return header_value(recorded, field)
319            .map(ConvertedValue::String)
320            .ok_or_else(|| {
321                TestScriptError::ExecutionError(format!(
322                    "Variable '{variable_id}': the response has no '{field}' header."
323                ))
324            });
325    }
326
327    if let Some(expression) = variable
328        .expression
329        .as_ref()
330        .and_then(|exp| exp.value.as_ref())
331    {
332        let values =
333            if let Some(source_id) = variable.sourceId.as_ref().and_then(|id| id.value.as_ref()) {
334                let source = state.resolve_fixture(source_id)?;
335                vec![source]
336            } else {
337                vec![]
338            };
339
340        let eval_result = state
341            .fp_engine
342            .evaluate(expression, values)
343            .await
344            .map_err(|e| {
345                TestScriptError::ExecutionError(format!(
346                    "Failed to evaluate FHIRPath expression for variable '{variable_id}': {e}"
347                ))
348            })?;
349
350        let converted_values = eval_result
351            .iter()
352            .map(conversion::convert_meta_value)
353            .collect::<Vec<_>>();
354
355        if converted_values.len() == 1 {
356            Ok(converted_values.into_iter().next().unwrap())
357        } else {
358            Err(TestScriptError::ExecutionError(format!(
359                "Variable '{variable_id}' evaluation returned multiple values; only single value supported.",
360            )))
361        }
362    } else {
363        Err(TestScriptError::ExecutionError(format!(
364            "Only support variable with expression or headerField for variable id '{variable_id}'.",
365        )))
366    }
367}
368
369async fn evaluate_variable(
370    state: &TestState,
371    pointer: TypedPointer<TestScript, TestScript>,
372    value: &str,
373) -> Result<String, TestScriptError> {
374    let mut result = value.to_string();
375    let variable_pointer =
376        pointer.descend::<Vec<TestScriptVariable>>(&Key::Field("variable".to_string()));
377    let default_variables = vec![];
378
379    let variables = if let Some(pointer) = variable_pointer.as_ref() {
380        pointer.value().unwrap_or(&default_variables)
381    } else {
382        &default_variables
383    };
384
385    for reg_match in EXPRESSION_REGEX.captures_iter(value) {
386        let full_match = reg_match.get(0).map_or("", |m| m.as_str());
387        let Some(variable_id) = reg_match.get(1).map(|m| m.as_str()) else {
388            return Err(TestScriptError::ExecutionError(format!(
389                "Invalid variable expression in '{value}'."
390            )));
391        };
392
393        let variable = get_variable(state, variables, variable_id).await?;
394        result = result.replace(full_match, variable.to_string().as_str());
395    }
396
397    Ok(result)
398}
399
400/// What an operation sends: a FHIR interaction, or a plain HTTP request.
401enum OperationRequest {
402    Interaction(Box<FHIRRequest>),
403    Http { method: Method, url: String },
404}
405
406async fn testscript_operation_to_request(
407    state: &TestState,
408    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
409) -> Result<OperationRequest, TestScriptError> {
410    let operation = get_operation(pointer)?;
411
412    // An operation with a `url` and no `type` is a plain HTTP request to that
413    // (variable-evaluated) URL by its `method`: how a `TestScript` reaches what
414    // is not a FHIR interaction, such as an asynchronous request's status URL.
415    if get_operation_type(operation).is_none()
416        && let Some(url) = operation.url.as_ref().and_then(|url| url.value.as_deref())
417    {
418        // The method codes are the HTTP methods in lower case.
419        let method = operation
420            .method
421            .as_ref()
422            .and_then(BoundCode::as_str)
423            .unwrap_or("get");
424        let method = Method::from_bytes(method.to_ascii_uppercase().as_bytes()).map_err(|_| {
425            TestScriptError::ExecutionError(format!(
426                "Unsupported method '{method}' for operation at '{}'.",
427                pointer.path()
428            ))
429        })?;
430
431        return Ok(OperationRequest::Http {
432            method,
433            url: evaluate_variable(state, pointer.root(), url).await?,
434        });
435    }
436
437    testscript_operation_to_fhir_request(state, pointer)
438        .await
439        .map(|request| OperationRequest::Interaction(Box::new(request)))
440}
441
442async fn testscript_operation_to_fhir_request(
443    state: &TestState,
444    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
445) -> Result<FHIRRequest, TestScriptError> {
446    let operation = get_operation(pointer)?;
447    let op = get_operation_type(operation);
448
449    match op {
450        Some(op) if Some(op) == TestscriptOperationCodes::read().as_str() => {
451            read_request(state, pointer, operation)
452        }
453
454        Some(op) if Some(op) == TestscriptOperationCodes::vread().as_str() => {
455            version_read_request(state, pointer, operation)
456        }
457
458        Some(op) if Some(op) == TestscriptOperationCodes::search().as_str() => {
459            search_request(state, pointer, operation).await
460        }
461
462        Some(op) if Some(op) == TestscriptOperationCodes::history().as_str() => {
463            history_request(state, pointer, operation).await
464        }
465
466        Some(op) if Some(op) == TestscriptOperationCodes::transaction().as_str() => {
467            transaction_request(state, pointer, operation)
468        }
469
470        Some(op) if Some(op) == TestscriptOperationCodes::batch().as_str() => {
471            batch_request(state, pointer, operation)
472        }
473
474        Some(op) if Some(op) == TestscriptOperationCodes::create().as_str() => {
475            create_request(state, pointer, operation)
476        }
477
478        Some(op) if Some(op) == TestscriptOperationCodes::update().as_str() => {
479            update_request(state, pointer, operation)
480        }
481
482        Some(op) if Some(op) == TestscriptOperationCodes::update_create().as_str() => {
483            conditional_update_request(state, pointer, operation).await
484        }
485
486        Some(op) if Some(op) == TestscriptOperationCodes::patch().as_str() => {
487            patch_request(state, pointer, operation)
488        }
489
490        Some(op) if Some(op) == TestscriptOperationCodes::delete().as_str() => {
491            delete_request(state, pointer, operation)
492        }
493
494        Some(op)
495            if Some(op) == TestscriptOperationCodes::delete_cond_multiple().as_str()
496                || Some(op) == TestscriptOperationCodes::delete_cond_single().as_str() =>
497        {
498            conditional_delete_request(state, pointer, operation)
499        }
500
501        Some(op)
502            if Some(op) == TestscriptOperationCodes::capabilities().as_str()
503                || Some(op) == TestscriptOperationCodes::conforms().as_str() =>
504        {
505            Ok(FHIRRequest::Capabilities)
506        }
507
508        Some("invoke") => invoke_request(state, pointer, operation),
509
510        _ => Err(TestScriptError::ExecutionError(format!(
511            "Unsupported TestScript operation type: {op:?} at '{}'.",
512            pointer.path(),
513        ))),
514    }
515}
516
517fn read_request(
518    state: &TestState,
519    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
520    operation: &TestScriptSetupActionOperation,
521) -> Result<FHIRRequest, TestScriptError> {
522    let target_id = require_target_id(operation, pointer.path(), "Read")?;
523    let target = state.resolve_fixture(target_id)?;
524
525    Ok(FHIRRequest::Read(FHIRReadRequest {
526        resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
527        id: fixture_string_field(target, target_id, "id")?,
528    }))
529}
530
531fn version_read_request(
532    state: &TestState,
533    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
534    operation: &TestScriptSetupActionOperation,
535) -> Result<FHIRRequest, TestScriptError> {
536    let target_id = require_target_id(operation, pointer.path(), "Version Read")?;
537    let target = state.resolve_fixture(target_id)?;
538
539    Ok(FHIRRequest::VersionRead(FHIRVersionReadRequest {
540        resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
541        id: fixture_string_field(target, target_id, "id")?,
542        version_id: fixture_version_id(target, target_id)?.into(),
543    }))
544}
545
546async fn search_request(
547    state: &TestState,
548    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
549    operation: &TestScriptSetupActionOperation,
550) -> Result<FHIRRequest, TestScriptError> {
551    let query = operation
552        .params
553        .as_ref()
554        .and_then(|p| p.value.as_deref())
555        .unwrap_or_default();
556
557    let parameters =
558        parsed_parameters(state, pointer.root(), query, pointer.path(), "Search").await?;
559
560    if let Ok(resource_type) = derive_resource_type(operation, None, pointer.path()) {
561        Ok(FHIRRequest::Search(
562            haste_fhir_client::request::SearchRequest::Type(
563                haste_fhir_client::request::FHIRSearchTypeRequest {
564                    resource_type,
565                    parameters,
566                },
567            ),
568        ))
569    } else {
570        Ok(FHIRRequest::Search(
571            haste_fhir_client::request::SearchRequest::System(
572                haste_fhir_client::request::FHIRSearchSystemRequest { parameters },
573            ),
574        ))
575    }
576}
577
578async fn history_request(
579    state: &TestState,
580    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
581    operation: &TestScriptSetupActionOperation,
582) -> Result<FHIRRequest, TestScriptError> {
583    let parameters = parsed_parameters(
584        state,
585        pointer.root(),
586        operation
587            .params
588            .as_ref()
589            .and_then(|p| p.value.as_deref())
590            .unwrap_or_default(),
591        pointer.path(),
592        "History",
593    )
594    .await?;
595
596    if let Some(target_id) = operation
597        .targetId
598        .as_ref()
599        .and_then(|id| id.value.as_deref())
600    {
601        let target = state.resolve_fixture(target_id)?;
602
603        Ok(FHIRRequest::History(HistoryRequest::Instance(
604            FHIRHistoryInstanceRequest {
605                resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
606                id: fixture_string_field(target, target_id, "id")?,
607                parameters,
608            },
609        )))
610    } else if operation.resource.is_some() {
611        Ok(FHIRRequest::History(HistoryRequest::Type(
612            FHIRHistoryTypeRequest {
613                resource_type: derive_resource_type(operation, None, pointer.path())?,
614                parameters,
615            },
616        )))
617    } else {
618        Ok(FHIRRequest::History(HistoryRequest::System(
619            FHIRHistorySystemRequest { parameters },
620        )))
621    }
622}
623
624fn transaction_request(
625    state: &TestState,
626    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
627    operation: &TestScriptSetupActionOperation,
628) -> Result<FHIRRequest, TestScriptError> {
629    let source_id = require_source_id(operation, pointer.path(), "Transaction")?;
630    let source = state.resolve_fixture(source_id)?;
631    let resource = fixture_resource(source, source_id)?;
632
633    match resource {
634        Resource::Bundle(bundle) => {
635            if bundle.type_ != BundleType::transaction() {
636                return Err(TestScriptError::ExecutionError(format!(
637                    "Fixture must be a transaction bundle for transaction operations for sourceId '{source_id}'."
638                )));
639            }
640
641            Ok(FHIRRequest::Transaction(FHIRTransactionRequest {
642                resource: bundle,
643            }))
644        }
645        _ => Err(TestScriptError::ExecutionError(format!(
646            "Fixture '{source_id}' is not a transaction Bundle resource."
647        ))),
648    }
649}
650
651fn batch_request(
652    state: &TestState,
653    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
654    operation: &TestScriptSetupActionOperation,
655) -> Result<FHIRRequest, TestScriptError> {
656    let source_id = require_source_id(operation, pointer.path(), "Batch")?;
657    let source = state.resolve_fixture(source_id)?;
658    let resource = fixture_resource(source, source_id)?;
659
660    match resource {
661        Resource::Bundle(bundle) => {
662            if bundle.type_ != BundleType::batch() {
663                return Err(TestScriptError::ExecutionError(format!(
664                    "Fixture must be a batch bundle for batch operations for sourceId '{source_id}'."
665                )));
666            }
667
668            Ok(FHIRRequest::Batch(FHIRBatchRequest { resource: bundle }))
669        }
670        _ => Err(TestScriptError::ExecutionError(format!(
671            "Fixture '{source_id}' is not a batch Bundle resource."
672        ))),
673    }
674}
675
676fn create_request(
677    state: &TestState,
678    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
679    operation: &TestScriptSetupActionOperation,
680) -> Result<FHIRRequest, TestScriptError> {
681    let source_id = require_source_id(operation, pointer.path(), "Create")?;
682    let source = state.resolve_fixture(source_id)?;
683    let resource = fixture_resource(source, source_id)?;
684
685    Ok(FHIRRequest::Create(FHIRCreateRequest {
686        resource_type: derive_resource_type(operation, Some(source), pointer.path())?,
687        resource,
688    }))
689}
690
691fn update_request(
692    state: &TestState,
693    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
694    operation: &TestScriptSetupActionOperation,
695) -> Result<FHIRRequest, TestScriptError> {
696    let source_id = require_source_id(operation, pointer.path(), "Update")?;
697    let source = state.resolve_fixture(source_id)?;
698    let mut resource = fixture_resource(source, source_id)?;
699
700    let target_id = require_target_id(operation, pointer.path(), "Update")?;
701    let target = state.resolve_fixture(target_id)?;
702    let target_resource = fixture_resource(target, target_id)?;
703    let id = fixture_string_field(&target_resource, target_id, "id")?;
704
705    // A contained fixture's id is local to the TestScript. The update
706    // addresses the target, and the server refuses a body naming another id.
707    if let Some(body_id) = resource
708        .get_field_mut("id")
709        .and_then(|body_id| (body_id as &mut dyn Any).downcast_mut::<Option<String>>())
710    {
711        *body_id = Some(id.clone());
712    }
713
714    Ok(FHIRRequest::Update(UpdateRequest::Instance(
715        FHIRUpdateInstanceRequest {
716            resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
717            id,
718            resource,
719        },
720    )))
721}
722
723async fn conditional_update_request(
724    state: &TestState,
725    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
726    operation: &TestScriptSetupActionOperation,
727) -> Result<FHIRRequest, TestScriptError> {
728    let source_id = require_source_id(operation, pointer.path(), "UpdateCreate")?;
729    let source = state.resolve_fixture(source_id)?;
730    let resource = fixture_resource(source, source_id)?;
731
732    let parameters = parsed_parameters(
733        state,
734        pointer.root(),
735        operation
736            .params
737            .as_ref()
738            .and_then(|p| p.value.as_deref())
739            .unwrap_or_default(),
740        pointer.path(),
741        "UpdateCreate",
742    )
743    .await?;
744
745    Ok(FHIRRequest::Update(UpdateRequest::Conditional(
746        FHIRConditionalUpdateRequest {
747            resource_type: derive_resource_type(operation, Some(source), pointer.path())?,
748            parameters,
749            resource,
750        },
751    )))
752}
753
754fn delete_request(
755    state: &TestState,
756    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
757    operation: &TestScriptSetupActionOperation,
758) -> Result<FHIRRequest, TestScriptError> {
759    let target_id = require_target_id(operation, pointer.path(), "Delete")?;
760    let target = state.resolve_fixture(target_id)?;
761
762    Ok(FHIRRequest::Delete(DeleteRequest::Instance(
763        FHIRDeleteInstanceRequest {
764            resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
765            id: fixture_string_field(target, target_id, "id")?,
766        },
767    )))
768}
769
770fn conditional_delete_request(
771    _state: &TestState,
772    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
773    operation: &TestScriptSetupActionOperation,
774) -> Result<FHIRRequest, TestScriptError> {
775    let parameters = ParsedParameters::try_from(
776        operation
777            .params
778            .as_ref()
779            .and_then(|p| p.value.as_deref())
780            .unwrap_or_default(),
781    )
782    .map_err(|e| {
783        TestScriptError::ExecutionError(format!(
784            "Failed to parse parameters for conditional delete operation at '{}': {}",
785            pointer.path(),
786            e
787        ))
788    })?;
789
790    if operation.resource.is_some() {
791        Ok(FHIRRequest::Delete(DeleteRequest::Type(
792            FHIRDeleteTypeRequest {
793                resource_type: derive_resource_type(operation, None, pointer.path())?,
794                parameters,
795            },
796        )))
797    } else {
798        Ok(FHIRRequest::Delete(DeleteRequest::System(
799            FHIRDeleteSystemRequest { parameters },
800        )))
801    }
802}
803
804/// The patch source fixture must be a `Binary` resource carrying the JSON Patch
805/// document, base64-encoded in `data` with `contentType`
806/// `application/json-patch+json` — the standard way `TestScript` represents patch
807/// bodies, since there's no dedicated FHIR resource type for a JSON Patch document.
808fn patch_request(
809    state: &TestState,
810    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
811    operation: &TestScriptSetupActionOperation,
812) -> Result<FHIRRequest, TestScriptError> {
813    let target_id = require_target_id(operation, pointer.path(), "Patch")?;
814    let target = state.resolve_fixture(target_id)?;
815
816    let source_id = require_source_id(operation, pointer.path(), "Patch")?;
817    let source = state.resolve_fixture(source_id)?;
818    let Resource::Binary(binary) = fixture_resource(source, source_id)? else {
819        return Err(TestScriptError::ExecutionError(format!(
820            "Patch source fixture '{source_id}' must be a Binary resource containing the JSON Patch document."
821        )));
822    };
823
824    let content_type = binary.contentType.value.as_deref().unwrap_or_default();
825    if content_type != "application/json-patch+json" {
826        return Err(TestScriptError::ExecutionError(format!(
827            "Unsupported patch content type '{content_type}' for fixture '{source_id}'; only 'application/json-patch+json' is supported."
828        )));
829    }
830
831    let encoded = binary
832        .data
833        .as_ref()
834        .and_then(|d| d.value.as_deref())
835        .ok_or_else(|| {
836            TestScriptError::ExecutionError(format!(
837                "Patch source fixture '{source_id}' Binary resource has no 'data'."
838            ))
839        })?;
840
841    let decoded = BASE64.decode(encoded).map_err(|e| {
842        TestScriptError::ExecutionError(format!(
843            "Failed to base64-decode patch content for fixture '{source_id}': {e}"
844        ))
845    })?;
846
847    let patch: json_patch::Patch = serde_json::from_slice(&decoded).map_err(|e| {
848        TestScriptError::ExecutionError(format!(
849            "Failed to parse JSON Patch document for fixture '{source_id}': {e}"
850        ))
851    })?;
852
853    Ok(FHIRRequest::Patch(FHIRPatchRequest {
854        resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
855        id: fixture_string_field(target, target_id, "id")?,
856        patch,
857    }))
858}
859
860fn invoke_request(
861    state: &TestState,
862    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
863    operation: &TestScriptSetupActionOperation,
864) -> Result<FHIRRequest, TestScriptError> {
865    let op_code = operation
866        .url
867        .as_ref()
868        .and_then(|u| u.value.as_deref())
869        .ok_or_else(|| {
870            TestScriptError::ExecutionError(format!(
871                "Invoke operation requires url at '{}' which is used for the operation code.",
872                pointer.path()
873            ))
874        })?;
875
876    let fhir_operation = Operation::new(op_code);
877
878    let source_id = require_source_id(operation, pointer.path(), "Invoke")?;
879    let source = state.resolve_fixture(source_id)?;
880
881    let Resource::Parameters(parameters) = fixture_resource(source, source_id)? else {
882        return Err(TestScriptError::ExecutionError(format!(
883            "Source fixture '{source_id}' is not a Parameters resource."
884        )));
885    };
886
887    if let Some(target_id) = operation
888        .targetId
889        .as_ref()
890        .and_then(|id| id.value.as_deref())
891    {
892        let target = state.resolve_fixture(target_id)?;
893
894        Ok(FHIRRequest::Invocation(InvocationRequest::Instance(
895            FHIRInvokeInstanceRequest {
896                operation: fhir_operation,
897                resource_type: derive_resource_type(operation, Some(target), pointer.path())?,
898                id: fixture_string_field(target, target_id, "id")?,
899                parameters,
900            },
901        )))
902    } else if let Ok(resource_type) = derive_resource_type(operation, None, pointer.path()) {
903        Ok(FHIRRequest::Invocation(InvocationRequest::Type(
904            FHIRInvokeTypeRequest {
905                operation: fhir_operation,
906                resource_type,
907                parameters,
908            },
909        )))
910    } else {
911        Ok(FHIRRequest::Invocation(InvocationRequest::System(
912            FHIRInvokeSystemRequest {
913                operation: fhir_operation,
914                parameters,
915            },
916        )))
917    }
918}
919
920fn get_operation(
921    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
922) -> Result<&TestScriptSetupActionOperation, TestScriptError> {
923    pointer.value().ok_or_else(|| {
924        TestScriptError::ExecutionError(format!(
925            "Failed to retrieve TestScript operation at '{}'.",
926            pointer.path()
927        ))
928    })
929}
930
931fn get_operation_type(operation: &TestScriptSetupActionOperation) -> Option<&str> {
932    operation
933        .type_
934        .as_ref()
935        .and_then(|t| t.code.as_ref())
936        .and_then(|c| c.value.as_deref())
937}
938
939fn require_target_id<'a>(
940    operation: &'a TestScriptSetupActionOperation,
941    path: &str,
942    operation_name: &str,
943) -> Result<&'a str, TestScriptError> {
944    operation
945        .targetId
946        .as_ref()
947        .and_then(|id| id.value.as_deref())
948        .ok_or_else(|| {
949            TestScriptError::ExecutionError(format!(
950                "{operation_name} operation requires targetId at '{path}'."
951            ))
952        })
953}
954
955fn require_source_id<'a>(
956    operation: &'a TestScriptSetupActionOperation,
957    path: &str,
958    operation_name: &str,
959) -> Result<&'a str, TestScriptError> {
960    operation
961        .sourceId
962        .as_ref()
963        .and_then(|id| id.value.as_deref())
964        .ok_or_else(|| {
965            TestScriptError::ExecutionError(format!(
966                "{operation_name} operation requires sourceId at '{path}'."
967            ))
968        })
969}
970
971fn fixture_string_field(
972    fixture: &dyn MetaValue,
973    fixture_name: &str,
974    field: &str,
975) -> Result<String, TestScriptError> {
976    fixture
977        .get_field(field)
978        .ok_or_else(|| {
979            TestScriptError::ExecutionError(format!(
980                "Fixture '{fixture_name}' does not have '{field}' field."
981            ))
982        })?
983        .as_any()
984        .downcast_ref::<String>()
985        .cloned()
986        .ok_or_else(|| {
987            TestScriptError::ExecutionError(format!(
988                "Field '{field}' on fixture '{fixture_name}' is not a String."
989            ))
990        })
991}
992
993fn fixture_version_id(
994    fixture: &dyn MetaValue,
995    fixture_name: &str,
996) -> Result<String, TestScriptError> {
997    fixture
998        .get_field("meta")
999        .and_then(|meta| meta.get_field("versionId"))
1000        .ok_or_else(|| {
1001            TestScriptError::ExecutionError(format!(
1002                "Fixture '{fixture_name}' does not have a 'versionId' field."
1003            ))
1004        })?
1005        .as_any()
1006        .downcast_ref::<Box<FHIRId>>()
1007        .cloned()
1008        .and_then(|v| v.value)
1009        .ok_or_else(|| {
1010            TestScriptError::ExecutionError(format!(
1011                "Fixture '{fixture_name}' does not have a valid 'versionId' field."
1012            ))
1013        })
1014}
1015
1016fn fixture_resource(
1017    fixture: &dyn MetaValue,
1018    fixture_name: &str,
1019) -> Result<Resource, TestScriptError> {
1020    (fixture as &dyn Any)
1021        .downcast_ref::<Resource>()
1022        .cloned()
1023        .ok_or_else(|| {
1024            TestScriptError::ExecutionError(format!("Fixture '{fixture_name}' is not a Resource."))
1025        })
1026}
1027
1028async fn parsed_parameters(
1029    state: &TestState,
1030    root: TypedPointer<TestScript, TestScript>,
1031    raw: &str,
1032    path: &str,
1033    operation: &str,
1034) -> Result<ParsedParameters, TestScriptError> {
1035    let evaluated = evaluate_variable(state, root, raw).await?;
1036
1037    ParsedParameters::try_from(evaluated.as_str()).map_err(|e| {
1038        TestScriptError::ExecutionError(format!(
1039            "Failed to parse parameters for {operation} operation at '{path}': {e}"
1040        ))
1041    })
1042}
1043
1044/// Builds the headers declared by an operation's `requestHeader` entries.
1045///
1046/// Values are variable-evaluated first, so a `TestScript` may parameterize a header
1047/// the same way it parameterizes a URL or a search query.
1048async fn testscript_request_headers(
1049    state: &TestState,
1050    pointer: &TypedPointer<TestScript, TestScriptSetupActionOperation>,
1051    operation: &TestScriptSetupActionOperation,
1052) -> Result<Option<HeaderMap>, TestScriptError> {
1053    let Some(request_headers) = operation.requestHeader.as_ref() else {
1054        return Ok(None);
1055    };
1056
1057    let mut headers = HeaderMap::with_capacity(request_headers.len());
1058
1059    for request_header in request_headers {
1060        let field = request_header.field.value.as_deref().ok_or_else(|| {
1061            TestScriptError::ExecutionError(format!(
1062                "Missing requestHeader field for operation at '{}'.",
1063                pointer.path()
1064            ))
1065        })?;
1066
1067        let raw_value = request_header.value.value.as_deref().unwrap_or_default();
1068        let value = evaluate_variable(state, pointer.root(), raw_value).await?;
1069
1070        let name = HeaderName::try_from(field).map_err(|e| {
1071            TestScriptError::ExecutionError(format!(
1072                "Invalid requestHeader field '{field}' for operation at '{}': {e}",
1073                pointer.path()
1074            ))
1075        })?;
1076
1077        let value = HeaderValue::from_str(&value).map_err(|e| {
1078            TestScriptError::ExecutionError(format!(
1079                "Invalid requestHeader value for '{field}' for operation at '{}': {e}",
1080                pointer.path()
1081            ))
1082        })?;
1083
1084        headers.insert(name, value);
1085    }
1086
1087    Ok((!headers.is_empty()).then_some(headers))
1088}
1089
1090/// How often search is polled while waiting for a write to be indexed.
1091const INDEX_POLL_INTERVAL: Duration = Duration::from_millis(100);
1092
1093/// The write a response records, for requests that change a resource.
1094fn indexed_write(response: &FHIRResponse) -> Option<IndexedWrite> {
1095    let (resource, deleted) = match response {
1096        FHIRResponse::Create(created) => (&created.resource, false),
1097        FHIRResponse::Update(updated) => (&updated.resource, false),
1098        FHIRResponse::Patch(patched) => (&patched.resource, false),
1099        FHIRResponse::Delete(DeleteResponse::Instance(deleted)) => (&deleted.resource, true),
1100        _ => return None,
1101    };
1102    let json = serde_json::to_value(resource).ok()?;
1103    Some(IndexedWrite {
1104        resource_type: ResourceType::try_from(json.get("resourceType")?.as_str()?).ok()?,
1105        id: json.get("id")?.as_str()?.to_string(),
1106        version_id: if deleted {
1107            None
1108        } else {
1109            Some(json.pointer("/meta/versionId")?.as_str()?.to_string())
1110        },
1111    })
1112}
1113
1114/// Whether a request's result depends on the search index.
1115fn reads_search_index(request: &FHIRRequest) -> bool {
1116    matches!(
1117        request,
1118        FHIRRequest::Search(_)
1119            | FHIRRequest::Delete(DeleteRequest::Type(_) | DeleteRequest::System(_))
1120            | FHIRRequest::Update(UpdateRequest::Conditional(_))
1121    )
1122}
1123
1124/// Polls search until `write` is visible (its version returned, or gone after
1125/// a delete) or `timeout` runs out. On timeout the caller carries on, and a
1126/// search that depended on the write fails its assertions as usual.
1127async fn wait_for_index<CTX: Clone, Client: FHIRClient<CTX, OperationOutcomeError>>(
1128    client: &Client,
1129    ctx: &CTX,
1130    write: &IndexedWrite,
1131    timeout: Duration,
1132) {
1133    let Ok(parameters) = ParsedParameters::try_from(format!("_id={}", write.id).as_str()) else {
1134        return;
1135    };
1136    let request = FHIRRequest::Search(SearchRequest::Type(FHIRSearchTypeRequest {
1137        resource_type: write.resource_type.clone(),
1138        parameters,
1139    }));
1140    let deadline = tokio::time::Instant::now() + timeout;
1141
1142    loop {
1143        let versions: Vec<String> = match client.request(ctx.clone(), request.clone()).await {
1144            Ok(FHIRResponse::Search(SearchResponse::Type(response))) => response
1145                .bundle
1146                .entry
1147                .unwrap_or_default()
1148                .into_iter()
1149                .filter_map(|entry| {
1150                    let json = serde_json::to_value(entry.resource?).ok()?;
1151                    Some(json.pointer("/meta/versionId")?.as_str()?.to_string())
1152                })
1153                .collect(),
1154            _ => vec![],
1155        };
1156        let visible = match &write.version_id {
1157            Some(version_id) => versions.contains(version_id),
1158            None => versions.is_empty(),
1159        };
1160        if visible {
1161            return;
1162        }
1163        if tokio::time::Instant::now() >= deadline {
1164            tracing::warn!(
1165                resource_type = write.resource_type.as_ref(),
1166                id = write.id,
1167                "Timed out waiting for a write to be indexed"
1168            );
1169            return;
1170        }
1171        tokio::time::sleep(INDEX_POLL_INTERVAL).await;
1172    }
1173}
1174
1175/// The extension that repeats an operation while its response has the status
1176/// in its `valueString` (such as `"202"`), as a client polls an asynchronous
1177/// request's status URL. Only the last response is recorded.
1178const POLL_EXTENSION_URL: &str =
1179    "https://haste.health/fhir/StructureDefinition/testscript-poll-while-response-code";
1180
1181/// How long an operation is polled before the last response is taken as it is.
1182const POLL_TIMEOUT: Duration = Duration::from_secs(60);
1183
1184/// The shortest wait between polls, and the wait without a `Retry-After`.
1185const POLL_INTERVAL: Duration = Duration::from_secs(1);
1186
1187/// The status the operation's poll extension repeats it on, if it has one.
1188fn poll_while(
1189    operation: &TestScriptSetupActionOperation,
1190    path: &str,
1191) -> Result<Option<StatusCode>, TestScriptError> {
1192    let Some(extension) = operation
1193        .extension
1194        .as_ref()
1195        .and_then(|extensions| extensions.iter().find(|e| e.url == POLL_EXTENSION_URL))
1196    else {
1197        return Ok(None);
1198    };
1199
1200    match &extension.value {
1201        Some(ExtensionValueTypeChoice::String(code)) => code
1202            .value
1203            .as_deref()
1204            .and_then(|code| StatusCode::from_bytes(code.trim().as_bytes()).ok()),
1205        _ => None,
1206    }
1207    .map(Some)
1208    .ok_or_else(|| {
1209        TestScriptError::ExecutionError(format!(
1210            "The poll extension on the operation at '{path}' needs a valueString holding an \
1211             HTTP status code."
1212        ))
1213    })
1214}
1215
1216/// A response header's value, with repeats joined as HTTP allows (`, `).
1217fn header_value(recorded: &RecordedResponse, field: &str) -> Option<String> {
1218    let headers = &recorded.http.as_ref()?.headers;
1219    let values: Vec<&str> = headers
1220        .get_all(field)
1221        .iter()
1222        .filter_map(|value| value.to_str().ok())
1223        .collect();
1224
1225    (!values.is_empty()).then(|| values.join(", "))
1226}
1227
1228/// `Retry-After` in its delay-seconds form.
1229fn retry_after(recorded: &RecordedResponse) -> Option<Duration> {
1230    header_value(recorded, header::RETRY_AFTER.as_str())?
1231        .trim()
1232        .parse::<u64>()
1233        .ok()
1234        .map(Duration::from_secs)
1235}
1236
1237async fn send_operation<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
1238    client: &Client,
1239    ctx: CTX,
1240    request: &OperationRequest,
1241) -> RecordedResponse {
1242    match request {
1243        OperationRequest::Interaction(request) => {
1244            let (http, result) = client
1245                .request_with_metadata(ctx, request.as_ref().clone())
1246                .await;
1247            let response = match result {
1248                Ok(response) => Response::Interaction(Box::new(response)),
1249                Err(error) => Response::OperationError(Arc::new(error)),
1250            };
1251
1252            RecordedResponse { response, http }
1253        }
1254        OperationRequest::Http { method, url } => match client.send(ctx, method.clone(), url).await
1255        {
1256            Ok((metadata, body)) => RecordedResponse {
1257                // A body that is not a FHIR resource (a Bulk Data manifest,
1258                // say) is left to the HTTP-level assertions.
1259                response: Response::Http(
1260                    serde_json::from_slice::<Resource>(&body).ok().map(Box::new),
1261                ),
1262                http: Some(metadata),
1263            },
1264            Err(error) => RecordedResponse {
1265                response: Response::OperationError(Arc::new(error)),
1266                http: None,
1267            },
1268        },
1269    }
1270}
1271
1272/// Sends the operation, and while its response has the `poll_while` status
1273/// sends it again, waiting as long as `Retry-After` says, up to
1274/// [`POLL_TIMEOUT`]. Returns the last response.
1275async fn send_polling<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
1276    client: &Client,
1277    ctx: CTX,
1278    request: &OperationRequest,
1279    poll_while: Option<StatusCode>,
1280) -> RecordedResponse {
1281    let Some(poll_while) = poll_while else {
1282        return send_operation(client, ctx, request).await;
1283    };
1284    let deadline = tokio::time::Instant::now() + POLL_TIMEOUT;
1285
1286    loop {
1287        let recorded = send_operation(client, ctx.clone(), request).await;
1288        let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
1289        if recorded.http.as_ref().map(|http| http.status) != Some(poll_while) || remaining.is_zero()
1290        {
1291            return recorded;
1292        }
1293
1294        let wait = retry_after(&recorded)
1295            .unwrap_or_default()
1296            .max(POLL_INTERVAL)
1297            .min(remaining);
1298        tokio::time::sleep(wait).await;
1299    }
1300}
1301
1302async fn run_operation<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
1303    client: &Client,
1304    ctx: CTX,
1305    state: Arc<Mutex<TestState>>,
1306    pointer: TypedPointer<TestScript, TestScriptSetupActionOperation>,
1307    options: Arc<TestRunnerOptions>,
1308) -> Result<TestResult<TestReportSetupActionOperation>, TestScriptError> {
1309    let operation = pointer.value().ok_or_else(|| {
1310        TestScriptError::ExecutionError(format!(
1311            "Failed to retrieve TestScript operation at '{}'.",
1312            pointer.path()
1313        ))
1314    })?;
1315
1316    let mut state_guard = state.lock().await;
1317    let request = testscript_operation_to_request(&state_guard, &pointer).await?;
1318
1319    if let Some(timeout) = options.index_wait_timeout
1320        && let OperationRequest::Interaction(fhir_request) = &request
1321        && reads_search_index(fhir_request)
1322        && !state_guard.unindexed_writes.is_empty()
1323    {
1324        // One deadline for the whole set: the worker indexes them together.
1325        let deadline = tokio::time::Instant::now() + timeout;
1326        for write in std::mem::take(&mut state_guard.unindexed_writes) {
1327            let remaining = deadline.saturating_duration_since(tokio::time::Instant::now());
1328            if remaining.is_zero() {
1329                break;
1330            }
1331            wait_for_index(client, &ctx, &write, remaining).await;
1332        }
1333    }
1334
1335    let ctx = match testscript_request_headers(&state_guard, &pointer, operation).await? {
1336        Some(headers) => ctx.with_request_headers(headers),
1337        None => ctx,
1338    };
1339
1340    let poll_while = poll_while(operation, pointer.path())?;
1341    let recorded = send_polling(client, ctx, &request, poll_while).await;
1342
1343    if let Some(wait_duration) = options.wait_between_operations {
1344        tokio::time::sleep(wait_duration).await;
1345    }
1346
1347    let failure = match &recorded.response {
1348        Response::Interaction(response) => {
1349            if let Some(write) = indexed_write(response) {
1350                state_guard.unindexed_writes.push(write);
1351            }
1352            None
1353        }
1354        Response::OperationError(error) => Some(error.to_string()),
1355        Response::Http(_) => recorded
1356            .http
1357            .as_ref()
1358            .filter(|http| !http.status.is_success())
1359            .map(|http| format!("HTTP {}", http.status)),
1360    };
1361
1362    let fhir_request = match request {
1363        OperationRequest::Interaction(request) => Some(*request),
1364        OperationRequest::Http { .. } => None,
1365    };
1366    associate_request_response_variables(&mut state_guard, operation, fhir_request, recorded);
1367    drop(state_guard);
1368
1369    let value = match failure {
1370        None => TestReportSetupActionOperation {
1371            result: ReportActionResultCodes::pass(),
1372            ..Default::default()
1373        },
1374        Some(failure) => {
1375            tracing::warn!(
1376                path = pointer.path(),
1377                operation.label = operation
1378                    .label
1379                    .as_ref()
1380                    .and_then(|l| l.value.as_deref())
1381                    .unwrap_or("<no-label>"),
1382                operation.operation_type = get_operation_type(operation).unwrap_or("<unknown>"),
1383                error = %failure,
1384                "TestScript operation failed"
1385            );
1386
1387            TestReportSetupActionOperation {
1388                result: ReportActionResultCodes::warning(),
1389                message: Some(Box::new(FHIRMarkdown {
1390                    value: Some(format!("Operation failed: {failure}")),
1391                    ..Default::default()
1392                })),
1393                ..Default::default()
1394            }
1395        }
1396    };
1397
1398    Ok(TestResult {
1399        state: state.clone(),
1400        value,
1401    })
1402}
1403
1404fn get_source<'a>(
1405    state: &'a TestState,
1406    assertion: &TestScriptSetupActionAssert,
1407) -> Result<Option<&'a dyn MetaValue>, TestScriptError> {
1408    if let Some(source_id) = assertion.sourceId.as_ref().and_then(|id| id.value.as_ref()) {
1409        let source = state.resolve_fixture(source_id)?;
1410        Ok(Some(source))
1411    } else {
1412        match assertion
1413            .direction
1414            .as_ref()
1415            .unwrap_or(&AssertDirectionCodes::response())
1416        {
1417            assertion if assertion == &AssertDirectionCodes::request() => {
1418                if let Some(request) = state.latest_request.as_ref() {
1419                    request_to_meta_value(request)
1420                        .ok_or_else(|| TestScriptError::InvalidFixture)
1421                        .map(Some)
1422                } else {
1423                    Ok(None)
1424                }
1425            }
1426            assertion if assertion == &AssertDirectionCodes::response() => {
1427                if let Some(recorded) = state.latest_response.as_ref() {
1428                    response_to_meta_value(&recorded.response)
1429                        .ok_or_else(|| TestScriptError::InvalidFixture)
1430                        .map(Some)
1431                } else {
1432                    Ok(None)
1433                }
1434            }
1435            _ => Err(TestScriptError::ExecutionError(
1436                "Assert direction cannot be 'null' when sourceId is not provided.".to_string(),
1437            )),
1438        }
1439    }
1440}
1441
1442fn evaluate_operator(
1443    operator: &BoundCode<AssertOperatorCodes>,
1444    a: &Vec<conversion::ConvertedValue>,
1445    b: &Vec<conversion::ConvertedValue>,
1446) -> bool {
1447    match operator {
1448        operator
1449            if operator == &AssertOperatorCodes::equals()
1450                || operator == &AssertOperatorCodes::null() =>
1451        {
1452            a == b
1453        }
1454        operator if operator == &AssertOperatorCodes::not_equals() => !(a == b),
1455
1456        operator if operator == &AssertOperatorCodes::contains() => {
1457            if a.len() != 1 || b.len() != 1 {
1458                return false;
1459            }
1460
1461            match (&a[0], &b[0]) {
1462                (ConvertedValue::String(a_str), ConvertedValue::String(b_str)) => {
1463                    a_str.contains(b_str)
1464                }
1465                _ => false,
1466            }
1467        }
1468        operator if operator == &AssertOperatorCodes::empty() => {
1469            todo!("Empty operator not implemented")
1470        }
1471        operator if operator == &AssertOperatorCodes::eval() => {
1472            todo!("Eval operator not implemented")
1473        }
1474        operator if operator == &AssertOperatorCodes::greater_than() => {
1475            todo!("GreaterThan operator not implemented")
1476        }
1477        operator if operator == &AssertOperatorCodes::in_() => todo!("In operator not implemented"),
1478        operator if operator == &AssertOperatorCodes::less_than() => {
1479            todo!("LessThan operator not implemented")
1480        }
1481        operator if operator == &AssertOperatorCodes::not_contains() => {
1482            todo!("NotContains operator not implemented")
1483        }
1484        operator if operator == &AssertOperatorCodes::not_empty() => {
1485            todo!("NotEmpty operator not implemented")
1486        }
1487        operator if operator == &AssertOperatorCodes::not_in() => {
1488            todo!("NotIn operator not implemented")
1489        }
1490        _ => {
1491            todo!("Operator '{:?}' not implemented", operator)
1492        }
1493    }
1494    // a == b
1495}
1496
1497async fn derive_comparison_to(
1498    state: &TestState,
1499    assertion: &TestScriptSetupActionAssert,
1500) -> Result<Vec<ConvertedValue>, TestScriptError> {
1501    if let Some(comparision_fixture_id) = assertion
1502        .compareToSourceId
1503        .as_ref()
1504        .and_then(|c| c.value.as_ref())
1505    {
1506        let comparison_fixture = state.resolve_fixture(comparision_fixture_id)?;
1507
1508        let Some(comparison_expression) = assertion
1509            .compareToSourceExpression
1510            .as_ref()
1511            .and_then(|exp| exp.value.as_ref())
1512        else {
1513            return Err(TestScriptError::ExecutionError(
1514                "compareToSourceExpression is required when compareToSourceId is provided."
1515                    .to_string(),
1516            ));
1517        };
1518
1519        let result = state
1520            .fp_engine
1521            .evaluate(comparison_expression, vec![comparison_fixture])
1522            .await
1523            .map_err(|e| {
1524                TestScriptError::ExecutionError(format!(
1525                    "FHIRPath evaluation error for comparison fixture '{comparision_fixture_id}': {e}"
1526                ))
1527            })?;
1528
1529        Ok(result
1530            .iter()
1531            .map(conversion::convert_meta_value)
1532            .collect::<Vec<_>>())
1533    } else if let Some(value) = assertion.value.as_ref().and_then(|v| v.value.as_ref())
1534        && let Some(converted_value) = conversion::convert_string_value(value.as_ref())
1535    {
1536        Ok(vec![converted_value])
1537    } else {
1538        Err(TestScriptError::ExecutionError(
1539            "Failed to derive comparison value for assertion.".to_string(),
1540        ))
1541    }
1542}
1543
1544/// Assertions carry a `label`/`description` intended by the FHIR spec for
1545/// exactly this purpose: identifying an assertion in test engine output.
1546fn assert_label(assertion: &TestScriptSetupActionAssert) -> &str {
1547    assertion
1548        .label
1549        .as_ref()
1550        .and_then(|l| l.value.as_deref())
1551        .or_else(|| {
1552            assertion
1553                .description
1554                .as_ref()
1555                .and_then(|d| d.value.as_deref())
1556        })
1557        .unwrap_or("<no-label>")
1558}
1559
1560/// Assertions are what determine the testreports ultimate pass/fail status.
1561/// So set that within state here depending on assertion success/failure.
1562async fn run_assertion(
1563    state: Arc<Mutex<TestState>>,
1564    pointer: TypedPointer<TestScript, TestScriptSetupActionAssert>,
1565) -> Result<TestResult<TestReportSetupActionAssert>, TestScriptError> {
1566    let assertion = pointer.value().ok_or_else(|| {
1567        TestScriptError::ExecutionError(format!(
1568            "Failed to retrieve TestScript assertion at '{}'.",
1569            pointer.path()
1570        ))
1571    })?;
1572
1573    let mut state_guard = state.lock().await;
1574
1575    let default = AssertOperatorCodes::equals();
1576    let operator = assertion.operator.as_ref().unwrap_or(&default);
1577
1578    // Keep all uses of `source` inside this scope so the immutable borrow
1579    // of `state_guard` ends before we mutate `state_guard`.
1580    let failure_message = if let Some(message) =
1581        evaluate_http_assertion(&state_guard, assertion, pointer.path(), operator)?
1582    {
1583        Some(message)
1584    } else if assertion.resource.is_some() || assertion.expression.is_some() {
1585        let Some(source) = get_source(&state_guard, assertion)? else {
1586            return Err(TestScriptError::ExecutionError(format!(
1587                "Failed to resolve source for assertion at '{}'.",
1588                pointer.path()
1589            )));
1590        };
1591
1592        if let Some(message) =
1593            evaluate_resource_assertion(assertion, pointer.path(), source, operator)
1594        {
1595            Some(message)
1596        } else if let Some(expression) =
1597            assertion.expression.as_ref().and_then(|e| e.value.as_ref())
1598        {
1599            evaluate_expression_assertion(
1600                &state_guard,
1601                assertion,
1602                pointer.path(),
1603                source,
1604                operator,
1605                expression,
1606            )
1607            .await?
1608        } else {
1609            None
1610        }
1611    } else {
1612        None
1613    };
1614
1615    let (result, message) = match failure_message {
1616        Some(message) => {
1617            tracing::error!(
1618                assert.label = assert_label(assertion),
1619                path = pointer.path(),
1620                "{message}"
1621            );
1622
1623            state_guard.result = ReportResultCodes::fail();
1624
1625            (
1626                ReportActionResultCodes::fail(),
1627                Some(Box::new(FHIRMarkdown {
1628                    value: Some(message),
1629                    ..Default::default()
1630                })),
1631            )
1632        }
1633        None => (ReportActionResultCodes::pass(), None),
1634    };
1635
1636    Ok(TestResult {
1637        state: state.clone(),
1638        value: TestReportSetupActionAssert {
1639            result,
1640            message,
1641            ..Default::default()
1642        },
1643    })
1644}
1645
1646/// The status code an assertion's `response` code stands for.
1647fn response_code_status(code: &str) -> Option<u16> {
1648    Some(match code {
1649        "okay" => 200,
1650        "created" => 201,
1651        "noContent" => 204,
1652        "notModified" => 304,
1653        "bad" => 400,
1654        "forbidden" => 403,
1655        "notFound" => 404,
1656        "methodNotAllowed" => 405,
1657        "conflict" => 409,
1658        "gone" => 410,
1659        "preconditionFailed" => 412,
1660        "unprocessable" => 422,
1661        _ => return None,
1662    })
1663}
1664
1665/// Compares an HTTP value (a status code or a header, `None` when absent)
1666/// with an assertion's expected value under its operator, or `None` for an
1667/// operator that does not apply. `in` and `notIn` take a comma-separated list.
1668fn compare_http_value(operator: &str, actual: Option<&str>, expected: &str) -> Option<bool> {
1669    let listed = || {
1670        expected
1671            .split(',')
1672            .map(str::trim)
1673            .any(|item| Some(item) == actual)
1674    };
1675    let contains = actual.is_some_and(|actual| actual.contains(expected));
1676
1677    Some(match operator {
1678        "equals" => actual == Some(expected),
1679        "notEquals" => actual != Some(expected),
1680        "in" => listed(),
1681        "notIn" => !listed(),
1682        "contains" => contains,
1683        "notContains" => !contains,
1684        "empty" => actual.is_none_or(str::is_empty),
1685        "notEmpty" => actual.is_some_and(|actual| !actual.is_empty()),
1686        _ => return None,
1687    })
1688}
1689
1690/// An assertion's HTTP-level check, if it has one: `responseCode`,
1691/// `response`, `headerField` (against `value`) or `contentType`, made on the
1692/// response `sourceId` names or the latest one. An assertion holds at most one
1693/// check (invariants tst-5 and tst-6). Returns the failure.
1694fn evaluate_http_assertion(
1695    state: &TestState,
1696    assertion: &TestScriptSetupActionAssert,
1697    path: &str,
1698    operator: &BoundCode<AssertOperatorCodes>,
1699) -> Result<Option<String>, TestScriptError> {
1700    let response_code = assertion
1701        .responseCode
1702        .as_ref()
1703        .and_then(|code| code.value.as_deref());
1704    let response = assertion.response.as_ref().and_then(BoundCode::as_str);
1705    let header_field = assertion
1706        .headerField
1707        .as_ref()
1708        .and_then(|field| field.value.as_deref());
1709    let content_type = assertion
1710        .contentType
1711        .as_ref()
1712        .and_then(|content_type| content_type.value.as_deref());
1713
1714    if response_code.is_none()
1715        && response.is_none()
1716        && header_field.is_none()
1717        && content_type.is_none()
1718    {
1719        return Ok(None);
1720    }
1721
1722    let label = assert_label(assertion);
1723    let error = |message: String| {
1724        TestScriptError::ExecutionError(format!("Assertion '{label}' at '{path}': {message}"))
1725    };
1726    if assertion.direction.as_ref() == Some(&AssertDirectionCodes::request()) {
1727        return Err(error(
1728            "responseCode, response, headerField and contentType are checked against \
1729             responses only."
1730                .to_string(),
1731        ));
1732    }
1733
1734    let source_id = assertion
1735        .sourceId
1736        .as_ref()
1737        .and_then(|id| id.value.as_deref());
1738    let recorded = state.resolve_response(source_id)?;
1739    let Some(http) = recorded.http.as_ref() else {
1740        return Ok(Some(format!(
1741            "Assertion '{label}' at '{path}' failed: no HTTP response arrived."
1742        )));
1743    };
1744    let status = http.status.as_u16().to_string();
1745
1746    // What is checked, its actual value, and the expected one.
1747    let (what, actual, expected) = if let Some(code) = response_code {
1748        ("response code".to_string(), Some(status), code.to_string())
1749    } else if let Some(code) = response {
1750        let expected = response_code_status(code)
1751            .ok_or_else(|| error(format!("unknown response code '{code}'.")))?;
1752        (
1753            format!("response code (for '{code}')"),
1754            Some(status),
1755            expected.to_string(),
1756        )
1757    } else if let Some(field) = header_field {
1758        let expected = assertion
1759            .value
1760            .as_ref()
1761            .and_then(|value| value.value.as_deref())
1762            .unwrap_or_default();
1763        (
1764            format!("header '{field}'"),
1765            header_value(recorded, field),
1766            expected.to_string(),
1767        )
1768    } else {
1769        // The media type alone, without parameters such as `charset`.
1770        let actual = header_value(recorded, header::CONTENT_TYPE.as_str()).map(|value| {
1771            value
1772                .split(';')
1773                .next()
1774                .unwrap_or_default()
1775                .trim()
1776                .to_ascii_lowercase()
1777        });
1778        (
1779            "content type".to_string(),
1780            actual,
1781            content_type.unwrap_or_default().to_ascii_lowercase(),
1782        )
1783    };
1784
1785    let operator = operator.as_str().unwrap_or("equals");
1786    let passed = compare_http_value(operator, actual.as_deref(), &expected).ok_or_else(|| {
1787        error(format!(
1788            "operator '{operator}' is not supported for responseCode, response, headerField \
1789             or contentType."
1790        ))
1791    })?;
1792    if passed {
1793        return Ok(None);
1794    }
1795
1796    let actual = actual.map_or_else(|| "absent".to_string(), |actual| format!("'{actual}'"));
1797    let expected = match operator {
1798        "empty" | "notEmpty" => String::new(),
1799        _ => format!(" '{expected}'"),
1800    };
1801    Ok(Some(format!(
1802        "Assertion '{label}' at '{path}' failed: {what} was {actual}, expected {operator}{expected}."
1803    )))
1804}
1805
1806fn evaluate_resource_assertion(
1807    assertion: &TestScriptSetupActionAssert,
1808    path: &str,
1809    source: &dyn MetaValue,
1810    operator: &BoundCode<AssertOperatorCodes>,
1811) -> Option<String> {
1812    let resource_string = assertion.resource.as_ref()?.as_str()?;
1813
1814    let operation_evaluation_result = evaluate_operator(
1815        operator,
1816        &vec![conversion::ConvertedValue::String(
1817            resource_string.to_string(),
1818        )],
1819        &vec![conversion::ConvertedValue::String(
1820            source.fhir_type().to_string(),
1821        )],
1822    );
1823
1824    if operation_evaluation_result {
1825        return None;
1826    }
1827
1828    Some(format!(
1829        "Assertion '{}' at '{}' failed: resource type '{resource_string}' does not match '{}'.",
1830        assert_label(assertion),
1831        path,
1832        source.fhir_type()
1833    ))
1834}
1835
1836async fn evaluate_expression_assertion(
1837    state_guard: &TestState,
1838    assertion: &TestScriptSetupActionAssert,
1839    path: &str,
1840    source: &dyn MetaValue,
1841    operator: &BoundCode<AssertOperatorCodes>,
1842    expression: &str,
1843) -> Result<Option<String>, TestScriptError> {
1844    let comparison_to = derive_comparison_to(state_guard, assertion).await?;
1845
1846    let Ok(result) = state_guard
1847        .fp_engine
1848        .evaluate(expression, vec![source])
1849        .await
1850    else {
1851        tracing::error!(
1852            assert.label = assert_label(assertion),
1853            path = path,
1854            expression = expression,
1855            "Assertion '{}' at '{}' failed: FHIRPath expression '{expression}' failed to evaluate.",
1856            assert_label(assertion),
1857            path,
1858        );
1859
1860        return Err(TestScriptError::ExecutionError(format!(
1861            "Assertion '{}' at '{}': FHIRPath expression '{expression}' failed to evaluate.",
1862            assert_label(assertion),
1863            path,
1864        )));
1865    };
1866
1867    let converted_values = result
1868        .iter()
1869        .map(conversion::convert_meta_value)
1870        .collect::<Vec<_>>();
1871
1872    let operation_evaluation_result =
1873        evaluate_operator(operator, &converted_values, &comparison_to);
1874
1875    if operation_evaluation_result {
1876        return Ok(None);
1877    }
1878
1879    Ok(Some(format!(
1880        "Assertion '{}' at '{}' failed: '{converted_values:?}' {operator:?} '{comparison_to:?}'.",
1881        assert_label(assertion),
1882        path,
1883    )))
1884}
1885
1886async fn run_action<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
1887    client: &Client,
1888    ctx: CTX,
1889    state: Arc<Mutex<TestState>>,
1890    pointer: TypedPointer<TestScript, TestScriptTestAction>,
1891    options: Arc<TestRunnerOptions>,
1892) -> Result<TestResult<TestReportSetupAction>, TestScriptError> {
1893    tracing::info!("Running TestScript action at path: {}", pointer.path());
1894    let action = pointer.value().ok_or_else(|| {
1895        TestScriptError::ExecutionError(format!(
1896            "Failed to retrieve TestScript action at '{}'.",
1897            pointer.path()
1898        ))
1899    })?;
1900
1901    // Should be either an operation or an assert.
1902    // Both should not exist at the same time.
1903    if action.operation.is_some() {
1904        let Some(operation_pointer) =
1905            pointer.descend::<TestScriptSetupActionOperation>(&Key::Field("operation".to_string()))
1906        else {
1907            return Err(TestScriptError::ExecutionError(format!(
1908                "Failed to retrieve TestScript operation at '{}'.",
1909                pointer.path()
1910            )));
1911        };
1912
1913        let result = run_operation(client, ctx, state, operation_pointer, options).await?;
1914
1915        Ok(TestResult {
1916            state: result.state,
1917            value: TestReportSetupAction {
1918                operation: Some(result.value),
1919                ..Default::default()
1920            },
1921        })
1922    } else if action.assert.is_some() {
1923        let Some(assertion_pointer) =
1924            pointer.descend::<TestScriptSetupActionAssert>(&Key::Field("assert".to_string()))
1925        else {
1926            return Err(TestScriptError::ExecutionError(format!(
1927                "Failed to retrieve TestScript assertion at '{}'.",
1928                pointer.path()
1929            )));
1930        };
1931
1932        let assertion = run_assertion(state, assertion_pointer).await?;
1933
1934        Ok(TestResult {
1935            state: assertion.state,
1936            value: TestReportSetupAction {
1937                assert: Some(assertion.value),
1938                ..Default::default()
1939            },
1940        })
1941    } else {
1942        Err(TestScriptError::ExecutionError(format!(
1943            "TestScript action must have either an operation or an assert at '{}'.",
1944            pointer.path()
1945        )))
1946    }
1947}
1948
1949async fn run_setup_action<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
1950    client: &Client,
1951    ctx: CTX,
1952    state: Arc<Mutex<TestState>>,
1953    pointer: TypedPointer<TestScript, TestScriptSetupAction>,
1954    options: Arc<TestRunnerOptions>,
1955) -> Result<TestResult<TestReportSetupAction>, TestScriptError> {
1956    let action = pointer.value().ok_or_else(|| {
1957        TestScriptError::ExecutionError(format!(
1958            "Failed to retrieve TestScript action at '{}'.",
1959            pointer.path()
1960        ))
1961    })?;
1962
1963    tracing::info!("Running TestScript action at path: {}", pointer.path());
1964
1965    // Should be either an operation or an assert.
1966    // Both should not exist at the same time.
1967    if action.operation.is_some() {
1968        let Some(operation_pointer) =
1969            pointer.descend::<TestScriptSetupActionOperation>(&Key::Field("operation".to_string()))
1970        else {
1971            return Err(TestScriptError::ExecutionError(format!(
1972                "Failed to retrieve TestScript operation at '{}'.",
1973                pointer.path()
1974            )));
1975        };
1976
1977        let result = run_operation(client, ctx, state, operation_pointer, options).await?;
1978
1979        Ok(TestResult {
1980            state: result.state,
1981            value: TestReportSetupAction {
1982                operation: Some(result.value),
1983                ..Default::default()
1984            },
1985        })
1986    } else if action.assert.is_some() {
1987        let Some(assertion_pointer) =
1988            pointer.descend::<TestScriptSetupActionAssert>(&Key::Field("assert".to_string()))
1989        else {
1990            return Err(TestScriptError::ExecutionError(format!(
1991                "Failed to retrieve TestScript assertion at '{}'.",
1992                pointer.path()
1993            )));
1994        };
1995
1996        let assertion = run_assertion(state, assertion_pointer).await?;
1997
1998        Ok(TestResult {
1999            state: assertion.state,
2000            value: TestReportSetupAction {
2001                assert: Some(assertion.value),
2002                ..Default::default()
2003            },
2004        })
2005    } else {
2006        Err(TestScriptError::ExecutionError(format!(
2007            "TestScript action must have either an operation or an assert at '{}'.",
2008            pointer.path()
2009        )))
2010    }
2011}
2012
2013async fn setup_fixtures<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
2014    client: &Client,
2015    ctx: CTX,
2016    state: Arc<Mutex<TestState>>,
2017    pointer: TypedPointer<TestScript, TestScript>,
2018    _options: Arc<TestRunnerOptions>,
2019) -> Result<Arc<Mutex<TestState>>, OperationOutcomeError> {
2020    let mut state_lock = state.lock().await;
2021
2022    let Some(fixtures_pointer) =
2023        pointer.descend::<Vec<TestScriptFixture>>(&Key::Field("fixture".to_string()))
2024    else {
2025        return Ok(state.clone());
2026    };
2027
2028    let Some(fixtures) = fixtures_pointer.value() else {
2029        return Ok(state.clone());
2030    };
2031
2032    for fixture in fixtures {
2033        if let Some(reference_string) = fixture
2034            .resource
2035            .as_ref()
2036            .and_then(|r| r.reference.as_ref())
2037            .and_then(|refe| refe.value.as_ref())
2038        {
2039            let resolved_resource = if reference_string.starts_with('#')
2040                && let Some(contained) =
2041                    pointer.descend::<Vec<Resource>>(&Key::Field("contained".to_string()))
2042                && let Some(contained) = contained.value()
2043            {
2044                let local_id = &reference_string[1..];
2045                let Some(resource) = contained.iter().find(|res| {
2046                    if let Some(id) = res.get_field("id")
2047                        && let Some(id) = id.as_any().downcast_ref::<String>()
2048                    {
2049                        id.as_str() == local_id
2050                    } else {
2051                        false
2052                    }
2053                }) else {
2054                    return Err(OperationOutcomeError::error(
2055                        IssueType::not_found(),
2056                        format!("Contained resource with id '{local_id}' not found."),
2057                    ));
2058                };
2059
2060                resource.clone()
2061            } else {
2062                let parts = reference_string.split('/').collect::<Vec<&str>>();
2063                if parts.len() != 2 {
2064                    return Err(OperationOutcomeError::error(
2065                        IssueType::invalid(),
2066                        format!("Invalid fixture reference: {reference_string}"),
2067                    ));
2068                }
2069
2070                let resource_type = parts[0];
2071                let id = parts[1];
2072
2073                let Some(remote_resource) = client
2074                    .read(
2075                        ctx.clone(),
2076                        ResourceType::try_from(resource_type).map_err(|_| {
2077                            OperationOutcomeError::error(
2078                                IssueType::invalid(),
2079                                format!(
2080                                    "Invalid resource type in fixture reference: '{resource_type}'"
2081                                ),
2082                            )
2083                        })?,
2084                        id.to_string(),
2085                    )
2086                    .await?
2087                else {
2088                    return Err(OperationOutcomeError::error(
2089                        IssueType::not_found(),
2090                        format!("Resource '{resource_type}' with id '{id}' not found."),
2091                    ));
2092                };
2093
2094                remote_resource
2095            };
2096
2097            state_lock.fixtures.insert(
2098                fixture.id.clone().unwrap_or_default(),
2099                Fixtures::Resource(resolved_resource),
2100            );
2101        }
2102    }
2103
2104    drop(state_lock);
2105
2106    Ok(state)
2107}
2108
2109async fn run_setup<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
2110    client: &Client,
2111    ctx: CTX,
2112    state: Arc<Mutex<TestState>>,
2113    pointer: TypedPointer<TestScript, TestScriptSetup>,
2114    options: Arc<TestRunnerOptions>,
2115) -> Result<TestResult<TestReportSetup>, TestScriptError> {
2116    let mut cur_state = state;
2117
2118    let mut setup_results = TestReportSetup {
2119        action: vec![],
2120        ..Default::default()
2121    };
2122
2123    let Some(setup) = pointer.value() else {
2124        return Ok(TestResult {
2125            state: cur_state,
2126            value: setup_results,
2127        });
2128    };
2129
2130    for action in setup.action.iter().enumerate() {
2131        let action_pointer = pointer
2132            .descend::<Vec<TestScriptSetupAction>>(&Key::Field("action".to_string()))
2133            .and_then(|p| p.descend::<TestScriptSetupAction>(&Key::Index(action.0)));
2134
2135        let action_pointer = action_pointer.ok_or_else(|| {
2136            TestScriptError::ExecutionError(format!(
2137                "Failed to retrieve TestScript action at index {}.",
2138                action.0
2139            ))
2140        })?;
2141
2142        let result = run_setup_action(
2143            client,
2144            ctx.clone(),
2145            cur_state,
2146            action_pointer,
2147            options.clone(),
2148        )
2149        .await?;
2150        cur_state = result.state;
2151
2152        setup_results.action.push(result.value);
2153    }
2154
2155    Ok(TestResult {
2156        state: cur_state,
2157        value: setup_results,
2158    })
2159}
2160
2161async fn run_teardown<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
2162    client: &Client,
2163    ctx: CTX,
2164    state: Arc<Mutex<TestState>>,
2165    pointer: TypedPointer<TestScript, TestScriptTeardown>,
2166    options: Arc<TestRunnerOptions>,
2167) -> Result<TestResult<TestReportTeardown>, TestScriptError> {
2168    let mut cur_state = state;
2169
2170    let mut teardown_results = TestReportTeardown {
2171        action: vec![],
2172        ..Default::default()
2173    };
2174
2175    let Some(actions) = pointer.value() else {
2176        return Ok(TestResult {
2177            state: cur_state,
2178            value: teardown_results,
2179        });
2180    };
2181
2182    for action in actions.action.iter().enumerate() {
2183        let action_pointer = pointer
2184            .descend::<Vec<TestScriptTeardownAction>>(&Key::Field("action".to_string()))
2185            .and_then(|p| p.descend::<TestScriptTeardownAction>(&Key::Index(action.0)));
2186
2187        let action_pointer = action_pointer.ok_or_else(|| {
2188            TestScriptError::ExecutionError(format!(
2189                "Failed to retrieve TestScript teardown action at index {}.",
2190                action.0
2191            ))
2192        })?;
2193
2194        let operation_pointer = action_pointer
2195            .descend::<TestScriptSetupActionOperation>(&Key::Field("operation".to_string()))
2196            .ok_or_else(|| {
2197                TestScriptError::ExecutionError(format!(
2198                    "Failed to retrieve TestScript teardown operation at index {}.",
2199                    action.0
2200                ))
2201            })?;
2202
2203        let result = run_operation(
2204            client,
2205            ctx.clone(),
2206            cur_state,
2207            operation_pointer,
2208            options.clone(),
2209        )
2210        .await?;
2211        cur_state = result.state;
2212
2213        teardown_results.action.push(TestReportTeardownAction {
2214            operation: result.value,
2215            ..Default::default()
2216        });
2217    }
2218
2219    Ok(TestResult {
2220        state: cur_state,
2221        value: teardown_results,
2222    })
2223}
2224
2225async fn run_test<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
2226    client: &Client,
2227    ctx: CTX,
2228    state: Arc<Mutex<TestState>>,
2229    pointer: TypedPointer<TestScript, TestScriptTest>,
2230    options: Arc<TestRunnerOptions>,
2231) -> Result<TestResult<TestReportTest>, TestScriptError> {
2232    let mut cur_state = state;
2233    let mut test_report_test = TestReportTest {
2234        action: vec![],
2235        ..Default::default()
2236    };
2237
2238    let test = pointer.value().ok_or_else(|| {
2239        TestScriptError::ExecutionError(format!(
2240            "Failed to retrieve TestScript test at '{}'.",
2241            pointer.path()
2242        ))
2243    })?;
2244
2245    for action in test.action.iter().enumerate() {
2246        let Some(action_pointer) = pointer
2247            .descend::<Vec<TestScriptTestAction>>(&Key::Field("action".to_string()))
2248            .and_then(|p| p.descend(&Key::Index(action.0)))
2249        else {
2250            return Err(TestScriptError::ExecutionError(format!(
2251                "Failed to retrieve TestScript test action at index {}.",
2252                action.0
2253            )));
2254        };
2255        let result = run_action(
2256            client,
2257            ctx.clone(),
2258            cur_state,
2259            action_pointer,
2260            options.clone(),
2261        )
2262        .await?;
2263        cur_state = result.state;
2264        test_report_test.action.push(TestReportTestAction {
2265            operation: result.value.operation,
2266            assert: result.value.assert,
2267            ..Default::default()
2268        });
2269    }
2270
2271    Ok(TestResult {
2272        state: cur_state,
2273        value: test_report_test,
2274    })
2275}
2276
2277async fn run_tests<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
2278    client: &Client,
2279    ctx: CTX,
2280    state: Arc<Mutex<TestState>>,
2281    pointer: TypedPointer<TestScript, Vec<TestScriptTest>>,
2282    options: Arc<TestRunnerOptions>,
2283) -> Result<TestResult<Vec<TestReportTest>>, TestScriptError> {
2284    let mut test_results = vec![];
2285    let mut cur_state = state;
2286
2287    let Some(tests) = pointer.value() else {
2288        return Ok(TestResult {
2289            state: cur_state,
2290            value: test_results,
2291        });
2292    };
2293
2294    for test in tests.iter().enumerate() {
2295        let Some(test_pointer) = pointer.descend(&Key::Index(test.0)) else {
2296            return Err(TestScriptError::ExecutionError(format!(
2297                "Failed to retrieve TestScript test at index {}.",
2298                test.0
2299            )));
2300        };
2301        let test_result = run_test(
2302            client,
2303            ctx.clone(),
2304            cur_state,
2305            test_pointer,
2306            options.clone(),
2307        )
2308        .await?;
2309        cur_state = test_result.state;
2310        test_results.push(test_result.value);
2311    }
2312
2313    Ok(TestResult {
2314        state: cur_state,
2315        value: test_results,
2316    })
2317}
2318
2319pub struct TestRunnerOptions {
2320    pub wait_between_operations: Option<Duration>,
2321    /// Before a request that reads the search index (a search, conditional
2322    /// delete or conditional update), wait up to this long for the latest
2323    /// write to be indexed. Waits only as long as indexing takes, where
2324    /// `wait_between_operations` waits a fixed time after every operation.
2325    pub index_wait_timeout: Option<Duration>,
2326}
2327
2328/// Runs a FHIR `TestScript` using the provided client and execution context.
2329///
2330/// This executes the `TestScript` lifecycle:
2331/// - fixture setup
2332/// - setup actions
2333/// - test actions
2334/// - teardown actions
2335///
2336/// # Errors
2337///
2338/// Returns [`TestScriptError`] if:
2339/// - fixture setup fails
2340/// - setup actions fail
2341/// - test execution fails
2342/// - teardown execution fails
2343/// - an operation performed by the FHIR client fails
2344#[tracing::instrument(
2345    name = "testscript_run",
2346    skip_all,
2347    fields(
2348        testscript.id = test_script.id.as_deref().unwrap_or("<no-id>"),
2349        testscript.name = test_script.name.value.as_deref().unwrap_or("<unnamed>"),
2350        testscript.url = test_script.url.value.as_deref().unwrap_or("<no-url>"),
2351    )
2352)]
2353pub async fn run<CTX: Clone + WithRequestHeaders, Client: FHIRHttpExchange<CTX>>(
2354    client: &Client,
2355    ctx: CTX,
2356    test_script: Arc<TestScript>,
2357    options: Arc<TestRunnerOptions>,
2358) -> Result<TestReport, TestScriptError> {
2359    tracing::info!("Starting TestScript run");
2360
2361    let mut test_report = TestReport {
2362        status: ReportStatusCodes::completed(),
2363        testScript: Box::new(Reference {
2364            reference: Some(Box::new(FHIRString {
2365                value: Some(format!(
2366                    "Testscript/{}",
2367                    test_script.id.clone().unwrap_or_default()
2368                )),
2369                ..Default::default()
2370            })),
2371            ..Default::default()
2372        }),
2373        ..Default::default()
2374    };
2375
2376    let mut state = Arc::new(Mutex::new(TestState::new()));
2377    let pointer = TypedPointer::<TestScript, TestScript>::new(test_script);
2378
2379    state = setup_fixtures(client, ctx.clone(), state, pointer.clone(), options.clone())
2380        .await
2381        .map_err(TestScriptError::OperationError)?;
2382
2383    let mut running_state = Ok(());
2384
2385    // Run setup actions
2386    if let Some(setup_pointer) =
2387        pointer.descend::<TestScriptSetup>(&Key::Field("setup".to_string()))
2388    {
2389        tracing::info!("Running TestScript setup...");
2390        let setup_result = run_setup(
2391            client,
2392            ctx.clone(),
2393            state.clone(),
2394            setup_pointer,
2395            options.clone(),
2396        )
2397        .await;
2398        match setup_result {
2399            Ok(res) => {
2400                state = res.state;
2401                test_report.setup = Some(res.value);
2402            }
2403            Err(e) => {
2404                tracing::error!(phase = "setup", error = ?e, "TestScript run failed during setup");
2405                running_state = Err(e);
2406            }
2407        }
2408    }
2409
2410    // Run Test actions
2411    if running_state.is_ok()
2412        && let Some(test_pointer) =
2413            pointer.descend::<Vec<TestScriptTest>>(&Key::Field("test".to_string()))
2414    {
2415        tracing::info!("Running TestScript tests...");
2416        let test_result = run_tests(
2417            client,
2418            ctx.clone(),
2419            state.clone(),
2420            test_pointer,
2421            options.clone(),
2422        )
2423        .await;
2424
2425        match test_result {
2426            Ok(res) => {
2427                state = res.state;
2428                test_report.test = Some(res.value);
2429            }
2430
2431            Err(e) => {
2432                tracing::error!(phase = "test", error = ?e, "TestScript run failed during test execution");
2433                running_state = Err(e);
2434            }
2435        }
2436    }
2437
2438    if let Some(teardown_pointer) =
2439        pointer.descend::<TestScriptTeardown>(&Key::Field("teardown".to_string()))
2440    {
2441        tracing::info!("Running TestScript teardown...");
2442
2443        let result = run_teardown(
2444            client,
2445            ctx.clone(),
2446            state.clone(),
2447            teardown_pointer,
2448            options.clone(),
2449        )
2450        .await?;
2451
2452        // state = result.state;
2453        test_report.teardown = Some(result.value);
2454    }
2455
2456    running_state?;
2457
2458    let state_guard = state.lock().await;
2459    // Only set result to fail so if still pending can assume pass.
2460    // Flip to fail in assertion tests if any fail.
2461    match &state_guard.result {
2462        state if state == &ReportResultCodes::pending() => {
2463            test_report.result = ReportResultCodes::pass();
2464        }
2465        status => test_report.result = status.clone(),
2466    }
2467
2468    if test_report.result == ReportResultCodes::fail() {
2469        tracing::error!(result = ?test_report.result, "TestScript run finished with failures");
2470    } else {
2471        tracing::info!(result = ?test_report.result, "TestScript run finished");
2472    }
2473
2474    Ok(test_report)
2475}
2476
2477#[cfg(test)]
2478mod tests {
2479    use super::*;
2480    use haste_fhir_client::request::{FHIRCreateResponse, FHIRDeleteInstanceResponse};
2481
2482    fn patient(id: &str, version_id: &str) -> Resource {
2483        serde_json::from_value(serde_json::json!({
2484            "resourceType": "Patient",
2485            "id": id,
2486            "meta": { "versionId": version_id }
2487        }))
2488        .expect("valid Patient")
2489    }
2490
2491    #[test]
2492    fn writes_record_the_version_search_must_return() {
2493        let created = FHIRResponse::Create(FHIRCreateResponse {
2494            resource: patient("p1", "v1"),
2495        });
2496        let write = indexed_write(&created).expect("a create is a write");
2497        assert_eq!(write.resource_type, ResourceType::Patient);
2498        assert_eq!(write.id, "p1");
2499        assert_eq!(write.version_id.as_deref(), Some("v1"));
2500
2501        // A delete must be gone from search, whatever version it was.
2502        let deleted = FHIRResponse::Delete(DeleteResponse::Instance(Box::new(
2503            FHIRDeleteInstanceResponse {
2504                resource: patient("p1", "v2"),
2505            },
2506        )));
2507        assert_eq!(
2508            indexed_write(&deleted)
2509                .expect("a delete is a write")
2510                .version_id,
2511            None
2512        );
2513    }
2514
2515    #[test]
2516    fn only_index_backed_requests_wait() {
2517        let search = FHIRRequest::Search(SearchRequest::Type(FHIRSearchTypeRequest {
2518            resource_type: ResourceType::Patient,
2519            parameters: ParsedParameters::try_from("_tag=x").unwrap(),
2520        }));
2521        assert!(reads_search_index(&search));
2522
2523        let read = FHIRRequest::Read(FHIRReadRequest {
2524            resource_type: ResourceType::Patient,
2525            id: "p1".to_string(),
2526        });
2527        assert!(!reads_search_index(&read), "a read goes to the repository");
2528    }
2529}