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