aprender-serve 0.64.0

Pure Rust ML inference engine built from scratch - model serving for GGUF and safetensors

/// Test PARITY-010b: QA-039 Automatic model download from Hugging Face
#[test]
fn test_parity010b_model_download() {
    /// Model download configuration
    #[derive(Debug, Clone)]
    struct ModelDownloadConfig {
        repo_id: String,
        filename: String,
        revision: String,
        cache_dir: String,
    }

    impl ModelDownloadConfig {
        fn new(repo_id: &str, filename: &str) -> Self {
            Self {
                repo_id: repo_id.to_string(),
                filename: filename.to_string(),
                revision: "main".to_string(),
                cache_dir: "~/.cache/huggingface/hub".to_string(),
            }
        }

        fn url(&self) -> String {
            format!(
                "https://huggingface.co/{}/resolve/{}/{}",
                self.repo_id, self.revision, self.filename
            )
        }

        fn cache_path(&self) -> String {
            let repo_dir = self.repo_id.replace('/', "--");
            format!(
                "{}/models--{}/snapshots/{}/{}",
                self.cache_dir, repo_dir, self.revision, self.filename
            )
        }
    }

    /// Model download status
    #[derive(Debug, Clone)]
    enum DownloadStatus {
        Cached(String),     // Already in cache
        Downloaded(String), // Freshly downloaded
        Failed(String), // Download failed
    }

    /// Model downloader (test)
    struct ModelDownloader {
        configs: Vec<ModelDownloadConfig>,
    }

    impl ModelDownloader {
        fn new() -> Self {
            Self {
                configs: vec![
                    ModelDownloadConfig::new("TheBloke/phi-2-GGUF", "phi-2.Q4_K_M.gguf"),
                    ModelDownloadConfig::new("microsoft/phi-2", "model.safetensors"),
                ],
            }
        }

        /// Simulate download check
        fn check_or_download(&self, config: &ModelDownloadConfig, cached: bool) -> DownloadStatus {
            if cached {
                DownloadStatus::Cached(config.cache_path())
            } else {
                // In real impl: download from config.url()
                DownloadStatus::Downloaded(config.cache_path())
            }
        }
    }

    let downloader = ModelDownloader::new();
    let config = &downloader.configs[0];

    // Test: Model already cached
    let status = downloader.check_or_download(config, true);
    assert!(
        matches!(status, DownloadStatus::Cached(_)),
        "QA-039: Should return cached"
    );

    // Test: Model needs download
    let status = downloader.check_or_download(config, false);
    assert!(
        matches!(status, DownloadStatus::Downloaded(_)),
        "QA-039: Should download"
    );

    // Test: URL construction
    let url = config.url();
    assert!(
        url.contains("huggingface.co"),
        "QA-039: URL should be HuggingFace"
    );
    assert!(
        url.contains(&config.repo_id),
        "QA-039: URL should contain repo"
    );
    assert!(
        url.contains(&config.filename),
        "QA-039: URL should contain filename"
    );

    println!("\nPARITY-010b: Model download from HuggingFace");
    println!("  Repo: {}", config.repo_id);
    println!("  File: {}", config.filename);
    println!("  URL: {}", config.url());
}

/// Test PARITY-010c: QA-040 JSON schema validation for benchmark results
#[test]
fn test_parity010c_json_schema_validation() {
    /// JSON schema field definition
    #[derive(Debug, Clone)]
    struct SchemaField {
        name: String,
        field_type: FieldType,
        required: bool,
    }

    #[derive(Debug, Clone)]
    enum FieldType {
        String,
        Number,
        Integer,
        Boolean,
        Array(Box<FieldType>),
        Object(Vec<SchemaField>),
    }

    /// Benchmark result schema
    #[derive(Debug)]
    struct BenchmarkResultSchema {
        version: String,
        fields: Vec<SchemaField>,
    }

    impl BenchmarkResultSchema {
        fn v1() -> Self {
            Self {
                version: "1.0.0".to_string(),
                fields: vec![
                    SchemaField {
                        name: "schema_version".to_string(),
                        field_type: FieldType::String,
                        required: true,
                    },
                    SchemaField {
                        name: "timestamp".to_string(),
                        field_type: FieldType::String,
                        required: true,
                    },
                    SchemaField {
                        name: "git_commit".to_string(),
                        field_type: FieldType::String,
                        required: true,
                    },
                    SchemaField {
                        name: "throughput_tps".to_string(),
                        field_type: FieldType::Number,
                        required: true,
                    },
                    SchemaField {
                        name: "latency_p50_ms".to_string(),
                        field_type: FieldType::Number,
                        required: true,
                    },
                    SchemaField {
                        name: "latency_p95_ms".to_string(),
                        field_type: FieldType::Number,
                        required: true,
                    },
                    SchemaField {
                        name: "latency_p99_ms".to_string(),
                        field_type: FieldType::Number,
                        required: true,
                    },
                    SchemaField {
                        name: "cv".to_string(),
                        field_type: FieldType::Number,
                        required: true,
                    },
                    SchemaField {
                        name: "iterations".to_string(),
                        field_type: FieldType::Integer,
                        required: true,
                    },
                    SchemaField {
                        name: "environment".to_string(),
                        field_type: FieldType::Object(vec![
                            SchemaField {
                                name: "os".to_string(),
                                field_type: FieldType::String,
                                required: true,
                            },
                            SchemaField {
                                name: "arch".to_string(),
                                field_type: FieldType::String,
                                required: true,
                            },
                            SchemaField {
                                name: "cpu_cores".to_string(),
                                field_type: FieldType::Integer,
                                required: true,
                            },
                        ]),
                        required: true,
                    },
                ],
            }
        }

        fn required_field_count(&self) -> usize {
            self.fields.iter().filter(|f| f.required).count()
        }

        fn validate_field_presence(&self, field_names: &[&str]) -> Vec<String> {
            let mut missing = Vec::new();
            for field in &self.fields {
                if field.required && !field_names.contains(&field.name.as_str()) {
                    missing.push(field.name.clone());
                }
            }
            missing
        }
    }

    let schema = BenchmarkResultSchema::v1();

    // Test: Schema version
    assert_eq!(
        schema.version, "1.0.0",
        "QA-040: Schema version should be 1.0.0"
    );

    // Test: Required fields
    assert!(
        schema.required_field_count() >= 9,
        "QA-040: Should have >=9 required fields"
    );

    // Test: Validation with all fields
    let all_fields = vec![
        "schema_version",
        "timestamp",
        "git_commit",
        "throughput_tps",
        "latency_p50_ms",
        "latency_p95_ms",
        "latency_p99_ms",
        "cv",
        "iterations",
        "environment",
    ];
    let missing = schema.validate_field_presence(&all_fields);
    assert!(missing.is_empty(), "QA-040: All required fields present");

    // Test: Validation with missing fields
    let partial_fields = vec!["schema_version", "throughput_tps"];
    let missing = schema.validate_field_presence(&partial_fields);
    assert!(!missing.is_empty(), "QA-040: Should detect missing fields");
    assert!(
        missing.contains(&"timestamp".to_string()),
        "QA-040: timestamp should be missing"
    );

    println!("\nPARITY-010c: JSON schema validation");
    println!("  Schema version: {}", schema.version);
    println!("  Required fields: {}", schema.required_field_count());
    println!("  Total fields: {}", schema.fields.len());
}

/// Test PARITY-010d: Combined preflight and validation suite
#[test]
fn test_parity010d_benchmark_preflight_suite() {
    /// Complete preflight suite combining all checks
    #[derive(Debug)]
    struct BenchmarkPreflightSuite {
        server_checks: Vec<(&'static str, bool)>, // (name, required)
        model_checks: Vec<&'static str>,          // model repo IDs
        schema_version: &'static str,
    }

    impl BenchmarkPreflightSuite {
        fn standard() -> Self {
            Self {
                server_checks: vec![("Ollama", true), ("llama.cpp", false), ("vLLM", false)],
                model_checks: vec!["TheBloke/phi-2-GGUF", "microsoft/phi-2"],
                schema_version: "1.0.0",
            }
        }

        fn run_all(&self, servers_up: &[bool], models_cached: &[bool]) -> PreflightResult {
            let mut result = PreflightResult::default();

            // Server checks
            for ((name, required), &up) in self.server_checks.iter().zip(servers_up.iter()) {
                if up {
                    result.servers_passed += 1;
                } else if *required {
                    result.servers_failed += 1;
                    result.errors.push(format!("{} unavailable", name));
                } else {
                    result.servers_skipped += 1;
                }
            }

            // Model checks
            for (_model, &cached) in self.model_checks.iter().zip(models_cached.iter()) {
                if cached {
                    result.models_cached += 1;
                } else {
                    result.models_to_download += 1;
                }
            }

            result.schema_valid = true;
            result
        }
    }

    #[derive(Debug, Default)]
    struct PreflightResult {
        servers_passed: usize,
        servers_failed: usize,
        servers_skipped: usize,
        models_cached: usize,
        models_to_download: usize,
        schema_valid: bool,
        errors: Vec<String>,
    }

    impl PreflightResult {
        fn can_proceed(&self) -> bool {
            self.servers_failed == 0 && self.schema_valid
        }
    }

    let suite = BenchmarkPreflightSuite::standard();

    // Test: All ready
    let result = suite.run_all(&[true, true, true], &[true, true]);
    assert!(
        result.can_proceed(),
        "QA-038-040: Should proceed when all ready"
    );
    assert_eq!(result.servers_passed, 3);
    assert_eq!(result.models_cached, 2);

    // Test: Required server down
    let result = suite.run_all(&[false, true, true], &[true, true]);
    assert!(
        !result.can_proceed(),
        "QA-038-040: Should not proceed if required down"
    );

    // Test: Model needs download
    let result = suite.run_all(&[true, false, false], &[false, true]);
    assert!(
        result.can_proceed(),
        "QA-038-040: Can proceed with download needed"
    );
    assert_eq!(result.models_to_download, 1);

    println!("\nPARITY-010d: Complete preflight suite");
    println!("  Server checks: {}", suite.server_checks.len());
    println!("  Model checks: {}", suite.model_checks.len());
    println!("  Schema: {}", suite.schema_version);
}