mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-19 11:08:37 +00:00
refactor(test): dedupe discord category handler setup
This commit is contained in:
@@ -5,6 +5,8 @@ import { createDiscordMessageHandler } from "./monitor.js";
|
|||||||
import { __resetDiscordChannelInfoCacheForTest } from "./monitor/message-utils.js";
|
import { __resetDiscordChannelInfoCacheForTest } from "./monitor/message-utils.js";
|
||||||
import { __resetDiscordThreadStarterCacheForTest } from "./monitor/threading.js";
|
import { __resetDiscordThreadStarterCacheForTest } from "./monitor/threading.js";
|
||||||
|
|
||||||
|
type Config = ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
|
|
||||||
const sendMock = vi.fn();
|
const sendMock = vi.fn();
|
||||||
const reactMock = vi.fn();
|
const reactMock = vi.fn();
|
||||||
const updateLastRouteMock = vi.fn();
|
const updateLastRouteMock = vi.fn();
|
||||||
@@ -54,6 +56,66 @@ beforeEach(() => {
|
|||||||
__resetDiscordThreadStarterCacheForTest();
|
__resetDiscordThreadStarterCacheForTest();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const CATEGORY_GUILD_CFG = {
|
||||||
|
agents: {
|
||||||
|
defaults: {
|
||||||
|
model: "anthropic/claude-opus-4-5",
|
||||||
|
workspace: "/tmp/openclaw",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
session: { store: "/tmp/openclaw-sessions.json" },
|
||||||
|
channels: {
|
||||||
|
discord: {
|
||||||
|
dm: { enabled: true, policy: "open" },
|
||||||
|
guilds: {
|
||||||
|
"*": {
|
||||||
|
requireMention: false,
|
||||||
|
channels: { c1: { allow: true } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
routing: { allowFrom: [] },
|
||||||
|
} as Config;
|
||||||
|
|
||||||
|
function createCategoryGuildHandler() {
|
||||||
|
return createDiscordMessageHandler({
|
||||||
|
cfg: CATEGORY_GUILD_CFG,
|
||||||
|
discordConfig: CATEGORY_GUILD_CFG.channels.discord,
|
||||||
|
accountId: "default",
|
||||||
|
token: "token",
|
||||||
|
runtime: {
|
||||||
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
exit: (code: number): never => {
|
||||||
|
throw new Error(`exit ${code}`);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
botUserId: "bot-id",
|
||||||
|
guildHistories: new Map(),
|
||||||
|
historyLimit: 0,
|
||||||
|
mediaMaxBytes: 10_000,
|
||||||
|
textLimit: 2000,
|
||||||
|
replyToMode: "off",
|
||||||
|
dmEnabled: true,
|
||||||
|
groupDmEnabled: false,
|
||||||
|
guildEntries: {
|
||||||
|
"*": { requireMention: false, channels: { c1: { allow: true } } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function createCategoryGuildClient() {
|
||||||
|
return {
|
||||||
|
fetchChannel: vi.fn().mockResolvedValue({
|
||||||
|
type: ChannelType.GuildText,
|
||||||
|
name: "general",
|
||||||
|
parentId: "category-1",
|
||||||
|
}),
|
||||||
|
rest: { get: vi.fn() },
|
||||||
|
} as unknown as Client;
|
||||||
|
}
|
||||||
|
|
||||||
describe("discord tool result dispatch", () => {
|
describe("discord tool result dispatch", () => {
|
||||||
it("sends status replies with responsePrefix", async () => {
|
it("sends status replies with responsePrefix", async () => {
|
||||||
const cfg = {
|
const cfg = {
|
||||||
@@ -286,7 +348,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("uses channel id allowlists for non-thread channels with categories", async () => {
|
it("uses channel id allowlists for non-thread channels with categories", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
let capturedCtx: { SessionKey?: string } | undefined;
|
let capturedCtx: { SessionKey?: string } | undefined;
|
||||||
dispatchMock.mockImplementationOnce(async ({ ctx, dispatcher }) => {
|
dispatchMock.mockImplementationOnce(async ({ ctx, dispatcher }) => {
|
||||||
capturedCtx = ctx;
|
capturedCtx = ctx;
|
||||||
@@ -294,61 +355,8 @@ describe("discord tool result dispatch", () => {
|
|||||||
return { queuedFinal: true, counts: { final: 1 } };
|
return { queuedFinal: true, counts: { final: 1 } };
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg = {
|
const handler = createCategoryGuildHandler();
|
||||||
agents: {
|
const client = createCategoryGuildClient();
|
||||||
defaults: {
|
|
||||||
model: "anthropic/claude-opus-4-5",
|
|
||||||
workspace: "/tmp/openclaw",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
session: { store: "/tmp/openclaw-sessions.json" },
|
|
||||||
channels: {
|
|
||||||
discord: {
|
|
||||||
dm: { enabled: true, policy: "open" },
|
|
||||||
guilds: {
|
|
||||||
"*": {
|
|
||||||
requireMention: false,
|
|
||||||
channels: { c1: { allow: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
routing: { allowFrom: [] },
|
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
|
||||||
cfg,
|
|
||||||
discordConfig: cfg.channels.discord,
|
|
||||||
accountId: "default",
|
|
||||||
token: "token",
|
|
||||||
runtime: {
|
|
||||||
log: vi.fn(),
|
|
||||||
error: vi.fn(),
|
|
||||||
exit: (code: number): never => {
|
|
||||||
throw new Error(`exit ${code}`);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
botUserId: "bot-id",
|
|
||||||
guildHistories: new Map(),
|
|
||||||
historyLimit: 0,
|
|
||||||
mediaMaxBytes: 10_000,
|
|
||||||
textLimit: 2000,
|
|
||||||
replyToMode: "off",
|
|
||||||
dmEnabled: true,
|
|
||||||
groupDmEnabled: false,
|
|
||||||
guildEntries: {
|
|
||||||
"*": { requireMention: false, channels: { c1: { allow: true } } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const client = {
|
|
||||||
fetchChannel: vi.fn().mockResolvedValue({
|
|
||||||
type: ChannelType.GuildText,
|
|
||||||
name: "general",
|
|
||||||
parentId: "category-1",
|
|
||||||
}),
|
|
||||||
rest: { get: vi.fn() },
|
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
await handler(
|
await handler(
|
||||||
{
|
{
|
||||||
@@ -377,7 +385,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("prefixes group bodies with sender label", async () => {
|
it("prefixes group bodies with sender label", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
let capturedBody = "";
|
let capturedBody = "";
|
||||||
dispatchMock.mockImplementationOnce(async ({ ctx, dispatcher }) => {
|
dispatchMock.mockImplementationOnce(async ({ ctx, dispatcher }) => {
|
||||||
capturedBody = ctx.Body ?? "";
|
capturedBody = ctx.Body ?? "";
|
||||||
@@ -385,61 +392,8 @@ describe("discord tool result dispatch", () => {
|
|||||||
return { queuedFinal: true, counts: { final: 1 } };
|
return { queuedFinal: true, counts: { final: 1 } };
|
||||||
});
|
});
|
||||||
|
|
||||||
const cfg = {
|
const handler = createCategoryGuildHandler();
|
||||||
agents: {
|
const client = createCategoryGuildClient();
|
||||||
defaults: {
|
|
||||||
model: "anthropic/claude-opus-4-5",
|
|
||||||
workspace: "/tmp/openclaw",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
session: { store: "/tmp/openclaw-sessions.json" },
|
|
||||||
channels: {
|
|
||||||
discord: {
|
|
||||||
dm: { enabled: true, policy: "open" },
|
|
||||||
guilds: {
|
|
||||||
"*": {
|
|
||||||
requireMention: false,
|
|
||||||
channels: { c1: { allow: true } },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
routing: { allowFrom: [] },
|
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
|
||||||
cfg,
|
|
||||||
discordConfig: cfg.channels.discord,
|
|
||||||
accountId: "default",
|
|
||||||
token: "token",
|
|
||||||
runtime: {
|
|
||||||
log: vi.fn(),
|
|
||||||
error: vi.fn(),
|
|
||||||
exit: (code: number): never => {
|
|
||||||
throw new Error(`exit ${code}`);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
botUserId: "bot-id",
|
|
||||||
guildHistories: new Map(),
|
|
||||||
historyLimit: 0,
|
|
||||||
mediaMaxBytes: 10_000,
|
|
||||||
textLimit: 2000,
|
|
||||||
replyToMode: "off",
|
|
||||||
dmEnabled: true,
|
|
||||||
groupDmEnabled: false,
|
|
||||||
guildEntries: {
|
|
||||||
"*": { requireMention: false, channels: { c1: { allow: true } } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const client = {
|
|
||||||
fetchChannel: vi.fn().mockResolvedValue({
|
|
||||||
type: ChannelType.GuildText,
|
|
||||||
name: "general",
|
|
||||||
parentId: "category-1",
|
|
||||||
}),
|
|
||||||
rest: { get: vi.fn() },
|
|
||||||
} as unknown as Client;
|
|
||||||
|
|
||||||
await handler(
|
await handler(
|
||||||
{
|
{
|
||||||
@@ -468,7 +422,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("replies with pairing code and sender id when dmPolicy is pairing", async () => {
|
it("replies with pairing code and sender id when dmPolicy is pairing", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
const cfg = {
|
const cfg = {
|
||||||
agents: {
|
agents: {
|
||||||
defaults: {
|
defaults: {
|
||||||
@@ -480,7 +433,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
channels: {
|
channels: {
|
||||||
discord: { dm: { enabled: true, policy: "pairing", allowFrom: [] } },
|
discord: { dm: { enabled: true, policy: "pairing", allowFrom: [] } },
|
||||||
},
|
},
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as Config;
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = createDiscordMessageHandler({
|
||||||
cfg,
|
cfg,
|
||||||
|
|||||||
Reference in New Issue
Block a user