waybetter-for-x/apps/desktop/src-tauri/tests/plugins.rs
2026-09-30 23:01:13 +00:00

219 lines
12 KiB
Rust

use better_twttr::plugins::*;
use better_twttr::policy::{require_capture, require_local, tab_id};
use url::Url;
#[test]
fn tab_labels_are_strict() {
assert_eq!(tab_id("tab-abc-123"), Some("abc-123"));
for bad in ["tab-", "tab-../x", "tab-a b", "shell", "main", "xtab-1", &format!("tab-{}", "a".repeat(41))] {
assert_eq!(tab_id(bad), None, "{bad}");
}
let x = Url::parse("https://x.com/home").unwrap();
assert!(require_capture("tab-1", &x).is_ok());
assert!(require_capture("shell", &x).is_err());
assert!(require_capture("tab-1", &Url::parse("https://example.com/").unwrap()).is_err());
assert!(require_local("tab-1", &Url::parse("tauri://localhost/").unwrap(), false).is_err());
}
#[test]
fn plugin_file_names_cannot_escape_the_plugins_folder() {
assert!(valid_file_name("hide-trends.plugin.js"));
assert!(valid_file_name("My Plugin.plugin.js"));
for bad in ["../x.plugin.js", "a/b.plugin.js", r"a\b.plugin.js", ".plugin.js", ".hidden.plugin.js", "x.js", "C:x.plugin.js", ""] {
assert!(!valid_file_name(bad), "{bad}");
}
}
#[test]
fn meta_header_is_parsed_and_defaults_to_file_name() {
let source = "/**\n * @name Hide Trends\n * @description Hides trends\n * @version 1.2.0\n * @author Ada\n */\nmodule.exports = class {};";
let plugin = parse_meta("hide.plugin.js", source);
assert_eq!(plugin.name, "Hide Trends");
assert_eq!(plugin.description, "Hides trends");
assert_eq!(plugin.version, "1.2.0");
assert_eq!(plugin.author, "Ada");
assert_eq!(parse_meta("bare.plugin.js", "module.exports = {}").name, "bare");
}
#[test]
fn store_lists_toggles_and_persists_css() {
let dir = std::env::temp_dir().join(format!("xw-plugins-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let store = store(dir.clone());
let listed = store.list().unwrap();
let written = BUNDLED.iter().filter(|(file, _)| !better_twttr::areas::SUPERSEDED.iter().any(|row| row.file == *file)).count();
assert_eq!(listed.len(), 1 + written, "fresh folder gets the example and the first-party plugins that are not features of an area yet");
assert!(listed.iter().all(|plugin| !plugin.enabled), "nothing is enabled by default");
assert!(store.start_scripts().is_empty());
let file = "example-hide-trends.plugin.js".to_owned();
store.set_enabled(&file, true).unwrap();
assert!(store.list().unwrap().iter().any(|plugin| plugin.file == file && plugin.enabled));
assert_eq!(store.start_scripts().len(), 1);
assert!(store.set_enabled("../escape.plugin.js", true).is_err());
store.set_enabled(&file, false).unwrap();
assert!(store.start_scripts().is_empty());
store.set_css("body{color:red}").unwrap();
assert_eq!(store.css(), "body{color:red}");
assert!(store.set_css(&"a".repeat(MAX_CSS_BYTES + 1)).is_err());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn generated_scripts_embed_names_and_css_as_string_literals() {
let css = css_script("</style><script>alert(1)</script>\"'`").unwrap();
assert!(css.contains(r#"s.textContent="</style><script>alert(1)</script>\"'`";"#));
let stop = stop_script("a\".plugin.js").unwrap();
assert!(stop.contains(r#"const id="a\".plugin.js";"#));
let start = start_script("p.plugin.js", "module.exports = class { start(){} };").unwrap();
assert!(start.contains("module.exports = class { start(){} };"));
assert!(start.contains("location.origin!=='https://x.com'"));
}
#[test]
fn bundled_plugins_have_valid_names_and_complete_headers() {
assert!(BUNDLED.len() >= 10);
for (file, source) in BUNDLED {
assert!(valid_file_name(file), "{file}");
let meta = parse_meta(file, source);
assert_ne!(meta.name, file.trim_end_matches(PLUGIN_SUFFIX), "{file} has @name");
assert!(!meta.description.is_empty(), "{file} has @description");
assert!(!meta.version.is_empty(), "{file} has @version");
assert_eq!(meta.author, "wayBetter", "{file} has @author");
assert!(source.contains("module.exports = class"), "{file} is a CommonJS class");
assert!(source.contains("start()") && source.contains("stop()"), "{file} has start/stop");
assert!(start_script(file, source).is_ok());
}
}
#[test]
fn bundled_plugins_upgrade_only_untouched_files_and_respect_deletions() {
let dir = std::env::temp_dir().join(format!("xw-bundled-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let store = store(dir.clone());
let read = |file: &str| std::fs::read_to_string(dir.join(file)).unwrap();
// First run writes every shipped file.
store.install_bundled(&[("a.plugin.js", "v1-a"), ("b.plugin.js", "v1-b"), ("c.plugin.js", "v1-c")]);
assert_eq!(read("a.plugin.js"), "v1-a");
// The owner edits b and deletes c.
std::fs::write(dir.join("b.plugin.js"), "my edit").unwrap();
std::fs::remove_file(dir.join("c.plugin.js")).unwrap();
// An unrelated user file happens to use a new plugin's name.
std::fs::write(dir.join("d.plugin.js"), "mine").unwrap();
// Upgrade ships v2 of everything plus a new plugin e.
let v2 = [("a.plugin.js", "v2-a"), ("b.plugin.js", "v2-b"), ("c.plugin.js", "v2-c"), ("d.plugin.js", "v2-d"), ("e.plugin.js", "v2-e")];
store.install_bundled(&v2);
assert_eq!(read("a.plugin.js"), "v2-a", "untouched file is upgraded");
assert_eq!(read("b.plugin.js"), "my edit", "edited file is never overwritten");
assert!(!dir.join("c.plugin.js").exists(), "deleted file stays deleted");
assert_eq!(read("d.plugin.js"), "mine", "foreign file with a bundled name is kept");
assert_eq!(read("e.plugin.js"), "v2-e", "plugin new in this version is added");
// Running again (every list) is stable, and nothing got enabled.
store.install_bundled(&v2);
assert_eq!(read("b.plugin.js"), "my edit");
assert!(store.start_scripts().is_empty());
std::fs::remove_dir_all(dir).unwrap();
}
// ---------------------------------------------------------------------------------------------
// Area plugins: the bundled plugins that became features of Navigation+ (`areas::SUPERSEDED`,
// generated from src/areas/table.ts) are moved over once, without changing what the owner had on.
use better_twttr::areas::{FLAGS, SUPERSEDED};
const OLD_FOCUS: &str = include_str!("../bundled-plugins/focus-mode.plugin.js");
const OLD_KEYS: &str = include_str!("../bundled-plugins/keyboard-plus.plugin.js");
/// A plugins folder as the previous version left it: every bundled plugin written as shipped,
/// Focus Mode and Keyboard Plus among them. Returns the folder and the store of this version.
fn upgraded_folder(name: &str) -> (std::path::PathBuf, PluginStore) {
let dir = std::env::temp_dir().join(format!("xw-areas-{name}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let store = store(dir.clone());
store.ensure().unwrap();
let mut manifest: std::collections::BTreeMap<String, String> = serde_json::from_str(&std::fs::read_to_string(dir.join("bundled.json")).unwrap()).unwrap();
for (file, source) in [("bt-focus-mode.plugin.js", OLD_FOCUS), ("bt-keyboard-plus.plugin.js", OLD_KEYS)] {
std::fs::write(dir.join(file), source).unwrap();
manifest.insert(file.into(), store.file_hash(file).unwrap());
}
std::fs::write(dir.join("bundled.json"), serde_json::to_string(&manifest).unwrap()).unwrap();
(dir, store)
}
#[test]
fn area_plugin_table_is_consistent_with_what_ships() {
for file in ["bt-focus-mode.plugin.js", "bt-keyboard-plus.plugin.js"] {
assert!(SUPERSEDED.iter().any(|row| row.file == file), "{file} is a feature of Navigation+");
}
for row in SUPERSEDED {
assert!(valid_file_name(row.file), "{}", row.file);
assert!(FLAGS.contains(&row.flag), "{} is a flag of the table", row.flag);
assert!(BUNDLED.iter().any(|(file, _)| *file == row.file), "{} is a bundled plugin", row.file);
}
assert!(FLAGS.len() <= waybetter_host::settings::MAX_FEATURES);
for flag in FLAGS {
assert!(!flag.is_empty() && flag.len() <= waybetter_host::settings::MAX_FEATURE_KEY, "{flag}");
}
// The eight area switches, and every deck column flag is a flag of the table.
assert_eq!(FLAGS.iter().filter(|flag| flag.starts_with("area.")).count(), 8);
for flag in better_twttr::column::COLUMN_FLAGS {
assert!(FLAGS.contains(flag), "{flag}");
}
}
#[test]
fn area_plugin_migration_keeps_what_the_owner_had_on() {
// Focus Mode was on, Keyboard Plus off; both as shipped.
let (dir, store) = upgraded_folder("on");
store.set_enabled("bt-focus-mode.plugin.js", true).unwrap();
std::fs::write(dir.join("mine.plugin.js"), "/** @name Mine */ module.exports = class { start(){} stop(){} };").unwrap();
store.set_enabled("mine.plugin.js", true).unwrap();
let mut flags: Vec<String> = Vec::new();
let outcome = store.migrate_superseded(SUPERSEDED, |on| { flags.extend(on.to_vec()); Ok(()) }).unwrap();
assert_eq!(flags, ["navigation.focusMode"], "what was on is on as a feature; what was off stays off");
for file in ["bt-focus-mode.plugin.js", "bt-keyboard-plus.plugin.js"] {
assert!(outcome.removed.iter().any(|removed| removed == file), "{file}");
}
assert!(!dir.join("bt-focus-mode.plugin.js").exists() && !dir.join("bt-keyboard-plus.plugin.js").exists());
// The plugins that were not ported are untouched, and nothing starts twice.
assert!(store.is_enabled("mine.plugin.js"));
assert_eq!(store.start_scripts().len(), 1);
assert!(!store.list().unwrap().iter().any(|plugin| plugin.file.contains("focus-mode") || plugin.file.contains("keyboard-plus")));
// Every later start: nothing to do, and the files never come back.
assert_eq!(store.migrate_superseded(SUPERSEDED, |_| panic!("runs once")).unwrap().removed.len(), 0);
store.ensure().unwrap();
assert!(!dir.join("bt-focus-mode.plugin.js").exists());
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn area_plugin_migration_keeps_an_edited_plugin_and_leaves_its_feature_off() {
let (dir, store) = upgraded_folder("edited");
std::fs::write(dir.join("bt-focus-mode.plugin.js"), format!("{OLD_FOCUS}\n// my change")).unwrap();
store.set_enabled("bt-focus-mode.plugin.js", true).unwrap();
std::fs::remove_file(dir.join("bt-keyboard-plus.plugin.js")).unwrap();
let outcome = store.migrate_superseded(SUPERSEDED, |on| { assert!(on.is_empty(), "no built-in feature is turned on"); Ok(()) }).unwrap();
assert!(outcome.kept.iter().any(|kept| kept == "bt-focus-mode.plugin.js"));
assert!(store.is_enabled("bt-focus-mode.plugin.js"), "the owner's copy keeps running");
assert!(std::fs::read_to_string(dir.join("bt-focus-mode.plugin.js")).unwrap().ends_with("// my change"));
assert!(!dir.join("bt-keyboard-plus.plugin.js").exists(), "a plugin the owner deleted stays deleted");
std::fs::remove_dir_all(dir).unwrap();
}
#[test]
fn a_fresh_install_gets_no_replaced_plugin_file_and_nothing_enabled() {
let dir = std::env::temp_dir().join(format!("xw-areas-fresh-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let store = store(dir.clone());
store.ensure().unwrap();
let outcome = store.migrate_superseded(SUPERSEDED, |on| { assert!(on.is_empty()); Ok(()) }).unwrap();
assert!(outcome.removed.is_empty() && outcome.kept.is_empty());
for row in SUPERSEDED {
assert!(!dir.join(row.file).exists(), "{}", row.file);
}
assert!(store.start_scripts().is_empty(), "plugin files stay off until the owner turns them on");
std::fs::remove_dir_all(dir).unwrap();
}