|
| 1 | +/** |
| 2 | + * Agent tool calls pass the operation only in context and supply arguments under the tool's param |
| 3 | + * names, so the Discord mapping must run there; canvas "No change" flags must stay unsent. |
| 4 | + */ |
| 5 | +import { describe, expect, it } from 'vitest' |
| 6 | +import { DiscordV2Block } from '@/blocks/blocks/discord' |
| 7 | +import { discordAddReactionTool } from '@/tools/discord/add_reaction' |
| 8 | +import { discordArchiveThreadTool } from '@/tools/discord/archive_thread' |
| 9 | +import { discordBanMemberTool } from '@/tools/discord/ban_member' |
| 10 | +import { discordBulkDeleteMessagesTool } from '@/tools/discord/bulk_delete_messages' |
| 11 | +import { discordCreateChannelTool } from '@/tools/discord/create_channel' |
| 12 | +import { discordGetMessagesTool } from '@/tools/discord/get_messages' |
| 13 | +import { discordSendMessageTool } from '@/tools/discord/send_message' |
| 14 | +import { discordUpdateMemberTool } from '@/tools/discord/update_member' |
| 15 | +import { discordUpdateRoleTool } from '@/tools/discord/update_role' |
| 16 | +import type { ToolConfig } from '@/tools/types' |
| 17 | + |
| 18 | +function sentParams( |
| 19 | + inputs: Record<string, unknown>, |
| 20 | + context?: { operation: string } |
| 21 | +): Record<string, unknown> { |
| 22 | + const mapper = DiscordV2Block.tools.config?.params |
| 23 | + if (!mapper) throw new Error('Discord block has no params mapper') |
| 24 | + return { ...inputs, ...mapper(inputs, context) } |
| 25 | +} |
| 26 | + |
| 27 | +function requestUrl(tool: ToolConfig, params: Record<string, unknown>): string { |
| 28 | + const url = tool.request.url |
| 29 | + return typeof url === 'function' ? url(params) : url |
| 30 | +} |
| 31 | + |
| 32 | +function requestBody(tool: ToolConfig, params: Record<string, unknown>): unknown { |
| 33 | + const build = tool.request.body |
| 34 | + if (typeof build !== 'function') throw new Error(`${tool.id} has no request body`) |
| 35 | + return build(params) |
| 36 | +} |
| 37 | + |
| 38 | +describe('Discord agent tool calls', () => { |
| 39 | + it.each([ |
| 40 | + ['a comma list from the tool row', '111, 222'], |
| 41 | + ['an array from the model', ['111', '222']], |
| 42 | + ])('bulk deletes the message IDs given as %s', (_label, messageIds) => { |
| 43 | + const params = sentParams( |
| 44 | + { botToken: 'bot', serverId: '1', channelId: '9', messageIds }, |
| 45 | + { operation: 'discord_bulk_delete_messages' } |
| 46 | + ) |
| 47 | + expect(requestBody(discordBulkDeleteMessagesTool, params)).toEqual({ |
| 48 | + messages: ['111', '222'], |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it.each([ |
| 53 | + [true, true], |
| 54 | + ['false', false], |
| 55 | + ])('sends archived %j to Discord as the boolean %j', (archived, expected) => { |
| 56 | + const params = sentParams( |
| 57 | + { botToken: 'bot', serverId: '1', threadId: '7', archived }, |
| 58 | + { operation: 'discord_archive_thread' } |
| 59 | + ) |
| 60 | + expect(requestBody(discordArchiveThreadTool, params)).toEqual({ archived: expected }) |
| 61 | + }) |
| 62 | + |
| 63 | + it('runs a channel operation without a server ID, but not a guild operation', () => { |
| 64 | + const params = sentParams( |
| 65 | + { botToken: 'bot', channelId: '9', messageId: '8', emoji: '👍' }, |
| 66 | + { operation: 'discord_add_reaction' } |
| 67 | + ) |
| 68 | + expect(requestUrl(discordAddReactionTool, params)).toBe( |
| 69 | + 'https://discord.com/api/v10/channels/9/messages/8/reactions/%F0%9F%91%8D/@me' |
| 70 | + ) |
| 71 | + expect(() => |
| 72 | + sentParams({ botToken: 'bot', userId: '5' }, { operation: 'discord_get_member' }) |
| 73 | + ).toThrow('Server ID is required') |
| 74 | + }) |
| 75 | + |
| 76 | + it("pages messages from the model's cursor, never from a stale pinned-messages timestamp", () => { |
| 77 | + const agent = sentParams( |
| 78 | + { botToken: 'bot', serverId: '1', channelId: '9', before: '123' }, |
| 79 | + { operation: 'discord_get_messages' } |
| 80 | + ) |
| 81 | + expect(requestUrl(discordGetMessagesTool, agent)).toBe( |
| 82 | + 'https://discord.com/api/v10/channels/9/messages?limit=10&before=123' |
| 83 | + ) |
| 84 | + |
| 85 | + const canvas = sentParams({ |
| 86 | + operation: 'discord_get_messages', |
| 87 | + botToken: 'bot', |
| 88 | + serverId: '1', |
| 89 | + channelId: '9', |
| 90 | + before: '2026-01-01T00:00:00Z', |
| 91 | + }) |
| 92 | + expect(requestUrl(discordGetMessagesTool, canvas)).toBe( |
| 93 | + 'https://discord.com/api/v10/channels/9/messages?limit=10' |
| 94 | + ) |
| 95 | + }) |
| 96 | + |
| 97 | + it("creates the model's channel type over the tool row's seeded text-channel option", () => { |
| 98 | + const agent = sentParams( |
| 99 | + { botToken: 'bot', serverId: '1', name: 'vc', channelType: '0', type: 2 }, |
| 100 | + { operation: 'discord_create_channel' } |
| 101 | + ) |
| 102 | + expect(requestBody(discordCreateChannelTool, agent)).toEqual({ name: 'vc', type: 2 }) |
| 103 | + |
| 104 | + const canvas = sentParams({ |
| 105 | + operation: 'discord_create_channel', |
| 106 | + botToken: 'bot', |
| 107 | + serverId: '1', |
| 108 | + name: 'vc', |
| 109 | + channelType: '2', |
| 110 | + }) |
| 111 | + expect(requestBody(discordCreateChannelTool, canvas)).toEqual({ name: 'vc', type: 2 }) |
| 112 | + }) |
| 113 | + |
| 114 | + it("bans with the model's message purge window over the tool row's seeded zero days", () => { |
| 115 | + const agent = sentParams( |
| 116 | + { |
| 117 | + botToken: 'bot', |
| 118 | + serverId: '1', |
| 119 | + userId: '5', |
| 120 | + deleteMessageDays: '0', |
| 121 | + deleteMessageSeconds: 604800, |
| 122 | + }, |
| 123 | + { operation: 'discord_ban_member' } |
| 124 | + ) |
| 125 | + expect(requestBody(discordBanMemberTool, agent)).toEqual({ delete_message_seconds: 604800 }) |
| 126 | + |
| 127 | + const canvas = sentParams({ |
| 128 | + operation: 'discord_ban_member', |
| 129 | + botToken: 'bot', |
| 130 | + serverId: '1', |
| 131 | + userId: '5', |
| 132 | + deleteMessageDays: '7', |
| 133 | + }) |
| 134 | + expect(requestBody(discordBanMemberTool, canvas)).toEqual({ delete_message_seconds: 604800 }) |
| 135 | + }) |
| 136 | +}) |
| 137 | + |
| 138 | +describe('Discord canvas "No change" flags', () => { |
| 139 | + it('renames a member without touching their voice mute or deafen state', () => { |
| 140 | + const params = sentParams({ |
| 141 | + operation: 'discord_update_member', |
| 142 | + botToken: 'bot', |
| 143 | + serverId: '1', |
| 144 | + userId: '5', |
| 145 | + nick: 'Ada', |
| 146 | + mute: '', |
| 147 | + deaf: '', |
| 148 | + }) |
| 149 | + expect(requestBody(discordUpdateMemberTool, params)).toEqual({ nick: 'Ada' }) |
| 150 | + }) |
| 151 | + |
| 152 | + it('renames a role without changing whether it is hoisted or mentionable', () => { |
| 153 | + const params = sentParams({ |
| 154 | + operation: 'discord_update_role', |
| 155 | + botToken: 'bot', |
| 156 | + serverId: '1', |
| 157 | + roleId: '3', |
| 158 | + name: 'Mods', |
| 159 | + hoist: '', |
| 160 | + mentionable: '', |
| 161 | + }) |
| 162 | + expect(requestBody(discordUpdateRoleTool, params)).toEqual({ name: 'Mods' }) |
| 163 | + }) |
| 164 | +}) |
| 165 | + |
| 166 | +describe('Discord request paths', () => { |
| 167 | + it('rejects an ID that would steer the bot request to another endpoint', () => { |
| 168 | + expect(() => |
| 169 | + requestUrl(discordAddReactionTool, { |
| 170 | + botToken: 'bot', |
| 171 | + channelId: '9/../../guilds/1', |
| 172 | + messageId: '8', |
| 173 | + emoji: 'x', |
| 174 | + }) |
| 175 | + ).toThrow('channelId cannot contain a path separator') |
| 176 | + }) |
| 177 | +}) |
| 178 | + |
| 179 | +describe('Discord send message', () => { |
| 180 | + it('posts a file-only message without placeholder text', () => { |
| 181 | + const input = discordSendMessageTool.operation.input({ |
| 182 | + botToken: 'bot', |
| 183 | + serverId: '1', |
| 184 | + channelId: '9', |
| 185 | + files: [{ id: 'f', name: 'a.png', url: 'u', size: 1, type: 'image/png', key: 'k' }], |
| 186 | + } as never) as { content?: string } |
| 187 | + expect(input.content).toBeUndefined() |
| 188 | + }) |
| 189 | +}) |
0 commit comments