alef 0.83.2

Opinionated polyglot binding generator for Rust libraries
Documentation
use super::*;

use crate::core::config::{alef_config_schema, write_alef_config_schema};

fn vendor_schema(base_dir: &Path, version: &str) -> PathBuf {
    let path = base_dir.join(DEFAULT_SCHEMA_PATH);
    write_alef_config_schema(&path, version).expect("schema writes");
    path
}

/// Control, and the scope guarantee: a repository that never vendored the schema must get no
/// finding at all. `alef verify` reports on a file that exists; it never proposes one. ~keep
#[test]
fn tree_without_a_vendored_schema_reports_nothing() {
    let dir = tempfile::tempdir().expect("tempdir");

    assert!(find_stale_vendored_schema(dir.path(), "1.2.3").is_none());
}

/// Control: a current copy is not reported, so a finding cannot be mistaken for one this check
/// emits unconditionally. ~keep
#[test]
fn current_vendored_schema_reports_nothing() {
    let dir = tempfile::tempdir().expect("tempdir");
    vendor_schema(dir.path(), "1.2.3");

    assert!(find_stale_vendored_schema(dir.path(), "1.2.3").is_none());
}

#[test]
fn schema_describing_a_different_surface_is_reported_and_gates_the_exit_code() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join(DEFAULT_SCHEMA_PATH);
    std::fs::create_dir_all(path.parent().expect("schema has a parent")).expect("mkdir");
    let mut schema = alef_config_schema("1.2.3").expect("schema generation succeeds");
    schema
        .as_object_mut()
        .expect("schema root is an object")
        .insert("properties".to_string(), serde_json::json!({}));
    std::fs::write(&path, serde_json::to_string_pretty(&schema).expect("renders")).expect("writes");

    let finding = find_stale_vendored_schema(dir.path(), "1.2.3").expect("shape drift is reported");

    assert!(finding.describes_a_different_surface());
    let lines = finding.report_lines();
    assert_eq!(lines.len(), 2, "expected a heading and one path line, got: {lines:?}");
    assert!(
        lines[0].contains("describes a different alef.toml surface"),
        "heading must name the actual problem, got: {}",
        lines[0]
    );
    assert!(
        lines[1].contains(DEFAULT_SCHEMA_PATH) && lines[1].contains("alef schema --output"),
        "detail line must name the path and the remedy command, got: {}",
        lines[1]
    );
}

/// An unparseable vendored copy is drift too: it validates nothing, which is the same wrong
/// answer as validating against the wrong surface. ~keep
#[test]
fn unparseable_vendored_schema_gates_the_exit_code() {
    let dir = tempfile::tempdir().expect("tempdir");
    let path = dir.path().join(DEFAULT_SCHEMA_PATH);
    std::fs::create_dir_all(path.parent().expect("schema has a parent")).expect("mkdir");
    std::fs::write(&path, "{ truncated").expect("writes");

    let finding = find_stale_vendored_schema(dir.path(), "1.2.3").expect("unparseable copy is reported");

    assert!(finding.describes_a_different_surface());
}

/// The upgrade case the consumer actually hit: a new alef over a copy generated by the previous
/// one. It is visible in the report, names both versions and the remedy, and does NOT fail
/// verification -- otherwise every consumer who vendors the schema goes red on every release. ~keep
#[test]
fn version_stamp_drift_is_reported_without_gating_the_exit_code() {
    let dir = tempfile::tempdir().expect("tempdir");
    vendor_schema(dir.path(), "1.2.3");

    let finding = find_stale_vendored_schema(dir.path(), "1.2.4").expect("version stamp drift is reported");

    assert!(!finding.describes_a_different_surface());
    let lines = finding.report_lines();
    assert_eq!(lines.len(), 2, "expected a heading and one path line, got: {lines:?}");
    assert!(
        lines[0].contains("informational"),
        "heading must mark itself non-blocking, got: {}",
        lines[0]
    );
    assert!(
        lines[1].contains("stamped 1.2.3") && lines[1].contains("running alef 1.2.4"),
        "detail line must name both versions, got: {}",
        lines[1]
    );
    assert!(
        lines[1].contains("alef schema --output"),
        "detail line must name the remedy command, got: {}",
        lines[1]
    );
}