// engine/test-generators/platform-test-templates-ml.ts
// SPEC-015a: ML test scaffold content builders — data validation, accuracy, drift.
function buildMLTestHeader(specTitle) {
    return [
        '"""',
        `ML test scaffold — ${specTitle}`,
        'Tests: data validation, model accuracy thresholds, drift detection',
        '"""',
        '',
        'import pytest',
        'import numpy as np',
        '# import pandas as pd',
        '# from your_model import load_model, predict',
        '',
    ];
}
function buildMLTestDataValidationClass() {
    return [
        '',
        'class TestDataValidation:',
        '    """Criterion 13a: Data validation tests."""',
        '',
        '    def test_dataset_schema(self, sample_dataset):',
        '        """Dataset must have expected columns and dtypes."""',
        '        assert "feature_1" in sample_dataset.columns',
        '        assert "feature_2" in sample_dataset.columns',
        '        assert "target" in sample_dataset.columns',
        '',
        '    def test_no_missing_values_in_features(self, sample_dataset):',
        '        """Features must not contain null values."""',
        '        feature_cols = ["feature_1", "feature_2"]',
        '        assert sample_dataset[feature_cols].isnull().sum().sum() == 0',
        '',
        '    def test_label_class_distribution(self, sample_dataset):',
        '        """Label distribution must be within acceptable imbalance ratio."""',
        '        value_counts = sample_dataset["target"].value_counts(normalize=True)',
        '        min_class_ratio = value_counts.min()',
        '        assert min_class_ratio >= 0.1, f"Severe class imbalance: {min_class_ratio:.2%}"',
        '',
        '    def test_feature_value_ranges(self, sample_dataset):',
        '        """Features must be within expected physical ranges."""',
        '        assert sample_dataset["feature_1"].between(-1e6, 1e6).all()',
    ];
}
function buildMLTestAccuracyClass() {
    return [
        '',
        '',
        'class TestModelAccuracy:',
        '    """Criterion 13b: Model accuracy thresholds."""',
        '',
        '    def test_accuracy_above_threshold(self, trained_model, test_dataset):',
        '        """Model accuracy must meet minimum threshold."""',
        '        predictions = trained_model.predict(test_dataset["features"])',
        '        accuracy = (predictions == test_dataset["target"]).mean()',
        '        assert accuracy >= 0.85, f"Accuracy {accuracy:.3f} below threshold 0.85"',
        '',
        '    def test_f1_above_threshold(self, trained_model, test_dataset):',
        '        """F1 score must meet minimum threshold."""',
        '        from sklearn.metrics import f1_score',
        '        predictions = trained_model.predict(test_dataset["features"])',
        '        f1 = f1_score(test_dataset["target"], predictions, average="weighted")',
        '        assert f1 >= 0.80, f"F1 {f1:.3f} below threshold 0.80"',
        '',
        '    def test_inference_latency(self, trained_model, sample_input):',
        '        """Inference latency must be under 100ms."""',
        '        import time',
        '        start = time.perf_counter()',
        '        trained_model.predict(sample_input)',
        '        elapsed_ms = (time.perf_counter() - start) * 1000',
        '        assert elapsed_ms < 100, f"Inference too slow: {elapsed_ms:.1f}ms"',
    ];
}
function buildMLTestDriftClass() {
    return [
        '',
        '',
        'class TestDriftDetection:',
        '    """Criterion 13c: Data drift detection."""',
        '',
        '    def test_feature_distribution_stable(self, reference_data, new_data):',
        '        """New data distribution must not drift significantly from reference."""',
        '        from scipy.stats import ks_2samp',
        '        for col in ["feature_1", "feature_2"]:',
        '            stat, p_value = ks_2samp(reference_data[col], new_data[col])',
        '            assert p_value > 0.05, f"Drift detected in {col}: p={p_value:.4f}"',
        '',
        '    def test_prediction_distribution_stable(self, model, reference_data, new_data):',
        '        """Model prediction distribution must remain stable after data change."""',
        '        ref_preds = model.predict(reference_data)',
        '        new_preds = model.predict(new_data)',
        '        ref_mean = np.mean(ref_preds)',
        '        new_mean = np.mean(new_preds)',
        '        drift_ratio = abs(new_mean - ref_mean) / (ref_mean + 1e-8)',
        '        assert drift_ratio < 0.20, f"Prediction drift exceeds 20%: {drift_ratio:.2%}"',
        '',
        '',
        '@pytest.fixture',
        'def sample_dataset():',
        '    """Provide a sample dataset for testing."""',
        '    # Replace with your actual dataset loading',
        '    import pandas as pd',
        '    return pd.DataFrame({',
        '        "feature_1": np.random.randn(100),',
        '        "feature_2": np.random.randn(100),',
        '        "target": np.random.randint(0, 2, 100),',
        '    })',
    ];
}
/** Build the full ML test content string from sub-sections. */
export function buildMLTestContent(specTitle) {
    return [
        ...buildMLTestHeader(specTitle),
        ...buildMLTestDataValidationClass(),
        ...buildMLTestAccuracyClass(),
        ...buildMLTestDriftClass(),
    ].join('\n');
}
//# sourceMappingURL=platform-test-templates-ml.js.map