feat: complete T-105b account login reuse
This commit is contained in:
+63
-4
@@ -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)
|
||||
|
||||
|
||||
+43
-5
@@ -4284,12 +4284,14 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
self.addCleanup(tab.close)
|
||||
tab.table.selectRow(0)
|
||||
|
||||
class FakeProcess:
|
||||
pid = 1234
|
||||
|
||||
with mock.patch(
|
||||
"app.gui.accounts.launch_for_login",
|
||||
return_value=FakeProcess(),
|
||||
return_value={
|
||||
"action": "launched",
|
||||
"pid": 1234,
|
||||
"target_id": "target-new",
|
||||
"url": "https://seller.shopee.tw/portal/",
|
||||
},
|
||||
) as launch_for_login:
|
||||
tab.launch_login()
|
||||
|
||||
@@ -4302,8 +4304,44 @@ class GuiTests(TempDirMixin, unittest.TestCase):
|
||||
events = db.list_run_log_events(run_log.id, path=cfg["db_path"])
|
||||
messages = "\n".join(event.message for event in events)
|
||||
self.assertIn("step=launch_chrome result=start", messages)
|
||||
self.assertIn("step=launch_chrome result=success", messages)
|
||||
self.assertIn("step=launch_chrome result=launched", messages)
|
||||
self.assertIn("pid=1234", messages)
|
||||
self.assertIn("target_id=target-new", messages)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
|
||||
def test_accounts_tab_launch_login_reuse_writes_chrome_launch_run_log(self):
|
||||
with self.make_temp_dir() as temp_dir:
|
||||
cfg = self.make_config(temp_dir)
|
||||
account = accounts.create_account("主店", "alias-a", debug_port=9222, config=cfg)
|
||||
statuses = []
|
||||
tab = AccountsTab(config=cfg, status_callback=statuses.append)
|
||||
self.addCleanup(tab.close)
|
||||
tab.table.selectRow(0)
|
||||
|
||||
with mock.patch(
|
||||
"app.gui.accounts.launch_for_login",
|
||||
return_value={
|
||||
"action": "reused",
|
||||
"pid": None,
|
||||
"target_id": "target-existing",
|
||||
"url": "https://seller.shopee.tw/portal/",
|
||||
},
|
||||
) as launch_for_login:
|
||||
tab.launch_login()
|
||||
|
||||
launch_for_login.assert_called_once_with(account, config=cfg)
|
||||
self.assertEqual("已启动", tab.login_statuses["alias-a"])
|
||||
self.assertIn("已复用现有窗口", statuses[-1])
|
||||
run_log = db.list_run_logs(limit=1, run_type="chrome_launch", path=cfg["db_path"])[0]
|
||||
self.assertEqual("done", run_log.status)
|
||||
self.assertEqual(1, run_log.success_count)
|
||||
events = db.list_run_log_events(run_log.id, path=cfg["db_path"])
|
||||
messages = "\n".join(event.message for event in events)
|
||||
self.assertIn("step=launch_chrome result=start", messages)
|
||||
self.assertIn("step=launch_chrome result=reused", messages)
|
||||
self.assertIn("target_id=target-existing", messages)
|
||||
self.assertIn("url=https://seller.shopee.tw/portal/", messages)
|
||||
|
||||
self.assert_removed(temp_dir)
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user