|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const listSkillCommandsForAgents = vi.hoisted(() => vi.fn()); |
| 4 | +const parseStrictPositiveInteger = vi.hoisted(() => vi.fn()); |
| 5 | +const fetchMattermostUserTeams = vi.hoisted(() => vi.fn()); |
| 6 | +const normalizeMattermostBaseUrl = vi.hoisted(() => vi.fn((value: string | undefined) => value)); |
| 7 | +const isSlashCommandsEnabled = vi.hoisted(() => vi.fn()); |
| 8 | +const registerSlashCommands = vi.hoisted(() => vi.fn()); |
| 9 | +const resolveCallbackUrl = vi.hoisted(() => vi.fn()); |
| 10 | +const resolveSlashCommandConfig = vi.hoisted(() => vi.fn()); |
| 11 | +const activateSlashCommands = vi.hoisted(() => vi.fn()); |
| 12 | + |
| 13 | +vi.mock("../runtime-api.js", () => ({ |
| 14 | + listSkillCommandsForAgents, |
| 15 | + parseStrictPositiveInteger, |
| 16 | +})); |
| 17 | + |
| 18 | +vi.mock("./client.js", () => ({ |
| 19 | + fetchMattermostUserTeams, |
| 20 | + normalizeMattermostBaseUrl, |
| 21 | +})); |
| 22 | + |
| 23 | +vi.mock("./slash-commands.js", () => ({ |
| 24 | + DEFAULT_COMMAND_SPECS: [ |
| 25 | + { trigger: "ping", description: "ping" }, |
| 26 | + { trigger: "ping", description: "duplicate" }, |
| 27 | + ], |
| 28 | + isSlashCommandsEnabled, |
| 29 | + registerSlashCommands, |
| 30 | + resolveCallbackUrl, |
| 31 | + resolveSlashCommandConfig, |
| 32 | +})); |
| 33 | + |
| 34 | +vi.mock("./slash-state.js", () => ({ |
| 35 | + activateSlashCommands, |
| 36 | +})); |
| 37 | + |
| 38 | +describe("mattermost monitor slash", () => { |
| 39 | + afterEach(() => { |
| 40 | + vi.unstubAllEnvs(); |
| 41 | + }); |
| 42 | + |
| 43 | + it("returns early when slash commands are disabled", async () => { |
| 44 | + resolveSlashCommandConfig.mockReturnValue({ enabled: false }); |
| 45 | + isSlashCommandsEnabled.mockReturnValue(false); |
| 46 | + const { registerMattermostMonitorSlashCommands } = await import("./monitor-slash.js"); |
| 47 | + |
| 48 | + await registerMattermostMonitorSlashCommands({ |
| 49 | + client: {} as never, |
| 50 | + cfg: {} as never, |
| 51 | + runtime: {} as never, |
| 52 | + account: { config: {} } as never, |
| 53 | + baseUrl: "https://chat.example.com", |
| 54 | + botUserId: "bot-user", |
| 55 | + }); |
| 56 | + |
| 57 | + expect(fetchMattermostUserTeams).not.toHaveBeenCalled(); |
| 58 | + expect(activateSlashCommands).not.toHaveBeenCalled(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("registers deduped default and native skill commands across teams", async () => { |
| 62 | + vi.stubEnv("OPENCLAW_GATEWAY_PORT", "18888"); |
| 63 | + resolveSlashCommandConfig.mockReturnValue({ enabled: true, nativeSkills: true }); |
| 64 | + isSlashCommandsEnabled.mockReturnValue(true); |
| 65 | + parseStrictPositiveInteger.mockReturnValue(18888); |
| 66 | + fetchMattermostUserTeams.mockResolvedValue([{ id: "team-1" }, { id: "team-2" }]); |
| 67 | + resolveCallbackUrl.mockReturnValue("https://openclaw.test/slash"); |
| 68 | + listSkillCommandsForAgents.mockReturnValue([ |
| 69 | + { name: "skill", description: "Skill run" }, |
| 70 | + { name: "oc_ping", description: "Already prefixed" }, |
| 71 | + { name: " ", description: "ignored" }, |
| 72 | + ]); |
| 73 | + registerSlashCommands |
| 74 | + .mockResolvedValueOnce([{ token: "token-1", trigger: "ping" }]) |
| 75 | + .mockResolvedValueOnce([{ token: "token-2", trigger: "oc_skill" }]); |
| 76 | + const runtime = { |
| 77 | + log: vi.fn(), |
| 78 | + error: vi.fn(), |
| 79 | + }; |
| 80 | + |
| 81 | + const { registerMattermostMonitorSlashCommands } = await import("./monitor-slash.js"); |
| 82 | + |
| 83 | + await registerMattermostMonitorSlashCommands({ |
| 84 | + client: {} as never, |
| 85 | + cfg: { gateway: { port: 18789 } } as never, |
| 86 | + runtime: runtime as never, |
| 87 | + account: { config: { commands: {} }, accountId: "default" } as never, |
| 88 | + baseUrl: "https://chat.example.com", |
| 89 | + botUserId: "bot-user", |
| 90 | + }); |
| 91 | + |
| 92 | + expect(registerSlashCommands).toHaveBeenCalledTimes(2); |
| 93 | + expect(registerSlashCommands.mock.calls[0]?.[0]).toMatchObject({ |
| 94 | + teamId: "team-1", |
| 95 | + creatorUserId: "bot-user", |
| 96 | + callbackUrl: "https://openclaw.test/slash", |
| 97 | + }); |
| 98 | + expect(registerSlashCommands.mock.calls[0]?.[0].commands).toEqual([ |
| 99 | + { trigger: "ping", description: "ping" }, |
| 100 | + { |
| 101 | + trigger: "oc_skill", |
| 102 | + description: "Skill run", |
| 103 | + autoComplete: true, |
| 104 | + autoCompleteHint: "[args]", |
| 105 | + originalName: "skill", |
| 106 | + }, |
| 107 | + { |
| 108 | + trigger: "oc_ping", |
| 109 | + description: "Already prefixed", |
| 110 | + autoComplete: true, |
| 111 | + autoCompleteHint: "[args]", |
| 112 | + originalName: "oc_ping", |
| 113 | + }, |
| 114 | + ]); |
| 115 | + expect(activateSlashCommands).toHaveBeenCalledWith( |
| 116 | + expect.objectContaining({ |
| 117 | + commandTokens: ["token-1", "token-2"], |
| 118 | + triggerMap: new Map([ |
| 119 | + ["oc_skill", "skill"], |
| 120 | + ["oc_ping", "oc_ping"], |
| 121 | + ]), |
| 122 | + }), |
| 123 | + ); |
| 124 | + expect(runtime.log).toHaveBeenCalledWith( |
| 125 | + "mattermost: slash commands registered (2 commands across 2 teams, callback=https://openclaw.test/slash)", |
| 126 | + ); |
| 127 | + }); |
| 128 | + |
| 129 | + it("warns on loopback callback urls and reports partial team failures", async () => { |
| 130 | + resolveSlashCommandConfig.mockReturnValue({ enabled: true, nativeSkills: false }); |
| 131 | + isSlashCommandsEnabled.mockReturnValue(true); |
| 132 | + parseStrictPositiveInteger.mockReturnValue(undefined); |
| 133 | + fetchMattermostUserTeams.mockResolvedValue([{ id: "team-1" }, { id: "team-2" }]); |
| 134 | + resolveCallbackUrl.mockReturnValue("http://127.0.0.1:18789/slash"); |
| 135 | + registerSlashCommands |
| 136 | + .mockResolvedValueOnce([{ token: "token-1", trigger: "ping" }]) |
| 137 | + .mockRejectedValueOnce(new Error("boom")); |
| 138 | + const runtime = { |
| 139 | + log: vi.fn(), |
| 140 | + error: vi.fn(), |
| 141 | + }; |
| 142 | + |
| 143 | + const { registerMattermostMonitorSlashCommands } = await import("./monitor-slash.js"); |
| 144 | + |
| 145 | + await registerMattermostMonitorSlashCommands({ |
| 146 | + client: {} as never, |
| 147 | + cfg: { gateway: { customBindHost: "loopback" } } as never, |
| 148 | + runtime: runtime as never, |
| 149 | + account: { config: { commands: {} }, accountId: "default" } as never, |
| 150 | + baseUrl: "https://chat.example.com", |
| 151 | + botUserId: "bot-user", |
| 152 | + }); |
| 153 | + |
| 154 | + expect(runtime.error).toHaveBeenCalledWith( |
| 155 | + expect.stringContaining("slash commands callbackUrl resolved to http://127.0.0.1:18789/slash"), |
| 156 | + ); |
| 157 | + expect(runtime.error).toHaveBeenCalledWith( |
| 158 | + "mattermost: failed to register slash commands for team team-2: Error: boom", |
| 159 | + ); |
| 160 | + expect(runtime.error).toHaveBeenCalledWith( |
| 161 | + "mattermost: slash command registration completed with 1 team error(s)", |
| 162 | + ); |
| 163 | + }); |
| 164 | +}); |
0 commit comments