From fdcf26ffd332962e10fcdb70dd4e7c2ee4e520e3 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Sun, 22 Mar 2026 17:58:53 -0700 Subject: [PATCH] test(mattermost): cover setup adapter --- extensions/mattermost/src/setup-core.test.ts | 149 +++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 extensions/mattermost/src/setup-core.test.ts diff --git a/extensions/mattermost/src/setup-core.test.ts b/extensions/mattermost/src/setup-core.test.ts new file mode 100644 index 00000000000..4dac2947b4c --- /dev/null +++ b/extensions/mattermost/src/setup-core.test.ts @@ -0,0 +1,149 @@ +import { DEFAULT_ACCOUNT_ID } from "openclaw/plugin-sdk/setup"; +import { describe, expect, it, vi } from "vitest"; + +const resolveMattermostAccount = vi.hoisted(() => vi.fn()); +const normalizeMattermostBaseUrl = vi.hoisted(() => vi.fn((value: string | undefined) => value)); +const hasConfiguredSecretInput = vi.hoisted(() => vi.fn((value: unknown) => Boolean(value))); + +vi.mock("./mattermost/accounts.js", () => ({ + resolveMattermostAccount, +})); + +vi.mock("./mattermost/client.js", () => ({ + normalizeMattermostBaseUrl, +})); + +vi.mock("./secret-input.js", () => ({ + hasConfiguredSecretInput, +})); + +describe("mattermost setup core", () => { + it("reports configuration only when token and base url are both present", async () => { + const { isMattermostConfigured } = await import("./setup-core.js"); + + expect( + isMattermostConfigured({ + botToken: "bot-token", + baseUrl: "https://chat.example.com", + config: {}, + } as never), + ).toBe(true); + + expect( + isMattermostConfigured({ + botToken: "", + baseUrl: "https://chat.example.com", + config: { botToken: "secret-ref" }, + } as never), + ).toBe(true); + + expect( + isMattermostConfigured({ + botToken: "", + baseUrl: "", + config: {}, + } as never), + ).toBe(false); + }); + + it("resolves accounts with unresolved secret refs allowed", async () => { + resolveMattermostAccount.mockReturnValue({ accountId: "default" }); + + const { resolveMattermostAccountWithSecrets } = await import("./setup-core.js"); + const cfg = { channels: { mattermost: {} } }; + + expect(resolveMattermostAccountWithSecrets(cfg as never, "default")).toEqual({ + accountId: "default", + }); + expect(resolveMattermostAccount).toHaveBeenCalledWith({ + cfg, + accountId: "default", + allowUnresolvedSecretRef: true, + }); + }); + + it("validates env and explicit credential requirements", async () => { + const { mattermostSetupAdapter } = await import("./setup-core.js"); + + expect( + mattermostSetupAdapter.validateInput({ + accountId: "secondary", + input: { useEnv: true }, + } as never), + ).toBe("Mattermost env vars can only be used for the default account."); + + normalizeMattermostBaseUrl.mockReturnValue(undefined); + expect( + mattermostSetupAdapter.validateInput({ + accountId: DEFAULT_ACCOUNT_ID, + input: { useEnv: false, botToken: "tok", httpUrl: "not-a-url" }, + } as never), + ).toBe("Mattermost requires --bot-token and --http-url (or --use-env)."); + + normalizeMattermostBaseUrl.mockReturnValue("https://chat.example.com"); + expect( + mattermostSetupAdapter.validateInput({ + accountId: DEFAULT_ACCOUNT_ID, + input: { useEnv: false, botToken: "tok", httpUrl: "https://chat.example.com" }, + } as never), + ).toBeNull(); + }); + + it("applies normalized config for default and named accounts", async () => { + normalizeMattermostBaseUrl.mockReturnValue("https://chat.example.com"); + const { mattermostSetupAdapter } = await import("./setup-core.js"); + + expect( + mattermostSetupAdapter.applyAccountConfig({ + cfg: { channels: { mattermost: {} } }, + accountId: DEFAULT_ACCOUNT_ID, + input: { + name: "Default", + botToken: "tok", + httpUrl: "https://chat.example.com", + }, + } as never), + ).toEqual({ + channels: { + mattermost: { + enabled: true, + name: "Default", + botToken: "tok", + baseUrl: "https://chat.example.com", + }, + }, + }); + + expect( + mattermostSetupAdapter.applyAccountConfig({ + cfg: { + channels: { + mattermost: { + name: "Legacy", + }, + }, + }, + accountId: "Work Team", + input: { + name: "Work", + botToken: "tok2", + httpUrl: "https://chat.example.com", + }, + } as never), + ).toMatchObject({ + channels: { + mattermost: { + accounts: { + default: { name: "Legacy" }, + "work-team": { + enabled: true, + name: "Work", + botToken: "tok2", + baseUrl: "https://chat.example.com", + }, + }, + }, + }, + }); + }); +});