@kodelyth/twitch 2026.5.42 → 2026.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/klaw.plugin.json +219 -2
  2. package/package.json +19 -2
  3. package/api.ts +0 -21
  4. package/channel-plugin-api.ts +0 -1
  5. package/index.test.ts +0 -13
  6. package/index.ts +0 -16
  7. package/runtime-api.ts +0 -22
  8. package/setup-entry.ts +0 -9
  9. package/setup-plugin-api.ts +0 -3
  10. package/src/access-control.test.ts +0 -373
  11. package/src/access-control.ts +0 -195
  12. package/src/actions.test.ts +0 -75
  13. package/src/actions.ts +0 -175
  14. package/src/client-manager-registry.ts +0 -87
  15. package/src/config-schema.test.ts +0 -46
  16. package/src/config-schema.ts +0 -88
  17. package/src/config.test.ts +0 -233
  18. package/src/config.ts +0 -177
  19. package/src/monitor.ts +0 -311
  20. package/src/outbound.test.ts +0 -572
  21. package/src/outbound.ts +0 -242
  22. package/src/plugin.lifecycle.test.ts +0 -86
  23. package/src/plugin.live.test.ts +0 -120
  24. package/src/plugin.test.ts +0 -77
  25. package/src/plugin.ts +0 -220
  26. package/src/probe.test.ts +0 -196
  27. package/src/probe.ts +0 -130
  28. package/src/resolver.ts +0 -139
  29. package/src/runtime.ts +0 -9
  30. package/src/send.test.ts +0 -342
  31. package/src/send.ts +0 -191
  32. package/src/setup-surface.test.ts +0 -529
  33. package/src/setup-surface.ts +0 -526
  34. package/src/status.test.ts +0 -298
  35. package/src/status.ts +0 -179
  36. package/src/test-fixtures.ts +0 -30
  37. package/src/token.test.ts +0 -198
  38. package/src/token.ts +0 -93
  39. package/src/twitch-client.test.ts +0 -574
  40. package/src/twitch-client.ts +0 -276
  41. package/src/types.ts +0 -104
  42. package/src/utils/markdown.ts +0 -98
  43. package/src/utils/twitch.ts +0 -81
  44. package/test/setup.ts +0 -7
  45. package/tsconfig.json +0 -16
package/src/send.test.ts DELETED
@@ -1,342 +0,0 @@
1
- /**
2
- * Tests for send.ts module
3
- *
4
- * Tests cover:
5
- * - Message sending with valid configuration
6
- * - Account resolution and validation
7
- * - Channel normalization
8
- * - Markdown stripping
9
- * - Error handling for missing/invalid accounts
10
- * - Registry integration
11
- */
12
-
13
- import { describe, expect, it, vi } from "vitest";
14
- import { getClientManager } from "./client-manager-registry.js";
15
- import { resolveTwitchAccountContext } from "./config.js";
16
- import { sendMessageTwitchInternal } from "./send.js";
17
- import {
18
- BASE_TWITCH_TEST_ACCOUNT,
19
- installTwitchTestHooks,
20
- makeTwitchTestConfig,
21
- } from "./test-fixtures.js";
22
- import { stripMarkdownForTwitch } from "./utils/markdown.js";
23
-
24
- // Mock dependencies
25
- vi.mock("./config.js", () => ({
26
- DEFAULT_ACCOUNT_ID: "default",
27
- resolveTwitchAccountContext: vi.fn(),
28
- }));
29
-
30
- vi.mock("./utils/twitch.js", () => ({
31
- generateMessageId: vi.fn(() => "test-msg-id"),
32
- normalizeTwitchChannel: (channel: string) => channel.toLowerCase().replace(/^#/, ""),
33
- }));
34
-
35
- vi.mock("./utils/markdown.js", () => ({
36
- stripMarkdownForTwitch: vi.fn((text: string) => text.replace(/\*\*/g, "")),
37
- }));
38
-
39
- vi.mock("./client-manager-registry.js", () => ({
40
- getClientManager: vi.fn(),
41
- }));
42
-
43
- describe("send", () => {
44
- const mockLogger = {
45
- info: vi.fn(),
46
- warn: vi.fn(),
47
- error: vi.fn(),
48
- debug: vi.fn(),
49
- };
50
-
51
- const mockAccount = {
52
- ...BASE_TWITCH_TEST_ACCOUNT,
53
- accessToken: "test123",
54
- };
55
-
56
- const mockConfig = makeTwitchTestConfig(mockAccount);
57
- installTwitchTestHooks();
58
-
59
- describe("sendMessageTwitchInternal", () => {
60
- function setupAccountContext(params?: {
61
- account?: typeof mockAccount | null;
62
- configured?: boolean;
63
- availableAccountIds?: string[];
64
- }) {
65
- const account = params?.account === undefined ? mockAccount : params.account;
66
- vi.mocked(resolveTwitchAccountContext).mockImplementation((_cfg, accountId) => ({
67
- accountId: accountId?.trim() || "default",
68
- account,
69
- tokenResolution: { source: "config", token: account?.accessToken ?? "" },
70
- configured: account ? (params?.configured ?? true) : false,
71
- availableAccountIds: params?.availableAccountIds ?? ["default"],
72
- }));
73
- }
74
-
75
- async function mockSuccessfulSend(params: {
76
- messageId: string;
77
- stripMarkdown?: (text: string) => string;
78
- }) {
79
- setupAccountContext();
80
- vi.mocked(getClientManager).mockReturnValue({
81
- sendMessage: vi.fn().mockResolvedValue({
82
- ok: true,
83
- messageId: params.messageId,
84
- }),
85
- } as unknown as ReturnType<typeof getClientManager>);
86
- vi.mocked(stripMarkdownForTwitch).mockImplementation(
87
- params.stripMarkdown ?? ((text) => text),
88
- );
89
-
90
- return { stripMarkdownForTwitch };
91
- }
92
-
93
- it("should send a message successfully", async () => {
94
- await mockSuccessfulSend({ messageId: "twitch-msg-123" });
95
-
96
- const result = await sendMessageTwitchInternal(
97
- "#testchannel",
98
- "Hello Twitch!",
99
- mockConfig,
100
- "default",
101
- false,
102
- mockLogger as unknown as Console,
103
- );
104
-
105
- expect(result.ok).toBe(true);
106
- expect(result.messageId).toBe("twitch-msg-123");
107
- expect(typeof result.receipt.sentAt).toBe("number");
108
- expect({ ...result.receipt, sentAt: 0 }).toEqual({
109
- primaryPlatformMessageId: "twitch-msg-123",
110
- platformMessageIds: ["twitch-msg-123"],
111
- parts: [
112
- {
113
- platformMessageId: "twitch-msg-123",
114
- kind: "text",
115
- index: 0,
116
- raw: {
117
- channel: "twitch",
118
- conversationId: "testchannel",
119
- messageId: "twitch-msg-123",
120
- },
121
- },
122
- ],
123
- raw: [
124
- {
125
- channel: "twitch",
126
- conversationId: "testchannel",
127
- messageId: "twitch-msg-123",
128
- },
129
- ],
130
- sentAt: 0,
131
- });
132
- });
133
-
134
- it("should strip markdown when enabled", async () => {
135
- const { stripMarkdownForTwitch } = await mockSuccessfulSend({
136
- messageId: "twitch-msg-456",
137
- stripMarkdown: (text) => text.replace(/\*\*/g, ""),
138
- });
139
-
140
- await sendMessageTwitchInternal(
141
- "#testchannel",
142
- "**Bold** text",
143
- mockConfig,
144
- "default",
145
- true,
146
- mockLogger as unknown as Console,
147
- );
148
-
149
- expect(stripMarkdownForTwitch).toHaveBeenCalledWith("**Bold** text");
150
- });
151
-
152
- it("should return error when account not found", async () => {
153
- setupAccountContext({ account: null });
154
-
155
- const result = await sendMessageTwitchInternal(
156
- "#testchannel",
157
- "Hello!",
158
- mockConfig,
159
- "nonexistent",
160
- false,
161
- mockLogger as unknown as Console,
162
- );
163
-
164
- expect(result.ok).toBe(false);
165
- expect(result.error).toContain("Account not found: nonexistent");
166
- });
167
-
168
- it("should return error when account not configured", async () => {
169
- setupAccountContext({ configured: false });
170
-
171
- const result = await sendMessageTwitchInternal(
172
- "#testchannel",
173
- "Hello!",
174
- mockConfig,
175
- "default",
176
- false,
177
- mockLogger as unknown as Console,
178
- );
179
-
180
- expect(result.ok).toBe(false);
181
- expect(result.error).toContain("not properly configured");
182
- });
183
-
184
- it("should return error when no channel specified", async () => {
185
- // Set channel to undefined to trigger the error (bypassing type check)
186
- const accountWithoutChannel = {
187
- ...mockAccount,
188
- channel: undefined as unknown as string,
189
- };
190
- setupAccountContext({ account: accountWithoutChannel });
191
-
192
- const result = await sendMessageTwitchInternal(
193
- "",
194
- "Hello!",
195
- mockConfig,
196
- "default",
197
- false,
198
- mockLogger as unknown as Console,
199
- );
200
-
201
- expect(result.ok).toBe(false);
202
- expect(result.error).toContain("No channel specified");
203
- });
204
-
205
- it("should skip sending empty message after markdown stripping", async () => {
206
- setupAccountContext();
207
- vi.mocked(stripMarkdownForTwitch).mockReturnValue("");
208
-
209
- const result = await sendMessageTwitchInternal(
210
- "#testchannel",
211
- "**Only markdown**",
212
- mockConfig,
213
- "default",
214
- true,
215
- mockLogger as unknown as Console,
216
- );
217
-
218
- expect(result.ok).toBe(true);
219
- expect(result.messageId).toBe("skipped");
220
- expect(result.receipt.platformMessageIds).toStrictEqual([]);
221
- expect(result.receipt.parts).toStrictEqual([]);
222
- });
223
-
224
- it("should return error when client manager not found", async () => {
225
- setupAccountContext();
226
- vi.mocked(getClientManager).mockReturnValue(undefined);
227
-
228
- const result = await sendMessageTwitchInternal(
229
- "#testchannel",
230
- "Hello!",
231
- mockConfig,
232
- "default",
233
- false,
234
- mockLogger as unknown as Console,
235
- );
236
-
237
- expect(result.ok).toBe(false);
238
- expect(result.error).toContain("Client manager not found");
239
- });
240
-
241
- it("should handle send errors gracefully", async () => {
242
- setupAccountContext();
243
- vi.mocked(getClientManager).mockReturnValue({
244
- sendMessage: vi.fn().mockRejectedValue(new Error("Connection lost")),
245
- } as unknown as ReturnType<typeof getClientManager>);
246
-
247
- const result = await sendMessageTwitchInternal(
248
- "#testchannel",
249
- "Hello!",
250
- mockConfig,
251
- "default",
252
- false,
253
- mockLogger as unknown as Console,
254
- );
255
-
256
- expect(result.ok).toBe(false);
257
- expect(result.error).toBe("Connection lost");
258
- expect(mockLogger.error).toHaveBeenCalled();
259
- });
260
-
261
- it("should use account channel when channel parameter is empty", async () => {
262
- setupAccountContext();
263
- const mockSend = vi.fn().mockResolvedValue({
264
- ok: true,
265
- messageId: "twitch-msg-789",
266
- });
267
- vi.mocked(getClientManager).mockReturnValue({
268
- sendMessage: mockSend,
269
- } as unknown as ReturnType<typeof getClientManager>);
270
-
271
- await sendMessageTwitchInternal(
272
- "",
273
- "Hello!",
274
- mockConfig,
275
- "default",
276
- false,
277
- mockLogger as unknown as Console,
278
- );
279
-
280
- expect(mockSend).toHaveBeenCalledWith(
281
- mockAccount,
282
- "testchannel", // normalized account channel
283
- "Hello!",
284
- mockConfig,
285
- "default",
286
- );
287
- });
288
-
289
- it("uses the configured default account when accountId is omitted", async () => {
290
- const secondaryAccount = {
291
- ...mockAccount,
292
- username: "secondary-user",
293
- channel: "secondary-channel",
294
- };
295
- vi.mocked(resolveTwitchAccountContext).mockImplementation((_cfg, accountId) => ({
296
- accountId: accountId?.trim() || "secondary",
297
- account: secondaryAccount,
298
- tokenResolution: { source: "config", token: secondaryAccount.accessToken ?? "" },
299
- configured: true,
300
- availableAccountIds: ["default", "secondary"],
301
- }));
302
- const mockSend = vi.fn().mockResolvedValue({
303
- ok: true,
304
- messageId: "twitch-msg-secondary",
305
- });
306
- vi.mocked(getClientManager).mockReturnValue({
307
- sendMessage: mockSend,
308
- } as unknown as ReturnType<typeof getClientManager>);
309
-
310
- const result = await sendMessageTwitchInternal(
311
- "",
312
- "Hello!",
313
- {
314
- channels: {
315
- twitch: {
316
- defaultAccount: "secondary",
317
- },
318
- },
319
- } as never,
320
- undefined,
321
- false,
322
- mockLogger as unknown as Console,
323
- );
324
-
325
- expect(result.ok).toBe(true);
326
- expect(getClientManager).toHaveBeenCalledWith("secondary");
327
- expect(mockSend).toHaveBeenCalledWith(
328
- secondaryAccount,
329
- "secondary-channel",
330
- "Hello!",
331
- {
332
- channels: {
333
- twitch: {
334
- defaultAccount: "secondary",
335
- },
336
- },
337
- },
338
- "secondary",
339
- );
340
- });
341
- });
342
- });
package/src/send.ts DELETED
@@ -1,191 +0,0 @@
1
- /**
2
- * Twitch message sending functions with dependency injection support.
3
- *
4
- * These functions are the primary interface for sending messages to Twitch.
5
- * They support dependency injection via the `deps` parameter for testability.
6
- */
7
-
8
- import {
9
- createMessageReceiptFromOutboundResults,
10
- type MessageReceipt,
11
- } from "klaw/plugin-sdk/channel-message";
12
- import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
13
- import { formatErrorMessage } from "klaw/plugin-sdk/error-runtime";
14
- import { getClientManager as getRegistryClientManager } from "./client-manager-registry.js";
15
- import { resolveTwitchAccountContext } from "./config.js";
16
- import { stripMarkdownForTwitch } from "./utils/markdown.js";
17
- import { generateMessageId, normalizeTwitchChannel } from "./utils/twitch.js";
18
-
19
- /**
20
- * Result from sending a message to Twitch.
21
- */
22
- export interface SendMessageResult {
23
- /** Whether the send was successful */
24
- ok: boolean;
25
- /** The message ID (generated for tracking) */
26
- messageId: string;
27
- /** Receipt for visible sends; empty when no Twitch message was sent */
28
- receipt: MessageReceipt;
29
- /** Error message if the send failed */
30
- error?: string;
31
- }
32
-
33
- function createTwitchSendReceipt(params: {
34
- messageId: string;
35
- channel?: string | null;
36
- visible?: boolean;
37
- }): MessageReceipt {
38
- const messageId = params.messageId.trim();
39
- const conversationId = params.channel?.trim();
40
- const hasVisibleMessage = params.visible === true && messageId && messageId !== "skipped";
41
- return createMessageReceiptFromOutboundResults({
42
- results: hasVisibleMessage
43
- ? [
44
- {
45
- channel: "twitch",
46
- messageId,
47
- ...(conversationId ? { conversationId } : {}),
48
- },
49
- ]
50
- : [],
51
- kind: "text",
52
- });
53
- }
54
-
55
- /**
56
- * Internal send function used by the outbound adapter.
57
- *
58
- * This function has access to the full Klaw config and handles
59
- * account resolution, markdown stripping, and actual message sending.
60
- *
61
- * @param channel - The channel name
62
- * @param text - The message text
63
- * @param cfg - Full Klaw configuration
64
- * @param accountId - Account ID to use
65
- * @param stripMarkdown - Whether to strip markdown (default: true)
66
- * @param logger - Logger instance
67
- * @returns Result with message ID and status
68
- *
69
- * @example
70
- * const result = await sendMessageTwitchInternal(
71
- * "#mychannel",
72
- * "Hello Twitch!",
73
- * klawConfig,
74
- * "default",
75
- * true,
76
- * console,
77
- * );
78
- */
79
- export async function sendMessageTwitchInternal(
80
- channel: string,
81
- text: string,
82
- cfg: KlawConfig,
83
- accountId?: string,
84
- stripMarkdown: boolean = true,
85
- logger: Console = console,
86
- ): Promise<SendMessageResult> {
87
- const {
88
- account,
89
- configured,
90
- availableAccountIds,
91
- accountId: resolvedAccountId,
92
- } = resolveTwitchAccountContext(cfg, accountId);
93
- if (!account) {
94
- return {
95
- ok: false,
96
- messageId: generateMessageId(),
97
- receipt: createTwitchSendReceipt({ messageId: "", channel, visible: false }),
98
- error: `Account not found: ${accountId ?? "(default)"}. Available accounts: ${availableAccountIds.join(", ") || "none"}`,
99
- };
100
- }
101
-
102
- if (!configured) {
103
- return {
104
- ok: false,
105
- messageId: generateMessageId(),
106
- receipt: createTwitchSendReceipt({ messageId: "", channel, visible: false }),
107
- error:
108
- `Account ${resolvedAccountId} is not properly configured. ` +
109
- "Required: username, clientId, and token (config or env for default account).",
110
- };
111
- }
112
-
113
- const normalizedChannel = channel || account.channel;
114
- if (!normalizedChannel) {
115
- return {
116
- ok: false,
117
- messageId: generateMessageId(),
118
- receipt: createTwitchSendReceipt({
119
- messageId: "",
120
- channel: normalizedChannel,
121
- visible: false,
122
- }),
123
- error: "No channel specified and no default channel in account config",
124
- };
125
- }
126
- const deliveryChannel = normalizeTwitchChannel(normalizedChannel);
127
-
128
- const cleanedText = stripMarkdown ? stripMarkdownForTwitch(text) : text;
129
- if (!cleanedText) {
130
- return {
131
- ok: true,
132
- messageId: "skipped",
133
- receipt: createTwitchSendReceipt({
134
- messageId: "skipped",
135
- channel: deliveryChannel,
136
- visible: false,
137
- }),
138
- };
139
- }
140
-
141
- const clientManager = getRegistryClientManager(resolvedAccountId);
142
- if (!clientManager) {
143
- return {
144
- ok: false,
145
- messageId: generateMessageId(),
146
- receipt: createTwitchSendReceipt({
147
- messageId: "",
148
- channel: deliveryChannel,
149
- visible: false,
150
- }),
151
- error: `Client manager not found for account: ${resolvedAccountId}. Please start the Twitch gateway first.`,
152
- };
153
- }
154
-
155
- try {
156
- const result = await clientManager.sendMessage(
157
- account,
158
- deliveryChannel,
159
- cleanedText,
160
- cfg,
161
- resolvedAccountId,
162
- );
163
-
164
- if (!result.ok) {
165
- const messageId = result.messageId ?? generateMessageId();
166
- return {
167
- ok: false,
168
- messageId,
169
- receipt: createTwitchSendReceipt({ messageId, channel: deliveryChannel, visible: false }),
170
- error: result.error ?? "Send failed",
171
- };
172
- }
173
-
174
- const messageId = result.messageId ?? generateMessageId();
175
- return {
176
- ok: true,
177
- messageId,
178
- receipt: createTwitchSendReceipt({ messageId, channel: deliveryChannel, visible: true }),
179
- };
180
- } catch (error) {
181
- const errorMsg = formatErrorMessage(error);
182
- const messageId = generateMessageId();
183
- logger.error(`Failed to send message: ${errorMsg}`);
184
- return {
185
- ok: false,
186
- messageId,
187
- receipt: createTwitchSendReceipt({ messageId, channel: deliveryChannel, visible: false }),
188
- error: errorMsg,
189
- };
190
- }
191
- }