Files
agent-zero/tests/test_telegram_sessions_picker.py
Alessandro 9ae1939bd6 Restore secure Telegram scopes and preserve legacy configuration
Restore global, project, profile, and project/profile configuration with
one bot identity per saved scope. Preserve inherited configuration and
pause conflicting legacy bot identities without discarding their settings.

Revalidate scope, activation, and access rules for inbound messages,
commands, callbacks, session selection, and outbound delivery. Keep scoped
bots within their owning project/profile, omit credentials from persisted
chat metadata, and sanitize attachment filenames.

Automatically back up existing configuration, activation, and session
mappings before upgrade. Preserve their contents and stable global session
keys, while provisioning secure webhook secrets where legacy settings
require them. Document scope ownership, conflict recovery, and migration.

Validation:
- Docker framework suite: 216 passed, plus 73 subtests.
- Live Telegram status and message/reply round-trip confirmed.
- Missing and forged webhook credentials rejected with HTTP 403.
- Existing live configuration, activation, and session mappings unchanged.
2026-11-12 17:27:15 +01:00

81 lines
3.0 KiB
Python

import asyncio
from types import SimpleNamespace
from plugins._telegram_integration.helpers import command_ui
from plugins._telegram_integration.helpers.constants import (
CTX_TG_BOT,
CTX_TG_CHAT_ID,
CTX_TG_USER_ID,
CTX_TG_USERNAME,
)
class FakeContext:
def __init__(self, id, name, last_message, *, running=False):
self.id = id
self.name = name
self.data = {}
self._last_message = last_message
self._running = running
def output(self):
return {"last_message": self._last_message}
def is_running(self):
return self._running
def test_session_picker_shows_four_recent_sessions(monkeypatch):
contexts = [
FakeContext("ctx-1", "One", "2026-05-01T10:00:00"),
FakeContext("ctx-2", "Two", "2026-05-02T10:00:00"),
FakeContext("ctx-3", "Three", "2026-05-03T10:00:00"),
FakeContext("ctx-4", "Four", "2026-05-04T10:00:00"),
FakeContext("ctx-5", "Five", "2026-05-05T10:00:00"),
]
monkeypatch.setattr(command_ui.scopes, "can_select_session", lambda source, target: True)
current = contexts[4]
monkeypatch.setattr(command_ui, "AgentContext", SimpleNamespace(all=lambda: contexts))
text, markup = command_ui._session_view(current, 0)
assert "Current session: <b>Five</b>" in text
assert "Showing 1-4 of 5." in text
rows = markup["inline_keyboard"]
assert [row[0]["text"] for row in rows[:4]] == ["• Five", "Four", "Three", "Two"]
assert rows[-1] == [{"text": "Next", "callback_data": "tg:session:page:1"}]
def test_select_session_remaps_telegram_chat(monkeypatch):
current = FakeContext("ctx-current", "Current", "2026-05-03T10:00:00")
target = FakeContext("ctx-target", "Target", "2026-05-04T10:00:00")
current.data.update(
{
CTX_TG_BOT: "main",
CTX_TG_CHAT_ID: 123,
CTX_TG_USER_ID: 456,
CTX_TG_USERNAME: "anmol",
}
)
monkeypatch.setattr(command_ui.scopes, "can_select_session", lambda source, target: True)
saved = []
dirty = []
state_out = {}
monkeypatch.setattr(command_ui, "AgentContext", SimpleNamespace(all=lambda: [current, target]))
monkeypatch.setattr(command_ui, "save_tmp_chat", lambda context: saved.append(context.id))
monkeypatch.setattr(command_ui, "mark_dirty_for_context", lambda context_id, *, reason=None: dirty.append((context_id, reason)))
monkeypatch.setattr(command_ui, "_load_telegram_state", lambda: {"chats": {}})
monkeypatch.setattr(command_ui, "_save_telegram_state", lambda state: state_out.update(state))
selected = asyncio.run(command_ui._select_session(current, 0))
assert selected is target
assert target.data[CTX_TG_BOT] == "main"
assert target.data[CTX_TG_CHAT_ID] == 123
assert target.data[CTX_TG_USER_ID] == 456
assert target.data[CTX_TG_USERNAME] == "anmol"
assert state_out["chats"]["main:456:123"] == "ctx-target"
assert saved == ["ctx-target"]
assert ("ctx-target", "telegram.session_select") in dirty