""" tests/test_worker_shipping.py 用途:验证 Shipping Worker 在 Windows spawn 模式下使用启动它的虚拟环境解释器与统一自动标注队列。 依赖:worker_shipping、pytest monkeypatch。 逻辑:避免 multiprocessing 子进程回退到系统 Python;Worker 不再持有已删除的 XFA 专用 OCR 启动门禁。 """ from __future__ import annotations import hashlib import sys from pathlib import Path import pytest import worker_shipping from app.jobs.types import JobType from app.services.shipping_notice import queue_service pytestmark = pytest.mark.pure_unit def test_configure_shipping_worker_process_executable_uses_current_interpreter(monkeypatch): """子进程解释器必须与 Worker 主进程一致,防止环境混用。""" recorded: list[str] = [] monkeypatch.setattr(worker_shipping.mp, 'set_executable', recorded.append) worker_shipping.configure_shipping_worker_process_executable() # 关键断言:Windows spawn 子进程不能回退到 PATH 上的系统 Python。 assert recorded == [sys.executable] def test_shipping_worker_does_not_depend_on_removed_xfa_ocr_startup_gate(): """统一 PDF 自动标注由 shipping 队列消费,Worker 导入时不得依赖已删除 XFA OCR 模块。""" assert not hasattr(worker_shipping, 'assert_shipping_worker_capabilities') def test_shipping_worker_claims_unified_pdf_automark_jobs(monkeypatch): """统一自动标注必须进入 shipping worker 的领取白名单,不能永久 queued。""" received: dict[str, object] = {} def claim_next_job(**kwargs): received.update(kwargs) return None monkeypatch.setattr('app.jobs.queue.claim_next_job', claim_next_job) queue_service.claim_next_shipping_notice_job() assert JobType.SHIPPING_PDF_AUTOMARK in received['job_types'] def test_route_runtime_diagnostic_identifies_loaded_workflow_without_document_content(): """路由审计必须可定位 Worker 代码身份,且不得采集文档或字段内容。""" from app.jobs.handlers import shipping_order_extraction as handler diagnostic = handler._route_runtime_diagnostic() source_path = Path(diagnostic['workflow_module_path']) assert diagnostic['interpreter'] == sys.executable assert source_path.name == 'order_extraction_workflow.py' assert diagnostic['workflow_source_sha256'] == hashlib.sha256(source_path.read_bytes()).hexdigest() assert {'pdf', 'dataset', 'value', 'text', 'glyph'}.isdisjoint(diagnostic)