mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 20:08:26 +00:00
refactor(test): dedupe slack slash mocks
This commit is contained in:
@@ -1,43 +1,17 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { registerSlackMonitorSlashCommands } from "./slash.js";
|
import { getSlackSlashMocks, resetSlackSlashMocks } from "./slash.test-harness.js";
|
||||||
|
|
||||||
const dispatchMock = vi.fn();
|
const { dispatchMock } = getSlackSlashMocks();
|
||||||
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: "" }),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
dispatchMock.mockReset().mockResolvedValue({ counts: { final: 1, tool: 0, block: 0 } });
|
resetSlackSlashMocks();
|
||||||
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
|
||||||
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
|
||||||
resolveAgentRouteMock.mockReset().mockReturnValue({
|
|
||||||
agentId: "main",
|
|
||||||
sessionKey: "session:1",
|
|
||||||
accountId: "acct",
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function registerCommands(ctx: unknown, account: unknown) {
|
||||||
|
const { registerSlackMonitorSlashCommands } = await import("./slash.js");
|
||||||
|
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
||||||
|
}
|
||||||
|
|
||||||
function encodeValue(parts: { command: string; arg: string; value: string; userId: string }) {
|
function encodeValue(parts: { command: string; arg: string; value: string; userId: string }) {
|
||||||
return [
|
return [
|
||||||
"cmdarg",
|
"cmdarg",
|
||||||
@@ -99,7 +73,7 @@ function createHarness() {
|
|||||||
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();
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const handler = commands.get("/usage");
|
const handler = commands.get("/usage");
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
@@ -130,7 +104,7 @@ describe("Slack native command argument menus", () => {
|
|||||||
|
|
||||||
it("dispatches the command when a menu button is clicked", async () => {
|
it("dispatches the command when a menu button is clicked", async () => {
|
||||||
const { actions, ctx, account } = createHarness();
|
const { actions, ctx, account } = createHarness();
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const handler = actions.get("openclaw_cmdarg");
|
const handler = actions.get("openclaw_cmdarg");
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
@@ -158,7 +132,7 @@ describe("Slack native command argument menus", () => {
|
|||||||
|
|
||||||
it("rejects menu clicks from other users", async () => {
|
it("rejects menu clicks from other users", async () => {
|
||||||
const { actions, ctx, account } = createHarness();
|
const { actions, ctx, account } = createHarness();
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const handler = actions.get("openclaw_cmdarg");
|
const handler = actions.get("openclaw_cmdarg");
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
@@ -188,7 +162,7 @@ describe("Slack native command argument menus", () => {
|
|||||||
|
|
||||||
it("falls back to postEphemeral with token when respond is unavailable", async () => {
|
it("falls back to postEphemeral with token when respond is unavailable", async () => {
|
||||||
const { actions, postEphemeral, ctx, account } = createHarness();
|
const { actions, postEphemeral, ctx, account } = createHarness();
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const handler = actions.get("openclaw_cmdarg");
|
const handler = actions.get("openclaw_cmdarg");
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
@@ -212,7 +186,7 @@ describe("Slack native command argument menus", () => {
|
|||||||
|
|
||||||
it("treats malformed percent-encoding as an invalid button (no throw)", async () => {
|
it("treats malformed percent-encoding as an invalid button (no throw)", async () => {
|
||||||
const { actions, postEphemeral, ctx, account } = createHarness();
|
const { actions, postEphemeral, ctx, account } = createHarness();
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const handler = actions.get("openclaw_cmdarg");
|
const handler = actions.get("openclaw_cmdarg");
|
||||||
if (!handler) {
|
if (!handler) {
|
||||||
|
|||||||
@@ -1,43 +1,17 @@
|
|||||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
import { registerSlackMonitorSlashCommands } from "./slash.js";
|
import { getSlackSlashMocks, resetSlackSlashMocks } from "./slash.test-harness.js";
|
||||||
|
|
||||||
const dispatchMock = vi.fn();
|
const { dispatchMock } = getSlackSlashMocks();
|
||||||
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: "" }),
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
dispatchMock.mockReset().mockResolvedValue({ counts: { final: 1, tool: 0, block: 0 } });
|
resetSlackSlashMocks();
|
||||||
readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
|
||||||
upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
|
||||||
resolveAgentRouteMock.mockReset().mockReturnValue({
|
|
||||||
agentId: "main",
|
|
||||||
sessionKey: "session:1",
|
|
||||||
accountId: "acct",
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function registerCommands(ctx: unknown, account: unknown) {
|
||||||
|
const { registerSlackMonitorSlashCommands } = await import("./slash.js");
|
||||||
|
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
||||||
|
}
|
||||||
|
|
||||||
function createHarness(overrides?: {
|
function createHarness(overrides?: {
|
||||||
groupPolicy?: "open" | "allowlist";
|
groupPolicy?: "open" | "allowlist";
|
||||||
channelsConfig?: Record<string, { allow?: boolean; requireMention?: boolean }>;
|
channelsConfig?: Record<string, { allow?: boolean; requireMention?: boolean }>;
|
||||||
@@ -136,7 +110,7 @@ describe("slack slash commands channel policy", () => {
|
|||||||
channelId: "C_UNLISTED",
|
channelId: "C_UNLISTED",
|
||||||
channelName: "unlisted",
|
channelName: "unlisted",
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const { respond } = await runSlashHandler({
|
const { respond } = await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
@@ -159,7 +133,7 @@ describe("slack slash commands channel policy", () => {
|
|||||||
channelId: "C_DENIED",
|
channelId: "C_DENIED",
|
||||||
channelName: "denied",
|
channelName: "denied",
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const { respond } = await runSlashHandler({
|
const { respond } = await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
@@ -183,7 +157,7 @@ describe("slack slash commands channel policy", () => {
|
|||||||
channelId: "C_UNLISTED",
|
channelId: "C_UNLISTED",
|
||||||
channelName: "unlisted",
|
channelName: "unlisted",
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const { respond } = await runSlashHandler({
|
const { respond } = await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
@@ -209,7 +183,7 @@ describe("slack slash commands access groups", () => {
|
|||||||
channelName: "unknown",
|
channelName: "unknown",
|
||||||
resolveChannelName: async () => ({}),
|
resolveChannelName: async () => ({}),
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const { respond } = await runSlashHandler({
|
const { respond } = await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
@@ -233,7 +207,7 @@ describe("slack slash commands access groups", () => {
|
|||||||
channelName: "notdirectmessage",
|
channelName: "notdirectmessage",
|
||||||
resolveChannelName: async () => ({}),
|
resolveChannelName: async () => ({}),
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const { respond } = await runSlashHandler({
|
const { respond } = await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
@@ -260,7 +234,7 @@ describe("slack slash commands access groups", () => {
|
|||||||
channelName: "directmessage",
|
channelName: "directmessage",
|
||||||
resolveChannelName: async () => ({ name: "directmessage", type: "im" }),
|
resolveChannelName: async () => ({ name: "directmessage", type: "im" }),
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
await runSlashHandler({
|
await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
@@ -286,7 +260,7 @@ describe("slack slash commands access groups", () => {
|
|||||||
channelName: "private",
|
channelName: "private",
|
||||||
resolveChannelName: async () => ({}),
|
resolveChannelName: async () => ({}),
|
||||||
});
|
});
|
||||||
registerSlackMonitorSlashCommands({ ctx: ctx as never, account: account as never });
|
await registerCommands(ctx, account);
|
||||||
|
|
||||||
const { respond } = await runSlashHandler({
|
const { respond } = await runSlashHandler({
|
||||||
commands,
|
commands,
|
||||||
|
|||||||
44
src/slack/monitor/slash.test-harness.ts
Normal file
44
src/slack/monitor/slash.test-harness.ts
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
import { vi } from "vitest";
|
||||||
|
|
||||||
|
const mocks = vi.hoisted(() => ({
|
||||||
|
dispatchMock: vi.fn(),
|
||||||
|
readAllowFromStoreMock: vi.fn(),
|
||||||
|
upsertPairingRequestMock: vi.fn(),
|
||||||
|
resolveAgentRouteMock: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../auto-reply/reply/provider-dispatcher.js", () => ({
|
||||||
|
dispatchReplyWithDispatcher: (...args: unknown[]) => mocks.dispatchMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../pairing/pairing-store.js", () => ({
|
||||||
|
readChannelAllowFromStore: (...args: unknown[]) => mocks.readAllowFromStoreMock(...args),
|
||||||
|
upsertChannelPairingRequest: (...args: unknown[]) => mocks.upsertPairingRequestMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../routing/resolve-route.js", () => ({
|
||||||
|
resolveAgentRoute: (...args: unknown[]) => mocks.resolveAgentRouteMock(...args),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("../../agents/identity.js", async (importOriginal) => {
|
||||||
|
const actual = await importOriginal<typeof import("../../agents/identity.js")>();
|
||||||
|
return {
|
||||||
|
...actual,
|
||||||
|
resolveEffectiveMessagesConfig: () => ({ responsePrefix: "" }),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
export function getSlackSlashMocks() {
|
||||||
|
return mocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resetSlackSlashMocks() {
|
||||||
|
mocks.dispatchMock.mockReset().mockResolvedValue({ counts: { final: 1, tool: 0, block: 0 } });
|
||||||
|
mocks.readAllowFromStoreMock.mockReset().mockResolvedValue([]);
|
||||||
|
mocks.upsertPairingRequestMock.mockReset().mockResolvedValue({ code: "PAIRCODE", created: true });
|
||||||
|
mocks.resolveAgentRouteMock.mockReset().mockReturnValue({
|
||||||
|
agentId: "main",
|
||||||
|
sessionKey: "session:1",
|
||||||
|
accountId: "acct",
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user