refactor(tests): dedupe slack telegram and web monitor setup

This commit is contained in:
Peter Steinberger
2026-02-16 17:06:29 +00:00
parent 8df83d1835
commit a177f7b9fe
4 changed files with 220 additions and 365 deletions

View File

@@ -3,6 +3,14 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { deliverReplies } from "./delivery.js";
const loadWebMedia = vi.fn();
const baseDeliveryParams = {
chatId: "123",
token: "tok",
replyToMode: "off",
textLimit: 4000,
} as const;
type DeliverRepliesParams = Parameters<typeof deliverReplies>[0];
type RuntimeStub = { error: ReturnType<typeof vi.fn>; log?: ReturnType<typeof vi.fn> };
vi.mock("../../web/media.js", () => ({
loadWebMedia: (...args: unknown[]) => loadWebMedia(...args),
@@ -20,23 +28,41 @@ vi.mock("grammy", () => ({
},
}));
function createRuntime(withLog = true): RuntimeStub {
return withLog ? { error: vi.fn(), log: vi.fn() } : { error: vi.fn() };
}
function createBot(api: Record<string, unknown> = {}): Bot {
return { api } as unknown as Bot;
}
async function deliverWith(params: Omit<DeliverRepliesParams, "chatId" | "token">) {
await deliverReplies({
...baseDeliveryParams,
...params,
});
}
function mockMediaLoad(fileName: string, contentType: string, data: string) {
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from(data),
contentType,
fileName,
});
}
describe("deliverReplies", () => {
beforeEach(() => {
loadWebMedia.mockReset();
});
it("skips audioAsVoice-only payloads without logging an error", async () => {
const runtime = { error: vi.fn() };
const bot = { api: {} } as unknown as Bot;
const runtime = createRuntime(false);
await deliverReplies({
await deliverWith({
replies: [{ audioAsVoice: true }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
bot: createBot(),
});
expect(runtime.error).not.toHaveBeenCalled();
@@ -44,30 +70,22 @@ describe("deliverReplies", () => {
it("invokes onVoiceRecording before sending a voice note", async () => {
const events: string[] = [];
const runtime = { error: vi.fn() };
const runtime = createRuntime(false);
const sendVoice = vi.fn(async () => {
events.push("sendVoice");
return { message_id: 1, chat: { id: "123" } };
});
const bot = { api: { sendVoice } } as unknown as Bot;
const bot = createBot({ sendVoice });
const onVoiceRecording = vi.fn(async () => {
events.push("recordVoice");
});
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("voice"),
contentType: "audio/ogg",
fileName: "note.ogg",
});
mockMediaLoad("note.ogg", "audio/ogg", "voice");
await deliverReplies({
await deliverWith({
replies: [{ mediaUrl: "https://example.com/note.ogg", audioAsVoice: true }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
onVoiceRecording,
});
@@ -77,27 +95,19 @@ describe("deliverReplies", () => {
});
it("renders markdown in media captions", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendPhoto = vi.fn().mockResolvedValue({
message_id: 2,
chat: { id: "123" },
});
const bot = { api: { sendPhoto } } as unknown as Bot;
const bot = createBot({ sendPhoto });
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("image"),
contentType: "image/jpeg",
fileName: "photo.jpg",
});
mockMediaLoad("photo.jpg", "image/jpeg", "image");
await deliverReplies({
await deliverWith({
replies: [{ mediaUrl: "https://example.com/photo.jpg", text: "hi **boss**" }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
});
expect(sendPhoto).toHaveBeenCalledWith(
@@ -111,29 +121,21 @@ describe("deliverReplies", () => {
});
it("passes mediaLocalRoots to media loading", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendPhoto = vi.fn().mockResolvedValue({
message_id: 12,
chat: { id: "123" },
});
const bot = { api: { sendPhoto } } as unknown as Bot;
const bot = createBot({ sendPhoto });
const mediaLocalRoots = ["/tmp/workspace-work"];
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("image"),
contentType: "image/jpeg",
fileName: "photo.jpg",
});
mockMediaLoad("photo.jpg", "image/jpeg", "image");
await deliverReplies({
await deliverWith({
replies: [{ mediaUrl: "/tmp/workspace-work/photo.jpg" }],
chatId: "123",
token: "tok",
runtime,
bot,
mediaLocalRoots,
replyToMode: "off",
textLimit: 4000,
});
expect(loadWebMedia).toHaveBeenCalledWith("/tmp/workspace-work/photo.jpg", {
@@ -142,21 +144,17 @@ describe("deliverReplies", () => {
});
it("includes link_preview_options when linkPreview is false", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendMessage = vi.fn().mockResolvedValue({
message_id: 3,
chat: { id: "123" },
});
const bot = { api: { sendMessage } } as unknown as Bot;
const bot = createBot({ sendMessage });
await deliverReplies({
await deliverWith({
replies: [{ text: "Check https://example.com" }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
linkPreview: false,
});
@@ -170,21 +168,17 @@ describe("deliverReplies", () => {
});
it("does not include message_thread_id for DMs (threads don't exist in private chats)", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendMessage = vi.fn().mockResolvedValue({
message_id: 4,
chat: { id: "123" },
});
const bot = { api: { sendMessage } } as unknown as Bot;
const bot = createBot({ sendMessage });
await deliverReplies({
await deliverWith({
replies: [{ text: "Hello" }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
thread: { id: 1, scope: "dm" },
});
@@ -198,21 +192,17 @@ describe("deliverReplies", () => {
});
it("does not include link_preview_options when linkPreview is true", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendMessage = vi.fn().mockResolvedValue({
message_id: 4,
chat: { id: "123" },
});
const bot = { api: { sendMessage } } as unknown as Bot;
const bot = createBot({ sendMessage });
await deliverReplies({
await deliverWith({
replies: [{ text: "Check https://example.com" }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
linkPreview: true,
});
@@ -226,21 +216,18 @@ describe("deliverReplies", () => {
});
it("uses reply_to_message_id when quote text is provided", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendMessage = vi.fn().mockResolvedValue({
message_id: 10,
chat: { id: "123" },
});
const bot = { api: { sendMessage } } as unknown as Bot;
const bot = createBot({ sendMessage });
await deliverReplies({
await deliverWith({
replies: [{ text: "Hello there", replyToId: "500" }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "all",
textLimit: 4000,
replyQuoteText: "quoted text",
});
@@ -261,7 +248,7 @@ describe("deliverReplies", () => {
});
it("falls back to text when sendVoice fails with VOICE_MESSAGES_FORBIDDEN", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendVoice = vi
.fn()
.mockRejectedValue(
@@ -273,24 +260,16 @@ describe("deliverReplies", () => {
message_id: 5,
chat: { id: "123" },
});
const bot = { api: { sendVoice, sendMessage } } as unknown as Bot;
const bot = createBot({ sendVoice, sendMessage });
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("voice"),
contentType: "audio/ogg",
fileName: "note.ogg",
});
mockMediaLoad("note.ogg", "audio/ogg", "voice");
await deliverReplies({
await deliverWith({
replies: [
{ mediaUrl: "https://example.com/note.ogg", text: "Hello there", audioAsVoice: true },
],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
});
// Voice was attempted but failed
@@ -305,26 +284,18 @@ describe("deliverReplies", () => {
});
it("rethrows non-VOICE_MESSAGES_FORBIDDEN errors from sendVoice", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendVoice = vi.fn().mockRejectedValue(new Error("Network error"));
const sendMessage = vi.fn();
const bot = { api: { sendVoice, sendMessage } } as unknown as Bot;
const bot = createBot({ sendVoice, sendMessage });
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("voice"),
contentType: "audio/ogg",
fileName: "note.ogg",
});
mockMediaLoad("note.ogg", "audio/ogg", "voice");
await expect(
deliverReplies({
deliverWith({
replies: [{ mediaUrl: "https://example.com/note.ogg", text: "Hello", audioAsVoice: true }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
}),
).rejects.toThrow("Network error");
@@ -334,7 +305,7 @@ describe("deliverReplies", () => {
});
it("rethrows VOICE_MESSAGES_FORBIDDEN when no text fallback is available", async () => {
const runtime = { error: vi.fn(), log: vi.fn() };
const runtime = createRuntime();
const sendVoice = vi
.fn()
.mockRejectedValue(
@@ -343,23 +314,15 @@ describe("deliverReplies", () => {
),
);
const sendMessage = vi.fn();
const bot = { api: { sendVoice, sendMessage } } as unknown as Bot;
const bot = createBot({ sendVoice, sendMessage });
loadWebMedia.mockResolvedValueOnce({
buffer: Buffer.from("voice"),
contentType: "audio/ogg",
fileName: "note.ogg",
});
mockMediaLoad("note.ogg", "audio/ogg", "voice");
await expect(
deliverReplies({
deliverWith({
replies: [{ mediaUrl: "https://example.com/note.ogg", audioAsVoice: true }],
chatId: "123",
token: "tok",
runtime,
bot,
replyToMode: "off",
textLimit: 4000,
}),
).rejects.toThrow("VOICE_MESSAGES_FORBIDDEN");