766aa7ece6
B9: ask_with_help() Helper — Ja/Nein-Frage mit [H]-Hilfstext.
H/? zeigt help_text und fragt erneut; J/j/Y/y/1 → true, N/n/0 → false,
Enter → default. Suffix sprachabhängig (DE [J/N/H], EN [Y/N/H]).
B10: Sprach-Pack erweitert um multiroom_help, profile_q, profile_help,
ask_suffix (DE/EN). Multiroom-Frage mit ein-Satz-Erklärung.
B8: Profil-Frage selbsterklärend (P/T + [H]-Hilfe) via while-Schleife.
B11: DE-Dialoge auf J)/N() umgestellt (EN behält Y/N).
Tests: test_f98_i2.py um TestAskWithHelp erweitert (7 Tests). 89 passed.
Branch main @ 3aa1c5a. KEIN Push, KEIN Deploy.
294 lines
11 KiB
Python
294 lines
11 KiB
Python
"""
|
|
F98-I2: B16 State-Konsistenz (effective_multiroom) + B20 systemd-Machine-Fix Tests.
|
|
|
|
Testet die Iteration-2-Fixes aus Rainers Architektur-Review:
|
|
1. B16: effective_multiroom() liest Multiroom-Wahrheit aus EINER Quelle
|
|
(env MULTIROOM_AGENT → .env → RC_MULTIROOM → Default true).
|
|
2. B20: gateway_systemctl_v2() nutzt im sudo-Kontext --machine=<user>@.host.
|
|
3. B18: check_components() setzt REPAIR_UNIT_MISSING bei fehlender Unit.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
|
|
INSTALL_SH = Path(__file__).resolve().parents[1] / "install.sh"
|
|
|
|
|
|
def _read_install_sh() -> str:
|
|
return INSTALL_SH.read_text()
|
|
|
|
|
|
def _extract_functions(*names: str) -> str:
|
|
"""Extrahiert Funktions-Definitionen (plus log-Helfer) aus install.sh."""
|
|
content = INSTALL_SH.read_text()
|
|
lines = content.splitlines()
|
|
|
|
def _find_func(name: str) -> tuple[int, int] | None:
|
|
start = None
|
|
for i, line in enumerate(lines):
|
|
if line.startswith(f"{name}()"):
|
|
start = i
|
|
break
|
|
if start is None:
|
|
return None
|
|
end = start
|
|
for j in range(start + 1, len(lines)):
|
|
if lines[j].strip() == "}":
|
|
end = j
|
|
break
|
|
return start, end
|
|
|
|
header_parts = []
|
|
for helper in ("mask_secrets", "log", "warn", "err", "ok", "banner", "die"):
|
|
r = _find_func(helper)
|
|
if r:
|
|
header_parts.append("\n".join(lines[r[0]:r[1] + 1]))
|
|
header = "C_RESET=''; C_BLUE=''; C_GREEN=''; C_YELLOW=''; C_RED=''; C_BOLD=''\n\n" + "\n\n".join(header_parts)
|
|
|
|
funcs = []
|
|
for name in names:
|
|
r = _find_func(name)
|
|
if r:
|
|
funcs.append("\n".join(lines[r[0]:r[1] + 1]))
|
|
return header + "\n\n" + "\n\n".join(funcs) + "\n"
|
|
|
|
|
|
def _run_bash(snippet: str, extra_env: dict | None = None) -> tuple[str, str, int]:
|
|
env = {
|
|
"PATH": "/usr/bin:/bin:/usr/local/bin",
|
|
"HOME": "/tmp/f98-i2-home",
|
|
}
|
|
if extra_env:
|
|
env.update(extra_env)
|
|
result = subprocess.run(
|
|
["bash", "-c", snippet],
|
|
capture_output=True, text=True, env=env, timeout=30,
|
|
)
|
|
return result.stdout, result.stderr, result.returncode
|
|
|
|
|
|
class TestEffectiveMultiroom:
|
|
"""B16: effective_multiroom() — eine Wahrheitsquelle für Multiroom."""
|
|
|
|
def test_env_multiroom_agent_wins(self):
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ "MULTIROOM_AGENT='false'\n"
|
|
+ "RC_MULTIROOM='true'\n"
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0, f"effective_multiroom crashte:\n{out}\n{err}"
|
|
assert "RESULT=false" in out, f"env MULTIROOM_AGENT=false sollte gewinnen:\n{out}"
|
|
|
|
def test_dotenv_multiroom_agent_wins_over_rc(self):
|
|
"""MULTIROOM_AGENT=false in .env gewinnt über RC_MULTIROOM=true."""
|
|
import tempfile, os
|
|
with tempfile.TemporaryDirectory() as d:
|
|
hermes_dir = os.path.join(d, ".hermes")
|
|
os.makedirs(hermes_dir)
|
|
envfile = os.path.join(hermes_dir, ".env")
|
|
with open(envfile, "w") as f:
|
|
f.write("MULTIROOM_AGENT=false\n")
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ f"HOME='{d}'\n"
|
|
+ "RC_MULTIROOM='true'\n"
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "RESULT=false" in out, f".env MULTIROOM_AGENT=false sollte gewinnen:\n{out}\n{err}"
|
|
|
|
def test_rc_multiroom_used_when_no_env(self):
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ "RC_MULTIROOM='false'\n"
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "RESULT=false" in out, f"RC_MULTIROOM=false sollte gewinnen:\n{out}"
|
|
|
|
def test_default_true_when_nothing_set(self):
|
|
snippet = (
|
|
_extract_functions("effective_multiroom")
|
|
+ "echo \"RESULT=$(effective_multiroom)\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "RESULT=true" in out, f"Default sollte true sein:\n{out}"
|
|
|
|
|
|
class TestGatewaySystemctlV2:
|
|
"""B20: gateway_systemctl_v2() — Cross-User-fähiger systemctl-Wrapper."""
|
|
|
|
def test_function_exists(self):
|
|
content = _read_install_sh()
|
|
assert "gateway_systemctl_v2()" in content
|
|
|
|
def test_uses_machine_transport(self):
|
|
"""Der Machine-Transport --machine=<user>@.host muss im Code stehen."""
|
|
content = _read_install_sh()
|
|
assert "--machine=" in content
|
|
assert "@.host" in content
|
|
|
|
def test_derives_sudo_user(self):
|
|
"""SUDO_USER-Ableitung muss vorhanden sein."""
|
|
content = _read_install_sh()
|
|
assert "SUDO_USER" in content
|
|
|
|
def test_non_root_uses_plain_user(self):
|
|
"""Nicht-root: systemctl --user (kein Machine-Transport)."""
|
|
# Simuliere nicht-root: id -u liefert 1000, USER=bernd.
|
|
import tempfile, os
|
|
fake_id = "#!/bin/bash\nif [ \"$1\" = \"-u\" ]; then echo 1000; exit 0; fi\nexit 0\n"
|
|
fake_systemctl = "#!/bin/bash\necho \"ARGS=$*\"\n"
|
|
with tempfile.TemporaryDirectory() as d:
|
|
id_path = os.path.join(d, "id")
|
|
sc_path = os.path.join(d, "systemctl")
|
|
with open(id_path, "w") as f:
|
|
f.write(fake_id)
|
|
with open(sc_path, "w") as f:
|
|
f.write(fake_systemctl)
|
|
os.chmod(id_path, 0o755)
|
|
os.chmod(sc_path, 0o755)
|
|
snippet = (
|
|
_extract_functions("gateway_systemctl_v2")
|
|
+ "USER='bernd'\n"
|
|
+ "gateway_systemctl_v2 is-active hermes-gateway\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet, extra_env={"PATH": f"{d}:/usr/bin:/bin"})
|
|
# Nicht-root → systemctl --user is-active hermes-gateway
|
|
assert "--user" in out, f"Nicht-root sollte --user nutzen:\n{out}\n{err}"
|
|
assert "--machine=" not in out, f"Nicht-root darf keinen Machine-Transport nutzen:\n{out}"
|
|
|
|
def test_root_uses_machine_transport(self):
|
|
"""Root + SUDO_USER → --machine=<user>@.host --user."""
|
|
import tempfile, os
|
|
fake_id = "#!/bin/bash\nif [ \"$1\" = \"-u\" ]; then echo 0; exit 0; fi\nexit 0\n"
|
|
fake_systemctl = "#!/bin/bash\necho \"ARGS=$*\"\n"
|
|
with tempfile.TemporaryDirectory() as d:
|
|
id_path = os.path.join(d, "id")
|
|
sc_path = os.path.join(d, "systemctl")
|
|
with open(id_path, "w") as f:
|
|
f.write(fake_id)
|
|
with open(sc_path, "w") as f:
|
|
f.write(fake_systemctl)
|
|
os.chmod(id_path, 0o755)
|
|
os.chmod(sc_path, 0o755)
|
|
snippet = (
|
|
_extract_functions("gateway_systemctl_v2")
|
|
+ "USER='root'\n"
|
|
+ "SUDO_USER='bernd'\n"
|
|
+ "gateway_systemctl_v2 is-active hermes-gateway\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet, extra_env={"PATH": f"{d}:/usr/bin:/bin"})
|
|
assert "--machine=bernd@.host" in out, f"Root sollte Machine-Transport nutzen:\n{out}\n{err}"
|
|
assert "--user" in out, f"Root sollte --user behalten:\n{out}"
|
|
|
|
|
|
class TestRepairUnitMissing:
|
|
"""B18: check_components() setzt REPAIR_UNIT_MISSING bei fehlender Unit."""
|
|
|
|
def test_repair_unit_missing_flag_declared(self):
|
|
content = _read_install_sh()
|
|
assert "REPAIR_UNIT_MISSING" in content
|
|
|
|
def test_check_components_checks_unit_file(self):
|
|
content = _read_install_sh()
|
|
assert "hermes-gateway.service" in content
|
|
assert "Stufe 8" in content
|
|
|
|
def test_repair_components_has_stage_8(self):
|
|
content = _read_install_sh()
|
|
assert "Reparatur Stufe 8" in content
|
|
|
|
|
|
class TestAskWithHelp:
|
|
"""B9/B10/B11: ask_with_help() — [H]-Hilfe + J/N-Konsistenz."""
|
|
|
|
def test_ask_with_help_function_exists(self):
|
|
content = _read_install_sh()
|
|
assert "ask_with_help()" in content
|
|
|
|
def test_ask_with_help_yes(self):
|
|
snippet = (
|
|
_extract_functions("ask_with_help")
|
|
+ "GUIDED_DEFAULTS=false\n"
|
|
+ "LANG_CHOICE='de'\n"
|
|
+ "declare -A T\n"
|
|
+ "T[de_ask_suffix]=' [J/N/H]: '\n"
|
|
+ "ask_with_help MY_VAR 'Frage?' 'false' 'Hilfetext' <<< 'J'\n"
|
|
+ "echo \"MY_VAR=$MY_VAR\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0, f"ask_with_help crashte:\n{out}\n{err}"
|
|
assert "MY_VAR=true" in out, f"J sollte true setzen:\n{out}"
|
|
|
|
def test_ask_with_help_no(self):
|
|
snippet = (
|
|
_extract_functions("ask_with_help")
|
|
+ "GUIDED_DEFAULTS=false\n"
|
|
+ "LANG_CHOICE='de'\n"
|
|
+ "declare -A T\n"
|
|
+ "T[de_ask_suffix]=' [J/N/H]: '\n"
|
|
+ "ask_with_help MY_VAR 'Frage?' 'true' 'Hilfetext' <<< 'N'\n"
|
|
+ "echo \"MY_VAR=$MY_VAR\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "MY_VAR=false" in out, f"N sollte false setzen:\n{out}"
|
|
|
|
def test_ask_with_help_shows_help_then_accepts(self):
|
|
"""H zeigt Hilfe, dann J wird akzeptiert."""
|
|
snippet = (
|
|
_extract_functions("ask_with_help")
|
|
+ "GUIDED_DEFAULTS=false\n"
|
|
+ "LANG_CHOICE='de'\n"
|
|
+ "declare -A T\n"
|
|
+ "T[de_ask_suffix]=' [J/N/H]: '\n"
|
|
+ "ask_with_help MY_VAR 'Frage?' 'false' 'HILFETEXT-MARKER' <<< $'H\\nJ'\n"
|
|
+ "echo \"MY_VAR=$MY_VAR\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "HILFETEXT-MARKER" in out, f"H sollte Hilfe zeigen:\n{out}\n{err}"
|
|
assert "MY_VAR=true" in out, f"J nach H sollte true setzen:\n{out}"
|
|
|
|
def test_ask_with_help_default_on_empty(self):
|
|
snippet = (
|
|
_extract_functions("ask_with_help")
|
|
+ "GUIDED_DEFAULTS=false\n"
|
|
+ "LANG_CHOICE='de'\n"
|
|
+ "declare -A T\n"
|
|
+ "T[de_ask_suffix]=' [J/N/H]: '\n"
|
|
+ "ask_with_help MY_VAR 'Frage?' 'true' 'Hilfetext' <<< ''\n"
|
|
+ "echo \"MY_VAR=$MY_VAR\"\n"
|
|
)
|
|
out, err, rc = _run_bash(snippet)
|
|
assert rc == 0
|
|
assert "MY_VAR=true" in out, f"Default sollte bei leerer Eingabe greifen:\n{out}"
|
|
|
|
def test_language_pack_has_help_keys(self):
|
|
content = _read_install_sh()
|
|
assert "T[de_multiroom_help]" in content
|
|
assert "T[en_multiroom_help]" in content
|
|
assert "T[de_profile_help]" in content
|
|
assert "T[en_profile_help]" in content
|
|
assert "T[de_ask_suffix]" in content
|
|
assert "T[en_ask_suffix]" in content
|
|
|
|
def test_de_uses_jn_not_yn(self):
|
|
"""B11: DE-Dialoge nutzen J)/N(), nicht Y)/N()."""
|
|
content = _read_install_sh()
|
|
# Der DE-Multiroom-Text darf kein Y) mehr enthalten.
|
|
assert "J) Ja" in content
|
|
# EN behält Y/N (korrekt).
|
|
assert "Y) Yes" in content
|