vc-runtime 0.1.0

Runtime backend for Visual Compiler
Documentation
//! Common test helpers for runtime integration tests.

use anyhow::Result;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use tempfile::TempDir;
use vc_output::CompilerOutput;
use vc_prompt::{CharacterVisual, PromptCore};
use vc_runtime::{
    config::model::{
        AppConfig, BackendDefinition, BackendOptions, DummyConfig, GlobalConfig, RuntimeOverrides,
        RuntimeProfile,
    },
    renderer::OUTPUT_FILENAME,
    RuntimeInput,
};
use vc_shared::hashing::{sha256, Sha256Digest};
use vc_types::{AdapterKind, CompilerMetadata, CompilerVersion};

/// Creates a minimal valid RuntimeInput for testing.
pub fn minimal_runtime_input() -> RuntimeInput {
    RuntimeInput {
        vc_output: CompilerOutput {
            schema_version: 1,
            prompt: PromptCore {
                character: CharacterVisual { descriptors: vec!["test".to_string()] },
            },
            metadata: CompilerMetadata {
                compiler_version: CompilerVersion::new(0, 0, 1),
                adapter: AdapterKind::Sdxl,
                snapshot_hash: Sha256Digest::from([0; 32]),
                bundle_hash: Sha256Digest::from([0; 32]),
                prompt_hash: Sha256Digest::from([0; 32]),
            },
        },
    }
}

/// Creates a minimal valid AppConfig with a Dummy backend.
#[allow(dead_code)]
pub fn dummy_app_config() -> AppConfig {
    let mut backends = HashMap::new();
    backends.insert(
        "dummy".to_string(),
        BackendDefinition { enabled: true, options: BackendOptions::Dummy(DummyConfig::default()) },
    );

    let mut profiles = HashMap::new();
    profiles.insert(
        "default".to_string(),
        RuntimeProfile {
            backend_name: "dummy".to_string(),
            overrides: RuntimeOverrides::default(),
        },
    );

    AppConfig { global: GlobalConfig::default(), profiles, backends }
}

/// Creates a temporary directory and returns both the dir and its path.
#[allow(dead_code)]
pub fn temp_output_dir() -> Result<(TempDir, Box<Path>)> {
    let temp = tempfile::tempdir()?;
    let path = temp.path().to_path_buf().into_boxed_path();
    Ok((temp, path))
}

/// Asserts that the image file exists and is non-empty.
#[allow(dead_code)]
pub fn assert_image_exists(output_dir: &Path) -> Result<()> {
    let image_path = output_dir.join(OUTPUT_FILENAME);
    assert!(image_path.exists());
    let metadata = fs::metadata(&image_path)?;
    assert!(metadata.len() > 0);
    Ok(())
}

/// Returns the SHA256 hash of the image file.
#[allow(dead_code)]
pub fn get_image_hash(output_dir: &Path) -> Result<String> {
    let image_path = output_dir.join(OUTPUT_FILENAME);
    let bytes = fs::read(&image_path)?;
    Ok(sha256(&bytes).to_hex())
}