|
| 1 | +import { listNativeCommandSpecsForConfig as listRealNativeCommandSpecsForConfig } from "openclaw/plugin-sdk/command-auth-native"; |
| 2 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 3 | +import type { NativeCommandSpec } from "openclaw/plugin-sdk/native-command-registry"; |
| 4 | +import { |
| 5 | + createTestRegistry, |
| 6 | + resetPluginRuntimeStateForTest, |
| 7 | + setActivePluginRegistry, |
| 8 | +} from "openclaw/plugin-sdk/plugin-test-runtime"; |
| 9 | +import { danger, warn, type RuntimeEnv } from "openclaw/plugin-sdk/runtime-env"; |
| 10 | +import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime"; |
| 11 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 12 | +import { discordSetupPlugin } from "../channel.setup.js"; |
| 13 | +import { DISCORD_VOICE_COMMAND_SPEC } from "../voice/command.js"; |
| 14 | +import { resolveDiscordProviderCommandSpecs } from "./provider.commands.js"; |
| 15 | + |
| 16 | +type ResolverParams = Parameters<typeof resolveDiscordProviderCommandSpecs>[0]; |
| 17 | +type SkillCommands = ReturnType<NonNullable<ResolverParams["listSkillCommandsForAgents"]>>; |
| 18 | + |
| 19 | +const cfg: OpenClawConfig = {}; |
| 20 | +const skillCommands = [ |
| 21 | + { name: "skill-only", skillName: "Skill Only", description: "Skill only" }, |
| 22 | + { name: "extra-skill", skillName: "Extra Skill", description: "Extra skill" }, |
| 23 | +]; |
| 24 | + |
| 25 | +function createResolverHarness( |
| 26 | + options: { |
| 27 | + pluginCommandSpecs?: NativeCommandSpec[]; |
| 28 | + voiceEnabled?: boolean; |
| 29 | + nativeCommandSpecs?: NativeCommandSpec[]; |
| 30 | + skillCommands?: SkillCommands; |
| 31 | + maxDiscordCommands?: number; |
| 32 | + nativeSkillsEnabled?: boolean; |
| 33 | + } = {}, |
| 34 | +) { |
| 35 | + const error = vi.fn(); |
| 36 | + const log = vi.fn(); |
| 37 | + const runtime: RuntimeEnv = { error, log, exit: vi.fn() }; |
| 38 | + const configuredSkillCommands = options.skillCommands ?? skillCommands; |
| 39 | + const nativeCommandSpecs = options.nativeCommandSpecs ?? [ |
| 40 | + { name: "built-in", description: "Built in", acceptsArgs: false }, |
| 41 | + ]; |
| 42 | + const listSkillCommandsForAgents = vi.fn(() => configuredSkillCommands); |
| 43 | + const listNativeCommandSpecsForConfig = vi.fn( |
| 44 | + ( |
| 45 | + _config: OpenClawConfig, |
| 46 | + listOptions?: Parameters<NonNullable<ResolverParams["listNativeCommandSpecsForConfig"]>>[1], |
| 47 | + ): NativeCommandSpec[] => [ |
| 48 | + ...nativeCommandSpecs, |
| 49 | + ...(listOptions?.skillCommands ?? []).map((skill) => ({ |
| 50 | + name: skill.name, |
| 51 | + description: skill.description, |
| 52 | + acceptsArgs: true, |
| 53 | + })), |
| 54 | + ], |
| 55 | + ); |
| 56 | + const getPluginCommandSpecs = vi.fn(() => options.pluginCommandSpecs ?? []); |
| 57 | + |
| 58 | + return { |
| 59 | + error, |
| 60 | + getPluginCommandSpecs, |
| 61 | + listNativeCommandSpecsForConfig, |
| 62 | + listSkillCommandsForAgents, |
| 63 | + log, |
| 64 | + resolve: () => |
| 65 | + resolveDiscordProviderCommandSpecs({ |
| 66 | + cfg, |
| 67 | + runtime, |
| 68 | + nativeEnabled: true, |
| 69 | + nativeSkillsEnabled: options.nativeSkillsEnabled ?? true, |
| 70 | + voiceEnabled: options.voiceEnabled ?? false, |
| 71 | + maxDiscordCommands: options.maxDiscordCommands ?? 3, |
| 72 | + listSkillCommandsForAgents, |
| 73 | + listNativeCommandSpecsForConfig, |
| 74 | + getPluginCommandSpecs, |
| 75 | + }), |
| 76 | + }; |
| 77 | +} |
| 78 | + |
| 79 | +describe("resolveDiscordProviderCommandSpecs", () => { |
| 80 | + beforeEach(() => { |
| 81 | + resetPluginRuntimeStateForTest(); |
| 82 | + }); |
| 83 | + |
| 84 | + afterEach(() => { |
| 85 | + resetPluginRuntimeStateForTest(); |
| 86 | + }); |
| 87 | + |
| 88 | + it("discards provisional skill collisions when command overflow removes skills", async () => { |
| 89 | + const harness = createResolverHarness({ |
| 90 | + voiceEnabled: true, |
| 91 | + maxDiscordCommands: 4, |
| 92 | + pluginCommandSpecs: [ |
| 93 | + { |
| 94 | + name: "skill-only", |
| 95 | + description: "Plugin skill alias", |
| 96 | + descriptionLocalizations: { de: "Plugin-Fertigkeitsalias" }, |
| 97 | + acceptsArgs: false, |
| 98 | + }, |
| 99 | + { name: "plugin-unique", description: "Unique plugin", acceptsArgs: false }, |
| 100 | + ], |
| 101 | + }); |
| 102 | + |
| 103 | + const resolved = await harness.resolve(); |
| 104 | + |
| 105 | + expect(resolved.skillCommands).toEqual([]); |
| 106 | + expect(resolved.commandSpecs.map((command) => command.name)).toEqual([ |
| 107 | + "built-in", |
| 108 | + "vc", |
| 109 | + "skill-only", |
| 110 | + "plugin-unique", |
| 111 | + ]); |
| 112 | + expect(resolved.commandSpecs[2]).toEqual({ |
| 113 | + name: "skill-only", |
| 114 | + description: "Plugin skill alias", |
| 115 | + descriptionLocalizations: { de: "Plugin-Fertigkeitsalias" }, |
| 116 | + acceptsArgs: false, |
| 117 | + }); |
| 118 | + expect(harness.error).not.toHaveBeenCalled(); |
| 119 | + expect(harness.getPluginCommandSpecs).toHaveBeenCalledOnce(); |
| 120 | + expect(harness.getPluginCommandSpecs).toHaveBeenCalledWith("discord", { config: cfg }); |
| 121 | + expect(harness.listNativeCommandSpecsForConfig).toHaveBeenCalledTimes(2); |
| 122 | + expect(harness.log).toHaveBeenCalledOnce(); |
| 123 | + expect(harness.log).toHaveBeenCalledWith( |
| 124 | + warn( |
| 125 | + "5 commands exceed the 4-command Discord limit; removing per-skill commands and keeping /skill.", |
| 126 | + ), |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it("logs a final built-in collision once when command overflow retries without skills", async () => { |
| 131 | + const harness = createResolverHarness({ |
| 132 | + voiceEnabled: true, |
| 133 | + maxDiscordCommands: 4, |
| 134 | + pluginCommandSpecs: [ |
| 135 | + { name: "built-in", description: "Built-in collision", acceptsArgs: false }, |
| 136 | + { name: "plugin-unique", description: "Unique plugin", acceptsArgs: false }, |
| 137 | + ], |
| 138 | + }); |
| 139 | + |
| 140 | + const resolved = await harness.resolve(); |
| 141 | + |
| 142 | + expect(resolved.skillCommands).toEqual([]); |
| 143 | + expect(resolved.commandSpecs.map((command) => command.name)).toEqual([ |
| 144 | + "built-in", |
| 145 | + "vc", |
| 146 | + "plugin-unique", |
| 147 | + ]); |
| 148 | + expect(harness.error).toHaveBeenCalledOnce(); |
| 149 | + expect(harness.error).toHaveBeenCalledWith( |
| 150 | + danger( |
| 151 | + 'discord: plugin command "/built-in" duplicates an existing native command. Skipping.', |
| 152 | + ), |
| 153 | + ); |
| 154 | + expect(harness.getPluginCommandSpecs).toHaveBeenCalledOnce(); |
| 155 | + expect(harness.getPluginCommandSpecs).toHaveBeenCalledWith("discord", { config: cfg }); |
| 156 | + expect(harness.listNativeCommandSpecsForConfig).toHaveBeenCalledTimes(2); |
| 157 | + }); |
| 158 | + |
| 159 | + it("counts voice in the exact Discord command limit", async () => { |
| 160 | + const harness = createResolverHarness({ |
| 161 | + voiceEnabled: true, |
| 162 | + nativeSkillsEnabled: false, |
| 163 | + maxDiscordCommands: 100, |
| 164 | + nativeCommandSpecs: Array.from({ length: 99 }, (_value, index) => ({ |
| 165 | + name: `command-${String(index + 1)}`, |
| 166 | + description: `Command ${String(index + 1)}`, |
| 167 | + acceptsArgs: false, |
| 168 | + })), |
| 169 | + }); |
| 170 | + |
| 171 | + const resolved = await harness.resolve(); |
| 172 | + |
| 173 | + expect(resolved.commandSpecs).toHaveLength(100); |
| 174 | + expect(resolved.commandSpecs.at(-1)).toBe(DISCORD_VOICE_COMMAND_SPEC); |
| 175 | + expect(harness.log).not.toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + |
| 178 | + it("retains voice ownership when a plugin claims vc", async () => { |
| 179 | + const harness = createResolverHarness({ |
| 180 | + voiceEnabled: true, |
| 181 | + nativeSkillsEnabled: false, |
| 182 | + maxDiscordCommands: 100, |
| 183 | + pluginCommandSpecs: [{ name: "vc", description: "Plugin voice", acceptsArgs: false }], |
| 184 | + }); |
| 185 | + |
| 186 | + const resolved = await harness.resolve(); |
| 187 | + |
| 188 | + expect(resolved.commandSpecs.map((command) => command.name)).toEqual(["built-in", "vc"]); |
| 189 | + expect(resolved.commandSpecs[1]).toBe(DISCORD_VOICE_COMMAND_SPEC); |
| 190 | + expect(harness.error).toHaveBeenCalledOnce(); |
| 191 | + expect(harness.error).toHaveBeenCalledWith( |
| 192 | + danger('discord: plugin command "/vc" duplicates an existing native command. Skipping.'), |
| 193 | + ); |
| 194 | + expect(harness.getPluginCommandSpecs).toHaveBeenCalledOnce(); |
| 195 | + }); |
| 196 | + |
| 197 | + it("keeps a skill named vc from shadowing or duplicating voice", async () => { |
| 198 | + const vcSkillCommands: SkillCommands = [ |
| 199 | + { name: "vc", skillName: "Voice Skill", description: "Voice skill" }, |
| 200 | + ]; |
| 201 | + const harness = createResolverHarness({ |
| 202 | + voiceEnabled: true, |
| 203 | + maxDiscordCommands: 100, |
| 204 | + skillCommands: vcSkillCommands, |
| 205 | + }); |
| 206 | + |
| 207 | + const resolved = await harness.resolve(); |
| 208 | + |
| 209 | + expect(resolved.skillCommands).toEqual(vcSkillCommands); |
| 210 | + expect(resolved.commandSpecs.map((command) => command.name)).toEqual(["built-in", "vc"]); |
| 211 | + expect(resolved.commandSpecs[1]).toBe(DISCORD_VOICE_COMMAND_SPEC); |
| 212 | + expect(harness.error).not.toHaveBeenCalled(); |
| 213 | + }); |
| 214 | + |
| 215 | + it("deduplicates provider-renamed primary specs before Discord cap planning", async () => { |
| 216 | + setActivePluginRegistry( |
| 217 | + createTestRegistry([{ pluginId: "discord", plugin: discordSetupPlugin, source: "test" }]), |
| 218 | + ); |
| 219 | + const voiceSkill: SkillCommands[number] = { |
| 220 | + name: "voice", |
| 221 | + skillName: "Voice Skill", |
| 222 | + description: "Skill voice", |
| 223 | + }; |
| 224 | + const config: OpenClawConfig = { commands: { native: true, nativeSkills: true } }; |
| 225 | + const rawPrimary = listRealNativeCommandSpecsForConfig(config, { |
| 226 | + provider: "discord", |
| 227 | + skillCommands: [voiceSkill], |
| 228 | + }); |
| 229 | + const rawVoice = rawPrimary.filter( |
| 230 | + (spec) => normalizeLowercaseStringOrEmpty(spec.name) === "voice", |
| 231 | + ); |
| 232 | + const uniqueCount = new Set( |
| 233 | + rawPrimary.map((spec) => normalizeLowercaseStringOrEmpty(spec.name)).filter(Boolean), |
| 234 | + ).size; |
| 235 | + expect(rawVoice).toHaveLength(2); |
| 236 | + expect(rawVoice.map((spec) => spec.description)).toEqual([ |
| 237 | + "Control text-to-speech (TTS).", |
| 238 | + "Skill voice", |
| 239 | + ]); |
| 240 | + const log = vi.fn(); |
| 241 | + |
| 242 | + const resolved = await resolveDiscordProviderCommandSpecs({ |
| 243 | + cfg: config, |
| 244 | + runtime: { log, error: vi.fn(), exit: vi.fn() }, |
| 245 | + nativeEnabled: true, |
| 246 | + nativeSkillsEnabled: true, |
| 247 | + voiceEnabled: false, |
| 248 | + maxDiscordCommands: uniqueCount, |
| 249 | + listSkillCommandsForAgents: vi.fn(() => [voiceSkill]), |
| 250 | + getPluginCommandSpecs: vi.fn(() => []), |
| 251 | + }); |
| 252 | + |
| 253 | + expect(resolved.skillCommands).toEqual([voiceSkill]); |
| 254 | + expect(resolved.commandSpecs).toHaveLength(uniqueCount); |
| 255 | + expect( |
| 256 | + resolved.commandSpecs.filter( |
| 257 | + (spec) => normalizeLowercaseStringOrEmpty(spec.name) === "voice", |
| 258 | + ), |
| 259 | + ).toEqual([expect.objectContaining({ description: "Control text-to-speech (TTS)." })]); |
| 260 | + expect(log).not.toHaveBeenCalled(); |
| 261 | + }); |
| 262 | +}); |
0 commit comments