From 231ae69eef9ce603b055cf2909398b9138c93482 Mon Sep 17 00:00:00 2001 From: yiekheng Date: Sat, 2 May 2026 17:37:50 +0800 Subject: [PATCH] fix(hal): set_security_pin_api returns dict; cm_telegram now correct --- app/bot_cli.py | 8 ++------ app/cm_bot_hal.py | 2 +- tests/test_bot_cli.py | 24 +++++++++++------------- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/app/bot_cli.py b/app/bot_cli.py index c99c450..8f29baa 100644 --- a/app/bot_cli.py +++ b/app/bot_cli.py @@ -63,12 +63,8 @@ def cmd_set_pin(args): if not bot.is_whatsapp_url(args.link): print(f"ERROR: not a WhatsApp URL: {args.link}", file=sys.stderr) sys.exit(2) - t_username, f_username = bot.get_whatsapp_link_username(args.link) - success = bot.set_security_pin_api(args.link) - if not success: - print("ERROR: set_security_pin_api returned a falsy result", file=sys.stderr) - sys.exit(1) - print(f"OK: f_username={f_username} t_username={t_username}") + result = bot.set_security_pin_api(args.link) + print(f"OK: f_username={result['f_username']} t_username={result['t_username']}") def cmd_insert_user(args): diff --git a/app/cm_bot_hal.py b/app/cm_bot_hal.py index 4c9ff0a..052d175 100644 --- a/app/cm_bot_hal.py +++ b/app/cm_bot_hal.py @@ -177,7 +177,7 @@ class CM_BOT_HAL: ) if result == False: raise Exception('Failed to insert user to table user') - return result + return {"f_username": f_username, "t_username": t_username} def get_user_credit(self, f_username: str, f_password: str): cm_bot = CM_BOT() diff --git a/tests/test_bot_cli.py b/tests/test_bot_cli.py index baa8d5c..e2f08e8 100644 --- a/tests/test_bot_cli.py +++ b/tests/test_bot_cli.py @@ -76,29 +76,27 @@ class CmdSetPinTests(unittest.TestCase): mock_hal.set_security_pin_api.assert_not_called() @mock.patch.object(bot_cli, "CM_BOT_HAL") - def test_extracts_names_locally_and_succeeds(self, mock_hal_class): + def test_prints_names_from_hal_return_dict(self, mock_hal_class): + # set_security_pin_api now returns a dict on success and raises + # on any failure path. cmd_set_pin reads names directly from the + # dict instead of pre-fetching them via get_whatsapp_link_username. mock_hal = mock_hal_class.return_value mock_hal.is_whatsapp_url.return_value = True - mock_hal.get_whatsapp_link_username.return_value = ("t_user_42", "f_user_42") - mock_hal.set_security_pin_api.return_value = True + mock_hal.set_security_pin_api.return_value = { + "f_username": "f_user_42", + "t_username": "t_user_42", + } out = io.StringIO() with contextlib.redirect_stdout(out): bot_cli.cmd_set_pin(argparse.Namespace(link="https://chat.whatsapp.com/abc")) text = out.getvalue() self.assertIn("f_username=f_user_42", text) self.assertIn("t_username=t_user_42", text) + # The local get_whatsapp_link_username call from the old workaround + # is gone — the HAL resolves names internally. + mock_hal.get_whatsapp_link_username.assert_not_called() mock_hal.set_security_pin_api.assert_called_once_with("https://chat.whatsapp.com/abc") - @mock.patch.object(bot_cli, "CM_BOT_HAL") - def test_falsy_set_security_pin_result_exits_nonzero(self, mock_hal_class): - mock_hal = mock_hal_class.return_value - mock_hal.is_whatsapp_url.return_value = True - mock_hal.get_whatsapp_link_username.return_value = ("t", "f") - mock_hal.set_security_pin_api.return_value = False - with self.assertRaises(SystemExit) as cm: - bot_cli.cmd_set_pin(argparse.Namespace(link="https://chat.whatsapp.com/abc")) - self.assertEqual(cm.exception.code, 1) - def test_set_pin_subparser_dispatches(self): parser = bot_cli.build_parser() args = parser.parse_args(["set-pin", "https://chat.whatsapp.com/abc"])