import json from pathlib import Path import sys import subprocess import pytest from dx_productivity.trial_task_center.project_session import ProjectSessionWorker, ProjectSessionScope from dx_productivity.trial_task_center.worker_monitor import ProgressWatch, WorkerMonitorError def toy_worker(root): script = root / 'toy_session.py' script.write_text('''import pathlib,sys,time,json,os root=pathlib.Path(sys.argv[1]); initial=sys.argv[4]; monitor=sys.argv[5] while not (root/f'monitor-{monitor}.ready').exists():time.sleep(.005) session=(root/'project-session.request').read_text(encoding='utf-8').split('\\t')[0] job=initial; sequence=0 with (root/'starts.txt').open('a') as f:f.write(str(os.getpid())+'\\n') while True: sequence+=1 print(f'DX_PROGRESS\\t{monitor}\\t{initial}\\t{sequence}\\tsession_wait\\t1\\t1\\t1\\t{job}',flush=True) (root/'project-session-result.tsv').write_text(f'{session}\\t{job}\\tsaved_ok\\tsession_verified',encoding='utf-8') with (root/'steps.txt').open('a') as f:f.write(job+'\\n') while not (root/'project-session-next.tsv').exists():time.sleep(.005) # Match the real VBS continuation reader's bounded sharing-denial retry. for attempt in range(20): try: token,job=(root/'project-session-next.tsv').read_text().split('\\t') break except (PermissionError,FileNotFoundError):time.sleep(.05) else:raise RuntimeError('control read remained unavailable') (root/'project-session-next.tsv').unlink() if token!=session:sys.exit(8) if job=='close':break (root/'closed.txt').write_text('closed') ''', encoding='utf-8') return (sys.executable, '-u', str(script), str(root), sys.executable, '', 'job1') def test_same_project_steps_retain_one_supervised_worker_and_close(tmp_path): command = toy_worker(tmp_path) session = ProjectSessionWorker(tmp_path / '工程.prj', tmp_path) try: assert session.run_step(command, 3).returncode == 0 assert session.run_step((*command[:-1], 'job2'), 3).returncode == 0 finally: session.close() assert (tmp_path / 'closed.txt').exists() assert (tmp_path / 'steps.txt').read_text().splitlines() == ['job1', 'job2'] assert len((tmp_path / 'starts.txt').read_text().splitlines()) == 1 assert not (tmp_path / 'project-session.request').exists() def test_session_progress_keeps_monitor_identity_and_resets_each_step(): watch = ProgressWatch('token', 'first', 10, 0) first = b'DX_PROGRESS\ttoken\tfirst\t1\topen_project\t0\t1\t1\tfirst\n' second = b'DX_PROGRESS\ttoken\tfirst\t2\tnative_replace\t0\t1\t0\tsecond\n' assert watch.accept(first, 1) assert watch.accept(second, 5) value = watch.snapshot(now=6) assert value['job_id'] == 'second' assert value['stage_seconds'] == {'native_replace': 1} assert value['elapsed_seconds'] == 1 assert not watch.accept(second.replace(b'\tfirst\t2', b'\tforeign\t3'), 6) def test_real_vbs_session_authorization_fails_before_cad_for_missing_project(tmp_path): script = Path(__file__).resolve().parents[2] / 'integration/dxdesigner795/QuickPartReplace.vbs' project = tmp_path / 'missing.prj' (tmp_path / 'command.tsv').write_text(f'job_id\tproject_path\njob\t{project}\n') (tmp_path / 'batch.request').write_text('job') (tmp_path / 'project-session.request').write_text(f'{"a"*32}\t{project}\tjob') result = subprocess.run((r'C:\Windows\SysWOW64\cscript.exe', '//nologo', str(script), str(tmp_path), sys.executable, '', 'job'), capture_output=True, timeout=10, text=True) assert result.returncode == 2 assert 'confirmed project file does not exist' in result.stderr def test_switching_project_closes_previous_worker_before_starting_next(tmp_path): roots = [tmp_path / 'first', tmp_path / 'second'] for root in roots: root.mkdir() with ProjectSessionScope(None, enabled=True) as scope: assert scope.run_step(roots[0] / 'a.prj', toy_worker(roots[0]), 3).returncode == 0 scope.prepare(roots[1] / 'b.prj') assert (roots[0] / 'closed.txt').exists() assert scope.run_step(roots[1] / 'b.prj', toy_worker(roots[1]), 3).returncode == 0 assert (roots[1] / 'closed.txt').exists() def test_wrong_session_checkpoint_cannot_release_next_step(tmp_path): command = toy_worker(tmp_path) script = Path(command[2]) script.write_text(script.read_text(encoding='utf-8').replace( "f'{session}\\t{job}\\tsaved_ok\\tsession_verified'", "f'wrong-token\\t{job}\\tsaved_ok\\tsession_verified'").replace( "while not (root/'project-session-next.tsv').exists():time.sleep(.005)", 'sys.exit(0)'), encoding='utf-8') session = ProjectSessionWorker(tmp_path / 'a.prj', tmp_path) try: with pytest.raises(WorkerMonitorError, match='before authorized closing'): session.run_step(command, 3) finally: session.close() def test_completed_failure_is_delivered_and_next_step_starts_fresh_worker(tmp_path): command = toy_worker(tmp_path) script = Path(command[2]) script.write_text(script.read_text(encoding='utf-8').replace( 'job=initial; sequence=0', "job=initial; sequence=0\nif job=='failed':sys.exit(7)"), encoding='utf-8') project = tmp_path / 'a.prj' with ProjectSessionScope(None, enabled=True) as scope: failed = scope.run_step(project, (*command[:-1], 'failed'), 3) assert failed.returncode == 7 previous = scope.worker assert scope.run_step(project, command, 3).returncode == 0 assert scope.worker is not previous assert (tmp_path / 'closed.txt').exists()