victauri_test/lib.rs
1#![deny(missing_docs)]
2//! Integration testing for Tauri apps via the Victauri MCP server.
3//!
4//! Provides [`TestApp`] for managed app lifecycle and [`VictauriClient`] with
5//! Playwright-style interactions (`click_by_text`, `fill_by_id`, `expect_text`)
6//! plus assertion helpers for accessibility, performance, and state verification.
7//!
8//! # Quick Start
9//!
10//! ```rust,ignore
11//! use victauri_test::TestApp;
12//!
13//! #[tokio::test]
14//! async fn greet_flow() {
15//! let app = TestApp::spawn("cargo run -p my-app").await.unwrap();
16//! let mut client = app.client().await.unwrap();
17//!
18//! client.fill_by_id("name-input", "World").await.unwrap();
19//! client.click_by_id("greet-btn").await.unwrap();
20//! client.expect_text("Hello, World!").await.unwrap();
21//! }
22//! ```
23//!
24//! # Without `TestApp` (connect to existing server)
25//!
26//! ```rust,ignore
27//! use victauri_test::VictauriClient;
28//!
29//! #[tokio::test]
30//! async fn check_health() {
31//! let mut client = VictauriClient::discover().await.unwrap();
32//! let audit = client.audit_accessibility().await.unwrap();
33//! assert_eq!(audit["summary"]["violations"], 0);
34//! }
35//! ```
36
37mod app;
38mod assertions;
39mod client;
40pub mod coverage;
41mod error;
42pub mod prelude;
43pub mod reporting;
44pub mod smoke;
45pub mod visual;
46
47pub use app::TestApp;
48pub use assertions::{
49 CheckResult, VerifyBuilder, VerifyReport, assert_ipc_called, assert_ipc_called_with,
50 assert_ipc_not_called,
51};
52pub use client::{
53 VictauriClient, assert_ipc_healthy, assert_json_eq, assert_json_truthy,
54 assert_no_a11y_violations, assert_performance_budget, assert_state_matches,
55};
56pub use error::TestError;
57pub use smoke::{SmokeCheckResult, SmokeConfig, SmokeReport};
58
59/// Returns `true` if E2E tests should run (i.e., `VICTAURI_E2E` env var is set).
60///
61/// Use this to gate integration tests that require a running Tauri app:
62///
63/// ```rust,ignore
64/// #[tokio::test]
65/// async fn my_test() {
66/// if !victauri_test::is_e2e() { return; }
67/// // ...
68/// }
69/// ```
70#[must_use]
71pub fn is_e2e() -> bool {
72 std::env::var("VICTAURI_E2E").is_ok()
73}
74
75/// Connect to a Victauri server using standard env var configuration.
76///
77/// Reads `VICTAURI_PORT` (default 7373) and `VICTAURI_AUTH_TOKEN` (optional).
78/// This is a shorthand for `VictauriClient::discover()`.
79///
80/// # Errors
81///
82/// Returns [`TestError::Connection`] if the server is unreachable.
83pub async fn connect() -> Result<VictauriClient, TestError> {
84 VictauriClient::discover().await
85}
86
87/// Declare an E2E test that auto-skips when `VICTAURI_E2E` is not set
88/// and auto-connects to the running server.
89///
90/// # Example
91///
92/// ```rust,ignore
93/// use victauri_test::{e2e_test, VictauriClient};
94///
95/// e2e_test!(greet_flow, |client: &mut VictauriClient| async move {
96/// client.fill_by_id("name-input", "World").await.unwrap();
97/// client.click_by_id("greet-btn").await.unwrap();
98/// client.expect_text("Hello, World!").await.unwrap();
99/// });
100/// ```
101#[macro_export]
102macro_rules! e2e_test {
103 ($name:ident, |$client:ident : &mut VictauriClient| async move $body:block) => {
104 #[tokio::test]
105 async fn $name() {
106 if !$crate::is_e2e() {
107 return;
108 }
109 let mut $client = $crate::connect().await.unwrap();
110 $body
111 }
112 };
113 ($name:ident, |$client:ident| async move $body:block) => {
114 #[tokio::test]
115 async fn $name() {
116 if !$crate::is_e2e() {
117 return;
118 }
119 let mut $client = $crate::connect().await.unwrap();
120 $body
121 }
122 };
123}