azure_data_cosmos_driver 0.6.0

Core implementation layer for Azure Cosmos DB - provides transport, routing, and protocol handling for cross-language SDK reuse
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

//! Preview Distributed Transaction wire models.
//!
//! **Preview / work in progress.** Gated behind the disabled-by-default
//! `preview_dtx` feature; the DTX service feature is not yet generally
//! available. These types may change or be removed without notice and are
//! **not supported for production use**.

use std::{borrow::Cow, sync::Arc};

use azure_core::fmt::SafeDebug;
use azure_core::http::headers::AsHeaders;
use azure_core::Bytes;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::diagnostics::DiagnosticsContext;
use crate::models::{
    ActivityId, ContainerReference, CosmosResponseHeaders, PartitionKey, Precondition,
    RequestCharge, SessionToken,
};

/// The coordinator behavior requested for a distributed transaction.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum DistributedTransactionType {
    /// Atomic write transaction.
    Write,
    /// Snapshot read transaction.
    Read,
}

impl DistributedTransactionType {
    /// Returns the string representation of this transaction type.
    ///
    /// This is a diagnostic/display label that mirrors the variant name. It is
    /// **not** the coordinator wire value: the `x-ms-cosmos-operation-type`
    /// header sends `CommitDistributedTransaction` for [`Self::Write`] and
    /// `Read` for [`Self::Read`] (built in `execute_distributed_transaction`).
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Write => "Write",
            Self::Read => "Read",
        }
    }
}

impl std::fmt::Display for DistributedTransactionType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl AsRef<str> for DistributedTransactionType {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

/// The per-item operation kind in a distributed transaction.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DistributedTransactionOperationKind {
    /// Create an item.
    Create,
    /// Read an item.
    Read,
    /// Replace an item.
    Replace,
    /// Upsert an item.
    Upsert,
    /// Delete an item.
    Delete,
    /// Patch an item.
    Patch,
}

impl DistributedTransactionOperationKind {
    pub(crate) fn as_wire_str(self) -> &'static str {
        match self {
            Self::Create => "Create",
            Self::Read => "Read",
            Self::Replace => "Replace",
            Self::Upsert => "Upsert",
            Self::Delete => "Delete",
            Self::Patch => "Patch",
        }
    }
}

/// A resolved item target for a distributed transaction operation.
#[derive(Clone, SafeDebug)]
#[safe(true)]
#[non_exhaustive]
pub struct DistributedTransactionTarget {
    /// Resolved target container.
    pub container: ContainerReference,
    /// Item partition key.
    pub partition_key: PartitionKey,
    /// Item id.
    pub id: Cow<'static, str>,
}

impl DistributedTransactionTarget {
    /// Creates a target from resolved container metadata, partition key, and item id.
    pub fn new(
        container: ContainerReference,
        partition_key: impl Into<PartitionKey>,
        id: impl Into<Cow<'static, str>>,
    ) -> Self {
        Self {
            container,
            partition_key: partition_key.into(),
            id: id.into(),
        }
    }
}

/// A single operation buffered into a distributed transaction request.
#[derive(Clone, SafeDebug)]
#[safe(true)]
#[non_exhaustive]
pub struct DistributedTransactionOperation {
    /// Operation type.
    pub kind: DistributedTransactionOperationKind,
    /// Operation target.
    pub target: DistributedTransactionTarget,
    /// Optional raw JSON resource body.
    pub resource_body: Option<Bytes>,
    /// Optional per-operation session token.
    pub session_token: Option<SessionToken>,
    /// Optional ETag precondition.
    pub precondition: Option<Precondition>,
    /// Optional SQL predicate for server-side conditional PATCH.
    pub patch_filter_predicate: Option<Cow<'static, str>>,
}

impl DistributedTransactionOperation {
    /// Creates an operation for the given kind and target.
    pub fn new(
        kind: DistributedTransactionOperationKind,
        target: DistributedTransactionTarget,
    ) -> Self {
        Self {
            kind,
            target,
            resource_body: None,
            session_token: None,
            precondition: None,
            patch_filter_predicate: None,
        }
    }

    /// Sets the raw JSON resource body.
    pub fn with_resource_body(mut self, body: impl Into<Bytes>) -> Self {
        self.resource_body = Some(body.into());
        self
    }

    /// Sets the per-operation session token.
    pub fn with_session_token(mut self, session_token: impl Into<SessionToken>) -> Self {
        self.session_token = Some(session_token.into());
        self
    }

    /// Sets the ETag precondition.
    pub fn with_precondition(mut self, precondition: Precondition) -> Self {
        self.precondition = Some(precondition);
        self
    }

    /// Sets a SQL predicate for conditional PATCH operations.
    pub fn with_patch_filter_predicate(mut self, predicate: impl Into<Cow<'static, str>>) -> Self {
        self.patch_filter_predicate = Some(predicate.into());
        self
    }
}

/// Immutable distributed transaction request data.
#[derive(Clone, SafeDebug)]
#[safe(true)]
#[non_exhaustive]
pub struct DistributedTransactionRequest {
    /// Transaction type.
    pub transaction_type: DistributedTransactionType,
    /// Operations to send to the coordinator.
    pub operations: Vec<DistributedTransactionOperation>,
    /// Idempotency token generated once and reused across driver retries.
    pub idempotency_token: Uuid,
}

impl DistributedTransactionRequest {
    /// Creates a request with a fresh idempotency token.
    pub fn new(
        transaction_type: DistributedTransactionType,
        operations: Vec<DistributedTransactionOperation>,
    ) -> Self {
        Self {
            transaction_type,
            operations,
            idempotency_token: Uuid::new_v4(),
        }
    }

    /// Serializes the request body expected by the current .NET-compatible DTX endpoint.
    pub fn serialize_body(&self) -> crate::error::Result<Vec<u8>> {
        self.validate()?;

        let operations = self
            .operations
            .iter()
            .enumerate()
            .map(serialize_operation)
            .collect::<crate::error::Result<Vec<_>>>()?;

        serde_json::to_vec(&serde_json::json!({ "operations": operations })).map_err(|error| {
            crate::error::CosmosError::builder()
                .with_status(crate::error::CosmosStatus::SERIALIZATION_RESPONSE_BODY_INVALID)
                .with_message("failed to serialize distributed transaction request body")
                .with_source(error)
                .build()
        })
    }

    fn validate(&self) -> crate::error::Result<()> {
        for operation in &self.operations {
            match (self.transaction_type, operation.kind) {
                (DistributedTransactionType::Read, DistributedTransactionOperationKind::Read) => {}
                (DistributedTransactionType::Read, other) => {
                    return Err(invalid_dtx_request(format!(
                        "distributed read transaction cannot contain {} operations",
                        other.as_wire_str()
                    )));
                }
                (DistributedTransactionType::Write, DistributedTransactionOperationKind::Read) => {
                    return Err(invalid_dtx_request(
                        "distributed write transaction cannot contain Read operations",
                    ));
                }
                (DistributedTransactionType::Write, _) => {}
            }
            if operation.kind.requires_resource_body() && operation.resource_body.is_none() {
                return Err(invalid_dtx_request(format!(
                    "distributed transaction {} operations require resourceBody",
                    operation.kind.as_wire_str()
                )));
            }
        }
        Ok(())
    }
}

impl DistributedTransactionOperationKind {
    fn requires_resource_body(self) -> bool {
        matches!(
            self,
            Self::Create | Self::Replace | Self::Upsert | Self::Patch
        )
    }
}

fn invalid_dtx_request(message: impl Into<String>) -> crate::error::CosmosError {
    crate::error::CosmosError::builder()
        .with_status(crate::error::CosmosStatus::new(
            azure_core::http::StatusCode::BadRequest,
        ))
        .with_message(message.into())
        .build()
}

fn serialize_operation(
    (index, operation): (usize, &DistributedTransactionOperation),
) -> crate::error::Result<serde_json::Value> {
    let mut object = serde_json::Map::new();
    let target = &operation.target;

    object.insert(
        "databaseName".to_owned(),
        serde_json::Value::String(target.container.database_name().to_owned()),
    );
    object.insert(
        "collectionName".to_owned(),
        serde_json::Value::String(target.container.name().to_owned()),
    );
    object.insert(
        "id".to_owned(),
        serde_json::Value::String(target.id.to_string()),
    );
    object.insert(
        "collectionResourceId".to_owned(),
        serde_json::Value::String(target.container.rid().to_owned()),
    );
    object.insert(
        "databaseResourceId".to_owned(),
        serde_json::Value::String(target.container.database_rid().to_owned()),
    );
    object.insert("partitionKey".to_owned(), partition_key_json(target)?);
    object.insert("index".to_owned(), serde_json::json!(index));

    if let Some(body) = operation.resource_body.as_ref() {
        let mut resource_body =
            serde_json::from_slice::<serde_json::Value>(body).map_err(|error| {
                crate::error::CosmosError::builder()
                    .with_status(crate::error::CosmosStatus::SERIALIZATION_RESPONSE_BODY_INVALID)
                    .with_message(
                        "distributed transaction operation resource body must be valid JSON",
                    )
                    .with_source(error)
                    .build()
            })?;
        if let Some(predicate) = operation.patch_filter_predicate.as_ref() {
            match &mut resource_body {
                serde_json::Value::Object(map) => {
                    map.insert(
                        "condition".to_owned(),
                        serde_json::Value::String(predicate.to_string()),
                    );
                }
                _ => {
                    return Err(crate::error::CosmosError::builder()
                        .with_status(crate::error::CosmosStatus::new(
                            azure_core::http::StatusCode::BadRequest,
                        ))
                        .with_message(
                            "distributed transaction patch resource body must be a JSON object when a filter predicate is set",
                        )
                        .build());
                }
            }
        }
        validate_resource_body_id(operation, &resource_body)?;
        object.insert("resourceBody".to_owned(), resource_body);
    }

    if let Some(session_token) = operation.session_token.as_ref() {
        if !session_token.as_str().trim().is_empty() {
            object.insert(
                "sessionToken".to_owned(),
                serde_json::Value::String(session_token.as_str().to_owned()),
            );
        }
    }

    if let Some(precondition) = operation.precondition.as_ref() {
        match precondition {
            Precondition::IfMatch(etag) => {
                object.insert(
                    "ifMatch".to_owned(),
                    serde_json::Value::String(etag.to_string()),
                );
            }
            Precondition::IfNoneMatch(etag) => {
                object.insert(
                    "ifNoneMatch".to_owned(),
                    serde_json::Value::String(etag.to_string()),
                );
            }
        }
    }

    object.insert(
        "operationType".to_owned(),
        serde_json::Value::String(operation.kind.as_wire_str().to_owned()),
    );
    object.insert(
        "resourceType".to_owned(),
        serde_json::Value::String("Document".to_owned()),
    );

    Ok(serde_json::Value::Object(object))
}

fn validate_resource_body_id(
    operation: &DistributedTransactionOperation,
    resource_body: &serde_json::Value,
) -> crate::error::Result<()> {
    if !matches!(
        operation.kind,
        DistributedTransactionOperationKind::Create
            | DistributedTransactionOperationKind::Replace
            | DistributedTransactionOperationKind::Upsert
    ) {
        return Ok(());
    }

    match resource_body.get("id").and_then(|value| value.as_str()) {
        Some(body_id) if body_id == operation.target.id.as_ref() => Ok(()),
        Some(body_id) => Err(invalid_dtx_request(format!(
            "distributed transaction operation resourceBody.id ('{body_id}') must match operation id ('{}')",
            operation.target.id
        ))),
        None => Err(invalid_dtx_request(
            "distributed transaction create, replace, and upsert operations require resourceBody.id",
        )),
    }
}

fn partition_key_json(
    target: &DistributedTransactionTarget,
) -> crate::error::Result<serde_json::Value> {
    let (_, value) = target
        .partition_key
        .as_headers()?
        .next()
        .ok_or_else(|| invalid_partition_key("partition key did not produce a header value"))?;
    let text = value.as_str();
    serde_json::from_str(text).map_err(|error| {
        crate::error::CosmosError::builder()
            .with_status(crate::error::CosmosStatus::new(
                azure_core::http::StatusCode::BadRequest,
            ))
            .with_message("distributed transaction operations require a non-empty partition key")
            .with_source(error)
            .build()
    })
}

fn invalid_partition_key(message: impl Into<String>) -> crate::error::CosmosError {
    let message = message.into();
    crate::error::CosmosError::builder()
        .with_status(crate::error::CosmosStatus::new(
            azure_core::http::StatusCode::BadRequest,
        ))
        .with_message(message)
        .build()
}

/// The resource body returned for one distributed transaction operation.
#[derive(Clone, SafeDebug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DistributedTransactionResultBody {
    /// No resource body was returned.
    #[default]
    None,
    /// Raw JSON payload returned by the coordinator.
    Bytes(Bytes),
}

/// Result of one operation in a distributed transaction response.
#[derive(Clone, SafeDebug)]
#[safe(true)]
#[non_exhaustive]
pub struct DistributedTransactionOperationResult {
    /// Raw operation response object returned by the coordinator.
    #[safe(false)]
    pub raw_response: serde_json::Map<String, serde_json::Value>,
    /// Zero-based request operation index.
    pub index: usize,
    /// HTTP status code for this operation.
    pub status_code: azure_core::http::StatusCode,
    /// Cosmos sub-status code, when present.
    pub sub_status_code: Option<crate::models::SubStatusCode>,
    /// ETag returned for this operation, when present.
    pub etag: Option<azure_core::http::Etag>,
    /// Per-operation session token returned by the coordinator, when present.
    pub session_token: Option<SessionToken>,
    /// Partition key range id returned by the coordinator, when present.
    pub partition_key_range_id: Option<String>,
    /// Request charge for this operation.
    pub request_charge: Option<RequestCharge>,
    /// Operation resource body.
    #[safe(false)]
    pub resource_body: DistributedTransactionResultBody,
}

impl DistributedTransactionOperationResult {
    /// Returns `true` when the operation status is in the 2xx range.
    pub fn is_success_status_code(&self) -> bool {
        self.status_code.is_success()
    }

    /// Returns `true` when the operation is a completed DTX success outcome.
    ///
    /// Read transactions treat `304 NotModified` as a completed success code
    /// (no body), even though it is not in the HTTP 2xx success range.
    pub fn is_completed_status_code(&self) -> bool {
        is_dtx_completed_status_code(self.status_code)
    }
}

/// Response returned by the distributed transaction coordinator.
#[derive(Clone, SafeDebug)]
#[safe(true)]
#[non_exhaustive]
pub struct DistributedTransactionResponse {
    /// Overall transaction status.
    pub status_code: azure_core::http::StatusCode,
    /// Overall transaction sub-status, when present.
    pub sub_status_code: Option<crate::models::SubStatusCode>,
    /// Operation results ordered by request index.
    pub operation_results: Vec<DistributedTransactionOperationResult>,
    /// Idempotency token used for write retries.
    pub idempotency_token: Uuid,
    /// Parsed Cosmos response headers returned by the coordinator.
    pub headers: CosmosResponseHeaders,
    /// Activity ID returned by the service, when present.
    pub activity_id: Option<ActivityId>,
    /// Total request charge returned by the service, when present.
    pub request_charge: Option<RequestCharge>,
    /// Retry-after hint in milliseconds, when present.
    pub retry_after_ms: Option<u64>,
    /// Diagnostics captured while executing the transaction.
    pub diagnostics: Option<Arc<DiagnosticsContext>>,
    /// Whether the coordinator says the same idempotency token can be retried.
    pub is_retriable: bool,
    /// Coordinator diagnostic string, when present.
    pub diagnostic_string: Option<String>,
    /// Error message synthesized by the driver for malformed coordinator responses.
    pub error_message: Option<String>,
}

impl DistributedTransactionResponse {
    /// Parses a coordinator response body into a distributed transaction response.
    pub fn from_body(
        status_code: azure_core::http::StatusCode,
        sub_status_code: Option<crate::models::SubStatusCode>,
        body: &[u8],
        operation_count: usize,
        idempotency_token: Uuid,
    ) -> Self {
        let is_success_envelope = is_dtx_completed_status_code(status_code);
        let mut is_retriable = false;
        let mut diagnostic_string = None;

        if body.is_empty() {
            return Self::fallback(
                is_success_envelope,
                status_code,
                sub_status_code,
                operation_count,
                idempotency_token,
                is_retriable,
                diagnostic_string,
                None,
                "server response deserialization failure",
            );
        }

        let root: serde_json::Value = match serde_json::from_slice(body) {
            Ok(root) => root,
            Err(_) => {
                return Self::fallback(
                    is_success_envelope,
                    status_code,
                    sub_status_code,
                    operation_count,
                    idempotency_token,
                    is_retriable,
                    diagnostic_string,
                    None,
                    "server response deserialization failure",
                );
            }
        };

        is_retriable = get_property(&root, "isRetriable")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        diagnostic_string = get_property(&root, "diagnosticString")
            .and_then(|v| v.as_str())
            .map(ToOwned::to_owned);
        // `get_property` already matches keys case-insensitively, so a single
        // lookup per name covers both `message`/`Message` and `code`/`Code`.
        let service_error_message = get_property(&root, "message")
            .and_then(|v| v.as_str())
            .map(ToOwned::to_owned)
            .or_else(|| {
                get_property(&root, "code")
                    .and_then(|v| v.as_str())
                    .map(ToOwned::to_owned)
            });

        let operation_responses =
            get_property(&root, "operationResponses").and_then(|v| v.as_array());
        let Some(operation_responses) = operation_responses else {
            return Self::fallback(
                is_success_envelope,
                status_code,
                sub_status_code,
                operation_count,
                idempotency_token,
                is_retriable,
                diagnostic_string,
                service_error_message,
                "invalid server response",
            );
        };

        let mut parsed_results = Vec::with_capacity(operation_responses.len());
        for value in operation_responses {
            match parse_operation_result(value) {
                Ok(result) => parsed_results.push(result),
                Err(()) => {
                    return Self::fallback(
                        is_success_envelope,
                        status_code,
                        sub_status_code,
                        operation_count,
                        idempotency_token,
                        is_retriable,
                        diagnostic_string,
                        service_error_message,
                        "server response deserialization failure",
                    );
                }
            }
        }

        if parsed_results.len() != operation_count {
            return Self::fallback(
                is_success_envelope,
                status_code,
                sub_status_code,
                operation_count,
                idempotency_token,
                is_retriable,
                diagnostic_string,
                service_error_message,
                "invalid server response",
            );
        }

        let Some(mut ordered) = reorder_results(parsed_results, operation_count) else {
            return Self::fallback(
                is_success_envelope,
                status_code,
                sub_status_code,
                operation_count,
                idempotency_token,
                is_retriable,
                diagnostic_string,
                service_error_message,
                "server response deserialization failure",
            );
        };

        // Promote the MultiStatus envelope AFTER reordering so the promoted
        // status reflects the first failing operation in *request* order,
        // matching .NET's DistributedTransactionResponse (PR #5974). Promoting
        // before reorder would select the first failure in wire order, which
        // differs when the coordinator returns results out of order.
        let (status_code, sub_status_code) =
            promote_multistatus(status_code, sub_status_code, &ordered);

        Self {
            status_code,
            sub_status_code,
            operation_results: std::mem::take(&mut ordered),
            idempotency_token,
            headers: CosmosResponseHeaders::default(),
            activity_id: None,
            request_charge: None,
            retry_after_ms: None,
            diagnostics: None,
            is_retriable,
            diagnostic_string,
            error_message: None,
        }
    }

    /// Returns `true` when the overall status is in the 2xx range.
    pub fn is_success_status_code(&self) -> bool {
        self.status_code.is_success()
    }

    /// Returns `true` when the overall transaction completed successfully.
    ///
    /// This differs from [`Self::is_success_status_code`] for read transactions:
    /// an all-`304 NotModified` snapshot is complete and terminal, but `304` is
    /// not an HTTP 2xx success status.
    pub fn is_completed_status_code(&self) -> bool {
        is_dtx_completed_status_code(self.status_code)
    }

    /// Number of operation results.
    pub fn len(&self) -> usize {
        self.operation_results.len()
    }

    /// Returns `true` when there are no operation results.
    pub fn is_empty(&self) -> bool {
        self.operation_results.is_empty()
    }

    pub(crate) fn with_response_headers(mut self, headers: &CosmosResponseHeaders) -> Self {
        self.headers = headers.clone();
        if let Some(token) = headers.distributed_transaction_idempotency_token {
            self.idempotency_token = token;
        }
        self.activity_id = headers.activity_id.clone();
        self.request_charge = headers.request_charge;
        self.retry_after_ms = headers.retry_after_ms;
        self
    }

    pub(crate) fn with_diagnostics(mut self, diagnostics: Arc<DiagnosticsContext>) -> Self {
        self.diagnostics = Some(diagnostics);
        self
    }

    /// Builds a fail-closed / padded response for an uninterpretable coordinator body.
    ///
    /// On a completed-status envelope (`is_success_envelope`) an unparseable body
    /// is treated as a hard failure and synthesized as `500 InternalServerError`
    /// (`fail_closed`); on a non-success envelope the results are padded with the
    /// envelope status so the caller still sees one result per operation.
    #[allow(clippy::too_many_arguments)]
    fn fallback(
        is_success_envelope: bool,
        status_code: azure_core::http::StatusCode,
        sub_status_code: Option<crate::models::SubStatusCode>,
        operation_count: usize,
        idempotency_token: Uuid,
        is_retriable: bool,
        diagnostic_string: Option<String>,
        service_error_message: Option<String>,
        reason: &'static str,
    ) -> Self {
        if is_success_envelope {
            Self::fail_closed(
                operation_count,
                idempotency_token,
                is_retriable,
                diagnostic_string,
                reason,
            )
        } else {
            Self::padded(
                status_code,
                sub_status_code,
                operation_count,
                idempotency_token,
                is_retriable,
                diagnostic_string,
                service_error_message,
            )
        }
    }

    fn fail_closed(
        operation_count: usize,
        idempotency_token: Uuid,
        is_retriable: bool,
        diagnostic_string: Option<String>,
        error_message: impl Into<String>,
    ) -> Self {
        Self::padded(
            azure_core::http::StatusCode::InternalServerError,
            None,
            operation_count,
            idempotency_token,
            is_retriable,
            diagnostic_string,
            Some(error_message.into()),
        )
    }

    fn padded(
        status_code: azure_core::http::StatusCode,
        sub_status_code: Option<crate::models::SubStatusCode>,
        operation_count: usize,
        idempotency_token: Uuid,
        is_retriable: bool,
        diagnostic_string: Option<String>,
        error_message: Option<String>,
    ) -> Self {
        let operation_results = (0..operation_count)
            .map(|index| DistributedTransactionOperationResult {
                raw_response: raw_operation_response(index, status_code, sub_status_code),
                index,
                status_code,
                sub_status_code,
                etag: None,
                session_token: None,
                partition_key_range_id: None,
                request_charge: None,
                resource_body: DistributedTransactionResultBody::None,
            })
            .collect();

        Self {
            status_code,
            sub_status_code,
            operation_results,
            idempotency_token,
            headers: CosmosResponseHeaders::default(),
            activity_id: None,
            request_charge: None,
            retry_after_ms: None,
            diagnostics: None,
            is_retriable,
            diagnostic_string,
            error_message,
        }
    }
}

fn get_property<'a>(value: &'a serde_json::Value, name: &str) -> Option<&'a serde_json::Value> {
    value.as_object()?.iter().find_map(|(key, value)| {
        if key.eq_ignore_ascii_case(name) {
            Some(value)
        } else {
            None
        }
    })
}

fn is_dtx_completed_status_code(status_code: azure_core::http::StatusCode) -> bool {
    status_code.is_success() || status_code == azure_core::http::StatusCode::NotModified
}

fn parse_operation_result(
    value: &serde_json::Value,
) -> Result<DistributedTransactionOperationResult, ()> {
    let raw_response = value.as_object().cloned().ok_or(())?;
    let index = get_property(value, "index")
        .and_then(|v| v.as_u64())
        .and_then(|v| usize::try_from(v).ok())
        .ok_or(())?;
    let status_code = get_property(value, "statusCode")
        .and_then(|v| v.as_u64())
        .and_then(|v| u16::try_from(v).ok())
        .map(azure_core::http::StatusCode::from)
        .ok_or(())?;
    let sub_status_code = get_property(value, "subStatusCode")
        .and_then(|v| v.as_u64())
        .and_then(|v| u16::try_from(v).ok())
        .map(crate::models::SubStatusCode::new);
    let etag = get_property(value, "etag")
        .and_then(|v| v.as_str())
        .map(|s| azure_core::http::Etag::from(s.to_owned()));
    let session_token = get_property(value, "sessionToken")
        .and_then(|v| v.as_str())
        .map(|s| SessionToken::new(s.to_owned()));
    let partition_key_range_id = get_property(value, "partitionKeyRangeId")
        .and_then(|v| v.as_str())
        .map(ToOwned::to_owned);
    let request_charge = get_property(value, "requestCharge")
        .and_then(|v| v.as_f64())
        .map(RequestCharge::new);
    let resource_body = match get_property(value, "resourceBody") {
        Some(serde_json::Value::Null) | None => DistributedTransactionResultBody::None,
        Some(body) => {
            if !body.is_object() {
                return Err(());
            }
            let bytes = serde_json::to_vec(body).map_err(|_| ())?;
            DistributedTransactionResultBody::Bytes(Bytes::from(bytes))
        }
    };

    Ok(DistributedTransactionOperationResult {
        raw_response,
        index,
        status_code,
        sub_status_code,
        etag,
        session_token,
        partition_key_range_id,
        request_charge,
        resource_body,
    })
}

fn raw_operation_response(
    index: usize,
    status_code: azure_core::http::StatusCode,
    sub_status_code: Option<crate::models::SubStatusCode>,
) -> serde_json::Map<String, serde_json::Value> {
    let mut raw_response = serde_json::Map::new();
    raw_response.insert("index".to_owned(), serde_json::json!(index));
    raw_response.insert(
        "statusCode".to_owned(),
        serde_json::json!(u16::from(status_code)),
    );
    if let Some(sub_status_code) = sub_status_code {
        raw_response.insert(
            "subStatusCode".to_owned(),
            serde_json::json!(sub_status_code.value()),
        );
    }
    raw_response
}

fn reorder_results(
    results: Vec<DistributedTransactionOperationResult>,
    operation_count: usize,
) -> Option<Vec<DistributedTransactionOperationResult>> {
    let mut ordered = vec![None; operation_count];
    for result in results {
        let index = result.index;
        if index >= operation_count || ordered[index].is_some() {
            return None;
        }
        ordered[index] = Some(result);
    }
    ordered.into_iter().collect()
}

/// Promotes a `207 MultiStatus` envelope to the status of the first failing
/// operation (status `>= 400`, excluding `424 FailedDependency`).
///
/// Callers must pass results already reordered by request index so promotion
/// selects the first failure in *request* order, matching .NET (PR #5974).
/// Non-`207` envelopes are returned unchanged.
fn promote_multistatus(
    status_code: azure_core::http::StatusCode,
    sub_status_code: Option<crate::models::SubStatusCode>,
    results: &[DistributedTransactionOperationResult],
) -> (
    azure_core::http::StatusCode,
    Option<crate::models::SubStatusCode>,
) {
    if u16::from(status_code) != 207 {
        return (status_code, sub_status_code);
    }

    for result in results {
        let code = u16::from(result.status_code);
        if code != 424 && code >= 400 {
            return (result.status_code, result.sub_status_code);
        }
    }

    (status_code, sub_status_code)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::{
        AccountReference, ContainerProperties, PartitionKeyDefinition, SystemProperties,
    };
    use azure_core::http::Etag;
    use url::Url;

    fn container() -> ContainerReference {
        let account = AccountReference::with_master_key(
            Url::parse("https://example.documents.azure.com:443/").unwrap(),
            "dGVzdA==",
        );
        let pk_def: PartitionKeyDefinition = serde_json::from_str(r#"{"paths":["/pk"]}"#).unwrap();
        let properties = ContainerProperties {
            id: "coll".into(),
            partition_key: pk_def,
            system_properties: SystemProperties::default(),
        };
        ContainerReference::new(account, "db", "db_rid", "coll", "coll_rid", &properties)
    }

    fn target(id: &str) -> DistributedTransactionTarget {
        DistributedTransactionTarget::new(container(), PartitionKey::from("pk1"), id.to_owned())
    }

    #[test]
    fn serialize_create_operation() {
        let operation = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Create,
            target("item1"),
        )
        .with_resource_body(Bytes::from_static(br#"{"id":"item1","pk":"pk1"}"#))
        .with_session_token("0:1#9#4=8")
        .with_precondition(Precondition::if_match(Etag::from("\"etag\"")));
        let request =
            DistributedTransactionRequest::new(DistributedTransactionType::Write, vec![operation]);

        let body = request.serialize_body().unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        let op = &json["operations"][0];

        assert_eq!(op["databaseName"], "db");
        assert_eq!(op["collectionName"], "coll");
        assert_eq!(op["collectionResourceId"], "coll_rid");
        assert_eq!(op["databaseResourceId"], "db_rid");
        assert_eq!(op["partitionKey"], serde_json::json!(["pk1"]));
        assert_eq!(op["index"], 0);
        assert_eq!(op["resourceBody"]["id"], "item1");
        assert_eq!(op["sessionToken"], "0:1#9#4=8");
        assert_eq!(op["ifMatch"], "\"etag\"");
        assert_eq!(op["operationType"], "Create");
        assert_eq!(op["resourceType"], "Document");
        assert!(json.get("operationType").is_none());
    }

    #[test]
    fn serialize_read_omits_body_and_empty_session_token() {
        let operation = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Read,
            target("item1"),
        )
        .with_session_token("   ")
        .with_precondition(Precondition::if_none_match(Etag::from("\"etag\"")));
        let request =
            DistributedTransactionRequest::new(DistributedTransactionType::Read, vec![operation]);

        let body = request.serialize_body().unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        let op = &json["operations"][0];

        assert!(op.get("resourceBody").is_none());
        assert!(op.get("sessionToken").is_none());
        assert_eq!(op["ifNoneMatch"], "\"etag\"");
        assert_eq!(op["operationType"], "Read");
    }

    #[test]
    fn serialize_patch_adds_condition() {
        let operation = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Patch,
            target("item1"),
        )
        .with_resource_body(Bytes::from_static(br#"{"operations":[]}"#))
        .with_patch_filter_predicate("from c where c.status = 'pending'");
        let request =
            DistributedTransactionRequest::new(DistributedTransactionType::Write, vec![operation]);

        let body = request.serialize_body().unwrap();
        let json: serde_json::Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(
            json["operations"][0]["resourceBody"]["condition"],
            "from c where c.status = 'pending'"
        );
    }

    #[test]
    fn parse_out_of_order_results_reorders_by_index() {
        let body = br#"{"operationResponses":[{"index":2,"statusCode":204},{"index":0,"statusCode":201,"Etag":"e0"},{"index":1,"statusCode":200}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::Ok,
            None,
            body,
            3,
            Uuid::nil(),
        );

        assert_eq!(response.status_code, azure_core::http::StatusCode::Ok);
        assert_eq!(response.operation_results[0].index, 0);
        assert_eq!(response.operation_results[1].index, 1);
        assert_eq!(response.operation_results[2].index, 2);
        assert_eq!(
            response.operation_results[0].status_code,
            azure_core::http::StatusCode::Created
        );
    }

    #[test]
    fn parse_duplicate_index_success_fails_closed() {
        let body = br#"{"isRetriable":true,"diagnosticString":"diag","operationResponses":[{"index":0,"statusCode":201},{"index":1,"statusCode":201},{"index":1,"statusCode":201}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::Ok,
            None,
            body,
            3,
            Uuid::nil(),
        );

        assert_eq!(
            response.status_code,
            azure_core::http::StatusCode::InternalServerError
        );
        assert!(response.is_retriable);
        assert_eq!(response.diagnostic_string.as_deref(), Some("diag"));
        assert_eq!(response.len(), 3);
        assert!(response
            .operation_results
            .iter()
            .all(|result| result.status_code == azure_core::http::StatusCode::InternalServerError));
    }

    #[test]
    fn parse_duplicate_index_error_pads_with_envelope_status() {
        let body = br#"{"operationResponses":[{"index":0,"statusCode":201},{"index":1,"statusCode":201},{"index":1,"statusCode":201}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::Conflict,
            None,
            body,
            3,
            Uuid::nil(),
        );

        assert_eq!(response.status_code, azure_core::http::StatusCode::Conflict);
        assert_eq!(response.len(), 3);
        assert!(response
            .operation_results
            .iter()
            .all(|result| result.status_code == azure_core::http::StatusCode::Conflict));
    }

    #[test]
    fn parse_fewer_results_error_pads_with_envelope_status() {
        // The coordinator returned fewer per-op results than operations on a
        // non-success envelope: the payload is uninterpretable, so it is padded
        // to one result per operation, each carrying the envelope status.
        let body = br#"{"operationResponses":[{"index":0,"statusCode":201},{"index":1,"statusCode":201}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::Conflict,
            None,
            body,
            3,
            Uuid::nil(),
        );

        assert_eq!(response.status_code, azure_core::http::StatusCode::Conflict);
        assert_eq!(response.len(), 3);
        assert!(response
            .operation_results
            .iter()
            .all(|result| result.status_code == azure_core::http::StatusCode::Conflict));
    }

    #[test]
    fn parse_fewer_results_success_fails_closed() {
        // The same short payload on a success envelope must fail closed to `500`
        // rather than return a success with unverifiable per-operation data.
        let body = br#"{"operationResponses":[{"index":0,"statusCode":201},{"index":1,"statusCode":201}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::Ok,
            None,
            body,
            3,
            Uuid::nil(),
        );

        assert_eq!(
            response.status_code,
            azure_core::http::StatusCode::InternalServerError
        );
        assert_eq!(response.len(), 3);
    }

    #[test]
    fn parse_multistatus_promotes_first_failure_in_request_order() {
        // Wire order is [idx2=503, idx0=201, idx1=409]. After reordering by
        // index the request order is [201, 409, 503], so the first failing
        // operation in request order is index 1 (409 Conflict) — not the 503
        // that happens to appear first on the wire. Matches .NET PR #5974.
        let body = br#"{"operationResponses":[{"index":2,"statusCode":503},{"index":0,"statusCode":201},{"index":1,"statusCode":409}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::from(207_u16),
            None,
            body,
            3,
            Uuid::nil(),
        );

        assert_eq!(response.status_code, azure_core::http::StatusCode::Conflict);
        assert_eq!(
            response.operation_results[1].status_code,
            azure_core::http::StatusCode::Conflict
        );
        assert_eq!(
            response.operation_results[2].status_code,
            azure_core::http::StatusCode::ServiceUnavailable
        );
    }

    #[test]
    fn parse_write_abort_keeps_failure_and_rolled_back_siblings() {
        // 452 abort: op0 was prepared then rolled back (453 / 5415); op1 is the
        // real failure (409). 452 is not a 207, so the envelope is preserved and
        // each operation keeps its own status.
        let body = br#"{"isRetriable":false,"operationResponses":[{"index":0,"statusCode":453,"subStatusCode":5415},{"index":1,"statusCode":409}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::from(452_u16),
            None,
            body,
            2,
            Uuid::nil(),
        );

        assert_eq!(u16::from(response.status_code), 452);
        assert_eq!(u16::from(response.operation_results[0].status_code), 453);
        assert_eq!(
            response.operation_results[0].sub_status_code,
            Some(crate::models::SubStatusCode::new(5415))
        );
        assert_eq!(
            response.operation_results[1].status_code,
            azure_core::http::StatusCode::Conflict
        );
    }

    #[test]
    fn parse_read_multistatus_skips_failed_dependency_and_promotes_real_failure() {
        // 207 read snapshot failure: op0 was rewritten to 424 FailedDependency,
        // op1 is the real 404. Promotion skips 424 and surfaces the 404.
        let body = br#"{"operationResponses":[{"index":0,"statusCode":424},{"index":1,"statusCode":404}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::from(207_u16),
            None,
            body,
            2,
            Uuid::nil(),
        );

        assert_eq!(response.status_code, azure_core::http::StatusCode::NotFound);
        assert_eq!(u16::from(response.operation_results[0].status_code), 424);
        assert_eq!(
            response.operation_results[1].status_code,
            azure_core::http::StatusCode::NotFound
        );
    }

    #[test]
    fn serialize_patch_with_non_object_body_and_predicate_fails() {
        let operation = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Patch,
            target("item1"),
        )
        .with_resource_body(Bytes::from_static(br#"[1,2,3]"#))
        .with_patch_filter_predicate("from c where c.status = 'pending'");
        let request =
            DistributedTransactionRequest::new(DistributedTransactionType::Write, vec![operation]);

        let error = request.serialize_body().unwrap_err();
        assert_eq!(
            error.status().status_code(),
            azure_core::http::StatusCode::BadRequest
        );
    }

    #[test]
    fn serialize_rejects_invalid_transaction_type_operation_kind_combinations() {
        let write_in_read = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Create,
            target("item1"),
        )
        .with_resource_body(Bytes::from_static(br#"{"id":"item1","pk":"pk1"}"#));
        let request = DistributedTransactionRequest::new(
            DistributedTransactionType::Read,
            vec![write_in_read],
        );
        assert_eq!(
            request.serialize_body().unwrap_err().status().status_code(),
            azure_core::http::StatusCode::BadRequest
        );

        let read_in_write = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Read,
            target("item1"),
        );
        let request = DistributedTransactionRequest::new(
            DistributedTransactionType::Write,
            vec![read_in_write],
        );
        assert_eq!(
            request.serialize_body().unwrap_err().status().status_code(),
            azure_core::http::StatusCode::BadRequest
        );
    }

    #[test]
    fn serialize_rejects_create_replace_upsert_body_id_mismatch() {
        for kind in [
            DistributedTransactionOperationKind::Create,
            DistributedTransactionOperationKind::Replace,
            DistributedTransactionOperationKind::Upsert,
        ] {
            let operation = DistributedTransactionOperation::new(kind, target("item1"))
                .with_resource_body(Bytes::from_static(br#"{"id":"other","pk":"pk1"}"#));
            let request = DistributedTransactionRequest::new(
                DistributedTransactionType::Write,
                vec![operation],
            );
            assert_eq!(
                request.serialize_body().unwrap_err().status().status_code(),
                azure_core::http::StatusCode::BadRequest
            );
        }
    }

    #[test]
    fn serialize_rejects_missing_resource_body_for_body_required_write_operations() {
        for kind in [
            DistributedTransactionOperationKind::Create,
            DistributedTransactionOperationKind::Replace,
            DistributedTransactionOperationKind::Upsert,
            DistributedTransactionOperationKind::Patch,
        ] {
            let operation = DistributedTransactionOperation::new(kind, target("item1"));
            let request = DistributedTransactionRequest::new(
                DistributedTransactionType::Write,
                vec![operation],
            );
            let error = request.serialize_body().unwrap_err();
            assert_eq!(
                error.status().status_code(),
                azure_core::http::StatusCode::BadRequest,
                "{kind:?} should reject missing resourceBody"
            );
            assert!(
                error.to_string().contains("require resourceBody"),
                "{kind:?} produced unexpected error: {error}"
            );
        }
    }

    #[test]
    fn parse_all_not_modified_read_transaction_is_completed() {
        let body = br#"{"operationResponses":[{"index":0,"statusCode":304},{"index":1,"statusCode":304}]}"#;
        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::NotModified,
            None,
            body,
            2,
            Uuid::nil(),
        );

        assert!(!response.is_success_status_code());
        assert!(response.is_completed_status_code());
        assert!(response
            .operation_results
            .iter()
            .all(DistributedTransactionOperationResult::is_completed_status_code));
    }

    #[test]
    fn request_is_a_stable_replay_across_retries() {
        // The outer retry loop re-sends the request with the same idempotency
        // token and re-serializes the body on each attempt, so both must be
        // deterministic: a retry then replays byte-for-byte and the server
        // treats it as the same commit rather than a duplicate.
        let operation = DistributedTransactionOperation::new(
            DistributedTransactionOperationKind::Create,
            target("item1"),
        )
        .with_resource_body(Bytes::from_static(br#"{"id":"item1","pk":"pk1"}"#));
        let request =
            DistributedTransactionRequest::new(DistributedTransactionType::Write, vec![operation]);

        let token = request.idempotency_token;
        let first = request.serialize_body().unwrap();
        let second = request.serialize_body().unwrap();

        assert_eq!(
            first, second,
            "serialized body must be identical across retries"
        );
        assert_eq!(
            request.idempotency_token, token,
            "idempotency token must be stable across retries"
        );
    }

    #[test]
    fn response_headers_prefer_valid_idempotency_token_header() {
        let request_token = Uuid::new_v4();
        let response_token = Uuid::new_v4();
        let mut raw_headers = azure_core::http::headers::Headers::new();
        raw_headers.insert("x-ms-cosmos-idempotency-token", response_token.to_string());
        let headers = CosmosResponseHeaders::from_headers(&raw_headers);

        let response = DistributedTransactionResponse::from_body(
            azure_core::http::StatusCode::Ok,
            None,
            br#"{"operationResponses":[]}"#,
            0,
            request_token,
        )
        .with_response_headers(&headers);

        assert_eq!(response.idempotency_token, response_token);
    }

    #[test]
    fn response_headers_fall_back_to_request_idempotency_token() {
        for header_value in [None, Some("not-a-uuid")] {
            let request_token = Uuid::new_v4();
            let mut raw_headers = azure_core::http::headers::Headers::new();
            if let Some(header_value) = header_value {
                raw_headers.insert("x-ms-cosmos-idempotency-token", header_value);
            }
            let headers = CosmosResponseHeaders::from_headers(&raw_headers);

            let response = DistributedTransactionResponse::from_body(
                azure_core::http::StatusCode::Ok,
                None,
                br#"{"operationResponses":[]}"#,
                0,
                request_token,
            )
            .with_response_headers(&headers);

            assert_eq!(response.idempotency_token, request_token);
        }
    }
}