refluxer 0.1.0

Rust API wrapper for Fluxer
Documentation
//! Shared test helpers for integration tests.
//!
//! Required env vars:
//!   FLUXER_TOKEN  — bot token
//!   FLUXER_GUILD_ID — guild (server) ID to test against
//!
//! Optionally load from `.env` file in the workspace root.
//!
//! NOTE: Fluxer requires an active Gateway session before the bot can send
//! messages via the HTTP API. Tests that need to send messages must call
//! `start_gateway_session()` and keep the returned connection alive.
//!
//! Run all: cargo test -p refluxer --features client -- --test-threads=1

use std::sync::Once;

use refluxer::http::HttpClient;
use refluxer::model::id::GuildId;

static INIT: Once = Once::new();

pub fn init_env() {
    INIT.call_once(|| {
        let _ = dotenvy::from_filename("../.env");
        let _ = dotenvy::dotenv();
    });
}

pub fn setup() -> (HttpClient, GuildId) {
    init_env();

    let token = std::env::var("FLUXER_TOKEN").expect("FLUXER_TOKEN env var required");
    let guild_id: u64 = std::env::var("FLUXER_GUILD_ID")
        .expect("FLUXER_GUILD_ID env var required")
        .parse()
        .expect("FLUXER_GUILD_ID must be a number");

    let http = HttpClient::new(&token).expect("valid HTTP client");
    (http, GuildId::new(guild_id))
}

/// Start a Gateway session so the bot can send messages.
/// Returns a GatewayConnection that must be kept alive for the duration.
#[allow(dead_code)]
pub async fn start_gateway_session() -> refluxer::gateway::GatewayConnection {
    init_env();
    let token = std::env::var("FLUXER_TOKEN").expect("FLUXER_TOKEN env var required");

    let gateway_url = "wss://gateway.fluxer.app";

    let gw = refluxer::gateway::GatewayConnection::connect(gateway_url, &token)
        .await
        .expect("failed to connect to gateway");

    // Give the server a moment to process our identify
    tokio::time::sleep(std::time::Duration::from_secs(2)).await;

    gw
}