diff --git a/src/channels/plugins/target-parsing.test.ts b/src/channels/plugins/target-parsing.test.ts index 017c422996e..7f486582396 100644 --- a/src/channels/plugins/target-parsing.test.ts +++ b/src/channels/plugins/target-parsing.test.ts @@ -1,12 +1,96 @@ import { beforeEach, describe, expect, it } from "vitest"; -import { setDefaultChannelPluginRegistryForTests } from "../../commands/channel-test-helpers.js"; import { setActivePluginRegistry } from "../../plugins/runtime.js"; import { createTestRegistry } from "../../test-utils/channel-plugins.js"; import { parseExplicitTargetForChannel } from "./target-parsing.js"; +function parseTelegramTargetForTest(raw: string): { + to: string; + threadId?: number; + chatType?: "direct" | "group"; +} { + const trimmed = raw + .trim() + .replace(/^telegram:/i, "") + .replace(/^tg:/i, ""); + const prefixedTopic = /^group:([^:]+):topic:(\d+)$/i.exec(trimmed); + if (prefixedTopic) { + return { + to: prefixedTopic[1], + threadId: Number.parseInt(prefixedTopic[2], 10), + chatType: "group", + }; + } + const topic = /^([^:]+):topic:(\d+)$/i.exec(trimmed); + if (topic) { + return { + to: topic[1], + threadId: Number.parseInt(topic[2], 10), + chatType: topic[1].startsWith("-") ? "group" : "direct", + }; + } + return { + to: trimmed, + chatType: trimmed.startsWith("-") ? "group" : undefined, + }; +} + +function setMinimalTargetParsingRegistry(): void { + setActivePluginRegistry( + createTestRegistry([ + { + pluginId: "telegram", + plugin: { + id: "telegram", + meta: { + id: "telegram", + label: "Telegram", + selectionLabel: "Telegram", + docsPath: "/channels/telegram", + blurb: "test stub", + }, + capabilities: { chatTypes: ["direct", "group"] }, + config: { + listAccountIds: () => [], + resolveAccount: () => ({}), + }, + messaging: { + parseExplicitTarget: ({ raw }: { raw: string }) => parseTelegramTargetForTest(raw), + }, + }, + source: "test", + }, + { + pluginId: "demo-target", + source: "test", + plugin: { + id: "demo-target", + meta: { + id: "demo-target", + label: "Demo Target", + selectionLabel: "Demo Target", + docsPath: "/channels/demo-target", + blurb: "test stub", + }, + capabilities: { chatTypes: ["direct"] }, + config: { + listAccountIds: () => [], + resolveAccount: () => ({}), + }, + messaging: { + parseExplicitTarget: ({ raw }: { raw: string }) => ({ + to: raw.trim().toUpperCase(), + chatType: "direct" as const, + }), + }, + }, + }, + ]), + ); +} + describe("parseExplicitTargetForChannel", () => { beforeEach(() => { - setDefaultChannelPluginRegistryForTests(); + setMinimalTargetParsingRegistry(); }); it("parses Telegram targets via the registered channel plugin contract", () => { @@ -22,36 +106,6 @@ describe("parseExplicitTargetForChannel", () => { }); it("parses registered non-bundled channel targets via the active plugin contract", () => { - setActivePluginRegistry( - createTestRegistry([ - { - pluginId: "demo-target", - source: "test", - plugin: { - id: "demo-target", - meta: { - id: "demo-target", - label: "Demo Target", - selectionLabel: "Demo Target", - docsPath: "/channels/demo-target", - blurb: "test stub", - }, - capabilities: { chatTypes: ["direct"] }, - config: { - listAccountIds: () => [], - resolveAccount: () => ({}), - }, - messaging: { - parseExplicitTarget: ({ raw }: { raw: string }) => ({ - to: raw.trim().toUpperCase(), - chatType: "direct" as const, - }), - }, - }, - }, - ]), - ); - expect(parseExplicitTargetForChannel("demo-target", "team-room")).toEqual({ to: "TEAM-ROOM", chatType: "direct",