#![cfg(feature = "integration")]
mod common;
use std::path::PathBuf;
use tempfile::TempDir;
use viewpoint_core::context::TracingOptions;
fn temp_trace_dir() -> TempDir {
tempfile::tempdir().expect("Failed to create temp directory")
}
fn temp_trace_path(dir: &TempDir, name: &str) -> PathBuf {
dir.path().join(name)
}
#[tokio::test]
async fn test_tracing_start_stop() {
common::init_tracing();
let (browser, context, page) = common::launch_with_page().await;
let temp_dir = temp_trace_dir();
let trace_path = temp_trace_path(&temp_dir, "trace.zip");
context
.tracing()
.start(TracingOptions::new().name("test-trace"))
.await
.expect("Failed to start tracing");
page.goto("about:blank")
.goto()
.await
.expect("Failed to navigate");
context
.tracing()
.stop(&trace_path)
.await
.expect("Failed to stop tracing");
assert!(trace_path.exists(), "Trace file should exist");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_start_stop_discard() {
common::init_tracing();
let (browser, context, page) = common::launch_with_page().await;
context
.tracing()
.start(TracingOptions::new())
.await
.expect("Failed to start tracing");
page.goto("about:blank")
.goto()
.await
.expect("Failed to navigate");
context
.tracing()
.stop_discard()
.await
.expect("Failed to discard tracing");
assert!(
!context.tracing().is_recording().await,
"Should not be recording after discard"
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_chunks() {
common::init_tracing();
let (browser, context, page) = common::launch_with_page().await;
let temp_dir = temp_trace_dir();
let chunk_path = temp_trace_path(&temp_dir, "chunk.zip");
context
.tracing()
.start(TracingOptions::new())
.await
.expect("Failed to start tracing");
page.goto("about:blank")
.goto()
.await
.expect("Failed to navigate");
context
.tracing()
.start_chunk()
.await
.expect("Failed to start chunk");
context
.tracing()
.stop_chunk(&chunk_path)
.await
.expect("Failed to stop chunk");
assert!(chunk_path.exists(), "Chunk file should exist");
context
.tracing()
.stop_discard()
.await
.expect("Failed to discard tracing");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_is_recording() {
common::init_tracing();
let (browser, context, _page) = common::launch_with_page().await;
assert!(
!context.tracing().is_recording().await,
"Should not be recording initially"
);
context
.tracing()
.start(TracingOptions::new())
.await
.expect("Failed to start tracing");
assert!(
context.tracing().is_recording().await,
"Should be recording after start"
);
context
.tracing()
.stop_discard()
.await
.expect("Failed to stop tracing");
assert!(
!context.tracing().is_recording().await,
"Should not be recording after stop"
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_start_without_pages_fails() {
common::init_tracing();
let browser = common::launch_browser().await;
let context = browser
.new_context()
.await
.expect("Failed to create context");
let result = context.tracing().start(TracingOptions::new()).await;
assert!(result.is_err(), "Should fail without pages");
let err = result.unwrap_err();
let err_msg = err.to_string();
assert!(
err_msg.contains("no pages in context"),
"Error message should mention no pages: {err_msg}"
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_stop_without_start_fails() {
common::init_tracing();
let (browser, context, _page) = common::launch_with_page().await;
let temp_dir = temp_trace_dir();
let trace_path = temp_trace_path(&temp_dir, "trace.zip");
let result = context.tracing().stop(&trace_path).await;
assert!(result.is_err(), "Should fail without starting");
let err = result.unwrap_err();
let err_msg = err.to_string();
assert!(
err_msg.contains("not active"),
"Error message should mention tracing not active: {err_msg}"
);
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_double_start_fails() {
common::init_tracing();
let (browser, context, _page) = common::launch_with_page().await;
context
.tracing()
.start(TracingOptions::new())
.await
.expect("Failed to start tracing");
let result = context.tracing().start(TracingOptions::new()).await;
assert!(result.is_err(), "Double start should fail");
let err = result.unwrap_err();
let err_msg = err.to_string();
assert!(
err_msg.contains("already active"),
"Error message should mention already active: {err_msg}"
);
context
.tracing()
.stop_discard()
.await
.expect("Failed to discard tracing");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_with_screenshots() {
common::init_tracing();
let (browser, context, page) = common::launch_with_page().await;
let temp_dir = temp_trace_dir();
let trace_path = temp_trace_path(&temp_dir, "trace-screenshots.zip");
context
.tracing()
.start(
TracingOptions::new()
.name("screenshot-test")
.screenshots(true)
.snapshots(true),
)
.await
.expect("Failed to start tracing with screenshots");
page.goto("about:blank")
.goto()
.await
.expect("Failed to navigate");
context
.tracing()
.stop(&trace_path)
.await
.expect("Failed to stop tracing");
assert!(trace_path.exists(), "Trace file should exist");
browser.close().await.expect("Failed to close browser");
}
#[tokio::test]
async fn test_tracing_creates_valid_zip() {
common::init_tracing();
let (browser, context, page) = common::launch_with_page().await;
let temp_dir = temp_trace_dir();
let trace_path = temp_trace_path(&temp_dir, "trace-valid.zip");
context
.tracing()
.start(TracingOptions::new().name("zip-test"))
.await
.expect("Failed to start tracing");
page.goto("about:blank")
.goto()
.await
.expect("Failed to navigate");
context
.tracing()
.stop(&trace_path)
.await
.expect("Failed to stop tracing");
let file = std::fs::File::open(&trace_path).expect("Failed to open trace file");
let archive = zip::ZipArchive::new(file);
assert!(archive.is_ok(), "Trace should be a valid zip file");
let mut archive = archive.unwrap();
let file_names: Vec<String> = (0..archive.len())
.map(|i| archive.by_index(i).unwrap().name().to_string())
.collect();
assert!(
file_names.iter().any(|n| n.contains("trace")),
"Zip should contain trace data: {:?}",
file_names
);
browser.close().await.expect("Failed to close browser");
}