import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import { afterEach, beforeEach, describe, expect, expectTypeOf, it } from "vitest"; import type { DiscordProbe } from "../../discord/probe.js"; import type { DiscordTokenResolution } from "../../discord/token.js"; import type { IMessageProbe } from "../../imessage/probe.js"; import type { LineProbeResult } from "../../line/types.js"; import { setActivePluginRegistry } from "../../plugins/runtime.js"; import type { SignalProbe } from "../../signal/probe.js"; import type { SlackProbe } from "../../slack/probe.js"; import type { TelegramProbe } from "../../telegram/probe.js"; import type { TelegramTokenResolution } from "../../telegram/token.js"; import { createTestRegistry } from "../../test-utils/channel-plugins.js"; import { getChannelPluginCatalogEntry, listChannelPluginCatalogEntries } from "./catalog.js"; import { resolveChannelConfigWrites } from "./config-writes.js"; import { listDiscordDirectoryGroupsFromConfig, listDiscordDirectoryPeersFromConfig, listSlackDirectoryGroupsFromConfig, listSlackDirectoryPeersFromConfig, listTelegramDirectoryGroupsFromConfig, listTelegramDirectoryPeersFromConfig, listWhatsAppDirectoryGroupsFromConfig, listWhatsAppDirectoryPeersFromConfig, } from "./directory-config.js"; import { listChannelPlugins } from "./index.js"; import { loadChannelPlugin } from "./load.js"; import { loadChannelOutboundAdapter } from "./outbound/load.js"; import type { ChannelOutboundAdapter, ChannelPlugin } from "./types.js"; import type { BaseProbeResult, BaseTokenResolution } from "./types.js"; describe("channel plugin registry", () => { const emptyRegistry = createTestRegistry([]); const createPlugin = (id: string): ChannelPlugin => ({ id, meta: { id, label: id, selectionLabel: id, docsPath: `/channels/${id}`, blurb: "test", }, capabilities: { chatTypes: ["direct"] }, config: { listAccountIds: () => [], resolveAccount: () => ({}), }, }); beforeEach(() => { setActivePluginRegistry(emptyRegistry); }); afterEach(() => { setActivePluginRegistry(emptyRegistry); }); it("sorts channel plugins by configured order", () => { const registry = createTestRegistry( ["slack", "telegram", "signal"].map((id) => ({ pluginId: id, plugin: createPlugin(id), source: "test", })), ); setActivePluginRegistry(registry); const pluginIds = listChannelPlugins().map((plugin) => plugin.id); expect(pluginIds).toEqual(["telegram", "slack", "signal"]); }); }); describe("channel plugin catalog", () => { it("includes Microsoft Teams", () => { const entry = getChannelPluginCatalogEntry("msteams"); expect(entry?.install.npmSpec).toBe("@openclaw/msteams"); expect(entry?.meta.aliases).toContain("teams"); }); it("lists plugin catalog entries", () => { const ids = listChannelPluginCatalogEntries().map((entry) => entry.id); expect(ids).toContain("msteams"); }); it("includes external catalog entries", () => { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-catalog-")); const catalogPath = path.join(dir, "catalog.json"); fs.writeFileSync( catalogPath, JSON.stringify({ entries: [ { name: "@openclaw/demo-channel", openclaw: { channel: { id: "demo-channel", label: "Demo Channel", selectionLabel: "Demo Channel", docsPath: "/channels/demo-channel", blurb: "Demo entry", order: 999, }, install: { npmSpec: "@openclaw/demo-channel", }, }, }, ], }), ); const ids = listChannelPluginCatalogEntries({ catalogPaths: [catalogPath] }).map( (entry) => entry.id, ); expect(ids).toContain("demo-channel"); }); }); const emptyRegistry = createTestRegistry([]); const msteamsOutbound: ChannelOutboundAdapter = { deliveryMode: "direct", sendText: async () => ({ channel: "msteams", messageId: "m1" }), sendMedia: async () => ({ channel: "msteams", messageId: "m2" }), }; const msteamsPlugin: ChannelPlugin = { id: "msteams", meta: { id: "msteams", label: "Microsoft Teams", selectionLabel: "Microsoft Teams (Bot Framework)", docsPath: "/channels/msteams", blurb: "Bot Framework; enterprise support.", aliases: ["teams"], }, capabilities: { chatTypes: ["direct"] }, config: { listAccountIds: () => [], resolveAccount: () => ({}), }, outbound: msteamsOutbound, }; const registryWithMSTeams = createTestRegistry([ { pluginId: "msteams", plugin: msteamsPlugin, source: "test" }, ]); describe("channel plugin loader", () => { beforeEach(() => { setActivePluginRegistry(emptyRegistry); }); afterEach(() => { setActivePluginRegistry(emptyRegistry); }); it("loads channel plugins from the active registry", async () => { setActivePluginRegistry(registryWithMSTeams); const plugin = await loadChannelPlugin("msteams"); expect(plugin).toBe(msteamsPlugin); }); it("loads outbound adapters from registered plugins", async () => { setActivePluginRegistry(registryWithMSTeams); const outbound = await loadChannelOutboundAdapter("msteams"); expect(outbound).toBe(msteamsOutbound); }); }); describe("BaseProbeResult assignability", () => { it("TelegramProbe satisfies BaseProbeResult", () => { expectTypeOf().toMatchTypeOf(); }); it("DiscordProbe satisfies BaseProbeResult", () => { expectTypeOf().toMatchTypeOf(); }); it("SlackProbe satisfies BaseProbeResult", () => { expectTypeOf().toMatchTypeOf(); }); it("SignalProbe satisfies BaseProbeResult", () => { expectTypeOf().toMatchTypeOf(); }); it("IMessageProbe satisfies BaseProbeResult", () => { expectTypeOf().toMatchTypeOf(); }); it("LineProbeResult satisfies BaseProbeResult", () => { expectTypeOf().toMatchTypeOf(); }); }); describe("BaseTokenResolution assignability", () => { it("TelegramTokenResolution satisfies BaseTokenResolution", () => { expectTypeOf().toMatchTypeOf(); }); it("DiscordTokenResolution satisfies BaseTokenResolution", () => { expectTypeOf().toMatchTypeOf(); }); }); describe("resolveChannelConfigWrites", () => { it("defaults to allow when unset", () => { const cfg = {}; expect(resolveChannelConfigWrites({ cfg, channelId: "slack" })).toBe(true); }); it("blocks when channel config disables writes", () => { const cfg = { channels: { slack: { configWrites: false } } }; expect(resolveChannelConfigWrites({ cfg, channelId: "slack" })).toBe(false); }); it("account override wins over channel default", () => { const cfg = { channels: { slack: { configWrites: true, accounts: { work: { configWrites: false }, }, }, }, }; expect(resolveChannelConfigWrites({ cfg, channelId: "slack", accountId: "work" })).toBe(false); }); it("matches account ids case-insensitively", () => { const cfg = { channels: { slack: { configWrites: true, accounts: { Work: { configWrites: false }, }, }, }, }; expect(resolveChannelConfigWrites({ cfg, channelId: "slack", accountId: "work" })).toBe(false); }); }); describe("directory (config-backed)", () => { it("lists Slack peers/groups from config", async () => { const cfg = { channels: { slack: { botToken: "xoxb-test", appToken: "xapp-test", dm: { allowFrom: ["U123", "user:U999"] }, dms: { U234: {} }, channels: { C111: { users: ["U777"] } }, }, }, // oxlint-disable-next-line typescript/no-explicit-any } as any; const peers = await listSlackDirectoryPeersFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(peers?.map((e) => e.id).toSorted()).toEqual([ "user:u123", "user:u234", "user:u777", "user:u999", ]); const groups = await listSlackDirectoryGroupsFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(groups?.map((e) => e.id)).toEqual(["channel:c111"]); }); it("lists Discord peers/groups from config (numeric ids only)", async () => { const cfg = { channels: { discord: { token: "discord-test", dm: { allowFrom: ["<@111>", "nope"] }, dms: { "222": {} }, guilds: { "123": { users: ["<@12345>", "not-an-id"], channels: { "555": {}, "channel:666": {}, general: {}, }, }, }, }, }, // oxlint-disable-next-line typescript/no-explicit-any } as any; const peers = await listDiscordDirectoryPeersFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(peers?.map((e) => e.id).toSorted()).toEqual(["user:111", "user:12345", "user:222"]); const groups = await listDiscordDirectoryGroupsFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(groups?.map((e) => e.id).toSorted()).toEqual(["channel:555", "channel:666"]); }); it("lists Telegram peers/groups from config", async () => { const cfg = { channels: { telegram: { botToken: "telegram-test", allowFrom: ["123", "alice", "tg:@bob"], dms: { "456": {} }, groups: { "-1001": {}, "*": {} }, }, }, // oxlint-disable-next-line typescript/no-explicit-any } as any; const peers = await listTelegramDirectoryPeersFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(peers?.map((e) => e.id).toSorted()).toEqual(["123", "456", "@alice", "@bob"]); const groups = await listTelegramDirectoryGroupsFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(groups?.map((e) => e.id)).toEqual(["-1001"]); }); it("lists WhatsApp peers/groups from config", async () => { const cfg = { channels: { whatsapp: { allowFrom: ["+15550000000", "*", "123@g.us"], groups: { "999@g.us": { requireMention: true }, "*": {} }, }, }, // oxlint-disable-next-line typescript/no-explicit-any } as any; const peers = await listWhatsAppDirectoryPeersFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(peers?.map((e) => e.id)).toEqual(["+15550000000"]); const groups = await listWhatsAppDirectoryGroupsFromConfig({ cfg, accountId: "default", query: null, limit: null, }); expect(groups?.map((e) => e.id)).toEqual(["999@g.us"]); }); it("applies query and limit filtering for config-backed directories", async () => { const cfg = { channels: { slack: { botToken: "xoxb-test", appToken: "xapp-test", dm: { allowFrom: ["U100", "U200"] }, dms: { U300: {} }, channels: { C111: {}, C222: {}, C333: {} }, }, discord: { token: "discord-test", guilds: { "123": { channels: { "555": {}, "666": {}, "777": {}, }, }, }, }, telegram: { botToken: "telegram-test", groups: { "-1001": {}, "-1002": {}, "-2001": {} }, }, whatsapp: { groups: { "111@g.us": {}, "222@g.us": {}, "333@s.whatsapp.net": {} }, }, }, // oxlint-disable-next-line typescript/no-explicit-any } as any; const slackPeers = await listSlackDirectoryPeersFromConfig({ cfg, accountId: "default", query: "user:u", limit: 2, }); expect(slackPeers).toHaveLength(2); expect(slackPeers.every((entry) => entry.id.startsWith("user:u"))).toBe(true); const discordGroups = await listDiscordDirectoryGroupsFromConfig({ cfg, accountId: "default", query: "666", limit: 5, }); expect(discordGroups.map((entry) => entry.id)).toEqual(["channel:666"]); const telegramGroups = await listTelegramDirectoryGroupsFromConfig({ cfg, accountId: "default", query: "-100", limit: 1, }); expect(telegramGroups.map((entry) => entry.id)).toEqual(["-1001"]); const whatsAppGroups = await listWhatsAppDirectoryGroupsFromConfig({ cfg, accountId: "default", query: "@g.us", limit: 1, }); expect(whatsAppGroups.map((entry) => entry.id)).toEqual(["111@g.us"]); }); });