[core] Detect: Marks hold on and off sets; arena-core 0.2.0 checks pass

Marks keeps one verdict per exe: on ("This is a game") or off ("This is
not a game"). detect: Launched > marked off (None) > marked on (Marked,
with the library game if any) > InstallDir. Serde writes
{"on": [...], "off": [...]} and still reads the plain array as on;
an exe in both lists is refused, MAX_MARKS counts both.

Also: collapsible-if for clippy, rustfmt, README and FORMATS updated.
Checked: cargo test -p arena-core (183 passed), clippy -D warnings,
fmt --check, check for wasm32-unknown-unknown and aarch64-linux-android.
This commit is contained in:
2026-11-26 03:27:03 +01:00
parent 43701bbadc
commit 95d031fef9
6 changed files with 251 additions and 64 deletions
+122 -10
View File
@@ -53,7 +53,7 @@ fn found(game: &str, how: How) -> Option<Detection> {
const NONE: &Marks = &Marks::new();
// --- Rule 1: an exe outside every install_dir is never auto-detected ---
// --- an exe outside every install_dir is never auto-detected ---
#[test]
fn outside_every_install_dir_is_not_detected() {
@@ -115,7 +115,10 @@ fn a_drive_root_install_dir_does_not_claim_the_whole_drive() {
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 }));
assert_eq!(
detect(&p(r"d:\GAME.EXE"), false, &lib, NONE),
Some(Detection { game: Some(g.id), how: How::InstallDir })
);
}
#[test]
@@ -165,7 +168,7 @@ fn dot_dot_never_escapes() {
assert!(p(r"C:\Games\Foo").join(r"..\FooBar\fb.exe").is_err());
}
// --- Rule 2: launched by Arena ---
// --- launched by Arena ---
#[test]
fn launched_by_arena_is_always_detected() {
@@ -186,14 +189,17 @@ fn launched_beats_marked() {
assert_eq!(detect(&p(r"C:\Games\Foo\foo.exe"), true, &library(), &marks).map(|d| d.how), Some(How::Launched));
}
// --- Rule 3: marked ("This is a game") ---
// --- 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),
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);
@@ -201,10 +207,75 @@ fn marked_exe_outside_install_dirs_is_detected_without_a_game() {
}
#[test]
fn marked_exe_inside_an_install_dir_reports_the_game() {
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::InstallDir));
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<_>>(), 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]
@@ -224,12 +295,35 @@ 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();
// A plain array of paths, the first spelling kept, sorted by the case-folded key.
assert_eq!(json, r#"["C:\\Tools\\Game.exe","D:\\Emu\\Retro.exe"]"#);
// 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::<Marks>("[]").unwrap(), Marks::new());
}
#[test]
@@ -237,7 +331,22 @@ 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);
for bad in [r#"["relative\\x.exe"]"#, r#"["\\\\srv\\s\\x.exe"]"#, r#"[1]"#, r#"{"a":1}"#, r#""C:\\x.exe""#] {
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::<Marks>("{}").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::<Marks>(bad).is_err(), "{bad}");
}
}
@@ -255,6 +364,9 @@ fn marks_are_capped() {
assert!(serde_json::from_str::<Marks>(&json).is_ok());
let over: Vec<String> = (0..=limits::MAX_MARKS).map(|i| format!(r"C:\G\{i}.exe")).collect();
assert!(serde_json::from_str::<Marks>(&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::<Marks>(serde_json::json!({ "on": on, "off": off })).is_err());
}
#[test]