//! Worked consumer of the opt-in model-registry build gate.
//!
//! Each case configures the Ollama shortcut (so the model string is observable
//! to `enforce_model_registry`) via `App::local().model(..)`, then builds with
//! the committed example registry. `App::build()` only *constructs* the Ollama
//! client (no network I/O until a run), so these run offline without Docker or
//! a live Ollama. The registry check runs before any shortcut resolution.

use klieo::App;
use klieo_core::Error;

/// Committed example registry, resolved relative to the crate root (the cwd for
/// `cargo test`). `qwen2.5:14b` active, `llama2:7b` deprecated, `legacy-model`
/// sunset.
const REGISTRY: &str = "examples/model-registry/klieo-models.yaml";

/// Build a local-defaults App with `model` on the Ollama shortcut, optionally
/// enforcing `registry`. Construction-only — never contacts Ollama.
async fn build_with(model: &str, registry: Option<&str>) -> Result<App, Error> {
    let mut builder = App::local().model(model);
    if let Some(path) = registry {
        builder = builder.model_registry(path);
    }
    builder.build().await
}

#[tokio::test]
async fn active_model_builds() {
    assert!(build_with("qwen2.5:14b", Some(REGISTRY)).await.is_ok());
}

#[tokio::test]
async fn deprecated_model_still_builds() {
    assert!(build_with("llama2:7b", Some(REGISTRY)).await.is_ok());
}

#[tokio::test]
async fn unpinned_model_builds() {
    assert!(build_with("brand-new-model", Some(REGISTRY)).await.is_ok());
}

#[tokio::test]
async fn sunset_model_fails_build() {
    let err = build_with("legacy-model", Some(REGISTRY))
        .await
        .expect_err("sunset model must fail the build");
    assert!(
        matches!(err, Error::ModelSunset { .. }),
        "expected Error::ModelSunset, got {err:?}"
    );
}

#[tokio::test]
async fn sunset_model_builds_without_registry() {
    assert!(build_with("legacy-model", None).await.is_ok());
}
