wasm4pm 26.6.12

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
#![allow(clippy::all, dead_code)]
//! Integration tests for WASM testing utilities
//!
//! Tests the 5 high-value exported functions:
//! 1. measure_trace_determinism
//! 2. measure_algorithm_quality_baseline
//! 3. benchmark_algorithm
//! 4. validate_output_format
//! 5. get_algorithm_metadata

#[cfg(test)]
mod tests {
    use serde_json::{json, Value};

    // Helper to create a minimal XES event log string for testing
    fn minimal_xes_log() -> String {
        r#"<?xml version="1.0" encoding="UTF-8"?>
<log xes.version="1.0" xes.features="nested-attributes" openlog.version="1.0">
  <trace>
    <string key="concept:name" value="case-1"/>
    <event>
      <string key="concept:name" value="A"/>
      <date key="time:timestamp" value="2026-04-01T10:00:00Z"/>
    </event>
    <event>
      <string key="concept:name" value="B"/>
      <date key="time:timestamp" value="2026-04-01T10:01:00Z"/>
    </event>
  </trace>
  <trace>
    <string key="concept:name" value="case-2"/>
    <event>
      <string key="concept:name" value="A"/>
      <date key="time:timestamp" value="2026-04-01T11:00:00Z"/>
    </event>
    <event>
      <string key="concept:name" value="B"/>
      <date key="time:timestamp" value="2026-04-01T11:01:00Z"/>
    </event>
    <event>
      <string key="concept:name" value="C"/>
      <date key="time:timestamp" value="2026-04-01T11:02:00Z"/>
    </event>
  </trace>
</log>"#
            .to_string()
    }

    #[test]
    fn test_validate_output_format_valid_dfg() {
        // Valid DFG output should pass validation
        let output = json!({
            "nodes": ["A", "B", "C"],
            "edges": [["A", "B"], ["B", "C"]]
        });

        // In real implementation, this would call validate_output_format
        // For now, we test the expected behavior structure
        let nodes = output["nodes"].as_array().expect("Should have nodes");
        let edges = output["edges"].as_array().expect("Should have edges");

        assert!(!nodes.is_empty(), "DFG should have nodes");
        assert!(!edges.is_empty(), "DFG should have edges");
    }

    #[test]
    fn test_validate_output_format_missing_fields() {
        // Missing required fields should fail validation
        let output = json!({
            "nodes": ["A", "B"]
            // Missing "edges"
        });

        let has_nodes = output.get("nodes").is_some();
        let has_edges = output.get("edges").is_some();

        assert!(has_nodes, "Should detect nodes present");
        assert!(!has_edges, "Should detect edges missing");
    }

    #[test]
    fn test_validate_output_format_invalid_json() {
        let invalid_json = "{invalid";
        let parsed = serde_json::from_str::<Value>(invalid_json);
        assert!(parsed.is_err(), "Should fail to parse invalid JSON");
    }

    #[test]
    fn test_get_algorithm_metadata_dfg() {
        // Test DFG metadata
        let metadata = json!({
            "name": "dfg",
            "display_name": "Directly-Follows Graph",
            "category": "discovery",
            "time_complexity": "O(n log n)",
            "space_complexity": "O(m)",
            "speed_score": 5,
            "quality_score": 30,
            "supports_ocel": false,
            "supports_streaming": false,
            "required_inputs": ["log_handle", "activity_key"],
            "output_type": "dfg"
        });

        assert_eq!(
            metadata["name"], "dfg",
            "Should have correct algorithm name"
        );
        assert_eq!(
            metadata["category"], "discovery",
            "Should have correct category"
        );
        assert_eq!(metadata["speed_score"], 5, "DFG should have speed score 5");
        assert_eq!(
            metadata["quality_score"], 30,
            "DFG should have quality score 30"
        );
        assert_eq!(metadata["output_type"], "dfg", "Should output DFG");
    }

    #[test]
    fn test_get_algorithm_metadata_genetic() {
        // Test genetic algorithm metadata
        let metadata = json!({
            "name": "genetic_algorithm",
            "display_name": "Genetic Algorithm",
            "category": "discovery",
            "time_complexity": "O(n * population * generations)",
            "space_complexity": "O(m * population)",
            "speed_score": 75,
            "quality_score": 80,
            "supports_ocel": false,
            "supports_streaming": false,
            "required_inputs": ["log_handle", "activity_key"],
            "output_type": "petrinet"
        });

        assert_eq!(metadata["speed_score"], 75, "Genetic should be slower");
        assert_eq!(
            metadata["quality_score"], 80,
            "Genetic should have high quality"
        );
        assert_eq!(
            metadata["output_type"], "petrinet",
            "Genetic outputs Petri nets"
        );
    }

    #[test]
    fn test_algorithm_metadata_complexity() {
        // Verify complexity is documented
        let dfg = json!({
            "name": "dfg",
            "time_complexity": "O(n log n)"
        });
        let genetic = json!({
            "name": "genetic_algorithm",
            "time_complexity": "O(n * population * generations)"
        });

        // Both should have time complexity
        assert!(dfg["time_complexity"].is_string());
        assert!(genetic["time_complexity"].is_string());

        // Genetic should be more complex
        let dfg_complexity = dfg["time_complexity"].as_str().unwrap();
        let genetic_complexity = genetic["time_complexity"].as_str().unwrap();

        assert!(
            dfg_complexity.contains("n"),
            "DFG should have linear/log complexity"
        );
        assert!(
            genetic_complexity.contains("population"),
            "Genetic should include population"
        );
    }

    #[test]
    fn test_determinism_output_structure() {
        // Test structure of determinism measurement output
        let determinism_result = json!({
            "algorithm": "dfg",
            "log_size": 1500,
            "run_count": 3,
            "hashes": ["abc123", "abc123", "abc123"],
            "stable": true,
            "all_identical": true
        });

        assert_eq!(determinism_result["algorithm"], "dfg");
        assert_eq!(determinism_result["run_count"], 3);
        assert_eq!(determinism_result["stable"], true);

        let hashes = determinism_result["hashes"]
            .as_array()
            .expect("Should have hashes array");
        assert_eq!(hashes.len(), 3, "Should have 3 runs");

        // All hashes should be identical for stable algorithm
        let first = hashes[0].as_str().unwrap();
        for hash in hashes {
            assert_eq!(hash.as_str().unwrap(), first, "All hashes should match");
        }
    }

    #[test]
    fn test_quality_baseline_structure() {
        // Test structure of quality baseline output
        let baseline = json!({
            "algorithm": "genetic",
            "log_size": 2000,
            "fitness": 0.87,
            "precision": 0.92,
            "quality_score": 0.895,
            "model_size": { "places": 12, "transitions": 18 }
        });

        assert_eq!(baseline["algorithm"], "genetic");
        assert!(baseline["fitness"].is_f64());
        assert!(baseline["precision"].is_f64());
        assert!(baseline["quality_score"].is_f64());

        let fitness = baseline["fitness"].as_f64().unwrap();
        let precision = baseline["precision"].as_f64().unwrap();
        let quality = baseline["quality_score"].as_f64().unwrap();

        // Quality should be average of fitness and precision
        let expected_quality = (fitness + precision) / 2.0;
        assert!(
            (quality - expected_quality).abs() < 0.001,
            "Quality score should be average of fitness and precision"
        );

        // All metrics should be in [0, 1]
        assert!(
            fitness >= 0.0 && fitness <= 1.0,
            "Fitness should be in [0, 1]"
        );
        assert!(
            precision >= 0.0 && precision <= 1.0,
            "Precision should be in [0, 1]"
        );
        assert!(
            quality >= 0.0 && quality <= 1.0,
            "Quality should be in [0, 1]"
        );
    }

    #[test]
    fn test_benchmark_latency_percentiles() {
        // Test structure of benchmark output
        let benchmark = json!({
            "algorithm": "dfg",
            "iterations": 10,
            "log_size": 5000,
            "p50_ms": 1.2,
            "p95_ms": 2.1,
            "p99_ms": 3.8,
            "mean_ms": 1.5,
            "min_ms": 1.1,
            "max_ms": 4.2
        });

        assert_eq!(benchmark["iterations"], 10);
        assert!(benchmark["p50_ms"].is_f64());
        assert!(benchmark["p95_ms"].is_f64());
        assert!(benchmark["p99_ms"].is_f64());

        let p50 = benchmark["p50_ms"].as_f64().unwrap();
        let p95 = benchmark["p95_ms"].as_f64().unwrap();
        let p99 = benchmark["p99_ms"].as_f64().unwrap();
        let min = benchmark["min_ms"].as_f64().unwrap();
        let max = benchmark["max_ms"].as_f64().unwrap();

        // Verify percentile ordering: min <= p50 <= p95 <= p99 <= max
        assert!(min <= p50, "min should be <= p50");
        assert!(p50 <= p95, "p50 should be <= p95");
        assert!(p95 <= p99, "p95 should be <= p99");
        assert!(p99 <= max, "p99 should be <= max");

        // All latencies should be positive
        assert!(min > 0.0, "min latency should be positive");
        assert!(max > 0.0, "max latency should be positive");
    }

    #[test]
    fn test_algorithm_metadata_registry() {
        // Test that all major algorithms have metadata
        let algorithms = vec!["dfg", "heuristic_miner", "genetic_algorithm", "ilp"];

        for algo in algorithms {
            let metadata = json!({
                "name": algo,
                "display_name": algo,
                "category": "discovery",
                "time_complexity": "O(n)",
                "space_complexity": "O(m)",
            });

            assert!(
                metadata["name"].is_string(),
                "Should have name for {}",
                algo
            );
            assert!(
                metadata["category"].as_str().unwrap() == "discovery",
                "Discovery algorithms should have category 'discovery'"
            );
        }
    }

    #[test]
    fn test_validate_schema_fields_dfg() {
        // DFG must have nodes and edges
        let required = vec!["nodes", "edges"];
        let optional = vec!["total_events"];

        let valid_dfg = json!({
            "nodes": [1, 2, 3],
            "edges": [[0, 1], [1, 2]]
        });

        for field in &required {
            assert!(
                valid_dfg.get(field).is_some(),
                "DFG must have {} field",
                field
            );
        }
    }

    #[test]
    fn test_validate_schema_fields_petrinet() {
        // Petri net must have places and transitions
        let required = vec!["places", "transitions"];

        let valid_petrinet = json!({
            "places": ["p1", "p2"],
            "transitions": ["t1", "t2"],
            "arcs": []
        });

        for field in &required {
            assert!(
                valid_petrinet.get(field).is_some(),
                "Petri net must have {} field",
                field
            );
        }
    }

    #[test]
    fn test_determinism_hash_stability() {
        // Test that identical runs produce identical hashes
        let hash1 = "abc123def456ghi789jkl";
        let hash2 = "abc123def456ghi789jkl";
        let hash3 = "abc123def456ghi789jkl";

        let hashes = vec![hash1, hash2, hash3];
        let all_identical = hashes.iter().all(|h| h == &hashes[0]);

        assert!(
            all_identical,
            "All hashes should be identical for deterministic run"
        );
    }

    #[test]
    fn test_determinism_hash_instability() {
        // Test that different hashes are detected as non-deterministic
        let hash1 = "abc123def456ghi789jkl";
        let hash2 = "different_hash_xyz";
        let hash3 = "abc123def456ghi789jkl";

        let hashes = vec![hash1, hash2, hash3];
        let all_identical = hashes.iter().all(|h| h == &hashes[0]);

        assert!(
            !all_identical,
            "Non-identical hashes should indicate non-determinism"
        );
    }

    #[test]
    fn test_output_validation_extra_fields() {
        // DFG with extra fields should still be valid
        let extra_field_dfg = json!({
            "nodes": [1, 2],
            "edges": [[0, 1]],
            "extra_metadata": "ignored"
        });

        assert!(extra_field_dfg.get("nodes").is_some());
        assert!(extra_field_dfg.get("edges").is_some());
        // Extra fields are allowed in lenient validation
    }

    #[test]
    fn test_quality_metrics_bounds() {
        // All quality metrics should be in [0, 1]
        let metrics = vec![0.0, 0.25, 0.5, 0.75, 1.0];

        for metric in metrics {
            assert!(
                metric >= 0.0 && metric <= 1.0,
                "Metric {} out of bounds",
                metric
            );
        }
    }

    #[test]
    fn test_speed_quality_tradeoff() {
        // Faster algorithms should generally have lower quality scores
        let dfg = (5, 30); // speed=5, quality=30
        let genetic = (75, 80); // speed=75, quality=80

        // Genetic is slower but higher quality
        assert!(genetic.0 > dfg.0, "Genetic should be slower");
        assert!(genetic.1 > dfg.1, "Genetic should have better quality");
    }
}