Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277import inspectimport ioimport unittestfrom contextlib import redirect_stderrfrom unittest.mock import Mock, patch
from rich.syntax import Syntaxfrom textual.app import App, ComposeResultfrom textual.widgets import Markdown, Static
from letscode.main import ( ChatBubble, ChatEntry, JudgeOutput, LetsCodeApp, SolutionPreview, main, parse_cli_args, parse_question_id,)
class JudgeOutputSmokeApp(App[None]): def compose(self) -> ComposeResult: yield JudgeOutput()
class SolutionPreviewSmokeApp(App[None]): def compose(self) -> ComposeResult: yield SolutionPreview()
class ChatBubbleTest(unittest.TestCase): def test_chat_bubble_uses_role_classes(self) -> None: bubble = ChatBubble(ChatEntry("user", "How should I start?")) child = list(bubble.compose())[0]
self.assertTrue(bubble.has_class("chat-row")) self.assertTrue(bubble.has_class("user")) self.assertTrue(child.has_class("chat-bubble")) self.assertTrue(child.has_class("user"))
def test_chat_bubble_preserves_agent_role(self) -> None: bubble = ChatBubble(ChatEntry("agent", "Look for a hash map invariant.")) child = list(bubble.compose())[0]
self.assertTrue(bubble.has_class("agent")) self.assertTrue(child.has_class("agent"))
class JudgeTabWidgetsTest(unittest.TestCase): def test_solution_preview_is_composed_from_static_widgets(self) -> None: children = list(SolutionPreview().compose())
self.assertTrue(all(isinstance(child, Static) for child in children)) self.assertFalse(any(isinstance(child, Markdown) for child in children))
def test_solution_preview_uses_syntax_renderable_for_code(self) -> None: code_widget = SolutionPreview._code_widget("class Solution:\n pass")
self.assertIsInstance(getattr(code_widget, "_Static__content"), Syntax)
def test_judge_output_is_composed_from_static_widgets(self) -> None: children = list(JudgeOutput().compose())
self.assertTrue(all(isinstance(child, Static) for child in children)) self.assertFalse(any(isinstance(child, Markdown) for child in children))
def test_judge_output_does_not_override_textual_render_with_coroutine( self, ) -> None: self.assertFalse(inspect.iscoroutinefunction(JudgeOutput._render))
class JudgeOutputSmokeTest(unittest.IsolatedAsyncioTestCase): async def test_judge_output_renders_when_mounted(self) -> None: app = JudgeOutputSmokeApp()
async with app.run_test(size=(80, 20)) as pilot: await pilot.pause() judge_output = app.query_one(JudgeOutput) await judge_output.update_status("Ready") await pilot.pause()
class SolutionPreviewSmokeTest(unittest.IsolatedAsyncioTestCase): async def test_solution_preview_renders_when_mounted(self) -> None: app = SolutionPreviewSmokeApp()
async with app.run_test(size=(80, 20)) as pilot: await pilot.pause() solution_preview = app.query_one(SolutionPreview) await solution_preview.update_solution("class Solution:\n pass") await pilot.pause()
class ParseQuestionIdTest(unittest.TestCase): def test_parse_question_id_accepts_custom_negative_ids(self) -> None: self.assertEqual(parse_question_id("-1"), -1)
def test_parse_question_id_rejects_non_integer_values(self) -> None: self.assertIsNone(parse_question_id("two-sum"))
class CliArgsTest(unittest.TestCase): def test_parse_cli_args_accepts_problem_id(self) -> None: args = parse_cli_args(["--problem", "42"])
self.assertEqual(args.problem, 42) self.assertIsNone(args.new) self.assertFalse(args.no_leetcode) self.assertFalse(args.reset)
def test_parse_cli_args_accepts_custom_negative_problem_id(self) -> None: args = parse_cli_args(["--problem", "-1"])
self.assertEqual(args.problem, -1)
def test_parse_cli_args_accepts_no_leetcode(self) -> None: args = parse_cli_args(["--no-leetcode"])
self.assertTrue(args.no_leetcode) self.assertIsNone(args.problem) self.assertIsNone(args.new)
def test_parse_cli_args_accepts_new_prompt(self) -> None: args = parse_cli_args(["--new", "dynamic programming with strings"])
self.assertEqual(args.new, "dynamic programming with strings") self.assertIsNone(args.problem)
def test_parse_cli_args_rejects_non_integer_problem_id(self) -> None: with redirect_stderr(io.StringIO()): with self.assertRaises(SystemExit): parse_cli_args(["--problem", "two-sum"])
def test_parse_cli_args_rejects_problem_with_no_leetcode(self) -> None: with redirect_stderr(io.StringIO()): with self.assertRaises(SystemExit): parse_cli_args(["--problem", "42", "--no-leetcode"])
def test_parse_cli_args_rejects_problem_with_new_prompt(self) -> None: with redirect_stderr(io.StringIO()): with self.assertRaises(SystemExit): parse_cli_args(["--problem", "42", "--new", "graphs"])
class MainCliTest(unittest.TestCase): def test_main_reset_resets_store_and_does_not_start_app(self) -> None: with ( patch("letscode.main.ProblemStore") as problem_store_class, patch("letscode.main.LetsCodeApp") as app_class, ): main(["--reset"])
problem_store_class.return_value.reset_all.assert_called_once_with() app_class.assert_not_called()
def test_main_problem_starts_app_with_initial_problem_id(self) -> None: with patch("letscode.main.LetsCodeApp") as app_class: main(["--problem", "42"])
app_class.assert_called_once_with( initial_problem_id=42, initial_new_prompt=None, load_initial_problem=True, ) app_class.return_value.run.assert_called_once_with()
def test_main_no_leetcode_starts_app_without_initial_problem_load(self) -> None: with patch("letscode.main.LetsCodeApp") as app_class: main(["--no-leetcode"])
app_class.assert_called_once_with( initial_problem_id=None, initial_new_prompt=None, load_initial_problem=False, ) app_class.return_value.run.assert_called_once_with()
def test_main_new_starts_app_with_initial_new_prompt(self) -> None: with patch("letscode.main.LetsCodeApp") as app_class: main(["--new", "arrays and heaps"])
app_class.assert_called_once_with( initial_problem_id=None, initial_new_prompt="arrays and heaps", load_initial_problem=False, ) app_class.return_value.run.assert_called_once_with()
class QuitActionTest(unittest.TestCase): def test_quit_cancels_workers_and_closes_openai_connections(self) -> None: app = LetsCodeApp() app.workers.cancel_all = Mock() # type: ignore[method-assign] app.openai_http_clients.close = Mock() # type: ignore[method-assign] app.exit = Mock() # type: ignore[method-assign]
app.action_quit()
app.workers.cancel_all.assert_called_once_with() app.openai_http_clients.close.assert_called_once_with() app.exit.assert_called_once_with()
class SlashCommandTest(unittest.TestCase): def test_random_slash_command_starts_random_problem_worker(self) -> None: app = LetsCodeApp() workers: list[object] = []
def run_worker(work: object, *args: object, **kwargs: object) -> object: workers.append(work) if inspect.iscoroutine(work): work.close() return work
app.run_worker = run_worker # type: ignore[method-assign]
self.assertTrue(app.handle_slash_command("/random")) self.assertEqual(len(workers), 1)
def test_solve_slash_command_starts_solve_worker(self) -> None: app = LetsCodeApp() workers: list[object] = []
def run_worker(work: object, *args: object, **kwargs: object) -> object: workers.append(work) if inspect.iscoroutine(work): work.close() return work
app.run_worker = run_worker # type: ignore[method-assign]
self.assertTrue(app.handle_slash_command("/solve")) self.assertEqual(len(workers), 1)
def test_clear_slash_command_starts_clear_worker(self) -> None: app = LetsCodeApp() workers: list[object] = []
def run_worker(work: object, *args: object, **kwargs: object) -> object: workers.append(work) if inspect.iscoroutine(work): work.close() return work
app.run_worker = run_worker # type: ignore[method-assign]
self.assertTrue(app.handle_slash_command("/clear")) self.assertEqual(len(workers), 1)
def test_unknown_slash_command_is_consumed(self) -> None: app = LetsCodeApp() notifications: list[tuple[str, str | None]] = []
def notify(message: str, *args: object, **kwargs: object) -> None: notifications.append((message, kwargs.get("severity")))
app.notify = notify # type: ignore[method-assign]
self.assertTrue(app.handle_slash_command("/typo ask the agent")) self.assertEqual( notifications, [ ( "Unknown command: /typo. Available commands: " "/problem, /new, /hint, /fix, /solve, /clear, /reset, /random", "warning", ) ], )
if __name__ == "__main__": unittest.main()