feat(telegram): make /ctx work everywhere (#159)

This commit is contained in:
banteg
2026-01-17 01:17:50 +04:00
committed by GitHub
parent b215279a3c
commit 419ec5078b
13 changed files with 390 additions and 20 deletions
+59 -1
View File
@@ -4,8 +4,12 @@ from pathlib import Path
import pytest
from takopi.settings import TelegramTopicsSettings
from takopi.config import ProjectConfig, ProjectsConfig
from takopi.runners.mock import Return, ScriptRunner
from takopi.telegram.chat_sessions import ChatSessionStore
from takopi.telegram.chat_prefs import ChatPrefsStore, resolve_prefs_path
from takopi.telegram.commands.topics import (
_handle_chat_ctx_command,
_handle_chat_new_command,
_handle_ctx_command,
_handle_new_command,
@@ -13,7 +17,13 @@ from takopi.telegram.commands.topics import (
)
from takopi.telegram.topic_state import TopicStateStore
from takopi.telegram.types import TelegramIncomingMessage
from tests.telegram_fakes import FakeTransport, make_cfg
from tests.telegram_fakes import (
DEFAULT_ENGINE_ID,
FakeTransport,
_make_router,
make_cfg,
)
from takopi.transport_runtime import TransportRuntime
def _msg(
@@ -37,6 +47,27 @@ def _msg(
)
def _runtime(tmp_path: Path) -> tuple[TransportRuntime, Path]:
runner = ScriptRunner([Return(answer="ok")], engine=DEFAULT_ENGINE_ID)
projects = ProjectsConfig(
projects={
"alpha": ProjectConfig(
alias="Alpha",
path=tmp_path,
worktrees_dir=Path(".worktrees"),
)
},
default_project="alpha",
)
state_path = tmp_path / "takopi.toml"
runtime = TransportRuntime(
router=_make_router(runner),
projects=projects,
config_path=state_path,
)
return runtime, state_path
@pytest.mark.anyio
async def test_ctx_command_requires_topic(tmp_path: Path) -> None:
transport = FakeTransport()
@@ -60,6 +91,33 @@ async def test_ctx_command_requires_topic(tmp_path: Path) -> None:
assert "only works inside a topic" in text
@pytest.mark.anyio
async def test_chat_ctx_command_sets_binding(tmp_path: Path) -> None:
transport = FakeTransport()
runtime, state_path = _runtime(tmp_path)
cfg = replace(make_cfg(transport), runtime=runtime, session_mode="chat")
store = ChatPrefsStore(resolve_prefs_path(state_path))
msg = _msg("/ctx set alpha @dev", chat_type="private")
await _handle_chat_ctx_command(
cfg,
msg,
args_text="set alpha @dev",
chat_prefs=store,
)
msg_show = _msg("/ctx", chat_type="private")
await _handle_chat_ctx_command(
cfg,
msg_show,
args_text="",
chat_prefs=store,
)
text = transport.send_calls[-1]["message"].text
assert "bound ctx: Alpha @dev" in text
@pytest.mark.anyio
async def test_new_command_requires_topic(tmp_path: Path) -> None:
transport = FakeTransport()