mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 11:37:38 +00:00
refactor(discord): dedupe message preflight test runners
This commit is contained in:
@@ -168,6 +168,71 @@ function createMessage(params: {
|
|||||||
} as unknown as import("@buape/carbon").Message;
|
} as unknown as import("@buape/carbon").Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function runThreadBoundPreflight(params: {
|
||||||
|
threadId: string;
|
||||||
|
parentId: string;
|
||||||
|
message: import("@buape/carbon").Message;
|
||||||
|
threadBinding: import("../../infra/outbound/session-binding-service.js").SessionBindingRecord;
|
||||||
|
discordConfig: DiscordConfig;
|
||||||
|
registerBindingAdapter?: boolean;
|
||||||
|
}) {
|
||||||
|
if (params.registerBindingAdapter) {
|
||||||
|
registerSessionBindingAdapter({
|
||||||
|
channel: "discord",
|
||||||
|
accountId: "default",
|
||||||
|
listBySession: () => [],
|
||||||
|
resolveByConversation: (ref) =>
|
||||||
|
ref.conversationId === params.threadId ? params.threadBinding : null,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = createThreadClient({
|
||||||
|
threadId: params.threadId,
|
||||||
|
parentId: params.parentId,
|
||||||
|
});
|
||||||
|
|
||||||
|
return preflightDiscordMessage({
|
||||||
|
...createPreflightArgs({
|
||||||
|
cfg: DEFAULT_CFG,
|
||||||
|
discordConfig: params.discordConfig,
|
||||||
|
data: createGuildEvent({
|
||||||
|
channelId: params.threadId,
|
||||||
|
guildId: "guild-1",
|
||||||
|
author: params.message.author,
|
||||||
|
message: params.message,
|
||||||
|
}),
|
||||||
|
client,
|
||||||
|
}),
|
||||||
|
threadBindings: {
|
||||||
|
getByThreadId: (id: string) => (id === params.threadId ? params.threadBinding : undefined),
|
||||||
|
} as import("./thread-bindings.js").ThreadBindingManager,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function runGuildPreflight(params: {
|
||||||
|
channelId: string;
|
||||||
|
guildId: string;
|
||||||
|
message: import("@buape/carbon").Message;
|
||||||
|
discordConfig: DiscordConfig;
|
||||||
|
cfg?: import("../../config/config.js").OpenClawConfig;
|
||||||
|
guildEntries?: Parameters<typeof preflightDiscordMessage>[0]["guildEntries"];
|
||||||
|
}) {
|
||||||
|
return preflightDiscordMessage({
|
||||||
|
...createPreflightArgs({
|
||||||
|
cfg: params.cfg ?? DEFAULT_CFG,
|
||||||
|
discordConfig: params.discordConfig,
|
||||||
|
data: createGuildEvent({
|
||||||
|
channelId: params.channelId,
|
||||||
|
guildId: params.guildId,
|
||||||
|
author: params.message.author,
|
||||||
|
message: params.message,
|
||||||
|
}),
|
||||||
|
client: createGuildTextClient(params.channelId),
|
||||||
|
}),
|
||||||
|
guildEntries: params.guildEntries,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
describe("resolvePreflightMentionRequirement", () => {
|
describe("resolvePreflightMentionRequirement", () => {
|
||||||
it("requires mention when config requires mention and thread is not bound", () => {
|
it("requires mention when config requires mention and thread is not bound", () => {
|
||||||
expect(
|
expect(
|
||||||
@@ -210,7 +275,6 @@ describe("preflightDiscordMessage", () => {
|
|||||||
});
|
});
|
||||||
const threadId = "thread-system-1";
|
const threadId = "thread-system-1";
|
||||||
const parentId = "channel-parent-1";
|
const parentId = "channel-parent-1";
|
||||||
const client = createThreadClient({ threadId, parentId });
|
|
||||||
const message = createMessage({
|
const message = createMessage({
|
||||||
id: "m-system-1",
|
id: "m-system-1",
|
||||||
channelId: threadId,
|
channelId: threadId,
|
||||||
@@ -223,23 +287,14 @@ describe("preflightDiscordMessage", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await preflightDiscordMessage({
|
const result = await runThreadBoundPreflight({
|
||||||
...createPreflightArgs({
|
threadId,
|
||||||
cfg: DEFAULT_CFG,
|
parentId,
|
||||||
discordConfig: {
|
message,
|
||||||
allowBots: true,
|
threadBinding,
|
||||||
} as DiscordConfig,
|
discordConfig: {
|
||||||
data: createGuildEvent({
|
allowBots: true,
|
||||||
channelId: threadId,
|
} as DiscordConfig,
|
||||||
guildId: "guild-1",
|
|
||||||
author: message.author,
|
|
||||||
message,
|
|
||||||
}),
|
|
||||||
client,
|
|
||||||
}),
|
|
||||||
threadBindings: {
|
|
||||||
getByThreadId: (id: string) => (id === threadId ? threadBinding : undefined),
|
|
||||||
} as import("./thread-bindings.js").ThreadBindingManager,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
@@ -252,7 +307,6 @@ describe("preflightDiscordMessage", () => {
|
|||||||
});
|
});
|
||||||
const threadId = "thread-bot-regular-1";
|
const threadId = "thread-bot-regular-1";
|
||||||
const parentId = "channel-parent-regular-1";
|
const parentId = "channel-parent-regular-1";
|
||||||
const client = createThreadClient({ threadId, parentId });
|
|
||||||
const message = createMessage({
|
const message = createMessage({
|
||||||
id: "m-bot-regular-1",
|
id: "m-bot-regular-1",
|
||||||
channelId: threadId,
|
channelId: threadId,
|
||||||
@@ -264,30 +318,15 @@ describe("preflightDiscordMessage", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
registerSessionBindingAdapter({
|
const result = await runThreadBoundPreflight({
|
||||||
channel: "discord",
|
threadId,
|
||||||
accountId: "default",
|
parentId,
|
||||||
listBySession: () => [],
|
message,
|
||||||
resolveByConversation: (ref) => (ref.conversationId === threadId ? threadBinding : null),
|
threadBinding,
|
||||||
});
|
discordConfig: {
|
||||||
|
allowBots: true,
|
||||||
const result = await preflightDiscordMessage({
|
} as DiscordConfig,
|
||||||
...createPreflightArgs({
|
registerBindingAdapter: true,
|
||||||
cfg: DEFAULT_CFG,
|
|
||||||
discordConfig: {
|
|
||||||
allowBots: true,
|
|
||||||
} as DiscordConfig,
|
|
||||||
data: createGuildEvent({
|
|
||||||
channelId: threadId,
|
|
||||||
guildId: "guild-1",
|
|
||||||
author: message.author,
|
|
||||||
message,
|
|
||||||
}),
|
|
||||||
client,
|
|
||||||
}),
|
|
||||||
threadBindings: {
|
|
||||||
getByThreadId: (id: string) => (id === threadId ? threadBinding : undefined),
|
|
||||||
} as import("./thread-bindings.js").ThreadBindingManager,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
@@ -343,7 +382,6 @@ describe("preflightDiscordMessage", () => {
|
|||||||
it("drops bot messages without mention when allowBots=mentions", async () => {
|
it("drops bot messages without mention when allowBots=mentions", async () => {
|
||||||
const channelId = "channel-bot-mentions-off";
|
const channelId = "channel-bot-mentions-off";
|
||||||
const guildId = "guild-bot-mentions-off";
|
const guildId = "guild-bot-mentions-off";
|
||||||
const client = createGuildTextClient(channelId);
|
|
||||||
const message = createMessage({
|
const message = createMessage({
|
||||||
id: "m-bot-mentions-off",
|
id: "m-bot-mentions-off",
|
||||||
channelId,
|
channelId,
|
||||||
@@ -355,20 +393,13 @@ describe("preflightDiscordMessage", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await preflightDiscordMessage({
|
const result = await runGuildPreflight({
|
||||||
...createPreflightArgs({
|
channelId,
|
||||||
cfg: DEFAULT_CFG,
|
guildId,
|
||||||
discordConfig: {
|
message,
|
||||||
allowBots: "mentions",
|
discordConfig: {
|
||||||
} as DiscordConfig,
|
allowBots: "mentions",
|
||||||
data: createGuildEvent({
|
} as DiscordConfig,
|
||||||
channelId,
|
|
||||||
guildId,
|
|
||||||
author: message.author,
|
|
||||||
message,
|
|
||||||
}),
|
|
||||||
client,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).toBeNull();
|
expect(result).toBeNull();
|
||||||
@@ -377,7 +408,6 @@ describe("preflightDiscordMessage", () => {
|
|||||||
it("allows bot messages with explicit mention when allowBots=mentions", async () => {
|
it("allows bot messages with explicit mention when allowBots=mentions", async () => {
|
||||||
const channelId = "channel-bot-mentions-on";
|
const channelId = "channel-bot-mentions-on";
|
||||||
const guildId = "guild-bot-mentions-on";
|
const guildId = "guild-bot-mentions-on";
|
||||||
const client = createGuildTextClient(channelId);
|
|
||||||
const message = createMessage({
|
const message = createMessage({
|
||||||
id: "m-bot-mentions-on",
|
id: "m-bot-mentions-on",
|
||||||
channelId,
|
channelId,
|
||||||
@@ -390,20 +420,13 @@ describe("preflightDiscordMessage", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await preflightDiscordMessage({
|
const result = await runGuildPreflight({
|
||||||
...createPreflightArgs({
|
channelId,
|
||||||
cfg: DEFAULT_CFG,
|
guildId,
|
||||||
discordConfig: {
|
message,
|
||||||
allowBots: "mentions",
|
discordConfig: {
|
||||||
} as DiscordConfig,
|
allowBots: "mentions",
|
||||||
data: createGuildEvent({
|
} as DiscordConfig,
|
||||||
channelId,
|
|
||||||
guildId,
|
|
||||||
author: message.author,
|
|
||||||
message,
|
|
||||||
}),
|
|
||||||
client,
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(result).not.toBeNull();
|
expect(result).not.toBeNull();
|
||||||
@@ -412,7 +435,6 @@ describe("preflightDiscordMessage", () => {
|
|||||||
it("drops guild messages that mention another user when ignoreOtherMentions=true", async () => {
|
it("drops guild messages that mention another user when ignoreOtherMentions=true", async () => {
|
||||||
const channelId = "channel-other-mention-1";
|
const channelId = "channel-other-mention-1";
|
||||||
const guildId = "guild-other-mention-1";
|
const guildId = "guild-other-mention-1";
|
||||||
const client = createGuildTextClient(channelId);
|
|
||||||
const message = createMessage({
|
const message = createMessage({
|
||||||
id: "m-other-mention-1",
|
id: "m-other-mention-1",
|
||||||
channelId,
|
channelId,
|
||||||
@@ -425,18 +447,11 @@ describe("preflightDiscordMessage", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await preflightDiscordMessage({
|
const result = await runGuildPreflight({
|
||||||
...createPreflightArgs({
|
channelId,
|
||||||
cfg: DEFAULT_CFG,
|
guildId,
|
||||||
discordConfig: {} as DiscordConfig,
|
message,
|
||||||
data: createGuildEvent({
|
discordConfig: {} as DiscordConfig,
|
||||||
channelId,
|
|
||||||
guildId,
|
|
||||||
author: message.author,
|
|
||||||
message,
|
|
||||||
}),
|
|
||||||
client,
|
|
||||||
}),
|
|
||||||
guildEntries: {
|
guildEntries: {
|
||||||
[guildId]: {
|
[guildId]: {
|
||||||
requireMention: false,
|
requireMention: false,
|
||||||
@@ -451,7 +466,6 @@ describe("preflightDiscordMessage", () => {
|
|||||||
it("does not drop @everyone messages when ignoreOtherMentions=true", async () => {
|
it("does not drop @everyone messages when ignoreOtherMentions=true", async () => {
|
||||||
const channelId = "channel-other-mention-everyone";
|
const channelId = "channel-other-mention-everyone";
|
||||||
const guildId = "guild-other-mention-everyone";
|
const guildId = "guild-other-mention-everyone";
|
||||||
const client = createGuildTextClient(channelId);
|
|
||||||
const message = createMessage({
|
const message = createMessage({
|
||||||
id: "m-other-mention-everyone",
|
id: "m-other-mention-everyone",
|
||||||
channelId,
|
channelId,
|
||||||
@@ -464,18 +478,11 @@ describe("preflightDiscordMessage", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await preflightDiscordMessage({
|
const result = await runGuildPreflight({
|
||||||
...createPreflightArgs({
|
channelId,
|
||||||
cfg: DEFAULT_CFG,
|
guildId,
|
||||||
discordConfig: {} as DiscordConfig,
|
message,
|
||||||
data: createGuildEvent({
|
discordConfig: {} as DiscordConfig,
|
||||||
channelId,
|
|
||||||
guildId,
|
|
||||||
author: message.author,
|
|
||||||
message,
|
|
||||||
}),
|
|
||||||
client,
|
|
||||||
}),
|
|
||||||
guildEntries: {
|
guildEntries: {
|
||||||
[guildId]: {
|
[guildId]: {
|
||||||
requireMention: false,
|
requireMention: false,
|
||||||
|
|||||||
Reference in New Issue
Block a user