Tests: cover agents and fix web defaults
Co-authored-by: RealSid08 <RealSid08@users.noreply.github.com>
This commit is contained in:
@@ -1,10 +1,4 @@
|
||||
// Import test-helpers FIRST to set up mocks before other imports
|
||||
import {
|
||||
resetBaileysMocks,
|
||||
resetLoadConfigMock,
|
||||
setLoadConfigMock,
|
||||
} from "./test-helpers.js";
|
||||
|
||||
import "./test-helpers.js";
|
||||
import crypto from "node:crypto";
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
@@ -13,8 +7,8 @@ import sharp from "sharp";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { WarelayConfig } from "../config/config.js";
|
||||
import * as commandQueue from "../process/command-queue.js";
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
import * as commandQueue from "../process/command-queue.js";
|
||||
import {
|
||||
HEARTBEAT_PROMPT,
|
||||
HEARTBEAT_TOKEN,
|
||||
@@ -25,8 +19,11 @@ import {
|
||||
stripHeartbeatToken,
|
||||
} from "./auto-reply.js";
|
||||
import type { sendMessageWeb } from "./outbound.js";
|
||||
import * as commandQueue from "../process/command-queue.js";
|
||||
import { getQueueSize } from "../process/command-queue.js";
|
||||
import {
|
||||
resetBaileysMocks,
|
||||
resetLoadConfigMock,
|
||||
setLoadConfigMock,
|
||||
} from "./test-helpers.js";
|
||||
|
||||
const makeSessionStore = async (
|
||||
entries: Record<string, unknown> = {},
|
||||
@@ -535,9 +532,7 @@ describe("web auto-reply", () => {
|
||||
const storePath = path.join(tmpDir, "sessions.json");
|
||||
await fs.writeFile(storePath, JSON.stringify({}));
|
||||
|
||||
const queueSpy = vi
|
||||
.spyOn(commandQueue, "getQueueSize")
|
||||
.mockReturnValue(2);
|
||||
const queueSpy = vi.spyOn(commandQueue, "getQueueSize").mockReturnValue(2);
|
||||
const replyResolver = vi.fn();
|
||||
const listenerFactory = vi.fn(async () => {
|
||||
const onClose = new Promise<void>(() => {
|
||||
|
||||
@@ -12,13 +12,13 @@ import {
|
||||
import { danger, info, isVerbose, logVerbose, success } from "../globals.js";
|
||||
import { logInfo } from "../logger.js";
|
||||
import { getChildLogger } from "../logging.js";
|
||||
import { enqueueCommand, getQueueSize } from "../process/command-queue.js";
|
||||
import { defaultRuntime, type RuntimeEnv } from "../runtime.js";
|
||||
import { normalizeE164 } from "../utils.js";
|
||||
import { monitorWebInbox } from "./inbound.js";
|
||||
import { sendViaIpc, startIpcServer, stopIpcServer } from "./ipc.js";
|
||||
import { loadWebMedia } from "./media.js";
|
||||
import { sendMessageWeb } from "./outbound.js";
|
||||
import { enqueueCommand, getQueueSize } from "../process/command-queue.js";
|
||||
import {
|
||||
computeBackoff,
|
||||
newConnectionId,
|
||||
@@ -697,9 +697,7 @@ export async function monitorWebProvider(
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(
|
||||
danger(
|
||||
`Failed sending web auto-reply to ${from}: ${String(err)}`,
|
||||
),
|
||||
danger(`Failed sending web auto-reply to ${from}: ${String(err)}`),
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -713,7 +711,8 @@ export async function monitorWebProvider(
|
||||
if (getQueueSize() === 0) {
|
||||
await processBatch(msg.from);
|
||||
} else {
|
||||
bucket.timer = bucket.timer ?? setTimeout(() => void processBatch(msg.from), 150);
|
||||
bucket.timer =
|
||||
bucket.timer ?? setTimeout(() => void processBatch(msg.from), 150);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -754,7 +753,12 @@ export async function monitorWebProvider(
|
||||
mediaBuffer = media.buffer;
|
||||
mediaType = media.contentType;
|
||||
}
|
||||
const result = await listener.sendMessage(to, message, mediaBuffer, mediaType);
|
||||
const result = await listener.sendMessage(
|
||||
to,
|
||||
message,
|
||||
mediaBuffer,
|
||||
mediaType,
|
||||
);
|
||||
// Add to echo detection so we don't process our own message
|
||||
if (message) {
|
||||
recentlySent.add(message);
|
||||
@@ -763,7 +767,10 @@ export async function monitorWebProvider(
|
||||
if (firstKey) recentlySent.delete(firstKey);
|
||||
}
|
||||
}
|
||||
logInfo(`📤 IPC send to ${to}: ${message.substring(0, 50)}...`, runtime);
|
||||
logInfo(
|
||||
`📤 IPC send to ${to}: ${message.substring(0, 50)}...`,
|
||||
runtime,
|
||||
);
|
||||
// Show typing indicator after send so user knows more may be coming
|
||||
try {
|
||||
await listener.sendComposingTo(to);
|
||||
@@ -807,7 +814,10 @@ export async function monitorWebProvider(
|
||||
|
||||
// Warn if no messages in 30+ minutes
|
||||
if (minutesSinceLastMessage && minutesSinceLastMessage > 30) {
|
||||
heartbeatLogger.warn(logData, "⚠️ web relay heartbeat - no messages in 30+ minutes");
|
||||
heartbeatLogger.warn(
|
||||
logData,
|
||||
"⚠️ web relay heartbeat - no messages in 30+ minutes",
|
||||
);
|
||||
} else {
|
||||
heartbeatLogger.info(logData, "web relay heartbeat");
|
||||
}
|
||||
@@ -818,7 +828,9 @@ export async function monitorWebProvider(
|
||||
if (lastMessageAt) {
|
||||
const timeSinceLastMessage = Date.now() - lastMessageAt;
|
||||
if (timeSinceLastMessage > MESSAGE_TIMEOUT_MS) {
|
||||
const minutesSinceLastMessage = Math.floor(timeSinceLastMessage / 60000);
|
||||
const minutesSinceLastMessage = Math.floor(
|
||||
timeSinceLastMessage / 60000,
|
||||
);
|
||||
heartbeatLogger.warn(
|
||||
{
|
||||
connectionId,
|
||||
@@ -978,7 +990,11 @@ export async function monitorWebProvider(
|
||||
// Apply response prefix if configured (same as regular messages)
|
||||
let finalText = stripped.text;
|
||||
const responsePrefix = cfg.inbound?.responsePrefix;
|
||||
if (responsePrefix && finalText && !finalText.startsWith(responsePrefix)) {
|
||||
if (
|
||||
responsePrefix &&
|
||||
finalText &&
|
||||
!finalText.startsWith(responsePrefix)
|
||||
) {
|
||||
finalText = `${responsePrefix} ${finalText}`;
|
||||
}
|
||||
|
||||
|
||||
@@ -103,8 +103,13 @@ export async function monitorWebInbox(options: {
|
||||
const isSamePhone = from === selfE164;
|
||||
|
||||
if (!isSamePhone && Array.isArray(allowFrom) && allowFrom.length > 0) {
|
||||
if (!allowFrom.includes("*") && !allowFrom.map(normalizeE164).includes(from)) {
|
||||
logVerbose(`Blocked unauthorized sender ${from} (not in allowFrom list)`);
|
||||
if (
|
||||
!allowFrom.includes("*") &&
|
||||
!allowFrom.map(normalizeE164).includes(from)
|
||||
) {
|
||||
logVerbose(
|
||||
`Blocked unauthorized sender ${from} (not in allowFrom list)`,
|
||||
);
|
||||
continue; // Skip processing entirely
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,13 +78,13 @@ export function startIpcServer(sendHandler: SendHandler): void {
|
||||
success: true,
|
||||
messageId: result.messageId,
|
||||
};
|
||||
conn.write(JSON.stringify(response) + "\n");
|
||||
conn.write(`${JSON.stringify(response)}\n`);
|
||||
} catch (err) {
|
||||
const response: IpcSendResponse = {
|
||||
success: false,
|
||||
error: String(err),
|
||||
};
|
||||
conn.write(JSON.stringify(response) + "\n");
|
||||
conn.write(`${JSON.stringify(response)}\n`);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
@@ -93,7 +93,7 @@ export function startIpcServer(sendHandler: SendHandler): void {
|
||||
success: false,
|
||||
error: "Invalid request format",
|
||||
};
|
||||
conn.write(JSON.stringify(response) + "\n");
|
||||
conn.write(`${JSON.stringify(response)}\n`);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -174,7 +174,7 @@ export async function sendViaIpc(
|
||||
message,
|
||||
mediaUrl,
|
||||
};
|
||||
client.write(JSON.stringify(request) + "\n");
|
||||
client.write(`${JSON.stringify(request)}\n`);
|
||||
});
|
||||
|
||||
client.on("data", (data) => {
|
||||
@@ -198,7 +198,7 @@ export async function sendViaIpc(
|
||||
}
|
||||
});
|
||||
|
||||
client.on("error", (err) => {
|
||||
client.on("error", (_err) => {
|
||||
if (!resolved) {
|
||||
resolved = true;
|
||||
clearTimeout(timeout);
|
||||
|
||||
@@ -251,7 +251,11 @@ describe("web monitor inbox", () => {
|
||||
type: "notify",
|
||||
messages: [
|
||||
{
|
||||
key: { id: "unauth1", fromMe: false, remoteJid: "999@s.whatsapp.net" },
|
||||
key: {
|
||||
id: "unauth1",
|
||||
fromMe: false,
|
||||
remoteJid: "999@s.whatsapp.net",
|
||||
},
|
||||
message: { conversation: "unauthorized message" },
|
||||
messageTimestamp: 1_700_000_000,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user