From 3fb1abcdcbc556023e0d80ef75b219efb695aeb4 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 6 May 2026 08:56:09 +0100 Subject: [PATCH] test: isolate directory contract fixtures --- extensions/mattermost/src/channel.ts | 44 +++++++++++++++---- .../threading-directory-contract-suites.ts | 24 ++++++---- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/extensions/mattermost/src/channel.ts b/extensions/mattermost/src/channel.ts index 3458e5df81c..50bf98cad82 100644 --- a/extensions/mattermost/src/channel.ts +++ b/extensions/mattermost/src/channel.ts @@ -59,6 +59,10 @@ import type { MattermostConfig } from "./types.js"; const loadMattermostChannelRuntime = createLazyRuntimeModule(() => import("./channel.runtime.js")); +type MattermostDirectoryListParams = Parameters< + NonNullable["listGroups"]> +>[0]; + const mattermostSecurityAdapter = createRestrictSendersChannelSecurity({ channelKey: "mattermost", resolveDmPolicy: (account) => account.config.dmPolicy, @@ -110,6 +114,34 @@ function describeMattermostMessageTool({ }; } +function hasConfiguredMattermostDirectoryAccount({ + cfg, + accountId, +}: Pick): boolean { + const accounts = accountId + ? [resolveMattermostAccount({ cfg, accountId })] + : listMattermostAccountIds(cfg).map((listedAccountId) => + resolveMattermostAccount({ cfg, accountId: listedAccountId }), + ); + return accounts.some((account) => + Boolean(account.enabled && account.botToken?.trim() && account.baseUrl?.trim()), + ); +} + +async function listMattermostDirectoryGroups(params: MattermostDirectoryListParams) { + if (!hasConfiguredMattermostDirectoryAccount(params)) { + return []; + } + return (await loadMattermostChannelRuntime()).listMattermostDirectoryGroups(params); +} + +async function listMattermostDirectoryPeers(params: MattermostDirectoryListParams) { + if (!hasConfiguredMattermostDirectoryAccount(params)) { + return []; + } + return (await loadMattermostChannelRuntime()).listMattermostDirectoryPeers(params); +} + const mattermostMessageActions: ChannelMessageActionAdapter = { describeMessageTool: describeMattermostMessageTool, supportsAction: ({ action }) => { @@ -379,14 +411,10 @@ export const mattermostPlugin: ChannelPlugin = create collectRuntimeConfigAssignments, }, directory: createChannelDirectoryAdapter({ - listGroups: async (params) => - (await loadMattermostChannelRuntime()).listMattermostDirectoryGroups(params), - listGroupsLive: async (params) => - (await loadMattermostChannelRuntime()).listMattermostDirectoryGroups(params), - listPeers: async (params) => - (await loadMattermostChannelRuntime()).listMattermostDirectoryPeers(params), - listPeersLive: async (params) => - (await loadMattermostChannelRuntime()).listMattermostDirectoryPeers(params), + listGroups: listMattermostDirectoryGroups, + listGroupsLive: listMattermostDirectoryGroups, + listPeers: listMattermostDirectoryPeers, + listPeersLive: listMattermostDirectoryPeers, }), messaging: { targetPrefixes: ["mattermost"], diff --git a/src/channels/plugins/contracts/test-helpers/threading-directory-contract-suites.ts b/src/channels/plugins/contracts/test-helpers/threading-directory-contract-suites.ts index ab94129b4ee..3af5e712ff1 100644 --- a/src/channels/plugins/contracts/test-helpers/threading-directory-contract-suites.ts +++ b/src/channels/plugins/contracts/test-helpers/threading-directory-contract-suites.ts @@ -181,14 +181,22 @@ export async function expectChannelDirectoryBaseContract(params: { }) { const directory = params.plugin.directory; expect(directory).toBeDefined(); + const cfg = + params.cfg ?? + ({ + channels: { + [params.plugin.id]: { enabled: false }, + }, + } as unknown as OpenClawConfig); + const accountId = params.accountId ?? "default"; if (params.coverage === "presence") { return; } const runtime = await getDirectoryContractRuntime(); const self = await directory?.self?.({ - cfg: params.cfg ?? ({} as OpenClawConfig), - accountId: params.accountId ?? "default", + cfg, + accountId, runtime, }); if (self) { @@ -197,8 +205,8 @@ export async function expectChannelDirectoryBaseContract(params: { const peers = (await directory?.listPeers?.({ - cfg: params.cfg ?? ({} as OpenClawConfig), - accountId: params.accountId ?? "default", + cfg, + accountId, query: "", limit: 5, runtime, @@ -210,8 +218,8 @@ export async function expectChannelDirectoryBaseContract(params: { const groups = (await directory?.listGroups?.({ - cfg: params.cfg ?? ({} as OpenClawConfig), - accountId: params.accountId ?? "default", + cfg, + accountId, query: "", limit: 5, runtime, @@ -223,8 +231,8 @@ export async function expectChannelDirectoryBaseContract(params: { if (directory?.listGroupMembers && groups[0]?.id) { const members = await directory.listGroupMembers({ - cfg: params.cfg ?? ({} as OpenClawConfig), - accountId: params.accountId ?? "default", + cfg, + accountId, groupId: groups[0].id, limit: 5, runtime,