wasm4pm 26.7.1

High-performance process mining algorithms in WebAssembly for JavaScript/TypeScript
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
//! Algorithm Correctness Tests — Mathematical Property Oracles
//!
//! Each test proves a property that a stub or placeholder implementation CANNOT satisfy:
//!
//!   Rank 1 — Mathematical theorem:
//!     • Convergence: more iterations → final_fitness never decreases
//!     • Fitness range: all values ∈ [0.0, 1.0]
//!     • Determinism: fixed seed → bit-identical output across two runs
//!
//!   Rank 2 — Domain contract:
//!     • GA elitism: population sorted by fitness; top 25% survive every generation
//!     • SA best-tracking: best_fitness is monotone non-decreasing throughout the run
//!     • Hill Climbing pruning: edge count can only stay the same or decrease
//!     • ILP structure: one transition per activity, source place has initial token = 1
//!
//! Tests use pure-Rust `_from_log` variants so they run on native without the wasm-bindgen
//! runtime (which requires a wasm32 target to be fully functional).

use std::collections::BTreeMap;
use wasm4pm::advanced_algorithms::discover_heuristic_miner_from_log;
use wasm4pm::algorithms::discover_dfg_filtered_from_log;
use wasm4pm::algorithms::discover_footprints_from_log;
use wasm4pm::discovery::discover_dfg_from_log;
use wasm4pm::fast_discovery::{discover_astar_from_log, discover_hill_climbing_from_log};
use wasm4pm::genetic_discovery::{
    discover_aco_algorithm_from_log, discover_genetic_algorithm_from_log,
    discover_pso_algorithm_from_log,
};
use wasm4pm::ilp_discovery::discover_ilp_petri_net_from_log;
use wasm4pm::models::{AttributeValue, Event, EventLog, Trace};
use wasm4pm::more_discovery::{
    discover_inductive_miner_from_log, discover_simulated_annealing_from_log,
};
use wasm4pm::social_network::{
    discover_handover_network_from_log, discover_working_together_network_from_log,
};
use wasm4pm::temporal_profile::discover_temporal_profile_from_log;

// ---------------------------------------------------------------------------
// Shared fixture helpers
// ---------------------------------------------------------------------------

/// Build a controlled EventLog from (count, activities) pairs.
fn build_log(variants: &[(usize, &[&str])]) -> EventLog {
    let mut log = EventLog::new();
    let mut case_idx = 0usize;
    for (repeat, activities) in variants {
        for _ in 0..*repeat {
            let mut trace = Trace {
                attributes: {
                    let mut m = BTreeMap::new();
                    m.insert(
                        "concept:name".to_string(),
                        AttributeValue::String(format!("case-{}", case_idx)),
                    );
                    m
                },
                events: Vec::new(),
            };
            for (i, &act) in activities.iter().enumerate() {
                let mut attrs = BTreeMap::new();
                attrs.insert(
                    "concept:name".to_string(),
                    AttributeValue::String(act.to_string()),
                );
                attrs.insert(
                    "time:timestamp".to_string(),
                    AttributeValue::String(format!("2024-01-01T00:{:02}:00Z", i)),
                );
                trace.events.push(Event { attributes: attrs });
            }
            log.traces.push(trace);
            case_idx += 1;
        }
    }
    log
}

/// The controlled log used in most tests:
///   10× [Start, Register, Approve, End]
///    5× [Start, Register, Reject,  End]
/// 5 unique directly-follows edges, 5 activities.
fn controlled_log() -> EventLog {
    build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ])
}

// ---------------------------------------------------------------------------
// Genetic Algorithm — Rank 1 + Rank 2 properties
// ---------------------------------------------------------------------------

#[test]
fn ga_fitness_in_range() {
    let log = controlled_log();
    let (_, f) = discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("GA must produce a result for non-empty log");
    assert!(
        (0.0..=1.0).contains(&f),
        "GA fitness {:.4} outside [0, 1]",
        f
    );
}

/// Convergence (Rank 1): GA with 100 generations must achieve fitness ≥ 1 generation.
/// The elitism mechanism preserves the best individual across generations, so more
/// generations can only keep or improve the best solution — never worsen it.
#[test]
fn ga_convergence_more_generations_never_worse() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let (_, f1) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 1).expect("GA must succeed");
    let (_, f100) = discover_genetic_algorithm_from_log(&log, "concept:name", 20, 100)
        .expect("GA must succeed");

    assert!(
        f100 >= f1 - 1e-9,
        "GA: 100-gen fitness {:.4} < 1-gen fitness {:.4} — elitism invariant violated",
        f100,
        f1
    );
}

/// Determinism (Rank 1): same seed → bit-identical fitness across two calls on cloned logs.
#[test]
fn ga_deterministic_same_seed() {
    let log = controlled_log();
    let (_, f1) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 50).expect("GA must succeed");
    let (_, f2) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 50).expect("GA must succeed");
    assert_eq!(
        f1, f2,
        "GA is not deterministic: different fitness {:.6} vs {:.6} on identical inputs",
        f1, f2
    );
}

/// Structural (Rank 2): GA output DFG must have nodes from log vocabulary and non-negative edge count.
#[test]
fn ga_output_structure_valid() {
    let log = controlled_log();
    let (dfg, _) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30).expect("GA must succeed");
    assert!(
        !dfg.nodes.is_empty(),
        "GA DFG must contain at least one node"
    );
    // All node IDs must be activity names from the log vocabulary
    let activities = ["Start", "Register", "Approve", "Reject", "End"];
    for node in &dfg.nodes {
        assert!(
            activities.contains(&node.id.as_str()),
            "GA node '{}' is not from log vocabulary — stub returning random data?",
            node.id
        );
    }
}

// ---------------------------------------------------------------------------
// Particle Swarm Optimization — Rank 1 + Rank 2 properties
// ---------------------------------------------------------------------------

#[test]
fn pso_fitness_in_range() {
    let log = controlled_log();
    let (_, f) = discover_pso_algorithm_from_log(&log, "concept:name", 20, 30)
        .expect("PSO must produce a result for non-empty log");
    assert!(
        (0.0..=1.0).contains(&f),
        "PSO fitness {:.4} outside [0, 1]",
        f
    );
}

/// Convergence (Rank 1): global best is monotone non-decreasing by construction.
/// 50 iterations with the same swarm must achieve fitness ≥ 5 iterations.
#[test]
fn pso_convergence_more_iterations_never_worse() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let (_, f5) =
        discover_pso_algorithm_from_log(&log, "concept:name", 20, 5).expect("PSO must succeed");
    let (_, f50) =
        discover_pso_algorithm_from_log(&log, "concept:name", 20, 50).expect("PSO must succeed");

    assert!(
        f50 >= f5 - 1e-9,
        "PSO: 50-iter fitness {:.4} < 5-iter fitness {:.4} — global-best monotone violated",
        f50,
        f5
    );
}

/// Determinism (Rank 1): fixed seed → identical output.
#[test]
fn pso_deterministic_same_seed() {
    let log = controlled_log();
    let (_, f1) =
        discover_pso_algorithm_from_log(&log, "concept:name", 20, 50).expect("PSO must succeed");
    let (_, f2) =
        discover_pso_algorithm_from_log(&log, "concept:name", 20, 50).expect("PSO must succeed");
    assert_eq!(f1, f2, "PSO is not deterministic");
}

// ---------------------------------------------------------------------------
// Simulated Annealing — Rank 1 + Rank 2 properties
// ---------------------------------------------------------------------------

#[test]
fn sa_fitness_in_range() {
    let log = controlled_log();
    let (_, f) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.95);
    assert!(
        (0.0..=1.0).contains(&f),
        "SA fitness {:.4} outside [0, 1]",
        f
    );
}

/// Boltzmann slow-cooling property (Rank 1):
/// A slow cooling schedule (0.99) explores more states than fast cooling (0.5),
/// so on a simple 2-variant log it should achieve at least as good fitness.
#[test]
fn sa_slow_cooling_not_much_worse_than_fast() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let (_, f_fast) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.50);
    let (_, f_slow) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.99);
    // Slow cooling may or may not outperform on a given run; we allow 5% slack.
    assert!(
        f_slow >= f_fast - 0.05,
        "SA slow-cooling fitness {:.4} is >5% worse than fast-cooling {:.4}",
        f_slow,
        f_fast
    );
}

/// Best-tracking non-negativity (Rank 2):
/// SA always tracks the best-seen solution. Starting from empty edge set (fitness≥0),
/// and never updating best unless improved, the returned fitness must be ≥ 0.
#[test]
fn sa_best_tracking_nonnegative() {
    let log = controlled_log();
    let (_, f) = discover_simulated_annealing_from_log(&log, "concept:name", 0.9, 0.95);
    assert!(f >= 0.0, "SA returned negative fitness {:.4}", f);
}

/// Determinism (Rank 1): fixed seed → identical fitness.
#[test]
fn sa_deterministic_same_seed() {
    let log = controlled_log();
    let (_, f1) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.95);
    let (_, f2) = discover_simulated_annealing_from_log(&log, "concept:name", 1.0, 0.95);
    assert_eq!(f1, f2, "SA is not deterministic");
}

// ---------------------------------------------------------------------------
// Hill Climbing — Rank 1 + Rank 2 properties
// ---------------------------------------------------------------------------

/// Pruning invariant (Rank 1, mathematical):
/// Hill climbing starts with ALL observed edges and only removes cost-0 edges.
/// It NEVER adds edges. Therefore output_edges ≤ max_possible_edges.
#[test]
fn hill_climbing_never_increases_edge_count() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let max_edges = 5usize; // Start→Register, Register→{Approve,Reject}, {Approve,Reject}→End
    let dfg = discover_hill_climbing_from_log(&log, "concept:name");
    assert!(
        dfg.edges.len() <= max_edges,
        "Hill climbing output {} edges > max possible {} — edges were ADDED, not only removed",
        dfg.edges.len(),
        max_edges
    );
}

/// Local optimum on essential-only log (Rank 2):
/// When all traces follow a single path, every edge appears in every trace exactly once,
/// giving it a removal cost = N traces. No edge has cost 0, so hill climbing must
/// preserve all edges.
#[test]
fn hill_climbing_preserves_all_essential_edges() {
    let log = build_log(&[(10, &["A", "B", "C", "D"])]);
    let dfg = discover_hill_climbing_from_log(&log, "concept:name");
    assert_eq!(
        dfg.edges.len(),
        3,
        "Hill climbing removed an essential edge from single-variant log \
         (got {} edges, expected 3: A→B, B→C, C→D)",
        dfg.edges.len()
    );
}

/// Determinism (Rank 1): identical logs → identical output.
#[test]
fn hill_climbing_deterministic() {
    let log = controlled_log();
    let dfg1 = discover_hill_climbing_from_log(&log, "concept:name");
    let dfg2 = discover_hill_climbing_from_log(&log, "concept:name");
    assert_eq!(
        dfg1.edges.len(),
        dfg2.edges.len(),
        "Hill climbing is not deterministic"
    );
}

// ---------------------------------------------------------------------------
// A* (Best-First Search) — Rank 2 properties
// ---------------------------------------------------------------------------

/// Termination and bounded iterations (Rank 2):
/// A* must terminate within max_iterations and return a valid DFG.
#[test]
fn astar_terminates_and_bounded() {
    let log = controlled_log();
    let (dfg, iters) = discover_astar_from_log(&log, "concept:name", 500);
    assert!(
        iters <= 500,
        "A* ran {} iterations > max_iterations=500",
        iters
    );
    assert!(
        !dfg.nodes.is_empty(),
        "A* DFG must have at least one node from log activities"
    );
}

/// Fitness threshold invariant (Rank 1):
/// A*'s filter `fitness > 0.5` means every edge added contributed fitness > 0.5.
/// On the controlled log (5 edges, all highly frequent), the output must be non-empty.
#[test]
fn astar_finds_at_least_one_edge() {
    let log = controlled_log();
    let (dfg, _) = discover_astar_from_log(&log, "concept:name", 1000);
    assert!(
        !dfg.edges.is_empty(),
        "A* found 0 edges for a non-trivial log with 5 high-frequency edges \
         — fitness threshold may be too aggressive or algorithm is a stub"
    );
}

// ---------------------------------------------------------------------------
// ILP (Direct Petri Net Construction) — Rank 1 structural soundness
// ---------------------------------------------------------------------------

/// Bijection invariant (Rank 1 — mathematical):
/// The algorithm creates exactly one transition per unique activity.
#[test]
fn ilp_transitions_match_activities() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let activities = 5usize; // Start, Register, Approve, Reject, End
    let (pn, _, _) = discover_ilp_petri_net_from_log(&log, "concept:name");
    assert_eq!(
        pn.transitions.len(),
        activities,
        "ILP must produce exactly 1 transition per unique activity (expected {}, got {})",
        activities,
        pn.transitions.len()
    );
}

/// Workflow soundness — source initial marking (Rank 1):
/// A sound workflow net requires exactly 1 token in the source place at start.
#[test]
fn ilp_source_place_has_initial_token() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let (pn, _, _) = discover_ilp_petri_net_from_log(&log, "concept:name");
    let source_token = pn.initial_marking.get("p_source").copied().unwrap_or(0);
    assert_eq!(
        source_token, 1,
        "source place must have initial marking = 1 (sound workflow net invariant)"
    );
}

/// Fitness range (Rank 1): fitness ∈ [0, 1] and precision ∈ [0, 1].
#[test]
fn ilp_fitness_and_precision_in_range() {
    let log = build_log(&[
        (10, &["Start", "Register", "Approve", "End"]),
        (5, &["Start", "Register", "Reject", "End"]),
    ]);
    let (_, fitness, precision) = discover_ilp_petri_net_from_log(&log, "concept:name");
    assert!(
        (0.0..=1.0).contains(&fitness),
        "ILP fitness {:.4} outside [0, 1]",
        fitness
    );
    assert!(
        (0.0..=1.0).contains(&precision),
        "ILP precision {:.4} outside [0, 1]",
        precision
    );
}

/// High fitness on a perfectly-fitting log (Rank 1 — mathematical):
/// The ILP region-based algorithm selects consistent places that cover all causal
/// pairs; on a single-variant log [A,B,C,D] every trace replays without missing
/// tokens. token_replay_pure reports ≥ 0.8 (the final sink token counts as
/// "remaining" in the van der Aalst formula, giving 0.875 for 4-step traces).
#[test]
fn ilp_perfect_fitness_on_fitting_log() {
    let log = build_log(&[(10, &["A", "B", "C", "D"])]);
    let (_, fitness, _) = discover_ilp_petri_net_from_log(&log, "concept:name");
    assert!(
        fitness >= 0.8,
        "ILP fitness on single-variant fitting log must be >= 0.8, got {:.4}",
        fitness
    );
}

/// ILP detects parallel AND-split: log where A always precedes B and C in parallel.
/// Variant 1: A→B→D (10 traces), Variant 2: A→C→D (10 traces)
/// B and C are parallel (both follow A, both precede D, unordered w.r.t. each other).
/// The region-based ILP should produce fewer places than the DFG stub (which would
/// produce 4 places: A→B, A→C, B→D, C→D). Validates candidate generation + greedy cover.
#[test]
fn ilp_detects_parallel_and_split() {
    let log = build_log(&[(10, &["A", "B", "D"]), (10, &["A", "C", "D"])]);
    let (pn, fitness, _) = discover_ilp_petri_net_from_log(&log, "concept:name");
    // Must produce a valid model that replays both variants
    assert!(
        fitness >= 0.5,
        "ILP must achieve fitness >= 0.5 on parallel-split log, got {:.4}",
        fitness
    );
    // Must have source and sink at minimum
    assert!(
        pn.places.len() >= 2,
        "ILP must produce at least source and sink places"
    );
    // Must produce transitions for all 4 activities
    assert_eq!(
        pn.transitions.len(),
        4,
        "ILP must produce 4 transitions (A,B,C,D), got {}",
        pn.transitions.len()
    );
}

/// ILP detects self-loop (L1L activity): log where A has a self-loop.
/// The region-based ILP should add a self-loop place for A.
#[test]
fn ilp_detects_self_loop_place() {
    let log = build_log(&[(10, &["A", "A", "B"])]);
    let (pn, _, _) = discover_ilp_petri_net_from_log(&log, "concept:name");
    let has_loop_place = pn.places.iter().any(|p| p.id.contains("loop"));
    assert!(
        has_loop_place,
        "L1L activity A should produce a self-loop place"
    );
}

/// ILP output is a valid Petri net structure: source has initial marking,
/// transitions match activities, arcs are non-empty.
#[test]
fn ilp_output_is_valid_petri_net() {
    let log = build_log(&[(10, &["X", "Y", "Z"])]);
    let (pn, _, _) = discover_ilp_petri_net_from_log(&log, "concept:name");
    // Source place must have initial marking
    let source = pn.places.iter().find(|p| p.id == "p_source");
    assert!(source.is_some(), "Petri net must have p_source place");
    assert_eq!(
        source.unwrap().marking,
        Some(1),
        "p_source must have initial marking 1"
    );
    // Must have transitions for each activity
    assert_eq!(pn.transitions.len(), 3, "Must have 3 transitions for X,Y,Z");
    // Must have arcs
    assert!(!pn.arcs.is_empty(), "Petri net must have arcs");
}

/// Alpha++ output has correct structure: returns a Petri net (not DFG).
/// The real Alpha++ implementation produces places, transitions, and arcs.
#[test]
fn alpha_plus_plus_output_is_petri_net() {
    use wasm4pm::algorithms::discover_alpha_plus_plus_from_log;
    let log = build_log(&[(10, &["A", "B", "C"]), (5, &["A", "C", "B"])]);
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log).into_evidence();
    let pn = discover_alpha_plus_plus_from_log(&admitted, "concept:name", 0.0)
        .expect("alpha_plus_plus must succeed");
    assert!(!pn.places.is_empty(), "Alpha++ must produce places");
    assert!(
        !pn.transitions.is_empty(),
        "Alpha++ must produce transitions"
    );
    assert!(!pn.arcs.is_empty(), "Alpha++ must produce arcs");
    assert!(
        pn.places.iter().any(|p| p.id == "p_source"),
        "Alpha++ Petri net must have p_source"
    );
}

// ---------------------------------------------------------------------------
// DFG correctness
// ---------------------------------------------------------------------------

/// All DFG edges from discover_dfg_from_log must have frequency > 0.
/// A stub returning phantom edges would fail this.
#[test]
fn dfg_edges_have_positive_frequency() {
    let log = controlled_log();
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log).into_evidence();
    let dfg = discover_dfg_from_log(&admitted, "concept:name");
    assert!(
        !dfg.edges.is_empty(),
        "DFG must have at least one edge for a non-trivial log"
    );
    for edge in &dfg.edges {
        assert!(
            edge.frequency > 0,
            "DFG edge {}→{} has frequency 0",
            edge.from,
            edge.to
        );
    }
}

/// DFG filtered with threshold 0 must produce the same edge count as unfiltered.
/// DFG filtered with threshold > max frequency must produce 0 edges.
#[test]
fn dfg_filtered_threshold_monotone() {
    let log = controlled_log();
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log.clone()).into_evidence();
    let unfiltered = discover_dfg_filtered_from_log(&admitted, "concept:name", 0);
    let dfg = discover_dfg_from_log(&admitted, "concept:name");
    assert_eq!(
        unfiltered.edges.len(),
        dfg.edges.len(),
        "filtered(min=0) edge count must equal unfiltered"
    );

    let filtered = discover_dfg_filtered_from_log(&admitted, "concept:name", 999_999);
    assert!(
        filtered.edges.is_empty(),
        "filtered(min=999999) must produce no edges"
    );
}

/// Heuristic miner with high threshold must have ≤ edges than full DFG.
/// Rank 2 domain contract: filtering is monotone — more threshold → fewer edges.
#[test]
fn heuristic_miner_fewer_edges_than_dfg() {
    let log = controlled_log();
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log).into_evidence();
    let dfg = discover_dfg_from_log(&admitted, "concept:name");
    let hm = discover_heuristic_miner_from_log(&admitted.value, "concept:name", 0.5);
    assert!(
        hm.edges.len() <= dfg.edges.len(),
        "heuristic miner (threshold=0.5) must have ≤ DFG edges; got hm={} dfg={}",
        hm.edges.len(),
        dfg.edges.len()
    );
}

// ---------------------------------------------------------------------------
// Footprint matrix
// ---------------------------------------------------------------------------

/// Causal relation must be antisymmetric: if A→B is causal then B→A must not be.
/// Rank 1 — mathematical theorem from Alpha Miner definition.
#[test]
fn footprints_causal_antisymmetric() {
    use wasm4pm::algorithms::FootprintRelation;
    let log = build_log(&[(5, &["A", "B", "C"]), (5, &["A", "C", "B"])]);
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log).into_evidence();
    let fp = discover_footprints_from_log(&admitted, "concept:name");
    let n = fp.activities.len();
    for i in 0..n {
        for j in 0..n {
            if fp.matrix[i][j] == FootprintRelation::Causal {
                assert_ne!(
                    fp.matrix[j][i],
                    FootprintRelation::Causal,
                    "footprint antisymmetry violated: {}→{} is Causal AND {}→{} is Causal",
                    fp.activities[i],
                    fp.activities[j],
                    fp.activities[j],
                    fp.activities[i]
                );
            }
        }
    }
}

// ---------------------------------------------------------------------------
// ACO correctness
// ---------------------------------------------------------------------------

/// ACO fitness must be in [0, 1].
#[test]
fn aco_fitness_in_range() {
    let log = controlled_log();
    let result = discover_aco_algorithm_from_log(&log, "concept:name", 5, 10);
    let (dfg, fitness) = result.expect("ACO must find a solution on controlled_log");
    assert!(
        (0.0..=1.0).contains(&fitness),
        "ACO final_fitness out of [0,1]: {:.4}",
        fitness
    );
    assert!(!dfg.nodes.is_empty(), "ACO DFG must have nodes");
}

/// Two ACO runs with same seed must produce bit-identical fitness.
#[test]
fn aco_deterministic_same_seed() {
    let log = controlled_log();
    let r1 = discover_aco_algorithm_from_log(&log, "concept:name", 5, 5);
    let r2 = discover_aco_algorithm_from_log(&log, "concept:name", 5, 5);
    let (_, f1) = r1.expect("ACO run 1 failed");
    let (_, f2) = r2.expect("ACO run 2 failed");
    assert_eq!(
        f1.to_bits(),
        f2.to_bits(),
        "ACO must be deterministic: f1={:.6} f2={:.6}",
        f1,
        f2
    );
}

// ---------------------------------------------------------------------------
// Temporal profile
// ---------------------------------------------------------------------------

/// All temporal profile mean durations must be non-negative.
/// Rank 1 — mathematical: time only flows forward in valid XES logs.
#[test]
fn temporal_profile_nonnegative_durations() {
    use wasm4pm::models::AttributeValue;

    // Build log with real timestamps so the temporal profile has data.
    let mut log = EventLog::new();
    let base_ms: i64 = 1_700_000_000_000;
    for i in 0..5usize {
        let mut trace = Trace::new();
        trace.attributes.insert(
            "concept:name".to_string(),
            AttributeValue::String(format!("case-{i}")),
        );
        for (j, act) in ["A", "B", "C"].iter().enumerate() {
            let ts_ms = base_ms + (i as i64 * 10_000) + (j as i64 * 3_600_000);
            let ts_str = format!(
                "{}-{:02}-{:02}T{:02}:{:02}:{:02}+00:00",
                2023,
                11,
                1 + (ts_ms / 86_400_000 % 28) as u32,
                (ts_ms / 3_600_000 % 24) as u32,
                (ts_ms / 60_000 % 60) as u32,
                (ts_ms / 1_000 % 60) as u32,
            );
            let mut event = Event::new();
            event.attributes.insert(
                "concept:name".to_string(),
                AttributeValue::String(act.to_string()),
            );
            event
                .attributes
                .insert("time:timestamp".to_string(), AttributeValue::Date(ts_str));
            trace.events.push(event);
        }
        log.traces.push(trace);
    }

    let profile = discover_temporal_profile_from_log(&log, "concept:name", "time:timestamp");
    assert!(
        !profile.pairs.is_empty(),
        "temporal profile must have at least one pair"
    );
    for ((a, b), (mean, _stdev, _cnt)) in &profile.pairs {
        assert!(
            *mean >= 0.0,
            "temporal profile mean for {}→{} is negative: {:.2}",
            a,
            b,
            mean
        );
    }
}

// ---------------------------------------------------------------------------
// Social network
// ---------------------------------------------------------------------------

/// Handover count for a single-resource log must be zero (no resource change).
/// Rank 2 domain contract: handover requires A ≠ B on consecutive events.
#[test]
fn social_handover_single_resource_is_zero() {
    let mut log = EventLog::new();
    let mut trace = Trace::new();
    trace.attributes.insert(
        "concept:name".to_string(),
        AttributeValue::String("case-1".to_string()),
    );
    for act in &["A", "B", "C"] {
        let mut event = Event::new();
        event.attributes.insert(
            "concept:name".to_string(),
            AttributeValue::String(act.to_string()),
        );
        event.attributes.insert(
            "org:resource".to_string(),
            AttributeValue::String("Alice".to_string()),
        );
        trace.events.push(event);
    }
    log.traces.push(trace);

    let json_str = discover_handover_network_from_log(&log, "org:resource");
    let v: serde_json::Value = serde_json::from_str(&json_str).expect("valid JSON");
    let edges = v["edges"].as_array().expect("edges array");
    assert!(
        edges.is_empty(),
        "single-resource log must have 0 handover edges, got {}",
        edges.len()
    );
}

/// Working-together: two resources in the same trace must appear as co-occurrence edge.
#[test]
fn social_working_together_same_trace_produces_edge() {
    let mut log = EventLog::new();
    let mut trace = Trace::new();
    trace.attributes.insert(
        "concept:name".to_string(),
        AttributeValue::String("case-1".to_string()),
    );
    for (act, res) in &[("A", "Alice"), ("B", "Bob")] {
        let mut event = Event::new();
        event.attributes.insert(
            "concept:name".to_string(),
            AttributeValue::String(act.to_string()),
        );
        event.attributes.insert(
            "org:resource".to_string(),
            AttributeValue::String(res.to_string()),
        );
        trace.events.push(event);
    }
    log.traces.push(trace);

    let json_str = discover_working_together_network_from_log(&log, "org:resource");
    let v: serde_json::Value = serde_json::from_str(&json_str).expect("valid JSON");
    let edges = v["edges"].as_array().expect("edges array");
    assert_eq!(
        edges.len(),
        1,
        "two resources in one trace → exactly 1 co-occurrence edge"
    );
    let cnt = edges[0]["co_occurrences"].as_u64().unwrap_or(0);
    assert_eq!(cnt, 1, "co_occurrences must be 1");
}

// ---------------------------------------------------------------------------
// Bug-fix regression tests (Rank 2 — domain contract)
// ---------------------------------------------------------------------------

/// Hill Climbing produces real edge frequencies (not hardcoded 1) and is
/// a sound pruner: output ≤ full DFG edge count.
/// Rank 2 — domain contract: HC can only remove edges, and frequency must
/// reflect the observed log statistics.
#[test]
fn hc_prunes_below_dfg() {
    let log = build_log(&[
        (20, &["Start", "Process", "End"]),
        (20, &["Start", "Process", "Review", "End"]),
    ]);
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log.clone()).into_evidence();
    let dfg = discover_dfg_from_log(&admitted, "concept:name");
    let hc = discover_hill_climbing_from_log(&log, "concept:name");

    // Monotonicity: HC can only remove edges.
    assert!(
        hc.edges.len() <= dfg.edges.len(),
        "HC edge count {} must be ≤ DFG edge count {}",
        hc.edges.len(),
        dfg.edges.len()
    );

    // Frequencies must be real (>0) and at least one dominant edge must have
    // frequency matching its actual observation count (≥ 20 here).
    assert!(
        !hc.edges.is_empty(),
        "HC must produce at least one edge for a non-trivial log"
    );
    for edge in &hc.edges {
        assert!(
            edge.frequency > 0,
            "HC edge {}→{} has frequency 0 (frequencies must be real)",
            edge.from,
            edge.to
        );
    }
    let max_freq = hc.edges.iter().map(|e| e.frequency).max().unwrap_or(0);
    assert!(
        max_freq >= 20,
        "HC dominant edge frequency must be ≥ 20 (observed 20 times); got max={}",
        max_freq
    );
}

/// A* must successfully discover a DFG on a log with more than 100 directly-follows
/// pairs — the old `/100` penalty would cap the score to 0 at that scale.
/// Rank 1 — mathematical: result must be non-empty for any non-trivial log.
#[test]
fn astar_beyond_100_edges() {
    // 12 activities → up to 11 directly-follows pairs per trace; 3 variants → 12+ unique pairs.
    let log = build_log(&[
        (
            5,
            &["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"],
        ),
        (
            5,
            &["A", "C", "B", "D", "F", "E", "G", "I", "H", "J", "L", "K"],
        ),
        (
            5,
            &["L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A"],
        ),
    ]);
    let (dfg, _iters) = discover_astar_from_log(&log, "concept:name", 500);
    assert!(
        !dfg.edges.is_empty(),
        "A* must produce at least one edge on a large log"
    );
    // All edges in the result must reference valid activity names.
    let node_ids: std::collections::HashSet<&str> =
        dfg.nodes.iter().map(|n| n.id.as_str()).collect();
    for edge in &dfg.edges {
        assert!(
            node_ids.contains(edge.from.as_str()),
            "A* edge from={} references unknown node",
            edge.from
        );
        assert!(
            node_ids.contains(edge.to.as_str()),
            "A* edge to={} references unknown node",
            edge.to
        );
    }
}

/// GA/PSO/ACO edges must carry real frequency values (> 0) rather than the
/// previous hardcoded constant 1. On a log where every edge appears multiple
/// times, the max edge frequency must exceed 1.
/// Rank 2 — domain contract: frequency reflects observed log statistics.
#[test]
fn ga_edges_have_real_frequency() {
    // 20 traces each with [A, B, C, D] → each edge appears exactly 20 times.
    let log = build_log(&[(20, &["A", "B", "C", "D"])]);

    let (ga_dfg, _) =
        discover_genetic_algorithm_from_log(&log, "concept:name", 20, 30).expect("GA must succeed");
    let max_freq = ga_dfg.edges.iter().map(|e| e.frequency).max().unwrap_or(0);
    assert!(
        max_freq > 1,
        "GA edges must have real frequency (> 1 for repeated-trace log); got max={}",
        max_freq
    );

    let (pso_dfg, _) =
        discover_pso_algorithm_from_log(&log, "concept:name", 10, 20).expect("PSO must succeed");
    let pso_max = pso_dfg.edges.iter().map(|e| e.frequency).max().unwrap_or(0);
    assert!(
        pso_max > 1,
        "PSO edges must have real frequency; got max={}",
        pso_max
    );

    // ACO with enough ants to reliably find non-empty solutions.
    let (aco_dfg, _) =
        discover_aco_algorithm_from_log(&log, "concept:name", 30, 30).expect("ACO must succeed");
    // Verify structural correctness: any selected edges must have real frequencies.
    for edge in &aco_dfg.edges {
        assert!(
            edge.frequency > 0,
            "ACO edge {}→{} has frequency 0 (must be > 0 for observed edges)",
            edge.from,
            edge.to
        );
    }
    // With 30 ants × 30 iterations and a single-variant log, ACO must find the
    // full path — all 3 edges with frequency 20.
    if !aco_dfg.edges.is_empty() {
        let aco_max = aco_dfg.edges.iter().map(|e| e.frequency).max().unwrap_or(0);
        assert!(
            aco_max > 1,
            "ACO edges must carry real frequency (> 1); got max={}",
            aco_max
        );
    }
}

/// Inductive Miner must detect a parallel cut when activities are bidirectionally
/// connected in the directly-follows graph.
/// Log: [A, B, C] and [A, C, B] → B and C are parallel (both B→C and C→B exist).
/// Rank 1 — structural property: parallel cut exists iff groups >1 via Union-Find.
#[test]
fn inductive_parallel_cut_fires() {
    // Equal mix ensures both B→C and C→B are observed (bidirectional).
    let log = build_log(&[(5, &["A", "B", "C"]), (5, &["A", "C", "B"])]);
    let admitted = wasm4pm_compat::admission::Admission::<_, ()>::new(log).into_evidence();
    let json_str = discover_inductive_miner_from_log(&admitted, "concept:name");
    let v: serde_json::Value =
        serde_json::from_str(&json_str).expect("inductive miner must return valid JSON");

    // Walk the tree and collect all operator types.
    fn collect_operators(node: &serde_json::Value, ops: &mut Vec<String>) {
        if let Some(t) = node["node_type"].as_str() {
            ops.push(t.to_string());
        }
        if let Some(children) = node["children"].as_array() {
            for child in children {
                collect_operators(child, ops);
            }
        }
    }

    let mut operators = Vec::new();
    collect_operators(&v["root"], &mut operators);

    assert!(
        operators.contains(&"parallel".to_string()),
        "Inductive Miner must produce a 'parallel' node for a log with bidirectional \
         B↔C edges; got operators: {:?}",
        operators
    );
}

// ---------------------------------------------------------------------------
// A* best-tracking regression — iter-12 fix
// ---------------------------------------------------------------------------

/// Rank-1 mathematical: best-so-far is monotone non-decreasing in max_iterations.
/// Specifically, max_iterations=1 must NOT return an empty DFG when the log has
/// fitness-positive edges. Pre-fix bug: `best_dfg` was only updated using the
/// score of the popped node, so iteration 0 (which pops the empty-seed DFG with
/// score=0) would set best to empty; on max_iterations=1 the run returned an
/// empty DFG even though the expansion produced fitness>0 children.
#[test]
fn astar_max_iter_1_returns_non_empty_when_fitness_positive() {
    let log = controlled_log();
    let (dfg, iters) = discover_astar_from_log(&log, "concept:name", 1);
    assert_eq!(
        iters, 1,
        "A* must consume exactly 1 iteration when budget is 1; got {}",
        iters
    );
    assert!(
        !dfg.edges.is_empty(),
        "A* with max_iterations=1 returned 0 edges — regression to score-of-popped-node bug"
    );
}

/// Rank-1 monotone: more iterations cannot reduce the produced edge count below
/// what 1 iteration finds (best-so-far never regresses on a deterministic seed).
/// This catches lag-by-one-iteration regressions in best-tracking.
#[test]
fn astar_more_iter_never_fewer_edges() {
    let log = controlled_log();
    let (dfg1, _) = discover_astar_from_log(&log, "concept:name", 1);
    let (dfg_more, _) = discover_astar_from_log(&log, "concept:name", 20);
    assert!(
        dfg_more.edges.len() >= dfg1.edges.len(),
        "A* @ 20 iter has {} edges < 1 iter has {} edges — best-tracking regressed",
        dfg_more.edges.len(),
        dfg1.edges.len()
    );
}

// ---------------------------------------------------------------------------
// PSO pBest position correctness — iter-12 fix
// ---------------------------------------------------------------------------

/// Rank-2 domain contract: PSO global best is monotone non-decreasing in
/// iterations on a deterministic seed. Combined with iteration count, this
/// catches regressions where pBest position is destructively reset.
///
/// Pre-fix bug: pBest position was never copied when fitness improved (only
/// pBest fitness was assigned), and the initial pBest was `HashSet::new()`
/// (empty). The first iteration's blend-toward-pbest pulled toward an empty
/// set, dropping 20% of each particle's edges. This is masked from the global
/// best (which has its own monotone safety net) but degrades quality.
#[test]
fn pso_global_best_at_least_as_good_as_initial_spawn() {
    let log = controlled_log();
    let (_, f1) =
        discover_pso_algorithm_from_log(&log, "concept:name", 30, 1).expect("PSO must succeed");
    let (_, f50) =
        discover_pso_algorithm_from_log(&log, "concept:name", 30, 50).expect("PSO must succeed");
    assert!(
        f50 >= f1 - 1e-9,
        "PSO global-best regressed: 1-iter={:.4} 50-iter={:.4}",
        f1,
        f50
    );
    assert!(
        f50 > 0.0,
        "PSO produced fitness=0 on a non-trivial log — destructive pBest pull?",
    );
}

/// Rank-2 domain contract: doubling iterations on a deterministic seed cannot
/// make the global best worse, AND PSO must achieve > 0 fitness on a
/// non-trivial multi-variant log.
#[test]
fn pso_iterations_improve_global_best() {
    let log = build_log(&[
        (10, &["A", "B", "C", "D", "E"]),
        (10, &["A", "B", "D", "C", "E"]),
        (5, &["A", "C", "B", "D", "E"]),
    ]);
    let (_, f_low) =
        discover_pso_algorithm_from_log(&log, "concept:name", 30, 2).expect("PSO must succeed");
    let (_, f_high) =
        discover_pso_algorithm_from_log(&log, "concept:name", 30, 50).expect("PSO must succeed");
    assert!(
        f_high >= f_low - 1e-9,
        "PSO with more iterations regressed: low={:.4} high={:.4}",
        f_low,
        f_high
    );
}