reasoninglayer 1.0.3

Rust client SDK for the Reasoning Layer API
Documentation
//! Shared helpers for live-backend tests (`tests/diagnosis.rs`,
//! `tests/scenarios.rs`).
//!
//! These tests are gated with `#[ignore]` so a default `cargo test` run
//! skips them. Run them explicitly with:
//!
//! ```sh
//! cargo test --test diagnosis -- --ignored
//! cargo test --test scenarios -- --ignored
//! cargo test -- --ignored                    # both
//! ```
//!
//! Set `RL_BACKEND_URL` to point at a backend other than `http://localhost:8083`.
//! Each test creates a fresh UUID-v4 tenant so concurrent runs don't collide,
//! and best-effort wipes the tenant on teardown via `AdminClient::clear_tenant_data`.

#![allow(dead_code)]

use std::time::Duration;

use reasoninglayer::{AuthConfig, ClientConfig, ReasoningLayerClient};
use uuid::Uuid;

/// Backend URL — override with `RL_BACKEND_URL` env var.
pub fn backend_url() -> String {
    std::env::var("RL_BACKEND_URL").unwrap_or_else(|_| "http://localhost:8083".to_string())
}

/// Generate a fresh per-test tenant ID. Random UUID v4 keeps concurrent test
/// runs isolated.
pub fn fresh_tenant() -> String {
    Uuid::new_v4().to_string()
}

/// Build a client targeting the live backend with a one-shot tenant.
pub fn build_client(tenant_id: &str) -> ReasoningLayerClient {
    let config = ClientConfig::new(backend_url(), tenant_id, AuthConfig::Cookie)
        .with_timeout(Duration::from_secs(30))
        .with_max_retries(1);
    ReasoningLayerClient::new(config).expect("client builds")
}

/// Best-effort cleanup at the end of a test. Logs but does not fail if the
/// tenant wipe didn't go through — tests should still pass on cleanup error.
pub async fn cleanup(client: &ReasoningLayerClient, tenant_id: &str) {
    if let Err(err) = client.admin().clear_tenant_data(tenant_id, None).await {
        eprintln!("[cleanup] failed to clear tenant {tenant_id}: {err}");
    }
}

/// Bail-with-message helper. If the backend isn't reachable, panic with a
/// hint so the failure mode is obvious in `cargo test` output.
pub async fn require_backend(client: &ReasoningLayerClient) {
    if let Err(err) = client.health().check(None).await {
        panic!(
            "[require_backend] {} not reachable: {err}\n\
             Set RL_BACKEND_URL to point at a running backend, or remove `--ignored` to skip.",
            backend_url()
        );
    }
}