//! Functional test of the generated `phoebus-session-mango` script. //! //! Runs the real script against stub `mango`/`dms` binaries so the startup //! ordering, the Wayland-socket discovery, DMS launch, and helper cleanup are //! exercised without a physical seat or a Wayland compositor. use std::path::Path; use std::process::{Child, Command, Stdio}; use std::time::{Duration, Instant}; use phoebus_core::system::PHOEBUS_MANGO_SESSION_SCRIPT; fn write_exec(path: &Path, body: &str) { std::fs::write(path, body).unwrap(); use std::os::unix::fs::PermissionsExt; std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap(); } /// mango stub: creates a real unix socket at $XDG_RUNTIME_DIR/wayland-0 and exits /// after a moment so the session ends. const MANGO_OK: &str = r#"#!/bin/sh echo mango-started >> "$SESSION_TEST_DIR/mango.log" exec python3 -c 'import os,socket,time; s=socket.socket(socket.AF_UNIX); s.bind(os.path.join(os.environ["XDG_RUNTIME_DIR"],"wayland-0")); s.listen(1); time.sleep(3)' "#; /// mango stub that fails before creating the socket. const MANGO_FAIL: &str = "#!/bin/sh\nexit 1\n"; /// DMS stub: records whether the Wayland socket existed when it started, /// stores its pid, then sleeps so cleanup has something to kill. const DMS_STUB: &str = r#"#!/bin/sh if [ -S "$XDG_RUNTIME_DIR/wayland-0" ]; then s=ok; else s=missing; fi echo "dms $s wayland_display=${WAYLAND_DISPLAY:-missing}" >> "$SESSION_TEST_DIR/helpers.log" echo $$ >> "$SESSION_TEST_DIR/helper-pids" exec sleep 30 "#; fn wait_timeout(child: &mut Child, secs: u64) -> std::process::ExitStatus { let deadline = Instant::now() + Duration::from_secs(secs); loop { if let Some(status) = child.try_wait().unwrap() { return status; } if Instant::now() >= deadline { let _ = child.kill(); panic!("session script did not exit within {secs}s"); } std::thread::sleep(Duration::from_millis(100)); } } struct Fixture { dir: tempfile::TempDir, script: std::path::PathBuf, bin: std::path::PathBuf, runtime: std::path::PathBuf, } impl Fixture { fn new(mango_body: &str) -> Self { let dir = tempfile::tempdir().unwrap(); let bin = dir.path().join("bin"); let runtime = dir.path().join("runtime"); std::fs::create_dir_all(&bin).unwrap(); std::fs::create_dir_all(&runtime).unwrap(); let script = dir.path().join("phoebus-session-mango"); write_exec(&script, PHOEBUS_MANGO_SESSION_SCRIPT); write_exec(&bin.join("mango"), mango_body); write_exec(&bin.join("dms"), DMS_STUB); Self { dir, script, bin, runtime, } } fn run(&self) -> (std::process::ExitStatus, String) { let mut child = Command::new("bash") .arg(&self.script) .env_clear() .env("PATH", format!("{}:/usr/bin:/bin", self.bin.display())) .env("XDG_RUNTIME_DIR", &self.runtime) // Skip the dbus-run-session re-exec; the session bus is not what // this test exercises. .env("DBUS_SESSION_BUS_ADDRESS", "unix:path=/run/test-bus") .env("SESSION_TEST_DIR", self.dir.path()) .env("HOME", self.dir.path()) .env("USER", "phoebus") .stdout(Stdio::null()) .stderr(Stdio::piped()) .spawn() .unwrap(); let status = wait_timeout(&mut child, 20); let mut stderr = String::new(); use std::io::Read; if let Some(mut e) = child.stderr.take() { let _ = e.read_to_string(&mut stderr); } (status, stderr) } fn read(&self, name: &str) -> String { std::fs::read_to_string(self.dir.path().join(name)).unwrap_or_default() } } #[test] fn helpers_wait_for_the_socket_and_are_reaped() { let fixture = Fixture::new(MANGO_OK); let (status, stderr) = fixture.run(); assert!(status.success(), "session failed: {stderr}"); // Mango started and DMS observed the socket and exported WAYLAND_DISPLAY. let mango_log = fixture.read("mango.log"); assert!(mango_log.contains("mango-started")); let log = fixture.read("helpers.log"); assert!(log.contains("dms ok"), "helper log: {log:?}"); assert!( log.contains("wayland_display=wayland-0"), "helper log: {log:?}" ); assert!( !log.contains("missing"), "a helper raced the socket: {log:?}" ); // The helpers were killed when mango exited (logout leaves nothing behind). let pids = fixture.read("helper-pids"); assert!(!pids.trim().is_empty(), "no helper pids recorded"); std::thread::sleep(Duration::from_millis(300)); for pid in pids.lines() { assert!( !Path::new(&format!("/proc/{}", pid.trim())).exists(), "helper {pid} survived logout" ); } } #[test] fn failure_to_create_the_socket_is_reported() { let fixture = Fixture::new(MANGO_FAIL); let (status, stderr) = fixture.run(); assert!(!status.success(), "a dead compositor must fail the session"); assert!( stderr.contains("mango exited before creating a Wayland socket"), "{stderr}" ); assert!(fixture.read("helpers.log").is_empty()); } #[test] fn first_login_seeds_wallpaper_folder_and_letter_avatar_without_replacing_choices() { let fixture = Fixture::new(MANGO_OK); let wallpapers = fixture.dir.path().join("Pictures/Wallpapers"); std::fs::create_dir_all(&wallpapers).unwrap(); std::fs::write(wallpapers.join("aurora-lagoon.jpg"), b"sample image").unwrap(); let (status, stderr) = fixture.run(); assert!(status.success(), "session failed: {stderr}"); let state = fixture .dir .path() .join(".local/state/DankMaterialShell/session.json"); let cache = fixture .dir .path() .join(".cache/DankMaterialShell/cache.json"); let avatar = fixture.dir.path().join(".face"); let settings = fixture .dir .path() .join(".config/DankMaterialShell/settings.json"); let state_json: serde_json::Value = serde_json::from_slice(&std::fs::read(&state).unwrap()).unwrap(); let cache_json: serde_json::Value = serde_json::from_slice(&std::fs::read(&cache).unwrap()).unwrap(); let settings_json: serde_json::Value = serde_json::from_slice(&std::fs::read(&settings).unwrap()).unwrap(); assert_eq!(settings_json["currentThemeName"], "dynamic"); assert_eq!(settings_json["currentThemeCategory"], "dynamic"); assert_eq!( state_json["wallpaperPath"], wallpapers .join("aurora-lagoon.jpg") .to_string_lossy() .as_ref() ); assert_eq!( cache_json["wallpaperLastPath"], wallpapers.to_string_lossy().as_ref() ); assert_eq!( cache_json["fileBrowserSettings"]["wallpaper"]["lastPath"], wallpapers.to_string_lossy().as_ref() ); assert!(std::fs::read_to_string(&avatar) .unwrap() .contains(">P")); std::fs::write( &state, "{\"configVersion\":4,\"wallpaperPath\":\"/chosen/image.jpg\"}", ) .unwrap(); std::fs::write(&cache, "{\"configVersion\":3,\"wallpaperLastPath\":\"/chosen\",\"fileBrowserSettings\":{\"wallpaper\":{\"lastPath\":\"/chosen\"}}}").unwrap(); std::fs::write(&avatar, b"custom avatar").unwrap(); std::fs::write( &settings, r#"{"configVersion":18,"currentThemeName":"purple","currentThemeCategory":"generic"}"#, ) .unwrap(); std::fs::remove_file(fixture.runtime.join("wayland-0")).unwrap(); let (status, stderr) = fixture.run(); assert!(status.success(), "second session failed: {stderr}"); assert!(fixture .read(".local/state/DankMaterialShell/session.json") .contains("/chosen/image.jpg")); assert!(fixture .read(".cache/DankMaterialShell/cache.json") .contains("/chosen")); assert_eq!(std::fs::read(&avatar).unwrap(), b"custom avatar"); assert!(fixture .read(".config/DankMaterialShell/settings.json") .contains("purple")); } #[test] fn missing_theme_choice_is_filled_without_losing_other_dms_settings() { let fixture = Fixture::new(MANGO_OK); let wallpapers = fixture.dir.path().join("Pictures/Wallpapers"); std::fs::create_dir_all(&wallpapers).unwrap(); std::fs::write(wallpapers.join("aurora-lagoon.jpg"), b"sample image").unwrap(); let settings = fixture .dir .path() .join(".config/DankMaterialShell/settings.json"); std::fs::create_dir_all(settings.parent().unwrap()).unwrap(); std::fs::write(&settings, r#"{"configVersion":18,"fontSize":15}"#).unwrap(); let (status, stderr) = fixture.run(); assert!(status.success(), "session failed: {stderr}"); let saved: serde_json::Value = serde_json::from_slice(&std::fs::read(settings).unwrap()).unwrap(); assert_eq!(saved["currentThemeName"], "dynamic"); assert_eq!(saved["currentThemeCategory"], "dynamic"); assert_eq!(saved["fontSize"], 15); }