Fix provider-web mocks and make tests green

This commit is contained in:
Peter Steinberger
2025-11-25 00:16:01 +01:00
parent 938e237411
commit 9dd257a92d
4 changed files with 16 additions and 16 deletions

View File

@@ -214,11 +214,10 @@ describe("twilio interactions", () => {
});
it("waitForFinalStatus exits on failure", async () => {
const runtime = {
const runtime: index.RuntimeEnv = {
error: vi.fn(),
exit: ((code: number) => {
throw new Error(`exit ${code}`);
}) as (code: number) => never,
exit: vi.fn() as unknown as (code: number) => never,
log: console.log,
};
const fetch = vi.fn().mockResolvedValue({ status: "failed" });
const client = {
@@ -520,9 +519,7 @@ describe("monitoring", () => {
it("ensureFunnel failure path exits via runtime", async () => {
const runtime: index.RuntimeEnv = {
error: vi.fn(),
exit: ((code: number) => {
throw new Error(`exit ${code}`);
}) as (code: number) => never,
exit: vi.fn() as unknown as (code: number) => never,
log: console.log,
};
const exec = vi.fn().mockRejectedValue({ stdout: "Funnel is not enabled" });

View File

@@ -47,6 +47,7 @@ import {
normalizeE164,
normalizePath,
sleep,
toWhatsappJid,
withWhatsAppPrefix,
} from "./utils.js";
@@ -2161,7 +2162,7 @@ export {
uniqueBySid,
waitForFinalStatus,
waitForever,
type toWhatsappJid,
toWhatsappJid,
program,
};

View File

@@ -3,17 +3,18 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { MockBaileysSocket } from "../test/mocks/baileys.js";
import { createMockBaileys } from "../test/mocks/baileys.js";
const LAST_SOCKET_KEY = Symbol.for("warelay:lastSocket");
vi.mock("baileys", () => {
const created = createMockBaileys();
(globalThis as Record<PropertyKey, unknown>)[LAST_SOCKET_KEY] =
created.lastSocket;
(globalThis as Record<PropertyKey, unknown>)[
Symbol.for("warelay:lastSocket")
] = created.lastSocket;
return created.mod;
});
function getLastSocket(): MockBaileysSocket {
const getter = (globalThis as Record<PropertyKey, unknown>)[LAST_SOCKET_KEY];
const getter = (globalThis as Record<PropertyKey, unknown>)[
Symbol.for("warelay:lastSocket")
];
if (typeof getter === "function")
return (getter as () => MockBaileysSocket)();
if (!getter) throw new Error("Baileys mock not initialized");
@@ -46,8 +47,9 @@ describe("provider-web", () => {
beforeEach(() => {
vi.clearAllMocks();
const recreated = createMockBaileys();
(globalThis as Record<PropertyKey, unknown>)[LAST_SOCKET_KEY] =
recreated.lastSocket;
(globalThis as Record<PropertyKey, unknown>)[
Symbol.for("warelay:lastSocket")
] = recreated.lastSocket;
baileys.makeWASocket.mockImplementation(recreated.mod.makeWASocket);
baileys.useSingleFileAuthState.mockImplementation(
recreated.mod.useSingleFileAuthState,

View File

@@ -22,7 +22,7 @@ const WA_WEB_AUTH_FILE = path.join(
export async function createWaSocket(printQr: boolean, verbose: boolean) {
await ensureDir(path.dirname(WA_WEB_AUTH_FILE));
const { state, saveState } = useSingleFileAuthState(WA_WEB_AUTH_FILE);
const { state, saveState } = await useSingleFileAuthState(WA_WEB_AUTH_FILE);
const { version } = await fetchLatestBaileysVersion();
const logger = pino({ level: verbose ? "info" : "silent" });
const sock = makeWASocket({