Files
redmine/tests/test_nmi_runtime_profile.py
2026-10-18 03:18:36 -04:00

81 lines
2.5 KiB
Python

"""NMI runtime-profile contract for the Redmine CLI/TUI."""
from __future__ import annotations
from pathlib import Path
import pytest
import redmine_ticket_nmi
def test_runtime_profile_honors_explicit_production_mode_and_env_file(tmp_path, monkeypatch):
production_env = tmp_path / "production.env"
production_env.write_text("NMI_ENDPOINT_PRODUCTION=https://app.ldrprep.com/api.php\n")
# File existence alone is no longer readiness. Simulate the offline PHP
# diagnostic here; real CLI resolution is exercised by the integration tests.
import json
import subprocess
monkeypatch.setattr(redmine_ticket_nmi.subprocess, "run", lambda cmd, **kwargs:
subprocess.CompletedProcess(cmd, 0, json.dumps({
"ready": True, "mode": "production", "env_file": str(production_env),
"endpoint": "https://app.ldrprep.com/api.php", "problem": None,
}), ""))
profile = redmine_ticket_nmi.resolve_runtime_profile(
environ={
"NMI_MODE": "production",
"NMI_ENV_FILE": str(production_env),
}
)
assert profile.mode == "production"
assert profile.env_file == production_env
assert profile.ready is True
assert profile.problem is None
def test_runtime_profile_rejects_empty_env_file(tmp_path):
empty_env = tmp_path / "empty.env"
empty_env.touch()
profile = redmine_ticket_nmi.resolve_runtime_profile(
environ={
"NMI_MODE": "production",
"NMI_ENV_FILE": str(empty_env),
}
)
assert profile.mode == "production"
assert profile.ready is False
assert "empty" in profile.problem.lower()
def test_brief_refuses_unready_runtime_profile(monkeypatch, tmp_path):
empty_env = tmp_path / "empty.env"
empty_env.touch()
monkeypatch.setattr(
redmine_ticket_nmi,
"resolve_runtime_profile",
lambda **_kwargs: redmine_ticket_nmi.NmiRuntimeProfile(
mode="production",
env_file=empty_env,
nmi_bin=Path("/tmp/nmi"),
endpoint=None,
ready=False,
problem="NMI env file is empty",
),
)
payload = redmine_ticket_nmi.brief_for_issue(
{"id": 123, "subject": "item 200000001"},
[{"type": "item_no", "value": "200000001", "confidence": "high"}],
mode="production",
env_file=str(empty_env),
)
assert payload["ok"] is False
assert payload["error"] == "nmi_runtime_not_ready"
assert "empty" in payload["runtime"]["problem"].lower()