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