refactor: lint cleanups and helpers
This commit is contained in:
@@ -9,22 +9,40 @@ vi.mock("../src/web/media.js", () => ({
|
||||
})),
|
||||
}));
|
||||
|
||||
import { deliverWebReply } from "../src/web/auto-reply.js";
|
||||
import { defaultRuntime } from "../src/runtime.js";
|
||||
import { deliverWebReply } from "../src/web/auto-reply.js";
|
||||
import type { WebInboundMessage } from "../src/web/inbound.js";
|
||||
|
||||
const noopLogger = {
|
||||
info: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
};
|
||||
|
||||
function makeMsg() {
|
||||
function makeMsg(): WebInboundMessage {
|
||||
const reply = vi.fn<
|
||||
Parameters<WebInboundMessage["reply"]>,
|
||||
ReturnType<WebInboundMessage["reply"]>
|
||||
>();
|
||||
const sendMedia = vi.fn<
|
||||
Parameters<WebInboundMessage["sendMedia"]>,
|
||||
ReturnType<WebInboundMessage["sendMedia"]>
|
||||
>();
|
||||
const sendComposing = vi.fn<
|
||||
Parameters<WebInboundMessage["sendComposing"]>,
|
||||
ReturnType<WebInboundMessage["sendComposing"]>
|
||||
>();
|
||||
return {
|
||||
from: "+10000000000",
|
||||
conversationId: "+10000000000",
|
||||
to: "+20000000000",
|
||||
id: "abc",
|
||||
reply: vi.fn(),
|
||||
sendMedia: vi.fn(),
|
||||
} as any;
|
||||
body: "hello",
|
||||
chatType: "direct",
|
||||
chatId: "chat-1",
|
||||
sendComposing,
|
||||
reply,
|
||||
sendMedia,
|
||||
};
|
||||
}
|
||||
|
||||
describe("deliverWebReply retry", () => {
|
||||
@@ -54,7 +72,10 @@ describe("deliverWebReply retry", () => {
|
||||
|
||||
await expect(
|
||||
deliverWebReply({
|
||||
replyResult: { text: "caption", mediaUrl: "http://example.com/img.jpg" },
|
||||
replyResult: {
|
||||
text: "caption",
|
||||
mediaUrl: "http://example.com/img.jpg",
|
||||
},
|
||||
msg,
|
||||
maxMediaBytes: 5_000_000,
|
||||
replyLogger: noopLogger,
|
||||
@@ -66,4 +87,3 @@ describe("deliverWebReply retry", () => {
|
||||
expect(msg.sendMedia).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,57 +1,70 @@
|
||||
import { EventEmitter } from "node:events";
|
||||
|
||||
import { vi } from "vitest";
|
||||
|
||||
export type MockBaileysSocket = {
|
||||
ev: import("events").EventEmitter;
|
||||
ws: { close: ReturnType<typeof vi.fn> };
|
||||
sendPresenceUpdate: ReturnType<typeof vi.fn>;
|
||||
sendMessage: ReturnType<typeof vi.fn>;
|
||||
readMessages: ReturnType<typeof vi.fn>;
|
||||
user?: { id?: string };
|
||||
ev: EventEmitter;
|
||||
ws: { close: ReturnType<typeof vi.fn> };
|
||||
sendPresenceUpdate: ReturnType<typeof vi.fn>;
|
||||
sendMessage: ReturnType<typeof vi.fn>;
|
||||
readMessages: ReturnType<typeof vi.fn>;
|
||||
user?: { id?: string };
|
||||
};
|
||||
|
||||
export type MockBaileysModule = {
|
||||
DisconnectReason: { loggedOut: number };
|
||||
fetchLatestBaileysVersion: ReturnType<typeof vi.fn>;
|
||||
makeCacheableSignalKeyStore: ReturnType<typeof vi.fn>;
|
||||
makeWASocket: ReturnType<typeof vi.fn>;
|
||||
useMultiFileAuthState: ReturnType<typeof vi.fn>;
|
||||
jidToE164?: (jid: string) => string | null;
|
||||
proto?: unknown;
|
||||
downloadMediaMessage?: ReturnType<typeof vi.fn>;
|
||||
DisconnectReason: { loggedOut: number };
|
||||
fetchLatestBaileysVersion: ReturnType<typeof vi.fn>;
|
||||
makeCacheableSignalKeyStore: ReturnType<typeof vi.fn>;
|
||||
makeWASocket: ReturnType<typeof vi.fn>;
|
||||
useMultiFileAuthState: ReturnType<typeof vi.fn>;
|
||||
jidToE164?: (jid: string) => string | null;
|
||||
proto?: unknown;
|
||||
downloadMediaMessage?: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
|
||||
export function createMockBaileys(): { mod: MockBaileysModule; lastSocket: () => MockBaileysSocket } {
|
||||
const sockets: MockBaileysSocket[] = [];
|
||||
const makeWASocket = vi.fn((opts: unknown) => {
|
||||
const ev = new (require("events").EventEmitter)();
|
||||
const sock: MockBaileysSocket = {
|
||||
ev,
|
||||
ws: { close: vi.fn() },
|
||||
sendPresenceUpdate: vi.fn().mockResolvedValue(undefined),
|
||||
sendMessage: vi.fn().mockResolvedValue({ key: { id: "msg123" } }),
|
||||
readMessages: vi.fn().mockResolvedValue(undefined),
|
||||
user: { id: "123@s.whatsapp.net" },
|
||||
};
|
||||
setImmediate(() => ev.emit("connection.update", { connection: "open" }));
|
||||
sockets.push(sock);
|
||||
return sock;
|
||||
});
|
||||
export function createMockBaileys(): {
|
||||
mod: MockBaileysModule;
|
||||
lastSocket: () => MockBaileysSocket;
|
||||
} {
|
||||
const sockets: MockBaileysSocket[] = [];
|
||||
const makeWASocket = vi.fn((_opts: unknown) => {
|
||||
const ev = new EventEmitter();
|
||||
const sock: MockBaileysSocket = {
|
||||
ev,
|
||||
ws: { close: vi.fn() },
|
||||
sendPresenceUpdate: vi.fn().mockResolvedValue(undefined),
|
||||
sendMessage: vi.fn().mockResolvedValue({ key: { id: "msg123" } }),
|
||||
readMessages: vi.fn().mockResolvedValue(undefined),
|
||||
user: { id: "123@s.whatsapp.net" },
|
||||
};
|
||||
setImmediate(() => ev.emit("connection.update", { connection: "open" }));
|
||||
sockets.push(sock);
|
||||
return sock;
|
||||
});
|
||||
|
||||
const mod: MockBaileysModule = {
|
||||
DisconnectReason: { loggedOut: 401 },
|
||||
fetchLatestBaileysVersion: vi.fn().mockResolvedValue({ version: [1, 2, 3] }),
|
||||
makeCacheableSignalKeyStore: vi.fn((keys: unknown) => keys),
|
||||
makeWASocket,
|
||||
useMultiFileAuthState: vi.fn(async () => ({
|
||||
state: { creds: {}, keys: {} },
|
||||
saveCreds: vi.fn(),
|
||||
})),
|
||||
jidToE164: (jid: string) => jid.replace(/@.*$/, "").replace(/^/, "+"),
|
||||
downloadMediaMessage: vi.fn().mockResolvedValue(Buffer.from("img")),
|
||||
};
|
||||
const mod: MockBaileysModule = {
|
||||
DisconnectReason: { loggedOut: 401 },
|
||||
fetchLatestBaileysVersion: vi
|
||||
.fn()
|
||||
.mockResolvedValue({ version: [1, 2, 3] }),
|
||||
makeCacheableSignalKeyStore: vi.fn((keys: unknown) => keys),
|
||||
makeWASocket,
|
||||
useMultiFileAuthState: vi.fn(async () => ({
|
||||
state: { creds: {}, keys: {} },
|
||||
saveCreds: vi.fn(),
|
||||
})),
|
||||
jidToE164: (jid: string) => jid.replace(/@.*$/, "").replace(/^/, "+"),
|
||||
downloadMediaMessage: vi.fn().mockResolvedValue(Buffer.from("img")),
|
||||
};
|
||||
|
||||
return {
|
||||
mod,
|
||||
lastSocket: () => sockets[sockets.length - 1]!,
|
||||
};
|
||||
return {
|
||||
mod,
|
||||
lastSocket: () => {
|
||||
const last = sockets.at(-1);
|
||||
if (!last) {
|
||||
throw new Error("No Baileys sockets created");
|
||||
}
|
||||
return last;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user