Files
clawdbot/src/web/test-helpers.ts
Peter Steinberger 21ba0fb8a4 Fix test isolation to prevent loading real user config
Tests were picking up real ~/.warelay/warelay.json with emojis and
prefixes (like "🦞"), causing test assertions to fail. Added proper
config mocks to all test files.

Changes:
- Mock loadConfig() in index.core.test.ts, inbound.media.test.ts,
  monitor-inbox.test.ts
- Update test-helpers.ts default mock to disable all prefixes
- Tests now use clean config: no messagePrefix, no responsePrefix,
  no timestamp, allowFrom=["*"]

This ensures tests validate core behavior without user-specific config.
The responsePrefix feature itself is already fully config-driven - this
only fixes test isolation.
2025-11-30 18:00:57 +00:00

93 lines
2.6 KiB
TypeScript

import { vi } from "vitest";
import type { MockBaileysSocket } from "../../test/mocks/baileys.js";
import { createMockBaileys } from "../../test/mocks/baileys.js";
let loadConfigMock: () => unknown = () => ({
inbound: {
allowFrom: ["*"], // Allow all in tests by default
messagePrefix: undefined, // No message prefix in tests
responsePrefix: undefined, // No response prefix in tests
timestampPrefix: false, // No timestamp in tests
},
});
export function setLoadConfigMock(fn: () => unknown) {
loadConfigMock = fn;
}
export function resetLoadConfigMock() {
loadConfigMock = () => ({
inbound: {
allowFrom: ["*"],
messagePrefix: undefined,
responsePrefix: undefined,
timestampPrefix: false,
},
});
}
vi.mock("../config/config.js", () => ({
loadConfig: () => loadConfigMock(),
}));
vi.mock("../media/store.js", () => ({
saveMediaBuffer: vi
.fn()
.mockImplementation(async (_buf: Buffer, contentType?: string) => ({
id: "mid",
path: "/tmp/mid",
size: _buf.length,
contentType,
})),
}));
vi.mock("@whiskeysockets/baileys", () => {
const created = createMockBaileys();
(globalThis as Record<PropertyKey, unknown>)[
Symbol.for("warelay:lastSocket")
] = created.lastSocket;
return created.mod;
});
vi.mock("qrcode-terminal", () => ({
default: { generate: vi.fn() },
generate: vi.fn(),
}));
export const baileys = (await import(
"@whiskeysockets/baileys"
)) as unknown as typeof import("@whiskeysockets/baileys") & {
makeWASocket: ReturnType<typeof vi.fn>;
useMultiFileAuthState: ReturnType<typeof vi.fn>;
fetchLatestBaileysVersion: ReturnType<typeof vi.fn>;
makeCacheableSignalKeyStore: ReturnType<typeof vi.fn>;
};
export function resetBaileysMocks() {
const recreated = createMockBaileys();
(globalThis as Record<PropertyKey, unknown>)[
Symbol.for("warelay:lastSocket")
] = recreated.lastSocket;
baileys.makeWASocket.mockImplementation(recreated.mod.makeWASocket);
baileys.useMultiFileAuthState.mockImplementation(
recreated.mod.useMultiFileAuthState,
);
baileys.fetchLatestBaileysVersion.mockImplementation(
recreated.mod.fetchLatestBaileysVersion,
);
baileys.makeCacheableSignalKeyStore.mockImplementation(
recreated.mod.makeCacheableSignalKeyStore,
);
}
export function getLastSocket(): MockBaileysSocket {
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");
throw new Error("Invalid Baileys socket getter");
}