durable-execution-sdk 0.1.0-alpha3

AWS Durable Execution SDK for Lambda Rust Runtime
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
//! Newtype wrappers for domain identifiers in the AWS Durable Execution SDK.
//!
//! This module provides type-safe wrappers for string identifiers used throughout
//! the SDK. These newtypes prevent accidental mixing of different ID types at
//! compile time while maintaining full compatibility with string-based APIs.
//!
//! # Example
//!
//! ```rust
//! use durable_execution_sdk::types::{OperationId, ExecutionArn, CallbackId};
//!
//! // Create from String or &str (no validation)
//! let op_id = OperationId::from("op-123");
//! let op_id2: OperationId = "op-456".into();
//!
//! // Create with validation
//! let op_id3 = OperationId::new("op-789").unwrap();
//! assert!(OperationId::new("").is_err()); // Empty strings rejected
//!
//! // Use as string via Deref
//! assert!(op_id.starts_with("op-"));
//!
//! // Use in HashMap (implements Hash, Eq)
//! use std::collections::HashMap;
//! let mut map: HashMap<OperationId, String> = HashMap::new();
//! map.insert(op_id, "value".to_string());
//! ```

use std::fmt;
use std::hash::Hash;
use std::ops::Deref;

use serde::{Deserialize, Serialize};

/// Error returned when newtype validation fails.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
    /// The type name that failed validation
    pub type_name: &'static str,
    /// Description of the validation failure
    pub message: String,
}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}: {}", self.type_name, self.message)
    }
}

impl std::error::Error for ValidationError {}

/// A unique identifier for an operation within a durable execution.
///
/// `OperationId` wraps a `String` to provide type safety, preventing accidental
/// mixing with other string-based identifiers like `ExecutionArn` or `CallbackId`.
///
/// # Construction
///
/// ```rust
/// use durable_execution_sdk::types::OperationId;
///
/// // From String (no validation)
/// let id1 = OperationId::from("op-123".to_string());
///
/// // From &str (no validation)
/// let id2 = OperationId::from("op-456");
///
/// // Using Into trait (no validation)
/// let id3: OperationId = "op-789".into();
///
/// // With validation
/// let id4 = OperationId::new("op-abc").unwrap();
/// assert!(OperationId::new("").is_err()); // Empty strings rejected
/// ```
///
/// # String Access
///
/// ```rust
/// use durable_execution_sdk::types::OperationId;
///
/// let id = OperationId::from("op-123");
///
/// // Via Deref (automatic)
/// assert!(id.starts_with("op-"));
/// assert_eq!(id.len(), 6);
///
/// // Via AsRef
/// let s: &str = id.as_ref();
/// assert_eq!(s, "op-123");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct OperationId(String);

impl OperationId {
    /// Creates a new `OperationId` with validation.
    ///
    /// Returns an error if the value is empty.
    pub fn new(id: impl Into<String>) -> Result<Self, ValidationError> {
        let id = id.into();
        if id.is_empty() {
            return Err(ValidationError {
                type_name: "OperationId",
                message: "value cannot be empty".to_string(),
            });
        }
        Ok(Self(id))
    }

    /// Creates a new `OperationId` without validation.
    ///
    /// Use this when you know the value is valid or when migrating
    /// from existing code that uses raw strings.
    #[inline]
    pub fn new_unchecked(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Returns the inner string value.
    #[inline]
    pub fn into_inner(self) -> String {
        self.0
    }

    /// Returns a reference to the inner string.
    #[inline]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for OperationId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Deref for OperationId {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for OperationId {
    #[inline]
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<String> for OperationId {
    #[inline]
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for OperationId {
    #[inline]
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

/// The Amazon Resource Name identifying a durable execution.
///
/// `ExecutionArn` wraps a `String` to provide type safety for execution ARNs.
/// It includes validation to ensure the ARN follows the expected format.
///
/// # ARN Format
///
/// A valid durable execution ARN follows the pattern:
/// `arn:<partition>:lambda:<region>:<account>:function:<function-name>:durable:<execution-id>`
///
/// Supported partitions include: `aws`, `aws-cn`, `aws-us-gov`, `aws-iso`, `aws-iso-b`, etc.
///
/// # Construction
///
/// ```rust
/// use durable_execution_sdk::types::ExecutionArn;
///
/// // From String (no validation)
/// let arn1 = ExecutionArn::from("arn:aws:lambda:us-east-1:123456789012:function:my-func:durable:abc123".to_string());
///
/// // With validation
/// let arn2 = ExecutionArn::new("arn:aws:lambda:us-east-1:123456789012:function:my-func:durable:abc123");
/// assert!(arn2.is_ok());
///
/// // China region
/// let arn_cn = ExecutionArn::new("arn:aws-cn:lambda:cn-north-1:123456789012:function:my-func:durable:abc123");
/// assert!(arn_cn.is_ok());
///
/// // Invalid ARN rejected
/// assert!(ExecutionArn::new("").is_err());
/// assert!(ExecutionArn::new("not-an-arn").is_err());
/// ```
///
/// # String Access
///
/// ```rust
/// use durable_execution_sdk::types::ExecutionArn;
///
/// let arn = ExecutionArn::from("arn:aws:lambda:us-east-1:123456789012:function:my-func:durable:abc123");
///
/// // Via Deref (automatic)
/// assert!(arn.starts_with("arn:"));
///
/// // Via AsRef
/// let s: &str = arn.as_ref();
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ExecutionArn(String);

impl ExecutionArn {
    /// Creates a new `ExecutionArn` with validation.
    ///
    /// Returns an error if the value is empty, doesn't start with "arn:aws:lambda:",
    /// or doesn't contain ":durable:".
    pub fn new(arn: impl Into<String>) -> Result<Self, ValidationError> {
        let arn = arn.into();
        Self::validate(&arn)?;
        Ok(Self(arn))
    }

    /// Creates a new `ExecutionArn` without validation.
    ///
    /// Use this when you know the value is valid or when migrating
    /// from existing code that uses raw strings.
    #[inline]
    pub fn new_unchecked(arn: impl Into<String>) -> Self {
        Self(arn.into())
    }

    /// Returns the inner string value.
    #[inline]
    pub fn into_inner(self) -> String {
        self.0
    }

    /// Returns a reference to the inner string.
    #[inline]
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Validates that the string is a valid durable execution ARN format.
    ///
    /// A valid ARN should:
    /// - Not be empty
    /// - Start with "arn:" followed by a valid AWS partition (aws, aws-cn, aws-us-gov, etc.)
    /// - Contain ":lambda:" service identifier
    /// - Contain ":durable:" segment
    fn validate(value: &str) -> Result<(), ValidationError> {
        if value.is_empty() {
            return Err(ValidationError {
                type_name: "ExecutionArn",
                message: "value cannot be empty".to_string(),
            });
        }

        // Check for valid ARN prefix with any AWS partition
        // Valid partitions: aws, aws-cn, aws-us-gov, aws-iso, aws-iso-b, etc.
        if !value.starts_with("arn:") {
            return Err(ValidationError {
                type_name: "ExecutionArn",
                message: "must start with 'arn:'".to_string(),
            });
        }

        // Check for lambda service
        if !value.contains(":lambda:") {
            return Err(ValidationError {
                type_name: "ExecutionArn",
                message: "must contain ':lambda:' service identifier".to_string(),
            });
        }

        if !value.contains(":durable:") {
            return Err(ValidationError {
                type_name: "ExecutionArn",
                message: "must contain ':durable:' segment".to_string(),
            });
        }

        Ok(())
    }
}

impl fmt::Display for ExecutionArn {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Deref for ExecutionArn {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for ExecutionArn {
    #[inline]
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<String> for ExecutionArn {
    #[inline]
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for ExecutionArn {
    #[inline]
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

/// A unique identifier for a callback operation.
///
/// `CallbackId` wraps a `String` to provide type safety for callback identifiers.
/// Callback IDs are used by external systems to signal completion of asynchronous
/// operations.
///
/// # Construction
///
/// ```rust
/// use durable_execution_sdk::types::CallbackId;
///
/// // From String (no validation)
/// let id1 = CallbackId::from("callback-123".to_string());
///
/// // From &str (no validation)
/// let id2 = CallbackId::from("callback-456");
///
/// // With validation
/// let id3 = CallbackId::new("callback-abc").unwrap();
/// assert!(CallbackId::new("").is_err()); // Empty strings rejected
/// ```
///
/// # String Access
///
/// ```rust
/// use durable_execution_sdk::types::CallbackId;
///
/// let id = CallbackId::from("callback-123");
///
/// // Via Deref (automatic)
/// assert!(id.starts_with("callback-"));
///
/// // Via AsRef
/// let s: &str = id.as_ref();
/// assert_eq!(s, "callback-123");
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CallbackId(String);

impl CallbackId {
    /// Creates a new `CallbackId` with validation.
    ///
    /// Returns an error if the value is empty.
    pub fn new(id: impl Into<String>) -> Result<Self, ValidationError> {
        let id = id.into();
        if id.is_empty() {
            return Err(ValidationError {
                type_name: "CallbackId",
                message: "value cannot be empty".to_string(),
            });
        }
        Ok(Self(id))
    }

    /// Creates a new `CallbackId` without validation.
    ///
    /// Use this when you know the value is valid or when migrating
    /// from existing code that uses raw strings.
    #[inline]
    pub fn new_unchecked(id: impl Into<String>) -> Self {
        Self(id.into())
    }

    /// Returns the inner string value.
    #[inline]
    pub fn into_inner(self) -> String {
        self.0
    }

    /// Returns a reference to the inner string.
    #[inline]
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for CallbackId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Deref for CallbackId {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl AsRef<str> for CallbackId {
    #[inline]
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<String> for CallbackId {
    #[inline]
    fn from(s: String) -> Self {
        Self(s)
    }
}

impl From<&str> for CallbackId {
    #[inline]
    fn from(s: &str) -> Self {
        Self(s.to_string())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    // ==================== OperationId Tests ====================

    #[test]
    fn test_operation_id_from_string() {
        let id = OperationId::from("op-123".to_string());
        assert_eq!(id.as_str(), "op-123");
    }

    #[test]
    fn test_operation_id_from_str() {
        let id = OperationId::from("op-456");
        assert_eq!(id.as_str(), "op-456");
    }

    #[test]
    fn test_operation_id_into() {
        let id: OperationId = "op-789".into();
        assert_eq!(id.as_str(), "op-789");
    }

    #[test]
    fn test_operation_id_new_valid() {
        let id = OperationId::new("op-abc").unwrap();
        assert_eq!(id.as_str(), "op-abc");
    }

    #[test]
    fn test_operation_id_new_empty_rejected() {
        let result = OperationId::new("");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.type_name, "OperationId");
        assert!(err.message.contains("empty"));
    }

    #[test]
    fn test_operation_id_display() {
        let id = OperationId::from("op-display");
        assert_eq!(format!("{}", id), "op-display");
    }

    #[test]
    fn test_operation_id_debug() {
        let id = OperationId::from("op-debug");
        let debug_str = format!("{:?}", id);
        assert!(debug_str.contains("op-debug"));
    }

    #[test]
    fn test_operation_id_deref() {
        let id = OperationId::from("op-deref-test");
        assert!(id.starts_with("op-"));
        assert_eq!(id.len(), 13);
    }

    #[test]
    fn test_operation_id_as_ref() {
        let id = OperationId::from("op-asref");
        let s: &str = id.as_ref();
        assert_eq!(s, "op-asref");
    }

    #[test]
    fn test_operation_id_hash_and_eq() {
        let id1 = OperationId::from("op-hash");
        let id2 = OperationId::from("op-hash");
        let id3 = OperationId::from("op-different");

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);

        let mut map: HashMap<OperationId, String> = HashMap::new();
        map.insert(id1.clone(), "value1".to_string());
        assert_eq!(map.get(&id2), Some(&"value1".to_string()));
        assert_eq!(map.get(&id3), None);
    }

    #[test]
    fn test_operation_id_serde_roundtrip() {
        let id = OperationId::from("op-serde-test");
        let json = serde_json::to_string(&id).unwrap();
        assert_eq!(json, "\"op-serde-test\"");

        let deserialized: OperationId = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, id);
    }

    #[test]
    fn test_operation_id_into_inner() {
        let id = OperationId::from("op-inner");
        let inner = id.into_inner();
        assert_eq!(inner, "op-inner");
    }

    // ==================== ExecutionArn Tests ====================

    #[test]
    fn test_execution_arn_from_string() {
        let arn = ExecutionArn::from(
            "arn:aws:lambda:us-east-1:123456789012:function:my-func:durable:abc123".to_string(),
        );
        assert!(arn.as_str().starts_with("arn:aws:lambda:"));
    }

    #[test]
    fn test_execution_arn_from_str() {
        let arn =
            ExecutionArn::from("arn:aws:lambda:us-west-2:123456789012:function:test:durable:xyz");
        assert!(arn.contains(":durable:"));
    }

    #[test]
    fn test_execution_arn_new_valid() {
        let arn =
            ExecutionArn::new("arn:aws:lambda:eu-west-1:123456789012:function:func:durable:id123");
        assert!(arn.is_ok());
    }

    #[test]
    fn test_execution_arn_new_empty_rejected() {
        let result = ExecutionArn::new("");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.type_name, "ExecutionArn");
        assert!(err.message.contains("empty"));
    }

    #[test]
    fn test_execution_arn_new_invalid_prefix_rejected() {
        let result = ExecutionArn::new("not-an-arn");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("arn:"));
    }

    #[test]
    fn test_execution_arn_new_missing_lambda_rejected() {
        let result = ExecutionArn::new("arn:aws:s3:us-east-1:123456789012:bucket:durable:abc");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains(":lambda:"));
    }

    #[test]
    fn test_execution_arn_new_missing_durable_rejected() {
        let result = ExecutionArn::new("arn:aws:lambda:us-east-1:123456789012:function:my-func");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains(":durable:"));
    }

    #[test]
    fn test_execution_arn_new_aws_cn_partition() {
        let arn = ExecutionArn::new(
            "arn:aws-cn:lambda:cn-north-1:123456789012:function:my-func:durable:abc123",
        );
        assert!(arn.is_ok());
    }

    #[test]
    fn test_execution_arn_new_aws_us_gov_partition() {
        let arn = ExecutionArn::new(
            "arn:aws-us-gov:lambda:us-gov-west-1:123456789012:function:my-func:durable:abc123",
        );
        assert!(arn.is_ok());
    }

    #[test]
    fn test_execution_arn_new_aws_iso_partition() {
        let arn = ExecutionArn::new(
            "arn:aws-iso:lambda:us-iso-east-1:123456789012:function:my-func:durable:abc123",
        );
        assert!(arn.is_ok());
    }

    #[test]
    fn test_execution_arn_display() {
        let arn = ExecutionArn::from("arn:aws:lambda:us-east-1:123456789012:function:f:durable:x");
        assert_eq!(
            format!("{}", arn),
            "arn:aws:lambda:us-east-1:123456789012:function:f:durable:x"
        );
    }

    #[test]
    fn test_execution_arn_deref() {
        let arn = ExecutionArn::from("arn:aws:lambda:us-east-1:123456789012:function:f:durable:x");
        assert!(arn.starts_with("arn:"));
        assert!(arn.contains("lambda"));
    }

    #[test]
    fn test_execution_arn_hash_and_eq() {
        let arn1 = ExecutionArn::from("arn:aws:lambda:us-east-1:123:function:f:durable:a");
        let arn2 = ExecutionArn::from("arn:aws:lambda:us-east-1:123:function:f:durable:a");
        let arn3 = ExecutionArn::from("arn:aws:lambda:us-east-1:123:function:f:durable:b");

        assert_eq!(arn1, arn2);
        assert_ne!(arn1, arn3);

        let mut map: HashMap<ExecutionArn, i32> = HashMap::new();
        map.insert(arn1.clone(), 42);
        assert_eq!(map.get(&arn2), Some(&42));
    }

    #[test]
    fn test_execution_arn_serde_roundtrip() {
        let arn = ExecutionArn::from("arn:aws:lambda:us-east-1:123:function:f:durable:serde");
        let json = serde_json::to_string(&arn).unwrap();
        let deserialized: ExecutionArn = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, arn);
    }

    // ==================== CallbackId Tests ====================

    #[test]
    fn test_callback_id_from_string() {
        let id = CallbackId::from("callback-123".to_string());
        assert_eq!(id.as_str(), "callback-123");
    }

    #[test]
    fn test_callback_id_from_str() {
        let id = CallbackId::from("callback-456");
        assert_eq!(id.as_str(), "callback-456");
    }

    #[test]
    fn test_callback_id_new_valid() {
        let id = CallbackId::new("callback-abc").unwrap();
        assert_eq!(id.as_str(), "callback-abc");
    }

    #[test]
    fn test_callback_id_new_empty_rejected() {
        let result = CallbackId::new("");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.type_name, "CallbackId");
        assert!(err.message.contains("empty"));
    }

    #[test]
    fn test_callback_id_display() {
        let id = CallbackId::from("callback-display");
        assert_eq!(format!("{}", id), "callback-display");
    }

    #[test]
    fn test_callback_id_deref() {
        let id = CallbackId::from("callback-deref");
        assert!(id.starts_with("callback-"));
        assert_eq!(id.len(), 14);
    }

    #[test]
    fn test_callback_id_as_ref() {
        let id = CallbackId::from("callback-asref");
        let s: &str = id.as_ref();
        assert_eq!(s, "callback-asref");
    }

    #[test]
    fn test_callback_id_hash_and_eq() {
        let id1 = CallbackId::from("callback-hash");
        let id2 = CallbackId::from("callback-hash");
        let id3 = CallbackId::from("callback-other");

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);

        let mut map: HashMap<CallbackId, bool> = HashMap::new();
        map.insert(id1.clone(), true);
        assert_eq!(map.get(&id2), Some(&true));
    }

    #[test]
    fn test_callback_id_serde_roundtrip() {
        let id = CallbackId::from("callback-serde");
        let json = serde_json::to_string(&id).unwrap();
        assert_eq!(json, "\"callback-serde\"");

        let deserialized: CallbackId = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, id);
    }

    // ==================== ValidationError Tests ====================

    #[test]
    fn test_validation_error_display() {
        let err = ValidationError {
            type_name: "TestType",
            message: "test error message".to_string(),
        };
        assert_eq!(format!("{}", err), "TestType: test error message");
    }

    // ==================== Backward Compatibility Tests ====================

    /// Tests that existing serialized data (plain JSON strings) can be deserialized
    /// into newtypes, ensuring backward compatibility with existing checkpoints.
    #[test]
    fn test_backward_compat_deserialize_plain_string_to_operation_id() {
        // Simulate existing serialized data (plain JSON string)
        let existing_json = "\"existing-op-id-12345\"";

        // Should deserialize successfully into OperationId
        let id: OperationId = serde_json::from_str(existing_json).unwrap();
        assert_eq!(id.as_str(), "existing-op-id-12345");
    }

    #[test]
    fn test_backward_compat_deserialize_plain_string_to_execution_arn() {
        // Simulate existing serialized data (plain JSON string)
        let existing_json =
            "\"arn:aws:lambda:us-east-1:123456789012:function:my-func:durable:abc123\"";

        // Should deserialize successfully into ExecutionArn
        let arn: ExecutionArn = serde_json::from_str(existing_json).unwrap();
        assert!(arn.contains(":durable:"));
    }

    #[test]
    fn test_backward_compat_deserialize_plain_string_to_callback_id() {
        // Simulate existing serialized data (plain JSON string)
        let existing_json = "\"callback-xyz-789\"";

        // Should deserialize successfully into CallbackId
        let id: CallbackId = serde_json::from_str(existing_json).unwrap();
        assert_eq!(id.as_str(), "callback-xyz-789");
    }

    /// Tests that functions accepting `impl Into<NewType>` work with String,
    /// ensuring backward compatibility with existing code.
    #[test]
    fn test_backward_compat_impl_into_with_string() {
        fn accept_operation_id(id: impl Into<OperationId>) -> OperationId {
            id.into()
        }

        fn accept_execution_arn(arn: impl Into<ExecutionArn>) -> ExecutionArn {
            arn.into()
        }

        fn accept_callback_id(id: impl Into<CallbackId>) -> CallbackId {
            id.into()
        }

        // Should work with String
        let op_id = accept_operation_id("op-123".to_string());
        assert_eq!(op_id.as_str(), "op-123");

        // Should work with &str
        let op_id = accept_operation_id("op-456");
        assert_eq!(op_id.as_str(), "op-456");

        // Should work with ExecutionArn
        let arn =
            accept_execution_arn("arn:aws:lambda:us-east-1:123:function:f:durable:x".to_string());
        assert!(arn.contains(":durable:"));

        // Should work with CallbackId
        let cb_id = accept_callback_id("callback-abc");
        assert_eq!(cb_id.as_str(), "callback-abc");
    }

    /// Tests that newtypes serialize to the same format as plain strings,
    /// ensuring backward compatibility with existing consumers.
    #[test]
    fn test_backward_compat_serialize_same_as_string() {
        let op_id = OperationId::from("op-123");
        let plain_string = "op-123".to_string();

        let op_id_json = serde_json::to_string(&op_id).unwrap();
        let string_json = serde_json::to_string(&plain_string).unwrap();

        // Both should serialize to the same JSON
        assert_eq!(op_id_json, string_json);
        assert_eq!(op_id_json, "\"op-123\"");
    }

    /// Tests that newtypes can be used interchangeably with &str in string operations.
    #[test]
    fn test_backward_compat_string_operations() {
        let op_id = OperationId::from("op-123-suffix");

        // Should work with string methods via Deref
        assert!(op_id.starts_with("op-"));
        assert!(op_id.ends_with("-suffix"));
        assert!(op_id.contains("123"));
        assert_eq!(op_id.len(), 13); // "op-123-suffix" is 13 characters

        // Should work with functions expecting &str
        fn process_str(s: &str) -> String {
            s.to_uppercase()
        }
        assert_eq!(process_str(&op_id), "OP-123-SUFFIX");
    }

    // ==================== Property-Based Tests ====================

    use proptest::prelude::*;

    /// Strategy for generating non-empty strings (valid for OperationId and CallbackId)
    fn non_empty_string_strategy() -> impl Strategy<Value = String> {
        "[a-zA-Z0-9_-]{1,64}".prop_map(|s| s)
    }

    /// Strategy for generating valid ExecutionArn strings
    fn valid_execution_arn_strategy() -> impl Strategy<Value = String> {
        (
            prop_oneof![
                Just("aws"),
                Just("aws-cn"),
                Just("aws-us-gov"),
                Just("aws-iso"),
                Just("aws-iso-b"),
            ],
            prop_oneof![
                Just("us-east-1"),
                Just("us-west-2"),
                Just("eu-west-1"),
                Just("cn-north-1"),
                Just("us-gov-west-1"),
            ],
            "[0-9]{12}",
            "[a-zA-Z0-9_-]{1,32}",
            "[a-zA-Z0-9]{8,32}",
        )
            .prop_map(|(partition, region, account, func, exec_id)| {
                format!(
                    "arn:{}:lambda:{}:{}:function:{}:durable:{}",
                    partition, region, account, func, exec_id
                )
            })
    }

    /// Strategy for generating invalid ARN strings (missing required components)
    fn invalid_arn_strategy() -> impl Strategy<Value = String> {
        prop_oneof![
            // Empty string
            Just("".to_string()),
            // Missing arn: prefix
            "[a-zA-Z0-9]{10,30}".prop_map(|s| s),
            // Has arn: but missing lambda
            "[a-zA-Z0-9]{5,20}".prop_map(|s| format!("arn:aws:s3:us-east-1:123456789012:{}", s)),
            // Has arn: and lambda but missing durable
            "[a-zA-Z0-9]{5,20}"
                .prop_map(|s| format!("arn:aws:lambda:us-east-1:123456789012:function:{}", s)),
        ]
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(100))]

        // ==================== OperationId Property Tests ====================

        /// Feature: rust-sdk-test-suite, Property 8: OperationId Validation and Round-Trip
        /// For any non-empty string, OperationId::new() SHALL succeed and round-trip through serde.
        /// **Validates: Requirements 4.1**
        #[test]
        fn prop_operation_id_validation_and_roundtrip(s in non_empty_string_strategy()) {
            // Validation should succeed for non-empty strings
            let id = OperationId::new(&s).expect("non-empty string should be valid");

            // Round-trip through serde
            let json = serde_json::to_string(&id).expect("serialization should succeed");
            let deserialized: OperationId = serde_json::from_str(&json).expect("deserialization should succeed");

            // Should produce the same value
            prop_assert_eq!(&id, &deserialized);
            prop_assert_eq!(deserialized.as_str(), s.as_str());
        }

        /// Feature: rust-sdk-test-suite, Property 8: OperationId Empty String Validation
        /// For any empty string, OperationId::new() SHALL return ValidationError.
        /// **Validates: Requirements 4.4**
        #[test]
        fn prop_operation_id_empty_string_rejected(_dummy in Just(())) {
            let result = OperationId::new("");
            prop_assert!(result.is_err());
            let err = result.unwrap_err();
            prop_assert_eq!(err.type_name, "OperationId");
            prop_assert!(err.message.contains("empty"));
        }

        /// Feature: rust-sdk-test-suite, Property 8: OperationId From/Into Round-Trip
        /// For any string, creating via From and serializing should round-trip.
        #[test]
        fn prop_operation_id_from_roundtrip(s in ".*") {
            let id = OperationId::from(s.clone());

            // Round-trip through serde
            let json = serde_json::to_string(&id).expect("serialization should succeed");
            let deserialized: OperationId = serde_json::from_str(&json).expect("deserialization should succeed");

            prop_assert_eq!(&id, &deserialized);
            prop_assert_eq!(deserialized.as_str(), s.as_str());
        }

        // ==================== ExecutionArn Property Tests ====================

        /// Feature: rust-sdk-test-suite, Property 9: ExecutionArn Validation and Round-Trip
        /// For any valid ARN string, ExecutionArn::new() SHALL succeed and round-trip through serde.
        /// **Validates: Requirements 4.2**
        #[test]
        fn prop_execution_arn_validation_and_roundtrip(arn_str in valid_execution_arn_strategy()) {
            // Validation should succeed for valid ARN strings
            let arn = ExecutionArn::new(&arn_str).expect("valid ARN should be accepted");

            // Round-trip through serde
            let json = serde_json::to_string(&arn).expect("serialization should succeed");
            let deserialized: ExecutionArn = serde_json::from_str(&json).expect("deserialization should succeed");

            // Should produce the same value
            prop_assert_eq!(&arn, &deserialized);
            prop_assert_eq!(deserialized.as_str(), arn_str.as_str());
        }

        /// Feature: rust-sdk-test-suite, Property 9: ExecutionArn Invalid String Validation
        /// For any string not matching ARN pattern, ExecutionArn::new() SHALL return ValidationError.
        /// **Validates: Requirements 4.5**
        #[test]
        fn prop_execution_arn_invalid_rejected(invalid_arn in invalid_arn_strategy()) {
            let result = ExecutionArn::new(&invalid_arn);
            prop_assert!(result.is_err(), "Invalid ARN '{}' should be rejected", invalid_arn);
        }

        /// Feature: rust-sdk-test-suite, Property 9: ExecutionArn From/Into Round-Trip
        /// For any string, creating via From and serializing should round-trip.
        #[test]
        fn prop_execution_arn_from_roundtrip(s in ".*") {
            let arn = ExecutionArn::from(s.clone());

            // Round-trip through serde
            let json = serde_json::to_string(&arn).expect("serialization should succeed");
            let deserialized: ExecutionArn = serde_json::from_str(&json).expect("deserialization should succeed");

            prop_assert_eq!(&arn, &deserialized);
            prop_assert_eq!(deserialized.as_str(), s.as_str());
        }

        // ==================== CallbackId Property Tests ====================

        /// Feature: rust-sdk-test-suite, Property 10: CallbackId Validation and Round-Trip
        /// For any non-empty string, CallbackId::new() SHALL succeed and round-trip through serde.
        /// **Validates: Requirements 4.3**
        #[test]
        fn prop_callback_id_validation_and_roundtrip(s in non_empty_string_strategy()) {
            // Validation should succeed for non-empty strings
            let id = CallbackId::new(&s).expect("non-empty string should be valid");

            // Round-trip through serde
            let json = serde_json::to_string(&id).expect("serialization should succeed");
            let deserialized: CallbackId = serde_json::from_str(&json).expect("deserialization should succeed");

            // Should produce the same value
            prop_assert_eq!(&id, &deserialized);
            prop_assert_eq!(deserialized.as_str(), s.as_str());
        }

        /// Feature: rust-sdk-test-suite, Property 10: CallbackId Empty String Validation
        /// For any empty string, CallbackId::new() SHALL return ValidationError.
        /// **Validates: Requirements 4.4 (applies to CallbackId as well)**
        #[test]
        fn prop_callback_id_empty_string_rejected(_dummy in Just(())) {
            let result = CallbackId::new("");
            prop_assert!(result.is_err());
            let err = result.unwrap_err();
            prop_assert_eq!(err.type_name, "CallbackId");
            prop_assert!(err.message.contains("empty"));
        }

        /// Feature: rust-sdk-test-suite, Property 10: CallbackId From/Into Round-Trip
        /// For any string, creating via From and serializing should round-trip.
        #[test]
        fn prop_callback_id_from_roundtrip(s in ".*") {
            let id = CallbackId::from(s.clone());

            // Round-trip through serde
            let json = serde_json::to_string(&id).expect("serialization should succeed");
            let deserialized: CallbackId = serde_json::from_str(&json).expect("deserialization should succeed");

            prop_assert_eq!(&id, &deserialized);
            prop_assert_eq!(deserialized.as_str(), s.as_str());
        }

        // ==================== HashMap Key Behavior Property Tests ====================

        /// Feature: rust-sdk-test-suite, Property 11: OperationId HashMap Key Behavior
        /// For any OperationId instance, inserting into a HashMap and retrieving by an equal key
        /// SHALL return the same value.
        /// **Validates: Requirements 4.6**
        #[test]
        fn prop_operation_id_hashmap_key(s in non_empty_string_strategy(), value in any::<i32>()) {
            let id1 = OperationId::from(s.clone());
            let id2 = OperationId::from(s.clone());

            let mut map: HashMap<OperationId, i32> = HashMap::new();
            map.insert(id1, value);

            // Retrieving by equal key should return the same value
            prop_assert_eq!(map.get(&id2), Some(&value));
        }

        /// Feature: rust-sdk-test-suite, Property 11: ExecutionArn HashMap Key Behavior
        /// For any ExecutionArn instance, inserting into a HashMap and retrieving by an equal key
        /// SHALL return the same value.
        /// **Validates: Requirements 4.6**
        #[test]
        fn prop_execution_arn_hashmap_key(arn_str in valid_execution_arn_strategy(), value in any::<i32>()) {
            let arn1 = ExecutionArn::from(arn_str.clone());
            let arn2 = ExecutionArn::from(arn_str.clone());

            let mut map: HashMap<ExecutionArn, i32> = HashMap::new();
            map.insert(arn1, value);

            // Retrieving by equal key should return the same value
            prop_assert_eq!(map.get(&arn2), Some(&value));
        }

        /// Feature: rust-sdk-test-suite, Property 11: CallbackId HashMap Key Behavior
        /// For any CallbackId instance, inserting into a HashMap and retrieving by an equal key
        /// SHALL return the same value.
        /// **Validates: Requirements 4.6**
        #[test]
        fn prop_callback_id_hashmap_key(s in non_empty_string_strategy(), value in any::<i32>()) {
            let id1 = CallbackId::from(s.clone());
            let id2 = CallbackId::from(s.clone());

            let mut map: HashMap<CallbackId, i32> = HashMap::new();
            map.insert(id1, value);

            // Retrieving by equal key should return the same value
            prop_assert_eq!(map.get(&id2), Some(&value));
        }

        /// Feature: rust-sdk-test-suite, Property 11: Different keys should not collide
        /// For any two different strings, their newtypes should not be equal and should
        /// not collide in HashMap lookups.
        #[test]
        fn prop_different_keys_no_collision(
            s1 in non_empty_string_strategy(),
            s2 in non_empty_string_strategy(),
            value in any::<i32>()
        ) {
            prop_assume!(s1 != s2);

            let id1 = OperationId::from(s1);
            let id2 = OperationId::from(s2);

            let mut map: HashMap<OperationId, i32> = HashMap::new();
            map.insert(id1.clone(), value);

            // Different key should not find the value
            prop_assert_eq!(map.get(&id2), None);
            // Original key should still find the value
            prop_assert_eq!(map.get(&id1), Some(&value));
        }
    }
}