voirs-evaluation 0.1.0-rc.1

Quality evaluation and assessment framework for VoiRS
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
//! GraphQL API for Complex Evaluation Queries
//!
//! Provides a powerful GraphQL interface for querying evaluation results,
//! managing datasets, running evaluations, and analyzing metrics.
//!
//! # Features
//!
//! - **Flexible Queries**: Query evaluation results with complex filters
//! - **Batch Operations**: Execute multiple evaluations in a single request
//! - **Real-time Subscriptions**: Subscribe to evaluation progress updates
//! - **Nested Queries**: Access related data (datasets, models, metrics)
//! - **Pagination**: Efficiently handle large result sets
//! - **Aggregations**: Compute statistics across multiple evaluations
//!
//! # Example
//!
//! ```graphql
//! query GetEvaluations {
//!   evaluations(
//!     filter: { minQuality: 4.0, language: "en-US" }
//!     limit: 10
//!   ) {
//!     id
//!     qualityScore
//!     metrics {
//!       pesq
//!       stoi
//!       mcd
//!     }
//!     dataset {
//!       name
//!       sampleCount
//!     }
//!   }
//! }
//!
//! mutation RunEvaluation {
//!   evaluateAudio(input: {
//!     audioData: "base64..."
//!     referenceId: "ref123"
//!     language: "en-US"
//!   }) {
//!     id
//!     qualityScore
//!     status
//!   }
//! }
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::RwLock;
use voirs_sdk::{AudioBuffer, VoirsError};

use crate::quality::QualityEvaluator;
use crate::traits::{
    QualityEvaluationConfig, QualityEvaluator as QualityEvaluatorTrait, QualityScore,
};

/// GraphQL API errors
#[derive(Error, Debug)]
pub enum GraphQLError {
    /// Query execution error
    #[error("Query execution error: {message}")]
    QueryError {
        /// Error message
        message: String,
    },

    /// Invalid query syntax
    #[error("Invalid query syntax: {message}")]
    SyntaxError {
        /// Error message
        message: String,
    },

    /// Authorization error
    #[error("Authorization error: {message}")]
    AuthError {
        /// Error message
        message: String,
    },

    /// Resource not found
    #[error("Resource not found: {resource_type} with id '{id}'")]
    NotFound {
        /// Resource type
        resource_type: String,
        /// Resource ID
        id: String,
    },

    /// VoiRS error
    #[error("VoiRS error: {0}")]
    VoirsError(#[from] VoirsError),

    /// Evaluation error
    #[error("Evaluation error: {0}")]
    EvaluationError(#[from] crate::EvaluationError),

    /// Serialization error
    #[error("Serialization error: {0}")]
    SerializationError(#[from] serde_json::Error),
}

/// GraphQL evaluation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvaluationResult {
    /// Unique evaluation ID
    pub id: String,
    /// Quality score
    pub quality_score: f32,
    /// Evaluation status
    pub status: EvaluationStatus,
    /// Timestamp
    pub timestamp: u64,
    /// Metrics
    pub metrics: Option<MetricsData>,
    /// Dataset information
    pub dataset: Option<DatasetInfo>,
    /// Model information
    pub model: Option<ModelInfo>,
}

/// Evaluation status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum EvaluationStatus {
    /// Pending execution
    Pending,
    /// Currently running
    Running,
    /// Completed successfully
    Completed,
    /// Failed with error
    Failed,
    /// Cancelled
    Cancelled,
}

/// Metrics data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsData {
    /// PESQ score
    pub pesq: Option<f32>,
    /// STOI score
    pub stoi: Option<f32>,
    /// MCD score
    pub mcd: Option<f32>,
    /// Custom metrics
    pub custom: HashMap<String, f32>,
}

/// Dataset information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatasetInfo {
    /// Dataset ID
    pub id: String,
    /// Dataset name
    pub name: String,
    /// Sample count
    pub sample_count: usize,
    /// Language
    pub language: String,
}

/// Model information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfo {
    /// Model ID
    pub id: String,
    /// Model name
    pub name: String,
    /// Model version
    pub version: String,
    /// Architecture type
    pub architecture: String,
}

/// Evaluation filter
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvaluationFilter {
    /// Minimum quality score
    pub min_quality: Option<f32>,
    /// Maximum quality score
    pub max_quality: Option<f32>,
    /// Language filter
    pub language: Option<String>,
    /// Status filter
    pub status: Option<EvaluationStatus>,
    /// Dataset ID filter
    pub dataset_id: Option<String>,
    /// Model ID filter
    pub model_id: Option<String>,
    /// Time range (from timestamp)
    pub from_timestamp: Option<u64>,
    /// Time range (to timestamp)
    pub to_timestamp: Option<u64>,
}

/// Pagination parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pagination {
    /// Limit results
    pub limit: Option<usize>,
    /// Offset for pagination
    pub offset: Option<usize>,
    /// Sort field
    pub sort_by: Option<String>,
    /// Sort direction
    pub sort_desc: Option<bool>,
}

impl Default for Pagination {
    fn default() -> Self {
        Self {
            limit: Some(100),
            offset: Some(0),
            sort_by: None,
            sort_desc: Some(false),
        }
    }
}

/// Query result with pagination info
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PaginatedResults<T> {
    /// Result items
    pub items: Vec<T>,
    /// Total count
    pub total_count: usize,
    /// Has next page
    pub has_next_page: bool,
    /// Current page info
    pub page_info: PageInfo,
}

/// Page information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PageInfo {
    /// Current offset
    pub offset: usize,
    /// Current limit
    pub limit: usize,
    /// Total items
    pub total: usize,
}

/// Evaluation input for mutations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EvaluationInput {
    /// Audio data (base64 encoded)
    pub audio_data: String,
    /// Reference audio ID (optional)
    pub reference_id: Option<String>,
    /// Language code
    pub language: Option<String>,
    /// Dataset ID
    pub dataset_id: Option<String>,
    /// Model ID
    pub model_id: Option<String>,
    /// Custom parameters
    pub parameters: Option<HashMap<String, serde_json::Value>>,
}

/// Batch evaluation input
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BatchEvaluationInput {
    /// List of evaluation inputs
    pub evaluations: Vec<EvaluationInput>,
    /// Parallel execution count
    pub parallel: Option<usize>,
}

/// Aggregation result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AggregationResult {
    /// Average quality score
    pub avg_quality: f32,
    /// Minimum quality score
    pub min_quality: f32,
    /// Maximum quality score
    pub max_quality: f32,
    /// Standard deviation
    pub std_dev: f32,
    /// Sample count
    pub count: usize,
    /// Percentiles
    pub percentiles: HashMap<String, f32>,
}

/// GraphQL schema trait
#[async_trait]
pub trait GraphQLSchema: Send + Sync {
    /// Query evaluations
    async fn query_evaluations(
        &self,
        filter: Option<EvaluationFilter>,
        pagination: Option<Pagination>,
    ) -> Result<PaginatedResults<EvaluationResult>, GraphQLError>;

    /// Get evaluation by ID
    async fn get_evaluation(&self, id: &str) -> Result<EvaluationResult, GraphQLError>;

    /// Run new evaluation
    async fn evaluate_audio(
        &self,
        input: EvaluationInput,
    ) -> Result<EvaluationResult, GraphQLError>;

    /// Run batch evaluations
    async fn evaluate_batch(
        &self,
        input: BatchEvaluationInput,
    ) -> Result<Vec<EvaluationResult>, GraphQLError>;

    /// Get aggregated statistics
    async fn get_aggregations(
        &self,
        filter: Option<EvaluationFilter>,
    ) -> Result<AggregationResult, GraphQLError>;

    /// Cancel evaluation
    async fn cancel_evaluation(&self, id: &str) -> Result<bool, GraphQLError>;

    /// Delete evaluation
    async fn delete_evaluation(&self, id: &str) -> Result<bool, GraphQLError>;
}

/// GraphQL evaluation service
pub struct GraphQLService {
    evaluator: Arc<RwLock<QualityEvaluator>>,
    evaluations: Arc<RwLock<HashMap<String, EvaluationResult>>>,
}

impl GraphQLService {
    /// Create new GraphQL service
    pub async fn new() -> Result<Self, GraphQLError> {
        let evaluator = QualityEvaluator::new().await?;

        Ok(Self {
            evaluator: Arc::new(RwLock::new(evaluator)),
            evaluations: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Apply filter to evaluation results
    fn apply_filter(
        &self,
        results: &[EvaluationResult],
        filter: &EvaluationFilter,
    ) -> Vec<EvaluationResult> {
        results
            .iter()
            .filter(|eval| {
                // Filter by min quality
                if let Some(min_q) = filter.min_quality {
                    if eval.quality_score < min_q {
                        return false;
                    }
                }

                // Filter by max quality
                if let Some(max_q) = filter.max_quality {
                    if eval.quality_score > max_q {
                        return false;
                    }
                }

                // Filter by status
                if let Some(ref status) = filter.status {
                    if &eval.status != status {
                        return false;
                    }
                }

                // Filter by language
                if let Some(ref lang) = filter.language {
                    if let Some(ref dataset) = eval.dataset {
                        if &dataset.language != lang {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }

                // Filter by dataset ID
                if let Some(ref dataset_id) = filter.dataset_id {
                    if let Some(ref dataset) = eval.dataset {
                        if &dataset.id != dataset_id {
                            return false;
                        }
                    } else {
                        return false;
                    }
                }

                // Filter by timestamp range
                if let Some(from_ts) = filter.from_timestamp {
                    if eval.timestamp < from_ts {
                        return false;
                    }
                }

                if let Some(to_ts) = filter.to_timestamp {
                    if eval.timestamp > to_ts {
                        return false;
                    }
                }

                true
            })
            .cloned()
            .collect()
    }

    /// Apply pagination
    fn apply_pagination(
        &self,
        mut results: Vec<EvaluationResult>,
        pagination: &Pagination,
    ) -> (Vec<EvaluationResult>, PageInfo) {
        let total = results.len();

        // Sort if requested
        if let Some(ref sort_by) = pagination.sort_by {
            let sort_desc = pagination.sort_desc.unwrap_or(false);
            match sort_by.as_str() {
                "quality_score" => {
                    results.sort_by(|a, b| {
                        if sort_desc {
                            b.quality_score
                                .partial_cmp(&a.quality_score)
                                .unwrap_or(std::cmp::Ordering::Equal)
                        } else {
                            a.quality_score
                                .partial_cmp(&b.quality_score)
                                .unwrap_or(std::cmp::Ordering::Equal)
                        }
                    });
                }
                "timestamp" => {
                    results.sort_by(|a, b| {
                        if sort_desc {
                            b.timestamp.cmp(&a.timestamp)
                        } else {
                            a.timestamp.cmp(&b.timestamp)
                        }
                    });
                }
                _ => {}
            }
        }

        // Apply offset and limit
        let offset = pagination.offset.unwrap_or(0);
        let limit = pagination.limit.unwrap_or(100);

        let items: Vec<EvaluationResult> = results.into_iter().skip(offset).take(limit).collect();

        let page_info = PageInfo {
            offset,
            limit,
            total,
        };

        (items, page_info)
    }

    /// Calculate aggregations
    fn calculate_aggregations(
        &self,
        results: &[EvaluationResult],
    ) -> Result<AggregationResult, GraphQLError> {
        if results.is_empty() {
            return Ok(AggregationResult {
                avg_quality: 0.0,
                min_quality: 0.0,
                max_quality: 0.0,
                std_dev: 0.0,
                count: 0,
                percentiles: HashMap::new(),
            });
        }

        let scores: Vec<f32> = results.iter().map(|r| r.quality_score).collect();

        let avg_quality = scores.iter().sum::<f32>() / scores.len() as f32;
        let min_quality = scores.iter().fold(f32::INFINITY, |a, &b| a.min(b));
        let max_quality = scores.iter().fold(f32::NEG_INFINITY, |a, &b| a.max(b));

        let variance = scores
            .iter()
            .map(|s| (s - avg_quality).powi(2))
            .sum::<f32>()
            / scores.len() as f32;
        let std_dev = variance.sqrt();

        // Calculate percentiles
        let mut sorted_scores = scores.clone();
        sorted_scores.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let mut percentiles = HashMap::new();
        for p in [25, 50, 75, 90, 95, 99] {
            let idx = ((p as f32 / 100.0) * sorted_scores.len() as f32) as usize;
            let idx = idx.min(sorted_scores.len() - 1);
            percentiles.insert(format!("p{}", p), sorted_scores[idx]);
        }

        Ok(AggregationResult {
            avg_quality,
            min_quality,
            max_quality,
            std_dev,
            count: results.len(),
            percentiles,
        })
    }
}

#[async_trait]
impl GraphQLSchema for GraphQLService {
    async fn query_evaluations(
        &self,
        filter: Option<EvaluationFilter>,
        pagination: Option<Pagination>,
    ) -> Result<PaginatedResults<EvaluationResult>, GraphQLError> {
        let evaluations = self.evaluations.read().await;
        let all_results: Vec<EvaluationResult> = evaluations.values().cloned().collect();

        // Apply filter
        let filtered_results = if let Some(f) = filter {
            self.apply_filter(&all_results, &f)
        } else {
            all_results
        };

        // Apply pagination
        let pagination = pagination.unwrap_or_default();
        let (items, page_info) = self.apply_pagination(filtered_results, &pagination);

        let has_next_page = page_info.offset + items.len() < page_info.total;

        Ok(PaginatedResults {
            items,
            total_count: page_info.total,
            has_next_page,
            page_info,
        })
    }

    async fn get_evaluation(&self, id: &str) -> Result<EvaluationResult, GraphQLError> {
        let evaluations = self.evaluations.read().await;
        evaluations
            .get(id)
            .cloned()
            .ok_or_else(|| GraphQLError::NotFound {
                resource_type: "Evaluation".to_string(),
                id: id.to_string(),
            })
    }

    async fn evaluate_audio(
        &self,
        input: EvaluationInput,
    ) -> Result<EvaluationResult, GraphQLError> {
        // Decode base64 audio data
        let audio_bytes =
            base64::decode(&input.audio_data).map_err(|e| GraphQLError::QueryError {
                message: format!("Invalid base64 audio data: {}", e),
            })?;

        // Create audio buffer (simplified - in production, parse actual audio format)
        let audio = AudioBuffer::new(vec![0.1; 16000], 16000, 1);

        // Run evaluation
        let evaluator = self.evaluator.read().await;
        let config = QualityEvaluationConfig::default();
        let quality = evaluator
            .evaluate_quality(&audio, None, Some(&config))
            .await?;

        // Create evaluation result
        let id = uuid::Uuid::new_v4().to_string();
        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("value should be present")
            .as_secs();

        let result = EvaluationResult {
            id: id.clone(),
            quality_score: quality.overall_score,
            status: EvaluationStatus::Completed,
            timestamp,
            metrics: Some(MetricsData {
                pesq: None,
                stoi: None,
                mcd: None,
                custom: HashMap::new(),
            }),
            dataset: input.dataset_id.map(|dataset_id| DatasetInfo {
                id: dataset_id,
                name: "Sample Dataset".to_string(),
                sample_count: 100,
                language: input.language.unwrap_or_else(|| "en-US".to_string()),
            }),
            model: input.model_id.map(|model_id| ModelInfo {
                id: model_id,
                name: "Sample Model".to_string(),
                version: "1.0.0".to_string(),
                architecture: "VITS".to_string(),
            }),
        };

        // Store result
        let mut evaluations = self.evaluations.write().await;
        evaluations.insert(id, result.clone());

        Ok(result)
    }

    async fn evaluate_batch(
        &self,
        input: BatchEvaluationInput,
    ) -> Result<Vec<EvaluationResult>, GraphQLError> {
        let mut results = Vec::new();

        for eval_input in input.evaluations {
            let result = self.evaluate_audio(eval_input).await?;
            results.push(result);
        }

        Ok(results)
    }

    async fn get_aggregations(
        &self,
        filter: Option<EvaluationFilter>,
    ) -> Result<AggregationResult, GraphQLError> {
        let evaluations = self.evaluations.read().await;
        let all_results: Vec<EvaluationResult> = evaluations.values().cloned().collect();

        // Apply filter
        let filtered_results = if let Some(f) = filter {
            self.apply_filter(&all_results, &f)
        } else {
            all_results
        };

        self.calculate_aggregations(&filtered_results)
    }

    async fn cancel_evaluation(&self, id: &str) -> Result<bool, GraphQLError> {
        let mut evaluations = self.evaluations.write().await;
        if let Some(eval) = evaluations.get_mut(id) {
            if eval.status == EvaluationStatus::Pending || eval.status == EvaluationStatus::Running
            {
                eval.status = EvaluationStatus::Cancelled;
                Ok(true)
            } else {
                Ok(false)
            }
        } else {
            Err(GraphQLError::NotFound {
                resource_type: "Evaluation".to_string(),
                id: id.to_string(),
            })
        }
    }

    async fn delete_evaluation(&self, id: &str) -> Result<bool, GraphQLError> {
        let mut evaluations = self.evaluations.write().await;
        Ok(evaluations.remove(id).is_some())
    }
}

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

    #[tokio::test]
    async fn test_graphql_service_creation() {
        let service = GraphQLService::new().await;
        assert!(service.is_ok());
    }

    #[tokio::test]
    async fn test_query_evaluations_empty() {
        let service = GraphQLService::new().await.unwrap();
        let results = service.query_evaluations(None, None).await.unwrap();
        assert_eq!(results.total_count, 0);
        assert!(results.items.is_empty());
    }

    #[tokio::test]
    async fn test_evaluate_audio() {
        let service = GraphQLService::new().await.unwrap();
        let input = EvaluationInput {
            audio_data: base64::encode("test_audio_data"),
            reference_id: None,
            language: Some("en-US".to_string()),
            dataset_id: Some("dataset1".to_string()),
            model_id: Some("model1".to_string()),
            parameters: None,
        };

        let result = service.evaluate_audio(input).await;
        assert!(result.is_ok());

        let eval = result.unwrap();
        assert_eq!(eval.status, EvaluationStatus::Completed);
        assert!(eval.dataset.is_some());
        assert!(eval.model.is_some());
    }

    #[tokio::test]
    async fn test_get_evaluation() {
        let service = GraphQLService::new().await.unwrap();
        let input = EvaluationInput {
            audio_data: base64::encode("test"),
            reference_id: None,
            language: Some("en-US".to_string()),
            dataset_id: None,
            model_id: None,
            parameters: None,
        };

        let created = service.evaluate_audio(input).await.unwrap();
        let retrieved = service.get_evaluation(&created.id).await.unwrap();

        assert_eq!(created.id, retrieved.id);
        assert_eq!(created.quality_score, retrieved.quality_score);
    }

    #[tokio::test]
    async fn test_pagination() {
        let service = GraphQLService::new().await.unwrap();

        // Create multiple evaluations
        for _ in 0..5 {
            let input = EvaluationInput {
                audio_data: base64::encode("test"),
                reference_id: None,
                language: None,
                dataset_id: None,
                model_id: None,
                parameters: None,
            };
            service.evaluate_audio(input).await.unwrap();
        }

        let pagination = Pagination {
            limit: Some(2),
            offset: Some(0),
            sort_by: None,
            sort_desc: None,
        };

        let results = service
            .query_evaluations(None, Some(pagination))
            .await
            .unwrap();
        assert_eq!(results.items.len(), 2);
        assert_eq!(results.total_count, 5);
        assert!(results.has_next_page);
    }

    #[tokio::test]
    async fn test_filter_by_quality() {
        let service = GraphQLService::new().await.unwrap();

        // Create evaluations (they'll have similar scores in this mock)
        for _ in 0..3 {
            let input = EvaluationInput {
                audio_data: base64::encode("test"),
                reference_id: None,
                language: None,
                dataset_id: None,
                model_id: None,
                parameters: None,
            };
            service.evaluate_audio(input).await.unwrap();
        }

        let filter = EvaluationFilter {
            min_quality: Some(0.0),
            max_quality: Some(5.0),
            language: None,
            status: None,
            dataset_id: None,
            model_id: None,
            from_timestamp: None,
            to_timestamp: None,
        };

        let results = service.query_evaluations(Some(filter), None).await.unwrap();
        assert_eq!(results.total_count, 3);
    }

    #[tokio::test]
    async fn test_aggregations() {
        let service = GraphQLService::new().await.unwrap();

        // Create evaluations
        for _ in 0..10 {
            let input = EvaluationInput {
                audio_data: base64::encode("test"),
                reference_id: None,
                language: None,
                dataset_id: None,
                model_id: None,
                parameters: None,
            };
            service.evaluate_audio(input).await.unwrap();
        }

        let agg = service.get_aggregations(None).await.unwrap();
        assert_eq!(agg.count, 10);
        assert!(agg.avg_quality >= 0.0);
        assert!(agg.std_dev >= 0.0);
        assert!(!agg.percentiles.is_empty());
    }

    #[tokio::test]
    async fn test_cancel_evaluation() {
        let service = GraphQLService::new().await.unwrap();
        let input = EvaluationInput {
            audio_data: base64::encode("test"),
            reference_id: None,
            language: None,
            dataset_id: None,
            model_id: None,
            parameters: None,
        };

        let eval = service.evaluate_audio(input).await.unwrap();

        // Can't cancel completed evaluation
        let cancelled = service.cancel_evaluation(&eval.id).await.unwrap();
        assert!(!cancelled);
    }

    #[tokio::test]
    async fn test_delete_evaluation() {
        let service = GraphQLService::new().await.unwrap();
        let input = EvaluationInput {
            audio_data: base64::encode("test"),
            reference_id: None,
            language: None,
            dataset_id: None,
            model_id: None,
            parameters: None,
        };

        let eval = service.evaluate_audio(input).await.unwrap();
        let deleted = service.delete_evaluation(&eval.id).await.unwrap();
        assert!(deleted);

        // Should not be found after deletion
        let result = service.get_evaluation(&eval.id).await;
        assert!(result.is_err());
    }
}