// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Built on Domi, https://aula.marek-novak.cz //! Game detection for the overlay: the three rules, Windows path matching, and the persisted marks. use arena_core::detect::{Detection, How, Marks, detect}; use arena_core::{Game, GameId, Launch, ParseError, Source, WinPath, limits, manual}; use proptest::prelude::*; fn p(s: &str) -> WinPath { WinPath::parse(s).unwrap() } fn steam(app: u32, title: &str, dir: &str) -> Game { let id = GameId::new(Source::Steam, &app.to_string()).unwrap(); Game { id, source: Source::Steam, title: title.into(), install_dir: Some(p(dir)), launch: Launch::steam(app).unwrap(), cover: None, } } fn domi(app: &str) -> Game { Game { id: GameId::new(Source::Domi, app).unwrap(), source: Source::Domi, title: app.into(), install_dir: None, launch: Launch::domi_link(app).unwrap(), cover: None, } } fn library() -> Vec { vec![ steam(10, "Foo", r"C:\Games\Foo"), steam(20, "FooBar", r"C:\Games\FooBar"), steam(30, "Nested", r"C:\Games\Foo\Expansion"), domi("lapilli"), ] } fn id(s: &str) -> Option { Some(s.parse().unwrap()) } fn found(game: &str, how: How) -> Option { Some(Detection { game: id(game), how }) } const NONE: &Marks = &Marks::new(); // --- an exe outside every install_dir is never auto-detected --- #[test] fn outside_every_install_dir_is_not_detected() { let lib = library(); for exe in [ r"C:\Windows\System32\notepad.exe", r"C:\Games\foo.exe", r"C:\Games\Fo\game.exe", r"C:\Games\FooBarBaz\game.exe", r"C:\Games\Foo Bar\game.exe", r"D:\Games\Foo\game.exe", r"C:\Program Files\Steam\steam.exe", ] { assert_eq!(detect(&p(exe), false, &lib, NONE), None, "{exe}"); } assert_eq!(detect(&p(r"C:\x.exe"), false, &[], NONE), None); } #[test] fn inside_an_install_dir_is_detected() { let lib = library(); assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &lib, NONE), found("steam:10", How::InstallDir)); assert_eq!(detect(&p(r"C:\Games\Foo\bin\x64\foo.exe"), false, &lib, NONE), found("steam:10", How::InstallDir)); assert_eq!(detect(&p(r"C:\Games\FooBar\fb.exe"), false, &lib, NONE), found("steam:20", How::InstallDir)); } #[test] fn prefix_of_a_segment_never_matches() { // C:\Games\Foo must not claim C:\Games\FooBar's exes, in either direction, whatever the order. let only_foo = vec![steam(10, "Foo", r"C:\Games\Foo")]; assert_eq!(detect(&p(r"C:\Games\FooBar\fb.exe"), false, &only_foo, NONE), None); let only_foobar = vec![steam(20, "FooBar", r"C:\Games\FooBar")]; assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &only_foobar, NONE), None); assert_eq!(detect(&p(r"C:\Games\Foo.exe"), false, &only_foo, NONE), None); } #[test] fn the_deepest_install_dir_wins() { let lib = library(); let exe = p(r"C:\Games\Foo\Expansion\exp.exe"); assert_eq!(detect(&exe, false, &lib, NONE), found("steam:30", How::InstallDir)); let mut rev = lib.clone(); rev.reverse(); assert_eq!(detect(&exe, false, &rev, NONE), found("steam:30", How::InstallDir)); } #[test] fn the_install_dir_itself_is_not_an_exe_inside_it() { let lib = library(); assert_eq!(detect(&p(r"C:\Games\FooBar"), false, &lib, NONE), None); } #[test] fn a_drive_root_install_dir_does_not_claim_the_whole_drive() { // A manual game picked at D:\game.exe has the install folder D:\ — that must not make every exe on D: a game. let g = manual::game(r"D:\game.exe", Some("Root game")).unwrap(); assert_eq!(g.install_dir, Some(p(r"D:\"))); let lib = vec![g.clone()]; assert_eq!(detect(&p(r"D:\Tools\editor.exe"), false, &lib, NONE), None); assert_eq!(detect(&p(r"D:\other.exe"), false, &lib, NONE), None); // Its own exe is still found, through the game's launch exe. assert_eq!( detect(&p(r"d:\GAME.EXE"), false, &lib, NONE), Some(Detection { game: Some(g.id), how: How::InstallDir }) ); } #[test] fn a_game_s_own_launch_exe_is_detected() { let g = manual::game(r"E:\Old\Thing\thing.exe", None).unwrap(); let lib = vec![g.clone()]; assert_eq!(detect(&p(r"E:\Old\Thing\thing.exe"), false, &lib, NONE).map(|d| d.how), Some(How::InstallDir)); assert_eq!(detect(&p(r"E:\Old\Thing\helper.exe"), false, &lib, NONE).and_then(|d| d.game), Some(g.id)); } // --- Windows path rules --- #[test] fn matching_ignores_case_and_separator_style() { let lib = library(); for exe in [r"c:\games\foo\foo.exe", r"C:\GAMES\FOO\FOO.EXE", "C:/Games/foo/Bin/foo.exe", r"c:\\Games\\\Foo\\x.exe"] { assert_eq!(detect(&p(exe), false, &lib, NONE), found("steam:10", How::InstallDir), "{exe}"); } let upper = vec![steam(10, "Foo", r"C:\GAMES\FOO")]; assert_eq!(detect(&p(r"c:\games\foo\a.exe"), false, &upper, NONE), found("steam:10", How::InstallDir)); } #[test] fn non_ascii_case_folds_too() { let lib = vec![steam(40, "Hra", r"C:\Hry\Žluťoučký Kůň")]; assert_eq!(detect(&p(r"C:\HRY\ŽLUŤOUČKÝ KŮŇ\kun.exe"), false, &lib, NONE), found("steam:40", How::InstallDir)); } #[test] fn dot_dot_never_escapes() { // A path with `..` cannot even be built, so `C:\Games\Foo\..\Other\x.exe` can never pass as inside Foo. for s in [ r"C:\Games\Foo\..\Other\x.exe", r"C:\Games\Foo\..\..\Windows\x.exe", r"C:\Games\Foo\.\x.exe", r"C:\Games\Foo.\x.exe", r"C:\Games\Foo \x.exe", r"\\?\C:\Games\Foo\x.exe", r"\\server\share\Games\Foo\x.exe", ] { assert!(WinPath::parse(s).is_err(), "{s}"); } // Nor can a mark or a game folder carry one in through JSON. assert!(serde_json::from_str::(r#"["C:\\Games\\Foo\\..\\x.exe"]"#).is_err()); // An exe in a sibling folder is not inside, even if a join tried to climb. assert!(p(r"C:\Games\Foo").join(r"..\FooBar\fb.exe").is_err()); } // --- launched by Arena --- #[test] fn launched_by_arena_is_always_detected() { let lib = library(); assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), true, &lib, NONE), found("steam:10", How::Launched)); // Outside every install dir (a launcher stub, a Domi app): still a game, the window says which. assert_eq!( detect(&p(r"C:\Users\u\AppData\Local\Lapilli\lapilli.exe"), true, &lib, NONE), Some(Detection { game: None, how: How::Launched }) ); assert_eq!(detect(&p(r"C:\x.exe"), true, &[], NONE), Some(Detection { game: None, how: How::Launched })); } #[test] fn launched_beats_marked() { let mut marks = Marks::new(); marks.insert(p(r"C:\Games\Foo\foo.exe")).unwrap(); assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), true, &library(), &marks).map(|d| d.how), Some(How::Launched)); } // --- marked on ("This is a game") --- #[test] fn marked_exe_outside_install_dirs_is_detected_without_a_game() { let mut marks = Marks::new(); assert!(marks.insert(p(r"D:\Emulators\retro.exe")).unwrap()); let lib = library(); assert_eq!( detect(&p(r"D:\Emulators\retro.exe"), false, &lib, &marks), Some(Detection { game: None, how: How::Marked }) ); assert_eq!(detect(&p(r"d:\emulators\RETRO.exe"), false, &lib, &marks).map(|d| d.how), Some(How::Marked)); // Only that exe: not its folder, not its neighbours. assert_eq!(detect(&p(r"D:\Emulators\other.exe"), false, &lib, &marks), None); assert_eq!(detect(&p(r"D:\Emulators\retro.exe\x.exe"), false, &lib, &marks), None); } #[test] fn marked_on_inside_an_install_dir_is_marked_and_names_the_game() { let mut marks = Marks::new(); marks.insert(p(r"C:\Games\Foo\foo.exe")).unwrap(); assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &library(), &marks), found("steam:10", How::Marked)); } // --- marked off ("This is not a game") --- #[test] fn marked_off_is_never_auto_detected() { let lib = library(); let mut marks = Marks::new(); // A crash reporter inside a game's folder would be found by its install folder ... let reporter = p(r"C:\Games\Foo\CrashReporter.exe"); assert_eq!(detect(&reporter, false, &lib, NONE), found("steam:10", How::InstallDir)); // ... until the user says it is not a game, in any spelling. assert!(marks.set(p("c:/games/foo/crashreporter.EXE"), false).unwrap()); assert!(marks.contains_off(&reporter)); assert!(!marks.contains(&reporter)); assert_eq!(marks.get(&reporter), Some(false)); assert_eq!(detect(&reporter, false, &lib, &marks), None); // Only that exe: the game next to it is still found. assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), false, &lib, &marks), found("steam:10", How::InstallDir)); // A game's own launch exe can be switched off too. let g = manual::game(r"E:\Old\thing.exe", None).unwrap(); let mut m2 = Marks::new(); m2.set(p(r"E:\Old\thing.exe"), false).unwrap(); assert_eq!(detect(&p(r"E:\Old\thing.exe"), false, &[g], &m2), None); } #[test] fn launched_by_arena_beats_marked_off() { let mut marks = Marks::new(); marks.set(p(r"C:\Games\Foo\foo.exe"), false).unwrap(); marks.set(p(r"C:\Elsewhere\stub.exe"), false).unwrap(); assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), true, &library(), &marks), found("steam:10", How::Launched)); assert_eq!( detect(&p(r"C:\Elsewhere\stub.exe"), true, &library(), &marks), Some(Detection { game: None, how: How::Launched }) ); } #[test] fn on_and_off_replace_each_other() { let exe = p(r"D:\Emu\Retro.exe"); let mut marks = Marks::new(); assert_eq!(marks.get(&exe), None); assert_eq!(marks.set(exe.clone(), true), Ok(true)); assert_eq!(marks.set(exe.clone(), true), Ok(false), "already on"); assert_eq!(marks.set(p(r"d:\emu\RETRO.EXE"), false), Ok(true), "on -> off"); assert_eq!(marks.len(), 1, "one exe, never in both sets"); assert_eq!(marks.on().count(), 0); assert_eq!(marks.off().collect::>(), vec![&exe], "first spelling kept"); assert_eq!(detect(&exe, false, &[], &marks), None); // `remove` takes away only an "on" mark; `clear` takes away either. assert!(!marks.remove(&exe)); assert!(marks.contains_off(&exe)); assert_eq!(marks.set(exe.clone(), true), Ok(true), "off -> on"); assert_eq!(detect(&exe, false, &[], &marks).map(|d| d.how), Some(How::Marked)); assert!(marks.clear(&exe)); assert!(!marks.clear(&exe)); assert!(marks.is_empty()); // Switching a mark at the cap does not count as a new exe. let mut full = Marks::new(); for i in 0..limits::MAX_MARKS { full.set(p(&format!(r"C:\G\{i}.exe")), i % 2 == 0).unwrap(); } assert_eq!(full.set(p(r"C:\G\0.exe"), false), Ok(true)); assert_eq!(full.set(p(r"C:\G\new.exe"), false), Err(ParseError::TooMany("marks"))); } #[test] fn marks_are_keyed_by_the_normalised_path() { let mut marks = Marks::new(); assert!(marks.insert(p(r"C:\Emu\Retro.exe")).unwrap()); assert!(!marks.insert(p("c:/emu//RETRO.EXE")).unwrap(), "same exe, another spelling"); assert_eq!(marks.len(), 1); assert!(marks.contains(&p(r"c:\EMU\retro.exe"))); assert!(marks.remove(&p(r"C:\EMU\RETRO.EXE"))); assert!(marks.is_empty()); assert!(!marks.remove(&p(r"C:\Emu\Retro.exe"))); } #[test] fn marks_persist_through_json() { let mut marks = Marks::new(); marks.insert(p(r"D:\Emu\Retro.exe")).unwrap(); marks.insert(p(r"C:\Tools\Game.exe")).unwrap(); marks.set(p(r"C:\Games\Foo\CrashReporter.exe"), false).unwrap(); marks.set(p(r"C:\Games\Foo\Launcher.exe"), false).unwrap(); let json = serde_json::to_string(&marks).unwrap(); // Two arrays of paths, each sorted by the case-folded key, the first spelling kept. assert_eq!( json, concat!( r#"{"on":["C:\\Tools\\Game.exe","D:\\Emu\\Retro.exe"],"#, r#""off":["C:\\Games\\Foo\\CrashReporter.exe","C:\\Games\\Foo\\Launcher.exe"]}"# ) ); let back: Marks = serde_json::from_str(&json).unwrap(); assert_eq!(back, marks); assert_eq!(serde_json::to_string(&back).unwrap(), json); assert_eq!(detect(&p(r"d:\emu\retro.exe"), false, &[], &back).map(|d| d.how), Some(How::Marked)); assert_eq!(detect(&p(r"c:\games\foo\launcher.exe"), false, &library(), &back), None); // Empty marks write both keys. assert_eq!(serde_json::to_string(&Marks::new()).unwrap(), r#"{"on":[],"off":[]}"#); } #[test] fn the_plain_array_form_still_loads_as_on() { let m: Marks = serde_json::from_str(r#"["C:\\Tools\\Game.exe","D:\\Emu\\Retro.exe"]"#).unwrap(); assert_eq!(m.len(), 2); assert!(m.contains(&p(r"c:\tools\game.exe")) && m.contains(&p(r"D:\Emu\Retro.exe"))); assert_eq!(m.off().count(), 0); // Written back in the new form. assert_eq!(serde_json::to_string(&m).unwrap(), r#"{"on":["C:\\Tools\\Game.exe","D:\\Emu\\Retro.exe"],"off":[]}"#); assert_eq!(serde_json::from_str::("[]").unwrap(), Marks::new()); } #[test] fn marks_json_is_checked() { // Spellings of one exe collapse on load; bad paths and non-strings are refused. let m: Marks = serde_json::from_str(r#"["C:\\A\\x.exe","c:/a/X.EXE"]"#).unwrap(); assert_eq!(m.len(), 1); let m: Marks = serde_json::from_str(r#"{"off":["C:\\A\\x.exe","c:/a/X.EXE"]}"#).unwrap(); assert_eq!((m.len(), m.on().count()), (1, 0), "a missing list is empty"); assert_eq!(serde_json::from_str::("{}").unwrap(), Marks::new()); for bad in [ r#"["relative\\x.exe"]"#, r#"["\\\\srv\\s\\x.exe"]"#, r#"[1]"#, r#"{"a":1}"#, r#"{"on":[],"off":[],"extra":[]}"#, r#"{"on":"C:\\x.exe"}"#, r#"{"off":["C:\\G\\..\\x.exe"]}"#, r#""C:\\x.exe""#, "null", // One exe in both lists, in two spellings: the verdict is unknown, so refuse. r#"{"on":["C:\\A\\x.exe"],"off":["c:/a/X.EXE"]}"#, ] { assert!(serde_json::from_str::(bad).is_err(), "{bad}"); } } #[test] fn marks_are_capped() { let mut marks = Marks::new(); for i in 0..limits::MAX_MARKS { marks.insert(p(&format!(r"C:\G\{i}.exe"))).unwrap(); } assert_eq!(marks.insert(p(r"C:\G\one-more.exe")), Err(ParseError::TooMany("marks"))); // Re-marking one already there is fine at the cap. assert_eq!(marks.insert(p(r"C:\G\0.exe")), Ok(false)); let json = serde_json::to_string(&marks).unwrap(); assert!(serde_json::from_str::(&json).is_ok()); let over: Vec = (0..=limits::MAX_MARKS).map(|i| format!(r"C:\G\{i}.exe")).collect(); assert!(serde_json::from_str::(&serde_json::to_string(&over).unwrap()).is_err()); // The cap counts both lists together. let (on, off) = over.split_at(limits::MAX_MARKS / 2); assert!(serde_json::from_value::(serde_json::json!({ "on": on, "off": off })).is_err()); } #[test] fn how_serialises_snake_case() { let d = Detection { game: id("steam:10"), how: How::InstallDir }; assert_eq!(serde_json::to_string(&d).unwrap(), r#"{"game":"steam:10","how":"install_dir"}"#); assert_eq!(serde_json::to_string(&How::Launched).unwrap(), r#""launched""#); assert_eq!(serde_json::to_string(&How::Marked).unwrap(), r#""marked""#); } proptest! { /// Whatever the folder names, auto-detection only ever claims exes inside an install dir, on whole segments. #[test] fn auto_detection_never_leaves_install_dirs( dirs in proptest::collection::vec("[A-Za-z]{1,4}( [A-Za-z]{1,3})?", 1..4), exe in r"[A-Za-z]{1,4}(\\[A-Za-z]{1,4}){0,3}\\[a-z]{1,4}\.exe", ) { // Letters can spell device names (`con`, `nul`): those paths do not exist, so skip them. prop_assume!(dirs.iter().all(|d| WinPath::parse(&format!(r"C:\Games\{d}")).is_ok())); let exe = WinPath::parse(&format!(r"C:\Games\{exe}")); prop_assume!(exe.is_ok()); let exe = exe.unwrap(); let lib: Vec = dirs.iter().enumerate() .map(|(i, d)| steam(i as u32 + 1, d, &format!(r"C:\Games\{d}"))) .collect(); let first = exe.components().nth(1).unwrap().to_lowercase(); let expected = dirs.iter().any(|d| d.to_lowercase() == first); let got = detect(&exe, false, &lib, NONE); prop_assert_eq!(got.is_some(), expected); if let Some(d) = got { prop_assert_eq!(d.how, How::InstallDir); let game = lib.iter().find(|g| Some(&g.id) == d.game.as_ref()).unwrap(); prop_assert!(exe.starts_with(game.install_dir.as_ref().unwrap())); } } }