test: isolate directory contract fixtures

This commit is contained in:
Peter Steinberger
2026-05-06 08:56:09 +01:00
parent 9edeffc751
commit 3fb1abcdcb
2 changed files with 52 additions and 16 deletions

View File

@@ -59,6 +59,10 @@ import type { MattermostConfig } from "./types.js";
const loadMattermostChannelRuntime = createLazyRuntimeModule(() => import("./channel.runtime.js"));
type MattermostDirectoryListParams = Parameters<
NonNullable<NonNullable<ChannelPlugin["directory"]>["listGroups"]>
>[0];
const mattermostSecurityAdapter = createRestrictSendersChannelSecurity<ResolvedMattermostAccount>({
channelKey: "mattermost",
resolveDmPolicy: (account) => account.config.dmPolicy,
@@ -110,6 +114,34 @@ function describeMattermostMessageTool({
};
}
function hasConfiguredMattermostDirectoryAccount({
cfg,
accountId,
}: Pick<MattermostDirectoryListParams, "cfg" | "accountId">): 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<ResolvedMattermostAccount> = 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"],

View File

@@ -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,