feat: complete T-105b account login reuse

This commit is contained in:
chengma
2026-07-06 15:01:50 +08:00
parent 7ac0dcd00e
commit 0dad4151ef
12 changed files with 276 additions and 30 deletions
+63 -4
View File
@@ -76,17 +76,76 @@ class AccountsTests(TempDirMixin, unittest.TestCase):
self.assert_removed(temp_dir)
def test_launch_for_login_only_launches_chrome(self):
def test_launch_for_login_starts_chrome_when_port_not_running(self):
with self.make_temp_dir() as temp_dir:
cfg = self.make_config(temp_dir)
account = accounts.create_account("主店", "alias", debug_port=9222, config=cfg)
process = object()
with mock.patch("app.accounts.chrome.launch_chrome", return_value=process) as launch:
class FakeProcess:
pid = 1234
with mock.patch("app.accounts.chrome.is_running", return_value=False) as is_running, \
mock.patch("app.accounts.chrome.launch_chrome", return_value=FakeProcess()) as launch, \
mock.patch("app.accounts.chrome.wait_debug_ready", return_value=True) as wait_ready, \
mock.patch("app.accounts.cdp.http_get", return_value=[]), \
mock.patch(
"app.accounts.cdp.create_tab_info",
return_value={
"id": "target-new",
"url": "https://seller.shopee.tw/portal/",
"webSocketDebuggerUrl": "ws-new",
},
) as create_tab_info, \
mock.patch("app.accounts.cdp.activate_tab") as activate_tab:
result = accounts.launch_for_login("alias", config=cfg)
self.assertIs(process, result)
self.assertEqual("launched", result["action"])
self.assertTrue(result["launched"])
self.assertFalse(result["reused"])
self.assertEqual(1234, result["pid"])
self.assertEqual("target-new", result["target_id"])
is_running.assert_called_once_with(9222)
launch.assert_called_once_with(account, config=cfg)
wait_ready.assert_called_once_with(9222, timeout=60)
create_tab_info.assert_called_once_with(
"https://seller.shopee.tw/portal/",
host="127.0.0.1:9222",
)
activate_tab.assert_called_once_with("target-new", host="127.0.0.1:9222")
self.assert_removed(temp_dir)
def test_launch_for_login_reuses_running_chrome_without_starting_process(self):
with self.make_temp_dir() as temp_dir:
cfg = self.make_config(temp_dir)
accounts.create_account("主店", "alias", debug_port=9222, config=cfg)
existing_tab = {
"type": "page",
"id": "target-existing",
"url": "https://seller.shopee.tw/portal/",
"webSocketDebuggerUrl": "ws-existing",
}
with mock.patch("app.accounts.chrome.is_running", return_value=True) as is_running, \
mock.patch("app.accounts.chrome.launch_chrome") as launch, \
mock.patch("app.accounts.chrome.wait_debug_ready") as wait_ready, \
mock.patch("app.accounts.cdp.http_get", return_value=[existing_tab]) as http_get, \
mock.patch("app.accounts.cdp.create_tab_info") as create_tab_info, \
mock.patch("app.accounts.cdp.activate_tab") as activate_tab:
result = accounts.launch_for_login("alias", config=cfg)
self.assertEqual("reused", result["action"])
self.assertTrue(result["reused"])
self.assertFalse(result["launched"])
self.assertIsNone(result["pid"])
self.assertFalse(result["created_tab"])
self.assertEqual("target-existing", result["target_id"])
is_running.assert_called_once_with(9222)
http_get.assert_called_once_with("/json", host="127.0.0.1:9222")
activate_tab.assert_called_once_with("target-existing", host="127.0.0.1:9222")
launch.assert_not_called()
wait_ready.assert_not_called()
create_tab_info.assert_not_called()
self.assert_removed(temp_dir)