Includes atomic migrations, per-rule reminder semantics, first-run helper script and the note on the undecided UI chat idea. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
217 lines
7.3 KiB
Python
217 lines
7.3 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from app.chat import Target
|
|
from app.models import SUPPRESS_MUTE
|
|
from app.mutes import SOURCE_UI, Mute
|
|
from app.poller import Poller, PollerStatus
|
|
from app.routing import ALL_KINDS, Route
|
|
from tests.test_pipeline import Env, FakeAdapter, FakeOllama, problem
|
|
|
|
|
|
class StaticRoutes:
|
|
def __init__(self, routes):
|
|
self.routes = routes
|
|
|
|
def enabled_routes(self):
|
|
return [r for r in self.routes if r.enabled]
|
|
|
|
|
|
class StaticMutes:
|
|
def __init__(self, mutes):
|
|
self.mutes = mutes
|
|
self.purged = 0
|
|
|
|
def active(self, now=None):
|
|
return self.mutes
|
|
|
|
def purge_expired(self, now=None):
|
|
self.purged += 1
|
|
return 0
|
|
|
|
|
|
def route(position, host, targets, stop=False):
|
|
return Route(position, f"r{position}", position, True, host, "", 1, ALL_KINDS, stop, tuple(targets))
|
|
|
|
|
|
def two_backends():
|
|
return FakeAdapter("telegram", "tg-default"), FakeAdapter("matrix", "!room:s")
|
|
|
|
|
|
async def test_without_rules_every_enabled_backend_gets_default_target():
|
|
tg, mx = two_backends()
|
|
env = Env(FakeOllama(), adapter_list=[tg, mx])
|
|
await env.handle([problem()])
|
|
assert [s[0] for s in tg.sent] == ["tg-default"] and [s[0] for s in mx.sent] == ["!room:s"]
|
|
|
|
|
|
async def test_messages_split_by_host_and_target_set():
|
|
tg, mx = two_backends()
|
|
routes = StaticRoutes([route(1, "web*", [Target("telegram", "tg-default")], stop=True)])
|
|
env = Env(FakeOllama(), adapter_list=[tg, mx], routes=routes)
|
|
await env.handle([problem(host="web01"), problem(host="web01", service="cpu"), problem(host="db01")])
|
|
assert len(tg.sent) == 2 and len(mx.sent) == 1
|
|
web_text = next(text for _, text in tg.sent if "web01" in text)
|
|
assert "disk" in web_text and "cpu" in web_text and "db01" not in web_text
|
|
assert "db01" in mx.sent[0][1]
|
|
|
|
|
|
async def test_same_host_different_targets_gives_separate_messages():
|
|
tg, mx = two_backends()
|
|
routes = StaticRoutes([route(1, "", [Target("telegram", "tg-default")])])
|
|
only_cpu = Route(2, "cpu", 2, True, "", "cpu", 1, ALL_KINDS, False, (Target("matrix", "!room:s"),))
|
|
env = Env(FakeOllama(), adapter_list=[tg, mx], routes=StaticRoutes(routes.routes + [only_cpu]))
|
|
await env.handle([problem(service="disk"), problem(service="cpu")])
|
|
assert len(tg.sent) == 2
|
|
assert len(mx.sent) == 1 and "cpu" in mx.sent[0][1] and "disk" not in mx.sent[0][1]
|
|
|
|
|
|
async def test_edit_goes_to_all_deliveries_of_the_message():
|
|
tg, mx = two_backends()
|
|
env = Env(FakeOllama(), adapter_list=[tg, mx])
|
|
await env.handle([problem()])
|
|
assert len(tg.edits) == 1 and len(mx.edits) == 1
|
|
assert "Analyse:" in tg.edits[0][1] and "Analyse:" in mx.edits[0][1]
|
|
rows = env.db.query("SELECT backend, target FROM deliveries ORDER BY backend")
|
|
assert [(r["backend"], r["target"]) for r in rows] == [("matrix", "!room:s"), ("telegram", "tg-default")]
|
|
|
|
|
|
async def test_route_without_targets_stores_alert_but_sends_nothing():
|
|
tg, _ = two_backends()
|
|
env = Env(FakeOllama(), adapter_list=[tg], routes=StaticRoutes([route(1, "", [])]))
|
|
await env.handle([problem()])
|
|
assert tg.sent == [] and env.ollama.calls == []
|
|
assert env.repo.recent()[0]["ai_status"] == "skipped"
|
|
|
|
|
|
async def test_muted_event_stored_with_reason_and_not_sent():
|
|
mute = Mute(1, "h1", "*", 10**9, "", "admin", 0, SOURCE_UI)
|
|
env = Env(FakeOllama(), mutes=StaticMutes([mute]))
|
|
await env.handle([problem(host="h1"), problem(host="h2")])
|
|
assert len(env.adapter.sent) == 1 and "h2" in env.adapter.sent[0][1]
|
|
muted = next(a for a in env.repo.recent() if a["host"] == "h1")
|
|
assert muted["suppressed"] == 1 and muted["suppress_reason"] == SUPPRESS_MUTE
|
|
assert len(env.ollama.calls) == 1
|
|
|
|
|
|
async def test_failed_send_marks_ai_skipped_not_pending():
|
|
class Broken(FakeAdapter):
|
|
async def send(self, target, text):
|
|
raise RuntimeError("down")
|
|
|
|
env = Env(FakeOllama(), adapter_list=[Broken()])
|
|
await env.handle([problem()])
|
|
assert env.repo.recent()[0]["ai_status"] == "skipped"
|
|
|
|
|
|
async def test_queue_overflow_drops_oldest(caplog):
|
|
env = Env(FakeOllama(), queue_size=2)
|
|
with caplog.at_level(logging.WARNING):
|
|
await env.pipeline.handle([problem(service="s1"), problem(service="s2"), problem(service="s3")])
|
|
await env.pipeline.drain()
|
|
await env.pipeline.wait_idle()
|
|
text = env.adapter.sent[0][1]
|
|
assert "s1" not in text and "s2" in text and "s3" in text
|
|
assert "verworfen" in caplog.text
|
|
|
|
|
|
async def test_handle_returns_immediately_with_hanging_adapter():
|
|
class Hanging(FakeAdapter):
|
|
async def send(self, target, text):
|
|
await asyncio.Event().wait()
|
|
|
|
env = Env(FakeOllama(), adapter_list=[Hanging()])
|
|
worker = asyncio.create_task(env.pipeline.run())
|
|
try:
|
|
await asyncio.wait_for(env.pipeline.handle([problem()]), 1)
|
|
await asyncio.wait_for(env.pipeline.handle([problem(host="h2")]), 1)
|
|
finally:
|
|
worker.cancel()
|
|
await asyncio.gather(worker, return_exceptions=True)
|
|
|
|
|
|
async def test_poller_cycle_not_blocked_by_hanging_adapter():
|
|
class Hanging(FakeAdapter):
|
|
async def send(self, target, text):
|
|
await asyncio.Event().wait()
|
|
|
|
class Store:
|
|
def get_int(self, key, default, minimum=0):
|
|
return default
|
|
|
|
class State:
|
|
def load(self):
|
|
return {}
|
|
|
|
def apply(self, previous, rows, now):
|
|
pass
|
|
|
|
from app.checkmk import ServiceState
|
|
|
|
class Client:
|
|
async def problem_services(self):
|
|
return [ServiceState("h1", "disk", 2, True, "voll", 1, False, False)]
|
|
|
|
async def problem_hosts(self):
|
|
return []
|
|
|
|
async def aclose(self):
|
|
pass
|
|
|
|
env = Env(FakeOllama(), adapter_list=[Hanging()])
|
|
worker = asyncio.create_task(env.pipeline.run())
|
|
poller = Poller(Store(), State(), env.pipeline.handle, PollerStatus(), client_factory=Client)
|
|
try:
|
|
await asyncio.wait_for(poller.run_cycle(), 1)
|
|
await asyncio.wait_for(poller.run_cycle(), 1)
|
|
finally:
|
|
worker.cancel()
|
|
await asyncio.gather(worker, return_exceptions=True)
|
|
|
|
|
|
async def test_ticker_prunes_expired_mutes():
|
|
mutes = StaticMutes([])
|
|
env = Env(FakeOllama(), mutes=mutes)
|
|
env.clock.now = 10_000.0
|
|
await env.pipeline._prune_mutes()
|
|
await env.pipeline._prune_mutes()
|
|
assert mutes.purged == 1
|
|
env.clock.now += 61
|
|
await env.pipeline._prune_mutes()
|
|
assert mutes.purged == 2
|
|
|
|
|
|
async def test_finish_flushes_held_events():
|
|
env = Env(FakeOllama())
|
|
await env.handle([problem(service="s0")])
|
|
env.clock.now += 10
|
|
await env.handle([problem(service="s1")])
|
|
assert len(env.adapter.sent) == 1
|
|
await env.pipeline.finish()
|
|
assert len(env.adapter.sent) == 2
|
|
|
|
|
|
async def test_edit_retries_with_fresh_adapter_after_replacement():
|
|
old = FakeAdapter()
|
|
|
|
async def broken_edit(ref, text):
|
|
raise RuntimeError("client closed")
|
|
|
|
old.edit = broken_edit
|
|
env = Env(FakeOllama(), adapter_list=[old])
|
|
fresh = FakeAdapter()
|
|
original_current = env.pipeline._adapters
|
|
|
|
calls = {"n": 0}
|
|
|
|
async def swapping():
|
|
calls["n"] += 1
|
|
if calls["n"] > 3:
|
|
return [fresh]
|
|
return await original_current()
|
|
|
|
env.pipeline._deliverer._adapters = swapping
|
|
env.pipeline._adapters = swapping
|
|
await env.handle([problem()])
|
|
assert len(fresh.edits) == 1
|