Add Beat Saber data backup command
This commit is contained in:
+237
-4
@@ -1,21 +1,26 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
import os
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from zipfile import ZipFile
|
||||
|
||||
from plugin_helper.bootstrap import _run_ipa
|
||||
from plugin_helper.checker import check_lock
|
||||
from plugin_helper.cli import installed_plugins_report
|
||||
from plugin_helper.cli import installed_plugins_report, run
|
||||
from plugin_helper.fsutil import sha256_file
|
||||
from plugin_helper.installer import apply_plan, uninstall_plugin
|
||||
from plugin_helper.instances import get_instance, list_instances
|
||||
from plugin_helper.models import Lockfile, LockedPlugin, Registry, RegistryPlugin
|
||||
from plugin_helper.planner import create_plan
|
||||
from plugin_helper.scanner import scan_instance
|
||||
from plugin_helper.state import downloads_dir, load_installed_state, plugin_downloads_dir
|
||||
from plugin_helper.scanner import scan_bootstrap_files, scan_instance
|
||||
from plugin_helper.state import downloads_dir, load_installed_state, plugin_downloads_dir, save_bootstrap_state
|
||||
from plugin_helper.updates import check_updates
|
||||
from plugin_helper.userdata import backup_userdata
|
||||
from plugin_helper.userdata import backup_userdata, infer_windows_appdata_path, sync_windows_data_repo
|
||||
|
||||
|
||||
class PluginHelperTests(unittest.TestCase):
|
||||
@@ -39,6 +44,87 @@ class PluginHelperTests(unittest.TestCase):
|
||||
self.assertEqual(scan["files"][0]["path"], "Plugins/Example.dll")
|
||||
self.assertIn("sha256", scan["files"][0])
|
||||
|
||||
def test_multi_root_instances_and_ambiguous_lookup(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
windows = root / "windows"
|
||||
local = root / "local"
|
||||
win_inst = windows / "1.44.1"
|
||||
local_inst = local / "1.44.1"
|
||||
(win_inst / "Beat Saber_Data").mkdir(parents=True)
|
||||
(local_inst / "Beat Saber_Data").mkdir(parents=True)
|
||||
|
||||
instances = list_instances([windows, local])
|
||||
|
||||
self.assertEqual(len(instances), 2)
|
||||
self.assertEqual({item.path for item in instances}, {win_inst, local_inst})
|
||||
with self.assertRaisesRegex(ValueError, "ambiguous"):
|
||||
get_instance([windows, local], "1.44.1")
|
||||
|
||||
def test_menu_selects_instance_and_action(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
instance = root / "instances" / "1.40.8"
|
||||
state = root / "state"
|
||||
(instance / "Beat Saber_Data").mkdir(parents=True)
|
||||
(instance / "Plugins").mkdir()
|
||||
|
||||
answers = iter(["1", "3", "q"])
|
||||
output = StringIO()
|
||||
with patch("builtins.input", side_effect=lambda _: next(answers)), patch("sys.stdout", output):
|
||||
status = run(
|
||||
[
|
||||
"--instances-root",
|
||||
str(root / "instances"),
|
||||
"--state-dir",
|
||||
str(state),
|
||||
"menu",
|
||||
]
|
||||
)
|
||||
|
||||
self.assertEqual(status, 0)
|
||||
self.assertIn("Counts files currently present", output.getvalue())
|
||||
|
||||
def test_menu_routes_duplicate_instance_names_by_selected_root(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
first_root = root / "a-root"
|
||||
second_root = root / "z-root"
|
||||
first = first_root / "1.44.1"
|
||||
second = second_root / "1.44.1"
|
||||
state = root / "state"
|
||||
(first / "Beat Saber_Data").mkdir(parents=True)
|
||||
(second / "Beat Saber_Data").mkdir(parents=True)
|
||||
(second / "Plugins").mkdir()
|
||||
(second / "Plugins" / "Example.dll").write_bytes(b"dll")
|
||||
|
||||
answers = iter(["2", "3", "q"])
|
||||
output = StringIO()
|
||||
with patch("builtins.input", side_effect=lambda _: next(answers)), patch("sys.stdout", output):
|
||||
status = run(
|
||||
[
|
||||
"--instances-root",
|
||||
os.pathsep.join([str(first_root), str(second_root)]),
|
||||
"--state-dir",
|
||||
str(state),
|
||||
"menu",
|
||||
]
|
||||
)
|
||||
|
||||
self.assertEqual(status, 0)
|
||||
self.assertIn("1.44.1: 1 files", output.getvalue())
|
||||
|
||||
def test_run_ipa_timeout_returns_control(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
result = _run_ipa(
|
||||
command=["python", "-c", "import time; time.sleep(30)"],
|
||||
instance_path=Path(tmp),
|
||||
timeout_seconds=1,
|
||||
)
|
||||
|
||||
self.assertTrue(result["timedOut"])
|
||||
self.assertNotEqual(result["returncode"], 0)
|
||||
|
||||
def test_plan_apply_and_uninstall_dll(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
work = Path(tmp)
|
||||
@@ -235,6 +321,103 @@ class PluginHelperTests(unittest.TestCase):
|
||||
repo_root=work,
|
||||
)
|
||||
|
||||
def test_scan_bootstrap_files_includes_root_ipa_and_bsipa_dirs(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
instance = Path(tmp)
|
||||
(instance / "IPA" / "Backups").mkdir(parents=True)
|
||||
(instance / "Libs").mkdir()
|
||||
(instance / "winhttp.dll").write_bytes(b"proxy")
|
||||
(instance / "IPA.exe").write_bytes(b"ipa")
|
||||
(instance / "IPA.exe.config").write_bytes(b"config")
|
||||
(instance / "IPA" / "Backups" / "Beat Saber.exe.bak").write_bytes(b"backup")
|
||||
(instance / "Libs" / "0Harmony.dll").write_bytes(b"harmony")
|
||||
|
||||
paths = [item["path"] for item in scan_bootstrap_files(instance)]
|
||||
|
||||
self.assertEqual(
|
||||
paths,
|
||||
[
|
||||
"IPA.exe",
|
||||
"IPA.exe.config",
|
||||
"IPA/Backups/Beat Saber.exe.bak",
|
||||
"Libs/0Harmony.dll",
|
||||
"winhttp.dll",
|
||||
],
|
||||
)
|
||||
|
||||
def test_plan_requires_healthy_bootstrap_for_locked_bsipa_dependencies(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
work = Path(tmp)
|
||||
instance = work / "instances" / "1.44.1"
|
||||
state = work / "state"
|
||||
instance.mkdir(parents=True)
|
||||
(instance / "Beat Saber_Data").mkdir()
|
||||
asset = plugin_downloads_dir(state, "1.44.1", "example") / "Example.dll"
|
||||
asset.write_bytes(b"managed dll")
|
||||
|
||||
registry = Registry(
|
||||
{
|
||||
"bsipa": RegistryPlugin(
|
||||
id="bsipa",
|
||||
name="BSIPA",
|
||||
repo=None,
|
||||
install_strategy="root-zip",
|
||||
),
|
||||
"example": RegistryPlugin(
|
||||
id="example",
|
||||
name="Example",
|
||||
repo=None,
|
||||
install_strategy="dll-to-plugins",
|
||||
),
|
||||
}
|
||||
)
|
||||
lockfile = Lockfile(
|
||||
beat_saber_version="1.44.1",
|
||||
instance="1.44.1",
|
||||
plugins=(
|
||||
LockedPlugin(id="bsipa", repo=None, tag="4.3.7", asset="BSIPA.zip", sha256=None),
|
||||
LockedPlugin(
|
||||
id="example",
|
||||
repo=None,
|
||||
tag="v1.0.0",
|
||||
asset="Example.dll",
|
||||
sha256=sha256_file(asset),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
with self.assertRaisesRegex(ValueError, "BSIPA bootstrap is not healthy"):
|
||||
create_plan(
|
||||
instance="1.44.1",
|
||||
instance_path=instance,
|
||||
beat_saber_version="1.44.1",
|
||||
registry=registry,
|
||||
lockfile=lockfile,
|
||||
state_root=state,
|
||||
repo_root=work,
|
||||
selected={"example"},
|
||||
)
|
||||
|
||||
(instance / "IPA").mkdir()
|
||||
(instance / "Libs").mkdir()
|
||||
(instance / "Logs").mkdir()
|
||||
(instance / "IPA.exe").write_bytes(b"ipa")
|
||||
(instance / "winhttp.dll").write_bytes(b"proxy")
|
||||
(instance / "Logs" / "_latest.log").write_text("Beat Saber IPA (BSIPA): 4.3.7\n", encoding="utf-8")
|
||||
save_bootstrap_state(state, "1.44.1", {"files": scan_bootstrap_files(instance)})
|
||||
|
||||
plan, _ = create_plan(
|
||||
instance="1.44.1",
|
||||
instance_path=instance,
|
||||
beat_saber_version="1.44.1",
|
||||
registry=registry,
|
||||
lockfile=lockfile,
|
||||
state_root=state,
|
||||
repo_root=work,
|
||||
selected={"example"},
|
||||
)
|
||||
self.assertEqual(plan["changes"][0]["target"], "Plugins/Example.dll")
|
||||
|
||||
def test_userdata_backup_contains_manifest(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
@@ -247,6 +430,56 @@ class PluginHelperTests(unittest.TestCase):
|
||||
self.assertTrue(Path(result["archive"]).exists())
|
||||
self.assertEqual(result["manifest"]["fileCount"], 1)
|
||||
|
||||
def test_infer_windows_appdata_path_from_mounted_instance(self) -> None:
|
||||
instance = Path("/home/pleb/Windows/Users/pleb/BSManager/BSInstances/1.44.1")
|
||||
|
||||
self.assertEqual(
|
||||
infer_windows_appdata_path(instance),
|
||||
Path("/home/pleb/Windows/Users/pleb/AppData/LocalLow/Hyperbolic Magnetism/Beat Saber"),
|
||||
)
|
||||
|
||||
def test_sync_windows_data_repo_copies_into_stable_backup_root(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
root = Path(tmp)
|
||||
instance = root / "Users" / "pleb" / "BSManager" / "BSInstances" / "1.44.1"
|
||||
appdata = root / "Users" / "pleb" / "AppData" / "LocalLow" / "Hyperbolic Magnetism" / "Beat Saber"
|
||||
backup_repo = root / "backup"
|
||||
(instance / "UserData").mkdir(parents=True)
|
||||
(instance / "UserData" / "settings.json").write_text("{}", encoding="utf-8")
|
||||
(instance / "UserData" / "BeatLeader" / "Replays").mkdir(parents=True)
|
||||
(instance / "UserData" / "BeatLeader" / "Replays" / "big.bsor").write_text("replay", encoding="utf-8")
|
||||
(instance / "UserData" / "ScoreSaber" / "Replays").mkdir(parents=True)
|
||||
(instance / "UserData" / "ScoreSaber" / "Replays" / "big.bsor").write_text("replay", encoding="utf-8")
|
||||
(instance / "UserData" / "BeatSaberPlus" / "Cache").mkdir(parents=True)
|
||||
(instance / "UserData" / "BeatSaberPlus" / "Cache" / "cached.dat").write_text("cache", encoding="utf-8")
|
||||
(instance / "UserData" / "BeatSaverNotifier.json").write_text('{"refreshToken":"secret"}', encoding="utf-8")
|
||||
(instance / "UserData" / "Accsaber").mkdir(parents=True)
|
||||
(instance / "UserData" / "Accsaber" / "PlayerScoreCache.json").write_text("{}", encoding="utf-8")
|
||||
appdata.mkdir(parents=True)
|
||||
(appdata / "Player.log").write_text("log", encoding="utf-8")
|
||||
(appdata / "settings.cfg").write_text("settings", encoding="utf-8")
|
||||
|
||||
result = sync_windows_data_repo(
|
||||
instance="1.44.1",
|
||||
instance_path=instance,
|
||||
backup_root=backup_repo,
|
||||
)
|
||||
|
||||
self.assertEqual(result["backupRoot"], str(backup_repo))
|
||||
self.assertEqual((backup_repo / "UserData" / "settings.json").read_text(), "{}")
|
||||
self.assertFalse((backup_repo / "UserData" / "BeatLeader" / "Replays").exists())
|
||||
self.assertFalse((backup_repo / "UserData" / "ScoreSaber" / "Replays").exists())
|
||||
self.assertFalse((backup_repo / "UserData" / "BeatSaberPlus" / "Cache").exists())
|
||||
self.assertFalse((backup_repo / "UserData" / "BeatSaverNotifier.json").exists())
|
||||
self.assertFalse((backup_repo / "UserData" / "Accsaber" / "PlayerScoreCache.json").exists())
|
||||
self.assertFalse((backup_repo / "AppData" / "Player.log").exists())
|
||||
self.assertEqual((backup_repo / "AppData" / "settings.cfg").read_text(), "settings")
|
||||
descriptor = json.loads((backup_repo / "backup-descriptor.json").read_text(encoding="utf-8"))
|
||||
self.assertEqual(descriptor["instance"], "1.44.1")
|
||||
self.assertEqual(descriptor["sources"][0]["source"], str(instance / "UserData"))
|
||||
self.assertIn("BeatLeader/Replays", descriptor["skipped"])
|
||||
self.assertIn("*.log", descriptor["excludePatterns"])
|
||||
|
||||
def test_check_reports_missing_asset(self) -> None:
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
work = Path(tmp)
|
||||
|
||||
Reference in New Issue
Block a user