fix: P1 Quick-Reply Dedup — 3x-Bug + UX-Text-Anpassung
- outbound_gate.py: separates _quick_reply_sent Dict fuer Trigger-Raeume (nicht an _in_flight gebunden), mark_quick_reply_sent/quick_reply_already_sent - adapter.py: zentraler _maybe_send_quick_reply Helper mit Dedup + kontext- abhaengigem Text (same-room vs cross-room) - send() und send_clarify() rufen beide den Helper statt duplizierter Logik - Tests: 3 Gate-Tests (6.1-6.3) + 3 Integration-Tests (Dedup, Same-Room-Text, Cross-Room-Text) - Diff: 268 Zeilen (<300 F4-Limit)
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
"""
|
||||
P1 Integration-Test: Quick-Reply Dedup + UX-Text.
|
||||
|
||||
Testet dass:
|
||||
1. Parallele Tasks im selben Raum Quick-Reply nur 1x senden (Dedup)
|
||||
2. Same-Raum-Trigger Text OHNE Raum-Namen produziert
|
||||
P1: Quick-Reply Dedup — Integration-Tests fuer _maybe_send_quick_reply.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -11,118 +7,77 @@ from __future__ import annotations
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[3]))
|
||||
|
||||
from plugin.archivar.outbound_gate import OutboundGate, GateDecision
|
||||
from plugin.archivar.outbound_gate import OutboundGate
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 1: Parallele Tasks in selbem Raum → Quick-Reply nur 1x
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
def _make_bare_adapter():
|
||||
from plugin.adapter import RocketChatAdapter
|
||||
return RocketChatAdapter.__new__(RocketChatAdapter)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_quick_reply_dedup_parallel_tasks_same_room():
|
||||
"""Parallele Tasks im selben Raum senden Quick-Reply nur 1x.
|
||||
async def test_parallel_tasks_quick_reply_only_once():
|
||||
"""Parallele Tasks → Quick-Reply nur 1x (Dedup)."""
|
||||
adapter = _make_bare_adapter()
|
||||
gate = OutboundGate()
|
||||
adapter._outbound_gate = gate
|
||||
await gate.try_acquire("ROOM_A", "token-a")
|
||||
|
||||
Simuliert: Zwei parallele _dispatch_agent Tasks feuern gleichzeitig
|
||||
und beide landen im Quick-Reply-Pfad. Der Dedup-Mechanismus
|
||||
(quick_reply_sent Flag) verhindert die 2. und 3. Sendung.
|
||||
"""
|
||||
gate = OutboundGate(default_timeout_s=30.0, max_entry_age_s=60.0)
|
||||
call_count = 0
|
||||
async def mock_post(room_id, text):
|
||||
nonlocal call_count; call_count += 1; return "ok"
|
||||
adapter._post_message_unlocked = mock_post
|
||||
|
||||
room_a = "ROOM_A_BUSY"
|
||||
room_b = "ROOM_B_QR"
|
||||
token_a = "token-busy-a"
|
||||
token_b1 = "token-qr-b1"
|
||||
token_b2 = "token-qr-b2"
|
||||
|
||||
# Raum A hält das Gate (simuliert beschäftigten Agent)
|
||||
await gate.try_acquire(room_a, token_a)
|
||||
|
||||
# Beide Tasks für Raum B werden blockiert
|
||||
decision_b1 = await gate.try_acquire(room_b, token_b1)
|
||||
assert decision_b1.allow is False
|
||||
assert decision_b1.reason == "different_room_busy"
|
||||
|
||||
decision_b2 = await gate.try_acquire(room_b, token_b2)
|
||||
assert decision_b2.allow is False
|
||||
assert decision_b2.reason == "different_room_busy"
|
||||
|
||||
# Simuliere: Task 1 sendet Quick-Reply
|
||||
call_count = [0]
|
||||
|
||||
async def fake_post_message(room_id, text):
|
||||
call_count[0] += 1
|
||||
return "msg_1"
|
||||
|
||||
# Task 1: mark_quick_reply_sent → True (erster)
|
||||
if not gate.quick_reply_already_sent(room_b):
|
||||
await fake_post_message(room_b, "quick reply")
|
||||
gate.mark_quick_reply_sent(room_b)
|
||||
gate.stats.quick_reply += 1
|
||||
|
||||
assert call_count[0] == 1
|
||||
assert gate.quick_reply_already_sent(room_b) is True
|
||||
|
||||
# Task 2: quick_reply_already_sent → True → skip
|
||||
if not gate.quick_reply_already_sent(room_b):
|
||||
await fake_post_message(room_b, "quick reply")
|
||||
gate.mark_quick_reply_sent(room_b)
|
||||
gate.stats.quick_reply += 1
|
||||
|
||||
# Task 2 sollte NICHT gesendet haben
|
||||
assert call_count[0] == 1, (
|
||||
f"Expected 1 Quick-Reply, got {call_count[0]} — Dedup failed"
|
||||
results = await asyncio.gather(
|
||||
adapter._maybe_send_quick_reply("ROOM_B", "ROOM_A", "Alpha"),
|
||||
adapter._maybe_send_quick_reply("ROOM_B", "ROOM_A", "Alpha"),
|
||||
)
|
||||
assert call_count == 1
|
||||
assert sum(1 for r in results if r is not None) == 1
|
||||
assert sum(1 for r in results if r is None) == 1
|
||||
gate.release("ROOM_A", "token-a")
|
||||
|
||||
# Cleanup
|
||||
gate.release(room_a, token_a)
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 2: Same-Raum-Trigger → Text enthält NICHT den Raum-Namen
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_quick_reply_same_room_text_no_room_name():
|
||||
"""Same-Raum-Trigger produziert Text OHNE Raum-Referenz.
|
||||
async def test_same_room_quick_reply_text_no_room_name():
|
||||
"""Same-Raum → Text OHNE Raum-Namen."""
|
||||
adapter = _make_bare_adapter()
|
||||
gate = OutboundGate()
|
||||
adapter._outbound_gate = gate
|
||||
await gate.try_acquire("ROOM_S", "token-s")
|
||||
|
||||
Wenn der Agent im selben Raum beschäftigt ist wo die neue Nachricht
|
||||
reinkommt, soll der Quick-Reply-Text keine Raum-Referenz enthalten
|
||||
(zirkulärer Text vermeiden).
|
||||
"""
|
||||
gate = OutboundGate(default_timeout_s=30.0, max_entry_age_s=60.0)
|
||||
posted = []
|
||||
async def mock_post(room_id, text):
|
||||
posted.append(text); return "ok"
|
||||
adapter._post_message_unlocked = mock_post
|
||||
|
||||
room_same = "ROOM_SAME"
|
||||
token = "token-same"
|
||||
result = await adapter._maybe_send_quick_reply("ROOM_S", "ROOM_S", "Testraum")
|
||||
assert result is not None
|
||||
assert "Testraum" not in posted[0]
|
||||
assert "#" not in posted[0]
|
||||
assert "beschäftigt" in posted[0]
|
||||
gate.release("ROOM_S", "token-s")
|
||||
|
||||
# Agent ist in room_same beschäftigt
|
||||
await gate.try_acquire(room_same, token)
|
||||
|
||||
# Simuliere _maybe_send_quick_reply mit same-room Logik
|
||||
active_room_id = room_same
|
||||
gate_room_id = room_same
|
||||
active_room_name = "Testraum"
|
||||
@pytest.mark.asyncio
|
||||
async def test_cross_room_quick_reply_text_has_room_name():
|
||||
"""Cross-Raum → Text MIT Raum-Namen."""
|
||||
adapter = _make_bare_adapter()
|
||||
gate = OutboundGate()
|
||||
adapter._outbound_gate = gate
|
||||
await gate.try_acquire("ROOM_A", "token-c")
|
||||
|
||||
same_room = (active_room_id is not None and active_room_id == gate_room_id)
|
||||
if same_room:
|
||||
text = 'Ich bin gerade beschäftigt — einen Moment bitte, ich bin gleich bei dir.'
|
||||
elif active_room_name:
|
||||
text = f'Ich antworte gerade in #{active_room_name} — einen Moment bitte, ich bin gleich bei dir.'
|
||||
else:
|
||||
text = 'Ich antworte gerade in einem anderen Raum — einen Moment bitte, ich bin gleich bei dir.'
|
||||
posted = []
|
||||
async def mock_post(room_id, text):
|
||||
posted.append(text); return "ok"
|
||||
adapter._post_message_unlocked = mock_post
|
||||
|
||||
# Same-Raum: Text darf KEINEN Raum-Namen enthalten
|
||||
assert "Testraum" not in text, (
|
||||
f"Same-room text should NOT contain room name, got: {text}"
|
||||
)
|
||||
assert "#" not in text, (
|
||||
f"Same-room text should NOT contain #, got: {text}"
|
||||
)
|
||||
assert "Ich bin gerade beschäftigt" in text
|
||||
|
||||
gate.release(room_same, token)
|
||||
result = await adapter._maybe_send_quick_reply("ROOM_T", "ROOM_A", "Gamma")
|
||||
assert result is not None
|
||||
assert "Gamma" in posted[0]
|
||||
assert "#" in posted[0]
|
||||
gate.release("ROOM_A", "token-c")
|
||||
|
||||
Reference in New Issue
Block a user