mirror of https://github.com/openclaw/openclaw.git
test: reduce discord component partial mocks
This commit is contained in:
parent
8e9607c064
commit
2da3b45ce7
|
|
@ -0,0 +1,5 @@
|
|||
export {
|
||||
readStoreAllowFromForDmPolicy,
|
||||
resolvePinnedMainDmOwnerFromAllowlist,
|
||||
} from "openclaw/plugin-sdk/security-runtime";
|
||||
export { upsertChannelPairingRequest } from "openclaw/plugin-sdk/conversation-runtime";
|
||||
|
|
@ -15,17 +15,20 @@ import { resolveCommandAuthorizedFromAuthorizers } from "openclaw/plugin-sdk/com
|
|||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { DiscordAccountConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import { isDangerousNameMatchingEnabled } from "openclaw/plugin-sdk/dangerous-name-runtime";
|
||||
import { resolveOpenProviderRuntimeGroupPolicy } from "openclaw/plugin-sdk/runtime-group-policy";
|
||||
import * as conversationRuntime from "openclaw/plugin-sdk/conversation-runtime";
|
||||
import { resolveAgentRoute } from "openclaw/plugin-sdk/routing";
|
||||
import { logVerbose } from "openclaw/plugin-sdk/runtime-env";
|
||||
import * as securityRuntime from "openclaw/plugin-sdk/security-runtime";
|
||||
import { resolveOpenProviderRuntimeGroupPolicy } from "openclaw/plugin-sdk/runtime-group-policy";
|
||||
import { logError } from "openclaw/plugin-sdk/text-runtime";
|
||||
import {
|
||||
parseDiscordComponentCustomId,
|
||||
parseDiscordModalCustomId,
|
||||
} from "../component-custom-id.js";
|
||||
import type { DiscordComponentEntry, DiscordModalEntry } from "../components.js";
|
||||
import {
|
||||
readStoreAllowFromForDmPolicy,
|
||||
resolvePinnedMainDmOwnerFromAllowlist,
|
||||
upsertChannelPairingRequest,
|
||||
} from "./agent-components-helpers.runtime.js";
|
||||
import {
|
||||
type DiscordGuildEntryResolved,
|
||||
isDiscordGroupAllowedByPolicy,
|
||||
|
|
@ -138,8 +141,6 @@ export function buildAgentSelectCustomId(componentId: string): string {
|
|||
return `${AGENT_SELECT_KEY}:componentId=${encodeURIComponent(componentId)}`;
|
||||
}
|
||||
|
||||
const { resolvePinnedMainDmOwnerFromAllowlist } = securityRuntime;
|
||||
|
||||
export function resolveAgentComponentRoute(params: {
|
||||
ctx: AgentComponentContext;
|
||||
rawGuildId: string | undefined;
|
||||
|
|
@ -512,7 +513,7 @@ async function ensureDmComponentAuthorized(params: {
|
|||
return false;
|
||||
}
|
||||
|
||||
const storeAllowFrom = await securityRuntime.readStoreAllowFromForDmPolicy({
|
||||
const storeAllowFrom = await readStoreAllowFromForDmPolicy({
|
||||
provider: "discord",
|
||||
accountId: ctx.accountId,
|
||||
dmPolicy,
|
||||
|
|
@ -525,13 +526,14 @@ async function ensureDmComponentAuthorized(params: {
|
|||
if (dmPolicy === "pairing") {
|
||||
const pairingResult = await createChannelPairingChallengeIssuer({
|
||||
channel: "discord",
|
||||
upsertPairingRequest: async ({ id, meta }) =>
|
||||
await conversationRuntime.upsertChannelPairingRequest({
|
||||
upsertPairingRequest: async ({ id, meta }) => {
|
||||
return await upsertChannelPairingRequest({
|
||||
channel: "discord",
|
||||
id,
|
||||
accountId: ctx.accountId,
|
||||
meta,
|
||||
}),
|
||||
});
|
||||
},
|
||||
})({
|
||||
senderId: user.id,
|
||||
senderIdLine: `Your Discord user id: ${user.id}`,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
export {
|
||||
buildPluginBindingResolvedText,
|
||||
parsePluginBindingApprovalCustomId,
|
||||
recordInboundSession,
|
||||
resolvePluginConversationBindingApproval,
|
||||
} from "openclaw/plugin-sdk/conversation-runtime";
|
||||
|
|
@ -86,9 +86,7 @@ import {
|
|||
import { buildDirectLabel, buildGuildLabel } from "./reply-context.js";
|
||||
import { deliverDiscordReply } from "./reply-delivery.js";
|
||||
|
||||
let conversationRuntimePromise:
|
||||
| Promise<typeof import("openclaw/plugin-sdk/conversation-runtime")>
|
||||
| undefined;
|
||||
let conversationRuntimePromise: Promise<typeof import("./agent-components.runtime.js")> | undefined;
|
||||
let componentsRuntimePromise: Promise<typeof import("../components.js")> | undefined;
|
||||
let pluginRuntimePromise: Promise<typeof import("openclaw/plugin-sdk/plugin-runtime")> | undefined;
|
||||
let replyRuntimePromise: Promise<typeof import("openclaw/plugin-sdk/reply-runtime")> | undefined;
|
||||
|
|
@ -98,7 +96,7 @@ let replyPipelineRuntimePromise:
|
|||
let typingRuntimePromise: Promise<typeof import("./typing.js")> | undefined;
|
||||
|
||||
async function loadConversationRuntime() {
|
||||
conversationRuntimePromise ??= import("openclaw/plugin-sdk/conversation-runtime");
|
||||
conversationRuntimePromise ??= import("./agent-components.runtime.js");
|
||||
return await conversationRuntimePromise;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,7 @@ import type { ButtonInteraction, ComponentData, StringSelectMenuInteraction } fr
|
|||
import { ChannelType } from "discord-api-types/v10";
|
||||
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import type { DiscordAccountConfig } from "openclaw/plugin-sdk/config-runtime";
|
||||
import * as conversationRuntime from "openclaw/plugin-sdk/conversation-runtime";
|
||||
import { buildAgentSessionKey } from "openclaw/plugin-sdk/routing";
|
||||
import * as securityRuntime from "openclaw/plugin-sdk/security-runtime";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { peekSystemEvents, resetSystemEventsForTest } from "../../../../src/infra/system-events.js";
|
||||
import { expectPairingReplyText } from "../../../../test/helpers/pairing-reply.js";
|
||||
|
|
@ -100,17 +98,6 @@ describe("agent components", () => {
|
|||
beforeEach(() => {
|
||||
resetDiscordComponentRuntimeMocks();
|
||||
resetSystemEventsForTest();
|
||||
vi.spyOn(securityRuntime, "readStoreAllowFromForDmPolicy").mockImplementation(
|
||||
async (params) => {
|
||||
if (params.shouldRead === false || params.dmPolicy === "allowlist") {
|
||||
return [];
|
||||
}
|
||||
return await readAllowFromStoreMock(params.provider, params.accountId);
|
||||
},
|
||||
);
|
||||
vi.spyOn(conversationRuntime, "upsertChannelPairingRequest").mockImplementation(
|
||||
upsertPairingRequestMock,
|
||||
);
|
||||
});
|
||||
|
||||
it("sends pairing reply when DM sender is not allowlisted", async () => {
|
||||
|
|
|
|||
|
|
@ -1,11 +1,13 @@
|
|||
import { vi } from "vitest";
|
||||
import { parsePluginBindingApprovalCustomId } from "../../../../src/plugins/conversation-binding.js";
|
||||
import { resolvePinnedMainDmOwnerFromAllowlist } from "../../../../src/security/dm-policy-shared.js";
|
||||
|
||||
const runtimeMocks = vi.hoisted(() => ({
|
||||
buildPluginBindingResolvedTextMock: vi.fn(),
|
||||
readAllowFromStoreMock: vi.fn(),
|
||||
upsertPairingRequestMock: vi.fn(),
|
||||
recordInboundSessionMock: vi.fn(),
|
||||
resolvePluginConversationBindingApprovalMock: vi.fn(),
|
||||
buildPluginBindingResolvedTextMock: vi.fn(),
|
||||
upsertPairingRequestMock: vi.fn(),
|
||||
}));
|
||||
|
||||
export const readAllowFromStoreMock = runtimeMocks.readAllowFromStoreMock;
|
||||
|
|
@ -15,31 +17,6 @@ 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),
|
||||
};
|
||||
}
|
||||
|
||||
async function createAllowFromRuntimeMock<TModule>(
|
||||
importOriginal: () => Promise<TModule>,
|
||||
): Promise<TModule & { readStoreAllowFromForDmPolicy: typeof readStoreAllowFromForDmPolicy }> {
|
||||
const actual = await importOriginal();
|
||||
return {
|
||||
...actual,
|
||||
readStoreAllowFromForDmPolicy,
|
||||
};
|
||||
}
|
||||
|
||||
async function readStoreAllowFromForDmPolicy(params: {
|
||||
provider: string;
|
||||
accountId: string;
|
||||
|
|
@ -52,22 +29,24 @@ async function readStoreAllowFromForDmPolicy(params: {
|
|||
return await readAllowFromStoreMock(params.provider, params.accountId);
|
||||
}
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/security-runtime", (importOriginal) =>
|
||||
createAllowFromRuntimeMock(importOriginal),
|
||||
);
|
||||
|
||||
vi.mock("openclaw/plugin-sdk/conversation-runtime", createConversationRuntimeMock);
|
||||
vi.mock("openclaw/plugin-sdk/conversation-runtime.js", createConversationRuntimeMock);
|
||||
vi.mock("../../../../src/pairing/pairing-store.js", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("../../../../src/pairing/pairing-store.js")>();
|
||||
vi.mock("../monitor/agent-components-helpers.runtime.js", () => {
|
||||
return {
|
||||
...actual,
|
||||
readStoreAllowFromForDmPolicy,
|
||||
resolvePinnedMainDmOwnerFromAllowlist,
|
||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
||||
};
|
||||
});
|
||||
vi.mock("../../../../src/security/dm-policy-shared.js", (importOriginal) =>
|
||||
createAllowFromRuntimeMock(importOriginal),
|
||||
);
|
||||
|
||||
vi.mock("../monitor/agent-components.runtime.js", () => {
|
||||
return {
|
||||
buildPluginBindingResolvedText: (...args: unknown[]) =>
|
||||
buildPluginBindingResolvedTextMock(...args),
|
||||
parsePluginBindingApprovalCustomId,
|
||||
recordInboundSession: (...args: unknown[]) => recordInboundSessionMock(...args),
|
||||
resolvePluginConversationBindingApproval: (...args: unknown[]) =>
|
||||
resolvePluginConversationBindingApprovalMock(...args),
|
||||
};
|
||||
});
|
||||
|
||||
export function resetDiscordComponentRuntimeMocks() {
|
||||
readAllowFromStoreMock.mockClear().mockResolvedValue([]);
|
||||
|
|
|
|||
Loading…
Reference in New Issue