Files
cowork-local/tests/unit/test_task_repository.py
T
vudt15andClaude Sonnet 5 69ab8e125b feat(R07): task repository, schedule calculator, Qt clock adapter, task/AI-planner services
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>
2026-08-27 17:27:53 +09:00

71 lines
2.4 KiB
Python

"""EPIC R07-T01: TaskRepository + core/tasks.py::save_task atomic write.
The motivating bug: ``core/tasks.py::save_task`` used to
``path.write_text(json.dumps(...))`` — two syscalls, no atomicity, same class
of bug already fixed for projects/conversations at R06-T02. A failure between
the write and the replace must never leave a half-written task JSON file on
disk; that is the one property the crash-injection test exists to pin.
"""
from __future__ import annotations
import json
import pytest
from cowork_local.core.tasks import new_task
from cowork_local.infrastructure.persistence.json import TaskRepository
def test_task_repository_crud_round_trip(tmp_path):
repo = TaskRepository(tmp_path)
task = repo.create("My Task")
repo.save(task)
assert [t["task_id"] for t in repo.list()] == [task["task_id"]]
assert repo.get(task["task_id"])["title"] == "My Task"
task["title"] = "Renamed"
repo.save(task)
assert repo.get(task["task_id"])["title"] == "Renamed"
repo.delete(task["task_id"])
assert repo.get(task["task_id"]) is None
assert repo.list() == []
def test_task_repository_duplicate_keeps_config_resets_identity(tmp_path):
repo = TaskRepository(tmp_path)
task = repo.create("Original", description="d")
repo.save(task)
dup = repo.duplicate(task)
repo.save(dup)
assert dup["task_id"] != task["task_id"]
assert dup["description"] == "d"
assert {t["task_id"] for t in repo.list()} == {task["task_id"], dup["task_id"]}
def test_save_task_never_corrupts_existing_file_on_crash(tmp_path, monkeypatch):
"""Same guarantee as ``test_atomic_write_and_repositories.py``'s crash
test, exercised through ``core/tasks.py::save_task`` directly (not just
through the repository) since that is the function every existing
scheduler/executor call site still uses."""
from cowork_local.core.tasks import save_task, task_path
task = new_task("Stable")
save_task(task, tmp_path)
import cowork_local.infrastructure.persistence.json.atomic_write as mod
def boom(*_a, **_k):
raise OSError("simulated crash between write and replace")
monkeypatch.setattr(mod.os, "replace", boom)
task["title"] = "Corrupted?"
with pytest.raises(OSError):
save_task(task, tmp_path)
on_disk = json.loads(task_path(task["task_id"], tmp_path).read_text(encoding="utf-8"))
assert on_disk["title"] == "Stable"