"""停止标记控制真实CLI领取边界,不中断正在完成的单次处理。""" from argparse import Namespace from contextlib import nullcontext from types import SimpleNamespace from unittest.mock import Mock import pytest from app import cli from app.core.config import Settings @pytest.mark.parametrize("already_stopped", [False, True]) def test_停止后不再领取下一任务(monkeypatch, tmp_path, already_stopped): marker = tmp_path / "stop.requested" if already_stopped: marker.touch() settings = Settings(_env_file=None, app_secret_key="x" * 32) monkeypatch.setattr(cli, "get_settings", lambda: settings) monkeypatch.setattr(cli, "get_session_factory", lambda: lambda: nullcontext(object())) def finish(_session, **_kwargs): marker.touch() return "completed" consume = Mock(side_effect=finish) pulse = Mock() monkeypatch.setattr(cli, "run_once", consume) monkeypatch.setattr(cli, "get_batch", lambda *_: SimpleNamespace(status="SUCCEEDED")) monkeypatch.setattr("app.services.tplus.automation.heartbeat", pulse) result = cli._tplus_worker(Namespace(connection_file=None, poll_seconds=2, watch=True, stop_file=str(marker))) assert result == 0 assert consume.call_count == (0 if already_stopped else 1) assert pulse.call_args.args[3] == "STOPPED"