mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 19:50:22 +00:00
refactor: dedupe test and script helpers
This commit is contained in:
85
test/helpers/extensions/discord-component-runtime.ts
Normal file
85
test/helpers/extensions/discord-component-runtime.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { vi } from "vitest";
|
||||
|
||||
const runtimeMocks = vi.hoisted(() => ({
|
||||
readAllowFromStoreMock: vi.fn(),
|
||||
upsertPairingRequestMock: vi.fn(),
|
||||
recordInboundSessionMock: vi.fn(),
|
||||
resolvePluginConversationBindingApprovalMock: vi.fn(),
|
||||
buildPluginBindingResolvedTextMock: vi.fn(),
|
||||
}));
|
||||
|
||||
export const readAllowFromStoreMock = runtimeMocks.readAllowFromStoreMock;
|
||||
export const upsertPairingRequestMock = runtimeMocks.upsertPairingRequestMock;
|
||||
export const recordInboundSessionMock = runtimeMocks.recordInboundSessionMock;
|
||||
export const resolvePluginConversationBindingApprovalMock =
|
||||
runtimeMocks.resolvePluginConversationBindingApprovalMock;
|
||||
export const buildPluginBindingResolvedTextMock = runtimeMocks.buildPluginBindingResolvedTextMock;
|
||||
|
||||
async function createConversationRuntimeMock(
|
||||
importOriginal: () => Promise<typeof import("openclaw/plugin-sdk/conversation-runtime")>,
|
||||
) {
|
||||
const actual = await importOriginal();
|
||||
return {
|
||||
...actual,
|
||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
||||
resolvePluginConversationBindingApproval: (...args: unknown[]) =>
|
||||
resolvePluginConversationBindingApprovalMock(...args),
|
||||
buildPluginBindingResolvedText: (...args: unknown[]) =>
|
||||
buildPluginBindingResolvedTextMock(...args),
|
||||
recordInboundSession: (...args: unknown[]) => recordInboundSessionMock(...args),
|
||||
};
|
||||
}
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/security-runtime", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("openclaw/plugin-sdk/security-runtime")>();
|
||||
return {
|
||||
...actual,
|
||||
readStoreAllowFromForDmPolicy: async (params: {
|
||||
provider: string;
|
||||
accountId: string;
|
||||
dmPolicy?: string | null;
|
||||
shouldRead?: boolean | null;
|
||||
}) => {
|
||||
if (params.shouldRead === false || params.dmPolicy === "allowlist") {
|
||||
return [];
|
||||
}
|
||||
return await readAllowFromStoreMock(params.provider, params.accountId);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/conversation-runtime", createConversationRuntimeMock);
|
||||
vi.mock("openclaw/plugin-sdk/conversation-runtime.js", createConversationRuntimeMock);
|
||||
|
||||
export function resetDiscordComponentRuntimeMocks() {
|
||||
readAllowFromStoreMock.mockClear().mockResolvedValue([]);
|
||||
upsertPairingRequestMock.mockClear().mockResolvedValue({ code: "PAIRCODE", created: true });
|
||||
recordInboundSessionMock.mockClear().mockResolvedValue(undefined);
|
||||
resolvePluginConversationBindingApprovalMock.mockReset().mockResolvedValue({
|
||||
status: "approved",
|
||||
binding: {
|
||||
bindingId: "binding-1",
|
||||
pluginId: "openclaw-codex-app-server",
|
||||
pluginName: "OpenClaw App Server",
|
||||
pluginRoot: "/plugins/codex",
|
||||
channel: "discord",
|
||||
accountId: "default",
|
||||
conversationId: "user:123456789",
|
||||
boundAt: Date.now(),
|
||||
},
|
||||
request: {
|
||||
id: "approval-1",
|
||||
pluginId: "openclaw-codex-app-server",
|
||||
pluginName: "OpenClaw App Server",
|
||||
pluginRoot: "/plugins/codex",
|
||||
requestedAt: Date.now(),
|
||||
conversation: {
|
||||
channel: "discord",
|
||||
accountId: "default",
|
||||
conversationId: "user:123456789",
|
||||
},
|
||||
},
|
||||
decision: "allow-once",
|
||||
});
|
||||
buildPluginBindingResolvedTextMock.mockReset().mockReturnValue("Binding approved.");
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
import { vi } from "vitest";
|
||||
import { expect, vi } from "vitest";
|
||||
import type { ResolvedZaloAccount } from "../../../extensions/zalo/src/accounts.js";
|
||||
import {
|
||||
clearZaloWebhookSecurityStateForTest,
|
||||
monitorZaloProvider,
|
||||
} from "../../../extensions/zalo/src/monitor.js";
|
||||
import type { PluginRuntime } from "../../../extensions/zalo/src/runtime-api.js";
|
||||
import type { OpenClawConfig } from "../../../extensions/zalo/src/runtime-api.js";
|
||||
import { normalizeSecretInputString } from "../../../extensions/zalo/src/secret-input.js";
|
||||
import { createEmptyPluginRegistry } from "../../../src/plugins/registry.js";
|
||||
import { setActivePluginRegistry } from "../../../src/plugins/runtime.js";
|
||||
import { withServer } from "../http-test-server.js";
|
||||
import { createPluginRuntimeMock } from "./plugin-runtime-mock.js";
|
||||
import { createRuntimeEnv } from "./runtime-env.js";
|
||||
|
||||
export { withServer };
|
||||
@@ -110,6 +112,19 @@ export function createLifecycleAccount(params: {
|
||||
} as ResolvedZaloAccount;
|
||||
}
|
||||
|
||||
export function createLifecycleMonitorSetup(params: {
|
||||
accountId: string;
|
||||
dmPolicy: "open" | "pairing";
|
||||
allowFrom?: string[];
|
||||
webhookUrl?: string;
|
||||
webhookSecret?: string;
|
||||
}) {
|
||||
return {
|
||||
account: createLifecycleAccount(params),
|
||||
config: createLifecycleConfig(params),
|
||||
};
|
||||
}
|
||||
|
||||
export function createTextUpdate(params: {
|
||||
messageId: string;
|
||||
userId: string;
|
||||
@@ -129,6 +144,131 @@ export function createTextUpdate(params: {
|
||||
};
|
||||
}
|
||||
|
||||
export function createImageUpdate(params?: {
|
||||
messageId?: string;
|
||||
userId?: string;
|
||||
displayName?: string;
|
||||
chatId?: string;
|
||||
photoUrl?: string;
|
||||
date?: number;
|
||||
}) {
|
||||
return {
|
||||
event_name: "message.image.received",
|
||||
message: {
|
||||
date: params?.date ?? 1774086023728,
|
||||
chat: { chat_type: "PRIVATE" as const, id: params?.chatId ?? "chat-123" },
|
||||
caption: "",
|
||||
message_id: params?.messageId ?? "msg-123",
|
||||
message_type: "CHAT_PHOTO",
|
||||
from: {
|
||||
id: params?.userId ?? "user-123",
|
||||
is_bot: false,
|
||||
display_name: params?.displayName ?? "Test User",
|
||||
},
|
||||
photo_url: params?.photoUrl ?? "https://example.com/test-image.jpg",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function setLifecycleRuntimeCore(
|
||||
channel: NonNullable<NonNullable<Parameters<typeof createPluginRuntimeMock>[0]>["channel"]>,
|
||||
) {
|
||||
getZaloRuntimeMock.mockReturnValue(
|
||||
createPluginRuntimeMock({
|
||||
channel,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export function createImageLifecycleCore() {
|
||||
const finalizeInboundContextMock = vi.fn((ctx: Record<string, unknown>) => ctx);
|
||||
const recordInboundSessionMock = vi.fn(async () => undefined);
|
||||
const fetchRemoteMediaMock = vi.fn(async () => ({
|
||||
buffer: Buffer.from("image-bytes"),
|
||||
contentType: "image/jpeg",
|
||||
}));
|
||||
const saveMediaBufferMock = vi.fn(async () => ({
|
||||
path: "/tmp/zalo-photo.jpg",
|
||||
contentType: "image/jpeg",
|
||||
}));
|
||||
const core = createPluginRuntimeMock({
|
||||
channel: {
|
||||
media: {
|
||||
fetchRemoteMedia:
|
||||
fetchRemoteMediaMock as unknown as PluginRuntime["channel"]["media"]["fetchRemoteMedia"],
|
||||
saveMediaBuffer:
|
||||
saveMediaBufferMock as unknown as PluginRuntime["channel"]["media"]["saveMediaBuffer"],
|
||||
},
|
||||
reply: {
|
||||
finalizeInboundContext:
|
||||
finalizeInboundContextMock as unknown as PluginRuntime["channel"]["reply"]["finalizeInboundContext"],
|
||||
dispatchReplyWithBufferedBlockDispatcher: vi.fn(
|
||||
async () => undefined,
|
||||
) as unknown as PluginRuntime["channel"]["reply"]["dispatchReplyWithBufferedBlockDispatcher"],
|
||||
},
|
||||
session: {
|
||||
recordInboundSession:
|
||||
recordInboundSessionMock as unknown as PluginRuntime["channel"]["session"]["recordInboundSession"],
|
||||
},
|
||||
commands: {
|
||||
shouldComputeCommandAuthorized: vi.fn(
|
||||
() => false,
|
||||
) as unknown as PluginRuntime["channel"]["commands"]["shouldComputeCommandAuthorized"],
|
||||
resolveCommandAuthorizedFromAuthorizers: vi.fn(
|
||||
() => false,
|
||||
) as unknown as PluginRuntime["channel"]["commands"]["resolveCommandAuthorizedFromAuthorizers"],
|
||||
isControlCommandMessage: vi.fn(
|
||||
() => false,
|
||||
) as unknown as PluginRuntime["channel"]["commands"]["isControlCommandMessage"],
|
||||
},
|
||||
},
|
||||
});
|
||||
return {
|
||||
core,
|
||||
finalizeInboundContextMock,
|
||||
recordInboundSessionMock,
|
||||
fetchRemoteMediaMock,
|
||||
saveMediaBufferMock,
|
||||
};
|
||||
}
|
||||
|
||||
export function expectImageLifecycleDelivery(params: {
|
||||
fetchRemoteMediaMock: ReturnType<typeof vi.fn>;
|
||||
saveMediaBufferMock: ReturnType<typeof vi.fn>;
|
||||
finalizeInboundContextMock: ReturnType<typeof vi.fn>;
|
||||
recordInboundSessionMock: ReturnType<typeof vi.fn>;
|
||||
photoUrl?: string;
|
||||
senderName?: string;
|
||||
mediaPath?: string;
|
||||
mediaType?: string;
|
||||
}) {
|
||||
const photoUrl = params.photoUrl ?? "https://example.com/test-image.jpg";
|
||||
const senderName = params.senderName ?? "Test User";
|
||||
const mediaPath = params.mediaPath ?? "/tmp/zalo-photo.jpg";
|
||||
const mediaType = params.mediaType ?? "image/jpeg";
|
||||
expect(params.fetchRemoteMediaMock).toHaveBeenCalledWith({
|
||||
url: photoUrl,
|
||||
maxBytes: 5 * 1024 * 1024,
|
||||
});
|
||||
expect(params.saveMediaBufferMock).toHaveBeenCalledTimes(1);
|
||||
expect(params.finalizeInboundContextMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
SenderName: senderName,
|
||||
MediaPath: mediaPath,
|
||||
MediaType: mediaType,
|
||||
}),
|
||||
);
|
||||
expect(params.recordInboundSessionMock).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
ctx: expect.objectContaining({
|
||||
SenderName: senderName,
|
||||
MediaPath: mediaPath,
|
||||
MediaType: mediaType,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
export async function settleAsyncWork(): Promise<void> {
|
||||
for (let i = 0; i < 6; i += 1) {
|
||||
await Promise.resolve();
|
||||
@@ -152,6 +292,21 @@ export async function postWebhookUpdate(params: {
|
||||
});
|
||||
}
|
||||
|
||||
export async function postWebhookReplay(params: {
|
||||
baseUrl: string;
|
||||
path: string;
|
||||
secret: string;
|
||||
payload: Record<string, unknown>;
|
||||
settleBeforeReplay?: boolean;
|
||||
}) {
|
||||
const first = await postWebhookUpdate(params);
|
||||
if (params.settleBeforeReplay) {
|
||||
await settleAsyncWork();
|
||||
}
|
||||
const replay = await postWebhookUpdate(params);
|
||||
return { first, replay };
|
||||
}
|
||||
|
||||
export async function startWebhookLifecycleMonitor(params: {
|
||||
account: ResolvedZaloAccount;
|
||||
config: OpenClawConfig;
|
||||
|
||||
Reference in New Issue
Block a user