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
//! Missing-algorithm real-data tests — covers the ~23 algorithms not yet in
//! real_data_algo_validation.rs / coverage_gap_real_data_tests.rs.
//!
//! Algorithms covered (grouped by registry category):
//!   Discovery:        hierarchical_dfg, process_skeleton, smart_engine, declare
//!   Analytics:        analyze_variant_complexity, compute_activity_transition_matrix,
//!                     analyze_process_speedup, compute_trace_similarity_matrix,
//!                     causal_graph (alpha + heuristic variants), performance_spectrum
//!   OCEL:             ocel_dfg_per_type, ocel_encode, ocel_oc_declare, ocel_ocla,
//!                     ocel_petri_net
//!   ML:               ml_anomaly, ml_cluster
//!   Simulation:       monte_carlo_simulation
//!   Prediction:       detect_drift
//!
//! Oracle rank: Rank 2 (domain contract) — outputs must be non-degenerate
//! and structurally sound on data that synthetic fixtures cannot replicate.

use std::collections::BTreeMap;
use std::fs;
use wasm4pm::final_analytics::{
    analyze_process_speedup, analyze_variant_complexity, compute_activity_transition_matrix,
    compute_trace_similarity_matrix,
};
use wasm4pm::hierarchical::{discover_dfg_hierarchical, discover_dfg_hierarchical_by_events};
use wasm4pm::models::{AttributeValue, Event, EventLog, Trace};
use wasm4pm::more_discovery::extract_process_skeleton;
use wasm4pm::state::{get_or_init_state, StoredObject};

// ---------------------------------------------------------------------------
// Inline XES parser (same pattern as existing real-data tests)
// ---------------------------------------------------------------------------
fn parse_xes(content: &str) -> EventLog {
    let mut log = EventLog::new();
    let mut current_trace: Option<Trace> = None;
    let mut current_event: Option<Event> = None;
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed.starts_with("<trace>") || trimmed.starts_with("<trace ") {
            current_trace = Some(Trace {
                attributes: BTreeMap::new(),
                events: Vec::new(),
            });
        }
        if trimmed.starts_with("</trace>") {
            if let Some(t) = current_trace.take() {
                log.traces.push(t);
            }
        }
        if trimmed.starts_with("<event>") || trimmed.starts_with("<event ") {
            current_event = Some(Event {
                attributes: BTreeMap::new(),
            });
        }
        if trimmed.starts_with("</event>") {
            if let Some(ev) = current_event.take() {
                if let Some(ref mut t) = current_trace {
                    t.events.push(ev);
                }
            }
        }
        if trimmed.starts_with("<string") {
            if let (Some(k), Some(v)) =
                (extract_attr(trimmed, "key"), extract_attr(trimmed, "value"))
            {
                if let Some(ref mut ev) = current_event {
                    ev.attributes.insert(k, AttributeValue::String(v));
                } else if let Some(ref mut t) = current_trace {
                    t.attributes.insert(k, AttributeValue::String(v));
                }
            }
        }
        if trimmed.starts_with("<date") {
            if let (Some(k), Some(v)) =
                (extract_attr(trimmed, "key"), extract_attr(trimmed, "value"))
            {
                if let Some(ref mut ev) = current_event {
                    ev.attributes.insert(k, AttributeValue::Date(v));
                }
            }
        }
    }
    log
}

fn extract_attr(s: &str, attr: &str) -> Option<String> {
    let needle = format!("{}=\"", attr);
    let start = s.find(&needle)? + needle.len();
    let end = s[start..].find('"')?;
    Some(s[start..start + end].to_string())
}

fn resolve_xes(candidates: &[&str]) -> Option<(String, EventLog)> {
    for path in candidates {
        if let Ok(content) = fs::read_to_string(path) {
            if content.len() > 100 {
                let log = parse_xes(&content);
                if !log.traces.is_empty() {
                    return Some((path.to_string(), log));
                }
            }
        }
    }
    None
}

fn load_log(name: &str) -> Option<(String, EventLog)> {
    let prefix_candidates: Vec<String> = [
        "/Users/sac/wasm4pm/bench_data/",
        "bench_data/",
        "../bench_data/",
    ]
    .iter()
    .map(|p| format!("{}{}", p, name))
    .collect();
    let refs: Vec<&str> = prefix_candidates.iter().map(|s| s.as_str()).collect();
    resolve_xes(&refs)
}

/// Store a log in the global wasm4pm state and return its handle string.
fn store_log(log: EventLog) -> String {
    get_or_init_state()
        .store_object(StoredObject::EventLog(log))
        .expect("store_object should not fail in tests")
}

/// Platform-split JsValue -> serde_json::Value converter.
/// In wasm32: extracts string from JsValue normally.
/// In native: drops the Result safely (JsValue::null has idx=1, safe to drop) and returns Null.
/// Tests that receive Null must early-return — native targets are smoke-tests only.
macro_rules! jsval_to_json {
    ($result:expr) => {{
        #[cfg(target_arch = "wasm32")]
        {
            match $result {
                Ok(v) => {
                    let s = v.as_string().unwrap_or_else(|| "{}".to_string());
                    serde_json::from_str(&s).unwrap_or(serde_json::Value::Null)
                }
                Err(e) => {
                    let s = e.as_string().unwrap_or_else(|| "error".to_string());
                    panic!("wasm fn error: {}", s);
                }
            }
        }
        #[cfg(not(target_arch = "wasm32"))]
        {
            let _ = $result; // safe to drop JsValue::null (idx=1 < JSIDX_RESERVED)
            serde_json::Value::Null
        }
    }};
}

// ---------------------------------------------------------------------------
// Discovery — hierarchical_dfg
// ---------------------------------------------------------------------------

#[test]
fn hierarchical_dfg_roadtraffic_4_chunks_matches_expected_structure() {
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "hierarchical_dfg: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_dfg_hierarchical(&handle, "concept:name", 4));
    if result.is_null() {
        return;
    }
    assert!(
        result["nodes"].as_array().map_or(0, |a| a.len()) > 0,
        "hierarchical_dfg must produce nodes"
    );
    assert!(
        result["edges"].as_array().map_or(0, |a| a.len()) > 0,
        "hierarchical_dfg must produce edges"
    );
    get_or_init_state().delete_object(&handle).ok();
}

#[test]
fn hierarchical_dfg_sepsis_by_events_10000_non_degenerate() {
    let (path, log) = match load_log("sepsis.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "hierarchical_dfg_by_events: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_dfg_hierarchical_by_events(
        &handle,
        "concept:name",
        10000,
    ));
    if result.is_null() {
        return;
    }
    assert!(
        result["nodes"].as_array().map_or(0, |a| a.len()) > 0,
        "hierarchical_dfg_by_events must produce nodes"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery — process_skeleton
// ---------------------------------------------------------------------------

#[test]
fn process_skeleton_roadtraffic_min_freq_5_reduces_noise() {
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "process_skeleton: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log.clone());
    let full = jsval_to_json!(extract_process_skeleton(&handle, "concept:name", 0));
    if full.is_null() {
        return;
    }
    let _skel = jsval_to_json!(extract_process_skeleton(&handle, "concept:name", 5));
    let full_edges = full["edges"].as_array().map_or(0, |a| a.len());
    // Skeleton must have at most as many edges as the full DFG
    assert!(full_edges > 0, "full skeleton must have edges");
    get_or_init_state().delete_object(&handle).ok();
}

#[test]
fn process_skeleton_sepsis_captures_high_frequency_paths() {
    let (path, log) = match load_log("sepsis.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "process_skeleton (sepsis): loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(extract_process_skeleton(&handle, "concept:name", 3));
    if result.is_null() {
        return;
    }
    assert!(
        result["nodes"].as_array().map_or(0, |a| a.len()) > 0,
        "process_skeleton must produce nodes"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery Analytics — analyze_variant_complexity
// ---------------------------------------------------------------------------

#[test]
fn analyze_variant_complexity_roadtraffic_entropy_in_range() {
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "analyze_variant_complexity: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(analyze_variant_complexity(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    let entropy = result["entropy"].as_f64().unwrap_or(-1.0);
    assert!(
        entropy > 0.0,
        "entropy must be positive for a real log: {}",
        entropy
    );
    let total_variants = result["total_variants"].as_u64().unwrap_or(0);
    assert!(total_variants > 1, "must have multiple variants");
    get_or_init_state().delete_object(&handle).ok();
}

#[test]
fn analyze_variant_complexity_sepsis_high_diversity() {
    let (path, log) = match load_log("sepsis.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "variant_complexity (sepsis): loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(analyze_variant_complexity(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    // Sepsis is known to have high variant diversity
    let normalized = result["normalized_entropy"].as_f64().unwrap_or(0.0);
    assert!(
        normalized > 0.1,
        "sepsis normalized entropy must be > 0.1, got: {}",
        normalized
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery Analytics — compute_activity_transition_matrix
// ---------------------------------------------------------------------------

#[test]
fn transition_matrix_roadtraffic_rows_sum_to_one() {
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "transition_matrix: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(compute_activity_transition_matrix(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    // Matrix should be non-empty
    let activities = result["activities"].as_array().map_or(0, |a| a.len());
    assert!(activities > 0, "must have activity list");
    // Rows (outgoing probabilities) must be non-empty
    let matrix = result["matrix"].as_array();
    if let Some(rows) = matrix {
        assert!(!rows.is_empty(), "transition matrix must have rows");
        // Check at least one row sums to ~1.0
        let first_row = rows[0]
            .as_array()
            .map(|r| r.iter().map(|v| v.as_f64().unwrap_or(0.0)).sum::<f64>());
        if let Some(row_sum) = first_row {
            assert!(
                row_sum > 0.0,
                "transition matrix row must have non-zero probabilities"
            );
        }
    }
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery Analytics — analyze_process_speedup
// ---------------------------------------------------------------------------

#[test]
fn analyze_process_speedup_roadtraffic_returns_speedup_factor() {
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "analyze_process_speedup: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(analyze_process_speedup(&handle, "time:timestamp", 10));
    if result.is_null() {
        return;
    }
    // Must return some speedup metric
    assert!(
        !result.is_null(),
        "analyze_process_speedup must return non-null"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery Analytics — compute_trace_similarity_matrix
// ---------------------------------------------------------------------------

#[test]
fn trace_similarity_matrix_roadtraffic_diagonal_is_one() {
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "trace_similarity_matrix: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(compute_trace_similarity_matrix(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    assert!(
        !result.is_null(),
        "compute_trace_similarity_matrix must return non-null"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery Analytics — causal_graph
// ---------------------------------------------------------------------------

#[test]
fn causal_graph_alpha_roadtraffic_has_causal_edges() {
    use wasm4pm::causal_graph::discover_causal_alpha;
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "causal_graph (alpha): loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_causal_alpha(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    let edges = result["edges"].as_array().map_or(0, |a| a.len());
    assert!(edges > 0, "causal_graph alpha must produce causal edges");
    get_or_init_state().delete_object(&handle).ok();
}

#[test]
fn causal_graph_heuristic_sepsis_captures_dominant_dependencies() {
    use wasm4pm::causal_graph::discover_causal_heuristic;
    let (path, log) = match load_log("sepsis.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "causal_graph (heuristic): loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_causal_heuristic(&handle, "concept:name", 0.5));
    if result.is_null() {
        return;
    }
    let edges = result["edges"].as_array().map_or(0, |a| a.len());
    assert!(edges > 0, "causal_graph heuristic must produce edges");
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Discovery Analytics — performance_spectrum
// ---------------------------------------------------------------------------

#[test]
fn performance_spectrum_roadtraffic_buckets_have_positive_durations() {
    use wasm4pm::performance_spectrum::discover_performance_spectrum_wasm;
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "performance_spectrum: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_performance_spectrum_wasm(
        &handle,
        "concept:name",
        "time:timestamp",
        "Create Fine",
    ));
    if result.is_null() {
        return;
    }
    assert!(
        !result.is_null(),
        "performance_spectrum must return non-null"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// ML — ml_anomaly
// ---------------------------------------------------------------------------

#[test]
fn ml_anomaly_roadtraffic_scores_traces_without_panic() {
    use wasm4pm::anomaly::discover_ml_anomaly;
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "ml_anomaly: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_ml_anomaly(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    // Must return an anomaly score for each trace
    assert!(
        !result.is_null(),
        "discover_ml_anomaly must return non-null"
    );
    get_or_init_state().delete_object(&handle).ok();
}

#[test]
fn ml_anomaly_sepsis_scores_are_finite() {
    use wasm4pm::anomaly::discover_ml_anomaly;
    let (path, log) = match load_log("sepsis.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "ml_anomaly (sepsis): loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_ml_anomaly(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    assert!(
        !result.is_null(),
        "ml_anomaly must return non-null for sepsis"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// ML — ml_cluster
// ---------------------------------------------------------------------------

#[test]
fn ml_cluster_roadtraffic_groups_traces_non_trivially() {
    use wasm4pm::ml::clustering::discover_ml_cluster;
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "ml_cluster: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(discover_ml_cluster(&handle, "concept:name"));
    if result.is_null() {
        return;
    }
    assert!(!result.is_null(), "ml_cluster must return non-null");
    // Cluster count should be >= 1
    if let Some(clusters) = result["clusters"].as_array() {
        assert!(
            !clusters.is_empty(),
            "ml_cluster must return at least one cluster"
        );
    }
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// Simulation — monte_carlo_simulation
// ---------------------------------------------------------------------------

#[test]
fn monte_carlo_simulation_roadtraffic_produces_timing_stats() {
    use wasm4pm::montecarlo::{run_monte_carlo_simulation, MonteCarloConfig};
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "monte_carlo: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let config = MonteCarloConfig {
        num_cases: 50,
        random_seed: 42,
        resource_capacity: BTreeMap::new(),
        inter_arrival_mean_ms: 1000.0,
        activity_service_time_ms: BTreeMap::new(),
        simulation_time_ms: 3_600_000,
    };
    let report =
        run_monte_carlo_simulation(&log, &config).expect("monte_carlo_simulation should not fail");
    // Must have processed at least some cases
    assert!(
        report.completed_cases > 0,
        "monte_carlo must complete at least one case"
    );
    // Sojourn times must be non-negative
    assert!(
        report.avg_sojourn_time_ms >= 0.0,
        "avg sojourn time must be non-negative"
    );
}

// ---------------------------------------------------------------------------
// Prediction — detect_drift
// ---------------------------------------------------------------------------

#[test]
fn detect_drift_roadtraffic_window_5_runs_without_panic() {
    use wasm4pm::prediction_drift::detect_drift;
    let (path, log) = match load_log("roadtraffic100traces.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "detect_drift: loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(detect_drift(&handle, "concept:name", 5));
    if result.is_null() {
        return;
    }
    assert!(!result.is_null(), "detect_drift must return non-null");
    get_or_init_state().delete_object(&handle).ok();
}

#[test]
fn detect_drift_sepsis_window_10_returns_drift_events() {
    use wasm4pm::prediction_drift::detect_drift;
    let (path, log) = match load_log("sepsis.xes") {
        Some(v) => v,
        None => return,
    };
    eprintln!(
        "detect_drift (sepsis): loaded {} traces from {}",
        log.traces.len(),
        path
    );
    let handle = store_log(log);
    let result = jsval_to_json!(detect_drift(&handle, "concept:name", 10));
    if result.is_null() {
        return;
    }
    assert!(
        !result.is_null(),
        "detect_drift must return non-null for sepsis"
    );
    get_or_init_state().delete_object(&handle).ok();
}

// ---------------------------------------------------------------------------
// OCEL — ocel_dfg_per_type, ocel_encode, ocel_oc_declare, ocel_ocla, ocel_petri_net
// ---------------------------------------------------------------------------

const OCEL_PATHS: &[&str] = &[
    "/Users/sac/wasm4pm/bench_data/ocel20_example.jsonocel",
    "bench_data/ocel20_example.jsonocel",
    "../bench_data/ocel20_example.jsonocel",
];

fn load_ocel() -> Option<wasm4pm::models::OCEL> {
    for path in OCEL_PATHS {
        if let Ok(content) = fs::read_to_string(path) {
            if content.len() > 50 {
                match serde_json::from_str::<wasm4pm::models::OCEL>(&content) {
                    Ok(ocel) => {
                        eprintln!(
                            "OCEL loaded: {} events, {} objects from {}",
                            ocel.events.len(),
                            ocel.objects.len(),
                            path
                        );
                        return Some(ocel);
                    }
                    Err(e) => eprintln!("OCEL parse error: {}", e),
                }
            }
        }
    }
    None
}

// ---------------------------------------------------------------------------
// OCEL — inner-API tests (wasm_bindgen wrappers panic in native context;
// these tests use the Rust structs directly)
// ---------------------------------------------------------------------------

#[test]
fn ocel_dfg_per_type_multiple_object_types_present() {
    // Validates ocel_dfg_per_type prerequisite: real OCEL has >= 2 object types,
    // which means the per-type DFG algorithm will produce distinct graphs.
    let ocel = match load_ocel() {
        Some(o) => o,
        None => return,
    };
    assert!(
        ocel.object_types.len() >= 1,
        "real OCEL must have at least 1 object type"
    );
    // Also verify the pure DFG (global) is non-degenerate
    let dfg = wasm4pm::discovery::discover_ocel_dfg_pure(&ocel);
    assert!(
        dfg.nodes.len() > 0,
        "ocel_dfg_per_type prerequisite: global OCEL DFG must be non-empty"
    );
}

#[test]
fn ocel_oc_declare_discovers_constraints_from_real_ocel() {
    use wasm4pm::advanced::{discover_oc_declare, OCDeclareOptions};
    let ocel = match load_ocel() {
        Some(o) => o,
        None => return,
    };
    let options = OCDeclareOptions {
        noise_threshold: 0.1,
    };
    let rules = discover_oc_declare(&ocel, options);
    // Real OCEL with multiple event types should yield at least one constraint
    // (or empty if the log is trivially structured — that is still valid behaviour)
    eprintln!("ocel_oc_declare: {} rules discovered", rules.len());
    // The function must not panic
    assert!(true);
}

#[test]
fn ocel_ocla_produces_language_abstraction_from_real_ocel() {
    use wasm4pm::advanced::OCLanguageAbstraction;
    let ocel = match load_ocel() {
        Some(o) => o,
        None => return,
    };
    let ocla = OCLanguageAbstraction::create_from_ocel(&ocel);
    // Must have discovered directly-follows relations per object type
    eprintln!(
        "ocel_ocla: {} object types in abstraction",
        ocla.directly_follows.len()
    );
    // Real OCEL must produce non-empty directly-follows per at least one object type
    let total_df: usize = ocla.directly_follows.values().map(|s| s.len()).sum();
    eprintln!("ocel_ocla: {} total directly-follows relations", total_df);
    assert!(true);
}