diff options
| -rw-r--r-- | .tasks/tasks.jsonl | 1 | ||||
| -rw-r--r-- | Biz/PodcastItLater/Worker.py | 113 |
2 files changed, 114 insertions, 0 deletions
diff --git a/.tasks/tasks.jsonl b/.tasks/tasks.jsonl index 0584cd9..c81b70f 100644 --- a/.tasks/tasks.jsonl +++ b/.tasks/tasks.jsonl @@ -198,3 +198,4 @@ {"taskCreatedAt":"2025-11-22T13:01:18.426816879Z","taskDependencies":[],"taskDescription":"Update repository setup scripts (e.g. Omni/Ide/hooks or task init) to automatically run 'git config commit.template .gitmessage' so all users get the template.","taskId":"t-1o2bkseag8u","taskNamespace":"Omni/Ide.hs","taskParent":null,"taskPriority":"P2","taskStatus":"Open","taskTitle":"Automate git commit template configuration","taskType":"WorkTask","taskUpdatedAt":"2025-11-22T13:01:18.426816879Z"} {"taskCreatedAt":"2025-11-22T13:03:21.434586142Z","taskDependencies":[],"taskDescription":"Move detailed documentation (Task Manager, Bild, Git Workflow) to separate README files in their respective namespaces. Keep AGENTS.md focused on critical rules, cheat sheets, and pointers to the detailed docs. Goal is to reduce token usage.","taskId":"t-1o2bkufixnc","taskNamespace":"Omni.hs","taskParent":null,"taskPriority":"P2","taskStatus":"Open","taskTitle":"Refactor and condense AGENTS.md","taskType":"WorkTask","taskUpdatedAt":"2025-11-22T13:04:41.243757221Z"} {"taskCreatedAt":"2025-11-21T04:37:55.163249193Z","taskDependencies":[{"depId":"t-144gqry","depType":"DiscoveredFrom"}],"taskDescription":null,"taskId":"t-rwadhwrzt","taskNamespace":null,"taskParent":null,"taskPriority":"P2","taskStatus":"Open","taskTitle":"Fix bild failure for Biz/PodcastItLater/Web.py","taskType":"WorkTask","taskUpdatedAt":"2025-11-21T04:37:55.163249193Z"} +{"taskCreatedAt":"2025-11-21T05:28:31.973657907Z","taskDependencies":[],"taskDescription":null,"taskId":"t-rwagbsb6w","taskNamespace":"Biz/PodcastItLater.hs","taskParent":null,"taskPriority":"P2","taskStatus":"Done","taskTitle":"Add error handling tests for Worker","taskType":"WorkTask","taskUpdatedAt":"2025-11-21T05:40:59.255645021Z"} diff --git a/Biz/PodcastItLater/Worker.py b/Biz/PodcastItLater/Worker.py index 94db30e..3245fdd 100644 --- a/Biz/PodcastItLater/Worker.py +++ b/Biz/PodcastItLater/Worker.py @@ -2042,6 +2042,117 @@ class TestJobProcessing(Test.TestCase): mock_update.assert_not_called() +class TestWorkerErrorHandling(Test.TestCase): + """Test worker error handling and recovery.""" + + def setUp(self) -> None: + """Set up test environment.""" + Core.Database.init_db() + self.user_id, _ = Core.Database.create_user("test@example.com") + self.job_id = Core.Database.add_to_queue( + "https://example.com", + "test@example.com", + self.user_id, + ) + self.shutdown_handler = ShutdownHandler() + self.processor = ArticleProcessor(self.shutdown_handler) + + @staticmethod + def tearDown() -> None: + """Clean up.""" + Core.Database.teardown() + + def test_process_pending_jobs_exception_handling(self) -> None: + """Test that process_pending_jobs handles exceptions.""" + + def side_effect(job: dict[str, Any]) -> None: + # Simulate process_job starting and setting status to processing + Core.Database.update_job_status(job["id"], "processing") + msg = "Unexpected Error" + raise ValueError(msg) + + with ( + unittest.mock.patch.object( + self.processor, + "process_job", + side_effect=side_effect, + ), + unittest.mock.patch( + "Biz.PodcastItLater.Core.Database.update_job_status", + side_effect=Core.Database.update_job_status, + ) as _mock_update, + ): + process_pending_jobs(self.processor) + + # Job should be marked as error + job = Core.Database.get_job_by_id(self.job_id) + self.assertIsNotNone(job) + if job: + self.assertEqual(job["status"], "error") + self.assertIn("Unexpected Error", job["error_message"]) + + def test_process_retryable_jobs_success(self) -> None: + """Test processing of retryable jobs.""" + # Set up a retryable job + Core.Database.update_job_status(self.job_id, "error", "Fail 1") + + # Modify created_at to be in the past to satisfy backoff + with Core.Database.get_connection() as conn: + conn.execute( + "UPDATE queue SET created_at = ? WHERE id = ?", + ( + ( + datetime.now(tz=timezone.utc) - timedelta(minutes=5) + ).isoformat(), + self.job_id, + ), + ) + conn.commit() + + process_retryable_jobs() + + job = Core.Database.get_job_by_id(self.job_id) + self.assertIsNotNone(job) + if job: + self.assertEqual(job["status"], "pending") + + def test_process_retryable_jobs_not_ready(self) -> None: + """Test that jobs are not retried before backoff period.""" + # Set up a retryable job that just failed + Core.Database.update_job_status(self.job_id, "error", "Fail 1") + + # created_at is now, so backoff should prevent retry + process_retryable_jobs() + + job = Core.Database.get_job_by_id(self.job_id) + self.assertIsNotNone(job) + if job: + self.assertEqual(job["status"], "error") + + +class TestTextChunking(Test.TestCase): + """Test text chunking edge cases.""" + + def test_split_text_single_long_word(self) -> None: + """Handle text with a single word exceeding limit.""" + long_word = "a" * 4000 + chunks = split_text_into_chunks(long_word, max_chars=3000) + + # Should keep it as one chunk or split? + # The current implementation does not split words + self.assertEqual(len(chunks), 1) + self.assertEqual(len(chunks[0]), 4000) + + def test_split_text_no_sentence_boundaries(self) -> None: + """Handle long text with no sentence boundaries.""" + text = "word " * 1000 # 5000 chars + chunks = split_text_into_chunks(text, max_chars=3000) + + # Should keep it as one chunk as it can't split by ". " + self.assertEqual(len(chunks), 1) + self.assertGreater(len(chunks[0]), 3000) + + def test() -> None: """Run the tests.""" Test.run( @@ -2051,6 +2162,8 @@ def test() -> None: TestTextToSpeech, TestMemoryEfficiency, TestJobProcessing, + TestWorkerErrorHandling, + TestTextChunking, ], ) |
