style(tests): format imports
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import fs from "node:fs/promises";
|
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
|
import { type AddressInfo, createServer } from "node:net";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { type AddressInfo, createServer } from "node:net";
|
|
||||||
import { describe, expect, test, vi } from "vitest";
|
import { describe, expect, test, vi } from "vitest";
|
||||||
import { WebSocket } from "ws";
|
import { WebSocket } from "ws";
|
||||||
import { agentCommand } from "../commands/agent.js";
|
import { agentCommand } from "../commands/agent.js";
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
export class GatewayLockError extends Error {
|
export class GatewayLockError extends Error {
|
||||||
constructor(message: string, public readonly cause?: unknown) {
|
constructor(
|
||||||
|
message: string,
|
||||||
|
public readonly cause?: unknown,
|
||||||
|
) {
|
||||||
super(message);
|
super(message);
|
||||||
this.name = "GatewayLockError";
|
this.name = "GatewayLockError";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import os from "node:os";
|
|||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import sharp from "sharp";
|
import sharp from "sharp";
|
||||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
import { getReplyFromConfig } from "../auto-reply/reply.js";
|
||||||
import type { ClawdisConfig } from "../config/config.js";
|
import type { ClawdisConfig } from "../config/config.js";
|
||||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||||
import * as commandQueue from "../process/command-queue.js";
|
import * as commandQueue from "../process/command-queue.js";
|
||||||
@@ -18,7 +18,6 @@ import {
|
|||||||
runWebHeartbeatOnce,
|
runWebHeartbeatOnce,
|
||||||
stripHeartbeatToken,
|
stripHeartbeatToken,
|
||||||
} from "./auto-reply.js";
|
} from "./auto-reply.js";
|
||||||
import { getReplyFromConfig } from "../auto-reply/reply.js";
|
|
||||||
import type { sendMessageWhatsApp } from "./outbound.js";
|
import type { sendMessageWhatsApp } from "./outbound.js";
|
||||||
import {
|
import {
|
||||||
resetBaileysMocks,
|
resetBaileysMocks,
|
||||||
@@ -565,48 +564,52 @@ describe("web auto-reply", () => {
|
|||||||
await run;
|
await run;
|
||||||
});
|
});
|
||||||
|
|
||||||
it("stops after hitting max reconnect attempts", { timeout: 20000 }, async () => {
|
it(
|
||||||
const closeResolvers: Array<() => void> = [];
|
"stops after hitting max reconnect attempts",
|
||||||
const sleep = vi.fn(async () => {});
|
{ timeout: 20000 },
|
||||||
const listenerFactory = vi.fn(async () => {
|
async () => {
|
||||||
const onClose = new Promise<void>((res) => closeResolvers.push(res));
|
const closeResolvers: Array<() => void> = [];
|
||||||
return { close: vi.fn(), onClose };
|
const sleep = vi.fn(async () => {});
|
||||||
});
|
const listenerFactory = vi.fn(async () => {
|
||||||
const runtime = {
|
const onClose = new Promise<void>((res) => closeResolvers.push(res));
|
||||||
log: vi.fn(),
|
return { close: vi.fn(), onClose };
|
||||||
error: vi.fn(),
|
});
|
||||||
exit: vi.fn(),
|
const runtime = {
|
||||||
};
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
exit: vi.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
const run = monitorWebProvider(
|
const run = monitorWebProvider(
|
||||||
false,
|
false,
|
||||||
listenerFactory,
|
listenerFactory,
|
||||||
true,
|
true,
|
||||||
async () => ({ text: "ok" }),
|
async () => ({ text: "ok" }),
|
||||||
runtime as never,
|
runtime as never,
|
||||||
undefined,
|
undefined,
|
||||||
{
|
{
|
||||||
heartbeatSeconds: 1,
|
heartbeatSeconds: 1,
|
||||||
reconnect: { initialMs: 5, maxMs: 5, maxAttempts: 2, factor: 1.1 },
|
reconnect: { initialMs: 5, maxMs: 5, maxAttempts: 2, factor: 1.1 },
|
||||||
sleep,
|
sleep,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
await Promise.resolve();
|
await Promise.resolve();
|
||||||
expect(listenerFactory).toHaveBeenCalledTimes(1);
|
expect(listenerFactory).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
closeResolvers.shift()?.();
|
closeResolvers.shift()?.();
|
||||||
await new Promise((resolve) => setTimeout(resolve, 15));
|
await new Promise((resolve) => setTimeout(resolve, 15));
|
||||||
expect(listenerFactory).toHaveBeenCalledTimes(2);
|
expect(listenerFactory).toHaveBeenCalledTimes(2);
|
||||||
|
|
||||||
closeResolvers.shift()?.();
|
closeResolvers.shift()?.();
|
||||||
await new Promise((resolve) => setTimeout(resolve, 15));
|
await new Promise((resolve) => setTimeout(resolve, 15));
|
||||||
await run;
|
await run;
|
||||||
|
|
||||||
expect(runtime.error).toHaveBeenCalledWith(
|
expect(runtime.error).toHaveBeenCalledWith(
|
||||||
expect.stringContaining("max attempts reached"),
|
expect.stringContaining("max attempts reached"),
|
||||||
);
|
);
|
||||||
});
|
},
|
||||||
|
);
|
||||||
|
|
||||||
it("skips reply heartbeat when requests are running", async () => {
|
it("skips reply heartbeat when requests are running", async () => {
|
||||||
const tmpDir = await fs.mkdtemp(
|
const tmpDir = await fs.mkdtemp(
|
||||||
|
|||||||
@@ -553,59 +553,67 @@ describe("web monitor inbox", () => {
|
|||||||
await listener.close();
|
await listener.close();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
it("defaults to self-only when no config is present", async () => {
|
it("defaults to self-only when no config is present", async () => {
|
||||||
// No config file => allowFrom should be derived from selfE164
|
// No config file => allowFrom should be derived from selfE164
|
||||||
mockLoadConfig.mockReturnValue({});
|
mockLoadConfig.mockReturnValue({});
|
||||||
|
|
||||||
const onMessage = vi.fn();
|
const onMessage = vi.fn();
|
||||||
const listener = await monitorWebInbox({ verbose: false, onMessage });
|
const listener = await monitorWebInbox({ verbose: false, onMessage });
|
||||||
const sock = await createWaSocket();
|
const sock = await createWaSocket();
|
||||||
|
|
||||||
// Message from someone else should be blocked
|
// Message from someone else should be blocked
|
||||||
const upsertBlocked = {
|
const upsertBlocked = {
|
||||||
type: "notify",
|
type: "notify",
|
||||||
messages: [
|
messages: [
|
||||||
{
|
{
|
||||||
key: { id: "no-config-1", fromMe: false, remoteJid: "999@s.whatsapp.net" },
|
key: {
|
||||||
message: { conversation: "ping" },
|
id: "no-config-1",
|
||||||
messageTimestamp: 1_700_000_000,
|
fromMe: false,
|
||||||
|
remoteJid: "999@s.whatsapp.net",
|
||||||
},
|
},
|
||||||
],
|
message: { conversation: "ping" },
|
||||||
};
|
messageTimestamp: 1_700_000_000,
|
||||||
|
|
||||||
sock.ev.emit("messages.upsert", upsertBlocked);
|
|
||||||
await new Promise((resolve) => setImmediate(resolve));
|
|
||||||
expect(onMessage).not.toHaveBeenCalled();
|
|
||||||
|
|
||||||
// Message from self should be allowed
|
|
||||||
const upsertSelf = {
|
|
||||||
type: "notify",
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
key: { id: "no-config-2", fromMe: false, remoteJid: "123@s.whatsapp.net" },
|
|
||||||
message: { conversation: "self ping" },
|
|
||||||
messageTimestamp: 1_700_000_001,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
sock.ev.emit("messages.upsert", upsertSelf);
|
|
||||||
await new Promise((resolve) => setImmediate(resolve));
|
|
||||||
|
|
||||||
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
||||||
expect(onMessage).toHaveBeenCalledWith(
|
|
||||||
expect.objectContaining({ body: "self ping", from: "+123", to: "+123" }),
|
|
||||||
);
|
|
||||||
|
|
||||||
// Reset mock for other tests
|
|
||||||
mockLoadConfig.mockReturnValue({
|
|
||||||
inbound: {
|
|
||||||
allowFrom: ["*"],
|
|
||||||
messagePrefix: undefined,
|
|
||||||
responsePrefix: undefined,
|
|
||||||
timestampPrefix: false,
|
|
||||||
},
|
},
|
||||||
});
|
],
|
||||||
|
};
|
||||||
|
|
||||||
await listener.close();
|
sock.ev.emit("messages.upsert", upsertBlocked);
|
||||||
|
await new Promise((resolve) => setImmediate(resolve));
|
||||||
|
expect(onMessage).not.toHaveBeenCalled();
|
||||||
|
|
||||||
|
// Message from self should be allowed
|
||||||
|
const upsertSelf = {
|
||||||
|
type: "notify",
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
key: {
|
||||||
|
id: "no-config-2",
|
||||||
|
fromMe: false,
|
||||||
|
remoteJid: "123@s.whatsapp.net",
|
||||||
|
},
|
||||||
|
message: { conversation: "self ping" },
|
||||||
|
messageTimestamp: 1_700_000_001,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
sock.ev.emit("messages.upsert", upsertSelf);
|
||||||
|
await new Promise((resolve) => setImmediate(resolve));
|
||||||
|
|
||||||
|
expect(onMessage).toHaveBeenCalledTimes(1);
|
||||||
|
expect(onMessage).toHaveBeenCalledWith(
|
||||||
|
expect.objectContaining({ body: "self ping", from: "+123", to: "+123" }),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reset mock for other tests
|
||||||
|
mockLoadConfig.mockReturnValue({
|
||||||
|
inbound: {
|
||||||
|
allowFrom: ["*"],
|
||||||
|
messagePrefix: undefined,
|
||||||
|
responsePrefix: undefined,
|
||||||
|
timestampPrefix: false,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
await listener.close();
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user