|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
10 | | -from cron.scheduler import _resolve_origin, _resolve_delivery_target, _deliver_result, run_job |
| 10 | +from cron.scheduler import _resolve_origin, _resolve_delivery_target, _deliver_result, run_job, SILENT_MARKER |
11 | 11 |
|
12 | 12 |
|
13 | 13 | class TestResolveOrigin: |
@@ -449,3 +449,97 @@ def _skill_view(name): |
449 | 449 | assert "Instructions for blogwatcher." in prompt_arg |
450 | 450 | assert "Instructions for find-nearby." in prompt_arg |
451 | 451 | assert "Combine the results." in prompt_arg |
| 452 | + |
| 453 | + |
| 454 | +class TestSilentDelivery: |
| 455 | + """Verify that [SILENT] responses suppress delivery while still saving output.""" |
| 456 | + |
| 457 | + def _make_job(self): |
| 458 | + return { |
| 459 | + "id": "monitor-job", |
| 460 | + "name": "monitor", |
| 461 | + "deliver": "origin", |
| 462 | + "origin": {"platform": "telegram", "chat_id": "123"}, |
| 463 | + } |
| 464 | + |
| 465 | + def test_normal_response_delivers(self): |
| 466 | + with patch("cron.scheduler.get_due_jobs", return_value=[self._make_job()]), \ |
| 467 | + patch("cron.scheduler.run_job", return_value=(True, "# output", "Results here", None)), \ |
| 468 | + patch("cron.scheduler.save_job_output", return_value="/tmp/out.md"), \ |
| 469 | + patch("cron.scheduler._deliver_result") as deliver_mock, \ |
| 470 | + patch("cron.scheduler.mark_job_run"): |
| 471 | + from cron.scheduler import tick |
| 472 | + tick(verbose=False) |
| 473 | + deliver_mock.assert_called_once() |
| 474 | + |
| 475 | + def test_silent_response_suppresses_delivery(self, caplog): |
| 476 | + with patch("cron.scheduler.get_due_jobs", return_value=[self._make_job()]), \ |
| 477 | + patch("cron.scheduler.run_job", return_value=(True, "# output", "[SILENT]", None)), \ |
| 478 | + patch("cron.scheduler.save_job_output", return_value="/tmp/out.md"), \ |
| 479 | + patch("cron.scheduler._deliver_result") as deliver_mock, \ |
| 480 | + patch("cron.scheduler.mark_job_run"): |
| 481 | + from cron.scheduler import tick |
| 482 | + with caplog.at_level(logging.INFO, logger="cron.scheduler"): |
| 483 | + tick(verbose=False) |
| 484 | + deliver_mock.assert_not_called() |
| 485 | + assert any(SILENT_MARKER in r.message for r in caplog.records) |
| 486 | + |
| 487 | + def test_silent_with_note_suppresses_delivery(self): |
| 488 | + with patch("cron.scheduler.get_due_jobs", return_value=[self._make_job()]), \ |
| 489 | + patch("cron.scheduler.run_job", return_value=(True, "# output", "[SILENT] No changes detected", None)), \ |
| 490 | + patch("cron.scheduler.save_job_output", return_value="/tmp/out.md"), \ |
| 491 | + patch("cron.scheduler._deliver_result") as deliver_mock, \ |
| 492 | + patch("cron.scheduler.mark_job_run"): |
| 493 | + from cron.scheduler import tick |
| 494 | + tick(verbose=False) |
| 495 | + deliver_mock.assert_not_called() |
| 496 | + |
| 497 | + def test_silent_is_case_insensitive(self): |
| 498 | + with patch("cron.scheduler.get_due_jobs", return_value=[self._make_job()]), \ |
| 499 | + patch("cron.scheduler.run_job", return_value=(True, "# output", "[silent] nothing new", None)), \ |
| 500 | + patch("cron.scheduler.save_job_output", return_value="/tmp/out.md"), \ |
| 501 | + patch("cron.scheduler._deliver_result") as deliver_mock, \ |
| 502 | + patch("cron.scheduler.mark_job_run"): |
| 503 | + from cron.scheduler import tick |
| 504 | + tick(verbose=False) |
| 505 | + deliver_mock.assert_not_called() |
| 506 | + |
| 507 | + def test_failed_job_always_delivers(self): |
| 508 | + """Failed jobs deliver regardless of [SILENT] in output.""" |
| 509 | + with patch("cron.scheduler.get_due_jobs", return_value=[self._make_job()]), \ |
| 510 | + patch("cron.scheduler.run_job", return_value=(False, "# output", "", "some error")), \ |
| 511 | + patch("cron.scheduler.save_job_output", return_value="/tmp/out.md"), \ |
| 512 | + patch("cron.scheduler._deliver_result") as deliver_mock, \ |
| 513 | + patch("cron.scheduler.mark_job_run"): |
| 514 | + from cron.scheduler import tick |
| 515 | + tick(verbose=False) |
| 516 | + deliver_mock.assert_called_once() |
| 517 | + |
| 518 | + def test_output_saved_even_when_delivery_suppressed(self): |
| 519 | + with patch("cron.scheduler.get_due_jobs", return_value=[self._make_job()]), \ |
| 520 | + patch("cron.scheduler.run_job", return_value=(True, "# full output", "[SILENT]", None)), \ |
| 521 | + patch("cron.scheduler.save_job_output") as save_mock, \ |
| 522 | + patch("cron.scheduler._deliver_result") as deliver_mock, \ |
| 523 | + patch("cron.scheduler.mark_job_run"): |
| 524 | + save_mock.return_value = "/tmp/out.md" |
| 525 | + from cron.scheduler import tick |
| 526 | + tick(verbose=False) |
| 527 | + save_mock.assert_called_once_with("monitor-job", "# full output") |
| 528 | + deliver_mock.assert_not_called() |
| 529 | + |
| 530 | + |
| 531 | +class TestBuildJobPromptSilentHint: |
| 532 | + """Verify _build_job_prompt always injects [SILENT] guidance.""" |
| 533 | + |
| 534 | + def test_hint_always_present(self): |
| 535 | + from cron.scheduler import _build_job_prompt |
| 536 | + job = {"prompt": "Check for updates"} |
| 537 | + result = _build_job_prompt(job) |
| 538 | + assert "[SILENT]" in result |
| 539 | + assert "Check for updates" in result |
| 540 | + |
| 541 | + def test_hint_present_even_without_prompt(self): |
| 542 | + from cron.scheduler import _build_job_prompt |
| 543 | + job = {"prompt": ""} |
| 544 | + result = _build_job_prompt(job) |
| 545 | + assert "[SILENT]" in result |
0 commit comments