197 lines
7.9 KiB
Python
197 lines
7.9 KiB
Python
"""F068 — 7-Tage-Stabilitätstest mit erweiterter Telemetrie: Tests.
|
|
|
|
Testet plugin/archivar/stability_monitor.py:
|
|
1. Module-Klassen & @implements-Marker
|
|
2. Test-Lifecycle: start / status / Fenster
|
|
3. Telemetrie-Sampling (message/error/restart) nur im laufenden Test
|
|
4. Persistenz (save/load über Instanzen hinweg)
|
|
5. Bericht-Generierung (automatisch nach Fenster + manuell)
|
|
6. is_completed / seconds_remaining Logik
|
|
7. Report-Bewertung (stabil / instabil)
|
|
|
|
@implements: [F-068]
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
|
|
from plugin.archivar.stability_monitor import (
|
|
StabilityMonitor,
|
|
StabilityStatus,
|
|
STABILITY_WINDOW_S,
|
|
)
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# 1. Grundlegende Module-Struktur
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
def test_module_implements_marker():
|
|
src = Path(__file__).resolve().parent.parent / "plugin" / "archivar" / "stability_monitor.py"
|
|
content = src.read_text()
|
|
assert "@implements: [F-068]" in content, \
|
|
"stability_monitor.py fehlt @implements: [F-068] (Bindestrich Pflicht)"
|
|
|
|
|
|
def test_classes_present():
|
|
from plugin.archivar.stability_monitor import StabilityMonitor as SM
|
|
assert SM is StabilityMonitor
|
|
assert StabilityStatus.RUNNING.value == "running"
|
|
|
|
|
|
@pytest.fixture
|
|
def monitor(tmp_path):
|
|
"""Isoliertes Monitor mit temp State-Pfad."""
|
|
m = StabilityMonitor(state_path=str(tmp_path / "stability-test.json"))
|
|
m.load()
|
|
return m
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# 2. Test-Lifecycle
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
def test_initial_state(monitor):
|
|
assert monitor.status == StabilityStatus.INACTIVE
|
|
assert monitor.message_count == 0
|
|
assert monitor.error_count == 0
|
|
assert monitor.restart_count == 0
|
|
|
|
|
|
def test_start_transitions_to_running(monitor):
|
|
msg = monitor.start()
|
|
assert monitor.status == StabilityStatus.RUNNING
|
|
assert monitor._started_at is not None
|
|
assert "gestartet" in msg.lower() or "Stabilitätstest" in msg
|
|
|
|
|
|
def test_start_is_idempotent_while_running(monitor):
|
|
monitor.start()
|
|
first_start = monitor._started_at
|
|
msg = monitor.start()
|
|
assert monitor._started_at == first_start, "Wiederholter Start darf Fenster nicht neu setzen"
|
|
assert "läuft bereits" in msg
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# 3. Telemetrie-Sampling
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
def test_sampling_only_while_running(monitor):
|
|
# INACTIVE → keine Zählung
|
|
monitor.record_message()
|
|
monitor.record_error()
|
|
monitor.record_restart()
|
|
assert monitor.message_count == 0
|
|
assert monitor.error_count == 0
|
|
assert monitor.restart_count == 0
|
|
|
|
# RUNNING → Zählung
|
|
monitor.start()
|
|
monitor.record_message()
|
|
monitor.record_message()
|
|
monitor.record_error()
|
|
monitor.record_restart()
|
|
assert monitor.message_count == 2
|
|
assert monitor.error_count == 1
|
|
assert monitor.restart_count == 1
|
|
|
|
|
|
def test_message_sets_last_timestamp(monitor):
|
|
monitor.start()
|
|
monitor.record_message()
|
|
assert monitor._last_message_ts is not None
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# 4. Persistenz
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
def test_save_load_roundtrip(monitor, tmp_path):
|
|
monitor.start()
|
|
monitor.record_message()
|
|
monitor.record_message()
|
|
monitor.record_error()
|
|
monitor.save()
|
|
|
|
m2 = StabilityMonitor(state_path=str(tmp_path / "stability-test.json"))
|
|
assert m2.load() is True
|
|
assert m2.status == StabilityStatus.RUNNING
|
|
assert m2.message_count == 2
|
|
assert m2.error_count == 1
|
|
assert m2._started_at is not None
|
|
|
|
|
|
def test_load_missing_file_is_ok(monitor, tmp_path):
|
|
m2 = StabilityMonitor(state_path=str(tmp_path / "nicht-da.json"))
|
|
assert m2.load() is True
|
|
assert m2.status == StabilityStatus.INACTIVE
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# 5. Bericht-Generierung
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
def test_manual_report_early_keeps_running(monitor):
|
|
monitor.start()
|
|
monitor.record_message()
|
|
report = monitor.generate_report_snapshot()
|
|
assert "Stabilitätsbericht" in report
|
|
# Fenster nicht abgelaufen → bleibt RUNNING
|
|
assert monitor.status == StabilityStatus.RUNNING
|
|
|
|
|
|
def test_auto_report_after_window(monitor):
|
|
monitor.start()
|
|
monitor.record_message()
|
|
monitor.record_message()
|
|
monitor.record_message() # 3 Nachrichten, 0 Fehler → stabil
|
|
# Fenster ablaufen lassen (Zeit manipulieren)
|
|
monitor._started_at = time.time() - STABILITY_WINDOW_S - 1
|
|
assert monitor.is_completed() is True
|
|
report = monitor.generate_report_snapshot()
|
|
assert "Stabilitätsbericht" in report
|
|
assert monitor.status == StabilityStatus.REPORTED
|
|
|
|
|
|
def test_report_stability_grading(monitor):
|
|
# instabil: viele Fehler — Fenster ablaufen lassen für End-Bewertung
|
|
monitor.start()
|
|
for _ in range(10):
|
|
monitor.record_error()
|
|
monitor._started_at = time.time() - STABILITY_WINDOW_S - 1
|
|
assert monitor.is_completed() is True
|
|
report = monitor.generate_report_snapshot()
|
|
assert "Instabil" in report or "🔴" in report
|
|
|
|
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
# 6. Fenster-Logik
|
|
# ═══════════════════════════════════════════════════════════════════════════
|
|
|
|
def test_seconds_remaining_full_window(monitor):
|
|
monitor.start()
|
|
remaining = monitor.seconds_remaining()
|
|
assert 0 <= remaining <= STABILITY_WINDOW_S
|
|
|
|
|
|
def test_seconds_remaining_zero_when_expired(monitor):
|
|
monitor.start()
|
|
monitor._started_at = time.time() - STABILITY_WINDOW_S - 100
|
|
assert monitor.seconds_remaining() == 0.0
|
|
|
|
|
|
def test_reset(monitor):
|
|
monitor.start()
|
|
monitor.record_message()
|
|
monitor.record_error()
|
|
monitor.reset()
|
|
assert monitor.status == StabilityStatus.INACTIVE
|
|
assert monitor.message_count == 0
|
|
assert monitor.error_count == 0
|