mirror of
https://github.com/openclaw/openclaw.git
synced 2026-03-12 07:20:45 +00:00
chore: Fix types.
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
import { EventEmitter } from "node:events";
|
import { EventEmitter } from "node:events";
|
||||||
import { vi } from "vitest";
|
import { vi } from "vitest";
|
||||||
|
|
||||||
|
type BaileysExports = typeof import("@whiskeysockets/baileys");
|
||||||
|
type FetchLatestBaileysVersionFn = BaileysExports["fetchLatestBaileysVersion"];
|
||||||
|
type MakeCacheableSignalKeyStoreFn = BaileysExports["makeCacheableSignalKeyStore"];
|
||||||
|
type MakeWASocketFn = BaileysExports["makeWASocket"];
|
||||||
|
type UseMultiFileAuthStateFn = BaileysExports["useMultiFileAuthState"];
|
||||||
|
type DownloadMediaMessageFn = BaileysExports["downloadMediaMessage"];
|
||||||
|
|
||||||
export type MockBaileysSocket = {
|
export type MockBaileysSocket = {
|
||||||
ev: EventEmitter;
|
ev: EventEmitter;
|
||||||
ws: { close: ReturnType<typeof vi.fn> };
|
ws: { close: ReturnType<typeof vi.fn> };
|
||||||
@@ -12,13 +19,13 @@ export type MockBaileysSocket = {
|
|||||||
|
|
||||||
export type MockBaileysModule = {
|
export type MockBaileysModule = {
|
||||||
DisconnectReason: { loggedOut: number };
|
DisconnectReason: { loggedOut: number };
|
||||||
fetchLatestBaileysVersion: ReturnType<typeof vi.fn>;
|
fetchLatestBaileysVersion: ReturnType<typeof vi.fn<FetchLatestBaileysVersionFn>>;
|
||||||
makeCacheableSignalKeyStore: ReturnType<typeof vi.fn>;
|
makeCacheableSignalKeyStore: ReturnType<typeof vi.fn<MakeCacheableSignalKeyStoreFn>>;
|
||||||
makeWASocket: ReturnType<typeof vi.fn>;
|
makeWASocket: ReturnType<typeof vi.fn<MakeWASocketFn>>;
|
||||||
useMultiFileAuthState: ReturnType<typeof vi.fn>;
|
useMultiFileAuthState: ReturnType<typeof vi.fn<UseMultiFileAuthStateFn>>;
|
||||||
jidToE164?: (jid: string) => string | null;
|
jidToE164?: (jid: string) => string | null;
|
||||||
proto?: unknown;
|
proto?: unknown;
|
||||||
downloadMediaMessage?: ReturnType<typeof vi.fn>;
|
downloadMediaMessage?: ReturnType<typeof vi.fn<DownloadMediaMessageFn>>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function createMockBaileys(): {
|
export function createMockBaileys(): {
|
||||||
@@ -26,7 +33,7 @@ export function createMockBaileys(): {
|
|||||||
lastSocket: () => MockBaileysSocket;
|
lastSocket: () => MockBaileysSocket;
|
||||||
} {
|
} {
|
||||||
const sockets: MockBaileysSocket[] = [];
|
const sockets: MockBaileysSocket[] = [];
|
||||||
const makeWASocket = vi.fn((_opts: unknown) => {
|
const makeWASocket = vi.fn<MakeWASocketFn>((_opts) => {
|
||||||
const ev = new EventEmitter();
|
const ev = new EventEmitter();
|
||||||
const sock: MockBaileysSocket = {
|
const sock: MockBaileysSocket = {
|
||||||
ev,
|
ev,
|
||||||
@@ -38,20 +45,22 @@ export function createMockBaileys(): {
|
|||||||
};
|
};
|
||||||
setImmediate(() => ev.emit("connection.update", { connection: "open" }));
|
setImmediate(() => ev.emit("connection.update", { connection: "open" }));
|
||||||
sockets.push(sock);
|
sockets.push(sock);
|
||||||
return sock;
|
return sock as unknown as ReturnType<MakeWASocketFn>;
|
||||||
});
|
});
|
||||||
|
|
||||||
const mod: MockBaileysModule = {
|
const mod: MockBaileysModule = {
|
||||||
DisconnectReason: { loggedOut: 401 },
|
DisconnectReason: { loggedOut: 401 },
|
||||||
fetchLatestBaileysVersion: vi.fn().mockResolvedValue({ version: [1, 2, 3] }),
|
fetchLatestBaileysVersion: vi
|
||||||
makeCacheableSignalKeyStore: vi.fn((keys: unknown) => keys),
|
.fn<FetchLatestBaileysVersionFn>()
|
||||||
|
.mockResolvedValue({ version: [1, 2, 3], isLatest: true }),
|
||||||
|
makeCacheableSignalKeyStore: vi.fn<MakeCacheableSignalKeyStoreFn>((keys) => keys),
|
||||||
makeWASocket,
|
makeWASocket,
|
||||||
useMultiFileAuthState: vi.fn(async () => ({
|
useMultiFileAuthState: vi.fn<UseMultiFileAuthStateFn>(async () => ({
|
||||||
state: { creds: {}, keys: {} },
|
state: { creds: {}, keys: {} } as Awaited<ReturnType<UseMultiFileAuthStateFn>>["state"],
|
||||||
saveCreds: vi.fn(),
|
saveCreds: vi.fn(),
|
||||||
})),
|
})),
|
||||||
jidToE164: (jid: string) => jid.replace(/@.*$/, "").replace(/^/, "+"),
|
jidToE164: (jid: string) => jid.replace(/@.*$/, "").replace(/^/, "+"),
|
||||||
downloadMediaMessage: vi.fn().mockResolvedValue(Buffer.from("img")),
|
downloadMediaMessage: vi.fn<DownloadMediaMessageFn>().mockResolvedValue(Buffer.from("img")),
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
Reference in New Issue
Block a user