Skip to content

Commit ab0b70d

Browse files
committed
fix(test): trim target parsing registry imports
1 parent 3130c15 commit ab0b70d

1 file changed

Lines changed: 86 additions & 32 deletions

File tree

‎src/channels/plugins/target-parsing.test.ts‎

Lines changed: 86 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,96 @@
11
import { beforeEach, describe, expect, it } from "vitest";
2-
import { setDefaultChannelPluginRegistryForTests } from "../../commands/channel-test-helpers.js";
32
import { setActivePluginRegistry } from "../../plugins/runtime.js";
43
import { createTestRegistry } from "../../test-utils/channel-plugins.js";
54
import { parseExplicitTargetForChannel } from "./target-parsing.js";
65

6+
function parseTelegramTargetForTest(raw: string): {
7+
to: string;
8+
threadId?: number;
9+
chatType?: "direct" | "group";
10+
} {
11+
const trimmed = raw
12+
.trim()
13+
.replace(/^telegram:/i, "")
14+
.replace(/^tg:/i, "");
15+
const prefixedTopic = /^group:([^:]+):topic:(\d+)$/i.exec(trimmed);
16+
if (prefixedTopic) {
17+
return {
18+
to: prefixedTopic[1],
19+
threadId: Number.parseInt(prefixedTopic[2], 10),
20+
chatType: "group",
21+
};
22+
}
23+
const topic = /^([^:]+):topic:(\d+)$/i.exec(trimmed);
24+
if (topic) {
25+
return {
26+
to: topic[1],
27+
threadId: Number.parseInt(topic[2], 10),
28+
chatType: topic[1].startsWith("-") ? "group" : "direct",
29+
};
30+
}
31+
return {
32+
to: trimmed,
33+
chatType: trimmed.startsWith("-") ? "group" : undefined,
34+
};
35+
}
36+
37+
function setMinimalTargetParsingRegistry(): void {
38+
setActivePluginRegistry(
39+
createTestRegistry([
40+
{
41+
pluginId: "telegram",
42+
plugin: {
43+
id: "telegram",
44+
meta: {
45+
id: "telegram",
46+
label: "Telegram",
47+
selectionLabel: "Telegram",
48+
docsPath: "/channels/telegram",
49+
blurb: "test stub",
50+
},
51+
capabilities: { chatTypes: ["direct", "group"] },
52+
config: {
53+
listAccountIds: () => [],
54+
resolveAccount: () => ({}),
55+
},
56+
messaging: {
57+
parseExplicitTarget: ({ raw }: { raw: string }) => parseTelegramTargetForTest(raw),
58+
},
59+
},
60+
source: "test",
61+
},
62+
{
63+
pluginId: "demo-target",
64+
source: "test",
65+
plugin: {
66+
id: "demo-target",
67+
meta: {
68+
id: "demo-target",
69+
label: "Demo Target",
70+
selectionLabel: "Demo Target",
71+
docsPath: "/channels/demo-target",
72+
blurb: "test stub",
73+
},
74+
capabilities: { chatTypes: ["direct"] },
75+
config: {
76+
listAccountIds: () => [],
77+
resolveAccount: () => ({}),
78+
},
79+
messaging: {
80+
parseExplicitTarget: ({ raw }: { raw: string }) => ({
81+
to: raw.trim().toUpperCase(),
82+
chatType: "direct" as const,
83+
}),
84+
},
85+
},
86+
},
87+
]),
88+
);
89+
}
90+
791
describe("parseExplicitTargetForChannel", () => {
892
beforeEach(() => {
9-
setDefaultChannelPluginRegistryForTests();
93+
setMinimalTargetParsingRegistry();
1094
});
1195

1296
it("parses Telegram targets via the registered channel plugin contract", () => {
@@ -22,36 +106,6 @@ describe("parseExplicitTargetForChannel", () => {
22106
});
23107

24108
it("parses registered non-bundled channel targets via the active plugin contract", () => {
25-
setActivePluginRegistry(
26-
createTestRegistry([
27-
{
28-
pluginId: "demo-target",
29-
source: "test",
30-
plugin: {
31-
id: "demo-target",
32-
meta: {
33-
id: "demo-target",
34-
label: "Demo Target",
35-
selectionLabel: "Demo Target",
36-
docsPath: "/channels/demo-target",
37-
blurb: "test stub",
38-
},
39-
capabilities: { chatTypes: ["direct"] },
40-
config: {
41-
listAccountIds: () => [],
42-
resolveAccount: () => ({}),
43-
},
44-
messaging: {
45-
parseExplicitTarget: ({ raw }: { raw: string }) => ({
46-
to: raw.trim().toUpperCase(),
47-
chatType: "direct" as const,
48-
}),
49-
},
50-
},
51-
},
52-
]),
53-
);
54-
55109
expect(parseExplicitTargetForChannel("demo-target", "team-room")).toEqual({
56110
to: "TEAM-ROOM",
57111
chatType: "direct",

0 commit comments

Comments
 (0)