Files
openclaw/extensions/telegram/src/sequential-key.test.ts
Peter Steinberger da44d52ac6 feat: ask_user — structured questions from the agent with web card, channel buttons, and text answers (#109922)
* feat(gateway): add transient question runtime (question.* methods + broadcasts)

* feat(agents): add blocking ask_user question tool with chat prompt delivery and text-reply claim

* feat(ui): interactive in-thread question cards for ask_user

* feat(channels): native tap-to-answer buttons for ask_user on Telegram, Discord, and Slack

* feat(ui): unify codex and gateway question cards with interactive gateway answering

* refactor(agents): collapse ask_user pending state to one registry; docs for ask_user

* fix(agents): include ask_user in normal gateway runs; add question-flow control-ui e2e

* test(ui): avoid credential-shaped fixture in question card test

* refactor(ui): reorder stream-group context keys

* fix(gateway,ui): validate question answers at resolve; reject secret/duplicate-label questions; UI retry and reconnect hardening

* fix(gateway,agents): canonicalize accepted option answers; bound ask_user option labels to 64 chars

* chore(ci): prune unused question exports, allowlist mobile question events, fix discord lint

* chore(ci): regenerate protocol/i18n/docs/tool-display artifacts for question surface

* fix(protocol): flatten QuestionRecord for native codegen; drop TS-only alias from schema registry

* chore(android): regenerate ask-user localization resources

* docs: regenerate docs map after rebase

* fix(ci): avoid stale read-only dependency disks

* test: remove stale reef lint suppression ratchet

* fix(ci): keep source locale drift advisory in release gates

* fix(ci): scope locale advisory handling to parity check
2026-07-17 22:24:17 +01:00

317 lines
8.7 KiB
TypeScript

// Telegram tests cover sequential key plugin behavior.
import type { Chat, Message } from "grammy/types";
import { describe, expect, it } from "vitest";
import { getTelegramSequentialKey } from "./sequential-key.js";
const mockChat = (chat: Pick<Chat, "id"> & Partial<Pick<Chat, "type" | "is_forum">>): Chat =>
chat as Chat;
const mockMessage = (message: Pick<Message, "chat"> & Partial<Message>): Message =>
({
message_id: 1,
date: 0,
...message,
}) as Message;
describe("getTelegramSequentialKey", () => {
it.each([
[{ message: mockMessage({ chat: mockChat({ id: 123 }) }) }, "telegram:123"],
[
{
message: mockMessage({
chat: mockChat({ id: 123, type: "private" }),
message_thread_id: 9,
}),
},
"telegram:123:topic:9",
],
[
{
message: mockMessage({
chat: mockChat({ id: 123, type: "supergroup" }),
message_thread_id: 9,
}),
},
"telegram:123",
],
[
{
message: mockMessage({
chat: mockChat({ id: 123, type: "supergroup" }),
message_thread_id: 9,
is_topic_message: true,
}),
},
"telegram:123:topic:9",
],
[
{
message: mockMessage({
chat: mockChat({ id: 123, type: "supergroup" }),
is_topic_message: true,
}),
},
"telegram:123:topic:1",
],
[
{
message: mockMessage({
chat: mockChat({ id: 123, type: "supergroup", is_forum: true }),
}),
},
"telegram:123:topic:1",
],
[{ update: { message: mockMessage({ chat: mockChat({ id: 555 }) }) } }, "telegram:555"],
[
{
channelPost: mockMessage({ chat: mockChat({ id: -100777111222, type: "channel" }) }),
},
"telegram:-100777111222",
],
[
{
update: {
channel_post: mockMessage({ chat: mockChat({ id: -100777111223, type: "channel" }) }),
},
},
"telegram:-100777111223",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/stop" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/steer keep going" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/tell use the cache" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/queue status" }) },
"telegram:123:control",
],
[
{
message: mockMessage({
chat: mockChat({ id: -100, type: "supergroup", is_forum: true }),
is_topic_message: true,
message_thread_id: 5907,
text: "/stop@vacs_tars_bot",
}),
},
"telegram:-100:control",
],
[
{
message: mockMessage({
chat: mockChat({ id: -100, type: "supergroup", is_forum: true }),
is_topic_message: true,
message_thread_id: 5907,
text: "/steer@vacs_tars_bot keep going",
}),
},
"telegram:-100:control",
],
[
{
me: { username: "openclaw_bot" } as never,
message: mockMessage({
chat: mockChat({ id: -100, type: "supergroup", is_forum: true }),
is_topic_message: true,
message_thread_id: 5907,
text: "/tell@openclaw_bot keep going!",
}),
},
"telegram:-100:control",
],
[
{
me: { username: "openclaw_bot" } as never,
message: mockMessage({
chat: mockChat({ id: -100, type: "supergroup", is_forum: true }),
is_topic_message: true,
message_thread_id: 5907,
text: "/queue@some_other_bot status",
}),
},
"telegram:-100:topic:5907",
],
[
{
me: { username: "openclaw_bot" } as never,
message: mockMessage({
chat: mockChat({ id: -100, type: "supergroup", is_forum: true }),
is_topic_message: true,
message_thread_id: 5907,
text: "/stop@some_other_bot",
}),
},
"telegram:-100:topic:5907",
],
[
{
me: { username: "openclaw_bot" } as never,
message: mockMessage({
chat: mockChat({ id: -100, type: "supergroup", is_forum: true }),
is_topic_message: true,
message_thread_id: 5907,
text: "/stop@openclaw_bot!",
}),
},
"telegram:-100:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/status" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/commands" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/help" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/tools" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/tasks" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/context" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/whoami" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/diagnostics" }) },
"telegram:123",
],
[
{
message: mockMessage({
chat: mockChat({ id: 123 }),
text: "/diagnostics confirm abc123def456",
}),
},
"telegram:123",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/export-session" }) },
"telegram:123",
],
[{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/export" }) }, "telegram:123"],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/export-trajectory" }) },
"telegram:123",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/trajectory" }) },
"telegram:123",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/btw what is the time?" }) },
"telegram:123:btw:1",
],
[
{
me: { username: "openclaw_bot" } as never,
message: mockMessage({
chat: mockChat({ id: 123 }),
text: "/btw@openclaw_bot what is the time?",
}),
},
"telegram:123:btw:1",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "stop" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "stop please" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "do not do that" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "остановись" }) },
"telegram:123:control",
],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "halt" }) },
"telegram:123:control",
],
[
{
update: {
callback_query: {
message: mockMessage({ chat: mockChat({ id: 123 }) }),
data: "/approve plugin:abc123 allow-once",
},
},
},
"telegram:123:approval",
],
[
{
update: {
callback_query: {
message: mockMessage({ chat: mockChat({ id: 456 }) }),
data: "/approve exec:def456 deny",
},
},
},
"telegram:456:approval",
],
[
{
update: {
callback_query: {
message: mockMessage({ chat: mockChat({ id: 789 }) }),
data: "/approve plugin:ghi789 always",
},
},
},
"telegram:789:approval",
],
[
{
update: {
callback_query: {
message: mockMessage({ chat: mockChat({ id: 321 }) }),
data: "tgq1:ask_0123456789abcdef0123456789abcdef:2",
},
},
},
"telegram:321:question",
],
[
{
update: {
callback_query: {
message: mockMessage({ chat: mockChat({ id: 123 }) }),
data: "some-other-button",
},
},
},
"telegram:123",
],
[{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/abort" }) }, "telegram:123"],
[{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "/abort now" }) }, "telegram:123"],
[
{ message: mockMessage({ chat: mockChat({ id: 123 }), text: "please do not do that" }) },
"telegram:123",
],
])("resolves key %#", (input, expected) => {
expect(getTelegramSequentialKey(input)).toBe(expected);
});
});