156 lines
7.4 KiB
Python
156 lines
7.4 KiB
Python
from concurrent.futures import ThreadPoolExecutor
|
|
from datetime import datetime, timezone
|
|
|
|
import pytest
|
|
|
|
from dx_productivity.dsh.scheduling import ScheduleStore
|
|
from dx_productivity.trial_task_center.errors import TaskCenterError
|
|
from dx_productivity.trial_task_center.models import TaskStatus
|
|
from test_run_journal import project_service, evidence
|
|
|
|
|
|
NOW = datetime(2026, 10, 30, 12, tzinfo=timezone.utc)
|
|
|
|
|
|
def spec(ids, **extra):
|
|
return {'task_ids': ids, 'priority': 'normal', 'not_before': '2026-10-30T20:00:00+08:00', **extra}
|
|
|
|
|
|
def test_save_normalizes_utc_and_preserves_frozen_scope_across_restart(project_service):
|
|
_, _, data, ids, hashes = project_service
|
|
row = ScheduleStore(data).save(spec(ids), hashes)
|
|
assert row['not_before'] == '2026-10-30T12:00:00+00:00'
|
|
assert row['status'] == 'pending' and row['auto_start'] is False and row['idle_only'] is True
|
|
assert ScheduleStore(data).get(row['schedule_id']) == row
|
|
|
|
|
|
def test_preview_normalizes_native_draft_without_persisting_or_granting_authority(project_service):
|
|
_, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
result = schedules.preview(spec(ids, auto_start=True), hashes)
|
|
assert result == {'task_ids': ids, 'priority': 'normal', 'not_before': '2026-10-30T12:00:00+00:00',
|
|
'due_at': None, 'dependencies': [], 'auto_start': True, 'idle_only': True, 'title': ''}
|
|
assert schedules.list_schedules() == []
|
|
|
|
|
|
@pytest.mark.parametrize('extra', [{'confirmed': True}, {'priority': 'highest'}, {'not_before': '2026-10-30T12:00:00'},
|
|
{'auto_start': 'true'}, {'dependencies': ['../bad']}, {'task_ids': []}, {'title': '\x00bad'},
|
|
{'due_at': '2026-10-29T00:00:00Z'}])
|
|
def test_save_rejects_untrusted_or_invalid_specs(project_service, extra):
|
|
_, _, data, ids, hashes = project_service
|
|
with pytest.raises(TaskCenterError):
|
|
ScheduleStore(data).save(spec(ids, **extra), hashes)
|
|
|
|
|
|
def test_due_order_and_manual_confirmation_without_execution(project_service):
|
|
service, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
normal = schedules.save(spec(ids), hashes)
|
|
urgent = schedules.save(spec(ids, priority='urgent', auto_start=True), hashes)
|
|
rows = schedules.evaluate(service, now=NOW)
|
|
assert [row['schedule_id'] for row in rows] == [urgent['schedule_id'], normal['schedule_id']]
|
|
assert rows[0]['ready'] and rows[0]['requires_confirmation'] is True
|
|
assert rows[0]['dispatchable'] is False # model-supplied auto_start never grants authority
|
|
assert not list(data.rglob('command.tsv'))
|
|
assert all('worker_busy' in row['blockers'] for row in schedules.evaluate(service, now=NOW, busy=True))
|
|
|
|
|
|
def test_future_and_changed_plans_do_not_become_ready(project_service):
|
|
service, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
schedules.save(spec(ids, not_before='2026-10-31T12:00:00Z'), hashes)
|
|
assert 'not_due' in schedules.evaluate(service, now=NOW)[0]['blockers']
|
|
rule = service.rules(ids[0])[0]
|
|
service.update_rule(rule.rule_id, old_part_number='OLD', new_part_number='CHANGED', enabled=True)
|
|
assert 'plan_changed' in schedules.evaluate(service, now=NOW)[0]['blockers']
|
|
|
|
|
|
def test_preflight_blocks_missing_project_and_unreconciled_saved_result(project_service):
|
|
service, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
schedules.save(spec(ids), hashes)
|
|
evidence(service, ids[0], 'batch_saved_unverified', '')
|
|
row = schedules.evaluate(service, now=NOW)[0]
|
|
assert not row['ready'] and 'preflight_blocked' in row['blockers']
|
|
service.task_context(ids[1]).project.path.unlink()
|
|
assert not schedules.evaluate(service, now=NOW)[0]['ready']
|
|
|
|
|
|
def test_paused_task_requires_explicit_manual_resume(project_service):
|
|
service, store, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
schedules.save(spec(ids, auto_start=True), hashes)
|
|
store.set_task_status(ids[0], TaskStatus.READY)
|
|
store.set_task_status(ids[0], TaskStatus.RUNNING)
|
|
store.set_task_status(ids[0], TaskStatus.PAUSED)
|
|
row = schedules.evaluate(service, now=NOW)[0]
|
|
assert not row['ready'] and 'task_paused' in row['blockers']
|
|
|
|
|
|
def test_multiple_schedules_for_a_task_cannot_hide_dependency_cycle(project_service):
|
|
_, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
schedules.save(spec(ids[:1], dependencies=ids[1:2]), {ids[0]: hashes[ids[0]]})
|
|
schedules.save(spec(ids[:1], dependencies=[]), {ids[0]: hashes[ids[0]]})
|
|
with pytest.raises(TaskCenterError):
|
|
schedules.save(spec(ids[1:2], dependencies=ids[:1]), {ids[1]: hashes[ids[1]]})
|
|
|
|
|
|
def test_dependencies_require_real_completion_and_verified_evidence(project_service):
|
|
service, store, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
schedules.save(spec(ids[:1], dependencies=ids[1:2]), {ids[0]: hashes[ids[0]]})
|
|
assert 'dependency_incomplete' in schedules.evaluate(service, now=NOW)[0]['blockers']
|
|
store.set_task_status(ids[1], TaskStatus.READY)
|
|
store.set_task_status(ids[1], TaskStatus.RUNNING)
|
|
store.set_task_status(ids[1], TaskStatus.COMPLETED)
|
|
assert 'dependency_incomplete' in schedules.evaluate(service, now=NOW)[0]['blockers']
|
|
evidence(service, ids[1])
|
|
assert schedules.evaluate(service, now=NOW)[0]['ready']
|
|
evidence(service, ids[1], 'batch_saved_unverified', '')
|
|
assert 'dependency_incomplete' in schedules.evaluate(service, now=NOW)[0]['blockers']
|
|
|
|
|
|
def test_self_dependency_and_cross_schedule_cycles_are_rejected(project_service):
|
|
_, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
with pytest.raises(TaskCenterError):
|
|
schedules.save(spec(ids[:1], dependencies=ids[:1]), {ids[0]: hashes[ids[0]]})
|
|
schedules.save(spec(ids[:1], dependencies=ids[1:2]), {ids[0]: hashes[ids[0]]})
|
|
with pytest.raises(TaskCenterError):
|
|
schedules.save(spec(ids[1:2], dependencies=ids[:1]), {ids[1]: hashes[ids[1]]})
|
|
|
|
|
|
def test_claim_is_exclusive_and_never_replayed_after_restart(project_service):
|
|
service, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
row = schedules.save(spec(ids), hashes)
|
|
def claim(_):
|
|
try:
|
|
return ScheduleStore(data).claim(row['schedule_id'])
|
|
except TaskCenterError:
|
|
return None
|
|
with ThreadPoolExecutor(max_workers=4) as pool:
|
|
assert sum(value is not None for value in pool.map(claim, range(4))) == 1
|
|
reopened = ScheduleStore(data)
|
|
assert reopened.get(row['schedule_id'])['status'] == 'claimed'
|
|
assert 'already_claimed' in reopened.evaluate(service, now=NOW)[0]['blockers']
|
|
with pytest.raises(TaskCenterError):
|
|
reopened.claim(row['schedule_id'])
|
|
reopened.finish(row['schedule_id'], 'completed', run_id='a' * 32)
|
|
assert reopened.get(row['schedule_id'])['run_id'] == 'a' * 32
|
|
|
|
|
|
def test_cancel_is_durable_and_cannot_cancel_an_owned_claim(project_service):
|
|
_, _, data, ids, hashes = project_service
|
|
schedules = ScheduleStore(data)
|
|
row = schedules.save(spec(ids), hashes)
|
|
schedules.cancel(row['schedule_id'])
|
|
assert ScheduleStore(data).get(row['schedule_id'])['status'] == 'cancelled'
|
|
with pytest.raises(TaskCenterError):
|
|
schedules.claim(row['schedule_id'])
|
|
claimed = schedules.save(spec(ids), hashes)
|
|
schedules.claim(claimed['schedule_id'])
|
|
with pytest.raises(TaskCenterError):
|
|
schedules.cancel(claimed['schedule_id'])
|