mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 22:04:31 +00:00
refactor(test): share slack slash mocks
This commit is contained in:
@@ -1,31 +1,8 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { registerSlackMonitorSlashCommands } from "./slash.js";
|
import { registerSlackMonitorSlashCommands } from "./slash.js";
|
||||||
|
import { getDispatchMock } from "./slash.test-mocks.js";
|
||||||
|
|
||||||
const dispatchMock = vi.fn();
|
const dispatchMock = getDispatchMock();
|
||||||
const readAllowFromStoreMock = vi.fn();
|
|
||||||
const upsertPairingRequestMock = vi.fn();
|
|
||||||
const resolveAgentRouteMock = vi.fn();
|
|
||||||
|
|
||||||
vi.mock("../../auto-reply/reply/provider-dispatcher.js", () => ({
|
|
||||||
dispatchReplyWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
|
|
||||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../routing/resolve-route.js", () => ({
|
|
||||||
resolveAgentRoute: (...args: unknown[]) => resolveAgentRouteMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../agents/identity.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../../agents/identity.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveEffectiveMessagesConfig: () => ({ responsePrefix: "" }),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
function encodeValue(parts: { command: string; arg: string; value: string; userId: string }) {
|
function encodeValue(parts: { command: string; arg: string; value: string; userId: string }) {
|
||||||
return [
|
return [
|
||||||
@@ -85,17 +62,6 @@ function createHarness() {
|
|||||||
return { commands, actions, postEphemeral, ctx, account };
|
return { commands, actions, postEphemeral, ctx, account };
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
dispatchMock.mockReset().mockResolvedValue({ counts: { final: 1, tool: 0, block: 0 } });
|
|
||||||
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
|
||||||
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
|
||||||
resolveAgentRouteMock.mockReset().mockReturnValue({
|
|
||||||
agentId: "main",
|
|
||||||
sessionKey: "session:1",
|
|
||||||
accountId: "acct",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Slack native command argument menus", () => {
|
describe("Slack native command argument menus", () => {
|
||||||
it("shows a button menu when required args are omitted", async () => {
|
it("shows a button menu when required args are omitted", async () => {
|
||||||
const { commands, ctx, account } = createHarness();
|
const { commands, ctx, account } = createHarness();
|
||||||
|
|||||||
@@ -1,31 +1,8 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
import { registerSlackMonitorSlashCommands } from "./slash.js";
|
import { registerSlackMonitorSlashCommands } from "./slash.js";
|
||||||
|
import { getDispatchMock } from "./slash.test-mocks.js";
|
||||||
|
|
||||||
const dispatchMock = vi.fn();
|
const dispatchMock = getDispatchMock();
|
||||||
const readAllowFromStoreMock = vi.fn();
|
|
||||||
const upsertPairingRequestMock = vi.fn();
|
|
||||||
const resolveAgentRouteMock = vi.fn();
|
|
||||||
|
|
||||||
vi.mock("../../auto-reply/reply/provider-dispatcher.js", () => ({
|
|
||||||
dispatchReplyWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../pairing/pairing-store.js", () => ({
|
|
||||||
readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
|
|
||||||
upsertChannelPairingRequest: (...args: unknown[]) => upsertPairingRequestMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../routing/resolve-route.js", () => ({
|
|
||||||
resolveAgentRoute: (...args: unknown[]) => resolveAgentRouteMock(...args),
|
|
||||||
}));
|
|
||||||
|
|
||||||
vi.mock("../../agents/identity.js", async (importOriginal) => {
|
|
||||||
const actual = await importOriginal<typeof import("../../agents/identity.js")>();
|
|
||||||
return {
|
|
||||||
...actual,
|
|
||||||
resolveEffectiveMessagesConfig: () => ({ responsePrefix: "" }),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
function createHarness(overrides?: {
|
function createHarness(overrides?: {
|
||||||
groupPolicy?: "open" | "allowlist";
|
groupPolicy?: "open" | "allowlist";
|
||||||
@@ -117,17 +94,6 @@ async function runSlashHandler(params: {
|
|||||||
return { respond, ack };
|
return { respond, ack };
|
||||||
}
|
}
|
||||||
|
|
||||||
beforeEach(() => {
|
|
||||||
dispatchMock.mockReset().mockResolvedValue({ counts: { final: 1, tool: 0, block: 0 } });
|
|
||||||
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
|
||||||
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
|
||||||
resolveAgentRouteMock.mockReset().mockReturnValue({
|
|
||||||
agentId: "main",
|
|
||||||
sessionKey: "session:1",
|
|
||||||
accountId: "acct",
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("slack slash commands channel policy", () => {
|
describe("slack slash commands channel policy", () => {
|
||||||
it("allows unlisted channels when groupPolicy is open", async () => {
|
it("allows unlisted channels when groupPolicy is open", async () => {
|
||||||
const { commands, ctx, account, channelId, channelName } = createHarness({
|
const { commands, ctx, account, channelId, channelName } = createHarness({
|
||||||
|
|||||||
62
src/slack/monitor/slash.test-mocks.ts
Normal file
62
src/slack/monitor/slash.test-mocks.ts
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
import { beforeEach, vi } from "vitest";
|
||||||
|
|
||||||
|
// Avoid exporting vitest mock types (TS2742 under pnpm + d.ts emit).
|
||||||
|
// oxlint-disable-next-line typescript/no-explicit-any
|
||||||
|
type AnyMock = any;
|
||||||
|
|
||||||
|
const hoisted = vi.hoisted(() => ({
|
||||||
|
dispatchMock: vi.fn(),
|
||||||
|
readAllowFromStoreMock: vi.fn(),
|
||||||
|
upsertPairingRequestMock: vi.fn(),
|
||||||
|
resolveAgentRouteMock: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
export function getDispatchMock(): AnyMock {
|
||||||
|
return hoisted.dispatchMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getReadAllowFromStoreMock(): AnyMock {
|
||||||
|
return hoisted.readAllowFromStoreMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getUpsertPairingRequestMock(): AnyMock {
|
||||||
|
return hoisted.upsertPairingRequestMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getResolveAgentRouteMock(): AnyMock {
|
||||||
|
return hoisted.resolveAgentRouteMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
vi.mock("../../auto-reply/reply/provider-dispatcher.js", () => ({
|
||||||
|
dispatchReplyWithDispatcher: (...args: unknown[]) => hoisted.dispatchMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../pairing/pairing-store.js", () => ({
|
||||||
|
readChannelAllowFromStore: (...args: unknown[]) => hoisted.readAllowFromStoreMock(...args),
|
||||||
|
upsertChannelPairingRequest: (...args: unknown[]) => hoisted.upsertPairingRequestMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../routing/resolve-route.js", () => ({
|
||||||
|
resolveAgentRoute: (...args: unknown[]) => hoisted.resolveAgentRouteMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../agents/identity.js", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("../../agents/identity.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
resolveEffectiveMessagesConfig: () => ({ responsePrefix: "" }),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
hoisted.dispatchMock.mockReset().mockResolvedValue({ counts: { final: 1, tool: 0, block: 0 } });
|
||||||
|
hoisted.readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
||||||
|
hoisted.upsertPairingRequestMock
|
||||||
|
.mockReset()
|
||||||
|
.mockResolvedValue({ code: "PAIRCODE", created: true });
|
||||||
|
hoisted.resolveAgentRouteMock.mockReset().mockReturnValue({
|
||||||
|
agentId: "main",
|
||||||
|
sessionKey: "session:1",
|
||||||
|
accountId: "acct",
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user