From c4de2e0889992ba00e3056f42ed07ea5eafebe7f Mon Sep 17 00:00:00 2001 From: Aaron Ruan Date: Tue, 26 May 2026 22:08:02 +0800 Subject: [PATCH] Refresh --- src/letscode/main.py | 37 ++++++++++++++++++++++++++++++++++--- src/letscode/storage.py | 35 +++++++++++++++++++++++++++++++++++ tests/test_storage.py | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 tests/test_storage.py diff --git a/src/letscode/main.py b/src/letscode/main.py index c09e955..7e263a4 100644 --- a/src/letscode/main.py +++ b/src/letscode/main.py @@ -126,6 +126,7 @@ class LetsCodeApp(App): TESTFIRE_BUTTON_ID = "testfire-button" FULL_TEST_BUTTON_ID = "full-test-button" EDIT_BUTTON_ID = "edit-button" + REFRESH_JUDGE_BUTTON_ID = "refresh-judge-button" LOAD_PROBLEM_COMMAND = "/load_problem" HINT_COMMAND = "/hint" @@ -207,6 +208,7 @@ class LetsCodeApp(App): yield ScrollableContainer( Horizontal( Button("Edit", id=self.EDIT_BUTTON_ID), + Button("Refresh", id=self.REFRESH_JUDGE_BUTTON_ID), Button("Testfire", id=self.TESTFIRE_BUTTON_ID), Button("Full", id=self.FULL_TEST_BUTTON_ID), id="judge-buttons", @@ -245,7 +247,10 @@ class LetsCodeApp(App): def on_mount(self) -> None: self.query_one(self.INPUT_SELECTOR, PromptInput).focus() - self.current_problem_id = self.DEFAULT_PROBLEM_ID + self.problem_store = ProblemStore() + self.current_problem_id = ( + self.problem_store.load_last_question_id() or self.DEFAULT_PROBLEM_ID + ) self.current_problem_markdown = "" self.current_fetched_hints_markdown: list[str] = [] self.current_generated_hints_markdown: list[str] = [] @@ -319,7 +324,18 @@ class LetsCodeApp(App): self.current_judge = None self.chat_entries = [] - problem_markdown = await load_problem_markdown(question_id) + try: + self.problem_store.save_last_question_id(question_id) + except OSError as error: + self.notify( + f"Unable to persist current problem: {error}", + severity="warning", + ) + + problem_markdown = await load_problem_markdown( + question_id, + store=self.problem_store, + ) self.current_problem_markdown = problem_markdown.problem self.current_fetched_hints_markdown = problem_markdown.fetched_hints self.current_generated_hints_markdown = [ @@ -392,7 +408,7 @@ class LetsCodeApp(App): self.current_generated_hints_markdown.append(hint_markdown) try: - ProblemStore().add_generated_hint( + self.problem_store.add_generated_hint( self.current_problem_id, self.current_problem_markdown, self.current_fetched_hints_markdown, @@ -450,6 +466,11 @@ class LetsCodeApp(App): self.run_worker(self.open_solution_editor(), exclusive=True) return + if event.button.id == self.REFRESH_JUDGE_BUTTON_ID: + event.stop() + self.run_worker(self.refresh_judge(), exclusive=True) + return + if event.button.id == self.TESTFIRE_BUTTON_ID: event.stop() self.run_worker(self.run_judge("testfire"), exclusive=True) @@ -460,6 +481,16 @@ class LetsCodeApp(App): self.run_worker(self.run_judge("full"), exclusive=True) return + async def refresh_judge(self) -> None: + if not self.current_problem_markdown: + self.notify("Problem is still loading.", severity="warning") + return + + await self.query_one(self.JUDGE_SELECTOR, Markdown).update( + "# Judge\n\nRefreshing judge..." + ) + await self.prepare_judge() + async def run_judge(self, suite: JudgeSuite) -> None: if not self.current_problem_markdown: self.notify("Problem is still loading.", severity="warning") diff --git a/src/letscode/storage.py b/src/letscode/storage.py index d10627a..82307c6 100644 --- a/src/letscode/storage.py +++ b/src/letscode/storage.py @@ -35,6 +35,41 @@ class ProblemStore: "MyApp", "MyCompany" ).user_data_path self.problems_path = self.data_path / "problems" + self.state_path = self.data_path / "state.json" + + def load_last_question_id(self) -> int | None: + if not self.state_path.exists(): + return None + + try: + payload = json.loads(self.state_path.read_text(encoding="utf-8")) + except (OSError, json.JSONDecodeError): + return None + + if not isinstance(payload, dict): + return None + + try: + question_id = int(payload["last_question_id"]) + except (KeyError, TypeError, ValueError): + return None + + return question_id + + def save_last_question_id(self, question_id: int) -> None: + self.data_path.mkdir(parents=True, exist_ok=True) + temp_path = self.state_path.with_suffix(".json.tmp") + temp_path.write_text( + json.dumps( + { + "last_question_id": question_id, + "updated_at": datetime.now(UTC).isoformat(), + }, + indent=2, + ), + encoding="utf-8", + ) + temp_path.replace(self.state_path) def load_problem(self, question_id: int) -> StoredProblem | None: path = self._problem_path(question_id) diff --git a/tests/test_storage.py b/tests/test_storage.py new file mode 100644 index 0000000..3d3f4e9 --- /dev/null +++ b/tests/test_storage.py @@ -0,0 +1,32 @@ +import tempfile +import unittest +from pathlib import Path + +from letscode.storage import ProblemStore + + +class ProblemStoreStateTest(unittest.TestCase): + def test_save_and_load_last_question_id(self) -> None: + with tempfile.TemporaryDirectory() as directory: + store = ProblemStore(Path(directory)) + + store.save_last_question_id(42) + + self.assertEqual(store.load_last_question_id(), 42) + + def test_load_last_question_id_returns_none_without_state(self) -> None: + with tempfile.TemporaryDirectory() as directory: + store = ProblemStore(Path(directory)) + + self.assertIsNone(store.load_last_question_id()) + + def test_load_last_question_id_returns_none_for_invalid_state(self) -> None: + with tempfile.TemporaryDirectory() as directory: + store = ProblemStore(Path(directory)) + store.state_path.write_text("{", encoding="utf-8") + + self.assertIsNone(store.load_last_question_id()) + + +if __name__ == "__main__": + unittest.main() -- knotmirror 0.0.1