mirror of
https://github.com/moltbot/moltbot.git
synced 2026-04-27 00:17:29 +00:00
fix(channels): parse bundled targets without plugin registry
This commit is contained in:
59
src/channels/plugins/target-parsing.test.ts
Normal file
59
src/channels/plugins/target-parsing.test.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { setActivePluginRegistry } from "../../plugins/runtime.js";
|
||||
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
|
||||
import { parseExplicitTargetForChannel } from "./target-parsing.js";
|
||||
|
||||
describe("parseExplicitTargetForChannel", () => {
|
||||
beforeEach(() => {
|
||||
setActivePluginRegistry(createTestRegistry([]));
|
||||
});
|
||||
|
||||
it("parses bundled Telegram targets without an active Telegram registry entry", () => {
|
||||
expect(parseExplicitTargetForChannel("telegram", "telegram:group:-100123:topic:77")).toEqual({
|
||||
to: "-100123",
|
||||
threadId: 77,
|
||||
chatType: "group",
|
||||
});
|
||||
expect(parseExplicitTargetForChannel("telegram", "-100123")).toEqual({
|
||||
to: "-100123",
|
||||
chatType: "group",
|
||||
});
|
||||
});
|
||||
|
||||
it("parses registered non-bundled channel targets via the active plugin contract", () => {
|
||||
setActivePluginRegistry(
|
||||
createTestRegistry([
|
||||
{
|
||||
pluginId: "msteams",
|
||||
source: "test",
|
||||
plugin: {
|
||||
id: "msteams",
|
||||
meta: {
|
||||
id: "msteams",
|
||||
label: "Microsoft Teams",
|
||||
selectionLabel: "Microsoft Teams",
|
||||
docsPath: "/channels/msteams",
|
||||
blurb: "test stub",
|
||||
},
|
||||
capabilities: { chatTypes: ["direct"] },
|
||||
config: {
|
||||
listAccountIds: () => [],
|
||||
resolveAccount: () => ({}),
|
||||
},
|
||||
messaging: {
|
||||
parseExplicitTarget: ({ raw }: { raw: string }) => ({
|
||||
to: raw.trim().toUpperCase(),
|
||||
chatType: "direct" as const,
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
]),
|
||||
);
|
||||
|
||||
expect(parseExplicitTargetForChannel("msteams", "team-room")).toEqual({
|
||||
to: "TEAM-ROOM",
|
||||
chatType: "direct",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,4 +1,7 @@
|
||||
import { parseDiscordTarget } from "../../../extensions/discord/src/targets.js";
|
||||
import { parseTelegramTarget } from "../../../extensions/telegram/src/targets.js";
|
||||
import type { ChatType } from "../chat-type.js";
|
||||
import { normalizeChatChannelId } from "../registry.js";
|
||||
import { getChannelPlugin, normalizeChannelId } from "./registry.js";
|
||||
|
||||
export type ParsedChannelExplicitTarget = {
|
||||
@@ -11,10 +14,28 @@ function parseWithPlugin(
|
||||
rawChannel: string,
|
||||
rawTarget: string,
|
||||
): ParsedChannelExplicitTarget | null {
|
||||
const channel = normalizeChannelId(rawChannel);
|
||||
const channel = normalizeChatChannelId(rawChannel) ?? normalizeChannelId(rawChannel);
|
||||
if (!channel) {
|
||||
return null;
|
||||
}
|
||||
if (channel === "telegram") {
|
||||
const target = parseTelegramTarget(rawTarget);
|
||||
return {
|
||||
to: target.chatId,
|
||||
...(target.messageThreadId != null ? { threadId: target.messageThreadId } : {}),
|
||||
...(target.chatType === "unknown" ? {} : { chatType: target.chatType }),
|
||||
};
|
||||
}
|
||||
if (channel === "discord") {
|
||||
const target = parseDiscordTarget(rawTarget, { defaultKind: "channel" });
|
||||
if (!target) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
to: target.id,
|
||||
chatType: target.kind === "user" ? "direct" : "channel",
|
||||
};
|
||||
}
|
||||
return getChannelPlugin(channel)?.messaging?.parseExplicitTarget?.({ raw: rawTarget }) ?? null;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user