209 lines
8.9 KiB
Python
209 lines
8.9 KiB
Python
import json
|
|
import os
|
|
from pathlib import Path
|
|
import threading
|
|
import unittest
|
|
from unittest.mock import patch
|
|
|
|
os.environ.setdefault("QT_QPA_PLATFORM", "offscreen")
|
|
from PySide6 import QtCore as C, QtWidgets as W
|
|
from PySide6.QtTest import QTest
|
|
|
|
from ranch_assistant.account_storage import profile_directory, remove_account_files
|
|
from ranch_assistant.accounts_gui import AccountsWindow
|
|
from ranch_assistant.tests.test_core import workspace_temp
|
|
|
|
|
|
class RemovalTests(unittest.TestCase):
|
|
@classmethod
|
|
def setUpClass(cls):
|
|
cls.app = W.QApplication.instance() or W.QApplication([])
|
|
|
|
def wait(self, predicate):
|
|
for _ in range(1000):
|
|
self.app.processEvents()
|
|
QTest.qWait(2)
|
|
if predicate():
|
|
return
|
|
self.fail("Account shutdown did not finish")
|
|
|
|
def idle(self, hub):
|
|
for window in hub.windows.values():
|
|
window.timer.stop()
|
|
self.wait(lambda: all(not w.busy and w.engine.snapshot is not None for w in hub.windows.values()))
|
|
|
|
def remove(self, hub, identifier):
|
|
with patch.object(W.QMessageBox, "question", return_value=W.QMessageBox.StandardButton.Yes):
|
|
hub.confirm_remove_profile(identifier)
|
|
|
|
def test_close_all_remains_closed_and_manual_reopen_is_persisted(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
hub.close_profile(0)
|
|
hub.close()
|
|
hub = AccountsWindow(folder)
|
|
try:
|
|
self.assertEqual(hub.tabs.count(), 0)
|
|
self.assertEqual(len(hub.profiles), 1)
|
|
hub.open_profile(hub.profiles[0])
|
|
self.idle(hub)
|
|
hub.close()
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
self.assertEqual(hub.tabs.count(), 1)
|
|
finally:
|
|
hub.close()
|
|
|
|
def test_remove_closed_account_deletes_only_its_workspace_and_stays_removed(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
hub.add_profile()
|
|
self.idle(hub)
|
|
identifier = hub.profiles[-1]["id"]
|
|
target = profile_directory(folder, identifier)
|
|
hub.close_profile(hub.tabs.count() - 1)
|
|
(target / "sessions").mkdir()
|
|
(target / "sessions" / "test.json").write_text("synthetic session", encoding="utf-8")
|
|
protected = folder / "credentials.json"
|
|
protected.write_text("{}", encoding="utf-8")
|
|
try:
|
|
self.remove(hub, identifier)
|
|
self.assertFalse(target.exists())
|
|
self.assertTrue(protected.exists())
|
|
self.assertIn("default", hub.windows)
|
|
self.assertFalse(any(p["id"] == identifier for p in hub.profiles))
|
|
hub.close()
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
self.assertFalse(any(p["id"] == identifier for p in hub.profiles))
|
|
self.assertNotIn(identifier, hub.windows)
|
|
finally:
|
|
hub.close()
|
|
|
|
def test_remove_default_preserves_other_accounts_registry_and_shared_files(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
hub.add_profile()
|
|
self.idle(hub)
|
|
other = hub.profiles[-1]["id"]
|
|
sentinel = profile_directory(folder, other) / "keep.txt"
|
|
sentinel.write_text("another account", encoding="utf-8")
|
|
(folder / "sessions").mkdir()
|
|
(folder / "sessions" / "old.json").write_text("synthetic", encoding="utf-8")
|
|
(folder / "roles" / "role").mkdir(parents=True)
|
|
(folder / "roles" / "role" / "maps.json").write_text("{}", encoding="utf-8")
|
|
shared = folder / "shared-note.txt"
|
|
shared.write_text("keep", encoding="utf-8")
|
|
try:
|
|
self.remove(hub, "default")
|
|
for name in ("credentials.json", "sessions", "roles", "settings.json"):
|
|
self.assertFalse((folder / name).exists())
|
|
self.assertTrue(sentinel.exists())
|
|
self.assertTrue(shared.exists())
|
|
self.assertTrue(hub.index_path.exists())
|
|
self.assertEqual([p["id"] for p in hub.profiles], [other])
|
|
hub.close()
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
self.assertNotIn("default", hub.windows)
|
|
self.assertIn(other, hub.windows)
|
|
finally:
|
|
hub.close()
|
|
|
|
def test_cancel_removal_keeps_live_account_untouched(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
try:
|
|
with patch.object(W.QMessageBox, "question", return_value=W.QMessageBox.StandardButton.No):
|
|
hub.confirm_remove_profile("default")
|
|
self.assertIn("default", hub.windows)
|
|
self.assertFalse(hub.windows["default"].closed)
|
|
self.assertTrue(hub.profiles[0]["open"])
|
|
finally:
|
|
hub.close()
|
|
|
|
def test_busy_account_is_removed_only_after_worker_finishes_without_recreating_records(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
window = hub.windows["default"]
|
|
release, started = threading.Event(), threading.Event()
|
|
def work():
|
|
started.set()
|
|
release.wait(3)
|
|
return "synthetic completed job"
|
|
try:
|
|
window.submit("test", work)
|
|
self.wait(started.is_set)
|
|
self.remove(hub, "default")
|
|
self.assertIn("default", hub.windows)
|
|
self.assertIn("default", hub.pending_removals)
|
|
self.assertFalse(window.execute.isChecked())
|
|
self.assertFalse(window.monitoring)
|
|
release.set()
|
|
self.wait(lambda: not hub.windows and not hub.pending_removals)
|
|
QTest.qWait(400)
|
|
self.assertFalse((folder / "settings.json").exists())
|
|
self.assertFalse((folder / "credentials.json").exists())
|
|
self.assertEqual(json.loads(hub.index_path.read_text(encoding="utf-8"))["profiles"], [])
|
|
hub.close()
|
|
hub = AccountsWindow(folder)
|
|
self.assertEqual(hub.tabs.count(), 0)
|
|
self.assertEqual(hub.profiles, [])
|
|
finally:
|
|
release.set()
|
|
self.idle(hub)
|
|
hub.close()
|
|
|
|
def test_external_lock_blocks_removal_and_keeps_account_in_registry(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
hub.add_profile()
|
|
self.idle(hub)
|
|
identifier = hub.profiles[-1]["id"]
|
|
hub.close_profile(hub.tabs.count() - 1)
|
|
target = profile_directory(folder, identifier)
|
|
lock = C.QLockFile(str(target / "workspace.lock"))
|
|
self.assertTrue(lock.tryLock(0))
|
|
try:
|
|
self.remove(hub, identifier)
|
|
self.assertTrue(target.exists())
|
|
self.assertTrue(any(p["id"] == identifier for p in hub.profiles))
|
|
self.assertIn("其他窗口", hub.statusBar().currentMessage())
|
|
finally:
|
|
lock.unlock()
|
|
hub.close()
|
|
|
|
def test_failed_file_removal_keeps_registry_for_retry(self):
|
|
with workspace_temp() as folder:
|
|
hub = AccountsWindow(folder)
|
|
self.idle(hub)
|
|
try:
|
|
with patch("ranch_assistant.accounts_gui.remove_account_files", side_effect=OSError("synthetic failure")):
|
|
self.remove(hub, "default")
|
|
self.assertTrue(any(p["id"] == "default" for p in hub.profiles))
|
|
self.assertIn("未完全移除", hub.statusBar().currentMessage())
|
|
finally:
|
|
hub.close()
|
|
|
|
def test_profile_deletion_rejects_invalid_identifier_and_directory_links(self):
|
|
with workspace_temp() as folder, workspace_temp() as outside:
|
|
for identifier in ("..", "profiles", "../outside", "", None):
|
|
with self.assertRaises(ValueError):
|
|
profile_directory(folder, identifier)
|
|
(folder / "profiles").mkdir()
|
|
target = folder / "profiles" / ("a" * 32)
|
|
try:
|
|
target.symlink_to(outside, target_is_directory=True)
|
|
except OSError:
|
|
return # Some Windows installations do not permit symlinks.
|
|
try:
|
|
(outside / "keep.txt").write_text("keep", encoding="utf-8")
|
|
with self.assertRaises(ValueError):
|
|
remove_account_files(folder, "a" * 32)
|
|
self.assertTrue((outside / "keep.txt").exists())
|
|
finally:
|
|
target.unlink()
|