#![cfg(windows)]
use std::time::Duration;
use rust_expect::backend::{BackendType, PtyConfig};
use rust_expect::prelude::*;
use rust_expect::{Dialog, DialogStep};
#[test]
fn windows_line_endings() {
use rust_expect::encoding::{LineEndingStyle, detect_line_ending};
let crlf_text = "line1\r\nline2\r\nline3";
assert_eq!(detect_line_ending(crlf_text), Some(LineEndingStyle::CrLf));
let mixed_text = "line1\r\nline2\nline3";
assert_eq!(detect_line_ending(mixed_text), Some(LineEndingStyle::CrLf));
}
#[test]
fn crlf_normalization() {
use rust_expect::encoding::{LineEndingStyle, normalize_line_endings};
let unix_text = "line1\nline2\nline3";
let windows_text = normalize_line_endings(unix_text, LineEndingStyle::CrLf);
assert_eq!(windows_text, "line1\r\nline2\r\nline3");
let crlf_text = "line1\r\nline2\r\nline3";
let normalized = normalize_line_endings(crlf_text, LineEndingStyle::CrLf);
assert_eq!(normalized, crlf_text);
}
#[test]
fn session_builder_windows() {
let builder = SessionBuilder::new()
.command("cmd.exe")
.arg("/c")
.arg("echo hello")
.timeout(Duration::from_secs(30))
.env("PROMPT", "$P$G");
let config = builder.build();
assert_eq!(config.command, "cmd.exe");
assert!(config.args.contains(&"/c".to_string()));
}
#[test]
fn windows_control_chars() {
use rust_expect::ControlChar;
let ctrl_c = ControlChar::CtrlC;
assert_eq!(ctrl_c.as_byte(), 0x03);
let ctrl_d = ControlChar::CtrlD;
assert_eq!(ctrl_d.as_byte(), 0x04);
let ctrl_z = ControlChar::CtrlZ;
assert_eq!(ctrl_z.as_byte(), 0x1A);
}
#[test]
fn windows_env_vars() {
let builder = SessionBuilder::new()
.command("cmd.exe")
.env("COMPUTERNAME", "TESTPC")
.env("USERNAME", "testuser")
.env("USERPROFILE", "C:\\Users\\testuser");
let config = builder.build();
assert_eq!(config.env.get("COMPUTERNAME"), Some(&"TESTPC".to_string()));
assert_eq!(config.env.get("USERNAME"), Some(&"testuser".to_string()));
}
#[test]
fn pattern_windows_paths() {
let pattern = Pattern::literal("C:\\Users\\");
let text = "Current directory: C:\\Users\\testuser";
assert!(pattern.matches(text).is_some());
let unc_pattern = Pattern::literal("\\\\server\\share");
let unc_text = "Mapped drive: \\\\server\\share\\folder";
assert!(unc_pattern.matches(unc_text).is_some());
}
#[test]
fn regex_windows_patterns() {
let drive_pattern = Pattern::regex(r"[A-Z]:\\").unwrap();
assert!(drive_pattern.matches("C:\\Windows\\System32").is_some());
assert!(drive_pattern.matches("D:\\Data\\Files").is_some());
let process_pattern = Pattern::regex(r"\w+\.exe").unwrap();
assert!(process_pattern.matches("Running: notepad.exe").is_some());
}
#[test]
fn dialog_windows_prompts() {
let dialog = Dialog::named("windows_cmd")
.step(
DialogStep::new("prompt")
.with_expect(">")
.with_send("dir\r\n"),
)
.step(DialogStep::new("output").with_expect("Directory of"));
assert_eq!(dialog.len(), 2);
assert_eq!(dialog.name, "windows_cmd");
}
#[test]
fn quick_session_windows() {
let cmd_config = QuickSession::cmd();
assert_eq!(cmd_config.command, "cmd.exe");
assert_eq!(cmd_config.line_ending, LineEnding::CrLf);
let ps_config = QuickSession::powershell();
assert_eq!(ps_config.command, "powershell.exe");
assert!(ps_config.args.contains(&"-NoLogo".to_string()));
}
#[test]
fn pty_backend_windows() {
assert!(BackendType::Pty.is_available());
assert_eq!(BackendType::Pty.name(), "pty");
}
#[test]
fn pty_config_windows() {
let config = PtyConfig::default();
assert_eq!(config.dimensions.1, 24); assert_eq!(config.dimensions.0, 80); }
#[test]
fn windows_session_timeout() {
let config = SessionBuilder::new()
.command("cmd.exe")
.timeout(Duration::from_secs(60))
.build();
assert_eq!(config.timeout.default, Duration::from_secs(60));
}
#[test]
fn windows_script_patterns() {
let batch_pattern = Pattern::regex(r"\.bat|\.cmd").unwrap();
assert!(batch_pattern.matches("script.bat").is_some());
assert!(batch_pattern.matches("install.cmd").is_some());
let ps_pattern = Pattern::regex(r"\.ps1|\.psm1").unwrap();
assert!(ps_pattern.matches("setup.ps1").is_some());
assert!(ps_pattern.matches("module.psm1").is_some());
}
#[test]
fn windows_error_patterns() {
let error_pattern = Pattern::regex(r"'[^']+' is not recognized").unwrap();
assert!(
error_pattern
.matches("'foo' is not recognized as an internal or external command")
.is_some()
);
let access_denied = Pattern::literal("Access is denied");
assert!(access_denied.matches("Error: Access is denied.").is_some());
}
#[test]
fn windows_prompt_patterns() {
let cmd_prompt = Pattern::regex(r"[A-Z]:\\[^>]*>").unwrap();
assert!(cmd_prompt.matches("C:\\Users\\test>").is_some());
assert!(cmd_prompt.matches("D:\\Projects\\app>").is_some());
let ps_prompt = Pattern::regex(r"PS [A-Z]:\\[^>]+> ").unwrap();
assert!(ps_prompt.matches("PS C:\\Users\\test> ").is_some());
}
#[test]
fn windows_registry_patterns() {
let hklm_pattern = Pattern::literal("HKEY_LOCAL_MACHINE");
assert!(
hklm_pattern
.matches("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft")
.is_some()
);
let reg_pattern = Pattern::regex(r"HKEY_(LOCAL_MACHINE|CURRENT_USER|CLASSES_ROOT)\\").unwrap();
assert!(
reg_pattern
.matches("HKEY_LOCAL_MACHINE\\SOFTWARE")
.is_some()
);
assert!(reg_pattern.matches("HKEY_CURRENT_USER\\Software").is_some());
}
#[test]
fn quick_session_python_windows() {
let config = QuickSession::python();
assert_eq!(config.command, "python");
assert!(config.args.contains(&"-i".to_string()));
}
#[test]
fn windows_default_shell() {
let shell = QuickSession::default_shell();
if std::env::var("SHELL").is_err() {
assert_eq!(shell, "cmd.exe");
}
}
#[test]
fn dialog_windows_control() {
use rust_expect::ControlChar;
let dialog = Dialog::named("windows_interrupt")
.step(
DialogStep::new("running")
.with_expect("Running...")
.with_send_control(ControlChar::CtrlC),
)
.step(DialogStep::new("stopped").with_expect("Terminated"));
assert_eq!(dialog.len(), 2);
}
#[test]
fn windows_timeout_config() {
use rust_expect::util::TimeoutConfig;
let config = TimeoutConfig::uniform(Duration::from_secs(30));
assert_eq!(config.expect, Duration::from_secs(30));
assert_eq!(config.read, Duration::from_secs(30));
assert_eq!(config.write, Duration::from_secs(30));
assert_eq!(config.connect, Duration::from_secs(30));
assert_eq!(config.close, Duration::from_secs(30));
}