|
1 | 1 | // Mattermost tests cover slash state plugin behavior. |
2 | | -import { describe, expect, it } from "vitest"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
3 | 3 | import type { OpenClawConfig, RuntimeEnv } from "../runtime-api.js"; |
4 | 4 | import type { ResolvedMattermostAccount } from "./accounts.js"; |
5 | 5 | import type { MattermostRegisteredCommand } from "./slash-commands.js"; |
@@ -48,6 +48,49 @@ const slashApi = { |
48 | 48 | runtime: RuntimeEnv; |
49 | 49 | }; |
50 | 50 |
|
| 51 | +const ACCOUNT_STATES_KEY = Symbol.for("openclaw.mattermost.slash-account-states"); |
| 52 | + |
| 53 | +describe("slash-state global singleton", () => { |
| 54 | + afterEach(() => { |
| 55 | + deactivateSlashCommands(); |
| 56 | + }); |
| 57 | + |
| 58 | + it("anchors accountStates on globalThis", () => { |
| 59 | + deactivateSlashCommands(); |
| 60 | + activateSlashCommands({ |
| 61 | + account: createResolvedMattermostAccount("a1"), |
| 62 | + commandTokens: ["tok-a"], |
| 63 | + registeredCommands: [], |
| 64 | + api: slashApi, |
| 65 | + }); |
| 66 | + |
| 67 | + const globalStore = globalThis as Record<PropertyKey, unknown>; |
| 68 | + const map = globalStore[ACCOUNT_STATES_KEY]; |
| 69 | + expect(map).toBeInstanceOf(Map); |
| 70 | + expect((map as Map<string, unknown>).has("a1")).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it("preserves slash state across module reloads", async () => { |
| 74 | + deactivateSlashCommands(); |
| 75 | + activateSlashCommands({ |
| 76 | + account: createResolvedMattermostAccount("a1"), |
| 77 | + commandTokens: ["tok-reload"], |
| 78 | + registeredCommands: [], |
| 79 | + api: slashApi, |
| 80 | + }); |
| 81 | + |
| 82 | + vi.resetModules(); |
| 83 | + const reloaded = await import("./slash-state.js"); |
| 84 | + const match = reloaded.resolveSlashHandlerForToken("tok-reload"); |
| 85 | + |
| 86 | + expect(match.kind).toBe("single"); |
| 87 | + if (match.kind !== "single") { |
| 88 | + throw new Error("expected single match after module reload"); |
| 89 | + } |
| 90 | + expect(match.accountIds).toEqual(["a1"]); |
| 91 | + }); |
| 92 | +}); |
| 93 | + |
51 | 94 | describe("slash-state token routing", () => { |
52 | 95 | it("returns single match when token belongs to one account", () => { |
53 | 96 | deactivateSlashCommands(); |
|
0 commit comments