asupersync-conformance 0.3.1

Conformance test suite for async runtime specifications
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
//! Outcome and Obligation Conformance Test Suite
//!
//! Tests covering the four-valued outcome type (Ok, Err, Cancelled, Panicked),
//! outcome joining semantics, and obligation lifecycle (reserve, commit, abort, leak detection).
//!
//! # Outcome Tests
//!
//! Tests the severity lattice: Ok < Err < Cancelled < Panicked
//! - OC-001: Outcome::Ok construction and inspection
//! - OC-002: Outcome::Err construction and inspection
//! - OC-003: Outcome::Cancelled construction and inspection
//! - OC-004: Outcome::Panicked construction and inspection
//! - OC-005: Severity ordering (Ok < Err < Cancelled < Panicked)
//! - OC-006: Join of two Ok outcomes returns first Ok
//! - OC-007: Join of Ok and Err returns Err (worse wins)
//! - OC-008: Join of Err and Cancelled returns Cancelled
//! - OC-009: Join of any outcome with Panicked returns Panicked
//! - OC-010: Outcome map preserves non-Ok variants
//! - OC-011: Outcome into_result conversion
//! - OC-012: Outcome from Result conversion
//!
//! # Obligation Tests
//!
//! Tests the two-phase effect pattern and obligation lifecycle.
//! - OB-001: Obligation can be created in Reserved state
//! - OB-002: Obligation can be committed (successful resolution)
//! - OB-003: Obligation can be aborted (clean cancellation)
//! - OB-004: Leaked obligation is detected
//! - OB-005: Double commit panics
//! - OB-006: Commit after abort panics
//! - OB-007: Obligation kinds are distinguishable
//! - OB-008: Obligation description can be set

use crate::{
    ConformanceTest, MpscReceiver, MpscSender, OneshotSender, RuntimeInterface, TestCategory,
    TestMeta, TestResult, checkpoint,
};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

/// Get all outcome conformance tests.
pub fn outcome_tests<RT: RuntimeInterface>() -> Vec<ConformanceTest<RT>> {
    vec![
        oc_001_outcome_ok::<RT>(),
        oc_002_outcome_err::<RT>(),
        oc_003_outcome_cancelled::<RT>(),
        oc_004_outcome_panicked::<RT>(),
        oc_005_severity_ordering::<RT>(),
        oc_006_join_ok_ok::<RT>(),
        oc_007_join_ok_err::<RT>(),
        oc_008_join_err_cancelled::<RT>(),
        oc_009_join_any_panicked::<RT>(),
        oc_010_map_preserves_variants::<RT>(),
        oc_011_into_result_conversion::<RT>(),
        oc_012_from_result_conversion::<RT>(),
    ]
}

/// Get all obligation conformance tests.
pub fn obligation_tests<RT: RuntimeInterface>() -> Vec<ConformanceTest<RT>> {
    vec![
        ob_001_obligation_reserved::<RT>(),
        ob_002_obligation_commit::<RT>(),
        ob_003_obligation_abort::<RT>(),
        ob_004_leaked_detection::<RT>(),
        ob_005_double_commit_panics::<RT>(),
        ob_006_commit_after_abort_panics::<RT>(),
        ob_007_obligation_kinds::<RT>(),
        ob_008_obligation_description::<RT>(),
    ]
}

/// Get all outcome and obligation tests.
pub fn all_tests<RT: RuntimeInterface>() -> Vec<ConformanceTest<RT>> {
    let mut tests = outcome_tests::<RT>();
    tests.extend(obligation_tests::<RT>());
    tests
}

// ============================================================================
// Outcome Tests
// ============================================================================

/// OC-001: Outcome::Ok construction and inspection
///
/// Verifies that Outcome::Ok can be created and correctly reports its state.
pub fn oc_001_outcome_ok<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-001".to_string(),
            name: "Outcome::Ok construction".to_string(),
            description: "Create Outcome::Ok and verify predicates".to_string(),
            category: TestCategory::Spawn,
            tags: vec!["outcome".to_string(), "ok".to_string(), "basic".to_string()],
            expected: "Outcome is Ok, value is accessible".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Test Outcome::Ok construction and predicates
                // We use spawn to verify the outcome propagates correctly
                let handle = rt.spawn(async { 42i32 });
                let result = handle.await;

                checkpoint(
                    "outcome_ok",
                    serde_json::json!({
                        "value": result,
                        "is_ok": true
                    }),
                );

                if result != 42 {
                    return TestResult::failed(format!(
                        "Expected Outcome::Ok(42), got value {}",
                        result
                    ));
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-002: Outcome::Err construction and inspection
///
/// Verifies that errors returned from tasks are properly captured.
pub fn oc_002_outcome_err<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-002".to_string(),
            name: "Outcome::Err via task error".to_string(),
            description: "Errors from tasks are captured in results".to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "err".to_string(),
                "basic".to_string(),
            ],
            expected: "Error is propagated from task".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Using Result<T, E> in spawn to test error propagation
                let (tx, rx) = rt.oneshot_channel::<()>();
                drop(tx); // Close channel immediately

                // Receiving from closed channel should return error
                let result = rx.await;

                checkpoint(
                    "outcome_err",
                    serde_json::json!({
                        "result": format!("{:?}", result),
                        "is_err": result.is_err()
                    }),
                );

                if result.is_ok() {
                    return TestResult::failed("Expected error from closed oneshot channel");
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-003: Outcome::Cancelled construction and inspection
///
/// Verifies that cancellation produces a cancelled outcome.
pub fn oc_003_outcome_cancelled<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-003".to_string(),
            name: "Outcome::Cancelled via timeout".to_string(),
            description: "Timeout produces cancellation-like behavior".to_string(),
            category: TestCategory::Cancel,
            tags: vec![
                "outcome".to_string(),
                "cancelled".to_string(),
                "timeout".to_string(),
            ],
            expected: "Timeout produces timeout error (cancellation proxy)".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Use timeout as a proxy for cancellation
                let long_sleep = rt.sleep(Duration::from_secs(10));
                let result = rt
                    .timeout(Duration::from_millis(10), async {
                        long_sleep.await;
                        42
                    })
                    .await;

                checkpoint(
                    "outcome_cancelled",
                    serde_json::json!({
                        "result": format!("{:?}", result),
                        "timed_out": result.is_err()
                    }),
                );

                if result.is_ok() {
                    return TestResult::failed("Expected timeout (cancellation proxy)");
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-004: Outcome::Panicked construction and inspection
///
/// Verifies that panics in tasks are caught and don't crash the runtime.
pub fn oc_004_outcome_panicked<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-004".to_string(),
            name: "Outcome::Panicked via task panic".to_string(),
            description: "Panicking task doesn't crash runtime".to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "panicked".to_string(),
                "error".to_string(),
            ],
            expected: "Runtime survives panic, other tasks complete".to_string(),
        },
        |rt| {
            rt.block_on(async {
                let completed = Arc::new(AtomicBool::new(false));
                let completed_clone = completed.clone();

                let (good_tx, mut good_rx) = rt.mpsc_channel::<i32>(1);
                let (bad_started_tx, bad_started_rx) = rt.oneshot_channel::<()>();

                // Good task that should complete
                let _good = rt.spawn(async move {
                    std::thread::yield_now();
                    completed_clone.store(true, Ordering::SeqCst);
                    let _ = good_tx.send(42).await;
                });

                // Bad task that panics
                let _bad = rt.spawn(async move {
                    let _ = bad_started_tx.send(());
                    panic!("intentional panic for testing outcome");
                });

                // Wait for bad task to start
                let _ = bad_started_rx.await;

                // Good task should still complete
                let result = rt
                    .timeout(Duration::from_millis(500), async { good_rx.recv().await })
                    .await;

                checkpoint(
                    "outcome_panicked",
                    serde_json::json!({
                        "good_task_result": format!("{:?}", result),
                        "completed": completed.load(Ordering::SeqCst)
                    }),
                );

                match result {
                    Ok(Some(42)) => TestResult::passed(),
                    Ok(Some(other)) => TestResult::failed(format!("Expected 42, got {}", other)),
                    Ok(None) => {
                        // Channel closed is acceptable in some runtimes
                        checkpoint(
                            "channel_closed",
                            serde_json::json!({"note": "Channel closed, acceptable"}),
                        );
                        TestResult::passed()
                    }
                    Err(_) => TestResult::failed("Good task did not complete after panic"),
                }
            })
        },
    )
}

/// OC-005: Severity ordering (Ok < Err < Cancelled < Panicked)
///
/// Tests that severity levels are correctly ordered.
pub fn oc_005_severity_ordering<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-005".to_string(),
            name: "Outcome severity ordering".to_string(),
            description: "Verify Ok < Err < Cancelled < Panicked ordering".to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "severity".to_string(),
                "ordering".to_string(),
            ],
            expected: "Severity levels are totally ordered".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Test severity ordering through type system
                // asupersync::Severity: Ok=0 < Err=1 < Cancelled=2 < Panicked=3
                let severities = [
                    ("Ok", 0u8),
                    ("Err", 1u8),
                    ("Cancelled", 2u8),
                    ("Panicked", 3u8),
                ];

                checkpoint(
                    "severity_ordering",
                    serde_json::json!({
                        "severities": severities,
                        "ordering": "Ok < Err < Cancelled < Panicked"
                    }),
                );

                // Verify ordering is strict
                for i in 0..severities.len() {
                    for j in (i + 1)..severities.len() {
                        if severities[i].1 >= severities[j].1 {
                            return TestResult::failed(format!(
                                "Severity ordering violated: {} ({}) should be < {} ({})",
                                severities[i].0, severities[i].1, severities[j].0, severities[j].1
                            ));
                        }
                    }
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-006: Join of two Ok outcomes returns first Ok
///
/// When joining two Ok outcomes, the first one is returned.
pub fn oc_006_join_ok_ok<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-006".to_string(),
            name: "Join Ok + Ok returns first".to_string(),
            description: "Joining two Ok outcomes returns the first".to_string(),
            category: TestCategory::Spawn,
            tags: vec!["outcome".to_string(), "join".to_string(), "ok".to_string()],
            expected: "First Ok value is returned".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Spawn two tasks that both succeed
                let handle1 = rt.spawn(async { 1i32 });
                let handle2 = rt.spawn(async { 2i32 });

                let result1 = handle1.await;
                let result2 = handle2.await;

                checkpoint(
                    "join_ok_ok",
                    serde_json::json!({
                        "result1": result1,
                        "result2": result2,
                        "both_ok": true
                    }),
                );

                if result1 != 1 || result2 != 2 {
                    return TestResult::failed(format!(
                        "Expected (1, 2), got ({}, {})",
                        result1, result2
                    ));
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-007: Join of Ok and Err returns Err (worse wins)
///
/// When joining outcomes, the worse one (higher severity) wins.
pub fn oc_007_join_ok_err<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-007".to_string(),
            name: "Join Ok + Err returns Err".to_string(),
            description: "When joining, Err (worse) takes precedence over Ok".to_string(),
            category: TestCategory::Spawn,
            tags: vec!["outcome".to_string(), "join".to_string(), "err".to_string()],
            expected: "Err outcome takes precedence".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // One task succeeds, one fails via closed channel
                let (good_tx, mut good_rx) = rt.mpsc_channel::<i32>(1);
                let (_, bad_rx) = rt.oneshot_channel::<i32>();

                let _good = rt.spawn(async move {
                    let _ = good_tx.send(42).await;
                });

                // bad_rx's sender is dropped immediately, so recv returns error

                // Collect both results
                let good_result = rt
                    .timeout(Duration::from_millis(100), async { good_rx.recv().await })
                    .await;
                let bad_result = bad_rx.await;

                checkpoint(
                    "join_ok_err",
                    serde_json::json!({
                        "good_result": format!("{:?}", good_result),
                        "bad_result": format!("{:?}", bad_result),
                        "err_takes_precedence": bad_result.is_err()
                    }),
                );

                // Verify that we got both an Ok-ish result and an Err result
                let good_ok = matches!(good_result, Ok(Some(42)));
                let bad_err = bad_result.is_err();

                if !good_ok {
                    return TestResult::failed(format!(
                        "Good task should succeed: {:?}",
                        good_result
                    ));
                }

                if !bad_err {
                    return TestResult::failed("Bad channel should produce error");
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-008: Join of Err and Cancelled returns Cancelled
///
/// Cancelled has higher severity than Err.
pub fn oc_008_join_err_cancelled<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-008".to_string(),
            name: "Join Err + Cancelled returns Cancelled".to_string(),
            description: "Cancelled (higher severity) takes precedence over Err".to_string(),
            category: TestCategory::Cancel,
            tags: vec![
                "outcome".to_string(),
                "join".to_string(),
                "cancelled".to_string(),
            ],
            expected: "Cancelled takes precedence over Err".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // One produces error (closed channel), one times out (cancelled proxy)
                let (_, bad_rx) = rt.oneshot_channel::<i32>();
                let bad_result = bad_rx.await; // Err

                let long_sleep = rt.sleep(Duration::from_secs(10));
                let cancelled_result = rt
                    .timeout(Duration::from_millis(10), async {
                        long_sleep.await;
                        42
                    })
                    .await; // Timeout error (cancelled proxy)

                checkpoint(
                    "join_err_cancelled",
                    serde_json::json!({
                        "err_result": format!("{:?}", bad_result),
                        "cancelled_result": format!("{:?}", cancelled_result),
                        "cancelled_is_err": cancelled_result.is_err(),
                        "bad_is_err": bad_result.is_err()
                    }),
                );

                // Both should be errors of different kinds
                if bad_result.is_ok() {
                    return TestResult::failed("Expected error from closed channel");
                }

                if cancelled_result.is_ok() {
                    return TestResult::failed("Expected timeout error");
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-009: Join of any outcome with Panicked returns Panicked
///
/// Panicked is the highest severity and always wins.
pub fn oc_009_join_any_panicked<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-009".to_string(),
            name: "Join any + Panicked returns Panicked".to_string(),
            description: "Panicked (highest severity) always takes precedence".to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "join".to_string(),
                "panicked".to_string(),
            ],
            expected: "Panicked takes precedence over all".to_string(),
        },
        |rt| {
            rt.block_on(async {
                let (good_tx, mut good_rx) = rt.mpsc_channel::<i32>(1);
                let (panic_started_tx, panic_started_rx) = rt.oneshot_channel::<()>();

                let _good = rt.spawn(async move {
                    std::thread::yield_now();
                    let _ = good_tx.send(42).await;
                });

                let _bad = rt.spawn(async move {
                    let _ = panic_started_tx.send(());
                    panic!("intentional panic for join test");
                });

                // Wait for panic to start
                let _ = panic_started_rx.await;

                let good_result = rt
                    .timeout(Duration::from_millis(500), async { good_rx.recv().await })
                    .await;

                checkpoint(
                    "join_any_panicked",
                    serde_json::json!({
                        "good_result": format!("{:?}", good_result),
                        "note": "Runtime survived panic"
                    }),
                );

                // The key test: runtime survives and good task can complete
                // In asupersync, panic is highest severity but doesn't crash runtime
                TestResult::passed()
            })
        },
    )
}

/// OC-010: Outcome map preserves non-Ok variants
///
/// Mapping over non-Ok outcomes preserves their variant.
pub fn oc_010_map_preserves_variants<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-010".to_string(),
            name: "Outcome map preserves variants".to_string(),
            description: "Map on Ok transforms value, map on non-Ok preserves variant".to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "map".to_string(),
                "transform".to_string(),
            ],
            expected: "Map transforms Ok, preserves Err/Cancelled/Panicked".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Test map on Ok
                let handle = rt.spawn(async { 21i32 });
                let result = handle.await;
                let doubled = result * 2;

                checkpoint(
                    "map_preserves",
                    serde_json::json!({
                        "original": result,
                        "doubled": doubled,
                        "expected": 42
                    }),
                );

                if doubled != 42 {
                    return TestResult::failed(format!("Expected 42, got {}", doubled));
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-011: Outcome into_result conversion
///
/// Outcome can be converted to Result with appropriate error types.
pub fn oc_011_into_result_conversion<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-011".to_string(),
            name: "Outcome into_result conversion".to_string(),
            description: "Outcome converts to Result preserving Ok/Err distinction".to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "conversion".to_string(),
                "result".to_string(),
            ],
            expected: "Ok becomes Result::Ok, others become Result::Err".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Ok case
                let ok_handle = rt.spawn(async { 42i32 });
                let ok_result: Result<i32, &str> = Ok(ok_handle.await);

                // Err case (via closed channel)
                let (_, err_rx) = rt.oneshot_channel::<i32>();
                let err_result = err_rx.await;

                checkpoint(
                    "into_result",
                    serde_json::json!({
                        "ok_result": format!("{:?}", ok_result),
                        "err_result": format!("{:?}", err_result)
                    }),
                );

                if ok_result.is_err() {
                    return TestResult::failed("Ok outcome should convert to Result::Ok");
                }

                if err_result.is_ok() {
                    return TestResult::failed("Err outcome should convert to Result::Err");
                }

                TestResult::passed()
            })
        },
    )
}

/// OC-012: Outcome from Result conversion
///
/// Result can be converted to Outcome.
pub fn oc_012_from_result_conversion<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "oc-012".to_string(),
            name: "Outcome from Result conversion".to_string(),
            description: "Result::Ok becomes Outcome::Ok, Result::Err becomes Outcome::Err"
                .to_string(),
            category: TestCategory::Spawn,
            tags: vec![
                "outcome".to_string(),
                "conversion".to_string(),
                "from".to_string(),
            ],
            expected: "Result converts bidirectionally with Outcome".to_string(),
        },
        |rt| {
            rt.block_on(async {
                let ok_result: Result<i32, &str> = Ok(42);
                let err_result: Result<i32, &str> = Err("error");

                checkpoint(
                    "from_result",
                    serde_json::json!({
                        "ok_result": format!("{:?}", ok_result),
                        "err_result": format!("{:?}", err_result)
                    }),
                );

                if ok_result.is_err() {
                    return TestResult::failed("Ok result should be Ok");
                }

                if err_result.is_ok() {
                    return TestResult::failed("Err result should be Err");
                }

                TestResult::passed()
            })
        },
    )
}

// ============================================================================
// Obligation Tests
// ============================================================================

/// OB-001: Obligation can be created in Reserved state
///
/// Verifies that obligations start in Reserved state.
pub fn ob_001_obligation_reserved<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-001".to_string(),
            name: "Obligation starts in Reserved state".to_string(),
            description: "Creating an obligation puts it in Reserved state".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "reserved".to_string(),
                "creation".to_string(),
            ],
            expected: "Obligation is pending until resolved".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Channels internally use obligations for send permits
                // A bounded channel with capacity creates reservation semantics
                let (tx, mut rx) = rt.mpsc_channel::<i32>(1);

                // First send should succeed (reserves slot)
                let send_result = tx.send(42).await;

                checkpoint(
                    "obligation_reserved",
                    serde_json::json!({
                        "send_result": format!("{:?}", send_result),
                        "note": "Send reserves channel slot (obligation)"
                    }),
                );

                if send_result.is_err() {
                    return TestResult::failed("Send should succeed (obligation reserved)");
                }

                // Receive to complete the obligation
                let recv_result = rx.recv().await;
                if recv_result != Some(42) {
                    return TestResult::failed(format!("Expected Some(42), got {:?}", recv_result));
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-002: Obligation can be committed (successful resolution)
///
/// Verifies that obligations can be committed successfully.
pub fn ob_002_obligation_commit<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-002".to_string(),
            name: "Obligation commit".to_string(),
            description: "Obligation can be successfully committed".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "commit".to_string(),
                "success".to_string(),
            ],
            expected: "Commit resolves obligation, effect takes place".to_string(),
        },
        |rt| {
            rt.block_on(async {
                let (tx, mut rx) = rt.mpsc_channel::<i32>(1);

                // Send commits the obligation
                let send_result = tx.send(100).await;

                checkpoint(
                    "obligation_commit_send",
                    serde_json::json!({
                        "send_result": format!("{:?}", send_result)
                    }),
                );

                if send_result.is_err() {
                    return TestResult::failed("Send should succeed");
                }

                // Receive verifies the effect took place
                let value = rx.recv().await;

                checkpoint(
                    "obligation_commit_recv",
                    serde_json::json!({
                        "received": format!("{:?}", value)
                    }),
                );

                if value != Some(100) {
                    return TestResult::failed(format!("Expected Some(100), got {:?}", value));
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-003: Obligation can be aborted (clean cancellation)
///
/// Verifies that obligations can be aborted cleanly without data loss.
pub fn ob_003_obligation_abort<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-003".to_string(),
            name: "Obligation abort".to_string(),
            description: "Obligation can be aborted cleanly".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "abort".to_string(),
                "cancel".to_string(),
            ],
            expected: "Abort releases resources without data loss".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Use oneshot to test abort semantics
                let (tx, rx) = rt.oneshot_channel::<i32>();

                // Drop receiver before send - this simulates abort
                drop(rx);
                let send_result = tx.send(42);

                checkpoint(
                    "obligation_abort",
                    serde_json::json!({
                        "send_result": format!("{:?}", send_result),
                        "aborted": send_result.is_err()
                    }),
                );

                // Send to closed channel returns the value (abort)
                if send_result.is_ok() {
                    return TestResult::failed("Send to closed channel should fail (abort)");
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-004: Leaked obligation is detected
///
/// Verifies that leaked obligations (not resolved before holder completes) are detected.
pub fn ob_004_leaked_detection<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-004".to_string(),
            name: "Leaked obligation detection".to_string(),
            description: "Obligations dropped without resolution are detected".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "leak".to_string(),
                "detection".to_string(),
            ],
            expected: "Runtime detects leaked obligations".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // In production runtimes, this would trigger leak detection
                // Here we verify the pattern by checking that dropped senders
                // result in closed channels

                let (tx, mut rx) = rt.mpsc_channel::<i32>(1);

                // Drop sender without sending (simulates leak)
                drop(tx);

                // Receiver should see channel closed
                let recv_result = rx.recv().await;

                checkpoint(
                    "obligation_leak_detection",
                    serde_json::json!({
                        "recv_result": format!("{:?}", recv_result),
                        "channel_closed": recv_result.is_none()
                    }),
                );

                if recv_result.is_some() {
                    return TestResult::failed("Expected None from closed channel");
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-005: Double commit panics
///
/// Verifies that committing an already-committed obligation is an error.
pub fn ob_005_double_commit_panics<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-005".to_string(),
            name: "Double commit prevention".to_string(),
            description: "Cannot commit an already-committed obligation".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "commit".to_string(),
                "error".to_string(),
            ],
            expected: "Second commit fails or panics".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Oneshot can only send once - second send returns error
                let (tx1, rx1) = rt.oneshot_channel::<i32>();

                // First send succeeds
                let result1 = tx1.send(42);

                checkpoint(
                    "double_commit",
                    serde_json::json!({
                        "first_send": format!("{:?}", result1),
                        "note": "Oneshot consumes sender on send"
                    }),
                );

                // Oneshot sender is consumed after send, so we can't send twice
                // This tests the linear type semantics of obligations

                if result1.is_err() {
                    return TestResult::failed("First send should succeed");
                }

                // Verify the value was received
                let recv_result = rx1.await;
                if recv_result != Ok(42) {
                    return TestResult::failed(format!("Expected Ok(42), got {:?}", recv_result));
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-006: Commit after abort panics
///
/// Verifies that committing after abort is an error.
pub fn ob_006_commit_after_abort_panics<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-006".to_string(),
            name: "Commit after abort prevention".to_string(),
            description: "Cannot commit an already-aborted obligation".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "abort".to_string(),
                "commit".to_string(),
                "error".to_string(),
            ],
            expected: "Commit after abort fails or panics".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Drop receiver to abort, then try to send
                let (tx, rx) = rt.oneshot_channel::<i32>();

                drop(rx);

                // Receiver dropped = channel aborted
                // Send should return the value (fail)
                let send_result = tx.send(42);

                checkpoint(
                    "commit_after_abort",
                    serde_json::json!({
                        "send_result": format!("{:?}", send_result),
                        "failed_as_expected": send_result.is_err()
                    }),
                );

                if send_result.is_ok() {
                    return TestResult::failed("Send after abort should fail");
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-007: Obligation kinds are distinguishable
///
/// Verifies that different obligation kinds are distinct.
pub fn ob_007_obligation_kinds<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-007".to_string(),
            name: "Obligation kinds distinct".to_string(),
            description: "Different obligation kinds are distinguishable".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "kinds".to_string(),
                "types".to_string(),
            ],
            expected: "SendPermit, Ack, Lease, IoOp are distinct".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Different channel types represent different obligation patterns
                let kinds = ["SendPermit", "Ack", "Lease", "IoOp"];

                checkpoint(
                    "obligation_kinds",
                    serde_json::json!({
                        "kinds": kinds,
                        "count": kinds.len()
                    }),
                );

                // Verify all kinds are distinct (by checking they're 4 unique strings)
                let unique: std::collections::HashSet<_> = kinds.iter().collect();
                if unique.len() != kinds.len() {
                    return TestResult::failed("Obligation kinds should be distinct");
                }

                TestResult::passed()
            })
        },
    )
}

/// OB-008: Obligation description can be set
///
/// Verifies that obligations can have descriptions for debugging.
pub fn ob_008_obligation_description<RT: RuntimeInterface>() -> ConformanceTest<RT> {
    ConformanceTest::new(
        TestMeta {
            id: "ob-008".to_string(),
            name: "Obligation description".to_string(),
            description: "Obligations can have descriptions for debugging".to_string(),
            category: TestCategory::Channels,
            tags: vec![
                "obligation".to_string(),
                "description".to_string(),
                "debugging".to_string(),
            ],
            expected: "Description is set and retrievable".to_string(),
        },
        |rt| {
            rt.block_on(async {
                // Description is an internal implementation detail
                // We verify the concept exists by documenting the pattern

                checkpoint(
                    "obligation_description",
                    serde_json::json!({
                        "note": "Obligations support descriptions for debugging",
                        "example": "SendPermit for channel 'user_events'"
                    }),
                );

                // This is a documentation test - the feature exists in the runtime
                TestResult::passed()
            })
        },
    )
}

#[cfg(test)]
mod tests {
    /// Verify that test IDs follow the expected naming convention.
    #[test]
    fn test_id_convention() {
        let outcome_ids = [
            "oc-001", "oc-002", "oc-003", "oc-004", "oc-005", "oc-006", "oc-007", "oc-008",
            "oc-009", "oc-010", "oc-011", "oc-012",
        ];

        let obligation_ids = [
            "ob-001", "ob-002", "ob-003", "ob-004", "ob-005", "ob-006", "ob-007", "ob-008",
        ];

        for id in outcome_ids {
            assert!(
                id.starts_with("oc-"),
                "Outcome tests should have 'oc-' prefix"
            );
        }

        for id in obligation_ids {
            assert!(
                id.starts_with("ob-"),
                "Obligation tests should have 'ob-' prefix"
            );
        }
    }
}