use std::path::Path;
use crate::model::{CandidateDraft, Category, Safety};
use crate::rules::markers::parent_ends_with;
pub fn classify(project_dir: &Path, name: &str, path: &Path) -> Option<CandidateDraft> {
if name.ends_with(".ShipIt")
&& name.len() > ".ShipIt".len()
&& parent_ends_with(project_dir, &["Library", "Caches"])
{
return Some(CandidateDraft {
path: path.to_path_buf(),
name: name.to_string(),
rule_id: "app.shipit_caches".to_string(),
category: Category::Cache,
safety: Safety::Safe,
reasons: vec![
"Squirrel.Mac ShipIt installer leftovers (already-applied app updates)".to_string(),
],
warnings: Vec::new(),
restore_hint:
"none needed — these are leftover update packages from completed app updates"
.to_string(),
});
}
if name == "Chrome" && parent_ends_with(project_dir, &["Library", "Caches", "Google"]) {
return Some(CandidateDraft {
path: path.to_path_buf(),
name: name.to_string(),
rule_id: "chrome.cache".to_string(),
category: Category::Cache,
safety: Safety::Safe,
reasons: vec!["Chrome browser HTTP/disk cache (not user data)".to_string()],
warnings: Vec::new(),
restore_hint: "Chrome will repopulate the cache on next browsing".to_string(),
});
}
if name == "LarkInternational" && parent_ends_with(project_dir, &["Library", "Caches"]) {
return Some(CandidateDraft {
path: path.to_path_buf(),
name: name.to_string(),
rule_id: "app.lark_cache".to_string(),
category: Category::Cache,
safety: Safety::Safe,
reasons: vec!["Lark/Feishu rebuildable application cache".to_string()],
warnings: vec!["Close Lark/Feishu first if it is actively running".to_string()],
restore_hint: "Lark/Feishu will recreate this cache on next launch".to_string(),
});
}
if name == "GoogleUpdater"
&& parent_ends_with(project_dir, &["Library", "Application Support", "Google"])
{
return Some(CandidateDraft {
path: path.to_path_buf(),
name: name.to_string(),
rule_id: "chrome.google_updater".to_string(),
category: Category::Cache,
safety: Safety::Safe,
reasons: vec!["Chrome auto-updater historical state (will be recreated)".to_string()],
warnings: Vec::new(),
restore_hint: "Chrome's updater will recreate it on next launch".to_string(),
});
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn classifies_vscode_shipit() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join("com.microsoft.VSCode.ShipIt");
let draft = classify(&parent, "com.microsoft.VSCode.ShipIt", &path)
.expect("should classify VSCode ShipIt");
assert_eq!(draft.rule_id, "app.shipit_caches");
assert_eq!(draft.safety, Safety::Safe);
}
#[test]
fn classifies_notion_shipit() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join("notion.id.ShipIt");
let draft =
classify(&parent, "notion.id.ShipIt", &path).expect("should classify Notion ShipIt");
assert_eq!(draft.rule_id, "app.shipit_caches");
}
#[test]
fn classifies_telegram_shipit() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join("com.tdesktop.Telegram.ShipIt");
assert!(classify(&parent, "com.tdesktop.Telegram.ShipIt", &path).is_some());
}
#[test]
fn rejects_bare_shipit_directory() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join(".ShipIt");
assert!(classify(&parent, ".ShipIt", &path).is_none());
}
#[test]
fn rejects_shipit_outside_library_caches() {
let parent = PathBuf::from("/Users/me/Desktop");
let path = parent.join("com.microsoft.VSCode.ShipIt");
assert!(classify(&parent, "com.microsoft.VSCode.ShipIt", &path).is_none());
}
#[test]
fn rejects_non_shipit_name_in_library_caches() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join("com.microsoft.VSCode");
assert!(classify(&parent, "com.microsoft.VSCode", &path).is_none());
}
#[test]
fn classifies_chrome_cache_under_library_caches_google() {
let parent = PathBuf::from("/Users/me/Library/Caches/Google");
let path = parent.join("Chrome");
let draft = classify(&parent, "Chrome", &path).expect("should classify Chrome cache");
assert_eq!(draft.rule_id, "chrome.cache");
assert_eq!(draft.safety, Safety::Safe);
assert!(draft.reasons[0].contains("cache"));
}
#[test]
fn rejects_chrome_under_application_support_google() {
let parent = PathBuf::from("/Users/me/Library/Application Support/Google");
let path = parent.join("Chrome");
assert!(
classify(&parent, "Chrome", &path).is_none(),
"MUST NOT match Application Support/Google/Chrome (user data path)"
);
}
#[test]
fn rejects_chrome_with_wrong_grandparent() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join("Chrome");
assert!(classify(&parent, "Chrome", &path).is_none());
}
#[test]
fn rejects_chromium_under_library_caches_google() {
let parent = PathBuf::from("/Users/me/Library/Caches/Google");
let path = parent.join("Chromium");
assert!(classify(&parent, "Chromium", &path).is_none());
}
#[test]
fn classifies_lark_cache_under_library_caches() {
let parent = PathBuf::from("/Users/me/Library/Caches");
let path = parent.join("LarkInternational");
let draft =
classify(&parent, "LarkInternational", &path).expect("should classify Lark cache");
assert_eq!(draft.rule_id, "app.lark_cache");
assert_eq!(draft.safety, Safety::Safe);
}
#[test]
fn classifies_google_updater_under_application_support_google() {
let parent = PathBuf::from("/Users/me/Library/Application Support/Google");
let path = parent.join("GoogleUpdater");
let draft =
classify(&parent, "GoogleUpdater", &path).expect("should classify GoogleUpdater");
assert_eq!(draft.rule_id, "chrome.google_updater");
assert_eq!(draft.safety, Safety::Safe);
}
#[test]
fn rejects_google_updater_outside_application_support_google() {
let parent = PathBuf::from("/Users/me/Library/Caches/Google");
let path = parent.join("GoogleUpdater");
assert!(classify(&parent, "GoogleUpdater", &path).is_none());
}
#[test]
fn rejects_short_paths() {
let p = PathBuf::from("/Google");
assert!(classify(&p, "Chrome", &p.join("Chrome")).is_none());
assert!(classify(&p, "GoogleUpdater", &p.join("GoogleUpdater")).is_none());
}
}