fix: recognize modern Chrome Sessions so open restores login
All checks were successful
技能自动化发布 / release (push) Successful in 10s

This commit is contained in:
2026-09-16 14:13:38 +08:00
parent b93b2ea36e
commit 7709292f1c
5 changed files with 110 additions and 11 deletions

View File

@@ -18,16 +18,45 @@ class TestPlaywrightLaunchHelpers:
assert "--start-maximized" in args
class TestProfileHasRestorableSession:
def test_empty_profile_false(self, tmp_path):
from service.chrome_profile_prefs import profile_has_restorable_session
assert profile_has_restorable_session(str(tmp_path / "prof")) is False
def test_legacy_current_session(self, tmp_path):
from service.chrome_profile_prefs import profile_has_restorable_session
default = tmp_path / "prof" / "Default"
default.mkdir(parents=True)
(default / "Current Session").write_bytes(b"x")
assert profile_has_restorable_session(str(tmp_path / "prof")) is True
def test_modern_sessions_dir(self, tmp_path):
from service.chrome_profile_prefs import profile_has_restorable_session
sessions = tmp_path / "prof" / "Default" / "Sessions"
sessions.mkdir(parents=True)
(sessions / "Session_13434010951759956").write_bytes(b"sess")
assert profile_has_restorable_session(str(tmp_path / "prof")) is True
def test_modern_tabs_only(self, tmp_path):
from service.chrome_profile_prefs import profile_has_restorable_session
sessions = tmp_path / "prof" / "Default" / "Sessions"
sessions.mkdir(parents=True)
(sessions / "Tabs_13434010952144072").write_bytes(b"tabs")
assert profile_has_restorable_session(str(tmp_path / "prof")) is True
class TestCmdOpenDirectBrowser:
"""account open = same-process Chrome/Edge launch (like reveal-profile)."""
def test_cmd_open_launches_browser_exe(self, clean_db, tmp_path, capsys, monkeypatch):
def _seed_account(self, clean_db, tmp_path, profile):
from db.accounts_repo import insert_account
from service import browser_service
from util.platforms import seed_platforms
seed_platforms(clean_db)
profile = tmp_path / "prof"
aid = insert_account(
clean_db,
platform_key="sohu",
@@ -45,7 +74,9 @@ class TestCmdOpenDirectBrowser:
auth_strategy="password_auto",
)
clean_db.commit()
return aid
def _patch_launch(self, monkeypatch, browser_service, tmp_path):
fake_exe = str(tmp_path / "chrome.exe")
monkeypatch.setattr(
browser_service,
@@ -64,6 +95,14 @@ class TestCmdOpenDirectBrowser:
return FakeProc()
monkeypatch.setattr(browser_service.subprocess, "Popen", fake_popen)
return fake_exe, spawned
def test_cmd_open_launches_browser_exe(self, clean_db, tmp_path, capsys, monkeypatch):
from service import browser_service
profile = tmp_path / "prof"
aid = self._seed_account(clean_db, tmp_path, profile)
fake_exe, spawned = self._patch_launch(monkeypatch, browser_service, tmp_path)
browser_service.cmd_open(str(aid))
out = capsys.readouterr().out.strip()
@@ -88,6 +127,27 @@ class TestCmdOpenDirectBrowser:
assert "open_child_runner" not in joined
assert "python" not in joined.lower()
def test_cmd_open_skips_url_when_modern_sessions_exist(
self, clean_db, tmp_path, capsys, monkeypatch
):
"""Modern Chrome Sessions/ must restore without forcing homepage URL."""
from service import browser_service
profile = tmp_path / "prof"
sessions = profile / "Default" / "Sessions"
sessions.mkdir(parents=True)
(sessions / "Session_13434010951759956").write_bytes(b"sess")
aid = self._seed_account(clean_db, tmp_path, profile)
_fake_exe, spawned = self._patch_launch(monkeypatch, browser_service, tmp_path)
browser_service.cmd_open(str(aid))
out = capsys.readouterr().out.strip()
payload = json.loads(out.splitlines()[-1])
assert payload["success"] is True
assert "https://example.com" not in spawned["cmd"]
assert spawned["cmd"][-1].startswith("--")
class TestHumanChromeLaunchArgs:
def test_flags_then_url(self, tmp_path):