fix(channels): parse bundled targets without plugin registry

This commit is contained in:
Ayaan Zaidi
2026-03-16 17:24:37 +05:30
parent 092afc850d
commit 55f6d2d1ad
2 changed files with 81 additions and 1 deletions

View 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",
});
});
});

View File

@@ -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;
}