mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:38:27 +00:00
refactor(test): dedupe discord handler setup
This commit is contained in:
@@ -90,11 +90,42 @@ beforeEach(() => {
|
|||||||
|
|
||||||
const MENTION_PATTERNS_TEST_TIMEOUT_MS = process.platform === "win32" ? 90_000 : 60_000;
|
const MENTION_PATTERNS_TEST_TIMEOUT_MS = process.platform === "win32" ? 90_000 : 60_000;
|
||||||
|
|
||||||
|
type LoadedConfig = ReturnType<(typeof import("../config/config.js"))["loadConfig"]>;
|
||||||
|
|
||||||
|
function makeRuntime() {
|
||||||
|
return {
|
||||||
|
log: vi.fn(),
|
||||||
|
error: vi.fn(),
|
||||||
|
exit: (code: number): never => {
|
||||||
|
throw new Error(`exit ${code}`);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
async function createHandler(cfg: LoadedConfig) {
|
||||||
|
const { createDiscordMessageHandler } = await import("./monitor.js");
|
||||||
|
return createDiscordMessageHandler({
|
||||||
|
cfg,
|
||||||
|
discordConfig: cfg.channels.discord,
|
||||||
|
accountId: "default",
|
||||||
|
token: "token",
|
||||||
|
runtime: makeRuntime(),
|
||||||
|
botUserId: "bot-id",
|
||||||
|
guildHistories: new Map(),
|
||||||
|
historyLimit: 0,
|
||||||
|
mediaMaxBytes: 10_000,
|
||||||
|
textLimit: 2000,
|
||||||
|
replyToMode: "off",
|
||||||
|
dmEnabled: true,
|
||||||
|
groupDmEnabled: false,
|
||||||
|
guildEntries: cfg.channels.discord.guilds,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe("discord tool result dispatch", () => {
|
describe("discord tool result dispatch", () => {
|
||||||
it(
|
it(
|
||||||
"accepts guild messages when mentionPatterns match",
|
"accepts guild messages when mentionPatterns match",
|
||||||
async () => {
|
async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
const cfg = {
|
const cfg = {
|
||||||
agents: {
|
agents: {
|
||||||
defaults: {
|
defaults: {
|
||||||
@@ -116,28 +147,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
},
|
},
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = await createHandler(cfg);
|
||||||
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: true } },
|
|
||||||
});
|
|
||||||
|
|
||||||
const client = {
|
const client = {
|
||||||
fetchChannel: vi.fn().mockResolvedValue({
|
fetchChannel: vi.fn().mockResolvedValue({
|
||||||
@@ -227,7 +237,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
);
|
);
|
||||||
|
|
||||||
it("accepts guild reply-to-bot messages as implicit mentions", async () => {
|
it("accepts guild reply-to-bot messages as implicit mentions", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
const cfg = {
|
const cfg = {
|
||||||
agents: {
|
agents: {
|
||||||
defaults: {
|
defaults: {
|
||||||
@@ -245,28 +254,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
},
|
},
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = await createHandler(cfg);
|
||||||
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: true } },
|
|
||||||
});
|
|
||||||
|
|
||||||
const client = {
|
const client = {
|
||||||
fetchChannel: vi.fn().mockResolvedValue({
|
fetchChannel: vi.fn().mockResolvedValue({
|
||||||
@@ -327,7 +315,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("forks thread sessions and injects starter context", async () => {
|
it("forks thread sessions and injects starter context", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
let capturedCtx:
|
let capturedCtx:
|
||||||
| {
|
| {
|
||||||
SessionKey?: string;
|
SessionKey?: string;
|
||||||
@@ -360,28 +347,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
},
|
},
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = await createHandler(cfg);
|
||||||
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 } },
|
|
||||||
});
|
|
||||||
|
|
||||||
const threadChannel = {
|
const threadChannel = {
|
||||||
type: ChannelType.GuildText,
|
type: ChannelType.GuildText,
|
||||||
@@ -441,7 +407,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("skips thread starter context when disabled", async () => {
|
it("skips thread starter context when disabled", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
let capturedCtx:
|
let capturedCtx:
|
||||||
| {
|
| {
|
||||||
ThreadStarterBody?: string;
|
ThreadStarterBody?: string;
|
||||||
@@ -477,28 +442,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
},
|
},
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = await createHandler(cfg);
|
||||||
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: cfg.channels.discord.guilds,
|
|
||||||
});
|
|
||||||
|
|
||||||
const threadChannel = {
|
const threadChannel = {
|
||||||
type: ChannelType.GuildText,
|
type: ChannelType.GuildText,
|
||||||
@@ -550,7 +494,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("treats forum threads as distinct sessions without channel payloads", async () => {
|
it("treats forum threads as distinct sessions without channel payloads", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
let capturedCtx:
|
let capturedCtx:
|
||||||
| {
|
| {
|
||||||
SessionKey?: string;
|
SessionKey?: string;
|
||||||
@@ -578,28 +521,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
routing: { allowFrom: [] },
|
routing: { allowFrom: [] },
|
||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = await createHandler(cfg);
|
||||||
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 } },
|
|
||||||
});
|
|
||||||
|
|
||||||
const fetchChannel = vi
|
const fetchChannel = vi
|
||||||
.fn()
|
.fn()
|
||||||
@@ -655,8 +577,6 @@ describe("discord tool result dispatch", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("scopes thread sessions to the routed agent", async () => {
|
it("scopes thread sessions to the routed agent", async () => {
|
||||||
const { createDiscordMessageHandler } = await import("./monitor.js");
|
|
||||||
|
|
||||||
let capturedCtx:
|
let capturedCtx:
|
||||||
| {
|
| {
|
||||||
SessionKey?: string;
|
SessionKey?: string;
|
||||||
@@ -689,28 +609,7 @@ describe("discord tool result dispatch", () => {
|
|||||||
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
} as ReturnType<typeof import("../config/config.js").loadConfig>;
|
||||||
loadConfigMock.mockReturnValue(cfg);
|
loadConfigMock.mockReturnValue(cfg);
|
||||||
|
|
||||||
const handler = createDiscordMessageHandler({
|
const handler = await createHandler(cfg);
|
||||||
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 } },
|
|
||||||
});
|
|
||||||
|
|
||||||
const threadChannel = {
|
const threadChannel = {
|
||||||
type: ChannelType.GuildText,
|
type: ChannelType.GuildText,
|
||||||
|
|||||||
Reference in New Issue
Block a user