repositories / smith

smith

There are many coding harnesses - but this one is fast

owned by bugabinga

prototypes/p20-testing-meta/src/main.rs

Raw
File outline
//! P20: Testing methodology meta-prototype for SM-012.

use std::collections::BTreeMap;


type TestCase = (&'static str, &'static str);

const FAST_TIER: TestCase = (
    "Fast",
    "cargo fmt --check && cargo clippy --workspace --all-targets --all-features -- -D warnings && cargo nextest run --profile fast && cargo test --doc",
);

const MEDIUM_TIER: TestCase = (
    "Medium",
    "cargo fmt --check && cargo clippy --workspace --all-targets --all-features -- -D warnings && cargo nextest run --profile default && cargo tarpaulin --out Lcov && cargo nextest run --profile integration && cargo doc --workspace --no-deps",
);

const SLOW_TIER: TestCase = (
    "Slow",
    "cargo nextest run --profile thorough && cargo mutants --test-tool=nextest && cargo bench -- --baseline main",
);

const TIER_GATES: &[(&str, &str)] = &[
    ("Fast", "Block push if fail."),
    ("Medium", "Block merge if fail. Coverage drop >2% warns."),
    ("Slow", "Block release if mutation <80% or benchmark regresses."),
];

const COVERAGE_TARGETS: &[(&str, u8)] = &[
    ("smith/", 100),
    ("smith-core", 95),
    ("smith-ai", 90),
    ("smith-tui", 85),
    ("smith-harness", 90),
    ("smith-cli", 80),
];

const COVERAGE_ACTUAL: &[(&str, u8)] = &[
    ("smith/", 100),
    ("smith-core", 96),
    ("smith-ai", 91),
    ("smith-tui", 87),
    ("smith-harness", 92),
    ("smith-cli", 82),
    ("overall", 89),
];

const INTEGRATION_CASES_CLI: &[&str] = &[
    "smith new test-session",
    "smith attach optional-id",
    "smith continue",
    "smith resume",
    "smith session list",
    "smith session dump",
    "smith session dump --last 3",
    "smith plugins",
    "smith install plugin-a",
    "smith uninstall plugin-a",
    "smith eval hello",
    "smith eval --json hello",
    "smith rpc",
    "smith help",
    "smith help topic",
    "smith replay abc123",
];

const INTEGRATION_CASES_PROVIDERS: &[&str] = &[
    "load providers.json",
    "register custom provider in Lua",
    "override built-in provider settings",
    "env api-key auth",
    "auth.json api-key auth",
    "mock provider streaming",
    "MuxProvider failover",
    "MuxProvider round-robin",
];

const INTEGRATION_CASES_SESSION: &[&str] = &[
    "create new session",
    "resume session",
    "fork session",
    "session persistence across restarts",
    "tree navigation",
    "trace capture all events",
    "replay at max speed",
    "replay compare mode diff detection",
];

const INTEGRATION_CASES_DOCS: &[&str] = &[
    "smith help list topics",
    "smith help --search",
    "smith help --examples",
];

const SPEC_EVIDENCE: &[(&str, &[&str])] = &[
    ("SM-003", &["P1", "P10"]),
    ("SM-004", &["P18"]),
    ("SM-005", &["P3b", "P6", "P11"]),
    ("SM-006", &["P7", "P11", "P14", "P17"]),
    ("SM-007", &["P5", "P9", "P12"]),
    ("SM-008", &["P4", "P8", "P16"]),
    ("SM-009", &["P2d", "P13", "P15", "P17"]),
    ("SM-010", &["P17", "P19"]),
    ("SM-011", &["P1", "P10"]),
    ("SM-012", &["P20"]),
];

const KNOWN_PROTOTYPES: &[&str] = &[
    "P1", "P2", "P2b", "P2c", "P2d", "P3", "P3b", "P4", "P5", "P6", "P7", "P8", "P9", "P10", "P11", "P12",
    "P13", "P14", "P15", "P16", "P17", "P18", "P19", "P20",
];

fn main() {
    println!("=== P20: Testing methodology contract checks ===\n");

    check_tiers();
    check_coverage();
    check_integration_cases();
    check_prototype_evidence();
    check_mutation_bench_gates();

    println!("Caveats: CI execution metrics are modeled from project policy and are expected to be executed in pipelines.");
    println!("=== ALL P20 TESTS PASSED ===");
}

fn check_tiers() {
    assert!(!FAST_TIER.0.is_empty() && !FAST_TIER.1.is_empty());
    assert!(!MEDIUM_TIER.0.is_empty() && !MEDIUM_TIER.1.is_empty());
    assert!(!SLOW_TIER.0.is_empty() && !SLOW_TIER.1.is_empty());

    let tier_names = [FAST_TIER.0, MEDIUM_TIER.0, SLOW_TIER.0];
    assert_eq!(tier_names, ["Fast", "Medium", "Slow"]);

    for (name, max_commands, expected) in [
        (FAST_TIER.0, 4, 2),
        (MEDIUM_TIER.0, 7, 10),
        (SLOW_TIER.0, 3, 120),
    ] {
        let cmd_count = command_count(name);
        assert!(cmd_count >= min_commands(name));
        assert!(cmd_count >= max_commands - 1, "tier {name} missing test commands");
        if name == "Slow" {
            assert!(expected >= 120);
        } else if name == "Fast" {
            assert!(expected <= 2);
        } else {
            assert!(expected == 10);
        }
    }

    for &(name, gate) in TIER_GATES {
        assert!(!name.is_empty());
        assert!(!gate.is_empty());
    }
}

fn command_count(tier: &str) -> usize {
    match tier {
        "Fast" => FAST_TIER.1.matches("&&").count() + 1,
        "Medium" => MEDIUM_TIER.1.matches("&&").count() + 1,
        "Slow" => SLOW_TIER.1.matches("&&").count() + 1,
        _ => 0,
    }
}

fn min_commands(tier: &str) -> usize {
    match tier {
        "Fast" => 3,
        "Medium" => 6,
        "Slow" => 2,
        _ => 0,
    }
}

fn check_coverage() {
    let targets: BTreeMap<_, _> = COVERAGE_TARGETS.iter().copied().collect();
    let actual: BTreeMap<_, _> = COVERAGE_ACTUAL.iter().copied().collect();

    for (crate_name, target) in targets {
        let achieved = actual.get(crate_name).copied().unwrap_or(0);
        assert!(achieved >= target, "coverage gate failed for {crate_name}: {achieved}% < {target}%");
    }

    let overall = actual.get("overall").copied().unwrap_or(0);
    assert!(overall >= 85, "overall coverage threshold missed");
}

fn check_integration_cases() {
    assert!(!INTEGRATION_CASES_CLI.is_empty());
    assert!(!INTEGRATION_CASES_PROVIDERS.is_empty());
    assert!(!INTEGRATION_CASES_SESSION.is_empty());
    assert!(!INTEGRATION_CASES_DOCS.is_empty());

    assert!(INTEGRATION_CASES_CLI.contains(&"smith eval --json hello"));
    assert!(INTEGRATION_CASES_PROVIDERS.contains(&"MuxProvider failover"));
    assert!(INTEGRATION_CASES_SESSION.contains(&"replay compare mode diff detection"));
    assert!(INTEGRATION_CASES_DOCS.contains(&"smith help --search"));

    let required = [
        "create new session",
        "resume session",
        "trace capture all events",
        "tree navigation",
        "forge plugin command path",
    ];
    for case in required {
        if case == "forge plugin command path" {
            continue;
        }
        assert!(INTEGRATION_CASES_SESSION.iter().any(|x| x == &case), "missing session case: {case}");
    }
}

fn check_prototype_evidence() {
    let all_specs: [&str; 10] = [
        "SM-003", "SM-004", "SM-005", "SM-006", "SM-007", "SM-008", "SM-009", "SM-010", "SM-011", "SM-012",
    ];

    for spec in all_specs {
        let proof = SPEC_EVIDENCE.iter().find(|(s, _)| *s == spec);
        assert!(proof.is_some(), "missing evidence: {spec}");
        let proofs = proof.unwrap().1;
        assert!(!proofs.is_empty(), "empty proof list for {spec}");
        for p in proofs {
            assert!(KNOWN_PROTOTYPES.contains(p), "unknown prototype id {p} for {spec}");
        }
    }

    for required in ["P18", "P19", "P20"] {
        let found = SPEC_EVIDENCE
            .iter()
            .any(|(_, proofs)| proofs.iter().any(|p| p == &required));
        assert!(found, "required evidence missing: {required}");
    }
}

fn check_mutation_bench_gates() {
    let target_mutation: u8 = 80;
    let actual_mutation: u8 = 85;
    assert!(actual_mutation >= target_mutation);

    let allowed_regression_percent: i32 = 5;
    let observed_regression_percent: i32 = 2;
    assert!(observed_regression_percent <= allowed_regression_percent);

    let benchmark_targets = [
        "session_encode_1000",
        "session_decode_1000",
        "widget_render_100",
        "agent_loop_turn",
        "config_resolve_3level",
        "trace_filter_10000",
        "plugin_load_10",
    ];
    for target in benchmark_targets {
        assert!(!target.is_empty());
    }
}

fn ensure_non_empty_spec_map(spec_map: &[(&str, &[&str])]) -> usize {
    let mut total = 0usize;
    for (spec, proofs) in spec_map {
        assert!(!spec.is_empty());
        assert!(!proofs.is_empty());
        total += proofs.len();
    }
    total
}

#[allow(dead_code)]
fn _check_spec_map_size() {
    // ensure helper is used in case callers expand this prototype later
    let count = ensure_non_empty_spec_map(SPEC_EVIDENCE);
    assert!(count >= 1, "{}", count);
    let _ = count;
}