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