Team Hoa, EPIC R07 (Scheduling & Workflow Runtime) - Team Hoa scope only (R07-T01 -> T05; R07-T06 Co4EWorkflowService is Team Nam's). - R07-T01: infrastructure/persistence/json/task_repository_impl.py wraps core/tasks.py's CRUD; core/tasks.py::save_task now writes through atomic_write.write_json (same durability fix as R06-T02, save_task was still doing a plain write_text). - R07-T02: domain/tasks/schedule_calculator.py::ScheduleCalculator - the cron/interval/daily/weekly/monthly due-time math extracted from core/tasks.py, pure Python with is_holiday/make_cron injected so domain/ never imports core (ADR-001 I2). core/tasks.py keeps its old function names as thin wrappers so every existing caller is unchanged. This was previously untested; now has its own unit suite. - R07-T03: infrastructure/qt/qt_scheduler_clock.py::QtSchedulerClock wraps the QTimer TaskScheduler used to own directly, injected via a new `clock=` constructor param (defaults to a real one). Originally planned at platform/qt/... ; moved after confirming that name shadows the stdlib platform module (used by core/windows_sandbox_vm.py, core/appcontainer_sandbox.py) whenever the repo root is on sys.path. tests/fakes/fake_clock.py lets scheduler dispatch be tested tick-by-tick with no Qt event loop. - R07-T04: application/scheduling/task_application_service.py centralizes run_now/duplicate/pause/delete/bulk_delete and the Kanban drag-drop business rules (move_to_status), currently only reachable by driving the real ui/schedule_task_tab.py widget. - R07-T05: application/scheduling/ai_task_planner_service.py wraps core/ai_task_planner.py::plan_tasks and core/task_import.py::import_tasks as a seam, plus the attachment-stamping step that used to only exist inside the AI-create dialog's worker closure. pytest: 328 pass (same 4 pre-existing failures as the R05/R06 baseline, unrelated to this work - see docs/refactor/BaoCao_TeamHoa_R05_R06.md). scripts/check_imports.py: PASS. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
192 lines
6.1 KiB
Python
192 lines
6.1 KiB
Python
"""EPIC R07-T04: TaskApplicationService — CRUD + dispatch rules, no Qt.
|
|
|
|
Everything here used to be exercised only by driving the real
|
|
``ui/schedule_task_tab.py`` widget (a QListWidget drag gesture, a QMenu
|
|
click). These tests drive the same rules directly through the service.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from cowork_local.application.scheduling.task_application_service import (
|
|
TaskApplicationService,
|
|
)
|
|
from cowork_local.infrastructure.persistence.json import TaskRepository
|
|
|
|
|
|
def _service(tmp_path, run_now=None):
|
|
return TaskApplicationService(TaskRepository(tmp_path), run_now=run_now)
|
|
|
|
|
|
def _new_saved_task(repo: TaskRepository, **overrides):
|
|
task = repo.create("T", **overrides)
|
|
repo.save(task)
|
|
return task
|
|
|
|
|
|
def test_run_now_rejects_manual_task_type(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo, task_type="manual")
|
|
service = TaskApplicationService(repo, run_now=lambda tid: True)
|
|
|
|
result = service.run_now(task["task_id"])
|
|
|
|
assert result.ok is False
|
|
assert result.reason == "manual_task"
|
|
|
|
|
|
def test_run_now_without_scheduler_wired_reports_no_scheduler(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo, task_type="cowork")
|
|
service = TaskApplicationService(repo, run_now=None)
|
|
|
|
result = service.run_now(task["task_id"])
|
|
|
|
assert result.ok is False
|
|
assert result.reason == "no_scheduler"
|
|
|
|
|
|
def test_run_now_delegates_to_injected_scheduler(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo, task_type="cowork")
|
|
seen = []
|
|
service = TaskApplicationService(repo, run_now=lambda tid: seen.append(tid) or True)
|
|
|
|
result = service.run_now(task["task_id"])
|
|
|
|
assert result.ok is True
|
|
assert seen == [task["task_id"]]
|
|
|
|
|
|
def test_run_now_missing_task_reports_not_found(tmp_path):
|
|
service = _service(tmp_path, run_now=lambda tid: True)
|
|
result = service.run_now("does-not-exist")
|
|
assert result.ok is False
|
|
assert result.reason == "not_found"
|
|
|
|
|
|
def test_duplicate_saves_a_copy_with_fresh_identity(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo, description="d")
|
|
service = TaskApplicationService(repo)
|
|
|
|
dup = service.duplicate(task["task_id"])
|
|
|
|
assert dup is not None
|
|
assert dup["task_id"] != task["task_id"]
|
|
assert dup["description"] == "d"
|
|
assert repo.get(dup["task_id"]) is not None # actually persisted, not just returned
|
|
|
|
|
|
def test_toggle_pause_then_resume_goes_to_backlog(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo)
|
|
service = TaskApplicationService(repo)
|
|
|
|
paused = service.toggle_pause(task["task_id"])
|
|
assert paused["status"] == "paused"
|
|
|
|
resumed = service.toggle_pause(task["task_id"])
|
|
assert resumed["status"] == "backlog"
|
|
|
|
|
|
def test_delete_reports_whether_the_task_existed(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo)
|
|
service = TaskApplicationService(repo)
|
|
|
|
assert service.delete(task["task_id"]) is True
|
|
assert repo.get(task["task_id"]) is None
|
|
assert service.delete(task["task_id"]) is False # already gone
|
|
|
|
|
|
def test_bulk_delete_counts_only_tasks_that_existed(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
a = _new_saved_task(repo)
|
|
b = _new_saved_task(repo)
|
|
service = TaskApplicationService(repo)
|
|
|
|
count = service.bulk_delete([a["task_id"], b["task_id"], "ghost-id"])
|
|
|
|
assert count == 2
|
|
assert repo.list() == []
|
|
|
|
|
|
def test_move_to_status_running_task_is_blocked(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo)
|
|
task["status"] = "running"
|
|
repo.save(task)
|
|
service = TaskApplicationService(repo)
|
|
|
|
result = service.move_to_status(task["task_id"], "backlog")
|
|
|
|
assert result.blocked is True
|
|
assert repo.get(task["task_id"])["status"] == "running" # untouched
|
|
|
|
|
|
def test_move_to_status_running_lane_dispatches_run_now(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo, task_type="cowork")
|
|
seen = []
|
|
service = TaskApplicationService(repo, run_now=lambda tid: seen.append(tid) or True)
|
|
|
|
result = service.move_to_status(task["task_id"], "running")
|
|
|
|
assert result.ran_now is True
|
|
assert result.run_now_result.ok is True
|
|
assert seen == [task["task_id"]]
|
|
|
|
|
|
def test_move_to_status_done_disables_the_schedule(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo)
|
|
task["schedule"]["enabled"] = True
|
|
task["schedule"]["run_at"] = "2026-08-28 09:00"
|
|
repo.save(task)
|
|
service = TaskApplicationService(repo)
|
|
|
|
result = service.move_to_status(task["task_id"], "done")
|
|
|
|
assert result.task["status"] == "done"
|
|
assert result.task["schedule"]["enabled"] is False
|
|
assert repo.get(task["task_id"])["schedule"]["enabled"] is False
|
|
|
|
|
|
def test_move_to_status_scheduled_without_run_at_needs_schedule(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo) # fresh task has schedule.run_at = None
|
|
service = TaskApplicationService(repo)
|
|
|
|
result = service.move_to_status(task["task_id"], "scheduled")
|
|
|
|
assert result.needs_schedule is True
|
|
assert repo.get(task["task_id"])["status"] == "scheduled"
|
|
assert repo.get(task["task_id"])["schedule"]["enabled"] is False
|
|
|
|
|
|
def test_move_to_status_scheduled_with_run_at_enables_it(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo)
|
|
task["schedule"]["run_at"] = "2026-08-28 09:00"
|
|
repo.save(task)
|
|
service = TaskApplicationService(repo)
|
|
|
|
result = service.move_to_status(task["task_id"], "scheduled")
|
|
|
|
assert result.needs_schedule is False
|
|
assert repo.get(task["task_id"])["schedule"]["enabled"] is True
|
|
|
|
|
|
def test_move_to_status_plain_change(tmp_path):
|
|
repo = TaskRepository(tmp_path)
|
|
task = _new_saved_task(repo)
|
|
service = TaskApplicationService(repo)
|
|
|
|
result = service.move_to_status(task["task_id"], "backlog")
|
|
|
|
assert result.task["status"] == "backlog"
|
|
|
|
|
|
def test_move_to_status_missing_task_returns_none(tmp_path):
|
|
service = _service(tmp_path)
|
|
assert service.move_to_status("does-not-exist", "backlog") is None
|