"""Tests for the bot CLI (app.bot_cli). The CLI mirrors the Telegram bot's manual-trigger surface (Telegram handlers /1, /2, /3) plus the operational ops (credit, transfer, monitor-once). With no args, it drops into a stdlib TUI menu. These tests mock app.bot_cli.CM_BOT_HAL so they never touch the database or cm99.net. The HAL class is imported at module load (which is a pure import — no env reads), so we can patch the symbol bound on app.bot_cli without affecting other tests. """ import argparse import contextlib import io import os import sys import unittest from unittest import mock import app.bot_cli as bot_cli class ParserSanityTests(unittest.TestCase): def test_build_parser_returns_argument_parser(self): parser = bot_cli.build_parser() self.assertIsInstance(parser, argparse.ArgumentParser) def test_main_with_no_args_dispatches_to_interactive(self): # When invoked with no subcommand, main() should drop into the # TUI loop. We verify the dispatch by patching cmd_interactive to # a no-op recorder. with mock.patch.object(bot_cli, "cmd_interactive", return_value=0) as mocked: rc = bot_cli.main([]) mocked.assert_called_once() 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) class CmdSetPinTests(unittest.TestCase): @mock.patch.object(bot_cli, "CM_BOT_HAL") def test_rejects_invalid_whatsapp_url(self, mock_hal_class): mock_hal = mock_hal_class.return_value mock_hal.is_whatsapp_url.return_value = False with self.assertRaises(SystemExit) as cm: bot_cli.cmd_set_pin(argparse.Namespace(link="https://not-whatsapp.example/x")) self.assertEqual(cm.exception.code, 2) 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): 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 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) 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"]) self.assertIs(args.func, bot_cli.cmd_set_pin) self.assertEqual(args.link, "https://chat.whatsapp.com/abc") class CmdInsertUserTests(unittest.TestCase): @mock.patch.object(bot_cli, "CM_BOT_HAL") def test_inserts_using_password_lookup_and_security_pin(self, mock_hal_class): mock_hal = mock_hal_class.return_value mock_hal.get_user_pass_from_acc.return_value = "abc12345" mock_hal.security_pin = "999111" mock_hal.insert_user_to_table_user.return_value = True out = io.StringIO() with contextlib.redirect_stdout(out): bot_cli.cmd_insert_user(argparse.Namespace(f_username="13c1234", t_username="player_x")) self.assertIn("OK: inserted 13c1234 → player_x", out.getvalue()) mock_hal.insert_user_to_table_user.assert_called_once_with({ "f_username": "13c1234", "f_password": "abc12345", "t_username": "player_x", "t_password": "999111", }) @mock.patch.object(bot_cli, "CM_BOT_HAL") def test_no_password_for_f_user_exits_2(self, mock_hal_class): mock_hal = mock_hal_class.return_value mock_hal.get_user_pass_from_acc.return_value = None with self.assertRaises(SystemExit) as cm: bot_cli.cmd_insert_user(argparse.Namespace(f_username="missing", t_username="player_x")) self.assertEqual(cm.exception.code, 2) mock_hal.insert_user_to_table_user.assert_not_called() @mock.patch.object(bot_cli, "CM_BOT_HAL") def test_insert_failure_exits_1(self, mock_hal_class): mock_hal = mock_hal_class.return_value mock_hal.get_user_pass_from_acc.return_value = "abc" mock_hal.security_pin = "000" mock_hal.insert_user_to_table_user.return_value = False with self.assertRaises(SystemExit) as cm: bot_cli.cmd_insert_user(argparse.Namespace(f_username="13c1234", t_username="player_x")) self.assertEqual(cm.exception.code, 1) def test_insert_user_subparser_dispatches(self): parser = bot_cli.build_parser() args = parser.parse_args(["insert-user", "13c1234", "player_x"]) self.assertIs(args.func, bot_cli.cmd_insert_user) self.assertEqual(args.f_username, "13c1234") self.assertEqual(args.t_username, "player_x") if __name__ == "__main__": unittest.main()