mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 06:42:44 +00:00
refactor: dedupe channel outbound and monitor tests
This commit is contained in:
@@ -2,7 +2,7 @@ import { EventEmitter } from "node:events";
|
||||
import fsSync from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { afterEach, beforeEach, vi } from "vitest";
|
||||
import { afterEach, beforeEach, expect, vi } from "vitest";
|
||||
import { resetLogger, setLoggerOverride } from "../logging.js";
|
||||
|
||||
// Avoid exporting vitest mock types (TS2742 under pnpm + d.ts emit).
|
||||
@@ -47,14 +47,18 @@ export type MockSock = {
|
||||
user: { id: string };
|
||||
};
|
||||
|
||||
function createResolvedMock() {
|
||||
return vi.fn().mockResolvedValue(undefined);
|
||||
}
|
||||
|
||||
function createMockSock(): MockSock {
|
||||
const ev = new EventEmitter();
|
||||
return {
|
||||
ev,
|
||||
ws: { close: vi.fn() },
|
||||
sendPresenceUpdate: vi.fn().mockResolvedValue(undefined),
|
||||
sendMessage: vi.fn().mockResolvedValue(undefined),
|
||||
readMessages: vi.fn().mockResolvedValue(undefined),
|
||||
sendPresenceUpdate: createResolvedMock(),
|
||||
sendMessage: createResolvedMock(),
|
||||
readMessages: createResolvedMock(),
|
||||
updateMediaMessage: vi.fn(),
|
||||
logger: {},
|
||||
signalRepository: {
|
||||
@@ -66,6 +70,15 @@ function createMockSock(): MockSock {
|
||||
};
|
||||
}
|
||||
|
||||
function getPairingStoreMocks() {
|
||||
const readChannelAllowFromStore = (...args: unknown[]) => readAllowFromStoreMock(...args);
|
||||
const upsertChannelPairingRequest = (...args: unknown[]) => upsertPairingRequestMock(...args);
|
||||
return {
|
||||
readChannelAllowFromStore,
|
||||
upsertChannelPairingRequest,
|
||||
};
|
||||
}
|
||||
|
||||
const sock: MockSock = createMockSock();
|
||||
|
||||
vi.mock("../media/store.js", () => ({
|
||||
@@ -85,10 +98,7 @@ vi.mock("../config/config.js", async (importOriginal) => {
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("../pairing/pairing-store.js", () => ({
|
||||
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
|
||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
||||
}));
|
||||
vi.mock("../pairing/pairing-store.js", () => getPairingStoreMocks());
|
||||
|
||||
vi.mock("./session.js", () => ({
|
||||
createWaSocket: vi.fn().mockResolvedValue(sock),
|
||||
@@ -100,6 +110,16 @@ export function getSock(): MockSock {
|
||||
return sock;
|
||||
}
|
||||
|
||||
export function expectPairingPromptSent(sock: MockSock, jid: string, senderE164: string) {
|
||||
expect(sock.sendMessage).toHaveBeenCalledTimes(1);
|
||||
expect(sock.sendMessage).toHaveBeenCalledWith(jid, {
|
||||
text: expect.stringContaining(`Your WhatsApp phone number: ${senderE164}`),
|
||||
});
|
||||
expect(sock.sendMessage).toHaveBeenCalledWith(jid, {
|
||||
text: expect.stringContaining("Pairing code: PAIRCODE"),
|
||||
});
|
||||
}
|
||||
|
||||
let authDir: string | undefined;
|
||||
|
||||
export function getAuthDir(): string {
|
||||
|
||||
Reference in New Issue
Block a user