use kick_api::{LiveChatClient, fetch_channel_info};
use std::time::Duration;
#[tokio::test]
#[ignore]
async fn test_connect_by_username() {
let username = std::env::var("KICK_TEST_USERNAME")
.unwrap_or_else(|_| "hello_kiko".to_string());
println!("Connecting to {}'s chat...", username);
let mut chat = LiveChatClient::connect_by_username(&username)
.await
.expect("Should connect by username");
let result = tokio::time::timeout(
Duration::from_secs(10),
chat.next_event(),
)
.await;
match result {
Ok(Ok(Some(event))) => {
println!("Received event: {}", event.event);
assert!(!event.event.is_empty());
}
Ok(Ok(None)) => println!("Connection closed"),
Ok(Err(e)) => panic!("Error: {}", e),
Err(_) => println!("No events in 10s — channel is quiet, but connection works"),
}
chat.close().await.expect("Should close cleanly");
}
#[tokio::test]
#[ignore]
async fn test_fetch_channel_info() {
let username = std::env::var("KICK_TEST_USERNAME")
.unwrap_or_else(|_| "hello_kiko".to_string());
let info = fetch_channel_info(&username)
.await
.expect("Should fetch channel info");
println!("Channel: {} (ID: {})", info.slug, info.id);
assert_eq!(info.slug, username);
assert!(info.id > 0);
assert!(info.user_id > 0);
println!("Chatroom ID: {}", info.chatroom.id);
println!("Chat mode: {:?}", info.chatroom.chat_mode);
println!("Slow mode: {} ({}s interval)", info.chatroom.slow_mode, info.chatroom.message_interval);
println!("Followers only: {}", info.chatroom.followers_mode);
println!("Subscribers only: {}", info.chatroom.subscribers_mode);
assert!(info.chatroom.id > 0);
println!("Subscriber badge tiers: {}", info.subscriber_badges.len());
for badge in &info.subscriber_badges {
println!(" {}mo: {}", badge.months, badge.badge_image.src);
assert!(badge.months > 0);
assert!(!badge.badge_image.src.is_empty());
}
if let Some(user) = &info.user {
println!("User: {} (bio: {:?})", user.username, user.bio);
assert!(!user.username.is_empty());
}
match &info.livestream {
Some(stream) => {
println!("LIVE: {} — {} viewers", stream.session_title.as_deref().unwrap_or("(no title)"), stream.viewer_count);
assert!(stream.is_live);
}
None => println!("Channel is offline"),
}
println!("Followers: {}, Verified: {}", info.followers_count, info.verified);
}
#[tokio::test]
#[ignore]
async fn test_fetch_then_connect() {
let username = std::env::var("KICK_TEST_USERNAME")
.unwrap_or_else(|_| "hello_kiko".to_string());
let info = fetch_channel_info(&username)
.await
.expect("Should fetch channel info");
let chatroom_id = info.chatroom.id;
println!("Resolved {} -> chatroom {}", username, chatroom_id);
let mut chat = LiveChatClient::connect(chatroom_id)
.await
.expect("Should connect with resolved chatroom ID");
let result = tokio::time::timeout(Duration::from_secs(10), chat.next_event()).await;
match result {
Ok(Ok(Some(event))) => println!("Got event: {}", event.event),
Ok(Ok(None)) => println!("Connection closed"),
Ok(Err(e)) => panic!("Error: {}", e),
Err(_) => println!("No events in 10s — connection works"),
}
chat.close().await.expect("Should close cleanly");
}
#[tokio::test]
#[ignore]
async fn test_connect_to_chatroom() {
let chatroom_id: u64 = std::env::var("KICK_TEST_CHATROOM_ID")
.unwrap_or_else(|_| "27670567".to_string())
.parse()
.expect("KICK_TEST_CHATROOM_ID must be a number");
let mut chat = LiveChatClient::connect(chatroom_id)
.await
.expect("Should connect to chatroom WebSocket");
let result = tokio::time::timeout(Duration::from_secs(10), chat.next_event()).await;
match result {
Ok(Ok(Some(event))) => {
println!("Received event: {}", event.event);
assert!(!event.event.is_empty());
assert!(!event.data.is_empty());
}
Ok(Ok(None)) => {
println!("Connection closed (chatroom may be inactive)");
}
Ok(Err(e)) => {
panic!("WebSocket error: {}", e);
}
Err(_) => {
println!("No events in 10s (chatroom is quiet) — connection works");
}
}
chat.close().await.expect("Should close cleanly");
}
#[tokio::test]
#[ignore]
async fn test_read_chat_message() {
let chatroom_id: u64 = std::env::var("KICK_TEST_CHATROOM_ID")
.unwrap_or_else(|_| "27670567".to_string())
.parse()
.expect("KICK_TEST_CHATROOM_ID must be a number");
let mut chat = LiveChatClient::connect(chatroom_id)
.await
.expect("Should connect to chatroom WebSocket");
let result = tokio::time::timeout(Duration::from_secs(30), chat.next_message()).await;
match result {
Ok(Ok(Some(msg))) => {
println!("Got message from {}: {}", msg.sender.username, msg.content);
assert!(!msg.id.is_empty(), "Message ID should not be empty");
assert!(!msg.content.is_empty(), "Message content should not be empty");
assert!(!msg.sender.username.is_empty(), "Sender username should not be empty");
assert!(msg.sender.id > 0, "Sender ID should be positive");
}
Ok(Ok(None)) => {
println!("Connection closed before a message arrived");
}
Ok(Err(e)) => {
panic!("Error reading message: {}", e);
}
Err(_) => {
println!("No chat messages in 30s — channel is quiet, but connection works");
}
}
chat.close().await.expect("Should close cleanly");
}
#[tokio::test]
#[ignore]
async fn test_send_ping() {
let chatroom_id: u64 = std::env::var("KICK_TEST_CHATROOM_ID")
.unwrap_or_else(|_| "27670567".to_string())
.parse()
.expect("KICK_TEST_CHATROOM_ID must be a number");
let mut chat = LiveChatClient::connect(chatroom_id)
.await
.expect("Should connect to chatroom WebSocket");
chat.send_ping().await.expect("Ping should succeed");
chat.close().await.expect("Should close cleanly");
}