peppa/packages/testkit/src/peppa-dev-tg.ts
Асель 112c08b063 PEP-6: dev stand with mock Telegram so agents can play the PM
- mock-telegram as a standalone server with /_mock/* inspection and PM
  actions (messages, send, click, callbacks)
- peppa-dev up|down|status|peppa|tg: isolated DB peppa_dev, tmux -L
  peppa-dev, ~/.peppa-dev, the real bot against mock-telegram, mock agents;
  test PM paired automatically via /start
- verified: question from a mock session answered with a button on the
  stand, delivered to the session; prod sessions/tmux/Telegram untouched
- OpenSpec: pep-6 archived into spec dev-sandbox

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 21:10:37 +05:00

68 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// `peppa-dev tg ...`: the dev stand's PM chat — read bot messages and act as the PM.
const API = process.env.TELEGRAM_API_URL ?? "http://127.0.0.1:3190";
type Msg = { message_id: number; text: string; edits: number; reply_to?: number; reply_markup?: { inline_keyboard: Array<Array<{ text: string; callback_data?: string }>> } };
const get = async <T>(p: string) => (await fetch(`${API}${p}`)).json() as Promise<T>;
const post = async <T>(p: string, body: unknown) =>
(await fetch(`${API}${p}`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) })).json() as Promise<T>;
const plain = (html: string) => html.replace(/<[^>]+>/g, "").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&");
function show(m: Msg) {
const buttons = (m.reply_markup?.inline_keyboard ?? []).flat();
console.log(`── #${m.message_id}${m.edits ? ` (edited ×${m.edits})` : ""}${m.reply_to ? ` ↩ #${m.reply_to}` : ""}`);
console.log(plain(m.text));
if (buttons.length) console.log(` [${buttons.map((b) => `${b.text} → ${b.callback_data}`).join("] [")}]`);
}
async function messages(n = 10) {
const all = await get<Msg[]>("/_mock/messages");
all.slice(-n).forEach(show);
if (!all.length) console.log("(no messages)");
}
/** After an action, show what the bot did in reaction. */
async function aftermath(sinceIds: Set<number>, edited: Map<number, number>) {
await new Promise((r) => setTimeout(r, 2500));
const all = await get<Msg[]>("/_mock/messages");
const changed = all.filter((m) => !sinceIds.has(m.message_id) || (edited.get(m.message_id) ?? 0) !== m.edits);
changed.forEach(show);
const toasts = await get<Array<{ text?: string }>>("/_mock/callbacks");
if (toasts.length) console.log(`toast: ${toasts.at(-1)?.text ?? ""}`);
}
async function snapshot() {
const all = await get<Msg[]>("/_mock/messages");
return { ids: new Set(all.map((m) => m.message_id)), edits: new Map(all.map((m) => [m.message_id, m.edits])) };
}
const [cmd, ...args] = process.argv.slice(2);
switch (cmd) {
case "messages":
await messages(Number(args[0] ?? 10));
break;
case "click": {
const id = Number(args[0]);
const want = args.slice(1).join(" ").toLowerCase();
const m = (await get<Msg[]>("/_mock/messages")).find((x) => x.message_id === id);
const b = (m?.reply_markup?.inline_keyboard ?? []).flat().find((x) => x.callback_data === want || x.text.toLowerCase().includes(want));
if (!b?.callback_data) throw new Error(`no button "${want}" on message #${id}`);
const s = await snapshot();
await post("/_mock/click", { messageId: id, data: b.callback_data });
await aftermath(s.ids, s.edits);
break;
}
case "reply":
case "send": {
const s = await snapshot();
await post("/_mock/send", cmd === "reply" ? { replyTo: Number(args[0]), text: args.slice(1).join(" ") } : { text: args.join(" ") });
await aftermath(s.ids, s.edits);
break;
}
case "callbacks":
console.log(JSON.stringify(await get("/_mock/callbacks"), null, 2));
break;
default:
console.log("peppa-dev tg messages [n] | click <msgId> <button text|data> | reply <msgId> <text> | send <text> | callbacks");
process.exit(1);
}