@kodelyth/line 2026.5.39 → 2026.5.42

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 (120) hide show
  1. package/api.ts +11 -0
  2. package/channel-plugin-api.ts +1 -0
  3. package/contract-api.ts +5 -0
  4. package/dist/accounts-CD4A1FE7.js +105 -0
  5. package/dist/api.js +11 -0
  6. package/dist/basic-cards-BISytiSa.js +307 -0
  7. package/dist/card-command-dQBX3fVN.js +240 -0
  8. package/dist/channel-DV5h44-j.js +649 -0
  9. package/dist/channel-plugin-api.js +2 -0
  10. package/dist/channel.runtime-Cc-v3szZ.js +4 -0
  11. package/dist/contract-api.js +2 -0
  12. package/dist/index.js +45 -0
  13. package/dist/markdown-to-line-CC3BU6CC.js +810 -0
  14. package/dist/monitor-Ci8Hg8ay.js +1485 -0
  15. package/dist/monitor.runtime-t6-QvlDB.js +2 -0
  16. package/dist/outbound.runtime-D1CxEvcL.js +2 -0
  17. package/dist/probe-BPSs_A_8.js +30 -0
  18. package/dist/probe.runtime-7u2o9QN5.js +2 -0
  19. package/dist/reply-payload-transform-CDuBzoT4.js +855 -0
  20. package/dist/runtime-api.js +291 -0
  21. package/dist/schedule-cards-D-yZMHDE.js +359 -0
  22. package/dist/secret-contract-api.js +5 -0
  23. package/dist/setup-api.js +2 -0
  24. package/dist/setup-entry.js +11 -0
  25. package/dist/setup-surface-CHfQ6Z4i.js +282 -0
  26. package/index.ts +53 -0
  27. package/klaw.plugin.json +2 -329
  28. package/package.json +4 -4
  29. package/runtime-api.ts +179 -0
  30. package/secret-contract-api.ts +4 -0
  31. package/setup-api.ts +2 -0
  32. package/setup-entry.ts +9 -0
  33. package/src/account-helpers.ts +16 -0
  34. package/src/accounts.test.ts +288 -0
  35. package/src/accounts.ts +187 -0
  36. package/src/actions.ts +61 -0
  37. package/src/auto-reply-delivery.test.ts +253 -0
  38. package/src/auto-reply-delivery.ts +200 -0
  39. package/src/bindings.ts +65 -0
  40. package/src/bot-access.ts +30 -0
  41. package/src/bot-handlers.test.ts +1094 -0
  42. package/src/bot-handlers.ts +620 -0
  43. package/src/bot-message-context.test.ts +420 -0
  44. package/src/bot-message-context.ts +586 -0
  45. package/src/bot.ts +66 -0
  46. package/src/card-command.ts +347 -0
  47. package/src/channel-access-token.ts +14 -0
  48. package/src/channel-api.ts +17 -0
  49. package/src/channel-setup-status.contract.test.ts +70 -0
  50. package/src/channel-shared.ts +48 -0
  51. package/src/channel.logout.test.ts +145 -0
  52. package/src/channel.runtime.ts +3 -0
  53. package/src/channel.sendPayload.test.ts +659 -0
  54. package/src/channel.setup.ts +11 -0
  55. package/src/channel.status.test.ts +63 -0
  56. package/src/channel.ts +155 -0
  57. package/src/config-adapter.ts +29 -0
  58. package/src/config-schema.test.ts +53 -0
  59. package/src/config-schema.ts +81 -0
  60. package/src/download.test.ts +164 -0
  61. package/src/download.ts +34 -0
  62. package/src/flex-templates/basic-cards.ts +395 -0
  63. package/src/flex-templates/common.ts +20 -0
  64. package/src/flex-templates/media-control-cards.ts +555 -0
  65. package/src/flex-templates/message.ts +13 -0
  66. package/src/flex-templates/schedule-cards.ts +467 -0
  67. package/src/flex-templates/types.ts +22 -0
  68. package/src/flex-templates.ts +32 -0
  69. package/src/gateway.ts +129 -0
  70. package/src/group-keys.test.ts +123 -0
  71. package/src/group-keys.ts +65 -0
  72. package/src/group-policy.ts +22 -0
  73. package/src/markdown-to-line.test.ts +348 -0
  74. package/src/markdown-to-line.ts +416 -0
  75. package/src/message-cards.test.ts +204 -0
  76. package/src/monitor-durable.test.ts +57 -0
  77. package/src/monitor-durable.ts +37 -0
  78. package/src/monitor.lifecycle.test.ts +499 -0
  79. package/src/monitor.runtime.ts +1 -0
  80. package/src/monitor.ts +507 -0
  81. package/src/outbound-media.test.ts +194 -0
  82. package/src/outbound-media.ts +120 -0
  83. package/src/outbound.runtime.ts +12 -0
  84. package/src/outbound.ts +427 -0
  85. package/src/probe.contract.test.ts +9 -0
  86. package/src/probe.runtime.ts +1 -0
  87. package/src/probe.ts +34 -0
  88. package/src/quick-reply-fallback.ts +10 -0
  89. package/src/reply-chunks.test.ts +180 -0
  90. package/src/reply-chunks.ts +110 -0
  91. package/src/reply-payload-transform.test.ts +392 -0
  92. package/src/reply-payload-transform.ts +317 -0
  93. package/src/rich-menu.test.ts +315 -0
  94. package/src/rich-menu.ts +326 -0
  95. package/src/runtime.ts +32 -0
  96. package/src/send-receipt.ts +32 -0
  97. package/src/send.test.ts +453 -0
  98. package/src/send.ts +531 -0
  99. package/src/setup-core.ts +149 -0
  100. package/src/setup-runtime-api.ts +9 -0
  101. package/src/setup-surface.test.ts +481 -0
  102. package/src/setup-surface.ts +229 -0
  103. package/src/signature.test.ts +34 -0
  104. package/src/signature.ts +24 -0
  105. package/src/status.ts +37 -0
  106. package/src/template-messages.ts +333 -0
  107. package/src/types.ts +130 -0
  108. package/src/webhook-node.test.ts +598 -0
  109. package/src/webhook-node.ts +155 -0
  110. package/src/webhook-utils.ts +10 -0
  111. package/src/webhook.ts +135 -0
  112. package/tsconfig.json +16 -0
  113. package/api.js +0 -7
  114. package/channel-plugin-api.js +0 -7
  115. package/contract-api.js +0 -7
  116. package/index.js +0 -7
  117. package/runtime-api.js +0 -7
  118. package/secret-contract-api.js +0 -7
  119. package/setup-api.js +0 -7
  120. package/setup-entry.js +0 -7
@@ -0,0 +1,347 @@
1
+ import type { KlawPluginApi } from "klaw/plugin-sdk/core";
2
+ import type { ReplyPayload } from "klaw/plugin-sdk/reply-runtime";
3
+ import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
4
+ import {
5
+ createActionCard,
6
+ createImageCard,
7
+ createInfoCard,
8
+ createListCard,
9
+ createReceiptCard,
10
+ type CardAction,
11
+ type ListItem,
12
+ } from "./flex-templates.js";
13
+ import type { LineChannelData } from "./types.js";
14
+
15
+ const CARD_USAGE = `Usage: /card <type> "title" "body" [options]
16
+
17
+ Types:
18
+ info "Title" "Body" ["Footer"]
19
+ image "Title" "Caption" --url <image-url>
20
+ action "Title" "Body" --actions "Btn1|url1,Btn2|text2"
21
+ list "Title" "Item1|Desc1,Item2|Desc2"
22
+ receipt "Title" "Item1:$10,Item2:$20" --total "$30"
23
+ confirm "Question?" --yes "Yes|data" --no "No|data"
24
+ buttons "Title" "Text" --actions "Btn1|url1,Btn2|data2"
25
+
26
+ Examples:
27
+ /card info "Welcome" "Thanks for joining!"
28
+ /card image "Product" "Check it out" --url https://example.com/img.jpg
29
+ /card action "Menu" "Choose an option" --actions "Order|/order,Help|/help"`;
30
+
31
+ function buildLineReply(lineData: LineChannelData): ReplyPayload {
32
+ return {
33
+ channelData: {
34
+ line: lineData,
35
+ },
36
+ };
37
+ }
38
+
39
+ /**
40
+ * Parse action string format: "Label|data,Label2|data2"
41
+ * Data can be a URL (uri action) or plain text (message action) or key=value (postback)
42
+ */
43
+ function parseActions(actionsStr: string | undefined): CardAction[] {
44
+ if (!actionsStr) {
45
+ return [];
46
+ }
47
+
48
+ const results: CardAction[] = [];
49
+
50
+ for (const part of actionsStr.split(",")) {
51
+ const [label, data] = part
52
+ .trim()
53
+ .split("|")
54
+ .map((s) => s.trim());
55
+ if (!label) {
56
+ continue;
57
+ }
58
+
59
+ const actionData = data || label;
60
+
61
+ if (actionData.startsWith("http://") || actionData.startsWith("https://")) {
62
+ results.push({
63
+ label,
64
+ action: { type: "uri", label: label.slice(0, 20), uri: actionData },
65
+ });
66
+ } else if (actionData.includes("=")) {
67
+ results.push({
68
+ label,
69
+ action: {
70
+ type: "postback",
71
+ label: label.slice(0, 20),
72
+ data: actionData.slice(0, 300),
73
+ displayText: label,
74
+ },
75
+ });
76
+ } else {
77
+ results.push({
78
+ label,
79
+ action: { type: "message", label: label.slice(0, 20), text: actionData },
80
+ });
81
+ }
82
+ }
83
+
84
+ return results;
85
+ }
86
+
87
+ /**
88
+ * Parse list items format: "Item1|Subtitle1,Item2|Subtitle2"
89
+ */
90
+ function parseListItems(itemsStr: string): ListItem[] {
91
+ return itemsStr
92
+ .split(",")
93
+ .map((part) => {
94
+ const [title, subtitle] = part
95
+ .trim()
96
+ .split("|")
97
+ .map((s) => s.trim());
98
+ return { title: title || "", subtitle };
99
+ })
100
+ .filter((item) => item.title);
101
+ }
102
+
103
+ /**
104
+ * Parse receipt items format: "Item1:$10,Item2:$20"
105
+ */
106
+ function parseReceiptItems(itemsStr: string): Array<{ name: string; value: string }> {
107
+ return itemsStr
108
+ .split(",")
109
+ .map((part) => {
110
+ const colonIndex = part.lastIndexOf(":");
111
+ if (colonIndex === -1) {
112
+ return { name: part.trim(), value: "" };
113
+ }
114
+ return {
115
+ name: part.slice(0, colonIndex).trim(),
116
+ value: part.slice(colonIndex + 1).trim(),
117
+ };
118
+ })
119
+ .filter((item) => item.name);
120
+ }
121
+
122
+ /**
123
+ * Parse quoted arguments from command string
124
+ * Supports: /card type "arg1" "arg2" "arg3" --flag value
125
+ */
126
+ function parseCardArgs(argsStr: string): {
127
+ type: string;
128
+ args: string[];
129
+ flags: Record<string, string>;
130
+ } {
131
+ const result: { type: string; args: string[]; flags: Record<string, string> } = {
132
+ type: "",
133
+ args: [],
134
+ flags: {},
135
+ };
136
+
137
+ // Extract type (first word)
138
+ const typeMatch = argsStr.match(/^(\w+)/);
139
+ if (typeMatch) {
140
+ result.type = normalizeLowercaseStringOrEmpty(typeMatch[1]);
141
+ argsStr = argsStr.slice(typeMatch[0].length).trim();
142
+ }
143
+
144
+ // Extract quoted arguments
145
+ const quotedRegex = /"([^"]*?)"/g;
146
+ let match;
147
+ while ((match = quotedRegex.exec(argsStr)) !== null) {
148
+ result.args.push(match[1]);
149
+ }
150
+
151
+ // Extract flags (--key value or --key "value")
152
+ const flagRegex = /--(\w+)\s+(?:"([^"]*?)"|(\S+))/g;
153
+ while ((match = flagRegex.exec(argsStr)) !== null) {
154
+ result.flags[match[1]] = match[2] ?? match[3];
155
+ }
156
+
157
+ return result;
158
+ }
159
+
160
+ export function registerLineCardCommand(api: KlawPluginApi): void {
161
+ api.registerCommand({
162
+ name: "card",
163
+ description: "Send a rich card message (LINE).",
164
+ acceptsArgs: true,
165
+ requireAuth: false,
166
+ handler: async (ctx) => {
167
+ const argsStr = ctx.args?.trim() ?? "";
168
+ if (!argsStr) {
169
+ return { text: CARD_USAGE };
170
+ }
171
+
172
+ const parsed = parseCardArgs(argsStr);
173
+ const { type, args, flags } = parsed;
174
+
175
+ if (!type) {
176
+ return { text: CARD_USAGE };
177
+ }
178
+
179
+ // Only LINE supports rich cards; fallback to text elsewhere.
180
+ if (ctx.channel !== "line") {
181
+ const fallbackText = args.join(" - ");
182
+ return { text: `[${type} card] ${fallbackText}`.trim() };
183
+ }
184
+
185
+ try {
186
+ switch (type) {
187
+ case "info": {
188
+ const [title = "Info", body = "", footer] = args;
189
+ const bubble = createInfoCard(title, body, footer);
190
+ return buildLineReply({
191
+ flexMessage: {
192
+ altText: `${title}: ${body}`.slice(0, 400),
193
+ contents: bubble,
194
+ },
195
+ });
196
+ }
197
+
198
+ case "image": {
199
+ const [title = "Image", caption = ""] = args;
200
+ const imageUrl = flags.url || flags.image;
201
+ if (!imageUrl) {
202
+ return { text: "Error: Image card requires --url <image-url>" };
203
+ }
204
+ const bubble = createImageCard(imageUrl, title, caption);
205
+ return buildLineReply({
206
+ flexMessage: {
207
+ altText: `${title}: ${caption}`.slice(0, 400),
208
+ contents: bubble,
209
+ },
210
+ });
211
+ }
212
+
213
+ case "action": {
214
+ const [title = "Actions", body = ""] = args;
215
+ const actions = parseActions(flags.actions);
216
+ if (actions.length === 0) {
217
+ return { text: 'Error: Action card requires --actions "Label1|data1,Label2|data2"' };
218
+ }
219
+ const bubble = createActionCard(title, body, actions, {
220
+ imageUrl: flags.url || flags.image,
221
+ });
222
+ return buildLineReply({
223
+ flexMessage: {
224
+ altText: `${title}: ${body}`.slice(0, 400),
225
+ contents: bubble,
226
+ },
227
+ });
228
+ }
229
+
230
+ case "list": {
231
+ const [title = "List", itemsStr = ""] = args;
232
+ const items = parseListItems(itemsStr || flags.items || "");
233
+ if (items.length === 0) {
234
+ return {
235
+ text: 'Error: List card requires items. Usage: /card list "Title" "Item1|Desc1,Item2|Desc2"',
236
+ };
237
+ }
238
+ const bubble = createListCard(title, items);
239
+ return buildLineReply({
240
+ flexMessage: {
241
+ altText: `${title}: ${items.map((i) => i.title).join(", ")}`.slice(0, 400),
242
+ contents: bubble,
243
+ },
244
+ });
245
+ }
246
+
247
+ case "receipt": {
248
+ const [title = "Receipt", itemsStr = ""] = args;
249
+ const items = parseReceiptItems(itemsStr || flags.items || "");
250
+ const total = flags.total ? { label: "Total", value: flags.total } : undefined;
251
+ const footer = flags.footer;
252
+
253
+ if (items.length === 0) {
254
+ return {
255
+ text: 'Error: Receipt card requires items. Usage: /card receipt "Title" "Item1:$10,Item2:$20" --total "$30"',
256
+ };
257
+ }
258
+
259
+ const bubble = createReceiptCard({ title, items, total, footer });
260
+ return buildLineReply({
261
+ flexMessage: {
262
+ altText: `${title}: ${items.map((i) => `${i.name} ${i.value}`).join(", ")}`.slice(
263
+ 0,
264
+ 400,
265
+ ),
266
+ contents: bubble,
267
+ },
268
+ });
269
+ }
270
+
271
+ case "confirm": {
272
+ const [question = "Confirm?"] = args;
273
+ const yesStr = flags.yes || "Yes|yes";
274
+ const noStr = flags.no || "No|no";
275
+
276
+ const [yesLabel, yesData] = yesStr.split("|").map((s) => s.trim());
277
+ const [noLabel, noData] = noStr.split("|").map((s) => s.trim());
278
+
279
+ return buildLineReply({
280
+ templateMessage: {
281
+ type: "confirm",
282
+ text: question,
283
+ confirmLabel: yesLabel || "Yes",
284
+ confirmData: yesData || "yes",
285
+ cancelLabel: noLabel || "No",
286
+ cancelData: noData || "no",
287
+ altText: question,
288
+ },
289
+ });
290
+ }
291
+
292
+ case "buttons": {
293
+ const [title = "Menu", text = "Choose an option"] = args;
294
+ const actionsStr = flags.actions || "";
295
+ const actionParts = parseActions(actionsStr);
296
+
297
+ if (actionParts.length === 0) {
298
+ return { text: 'Error: Buttons card requires --actions "Label1|data1,Label2|data2"' };
299
+ }
300
+
301
+ const templateActions: Array<{
302
+ type: "message" | "uri" | "postback";
303
+ label: string;
304
+ data?: string;
305
+ uri?: string;
306
+ }> = actionParts.map((a) => {
307
+ const action = a.action;
308
+ const label = action.label ?? a.label;
309
+ if (action.type === "uri") {
310
+ return { type: "uri" as const, label, uri: (action as { uri: string }).uri };
311
+ }
312
+ if (action.type === "postback") {
313
+ return {
314
+ type: "postback" as const,
315
+ label,
316
+ data: (action as { data: string }).data,
317
+ };
318
+ }
319
+ return {
320
+ type: "message" as const,
321
+ label,
322
+ data: (action as { text: string }).text,
323
+ };
324
+ });
325
+
326
+ return buildLineReply({
327
+ templateMessage: {
328
+ type: "buttons",
329
+ title,
330
+ text,
331
+ thumbnailImageUrl: flags.url || flags.image,
332
+ actions: templateActions,
333
+ },
334
+ });
335
+ }
336
+
337
+ default:
338
+ return {
339
+ text: `Unknown card type: "${type}". Available types: info, image, action, list, receipt, confirm, buttons`,
340
+ };
341
+ }
342
+ } catch (err) {
343
+ return { text: `Error creating card: ${String(err)}` };
344
+ }
345
+ },
346
+ });
347
+ }
@@ -0,0 +1,14 @@
1
+ export function resolveLineChannelAccessToken(
2
+ explicit: string | undefined,
3
+ params: { accountId: string; channelAccessToken: string },
4
+ ): string {
5
+ if (explicit?.trim()) {
6
+ return explicit.trim();
7
+ }
8
+ if (!params.channelAccessToken) {
9
+ throw new Error(
10
+ `LINE channel access token missing for account "${params.accountId}" (set channels.line.channelAccessToken or LINE_CHANNEL_ACCESS_TOKEN).`,
11
+ );
12
+ }
13
+ return params.channelAccessToken.trim();
14
+ }
@@ -0,0 +1,17 @@
1
+ export { clearAccountEntryFields } from "klaw/plugin-sdk/core";
2
+ import { DEFAULT_ACCOUNT_ID } from "klaw/plugin-sdk/account-id";
3
+ import type { KlawConfig } from "klaw/plugin-sdk/account-resolution";
4
+ import type { ChannelPlugin } from "klaw/plugin-sdk/core";
5
+ import { listLineAccountIds, resolveDefaultLineAccountId, resolveLineAccount } from "./accounts.js";
6
+ import { resolveExactLineGroupConfigKey } from "./group-keys.js";
7
+ import type { LineConfig, ResolvedLineAccount } from "./types.js";
8
+
9
+ export {
10
+ DEFAULT_ACCOUNT_ID,
11
+ listLineAccountIds,
12
+ resolveDefaultLineAccountId,
13
+ resolveExactLineGroupConfigKey,
14
+ resolveLineAccount,
15
+ };
16
+
17
+ export type { ChannelPlugin, LineConfig, KlawConfig, ResolvedLineAccount };
@@ -0,0 +1,70 @@
1
+ import {
2
+ installChannelSetupContractSuite,
3
+ installChannelStatusContractSuite,
4
+ } from "klaw/plugin-sdk/channel-test-helpers";
5
+ import type { KlawConfig } from "klaw/plugin-sdk/config-contracts";
6
+ import { describe, expect } from "vitest";
7
+ import { linePlugin, lineSetupPlugin } from "../api.js";
8
+
9
+ describe("line setup contract", () => {
10
+ installChannelSetupContractSuite({
11
+ plugin: lineSetupPlugin,
12
+ cases: [
13
+ {
14
+ name: "default account stores token and secret",
15
+ cfg: {} as KlawConfig,
16
+ input: {
17
+ channelAccessToken: "line-token",
18
+ channelSecret: "line-secret",
19
+ } as never,
20
+ expectedAccountId: "default",
21
+ assertPatchedConfig: (cfg) => {
22
+ expect(cfg.channels?.line?.enabled).toBe(true);
23
+ expect(cfg.channels?.line?.channelAccessToken).toBe("line-token");
24
+ expect(cfg.channels?.line?.channelSecret).toBe("line-secret");
25
+ },
26
+ },
27
+ {
28
+ name: "non-default env setup is rejected",
29
+ cfg: {} as KlawConfig,
30
+ accountId: "ops",
31
+ input: {
32
+ useEnv: true,
33
+ },
34
+ expectedAccountId: "ops",
35
+ expectedValidation: "LINE_CHANNEL_ACCESS_TOKEN can only be used for the default account.",
36
+ },
37
+ ],
38
+ });
39
+ });
40
+
41
+ describe("line status contract", () => {
42
+ installChannelStatusContractSuite({
43
+ plugin: linePlugin,
44
+ cases: [
45
+ {
46
+ name: "configured account produces a webhook status snapshot",
47
+ cfg: {
48
+ channels: {
49
+ line: {
50
+ enabled: true,
51
+ channelAccessToken: "line-token",
52
+ channelSecret: "line-secret",
53
+ },
54
+ },
55
+ } as KlawConfig,
56
+ runtime: {
57
+ accountId: "default",
58
+ running: true,
59
+ },
60
+ probe: { ok: true },
61
+ assertSnapshot: (snapshot) => {
62
+ expect(snapshot.accountId).toBe("default");
63
+ expect(snapshot.enabled).toBe(true);
64
+ expect(snapshot.configured).toBe(true);
65
+ expect(snapshot.mode).toBe("webhook");
66
+ },
67
+ },
68
+ ],
69
+ });
70
+ });
@@ -0,0 +1,48 @@
1
+ import { describeWebhookAccountSnapshot } from "klaw/plugin-sdk/account-helpers";
2
+ import { hasLineCredentials } from "./account-helpers.js";
3
+ import { type ChannelPlugin, type ResolvedLineAccount } from "./channel-api.js";
4
+ import { lineConfigAdapter } from "./config-adapter.js";
5
+ import { LineChannelConfigSchema } from "./config-schema.js";
6
+
7
+ const lineChannelMeta = {
8
+ id: "line",
9
+ label: "LINE",
10
+ selectionLabel: "LINE (Messaging API)",
11
+ detailLabel: "LINE Bot",
12
+ docsPath: "/channels/line",
13
+ docsLabel: "line",
14
+ blurb: "LINE Messaging API bot for Japan/Taiwan/Thailand markets.",
15
+ systemImage: "message.fill",
16
+ } as const;
17
+
18
+ export const lineChannelPluginCommon = {
19
+ meta: {
20
+ ...lineChannelMeta,
21
+ quickstartAllowFrom: true,
22
+ },
23
+ capabilities: {
24
+ chatTypes: ["direct", "group"],
25
+ reactions: false,
26
+ threads: false,
27
+ media: true,
28
+ nativeCommands: false,
29
+ blockStreaming: true,
30
+ },
31
+ reload: { configPrefixes: ["channels.line"] },
32
+ configSchema: LineChannelConfigSchema,
33
+ config: {
34
+ ...lineConfigAdapter,
35
+ isConfigured: (account: ResolvedLineAccount) => hasLineCredentials(account),
36
+ describeAccount: (account: ResolvedLineAccount) =>
37
+ describeWebhookAccountSnapshot({
38
+ account,
39
+ configured: hasLineCredentials(account),
40
+ extra: {
41
+ tokenSource: account.tokenSource ?? undefined,
42
+ },
43
+ }),
44
+ },
45
+ } satisfies Pick<
46
+ ChannelPlugin<ResolvedLineAccount>,
47
+ "meta" | "capabilities" | "reload" | "configSchema" | "config"
48
+ >;
@@ -0,0 +1,145 @@
1
+ import { createRuntimeEnv } from "klaw/plugin-sdk/plugin-test-runtime";
2
+ import { beforeEach, describe, expect, it, vi } from "vitest";
3
+ import type { KlawConfig, PluginRuntime, ResolvedLineAccount } from "../api.js";
4
+ import { lineGatewayAdapter } from "./gateway.js";
5
+ import { setLineRuntime } from "./runtime.js";
6
+
7
+ const DEFAULT_ACCOUNT_ID = "default";
8
+
9
+ type LineRuntimeMocks = {
10
+ replaceConfigFile: ReturnType<typeof vi.fn>;
11
+ resolveLineAccount: ReturnType<typeof vi.fn>;
12
+ };
13
+
14
+ function createRuntime(): { runtime: PluginRuntime; mocks: LineRuntimeMocks } {
15
+ const replaceConfigFile = vi.fn(async () => {});
16
+ const resolveLineAccount = vi.fn(
17
+ ({ cfg, accountId }: { cfg: KlawConfig; accountId?: string }) => {
18
+ const lineConfig = (cfg.channels?.line ?? {}) as {
19
+ tokenFile?: string;
20
+ secretFile?: string;
21
+ channelAccessToken?: string;
22
+ channelSecret?: string;
23
+ accounts?: Record<string, Record<string, unknown>>;
24
+ };
25
+ const entry =
26
+ accountId && accountId !== DEFAULT_ACCOUNT_ID
27
+ ? (lineConfig.accounts?.[accountId] ?? {})
28
+ : lineConfig;
29
+ const hasToken =
30
+ Boolean((entry as any).channelAccessToken) || Boolean((entry as any).tokenFile);
31
+ const hasSecret = Boolean((entry as any).channelSecret) || Boolean((entry as any).secretFile);
32
+ return { tokenSource: hasToken && hasSecret ? "config" : "none" };
33
+ },
34
+ );
35
+
36
+ const runtime = {
37
+ config: { replaceConfigFile },
38
+ } as unknown as PluginRuntime;
39
+
40
+ return { runtime, mocks: { replaceConfigFile, resolveLineAccount } };
41
+ }
42
+
43
+ function resolveAccount(
44
+ resolveLineAccount: LineRuntimeMocks["resolveLineAccount"],
45
+ cfg: KlawConfig,
46
+ accountId: string,
47
+ ): ResolvedLineAccount {
48
+ const resolver = resolveLineAccount as unknown as (params: {
49
+ cfg: KlawConfig;
50
+ accountId?: string;
51
+ }) => ResolvedLineAccount;
52
+ return resolver({ cfg, accountId });
53
+ }
54
+
55
+ async function runLogoutScenario(params: { cfg: KlawConfig; accountId: string }): Promise<{
56
+ result: Awaited<ReturnType<NonNullable<typeof lineGatewayAdapter.logoutAccount>>>;
57
+ mocks: LineRuntimeMocks;
58
+ }> {
59
+ const { runtime, mocks } = createRuntime();
60
+ setLineRuntime(runtime);
61
+ const account = resolveAccount(mocks.resolveLineAccount, params.cfg, params.accountId);
62
+ const result = await lineGatewayAdapter.logoutAccount!({
63
+ accountId: params.accountId,
64
+ cfg: params.cfg,
65
+ account,
66
+ runtime: createRuntimeEnv(),
67
+ });
68
+ return { result, mocks };
69
+ }
70
+
71
+ describe("linePlugin gateway.logoutAccount", () => {
72
+ beforeEach(() => {
73
+ setLineRuntime(createRuntime().runtime);
74
+ });
75
+
76
+ it("clears tokenFile/secretFile on default account logout", async () => {
77
+ const cfg: KlawConfig = {
78
+ channels: {
79
+ line: {
80
+ tokenFile: "/tmp/token",
81
+ secretFile: "/tmp/secret",
82
+ },
83
+ },
84
+ };
85
+ const { result, mocks } = await runLogoutScenario({
86
+ cfg,
87
+ accountId: DEFAULT_ACCOUNT_ID,
88
+ });
89
+
90
+ expect(result.cleared).toBe(true);
91
+ expect(result.loggedOut).toBe(true);
92
+ expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
93
+ nextConfig: {},
94
+ afterWrite: { mode: "auto" },
95
+ });
96
+ });
97
+
98
+ it("clears tokenFile/secretFile on account logout", async () => {
99
+ const cfg: KlawConfig = {
100
+ channels: {
101
+ line: {
102
+ accounts: {
103
+ primary: {
104
+ tokenFile: "/tmp/token",
105
+ secretFile: "/tmp/secret",
106
+ },
107
+ },
108
+ },
109
+ },
110
+ };
111
+ const { result, mocks } = await runLogoutScenario({
112
+ cfg,
113
+ accountId: "primary",
114
+ });
115
+
116
+ expect(result.cleared).toBe(true);
117
+ expect(result.loggedOut).toBe(true);
118
+ expect(mocks.replaceConfigFile).toHaveBeenCalledWith({
119
+ nextConfig: {},
120
+ afterWrite: { mode: "auto" },
121
+ });
122
+ });
123
+
124
+ it("does not write config when account has no token/secret fields", async () => {
125
+ const cfg: KlawConfig = {
126
+ channels: {
127
+ line: {
128
+ accounts: {
129
+ primary: {
130
+ name: "Primary",
131
+ },
132
+ },
133
+ },
134
+ },
135
+ };
136
+ const { result, mocks } = await runLogoutScenario({
137
+ cfg,
138
+ accountId: "primary",
139
+ });
140
+
141
+ expect(result.cleared).toBe(false);
142
+ expect(result.loggedOut).toBe(true);
143
+ expect(mocks.replaceConfigFile).not.toHaveBeenCalled();
144
+ });
145
+ });
@@ -0,0 +1,3 @@
1
+ export { monitorLineProvider } from "./monitor.js";
2
+ export { probeLineBot } from "./probe.js";
3
+ export { pushMessageLine } from "./send.js";