moonpool-sim 0.6.0

Simulation engine for the moonpool framework
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
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
//! Fault injection tests for storage simulation.
//!
//! These tests verify that the fault injection mechanisms work correctly
//! for various failure modes.

use moonpool_core::{OpenOptions, StorageFile, StorageProvider};
use moonpool_sim::{SimWorld, StorageConfiguration};
use std::net::IpAddr;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

const TEST_IP_STR: &str = "127.0.0.1";

fn test_ip() -> IpAddr {
    TEST_IP_STR.parse().expect("valid IP")
}

/// Helper to run an async storage test with proper simulation stepping.
async fn run_storage_test<F, Fut, T>(mut sim: SimWorld, f: F) -> T
where
    F: FnOnce(moonpool_sim::SimStorageProvider) -> Fut,
    Fut: std::future::Future<Output = T> + 'static,
    T: 'static,
{
    let provider = sim.storage_provider(test_ip());
    let handle = tokio::task::spawn_local(f(provider));

    while !handle.is_finished() {
        while sim.pending_event_count() > 0 {
            sim.step();
        }
        tokio::task::yield_now().await;
    }

    handle.await.expect("task panicked")
}

/// Create a local tokio runtime for tests.
fn local_runtime() -> tokio::runtime::LocalRuntime {
    tokio::runtime::Builder::new_current_thread()
        .enable_io()
        .enable_time()
        .build_local(Default::default())
        .expect("Failed to build local runtime")
}

/// Create a SimWorld with fast storage configuration.
fn fast_sim() -> SimWorld {
    let mut sim = SimWorld::new();
    sim.set_storage_config(StorageConfiguration::fast_local());
    sim
}

#[test]
fn test_read_corruption_fault() {
    local_runtime().block_on(async {
        // Create config with guaranteed read corruption
        let mut config = StorageConfiguration::fast_local();
        config.read_fault_probability = 1.0; // 100% corruption

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let result: std::io::Result<bool> = run_storage_test(sim, |provider| async move {
            // Write known data
            let mut file = provider
                .open("corrupt.txt", OpenOptions::create_write())
                .await?;
            let original = b"hello world!";
            file.write_all(original).await?;
            file.sync_all().await?;
            drop(file);

            // Read it back - with 100% corruption, data should be different
            let mut file = provider
                .open("corrupt.txt", OpenOptions::read_only())
                .await?;
            let mut buf = vec![0u8; original.len()];
            file.read_exact(&mut buf).await?;

            // Check if data was corrupted
            let corrupted = &buf != original;
            Ok(corrupted)
        })
        .await;

        let was_corrupted = result.expect("test failed");
        assert!(
            was_corrupted,
            "Read should have been corrupted with 100% probability"
        );
    });
}

#[test]
fn test_write_corruption_fault() {
    local_runtime().block_on(async {
        // Create config with guaranteed write corruption
        let mut config = StorageConfiguration::fast_local();
        config.write_fault_probability = 1.0; // 100% corruption

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let result: std::io::Result<bool> = run_storage_test(sim, |provider| async move {
            // Write data
            let mut file = provider
                .open("write_corrupt.txt", OpenOptions::create_write())
                .await?;
            let original = b"test data for write corruption";
            file.write_all(original).await?;
            file.sync_all().await?;
            drop(file);

            // Read without corruption (set a new file with no read faults)
            let mut file = provider
                .open("write_corrupt.txt", OpenOptions::read_only())
                .await?;
            let mut buf = vec![0u8; original.len()];
            file.read_exact(&mut buf).await?;

            // The data written may have been corrupted
            let corrupted = &buf != original;
            Ok(corrupted)
        })
        .await;

        // With write corruption, the data should be different
        let was_corrupted = result.expect("test failed");
        assert!(
            was_corrupted,
            "Write should have been corrupted with 100% probability"
        );
    });
}

#[test]
fn test_misdirected_write_fault() {
    local_runtime().block_on(async {
        // Create config with misdirected writes
        let mut config = StorageConfiguration::fast_local();
        config.misdirect_write_probability = 1.0; // 100% misdirection

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let result: std::io::Result<Vec<u8>> = run_storage_test(sim, |provider| async move {
            // Create a file and write sequentially
            let mut file = provider
                .open("misdirect.txt", OpenOptions::create_write())
                .await?;

            // Write known pattern
            for i in 0..10u8 {
                file.write_all(&[i]).await?;
            }
            file.sync_all().await?;
            drop(file);

            // Read back
            let mut file = provider
                .open("misdirect.txt", OpenOptions::read_only())
                .await?;
            let mut buf = Vec::new();
            file.read_to_end(&mut buf).await?;

            Ok(buf)
        })
        .await;

        let data = result.expect("test failed");

        // With misdirection, data order may not be sequential 0-9
        let expected: Vec<u8> = (0..10).collect();
        let is_normal = data == expected;

        // Either the data is wrong OR it could happen to be correct by chance
        // Just verify we got data back
        assert!(!data.is_empty(), "Should have gotten some data");
        println!(
            "Misdirection test: data was {} normal order",
            if is_normal { "in" } else { "NOT in" }
        );
    });
}

#[test]
fn test_misdirected_read_fault() {
    local_runtime().block_on(async {
        // Create config with misdirected reads
        let mut config = StorageConfiguration::fast_local();
        config.misdirect_read_probability = 1.0; // 100% misdirection

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let result: std::io::Result<Vec<u8>> = run_storage_test(sim, |provider| async move {
            // Create a file with known data pattern
            let mut file = provider
                .open("misdirect_read.txt", OpenOptions::create_write())
                .await?;
            file.write_all(b"AABBCCDDEE").await?;
            file.sync_all().await?;
            drop(file);

            // Read back with misdirection
            let mut file = provider
                .open("misdirect_read.txt", OpenOptions::read_only())
                .await?;
            let mut buf = Vec::new();
            file.read_to_end(&mut buf).await?;

            Ok(buf)
        })
        .await;

        let data = result.expect("test failed");

        // With read misdirection, data may come from wrong positions
        println!(
            "Misdirected read result: {:?}",
            String::from_utf8_lossy(&data)
        );
        assert!(!data.is_empty(), "Should have gotten some data");
    });
}

#[test]
fn test_phantom_write_fault() {
    local_runtime().block_on(async {
        // Create config with phantom writes (writes silently lost)
        let mut config = StorageConfiguration::fast_local();
        config.phantom_write_probability = 1.0; // 100% phantom

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let result: std::io::Result<Vec<u8>> = run_storage_test(sim, |provider| async move {
            // Write data that should be "lost"
            let mut file = provider
                .open("phantom.txt", OpenOptions::create_write())
                .await?;
            file.write_all(b"this data should be lost").await?;
            file.sync_all().await?;
            drop(file);

            // Read back - with phantom writes, data may be zeros or missing
            let mut file = provider
                .open("phantom.txt", OpenOptions::read_only())
                .await?;
            let mut buf = Vec::new();
            file.read_to_end(&mut buf).await?;

            Ok(buf)
        })
        .await;

        let data = result.expect("test failed");

        // With 100% phantom writes, the written data should NOT be persisted
        let expected = b"this data should be lost";
        let all_zeros = data.iter().all(|&b| b == 0);

        println!(
            "Phantom write result: {:?} (all zeros: {})",
            data, all_zeros
        );

        // Either empty, all zeros, or not matching expected
        assert!(
            data.is_empty() || all_zeros || &data != expected,
            "Phantom write should have lost the data"
        );
    });
}

#[test]
fn test_sync_failure_fault() {
    local_runtime().block_on(async {
        // Create config with sync failures
        let mut config = StorageConfiguration::fast_local();
        config.sync_failure_probability = 1.0; // 100% failure

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let result: std::io::Result<()> = run_storage_test(sim, |provider| async move {
            let mut file = provider
                .open("sync_fail.txt", OpenOptions::create_write())
                .await?;
            file.write_all(b"test").await?;

            // This should fail with 100% sync failure probability
            file.sync_all().await?;

            Ok(())
        })
        .await;

        // With 100% sync failure, this should be an error
        assert!(
            result.is_err(),
            "Sync should have failed with 100% failure probability"
        );
        println!("Sync failure error: {:?}", result.unwrap_err());
    });
}

#[test]
fn test_crash_torn_writes() {
    local_runtime().block_on(async {
        let mut config = StorageConfiguration::fast_local();
        config.crash_fault_probability = 0.0; // Disable random crashes, we'll trigger manually

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        // Write data but don't sync, then simulate crash
        let provider = sim.storage_provider(test_ip());
        let handle = tokio::task::spawn_local(async move {
            let mut file = provider
                .open("crash.txt", OpenOptions::create_write())
                .await
                .expect("open failed");
            file.write_all(b"unsynced data")
                .await
                .expect("write failed");
            // DON'T sync - data should be in pending state
            drop(file);
            Ok::<_, std::io::Error>(())
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }
        handle.await.expect("task panicked").expect("io error");

        // Simulate crash - this should apply torn write simulation
        sim.simulate_crash_for_process(test_ip(), true);

        // Verify the crash was processed
        // After crash, files are closed and pending writes may be corrupted
        let provider2 = sim.storage_provider(test_ip());
        let handle2 = tokio::task::spawn_local(async move {
            // File should still exist but data may be corrupted
            let exists = provider2.exists("crash.txt").await?;
            Ok::<_, std::io::Error>(exists)
        });

        while !handle2.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let exists = handle2.await.expect("task panicked").expect("io error");
        println!("File exists after crash: {}", exists);
        // The file may or may not exist depending on implementation
    });
}

#[test]
fn test_uninitialized_reads() {
    local_runtime().block_on(async {
        let result: std::io::Result<Vec<u8>> =
            run_storage_test(fast_sim(), |provider| async move {
                // Create a file and extend it without writing
                let file = provider
                    .open("uninit.txt", OpenOptions::create_write())
                    .await?;
                file.set_len(100).await?; // Extend to 100 bytes without writing
                file.sync_all().await?;
                drop(file);

                // Read the uninitialized region
                let mut file = provider
                    .open("uninit.txt", OpenOptions::read_only())
                    .await?;
                let mut buf = Vec::new();
                file.read_to_end(&mut buf).await?;

                Ok(buf)
            })
            .await;

        let data = result.expect("test failed");
        assert_eq!(data.len(), 100, "File should be 100 bytes");

        // Uninitialized data should be zeros or random depending on implementation
        // Most implementations return zeros for extended but never-written regions
        println!("Uninitialized read: first 10 bytes = {:?}", &data[..10]);
    });
}

// ============================================================================
// Mixed Fault Probability Tests
// ============================================================================

/// Test with realistic low fault probabilities (like production scenarios)
#[test]
fn test_mixed_low_fault_probabilities() {
    local_runtime().block_on(async {
        // Realistic fault probabilities - low but non-zero
        let mut config = StorageConfiguration::fast_local();
        config.read_fault_probability = 0.01; // 1%
        config.write_fault_probability = 0.01; // 1%
        config.sync_failure_probability = 0.005; // 0.5%

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        // Run many operations and count successes/failures
        let mut write_successes = 0;
        let mut write_failures = 0;
        let mut read_successes = 0;
        let mut read_failures = 0;

        for i in 0..100 {
            let provider = sim.storage_provider(test_ip());
            let filename = format!("mixed_{}.txt", i);

            // Try write
            let handle = tokio::task::spawn_local(async move {
                let mut file = provider
                    .open(&filename, OpenOptions::create_write())
                    .await?;
                file.write_all(b"test data").await?;
                file.sync_all().await?;
                drop(file);
                Ok::<_, std::io::Error>(())
            });

            while !handle.is_finished() {
                while sim.pending_event_count() > 0 {
                    sim.step();
                }
                tokio::task::yield_now().await;
            }

            match handle.await.expect("task panicked") {
                Ok(()) => write_successes += 1,
                Err(_) => write_failures += 1,
            }

            // Try read (if write succeeded)
            if write_successes > read_successes + read_failures {
                let provider2 = sim.storage_provider(test_ip());
                let filename = format!("mixed_{}.txt", i);
                let handle2 = tokio::task::spawn_local(async move {
                    let mut file = provider2.open(&filename, OpenOptions::read_only()).await?;
                    let mut buf = Vec::new();
                    file.read_to_end(&mut buf).await?;
                    Ok::<_, std::io::Error>(())
                });

                while !handle2.is_finished() {
                    while sim.pending_event_count() > 0 {
                        sim.step();
                    }
                    tokio::task::yield_now().await;
                }

                match handle2.await.expect("task panicked") {
                    Ok(()) => read_successes += 1,
                    Err(_) => read_failures += 1,
                }
            }
        }

        println!(
            "Mixed faults: writes {}/{}, reads {}/{}",
            write_successes,
            write_successes + write_failures,
            read_successes,
            read_successes + read_failures
        );

        // With 1% fault rates over 100 operations, we expect some failures
        // but not all failures
        assert!(
            write_successes > 0,
            "Some writes should succeed with low fault rates"
        );
        assert!(
            read_successes > 0,
            "Some reads should succeed with low fault rates"
        );
    });
}

/// Test with multiple fault types enabled simultaneously
#[test]
fn test_combined_fault_types() {
    local_runtime().block_on(async {
        let mut config = StorageConfiguration::fast_local();
        config.read_fault_probability = 0.1; // 10%
        config.write_fault_probability = 0.1; // 10%
        config.misdirect_read_probability = 0.05; // 5%
        config.misdirect_write_probability = 0.05; // 5%
        config.phantom_write_probability = 0.05; // 5%

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let provider = sim.storage_provider(test_ip());

        // Run operations - expect various fault behaviors
        let handle = tokio::task::spawn_local(async move {
            let mut results = Vec::new();

            for i in 0..20 {
                let filename = format!("combined_{}.txt", i);

                // Write
                let write_result = async {
                    let mut file = provider
                        .open(&filename, OpenOptions::create_write())
                        .await?;
                    file.write_all(format!("Data {}", i).as_bytes()).await?;
                    file.sync_all().await?;
                    Ok::<_, std::io::Error>(())
                }
                .await;

                // Read back if write succeeded
                let read_result = if write_result.is_ok() {
                    async {
                        let mut file = provider.open(&filename, OpenOptions::read_only()).await?;
                        let mut buf = Vec::new();
                        file.read_to_end(&mut buf).await?;
                        Ok::<_, std::io::Error>(buf)
                    }
                    .await
                } else {
                    Err(std::io::Error::new(
                        std::io::ErrorKind::Other,
                        "write failed",
                    ))
                };

                results.push((write_result.is_ok(), read_result.is_ok()));
            }

            Ok::<_, std::io::Error>(results)
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let results = handle.await.expect("task panicked").expect("test failed");

        let writes_ok = results.iter().filter(|(w, _)| *w).count();
        let reads_ok = results.iter().filter(|(_, r)| *r).count();

        println!(
            "Combined faults: {} writes OK, {} reads OK out of 20",
            writes_ok, reads_ok
        );

        // With combined 10-20% fault rates, expect mixed results
        // Not all should fail, not all should succeed
    });
}

// ============================================================================
// Fault Determinism Tests
// ============================================================================

/// Test that same seed produces same fault pattern
#[test]
fn test_fault_determinism_same_seed() {
    use moonpool_sim::set_sim_seed;

    local_runtime().block_on(async {
        let seed = 42u64;

        // Run with specific seed
        set_sim_seed(seed);

        let mut config = StorageConfiguration::fast_local();
        config.read_fault_probability = 0.5; // 50% for visibility

        let mut sim1 = SimWorld::new();
        sim1.set_storage_config(config.clone());

        let provider = sim1.storage_provider(test_ip());
        let handle = tokio::task::spawn_local(async move {
            let mut results = Vec::new();
            for i in 0..10 {
                let filename = format!("det_{}.txt", i);

                // Create file first
                let mut file = provider
                    .open(&filename, OpenOptions::create_write())
                    .await
                    .expect("create failed");
                file.write_all(b"test").await.expect("write failed");
                file.sync_all().await.expect("sync failed");
                drop(file);

                // Read back
                let result = async {
                    let mut file = provider.open(&filename, OpenOptions::read_only()).await?;
                    let mut buf = Vec::new();
                    file.read_to_end(&mut buf).await?;
                    Ok::<_, std::io::Error>(buf)
                }
                .await;

                results.push(result.is_ok());
            }
            results
        });

        while !handle.is_finished() {
            while sim1.pending_event_count() > 0 {
                sim1.step();
            }
            tokio::task::yield_now().await;
        }

        let results1 = handle.await.expect("task panicked");

        // Run again with same seed
        set_sim_seed(seed);

        let mut sim2 = SimWorld::new();
        sim2.set_storage_config(config);

        let provider = sim2.storage_provider(test_ip());
        let handle = tokio::task::spawn_local(async move {
            let mut results = Vec::new();
            for i in 0..10 {
                let filename = format!("det_{}.txt", i);

                let mut file = provider
                    .open(&filename, OpenOptions::create_write())
                    .await
                    .expect("create failed");
                file.write_all(b"test").await.expect("write failed");
                file.sync_all().await.expect("sync failed");
                drop(file);

                let result = async {
                    let mut file = provider.open(&filename, OpenOptions::read_only()).await?;
                    let mut buf = Vec::new();
                    file.read_to_end(&mut buf).await?;
                    Ok::<_, std::io::Error>(buf)
                }
                .await;

                results.push(result.is_ok());
            }
            results
        });

        while !handle.is_finished() {
            while sim2.pending_event_count() > 0 {
                sim2.step();
            }
            tokio::task::yield_now().await;
        }

        let results2 = handle.await.expect("task panicked");

        println!("Run 1: {:?}", results1);
        println!("Run 2: {:?}", results2);

        assert_eq!(
            results1, results2,
            "Same seed should produce same fault pattern"
        );
    });
}

/// Test that fault injection produces a mix of corrupted and intact reads
#[test]
fn test_fault_injection_produces_mixed_results() {
    use moonpool_sim::set_sim_seed;

    local_runtime().block_on(async {
        set_sim_seed(12345);

        let mut config = StorageConfiguration::fast_local();
        config.read_fault_probability = 0.5; // 50% read corruption

        let mut sim = SimWorld::new();
        sim.set_storage_config(config);

        let provider = sim.storage_provider(test_ip());
        let handle = tokio::task::spawn_local(async move {
            let mut corrupted_count = 0;
            let mut intact_count = 0;
            let original_data = b"testdata";

            for i in 0..20 {
                let filename = format!("mixed_{}.txt", i);

                let mut file = provider
                    .open(&filename, OpenOptions::create_write())
                    .await
                    .expect("create failed");
                file.write_all(original_data).await.expect("write failed");
                file.sync_all().await.expect("sync failed");
                drop(file);

                // Read back and check if corrupted
                let mut file = provider
                    .open(&filename, OpenOptions::read_only())
                    .await
                    .expect("open failed");
                let mut buf = vec![0u8; original_data.len()];
                file.read_exact(&mut buf).await.expect("read failed");

                if buf != original_data {
                    corrupted_count += 1;
                } else {
                    intact_count += 1;
                }
            }
            (corrupted_count, intact_count)
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let (corrupted, intact) = handle.await.expect("task panicked");

        println!(
            "Fault injection results: {} corrupted, {} intact out of 20",
            corrupted, intact
        );

        // With 50% fault probability over 20 operations, we should see
        // a mix of corrupted and intact reads (not all one or the other)
        assert!(
            corrupted > 0,
            "With 50% fault probability, some reads should be corrupted"
        );
        assert!(
            intact > 0,
            "With 50% fault probability, some reads should be intact"
        );
    });
}

/// Test corruption content is deterministic with same seed
#[test]
fn test_corruption_content_deterministic() {
    use moonpool_sim::set_sim_seed;

    local_runtime().block_on(async {
        let seed = 12345u64;

        let run_corruption_test = || async {
            set_sim_seed(seed);

            let mut config = StorageConfiguration::fast_local();
            config.read_fault_probability = 1.0; // 100% corruption

            let mut sim = SimWorld::new();
            sim.set_storage_config(config);

            let provider = sim.storage_provider(test_ip());
            let handle = tokio::task::spawn_local(async move {
                // Write known data
                let mut file = provider
                    .open("corrupt.txt", OpenOptions::create_write())
                    .await?;
                file.write_all(b"ABCDEFGH").await?;
                file.sync_all().await?;
                drop(file);

                // Read - will be corrupted
                let mut file = provider
                    .open("corrupt.txt", OpenOptions::read_only())
                    .await?;
                let mut buf = vec![0u8; 8];
                file.read_exact(&mut buf).await?;
                Ok::<_, std::io::Error>(buf)
            });

            while !handle.is_finished() {
                while sim.pending_event_count() > 0 {
                    sim.step();
                }
                tokio::task::yield_now().await;
            }

            handle.await.expect("task panicked").expect("io error")
        };

        let corrupted1 = run_corruption_test().await;
        let corrupted2 = run_corruption_test().await;

        println!("Corruption 1: {:?}", corrupted1);
        println!("Corruption 2: {:?}", corrupted2);

        assert_eq!(
            corrupted1, corrupted2,
            "Corruption pattern should be deterministic with same seed"
        );
    });
}

// ============================================================================
// Per-Process Storage Fault Isolation Tests
// ============================================================================

/// Test that per-process storage configs provide fault isolation between IPs.
#[test]
fn test_per_process_storage_config_isolation() {
    local_runtime().block_on(async {
        let ip1: IpAddr = "10.0.1.1".parse().expect("valid IP");
        let ip2: IpAddr = "10.0.1.2".parse().expect("valid IP");

        let mut sim = SimWorld::new();
        // Base config with no faults
        sim.set_storage_config(StorageConfiguration::fast_local());

        // IP1: 100% read corruption
        let mut config_ip1 = StorageConfiguration::fast_local();
        config_ip1.read_fault_probability = 1.0;
        sim.set_process_storage_config(ip1, config_ip1);

        // IP2: 0% faults (clean reads)
        let config_ip2 = StorageConfiguration::fast_local();
        sim.set_process_storage_config(ip2, config_ip2);

        let provider1 = sim.storage_provider(ip1);
        let provider2 = sim.storage_provider(ip2);

        let original = b"hello world!";

        // Write known data through both providers (different paths per IP)
        let handle = tokio::task::spawn_local(async move {
            // Write via IP1
            let mut file1 = provider1
                .open("ip1_data.txt", OpenOptions::create_write())
                .await?;
            file1.write_all(original).await?;
            file1.sync_all().await?;
            drop(file1);

            // Write via IP2
            let mut file2 = provider2
                .open("ip2_data.txt", OpenOptions::create_write())
                .await?;
            file2.write_all(original).await?;
            file2.sync_all().await?;
            drop(file2);

            // Read via IP1 - should be corrupted
            let mut file1 = provider1
                .open("ip1_data.txt", OpenOptions::read_only())
                .await?;
            let mut buf1 = vec![0u8; original.len()];
            file1.read_exact(&mut buf1).await?;

            // Read via IP2 - should be intact
            let mut file2 = provider2
                .open("ip2_data.txt", OpenOptions::read_only())
                .await?;
            let mut buf2 = vec![0u8; original.len()];
            file2.read_exact(&mut buf2).await?;

            Ok::<_, std::io::Error>((buf1, buf2))
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let (buf1, buf2) = handle.await.expect("task panicked").expect("io error");

        assert_ne!(
            &buf1[..],
            original.as_slice(),
            "IP1 read should be corrupted (100% read fault)"
        );
        assert_eq!(
            &buf2[..],
            original.as_slice(),
            "IP2 read should be intact (0% fault)"
        );
    });
}

/// Test that simulate_crash_for_process only affects the targeted process.
#[test]
fn test_simulate_crash_for_process_isolation() {
    local_runtime().block_on(async {
        let ip1: IpAddr = "10.0.1.1".parse().expect("valid IP");
        let ip2: IpAddr = "10.0.1.2".parse().expect("valid IP");

        let mut sim = fast_sim();

        let provider1 = sim.storage_provider(ip1);
        let provider2 = sim.storage_provider(ip2);

        // Write and sync data through both providers (different paths per IP)
        let handle = tokio::task::spawn_local(async move {
            let mut file1 = provider1
                .open("ip1_crash.txt", OpenOptions::create_write())
                .await?;
            file1.write_all(b"ip1 data").await?;
            file1.sync_all().await?;
            drop(file1);

            let mut file2 = provider2
                .open("ip2_crash.txt", OpenOptions::create_write())
                .await?;
            file2.write_all(b"ip2 data").await?;
            file2.sync_all().await?;
            drop(file2);

            Ok::<_, std::io::Error>(())
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }
        handle.await.expect("task panicked").expect("io error");

        // Crash only IP1
        sim.simulate_crash_for_process(ip1, true);

        // IP2 should still be able to read its file
        let provider2_after = sim.storage_provider(ip2);
        let handle2 = tokio::task::spawn_local(async move {
            let mut file = provider2_after
                .open("ip2_crash.txt", OpenOptions::read_only())
                .await?;
            let mut buf = Vec::new();
            file.read_to_end(&mut buf).await?;
            Ok::<_, std::io::Error>(buf)
        });

        while !handle2.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let ip2_data = handle2.await.expect("task panicked").expect("io error");
        assert_eq!(
            &ip2_data[..],
            b"ip2 data",
            "IP2 file should be unaffected by IP1 crash"
        );
    });
}

/// Test that wipe_storage_for_process only deletes files for the targeted process.
#[test]
fn test_wipe_storage_for_process() {
    local_runtime().block_on(async {
        let ip1: IpAddr = "10.0.1.1".parse().expect("valid IP");
        let ip2: IpAddr = "10.0.1.2".parse().expect("valid IP");

        let mut sim = fast_sim();

        let provider1 = sim.storage_provider(ip1);
        let provider2 = sim.storage_provider(ip2);

        // Write files through both providers (different paths per IP)
        let handle = tokio::task::spawn_local(async move {
            let mut file1 = provider1
                .open("ip1_wipe.txt", OpenOptions::create_write())
                .await?;
            file1.write_all(b"ip1 data").await?;
            file1.sync_all().await?;
            drop(file1);

            let mut file2 = provider2
                .open("ip2_wipe.txt", OpenOptions::create_write())
                .await?;
            file2.write_all(b"ip2 data").await?;
            file2.sync_all().await?;
            drop(file2);

            Ok::<_, std::io::Error>(())
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }
        handle.await.expect("task panicked").expect("io error");

        // Wipe only IP1's storage
        sim.wipe_storage_for_process(ip1);

        // Check IP1's file is gone
        let provider1_after = sim.storage_provider(ip1);
        let handle1 = tokio::task::spawn_local(async move {
            let exists = provider1_after.exists("ip1_wipe.txt").await?;
            Ok::<_, std::io::Error>(exists)
        });

        while !handle1.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let ip1_exists = handle1.await.expect("task panicked").expect("io error");
        assert!(!ip1_exists, "IP1 file should be deleted after wipe");

        // Check IP2's file still exists and is readable
        let provider2_after = sim.storage_provider(ip2);
        let handle2 = tokio::task::spawn_local(async move {
            let exists = provider2_after.exists("ip2_wipe.txt").await?;
            if exists {
                let mut file = provider2_after
                    .open("ip2_wipe.txt", OpenOptions::read_only())
                    .await?;
                let mut buf = Vec::new();
                file.read_to_end(&mut buf).await?;
                Ok::<_, std::io::Error>((true, buf))
            } else {
                Ok((false, Vec::new()))
            }
        });

        while !handle2.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let (ip2_exists, ip2_data) = handle2.await.expect("task panicked").expect("io error");
        assert!(ip2_exists, "IP2 file should still exist after IP1 wipe");
        assert_eq!(
            &ip2_data[..],
            b"ip2 data",
            "IP2 file data should be intact after IP1 wipe"
        );
    });
}

/// Test that per-process sync failure config is isolated between IPs.
#[test]
fn test_per_process_sync_failure_isolation() {
    local_runtime().block_on(async {
        let ip1: IpAddr = "10.0.1.1".parse().expect("valid IP");
        let ip2: IpAddr = "10.0.1.2".parse().expect("valid IP");

        let mut sim = SimWorld::new();
        sim.set_storage_config(StorageConfiguration::fast_local());

        // IP1: 100% sync failure
        let mut config_ip1 = StorageConfiguration::fast_local();
        config_ip1.sync_failure_probability = 1.0;
        sim.set_process_storage_config(ip1, config_ip1);

        // IP2: 0% sync failure
        sim.set_process_storage_config(ip2, StorageConfiguration::fast_local());

        let provider1 = sim.storage_provider(ip1);
        let provider2 = sim.storage_provider(ip2);

        let handle = tokio::task::spawn_local(async move {
            // IP1: sync should fail
            let mut file1 = provider1
                .open("ip1_sync.txt", OpenOptions::create_write())
                .await?;
            file1.write_all(b"data").await?;
            let sync_result1 = file1.sync_all().await;

            // IP2: sync should succeed
            let mut file2 = provider2
                .open("ip2_sync.txt", OpenOptions::create_write())
                .await?;
            file2.write_all(b"data").await?;
            let sync_result2 = file2.sync_all().await;

            Ok::<_, std::io::Error>((sync_result1, sync_result2))
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let (sync1, sync2) = handle.await.expect("task panicked").expect("io error");
        assert!(
            sync1.is_err(),
            "IP1 sync should fail (100% sync_failure_probability)"
        );
        assert!(
            sync2.is_ok(),
            "IP2 sync should succeed (0% sync_failure_probability)"
        );
    });
}

/// Test that phantom write isolation works per process.
///
/// IP1 has 100% phantom writes (writes appear to succeed but don't persist).
/// IP2 has 0% phantom writes. After crash, IP1's data is lost, IP2's survives.
#[test]
fn test_per_process_phantom_write_isolation() {
    local_runtime().block_on(async {
        let ip1: IpAddr = "10.0.1.1".parse().expect("valid IP");
        let ip2: IpAddr = "10.0.1.2".parse().expect("valid IP");

        let mut sim = SimWorld::new();
        sim.set_storage_config(StorageConfiguration::fast_local());

        // IP1: 100% phantom writes
        let mut config_ip1 = StorageConfiguration::fast_local();
        config_ip1.phantom_write_probability = 1.0;
        config_ip1.crash_fault_probability = 0.0;
        sim.set_process_storage_config(ip1, config_ip1);

        // IP2: no faults
        sim.set_process_storage_config(ip2, StorageConfiguration::fast_local());

        let provider1 = sim.storage_provider(ip1);
        let provider2 = sim.storage_provider(ip2);

        // Write and sync through both
        let handle = tokio::task::spawn_local(async move {
            let mut file1 = provider1
                .open("ip1_phantom.txt", OpenOptions::create_write())
                .await?;
            file1.write_all(b"phantom data").await?;
            file1.sync_all().await?;
            drop(file1);

            let mut file2 = provider2
                .open("ip2_phantom.txt", OpenOptions::create_write())
                .await?;
            file2.write_all(b"real data").await?;
            file2.sync_all().await?;
            drop(file2);

            Ok::<_, std::io::Error>(())
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }
        handle.await.expect("task panicked").expect("io error");

        // Crash both to reveal phantom writes
        sim.simulate_crash_for_process(ip1, false);
        sim.simulate_crash_for_process(ip2, false);

        // Read back: IP1's phantom writes should be lost, IP2's real writes survive
        let provider1 = sim.storage_provider(ip1);
        let provider2 = sim.storage_provider(ip2);

        let handle = tokio::task::spawn_local(async move {
            let mut file1 = provider1
                .open("ip1_phantom.txt", OpenOptions::read_only())
                .await?;
            let mut buf1 = Vec::new();
            file1.read_to_end(&mut buf1).await?;

            let mut file2 = provider2
                .open("ip2_phantom.txt", OpenOptions::read_only())
                .await?;
            let mut buf2 = Vec::new();
            file2.read_to_end(&mut buf2).await?;

            Ok::<_, std::io::Error>((buf1, buf2))
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let (buf1, buf2) = handle.await.expect("task panicked").expect("io error");
        // IP1: phantom writes mean data was never actually written
        assert_ne!(
            &buf1[..],
            b"phantom data",
            "IP1 phantom writes should not persist after crash"
        );
        // IP2: real writes should survive
        assert_eq!(
            &buf2[..],
            b"real data",
            "IP2 real writes should survive crash"
        );
    });
}

/// Test that global default config is used when no per-process config is set.
#[test]
fn test_default_config_fallback() {
    local_runtime().block_on(async {
        let ip: IpAddr = "10.0.1.1".parse().expect("valid IP");

        let mut sim = SimWorld::new();

        // Set global config with 100% read faults — do NOT set per-process config
        let mut config = StorageConfiguration::fast_local();
        config.read_fault_probability = 1.0;
        sim.set_storage_config(config);

        let provider = sim.storage_provider(ip);
        let original = b"test data here";

        let handle = tokio::task::spawn_local(async move {
            let mut file = provider
                .open("fallback.txt", OpenOptions::create_write())
                .await?;
            file.write_all(original).await?;
            file.sync_all().await?;
            drop(file);

            let mut file = provider
                .open("fallback.txt", OpenOptions::read_only())
                .await?;
            let mut buf = vec![0u8; original.len()];
            file.read_exact(&mut buf).await?;

            Ok::<_, std::io::Error>(buf)
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let buf = handle.await.expect("task panicked").expect("io error");
        assert_ne!(
            &buf[..],
            original.as_slice(),
            "Global fallback config should apply when no per-process config is set"
        );
    });
}

/// Test that wiped storage can be recreated by the same process.
#[test]
fn test_wipe_then_recreate() {
    local_runtime().block_on(async {
        let ip: IpAddr = "10.0.1.1".parse().expect("valid IP");

        let mut sim = fast_sim();
        let provider = sim.storage_provider(ip);

        // Create and write a file
        let handle = tokio::task::spawn_local(async move {
            let mut file = provider
                .open("recoverable.txt", OpenOptions::create_write())
                .await?;
            file.write_all(b"original").await?;
            file.sync_all().await?;
            drop(file);
            Ok::<_, std::io::Error>(())
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }
        handle.await.expect("task panicked").expect("io error");

        // Wipe storage
        sim.wipe_storage_for_process(ip);

        // Recreate the file at the same path
        let provider = sim.storage_provider(ip);
        let handle = tokio::task::spawn_local(async move {
            // File should not exist after wipe
            let exists = provider.exists("recoverable.txt").await?;
            assert!(!exists, "File should not exist after wipe");

            // Create new file at same path
            let mut file = provider
                .open("recoverable.txt", OpenOptions::create_write())
                .await?;
            file.write_all(b"recreated").await?;
            file.sync_all().await?;
            drop(file);

            // Read it back
            let mut file = provider
                .open("recoverable.txt", OpenOptions::read_only())
                .await?;
            let mut buf = Vec::new();
            file.read_to_end(&mut buf).await?;

            Ok::<_, std::io::Error>(buf)
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let buf = handle.await.expect("task panicked").expect("io error");
        assert_eq!(
            &buf[..],
            b"recreated",
            "Recreated file should contain new data"
        );
    });
}

/// Test that changing per-process config after files are open affects subsequent operations.
#[test]
fn test_dynamic_config_change() {
    local_runtime().block_on(async {
        let ip: IpAddr = "10.0.1.1".parse().expect("valid IP");

        let mut sim = SimWorld::new();
        sim.set_storage_config(StorageConfiguration::fast_local());

        // Start with clean config
        sim.set_process_storage_config(ip, StorageConfiguration::fast_local());

        let provider = sim.storage_provider(ip);
        let original = b"dynamic test";

        // Write data with clean config
        let p = provider.clone();
        let handle = tokio::task::spawn_local(async move {
            let mut file = p.open("dynamic.txt", OpenOptions::create_write()).await?;
            file.write_all(original).await?;
            file.sync_all().await?;
            drop(file);
            Ok::<_, std::io::Error>(())
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }
        handle.await.expect("task panicked").expect("io error");

        // Now change config to 100% read faults — file is already created
        let mut faulty_config = StorageConfiguration::fast_local();
        faulty_config.read_fault_probability = 1.0;
        sim.set_process_storage_config(ip, faulty_config);

        // Read with the new faulty config — should see corruption
        let handle = tokio::task::spawn_local(async move {
            let mut file = provider
                .open("dynamic.txt", OpenOptions::read_only())
                .await?;
            let mut buf = vec![0u8; original.len()];
            file.read_exact(&mut buf).await?;
            Ok::<_, std::io::Error>(buf)
        });

        while !handle.is_finished() {
            while sim.pending_event_count() > 0 {
                sim.step();
            }
            tokio::task::yield_now().await;
        }

        let buf = handle.await.expect("task panicked").expect("io error");
        assert_ne!(
            &buf[..],
            original.as_slice(),
            "Dynamically changed config should affect reads on existing files"
        );
    });
}