fix: P1 Quick-Reply Dedup — verhindert 3x-Sendung + kontextabhängiger UX-Text
BUG 1: Quick-Reply wurde 3x gesendet (send(), send_clarify(), parallele acquires) - _quick_reply_sent Set in OutboundGate für raumübergreifenden Dedup - _maybe_send_quick_reply Helper in adapter.py (eine zentrale Stelle) - Beide Call-Sites (send + send_clarify) durch Helper ersetzt BUG 2: Zirkulärer Quick-Reply-Text bei Same-Raum-Trigger - Same-Raum: 'Ich bin gerade beschäftigt — einen Moment bitte...' (ohne Raum) - Cross-Raum: 'Ich antworte gerade in #Raum — einen Moment bitte...' (mit Raum) Tests: 10 Gate-Tests + 3 Integration-Tests (13/13 grün)
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
"""
|
||||
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
|
||||
"""
|
||||
|
||||
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
|
||||
|
||||
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
# Test 1: Parallele Tasks in selbem Raum → Quick-Reply nur 1x
|
||||
# ═══════════════════════════════════════════════════════════════════════════
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_quick_reply_dedup_parallel_tasks_same_room():
|
||||
"""Parallele Tasks im selben Raum senden Quick-Reply nur 1x.
|
||||
|
||||
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)
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
# 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.
|
||||
|
||||
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)
|
||||
|
||||
room_same = "ROOM_SAME"
|
||||
token = "token-same"
|
||||
|
||||
# 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"
|
||||
|
||||
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.'
|
||||
|
||||
# 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)
|
||||
Reference in New Issue
Block a user