bottest/tests/e2e/chat.rs

148 lines
4.6 KiB
Rust
Raw Normal View History

use super::{should_run_e2e_tests, E2ETestContext};
use anyhow::{bail, Result};
2025-12-06 11:05:57 -03:00
use bottest::prelude::*;
use bottest::web::Locator;
#[tokio::test]
async fn test_chat_hi() -> Result<()> {
2025-12-06 11:05:57 -03:00
if !should_run_e2e_tests() {
eprintln!("Skipping: E2E tests disabled");
return Ok(());
2025-12-06 11:05:57 -03:00
}
let ctx = E2ETestContext::setup_with_browser().await?;
2025-12-06 11:05:57 -03:00
if !ctx.has_browser() {
ctx.close().await;
bail!("Browser not available - cannot run E2E test");
2025-12-06 11:05:57 -03:00
}
if ctx.ui.is_none() {
2025-12-06 11:05:57 -03:00
ctx.close().await;
bail!("BotUI not available - chat tests require botui running on port 3000");
2025-12-06 11:05:57 -03:00
}
let browser = ctx.browser.as_ref().unwrap();
let ui_url = ctx.ui.as_ref().unwrap().url.clone();
let chat_url = format!("{}/#chat", ui_url);
2025-12-06 11:05:57 -03:00
println!("🌐 Navigating to: {chat_url}");
2025-12-06 11:05:57 -03:00
if let Err(e) = browser.goto(&chat_url).await {
2025-12-06 11:05:57 -03:00
ctx.close().await;
bail!("Failed to navigate to chat: {e}");
2025-12-06 11:05:57 -03:00
}
println!("⏳ Waiting for page to load...");
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
2025-12-06 11:05:57 -03:00
let input = Locator::css("#messageInput, #ai-input, .ai-input");
2025-12-06 11:05:57 -03:00
let mut found_input = false;
for attempt in 1..=10 {
if browser.exists(input.clone()).await {
found_input = true;
println!("✓ Chat input found (attempt {attempt})");
break;
2025-12-06 11:05:57 -03:00
}
println!(" ... waiting for chat input (attempt {attempt}/10)");
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
2025-12-06 11:05:57 -03:00
}
if !found_input {
if let Ok(screenshot) = browser.screenshot().await {
let _ = std::fs::write("/tmp/bottest-chat-fail.png", &screenshot);
println!("Screenshot saved to /tmp/bottest-chat-fail.png");
2025-12-06 11:05:57 -03:00
}
if let Ok(source) = browser.page_source().await {
let preview: String = source.chars().take(2000).collect();
println!("Page source preview:\n{preview}");
2025-12-06 11:05:57 -03:00
}
ctx.close().await;
bail!("Chat input not found after 10 attempts");
2025-12-06 11:05:57 -03:00
}
println!("⌨️ Typing 'hi'...");
if let Err(e) = browser.type_text(input.clone(), "hi").await {
2025-12-06 11:05:57 -03:00
ctx.close().await;
bail!("Failed to type: {e}");
2025-12-06 11:05:57 -03:00
}
let send_btn = Locator::css("#sendBtn, #ai-send, .ai-send, button[type='submit']");
match browser.click(send_btn).await {
Ok(()) => println!("✓ Message sent (click)"),
Err(_) => match browser.press_key(input, "Enter").await {
Ok(()) => println!("✓ Message sent (Enter key)"),
Err(e) => println!("⚠ Send may have failed: {e}"),
},
2025-12-06 11:05:57 -03:00
}
println!("⏳ Waiting for bot response...");
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
2025-12-06 11:05:57 -03:00
let response =
Locator::css(".message.bot, .message.assistant, .bot-message, .assistant-message");
match browser.find_elements(response).await {
Ok(elements) if !elements.is_empty() => {
println!("✓ Bot responded! ({} messages)", elements.len());
2025-12-06 11:05:57 -03:00
}
_ => {
println!("⚠ No bot response detected (may need LLM configuration)");
2025-12-06 11:05:57 -03:00
}
}
if let Ok(screenshot) = browser.screenshot().await {
let _ = std::fs::write("/tmp/bottest-chat-result.png", &screenshot);
println!("📸 Screenshot: /tmp/bottest-chat-result.png");
2025-12-06 11:05:57 -03:00
}
ctx.close().await;
println!("✅ Chat test complete!");
Ok(())
2025-12-06 11:05:57 -03:00
}
#[tokio::test]
async fn test_chat_page_loads() -> Result<()> {
2025-12-06 11:05:57 -03:00
if !should_run_e2e_tests() {
return Ok(());
2025-12-06 11:05:57 -03:00
}
let ctx = E2ETestContext::setup_with_browser().await?;
2025-12-06 11:05:57 -03:00
if !ctx.has_browser() {
ctx.close().await;
bail!("Browser not available");
2025-12-06 11:05:57 -03:00
}
if ctx.ui.is_none() {
2025-12-06 11:05:57 -03:00
ctx.close().await;
bail!("BotUI not available - chat tests require botui. Start it with: cd ../botui && cargo run");
2025-12-06 11:05:57 -03:00
}
let browser = ctx.browser.as_ref().unwrap();
let ui_url = ctx.ui.as_ref().unwrap().url.clone();
let chat_url = format!("{}/#chat", ui_url);
2025-12-06 11:05:57 -03:00
if let Err(e) = browser.goto(&chat_url).await {
2025-12-06 11:05:57 -03:00
ctx.close().await;
bail!("Navigation failed: {e}");
2025-12-06 11:05:57 -03:00
}
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
2025-12-06 11:05:57 -03:00
let input = Locator::css("#messageInput, input[type='text'], textarea");
match browser.wait_for(input).await {
Ok(_) => println!("✓ Chat loaded"),
2025-12-06 11:05:57 -03:00
Err(e) => {
if let Ok(s) = browser.screenshot().await {
let _ = std::fs::write("/tmp/bottest-fail.png", &s);
2025-12-06 11:05:57 -03:00
}
ctx.close().await;
bail!("Chat not loaded: {e}");
2025-12-06 11:05:57 -03:00
}
}
ctx.close().await;
Ok(())
2025-12-06 11:05:57 -03:00
}