"""Configured downtime runners share the existing machine and order contracts.""" from datetime import timedelta from pathlib import Path import pytest from production_analytics.context import build_enlyze_production_order from production_analytics.service import downtime_runtime from production_analytics.service.downtime_runtime import load_downtime_config from production_analytics.service.shift_report_config import load_shift_report_config ROOT = Path(__file__).resolve().parents[1] NEW_RUNNERS = ( ("e4", "E4", "12026001013", "E 4-12026001013"), ("gitter1", "Gitter 1", "82026400869", "Gitter 1-82026400869"), ("gitter2", "Gitter 2", "82026400909", "Gitter 2-82026400909"), ("gitter3", "Gitter 3", "82026400900", "Gitter 3-82026400900"), ("gitter4", "Gitter 4", "82026400876", "Gitter 4-82026400876"), ("k5", "K5", "82026400921", "K 5-82026400921"), ("secumat12", "Secumat 1/2", "82026400920", "Sec 1/2-82026400920"), ("secumat3", "Secumat 3", "82026400912", "Sec 3-82026400912"), ) @pytest.mark.parametrize("slug,name,erp_order,enlyze_order", NEW_RUNNERS) def test_new_downtime_config_matches_report_machine_and_verified_order( slug, name, erp_order, enlyze_order, ): config = load_downtime_config(ROOT / "config" / f"{slug}-downtime.yaml") report_config = load_shift_report_config(ROOT / "config" / "shift-report-machines.yaml") machine = report_config.machines[name] assert (config.machine_id, config.erp_workplace) == ( machine.machine_id, machine.erp_workplace, ) assert build_enlyze_production_order(erp_order, config.production_order_format) == enlyze_order assert (config.poll_interval_seconds, config.lookback_hours, config.full_reconciliation_hours, config.completion_tolerance_m2) == ( 300, 48, 24, 0.001, ) assert machine.capabilities.production and machine.capabilities.downtime assert not machine.capabilities.material and machine.material is None speed_enabled = name in {"Gitter 1", "Gitter 2", "Gitter 3", "Gitter 4", "K5"} assert machine.capabilities.speed is speed_enabled assert (machine.speed is not None) is speed_enabled unit = ROOT / "deploy" / "systemd" / f"production-analytics-downtime-{slug}.service" text = unit.read_text(encoding="utf-8") assert f"--config {ROOT / 'config' / f'{slug}-downtime.yaml'}" in text assert "run downtime-reconcile" in text assert "--secrets-file" in text and "--erp-secrets-file" in text def test_existing_downtime_configs_and_capabilities_are_unchanged(): machines = load_shift_report_config(ROOT / "config" / "shift-report-machines.yaml").machines for slug, name in (("bento1", "B1"), ("e1", "E1"), ("k7", "K7")): config = load_downtime_config(ROOT / "config" / f"{slug}-downtime.yaml") assert config.machine_id == machines[name].machine_id assert machines[name].capabilities.downtime assert (config.poll_interval_seconds, config.lookback_hours, config.full_reconciliation_hours, config.completion_tolerance_m2) == ( 300, 48, 24, 0.001, ) @pytest.mark.parametrize("slug,name,erp_order,enlyze_order", NEW_RUNNERS) def test_existing_generic_runtime_builds_each_new_runner( monkeypatch, slug, name, erp_order, enlyze_order, ): monkeypatch.setattr(downtime_runtime, "load_secret_file", lambda path: {}) for settings in (downtime_runtime.ExplorationSettings, downtime_runtime.ErpSettings, downtime_runtime.PostgresSettings): monkeypatch.setattr(settings, "from_environment", lambda env: object()) runner = downtime_runtime.build_downtime_runner( ROOT / "config" / f"{slug}-downtime.yaml", secrets_file=ROOT / "secrets" / "enlyze.env", erp_secrets_file=ROOT / "secrets" / "erp.env", ) report_config = load_shift_report_config(ROOT / "config" / "shift-report-machines.yaml") machine = report_config.machines[name] assert runner.machine_id == runner.service.machine_id == machine.machine_id assert runner.service.workplace == machine.erp_workplace assert runner.interval == 300 assert runner.service.lookback == timedelta(hours=48) assert runner.service.full_reconciliation_interval == timedelta(hours=24) assert runner.service.tolerance == 0.001 assert build_enlyze_production_order( erp_order, runner.service.production_order_format) == enlyze_order def test_only_expected_machines_have_downtime_enabled(): machines = load_shift_report_config(ROOT / "config" / "shift-report-machines.yaml").machines assert {name for name, config in machines.items() if config.capabilities.downtime} == { "B1", "E1", "K7", *(name for _, name, _, _ in NEW_RUNNERS), }