From f5d4a554d64c962463a2d2fd87a6c1add7c7c16d Mon Sep 17 00:00:00 2001 From: yiekheng Date: Sat, 2 May 2026 16:58:24 +0800 Subject: [PATCH] feat(bot_cli): add register subcommand (Telegram /1 analog) --- app/bot_cli.py | 17 ++++++++++++++++- tests/test_bot_cli.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/bot_cli.py b/app/bot_cli.py index 8d1ec11..91c0dc0 100644 --- a/app/bot_cli.py +++ b/app/bot_cli.py @@ -12,12 +12,27 @@ def cmd_interactive(_args): raise NotImplementedError("cmd_interactive is implemented in a later task") +def _print_user(user: dict) -> None: + print(f"Username: {user['username']}") + print(f"Password: {user['password']}") + print(f"Link: {user['link']}") + + +def cmd_register(_args): + bot = CM_BOT_HAL() + _print_user(bot.get_user_api()) + + def build_parser() -> argparse.ArgumentParser: p = argparse.ArgumentParser( prog="bot_cli", description="CM Bot dev CLI (mirrors Telegram triggers).", ) - p.add_subparsers(dest="command") + sub = p.add_subparsers(dest="command") + + sp = sub.add_parser("register", aliases=["get-acc"], help="Get next available account (Telegram /1).") + sp.set_defaults(func=cmd_register) + return p diff --git a/tests/test_bot_cli.py b/tests/test_bot_cli.py index f5a23a4..35f0d79 100644 --- a/tests/test_bot_cli.py +++ b/tests/test_bot_cli.py @@ -36,5 +36,34 @@ class ParserSanityTests(unittest.TestCase): self.assertEqual(rc, 0) +class CmdRegisterTests(unittest.TestCase): + @mock.patch.object(bot_cli, "CM_BOT_HAL") + def test_prints_username_password_link(self, mock_hal_class): + mock_hal = mock_hal_class.return_value + mock_hal.get_user_api.return_value = { + "username": "13c1234", + "password": "abc12345", + "link": "https://example.com/r/foo", + } + out = io.StringIO() + with contextlib.redirect_stdout(out): + bot_cli.cmd_register(argparse.Namespace()) + text = out.getvalue() + self.assertIn("Username: 13c1234", text) + self.assertIn("Password: abc12345", text) + self.assertIn("Link: https://example.com/r/foo", text) + mock_hal.get_user_api.assert_called_once_with() + + def test_register_subparser_dispatches_to_cmd_register(self): + parser = bot_cli.build_parser() + args = parser.parse_args(["register"]) + self.assertIs(args.func, bot_cli.cmd_register) + + def test_get_acc_alias_dispatches_to_cmd_register(self): + parser = bot_cli.build_parser() + args = parser.parse_args(["get-acc"]) + self.assertIs(args.func, bot_cli.cmd_register) + + if __name__ == "__main__": unittest.main()