Unify read-only quick-run evidence snapshots and protocol models
This commit is contained in:
1 parent
42e5736ec6
commit
1df3f0cf30
4 files changed
+329
-228
No files matched your search
@@ -0,0 +1,78 @@
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
from dx_productivity.trial_task_center.models import ProjectRecord, ProjectStatus, RuleRecord
|
||||
from dx_productivity.trial_task_center.quick_run import QuickRunPreparer
|
||||
|
||||
|
||||
def fixture_queue(tmp_path):
|
||||
script = tmp_path / 'runtime.vbs'
|
||||
script.write_text("' fixture", encoding='ascii')
|
||||
preparer = QuickRunPreparer(tmp_path, script_source=script, run_token_factory=lambda: 'snapshot')
|
||||
project = ProjectRecord('project', 'task', 'One', Path('C:/one.prj'), Path('C:/'), True, True, ProjectStatus.PENDING, 0)
|
||||
rule = RuleRecord('rule', 'task', 0, 'OLD', 'NEW', True)
|
||||
handoff = preparer.prepare('task', (project,), (rule,))
|
||||
job = handoff.command_path.read_text(encoding='utf-8').splitlines()[1].split('\t')[1]
|
||||
return preparer, project, rule, handoff, job
|
||||
|
||||
|
||||
def test_snapshot_reads_each_file_once_and_uses_same_status(tmp_path, monkeypatch):
|
||||
preparer, project, rule, handoff, job = fixture_queue(tmp_path)
|
||||
handoff.status_path.write_text(f'job_id\tstate\tcode\tmessage\n{job}\tbatch_saved_unverified\t\tverify\n', encoding='utf-8')
|
||||
opens = []
|
||||
original = Path.open
|
||||
def tracked(path, *args, **kwargs):
|
||||
if path.parent == handoff.command_path.parent:
|
||||
opens.append(path.name)
|
||||
return original(path, *args, **kwargs)
|
||||
monkeypatch.setattr(Path, 'open', tracked)
|
||||
snapshot = preparer.read_snapshot('task', (project,), (rule,))
|
||||
assert opens.count('command.tsv') == opens.count('status.tsv') == 1
|
||||
assert snapshot.evidence_path == handoff.command_path.parent
|
||||
assert snapshot.has_evidence and snapshot.interrupted
|
||||
assert snapshot.progress.saved_items == 0
|
||||
assert snapshot.progress.failed_items == 1
|
||||
|
||||
|
||||
def test_snapshot_cancellation_stops_before_next_file_and_never_writes(tmp_path, monkeypatch):
|
||||
preparer, project, rule, handoff, job = fixture_queue(tmp_path)
|
||||
original = Path.open
|
||||
class Cancelled(Exception):
|
||||
pass
|
||||
class Control:
|
||||
cancelled = False
|
||||
def checkpoint(self):
|
||||
if self.cancelled:
|
||||
raise Cancelled
|
||||
control = Control()
|
||||
opened = []
|
||||
def tracked(path, *args, **kwargs):
|
||||
opened.append(path.name)
|
||||
result = original(path, *args, **kwargs)
|
||||
if path == handoff.command_path:
|
||||
control.cancelled = True
|
||||
return result
|
||||
monkeypatch.setattr(Path, 'open', tracked)
|
||||
with pytest.raises(Cancelled):
|
||||
preparer.read_snapshot('task', (project,), (rule,), read_control=control)
|
||||
assert opened == ['command.tsv']
|
||||
|
||||
|
||||
@pytest.mark.parametrize('state', ['alien_state', 'awaiting_native_replace'])
|
||||
def test_snapshot_unknown_states_do_not_complete_or_save(tmp_path, state):
|
||||
preparer, project, rule, handoff, job = fixture_queue(tmp_path)
|
||||
text = f'job_id\tstate\tcode\tmessage\n{job}\t{state}\t\tunknown\n'
|
||||
handoff.status_path.write_text(text, encoding='utf-8')
|
||||
snapshot = preparer.read_snapshot('task', (project,), (rule,))
|
||||
assert not snapshot.progress.queue_complete
|
||||
assert snapshot.progress.saved_items == 0
|
||||
assert snapshot.interrupted == (state == 'awaiting_native_replace')
|
||||
assert handoff.status_path.read_text(encoding='utf-8') == text
|
||||
|
||||
|
||||
def test_snapshot_corruption_retains_evidence_without_claiming_progress(tmp_path):
|
||||
preparer, project, rule, handoff, job = fixture_queue(tmp_path)
|
||||
handoff.status_path.write_text('corrupt', encoding='utf-8')
|
||||
snapshot = preparer.read_snapshot('task', (project,), (rule,))
|
||||
assert snapshot.has_evidence
|
||||
assert snapshot.progress.completed_items == 0
|
||||
assert snapshot.phase == 'selection'
|
||||
Reference in new issue
Block a user