|
| 1 | +/** |
| 2 | + * Tests that preflightDiscordMessage passively populates the directory cache |
| 3 | + * from the message author and mentionedUsers, enabling outbound |
| 4 | + * rewriteDiscordKnownMentions() to resolve @name → <@ID> for bot→bot @mention |
| 5 | + * chains without prompt-level ID injection. |
| 6 | + */ |
| 7 | +import { beforeEach, describe, expect, it } from "vitest"; |
| 8 | +import { |
| 9 | + __resetDiscordDirectoryCacheForTest, |
| 10 | + rememberDiscordDirectoryUser, |
| 11 | + resolveDiscordDirectoryUserId, |
| 12 | +} from "../directory-cache.js"; |
| 13 | +import { rewriteDiscordKnownMentions } from "../mentions.js"; |
| 14 | +import { preflightDiscordMessage } from "./message-handler.preflight.js"; |
| 15 | +import { |
| 16 | + createDiscordMessage, |
| 17 | + createDiscordPreflightArgs, |
| 18 | + createGuildEvent, |
| 19 | + createGuildTextClient, |
| 20 | + DEFAULT_PREFLIGHT_CFG, |
| 21 | +} from "./message-handler.preflight.test-helpers.js"; |
| 22 | + |
| 23 | +const GUILD_ID = "guild-1"; |
| 24 | +const CHANNEL_ID = "channel-1"; |
| 25 | +const BOT_USER_ID = "bot-self"; |
| 26 | + |
| 27 | +function makeAllowedGuildEntries() { |
| 28 | + return { |
| 29 | + [GUILD_ID]: { |
| 30 | + id: GUILD_ID, |
| 31 | + channels: { |
| 32 | + [CHANNEL_ID]: { allow: true, enabled: true, requireMention: false }, |
| 33 | + }, |
| 34 | + }, |
| 35 | + }; |
| 36 | +} |
| 37 | + |
| 38 | +function makeBaseParams(author: { |
| 39 | + id: string; |
| 40 | + bot: boolean; |
| 41 | + username: string; |
| 42 | + globalName?: string; |
| 43 | +}) { |
| 44 | + const carbonAuthor = author as unknown as import("@buape/carbon").Message["author"]; |
| 45 | + const message = createDiscordMessage({ |
| 46 | + id: "m-1", |
| 47 | + channelId: CHANNEL_ID, |
| 48 | + content: "hello", |
| 49 | + author, |
| 50 | + }); |
| 51 | + return createDiscordPreflightArgs({ |
| 52 | + cfg: DEFAULT_PREFLIGHT_CFG, |
| 53 | + discordConfig: { allowBots: true } as NonNullable< |
| 54 | + import("openclaw/plugin-sdk/config-runtime").OpenClawConfig["channels"] |
| 55 | + >["discord"], |
| 56 | + data: createGuildEvent({ |
| 57 | + channelId: CHANNEL_ID, |
| 58 | + guildId: GUILD_ID, |
| 59 | + author: carbonAuthor, |
| 60 | + message, |
| 61 | + }), |
| 62 | + client: createGuildTextClient(CHANNEL_ID), |
| 63 | + botUserId: BOT_USER_ID, |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +describe("preflightDiscordMessage: directory cache population", () => { |
| 68 | + beforeEach(() => { |
| 69 | + __resetDiscordDirectoryCacheForTest(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("caches the message author username so outbound rewrite resolves @name → <@ID>", async () => { |
| 73 | + const author = { id: "1488478869458780280", bot: true, username: "架构师" }; |
| 74 | + const params = makeBaseParams(author); |
| 75 | + |
| 76 | + await preflightDiscordMessage({ ...params, guildEntries: makeAllowedGuildEntries() }); |
| 77 | + |
| 78 | + const resolved = resolveDiscordDirectoryUserId({ |
| 79 | + accountId: "default", |
| 80 | + handle: "架构师", |
| 81 | + }); |
| 82 | + expect(resolved).toBe("1488478869458780280"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("rewrites @name in outbound text after author passes through preflight", async () => { |
| 86 | + // Simulate bot A's message arriving — bot A is the architect |
| 87 | + const author = { id: "1488478869458780280", bot: true, username: "架构师" }; |
| 88 | + const params = makeBaseParams(author); |
| 89 | + |
| 90 | + await preflightDiscordMessage({ ...params, guildEntries: makeAllowedGuildEntries() }); |
| 91 | + |
| 92 | + // Now another bot tries to mention the architect by display name |
| 93 | + const rewritten = rewriteDiscordKnownMentions("@架构师 请复核确认", { accountId: "default" }); |
| 94 | + expect(rewritten).toBe("<@1488478869458780280> 请复核确认"); |
| 95 | + }); |
| 96 | + |
| 97 | + it("caches globalName as an additional resolvable handle", async () => { |
| 98 | + const author = { |
| 99 | + id: "1488480784422535208", |
| 100 | + bot: true, |
| 101 | + username: "backend_eng", |
| 102 | + globalName: "后端工程师", |
| 103 | + }; |
| 104 | + const message = createDiscordMessage({ |
| 105 | + id: "m-2", |
| 106 | + channelId: CHANNEL_ID, |
| 107 | + content: "api done", |
| 108 | + author, |
| 109 | + }); |
| 110 | + const params = createDiscordPreflightArgs({ |
| 111 | + cfg: DEFAULT_PREFLIGHT_CFG, |
| 112 | + discordConfig: { allowBots: true } as NonNullable< |
| 113 | + import("openclaw/plugin-sdk/config-runtime").OpenClawConfig["channels"] |
| 114 | + >["discord"], |
| 115 | + data: { |
| 116 | + ...createGuildEvent({ |
| 117 | + channelId: CHANNEL_ID, |
| 118 | + guildId: GUILD_ID, |
| 119 | + author: author as unknown as import("@buape/carbon").Message["author"], |
| 120 | + message, |
| 121 | + }), |
| 122 | + // Inject globalName via the author object (as Carbon exposes it) |
| 123 | + author: { |
| 124 | + ...author, |
| 125 | + globalName: "后端工程师", |
| 126 | + } as unknown as import("@buape/carbon").Message["author"], |
| 127 | + } as import("./listeners.js").DiscordMessageEvent, |
| 128 | + client: createGuildTextClient(CHANNEL_ID), |
| 129 | + botUserId: BOT_USER_ID, |
| 130 | + }); |
| 131 | + |
| 132 | + await preflightDiscordMessage({ ...params, guildEntries: makeAllowedGuildEntries() }); |
| 133 | + |
| 134 | + // Both the username and globalName should resolve |
| 135 | + expect(resolveDiscordDirectoryUserId({ accountId: "default", handle: "backend_eng" })).toBe( |
| 136 | + "1488480784422535208", |
| 137 | + ); |
| 138 | + expect(resolveDiscordDirectoryUserId({ accountId: "default", handle: "后端工程师" })).toBe( |
| 139 | + "1488480784422535208", |
| 140 | + ); |
| 141 | + }); |
| 142 | + |
| 143 | + it("caches users listed in mentionedUsers so the receiver learns their IDs", async () => { |
| 144 | + const botSelf = { id: BOT_USER_ID, bot: true, username: "director_bot" }; |
| 145 | + const mentionedBot = { id: "1488479656549158922", username: "前端工程师" }; |
| 146 | + const message = { |
| 147 | + id: "m-3", |
| 148 | + content: "<@" + BOT_USER_ID + "> check this", |
| 149 | + channelId: CHANNEL_ID, |
| 150 | + timestamp: new Date().toISOString(), |
| 151 | + attachments: [], |
| 152 | + mentionedUsers: [{ ...mentionedBot }], |
| 153 | + mentionedRoles: [], |
| 154 | + mentionedEveryone: false, |
| 155 | + author: botSelf, |
| 156 | + } as unknown as import("@buape/carbon").Message; |
| 157 | + |
| 158 | + const params = createDiscordPreflightArgs({ |
| 159 | + cfg: DEFAULT_PREFLIGHT_CFG, |
| 160 | + discordConfig: { allowBots: true } as NonNullable< |
| 161 | + import("openclaw/plugin-sdk/config-runtime").OpenClawConfig["channels"] |
| 162 | + >["discord"], |
| 163 | + data: createGuildEvent({ |
| 164 | + channelId: CHANNEL_ID, |
| 165 | + guildId: GUILD_ID, |
| 166 | + author: botSelf as unknown as import("@buape/carbon").Message["author"], |
| 167 | + message, |
| 168 | + }), |
| 169 | + client: createGuildTextClient(CHANNEL_ID), |
| 170 | + botUserId: BOT_USER_ID, |
| 171 | + }); |
| 172 | + |
| 173 | + await preflightDiscordMessage({ ...params, guildEntries: makeAllowedGuildEntries() }); |
| 174 | + |
| 175 | + // The mentioned bot's username should now be resolvable |
| 176 | + expect(resolveDiscordDirectoryUserId({ accountId: "default", handle: "前端工程师" })).toBe( |
| 177 | + "1488479656549158922", |
| 178 | + ); |
| 179 | + }); |
| 180 | + |
| 181 | + it("cross-account fallback resolves handles cached by another account", () => { |
| 182 | + // Simulate bot A (accountId="architect") caching a user during preflight |
| 183 | + rememberDiscordDirectoryUser({ |
| 184 | + accountId: "architect", |
| 185 | + userId: "1488480784422535208", |
| 186 | + handles: ["后端工程师"], |
| 187 | + }); |
| 188 | + |
| 189 | + // Bot B (accountId="qa") should also be able to resolve the handle |
| 190 | + // via the cross-account fallback |
| 191 | + expect(resolveDiscordDirectoryUserId({ accountId: "qa", handle: "后端工程师" })).toBe( |
| 192 | + "1488480784422535208", |
| 193 | + ); |
| 194 | + |
| 195 | + // Rewrite should also work from a different account |
| 196 | + const rewritten = rewriteDiscordKnownMentions("@后端工程师 请确认", { accountId: "qa" }); |
| 197 | + expect(rewritten).toBe("<@1488480784422535208> 请确认"); |
| 198 | + }); |
| 199 | +}); |
0 commit comments