fix(hal): set_security_pin_api returns dict; cm_telegram now correct

This commit is contained in:
yiekheng 2026-05-02 17:37:50 +08:00
parent d32e4ba58b
commit 231ae69eef
3 changed files with 14 additions and 20 deletions

View File

@ -63,12 +63,8 @@ def cmd_set_pin(args):
if not bot.is_whatsapp_url(args.link): if not bot.is_whatsapp_url(args.link):
print(f"ERROR: not a WhatsApp URL: {args.link}", file=sys.stderr) print(f"ERROR: not a WhatsApp URL: {args.link}", file=sys.stderr)
sys.exit(2) sys.exit(2)
t_username, f_username = bot.get_whatsapp_link_username(args.link) result = bot.set_security_pin_api(args.link)
success = bot.set_security_pin_api(args.link) print(f"OK: f_username={result['f_username']} t_username={result['t_username']}")
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}")
def cmd_insert_user(args): def cmd_insert_user(args):

View File

@ -177,7 +177,7 @@ class CM_BOT_HAL:
) )
if result == False: if result == False:
raise Exception('Failed to insert user to table user') 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): def get_user_credit(self, f_username: str, f_password: str):
cm_bot = CM_BOT() cm_bot = CM_BOT()

View File

@ -76,29 +76,27 @@ class CmdSetPinTests(unittest.TestCase):
mock_hal.set_security_pin_api.assert_not_called() mock_hal.set_security_pin_api.assert_not_called()
@mock.patch.object(bot_cli, "CM_BOT_HAL") @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 = mock_hal_class.return_value
mock_hal.is_whatsapp_url.return_value = True 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 = {
mock_hal.set_security_pin_api.return_value = True "f_username": "f_user_42",
"t_username": "t_user_42",
}
out = io.StringIO() out = io.StringIO()
with contextlib.redirect_stdout(out): with contextlib.redirect_stdout(out):
bot_cli.cmd_set_pin(argparse.Namespace(link="https://chat.whatsapp.com/abc")) bot_cli.cmd_set_pin(argparse.Namespace(link="https://chat.whatsapp.com/abc"))
text = out.getvalue() text = out.getvalue()
self.assertIn("f_username=f_user_42", text) self.assertIn("f_username=f_user_42", text)
self.assertIn("t_username=t_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_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): def test_set_pin_subparser_dispatches(self):
parser = bot_cli.build_parser() parser = bot_cli.build_parser()
args = parser.parse_args(["set-pin", "https://chat.whatsapp.com/abc"]) args = parser.parse_args(["set-pin", "https://chat.whatsapp.com/abc"])