Fix provider-web mocks and make tests green

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

View File

@ -47,6 +47,7 @@ import {
normalizeE164, normalizeE164,
normalizePath, normalizePath,
sleep, sleep,
toWhatsappJid,
withWhatsAppPrefix, withWhatsAppPrefix,
} from "./utils.js"; } from "./utils.js";
@ -2161,7 +2162,7 @@ export {
uniqueBySid, uniqueBySid,
waitForFinalStatus, waitForFinalStatus,
waitForever, waitForever,
type toWhatsappJid, toWhatsappJid,
program, 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 type { MockBaileysSocket } from "../test/mocks/baileys.js";
import { createMockBaileys } from "../test/mocks/baileys.js"; import { createMockBaileys } from "../test/mocks/baileys.js";
const LAST_SOCKET_KEY = Symbol.for("warelay:lastSocket");
vi.mock("baileys", () => { vi.mock("baileys", () => {
const created = createMockBaileys(); const created = createMockBaileys();
(globalThis as Record<PropertyKey, unknown>)[LAST_SOCKET_KEY] = (globalThis as Record<PropertyKey, unknown>)[
created.lastSocket; Symbol.for("warelay:lastSocket")
] = created.lastSocket;
return created.mod; return created.mod;
}); });
function getLastSocket(): MockBaileysSocket { 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") if (typeof getter === "function")
return (getter as () => MockBaileysSocket)(); return (getter as () => MockBaileysSocket)();
if (!getter) throw new Error("Baileys mock not initialized"); if (!getter) throw new Error("Baileys mock not initialized");
@ -46,8 +47,9 @@ describe("provider-web", () => {
beforeEach(() => { beforeEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
const recreated = createMockBaileys(); const recreated = createMockBaileys();
(globalThis as Record<PropertyKey, unknown>)[LAST_SOCKET_KEY] = (globalThis as Record<PropertyKey, unknown>)[
recreated.lastSocket; Symbol.for("warelay:lastSocket")
] = recreated.lastSocket;
baileys.makeWASocket.mockImplementation(recreated.mod.makeWASocket); baileys.makeWASocket.mockImplementation(recreated.mod.makeWASocket);
baileys.useSingleFileAuthState.mockImplementation( baileys.useSingleFileAuthState.mockImplementation(
recreated.mod.useSingleFileAuthState, recreated.mod.useSingleFileAuthState,

View File

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