//! Unattended end-to-end self-test hook. Inert unless `VOICED_SELFTEST_OUT` is set: the webview asks //! for the report path at startup, runs its checks and hands the JSON back, and the process exits with //! the verdict. This is the release gate that proves the real webview works, which Node tests cannot. use std::io::Write; use std::path::{Path, PathBuf}; const ENV_VAR: &str = "VOICED_SELFTEST_OUT"; fn configured_path() -> Option { std::env::var_os(ENV_VAR) .filter(|v| !v.is_empty()) .map(PathBuf::from) } fn write_report(path: &Path, json: &str) -> Result<(), String> { if let Some(parent) = path.parent() { std::fs::create_dir_all(parent) .map_err(|e| format!("Could not create {}: {e}", parent.display()))?; } let mut file = std::fs::File::create(path).map_err(|e| format!("Could not write the report: {e}"))?; file.write_all(json.as_bytes()) .and_then(|()| file.sync_all()) .map_err(|e| format!("Could not write the report: {e}")) } #[tauri::command] pub fn selftest_config() -> Option { configured_path().map(|p| p.to_string_lossy().into_owned()) } #[tauri::command] pub fn selftest_report(json: String, ok: bool) -> Result<(), String> { let path = configured_path().ok_or_else(|| "The self-test is not enabled".to_string())?; write_report(&path, &json)?; std::process::exit(if ok { 0 } else { 1 }); } #[cfg(test)] mod tests { use super::*; #[test] fn writes_the_report_creating_folders() { let dir = tempfile::tempdir().unwrap(); let path = dir.path().join("a/b/report.json"); write_report(&path, "{\"ok\":true}").unwrap(); assert_eq!(std::fs::read_to_string(&path).unwrap(), "{\"ok\":true}"); } }