#![allow(dead_code)]
use slacko::{AuthConfig, SlackClient};
use std::sync::Once;
static INIT: Once = Once::new();
pub fn init() {
INIT.call_once(|| {
if std::env::var("RUST_LOG").is_ok() {
tracing_subscriber::fmt().with_test_writer().init();
}
});
}
pub fn test_client() -> Option<SlackClient> {
init();
match AuthConfig::from_env() {
Ok(auth) => SlackClient::new(auth).ok(),
Err(_) => None,
}
}
#[macro_export]
macro_rules! skip_if_no_client {
($client:expr) => {
match $client {
Some(c) => c,
None => {
eprintln!("Skipping test: No Slack credentials in environment");
return;
}
}
};
}
pub fn test_channel() -> String {
std::env::var("SLACK_TEST_CHANNEL").unwrap_or_else(|_| "general".to_string())
}
pub fn test_user_id() -> Option<String> {
std::env::var("SLACK_TEST_USER_ID").ok()
}
pub fn unique_message(prefix: &str) -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis();
format!("[TEST-{}] {} - {}", timestamp, prefix, uuid_v4())
}
fn uuid_v4() -> String {
use std::time::{SystemTime, UNIX_EPOCH};
let t = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
format!(
"{:x}-{:x}-4{:x}-{:x}-{:x}",
(t >> 96) as u32,
(t >> 80) as u16,
((t >> 64) as u16) & 0x0fff,
((t >> 48) as u16 & 0x3fff) | 0x8000,
t as u64 & 0xffffffffffff
)
}
pub async fn cleanup_message(client: &SlackClient, channel: &str, ts: &str) {
let _ = client.chat().delete_message(channel, ts).await;
}
pub async fn cleanup_leave_channel(client: &SlackClient, channel: &str) {
let _ = client.conversations().leave(channel).await;
}