datasynth-eval 3.1.1

Evaluation framework for synthetic financial data quality and coherence
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
//! Quality gate evaluation engine.
//!
//! Evaluates generation results against configurable pass/fail criteria.

use serde::{Deserialize, Serialize};

use crate::ComprehensiveEvaluation;

/// A quality metric that can be checked by a gate.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum QualityMetric {
    /// Benford's Law Mean Absolute Deviation.
    BenfordMad,
    /// Balance sheet coherence rate (0.0–1.0).
    BalanceCoherence,
    /// Document chain integrity rate (0.0–1.0).
    DocumentChainIntegrity,
    /// Correlation preservation score (0.0–1.0).
    CorrelationPreservation,
    /// Temporal consistency score (0.0–1.0).
    TemporalConsistency,
    /// Privacy MIA AUC-ROC score.
    PrivacyMiaAuc,
    /// Data completion rate (0.0–1.0).
    CompletionRate,
    /// Duplicate rate (0.0–1.0).
    DuplicateRate,
    /// Referential integrity rate (0.0–1.0).
    ReferentialIntegrity,
    /// Intercompany match rate (0.0–1.0).
    IcMatchRate,
    /// S2C chain completion rate.
    S2CChainCompletion,
    /// Payroll calculation accuracy.
    PayrollAccuracy,
    /// Manufacturing yield rate.
    ManufacturingYield,
    /// Bank reconciliation balance accuracy.
    BankReconciliationBalance,
    /// Financial reporting tie-back rate.
    FinancialReportingTieBack,
    /// AML detectability coverage.
    AmlDetectability,
    /// Process mining event coverage.
    ProcessMiningCoverage,
    /// Audit evidence coverage.
    AuditEvidenceCoverage,
    /// Anomaly separability (AUC-ROC).
    AnomalySeparability,
    /// Feature quality score.
    FeatureQualityScore,
    /// GNN readiness score.
    GnnReadinessScore,
    /// Domain gap score.
    DomainGapScore,
    /// Custom metric identified by name.
    Custom(String),
}

impl std::fmt::Display for QualityMetric {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::BenfordMad => write!(f, "benford_mad"),
            Self::BalanceCoherence => write!(f, "balance_coherence"),
            Self::DocumentChainIntegrity => write!(f, "document_chain_integrity"),
            Self::CorrelationPreservation => write!(f, "correlation_preservation"),
            Self::TemporalConsistency => write!(f, "temporal_consistency"),
            Self::PrivacyMiaAuc => write!(f, "privacy_mia_auc"),
            Self::CompletionRate => write!(f, "completion_rate"),
            Self::DuplicateRate => write!(f, "duplicate_rate"),
            Self::ReferentialIntegrity => write!(f, "referential_integrity"),
            Self::IcMatchRate => write!(f, "ic_match_rate"),
            Self::S2CChainCompletion => write!(f, "s2c_chain_completion"),
            Self::PayrollAccuracy => write!(f, "payroll_accuracy"),
            Self::ManufacturingYield => write!(f, "manufacturing_yield"),
            Self::BankReconciliationBalance => write!(f, "bank_reconciliation_balance"),
            Self::FinancialReportingTieBack => write!(f, "financial_reporting_tie_back"),
            Self::AmlDetectability => write!(f, "aml_detectability"),
            Self::ProcessMiningCoverage => write!(f, "process_mining_coverage"),
            Self::AuditEvidenceCoverage => write!(f, "audit_evidence_coverage"),
            Self::AnomalySeparability => write!(f, "anomaly_separability"),
            Self::FeatureQualityScore => write!(f, "feature_quality_score"),
            Self::GnnReadinessScore => write!(f, "gnn_readiness_score"),
            Self::DomainGapScore => write!(f, "domain_gap_score"),
            Self::Custom(name) => write!(f, "custom:{name}"),
        }
    }
}

/// Comparison operator for threshold checks.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum Comparison {
    /// Greater than or equal to threshold.
    Gte,
    /// Less than or equal to threshold.
    Lte,
    /// Equal to threshold (with epsilon).
    Eq,
    /// Between two thresholds (inclusive). Uses `threshold` as lower and `upper_threshold` as upper.
    Between,
}

/// Strategy for handling gate failures.
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum FailStrategy {
    /// Stop checking on first failure.
    FailFast,
    /// Check all gates and collect all failures.
    #[default]
    CollectAll,
}

/// A single quality gate with a metric, threshold, and comparison.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualityGate {
    /// Human-readable name for this gate.
    pub name: String,
    /// The metric to check.
    pub metric: QualityMetric,
    /// Threshold value for comparison.
    pub threshold: f64,
    /// Upper threshold for Between comparison.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub upper_threshold: Option<f64>,
    /// How to compare the metric value against the threshold.
    pub comparison: Comparison,
}

impl QualityGate {
    /// Create a new quality gate.
    pub fn new(
        name: impl Into<String>,
        metric: QualityMetric,
        threshold: f64,
        comparison: Comparison,
    ) -> Self {
        Self {
            name: name.into(),
            metric,
            threshold,
            upper_threshold: None,
            comparison,
        }
    }

    /// Create a gate that requires metric >= threshold.
    pub fn gte(name: impl Into<String>, metric: QualityMetric, threshold: f64) -> Self {
        Self::new(name, metric, threshold, Comparison::Gte)
    }

    /// Create a gate that requires metric <= threshold.
    pub fn lte(name: impl Into<String>, metric: QualityMetric, threshold: f64) -> Self {
        Self::new(name, metric, threshold, Comparison::Lte)
    }

    /// Create a gate that requires metric between lower and upper (inclusive).
    pub fn between(name: impl Into<String>, metric: QualityMetric, lower: f64, upper: f64) -> Self {
        Self {
            name: name.into(),
            metric,
            threshold: lower,
            upper_threshold: Some(upper),
            comparison: Comparison::Between,
        }
    }

    /// Check if an actual value passes this gate.
    pub fn check(&self, actual: f64) -> bool {
        match self.comparison {
            Comparison::Gte => actual >= self.threshold,
            Comparison::Lte => actual <= self.threshold,
            Comparison::Eq => (actual - self.threshold).abs() < 1e-9,
            Comparison::Between => {
                let upper = self.upper_threshold.unwrap_or(self.threshold);
                actual >= self.threshold && actual <= upper
            }
        }
    }
}

/// A named collection of quality gates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GateProfile {
    /// Profile name (e.g., "strict", "default", "lenient").
    pub name: String,
    /// List of quality gates in this profile.
    pub gates: Vec<QualityGate>,
    /// Strategy for handling failures.
    #[serde(default)]
    pub fail_strategy: FailStrategy,
}

impl GateProfile {
    /// Create a new gate profile.
    pub fn new(name: impl Into<String>, gates: Vec<QualityGate>) -> Self {
        Self {
            name: name.into(),
            gates,
            fail_strategy: FailStrategy::default(),
        }
    }

    /// Set the fail strategy.
    pub fn with_fail_strategy(mut self, strategy: FailStrategy) -> Self {
        self.fail_strategy = strategy;
        self
    }
}

/// Result of checking a single gate.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GateCheckResult {
    /// Gate name.
    pub gate_name: String,
    /// Metric checked.
    pub metric: QualityMetric,
    /// Whether the gate passed.
    pub passed: bool,
    /// Actual metric value.
    pub actual_value: Option<f64>,
    /// Expected threshold.
    pub threshold: f64,
    /// Comparison used.
    pub comparison: Comparison,
    /// Human-readable message.
    pub message: String,
}

/// Overall result of evaluating all gates in a profile.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GateResult {
    /// Whether all gates passed.
    pub passed: bool,
    /// Profile name used.
    pub profile_name: String,
    /// Individual gate results.
    pub results: Vec<GateCheckResult>,
    /// Summary message.
    pub summary: String,
    /// Number of gates that passed.
    pub gates_passed: usize,
    /// Total number of gates checked.
    pub gates_total: usize,
}

/// Engine that evaluates quality gates against a comprehensive evaluation.
pub struct GateEngine;

impl GateEngine {
    /// Evaluate a comprehensive evaluation against a gate profile.
    pub fn evaluate(evaluation: &ComprehensiveEvaluation, profile: &GateProfile) -> GateResult {
        let mut results = Vec::new();
        let mut all_passed = true;

        for gate in &profile.gates {
            let (actual_value, message) = Self::extract_metric(evaluation, gate);

            let check_result = match actual_value {
                Some(value) => {
                    let passed = gate.check(value);
                    if !passed {
                        all_passed = false;
                    }
                    GateCheckResult {
                        gate_name: gate.name.clone(),
                        metric: gate.metric.clone(),
                        passed,
                        actual_value: Some(value),
                        threshold: gate.threshold,
                        comparison: gate.comparison.clone(),
                        message: if passed {
                            format!(
                                "{}: {:.4} passes {:?} {:.4}",
                                gate.name, value, gate.comparison, gate.threshold
                            )
                        } else {
                            format!(
                                "{}: {:.4} fails {:?} {:.4}",
                                gate.name, value, gate.comparison, gate.threshold
                            )
                        },
                    }
                }
                None => {
                    // Metric not available - treat as not applicable (pass)
                    GateCheckResult {
                        gate_name: gate.name.clone(),
                        metric: gate.metric.clone(),
                        passed: true,
                        actual_value: None,
                        threshold: gate.threshold,
                        comparison: gate.comparison.clone(),
                        message: format!("{}: metric not available ({})", gate.name, message),
                    }
                }
            };

            let failed = !check_result.passed;
            results.push(check_result);

            if failed && profile.fail_strategy == FailStrategy::FailFast {
                break;
            }
        }

        let gates_passed = results.iter().filter(|r| r.passed).count();
        let gates_total = results.len();

        let summary = if all_passed {
            format!(
                "All {}/{} quality gates passed (profile: {})",
                gates_passed, gates_total, profile.name
            )
        } else {
            let failed_names: Vec<_> = results
                .iter()
                .filter(|r| !r.passed)
                .map(|r| r.gate_name.as_str())
                .collect();
            format!(
                "{}/{} quality gates passed, {} failed: {} (profile: {})",
                gates_passed,
                gates_total,
                gates_total - gates_passed,
                failed_names.join(", "),
                profile.name
            )
        };

        GateResult {
            passed: all_passed,
            profile_name: profile.name.clone(),
            results,
            summary,
            gates_passed,
            gates_total,
        }
    }

    /// Extract a metric value from a comprehensive evaluation.
    fn extract_metric(
        evaluation: &ComprehensiveEvaluation,
        gate: &QualityGate,
    ) -> (Option<f64>, String) {
        match &gate.metric {
            QualityMetric::BenfordMad => {
                let mad = evaluation.statistical.benford.as_ref().map(|b| b.mad);
                (mad, "benford analysis not available".to_string())
            }
            QualityMetric::BalanceCoherence => {
                let rate = evaluation.coherence.balance.as_ref().map(|b| {
                    if b.periods_evaluated == 0 {
                        0.0
                    } else {
                        (b.periods_evaluated - b.periods_imbalanced) as f64
                            / b.periods_evaluated as f64
                    }
                });
                (rate, "balance sheet evaluation not available".to_string())
            }
            QualityMetric::DocumentChainIntegrity => {
                let rate = evaluation
                    .coherence
                    .document_chain
                    .as_ref()
                    .map(|d| d.p2p_completion_rate);
                (rate, "document chain evaluation not available".to_string())
            }
            QualityMetric::CorrelationPreservation => {
                let rate = evaluation.statistical.correlation.as_ref().map(|c| {
                    let total = c.checks_passed + c.checks_failed;
                    if total > 0 {
                        c.checks_passed as f64 / total as f64
                    } else {
                        1.0 // No checks = perfect score
                    }
                });
                (rate, "correlation analysis not available".to_string())
            }
            QualityMetric::TemporalConsistency => {
                let rate = evaluation
                    .statistical
                    .temporal
                    .as_ref()
                    .map(|t| t.pattern_correlation);
                (rate, "temporal analysis not available".to_string())
            }
            QualityMetric::PrivacyMiaAuc => {
                let auc = evaluation
                    .privacy
                    .as_ref()
                    .and_then(|p| p.membership_inference.as_ref())
                    .map(|m| m.auc_roc);
                (auc, "privacy MIA evaluation not available".to_string())
            }
            QualityMetric::CompletionRate => {
                let rate = evaluation
                    .quality
                    .completeness
                    .as_ref()
                    .map(|c| c.overall_completeness);
                (rate, "completeness analysis not available".to_string())
            }
            QualityMetric::DuplicateRate => {
                let rate = evaluation
                    .quality
                    .uniqueness
                    .as_ref()
                    .map(|u| u.duplicate_rate);
                (rate, "uniqueness analysis not available".to_string())
            }
            QualityMetric::ReferentialIntegrity => {
                let rate = evaluation
                    .coherence
                    .referential
                    .as_ref()
                    .map(|r| r.overall_integrity_score);
                (
                    rate,
                    "referential integrity evaluation not available".to_string(),
                )
            }
            QualityMetric::IcMatchRate => {
                let rate = evaluation
                    .coherence
                    .intercompany
                    .as_ref()
                    .map(|ic| ic.match_rate);
                (rate, "IC matching evaluation not available".to_string())
            }
            QualityMetric::S2CChainCompletion => {
                let rate = evaluation
                    .coherence
                    .sourcing
                    .as_ref()
                    .map(|s| s.rfx_completion_rate);
                (rate, "sourcing evaluation not available".to_string())
            }
            QualityMetric::PayrollAccuracy => {
                let rate = evaluation
                    .coherence
                    .hr_payroll
                    .as_ref()
                    .map(|h| h.gross_to_net_accuracy);
                (rate, "HR/payroll evaluation not available".to_string())
            }
            QualityMetric::ManufacturingYield => {
                let rate = evaluation
                    .coherence
                    .manufacturing
                    .as_ref()
                    .map(|m| m.yield_rate_consistency);
                (rate, "manufacturing evaluation not available".to_string())
            }
            QualityMetric::BankReconciliationBalance => {
                let rate = evaluation
                    .coherence
                    .bank_reconciliation
                    .as_ref()
                    .map(|b| b.balance_accuracy);
                (
                    rate,
                    "bank reconciliation evaluation not available".to_string(),
                )
            }
            QualityMetric::FinancialReportingTieBack => {
                let rate = evaluation
                    .coherence
                    .financial_reporting
                    .as_ref()
                    .map(|fr| fr.statement_tb_tie_rate);
                (
                    rate,
                    "financial reporting evaluation not available".to_string(),
                )
            }
            QualityMetric::AmlDetectability => {
                let rate = evaluation
                    .banking
                    .as_ref()
                    .and_then(|b| b.aml.as_ref())
                    .map(|a| a.typology_coverage);
                (
                    rate,
                    "AML detectability evaluation not available".to_string(),
                )
            }
            QualityMetric::ProcessMiningCoverage => {
                let rate = evaluation
                    .process_mining
                    .as_ref()
                    .and_then(|pm| pm.event_sequence.as_ref())
                    .map(|es| es.timestamp_monotonicity);
                (rate, "process mining evaluation not available".to_string())
            }
            QualityMetric::AuditEvidenceCoverage => {
                let rate = evaluation
                    .coherence
                    .audit
                    .as_ref()
                    .map(|a| a.evidence_to_finding_rate);
                (rate, "audit evaluation not available".to_string())
            }
            QualityMetric::AnomalySeparability => {
                let score = evaluation
                    .ml_readiness
                    .anomaly_scoring
                    .as_ref()
                    .map(|a| a.anomaly_separability);
                (
                    score,
                    "anomaly scoring evaluation not available".to_string(),
                )
            }
            QualityMetric::FeatureQualityScore => {
                let score = evaluation
                    .ml_readiness
                    .feature_quality
                    .as_ref()
                    .map(|f| f.feature_quality_score);
                (
                    score,
                    "feature quality evaluation not available".to_string(),
                )
            }
            QualityMetric::GnnReadinessScore => {
                let score = evaluation
                    .ml_readiness
                    .gnn_readiness
                    .as_ref()
                    .map(|g| g.gnn_readiness_score);
                (score, "GNN readiness evaluation not available".to_string())
            }
            QualityMetric::DomainGapScore => {
                let score = evaluation
                    .ml_readiness
                    .domain_gap
                    .as_ref()
                    .map(|d| d.domain_gap_score);
                (score, "domain gap evaluation not available".to_string())
            }
            QualityMetric::Custom(name) => {
                // Custom metrics require user-provided evaluation logic and cannot be
                // auto-evaluated by the gate engine. We return `None` to skip this gate
                // (treated as pass) while logging a warning to make the gap visible.
                //
                // Users who need deterministic gate behaviour for a custom metric must
                // implement a custom `GateEvaluator` and register it before calling
                // `evaluate_profile`.
                tracing::warn!(
                    "Custom metric '{}' in gate '{}' has no registered evaluator — \
                     gate skipped; implement a custom GateEvaluator to enable evaluation",
                    name,
                    gate.name
                );
                (
                    None,
                    format!(
                        "custom metric '{name}' not implemented — gate skipped; \
                         register a custom GateEvaluator for real evaluation"
                    ),
                )
            }
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    fn sample_profile() -> GateProfile {
        GateProfile::new(
            "test",
            vec![
                QualityGate::lte("benford_compliance", QualityMetric::BenfordMad, 0.015),
                QualityGate::gte("completeness", QualityMetric::CompletionRate, 0.95),
            ],
        )
    }

    #[test]
    fn test_gate_check_gte() {
        let gate = QualityGate::gte("test", QualityMetric::CompletionRate, 0.95);
        assert!(gate.check(0.96));
        assert!(gate.check(0.95));
        assert!(!gate.check(0.94));
    }

    #[test]
    fn test_gate_check_lte() {
        let gate = QualityGate::lte("test", QualityMetric::BenfordMad, 0.015);
        assert!(gate.check(0.01));
        assert!(gate.check(0.015));
        assert!(!gate.check(0.016));
    }

    #[test]
    fn test_gate_check_between() {
        let gate = QualityGate::between("test", QualityMetric::DuplicateRate, 0.0, 0.05);
        assert!(gate.check(0.0));
        assert!(gate.check(0.03));
        assert!(gate.check(0.05));
        assert!(!gate.check(0.06));
    }

    #[test]
    fn test_gate_check_eq() {
        let gate = QualityGate::new("test", QualityMetric::BalanceCoherence, 1.0, Comparison::Eq);
        assert!(gate.check(1.0));
        assert!(!gate.check(0.99));
    }

    #[test]
    fn test_evaluate_empty_evaluation() {
        let evaluation = ComprehensiveEvaluation::new();
        let profile = sample_profile();
        let result = GateEngine::evaluate(&evaluation, &profile);
        // All metrics unavailable → treated as pass
        assert!(result.passed);
        assert_eq!(result.gates_total, 2);
    }

    #[test]
    fn test_fail_fast_stops_on_first_failure() {
        let evaluation = ComprehensiveEvaluation::new();
        let profile = GateProfile::new(
            "strict",
            vec![
                // This will fail because balance_coherence is not available
                // but N/A is treated as pass. Let's create a custom gate
                // that we know will fail
                QualityGate::gte(
                    "custom_gate",
                    QualityMetric::Custom("nonexistent".to_string()),
                    0.99,
                ),
                QualityGate::gte(
                    "another",
                    QualityMetric::Custom("also_nonexistent".to_string()),
                    0.99,
                ),
            ],
        )
        .with_fail_strategy(FailStrategy::FailFast);

        let result = GateEngine::evaluate(&evaluation, &profile);
        // Custom metrics unavailable are treated as pass, so both pass
        assert!(result.passed);
    }

    #[test]
    fn test_collect_all_reports_all_failures() {
        let evaluation = ComprehensiveEvaluation::new();
        let profile = GateProfile::new(
            "test",
            vec![
                QualityGate::lte("mad", QualityMetric::BenfordMad, 0.015),
                QualityGate::gte("completion", QualityMetric::CompletionRate, 0.95),
            ],
        )
        .with_fail_strategy(FailStrategy::CollectAll);

        let result = GateEngine::evaluate(&evaluation, &profile);
        assert_eq!(result.results.len(), 2);
    }

    #[test]
    fn test_gate_result_summary() {
        let evaluation = ComprehensiveEvaluation::new();
        let profile = sample_profile();
        let result = GateEngine::evaluate(&evaluation, &profile);
        assert!(result.summary.contains("test"));
    }

    #[test]
    fn test_quality_metric_display() {
        assert_eq!(QualityMetric::BenfordMad.to_string(), "benford_mad");
        assert_eq!(
            QualityMetric::BalanceCoherence.to_string(),
            "balance_coherence"
        );
        assert_eq!(
            QualityMetric::Custom("my_metric".to_string()).to_string(),
            "custom:my_metric"
        );
    }

    #[test]
    fn test_gate_profile_serialization() {
        let profile = sample_profile();
        let json = serde_json::to_string(&profile).expect("serialize");
        let deserialized: GateProfile = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(deserialized.name, "test");
        assert_eq!(deserialized.gates.len(), 2);
    }
}