from dataclasses import replace from pathlib import Path from types import SimpleNamespace import pytest from test_shift_report import END, START, composition from production_analytics.cli import __main__ as cli from production_analytics.presentation import shift_report as presenter from production_analytics.service import shift_report_runtime as runtime def arguments(*extra): return [ "report", "shift", "--machine", "E1", "--start", START.isoformat(), "--end", END.isoformat(), "--output", "/tmp/shift.pdf", *extra, ] @pytest.mark.parametrize( "extra,message", [ (["--machine", "UNKNOWN"], "Unknown shift-report machine"), (["--start", "2026-10-07T06:00:00"], "timezone"), (["--start", "invalid"], "Invalid isoformat"), (["--end", START.isoformat()], "end must follow start"), (["--end", "2026-10-06T06:00:00+02:00"], "end must follow start"), ], ) def test_validation_before_settings_or_source_access(monkeypatch, capsys, extra, message): def unexpected(*args, **kwargs): pytest.fail("Invalid arguments reached runtime setup") monkeypatch.setattr(cli, "_runtime_environment", unexpected) assert cli.main(arguments(*extra)) == 2 assert message in capsys.readouterr().err def test_start_end_and_output_are_required(): with pytest.raises(SystemExit) as caught: cli.main(["report", "shift", "--machine", "E1"]) assert caught.value.code == 2 def test_cli_builds_one_report_and_passes_output(monkeypatch, capsys): service, _, _, _ = composition("E1") value = service.build(machine_name="E1", start=START, end=END) calls = [] def environment(*paths): calls.append(paths) return {"settings": "supplied"} def build(machines, env): assert set(machines) == { "B1", "E1", "K7", "K3/4", "E4", "Gitter 1", "Gitter 2", "Gitter 3", "Gitter 4", "K5", "Secumat 1/2", "Secumat 3", "Bento 2", "K1", "K6", } assert env == {"settings": "supplied"} def report(**kwargs): calls.append(kwargs) return value return SimpleNamespace(build=report) monkeypatch.setattr(cli, "_runtime_environment", environment) monkeypatch.setattr(runtime, "build_shift_report_service", build) monkeypatch.setattr( presenter, "write_pdf", lambda report, output: calls.append((report, output)) ) assert cli.main(arguments()) == 0 assert calls == [ (Path("secrets/enlyze.env"), Path("secrets/erp.env"), Path("secrets/postgres.env")), {"machine_name": "E1", "start": START, "end": END}, (value, Path("/tmp/shift.pdf")), ] assert capsys.readouterr().out.strip() == "/tmp/shift.pdf" def test_runtime_wires_existing_readers_without_connecting(monkeypatch): machines = composition("E1")[0].machines tokens = {"postgres": object(), "erp": object(), "enlyze": object()} for settings, token in ( (runtime.PostgresSettings, tokens["postgres"]), (runtime.ErpSettings, tokens["erp"]), (runtime.ExplorationSettings, tokens["enlyze"]), ): monkeypatch.setattr(settings, "from_environment", lambda env, token=token: token) service = runtime.build_shift_report_service(machines, {}) assert service.production.gateway._settings is tokens["erp"] assert service.downtime.repository.settings is tokens["postgres"] assert service.materials.repository.settings is tokens["postgres"] assert service.channel_materials.repository.settings is tokens["postgres"] assert isinstance(service.speed.gateway, runtime.EnlyzeApiGateway) assert set(service.material_allocators) == {"K7"} def test_allocator_selection_uses_strategy_for_any_machine_name(monkeypatch): machines = dict(composition("K7")[0].machines) k7 = machines.pop("K7") machines["OtherMachine"] = replace(k7, machine_id="other-id", speed=None, capabilities=replace(k7.capabilities, speed=False)) machines["B1"] = replace(machines["B1"], material=replace( machines["B1"].material, allocation="none")) for settings in (runtime.PostgresSettings, runtime.ErpSettings, runtime.ExplorationSettings): monkeypatch.setattr(settings, "from_environment", lambda env: object()) service = runtime.build_shift_report_service(machines, {}) assert set(service.material_allocators) == {"OtherMachine"} assert isinstance(service.material_allocators["OtherMachine"], runtime.K7FiberAllocationService) def test_source_failure_is_safe_and_does_not_render(monkeypatch, capsys): def fail(**kwargs): raise RuntimeError("secret source details") monkeypatch.setattr(cli, "_runtime_environment", lambda *args: {}) monkeypatch.setattr( runtime, "build_shift_report_service", lambda *args: SimpleNamespace(build=fail) ) monkeypatch.setattr(presenter, "write_pdf", lambda *args: pytest.fail("Rendered failed read")) assert cli.main(arguments()) == 1 assert capsys.readouterr().err == "Report failed: RuntimeError\n"