vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! Error contract tests.

use anyhow::Result;
#[cfg(any(feature = "comfy-renderer", feature = "python-renderer"))]
use std::path::PathBuf;
#[cfg(any(feature = "comfy-renderer", feature = "python-renderer"))]
use url::Url;
#[cfg(any(feature = "comfy-renderer", feature = "python-renderer"))]
use vc_runtime::backend::{build_renderer, BuildContext};
#[cfg(feature = "comfy-renderer")]
use vc_runtime::config::model::ComfyConfig;
#[cfg(feature = "python-renderer")]
use vc_runtime::config::model::PythonConfig;
#[cfg(any(feature = "comfy-renderer", feature = "python-renderer"))]
use vc_runtime::config::model::{BackendDefinition, BackendOptions};

#[cfg(feature = "comfy-renderer")]
#[test]
fn registry_returns_factory_error_on_missing_comfy_context() -> Result<()> {
    let def = BackendDefinition {
        enabled: true,
        options: BackendOptions::ComfyUi(ComfyConfig {
            base_url: Url::parse("http://127.0.0.1:8188")?,
            timeout_secs: 30,
            poll_timeout_secs: 300,
            poll_interval_ms: 500,
            workflow_template_path: PathBuf::from("workflow.json"),
        }),
    };
    let ctx = BuildContext::builder().build();
    let result = build_renderer(&def, &ctx);

    assert!(result.is_err());
    if let Err(e) = result {
        let msg = e.to_string();
        assert!(msg.contains("ComfyUI context not provided"), "Unexpected error message: {}", msg);
    }
    Ok(())
}

#[cfg(feature = "python-renderer")]
#[test]
fn registry_returns_factory_error_on_missing_python_context() -> Result<()> {
    let def = BackendDefinition {
        enabled: true,
        options: BackendOptions::Python(PythonConfig {
            script_path: PathBuf::from("script.py"),
            python_path: Some(PathBuf::from("python3")),
        }),
    };
    let ctx = BuildContext::builder().build();
    let result = build_renderer(&def, &ctx);

    assert!(result.is_err());
    if let Err(e) = result {
        let msg = e.to_string();
        assert!(msg.contains("PythonContext is required"), "Unexpected error message: {}", msg);
    }
    Ok(())
}

#[test]
fn loader_returns_parse_error_for_invalid_toml() -> Result<()> {
    use std::io::Write;
    use tempfile::NamedTempFile;
    use vc_runtime::config::loader::load_config;

    let invalid = "this is not toml [";
    let mut file = NamedTempFile::new()?;
    file.write_all(invalid.as_bytes())?;

    let result = load_config(file.path());
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("TOML parse error"),
        "Expected TOML parse error, got: {}",
        err
    );
    Ok(())
}