Fix lint warnings and tighten test mocks
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import type { proto } from "baileys";
|
||||
import {
|
||||
DisconnectReason,
|
||||
fetchLatestBaileysVersion,
|
||||
@@ -8,13 +9,16 @@ import {
|
||||
makeWASocket,
|
||||
useSingleFileAuthState,
|
||||
} from "baileys";
|
||||
import type { proto } from "baileys";
|
||||
import pino from "pino";
|
||||
import qrcode from "qrcode-terminal";
|
||||
import { danger, info, logVerbose, success } from "./globals.js";
|
||||
import { ensureDir, jidToE164, toWhatsappJid } from "./utils.js";
|
||||
|
||||
const WA_WEB_AUTH_FILE = path.join(os.homedir(), ".warelay", "credentials.json");
|
||||
const WA_WEB_AUTH_FILE = path.join(
|
||||
os.homedir(),
|
||||
".warelay",
|
||||
"credentials.json",
|
||||
);
|
||||
|
||||
export async function createWaSocket(printQr: boolean, verbose: boolean) {
|
||||
await ensureDir(path.dirname(WA_WEB_AUTH_FILE));
|
||||
@@ -60,7 +64,9 @@ export async function createWaSocket(printQr: boolean, verbose: boolean) {
|
||||
return sock;
|
||||
}
|
||||
|
||||
export async function waitForWaConnection(sock: ReturnType<typeof makeWASocket>) {
|
||||
export async function waitForWaConnection(
|
||||
sock: ReturnType<typeof makeWASocket>,
|
||||
) {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
type OffCapable = {
|
||||
off?: (event: string, listener: (...args: unknown[]) => void) => void;
|
||||
@@ -68,7 +74,9 @@ export async function waitForWaConnection(sock: ReturnType<typeof makeWASocket>)
|
||||
const evWithOff = sock.ev as unknown as OffCapable;
|
||||
|
||||
const handler = (...args: unknown[]) => {
|
||||
const update = (args[0] ?? {}) as Partial<import("baileys").ConnectionState>;
|
||||
const update = (args[0] ?? {}) as Partial<
|
||||
import("baileys").ConnectionState
|
||||
>;
|
||||
if (update.connection === "open") {
|
||||
evWithOff.off?.("connection.update", handler);
|
||||
resolve();
|
||||
@@ -99,7 +107,9 @@ export async function sendMessageWeb(
|
||||
}
|
||||
const result = await sock.sendMessage(jid, { text: body });
|
||||
const messageId = result?.key?.id ?? "unknown";
|
||||
console.log(success(`✅ Sent via web session. Message ID: ${messageId} -> ${jid}`));
|
||||
console.log(
|
||||
success(`✅ Sent via web session. Message ID: ${messageId} -> ${jid}`),
|
||||
);
|
||||
} finally {
|
||||
try {
|
||||
sock.ws?.close();
|
||||
@@ -231,7 +241,9 @@ export async function monitorWebInbox(options: {
|
||||
reply,
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(danger(`Failed handling inbound web message: ${String(err)}`));
|
||||
console.error(
|
||||
danger(`Failed handling inbound web message: ${String(err)}`),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -254,7 +266,8 @@ function extractText(message: proto.IMessage | undefined): string | undefined {
|
||||
}
|
||||
const extended = message.extendedTextMessage?.text;
|
||||
if (extended?.trim()) return extended.trim();
|
||||
const caption = message.imageMessage?.caption ?? message.videoMessage?.caption;
|
||||
const caption =
|
||||
message.imageMessage?.caption ?? message.videoMessage?.caption;
|
||||
if (caption?.trim()) return caption.trim();
|
||||
return undefined;
|
||||
}
|
||||
@@ -271,6 +284,7 @@ function formatError(err: unknown): string {
|
||||
if (typeof err === "string") return err;
|
||||
const status = getStatusCode(err);
|
||||
const code = (err as { code?: unknown })?.code;
|
||||
if (status || code) return `status=${status ?? "unknown"} code=${code ?? "unknown"}`;
|
||||
if (status || code)
|
||||
return `status=${status ?? "unknown"} code=${code ?? "unknown"}`;
|
||||
return String(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user