|
| 1 | +import type { messagingApi, webhook } from "@line/bot-sdk"; |
| 2 | +import type { OpenClawConfig } from "openclaw/plugin-sdk/config-contracts"; |
| 3 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 4 | +import type { LineAccountConfig } from "./types.js"; |
| 5 | + |
| 6 | +type ReportChannelRoomJoin = |
| 7 | + typeof import("openclaw/plugin-sdk/channel-join-intro-runtime").reportChannelRoomJoin; |
| 8 | + |
| 9 | +const { reportJoin, getGroupSummary, createClient } = vi.hoisted(() => { |
| 10 | + const summary = vi.fn<messagingApi.MessagingApiClient["getGroupSummary"]>(); |
| 11 | + return { |
| 12 | + reportJoin: vi.fn<ReportChannelRoomJoin>(async () => ({ kind: "posted" })), |
| 13 | + getGroupSummary: summary, |
| 14 | + createClient: vi.fn(function () { |
| 15 | + return { getGroupSummary: summary }; |
| 16 | + }), |
| 17 | + }; |
| 18 | +}); |
| 19 | + |
| 20 | +vi.mock("openclaw/plugin-sdk/channel-join-intro-runtime", () => ({ |
| 21 | + reportChannelRoomJoin: reportJoin, |
| 22 | +})); |
| 23 | +vi.mock("@line/bot-sdk", async (importOriginal) => { |
| 24 | + const actual = await importOriginal<typeof import("@line/bot-sdk")>(); |
| 25 | + return { |
| 26 | + ...actual, |
| 27 | + messagingApi: { ...actual.messagingApi, MessagingApiClient: createClient }, |
| 28 | + }; |
| 29 | +}); |
| 30 | + |
| 31 | +let handleLineWebhookEvents: typeof import("./bot-handlers.js").handleLineWebhookEvents; |
| 32 | +let resolveLineAccount: typeof import("./accounts.js").resolveLineAccount; |
| 33 | + |
| 34 | +const groupId = `C${"a".repeat(32)}`; |
| 35 | +const roomId = `R${"b".repeat(32)}`; |
| 36 | +const userId = `U${"c".repeat(32)}`; |
| 37 | +const sources = [ |
| 38 | + { type: "group", groupId }, |
| 39 | + { type: "room", roomId }, |
| 40 | +] as const; |
| 41 | + |
| 42 | +function joinEvent(source: webhook.Source): webhook.JoinEvent { |
| 43 | + return { |
| 44 | + type: "join", |
| 45 | + source, |
| 46 | + replyToken: "join-reply", |
| 47 | + timestamp: 1_800_000_000_000, |
| 48 | + mode: "active", |
| 49 | + webhookEventId: "join-event", |
| 50 | + deliveryContext: { isRedelivery: false }, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +function createContext(config: LineAccountConfig = {}) { |
| 55 | + const cfg: OpenClawConfig = { |
| 56 | + agents: { list: [{ id: "main" }, { id: "room-agent" }] }, |
| 57 | + bindings: [{ agentId: "room-agent", match: { channel: "line", accountId: "work" } }], |
| 58 | + channels: { |
| 59 | + line: { |
| 60 | + accounts: { |
| 61 | + work: { |
| 62 | + channelAccessToken: "line-work-token", |
| 63 | + channelSecret: "line-work-secret", |
| 64 | + groupPolicy: "allowlist", |
| 65 | + groupAllowFrom: [userId], |
| 66 | + ...config, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }; |
| 72 | + return { |
| 73 | + cfg, |
| 74 | + account: resolveLineAccount({ cfg, accountId: "work" }), |
| 75 | + runtime: { log: vi.fn(), error: vi.fn(), exit: vi.fn() }, |
| 76 | + mediaMaxBytes: 1, |
| 77 | + processMessage: vi.fn(), |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +describe("LINE group join introductions", () => { |
| 82 | + beforeAll(async () => { |
| 83 | + ({ handleLineWebhookEvents } = await import("./bot-handlers.js")); |
| 84 | + ({ resolveLineAccount } = await import("./accounts.js")); |
| 85 | + }); |
| 86 | + |
| 87 | + beforeEach(() => { |
| 88 | + reportJoin.mockClear(); |
| 89 | + createClient.mockClear(); |
| 90 | + getGroupSummary.mockReset(); |
| 91 | + getGroupSummary.mockResolvedValue({ groupId, groupName: "Incident Response" }); |
| 92 | + }); |
| 93 | + |
| 94 | + afterAll(() => { |
| 95 | + vi.doUnmock("openclaw/plugin-sdk/channel-join-intro-runtime"); |
| 96 | + vi.doUnmock("@line/bot-sdk"); |
| 97 | + vi.resetModules(); |
| 98 | + }); |
| 99 | + |
| 100 | + it.each(sources)( |
| 101 | + "introduces the bot in its joined $type without human sender authorization", |
| 102 | + async (source) => { |
| 103 | + const context = createContext(); |
| 104 | + const conversationId = source.type === "group" ? source.groupId : source.roomId; |
| 105 | + |
| 106 | + await handleLineWebhookEvents([joinEvent(source)], context); |
| 107 | + |
| 108 | + expect(reportJoin).toHaveBeenCalledTimes(1); |
| 109 | + const params = reportJoin.mock.calls[0]?.[0]; |
| 110 | + if (!params) { |
| 111 | + throw new Error("Expected a LINE join introduction"); |
| 112 | + } |
| 113 | + expect(params).toMatchObject({ |
| 114 | + cfg: context.cfg, |
| 115 | + channel: "line", |
| 116 | + accountId: "work", |
| 117 | + conversationId, |
| 118 | + deliverTo: conversationId, |
| 119 | + roomAllowed: true, |
| 120 | + route: { |
| 121 | + agentId: "room-agent", |
| 122 | + sessionKey: `agent:room-agent:line:group:${conversationId.toLowerCase()}`, |
| 123 | + }, |
| 124 | + }); |
| 125 | + await expect(params.resolveRoomContext({ messageLimit: 100 })).resolves.toEqual( |
| 126 | + source.type === "group" |
| 127 | + ? { title: "Incident Response", historyUnavailable: true } |
| 128 | + : { historyUnavailable: true }, |
| 129 | + ); |
| 130 | + if (source.type === "group") { |
| 131 | + expect(getGroupSummary).toHaveBeenCalledWith(groupId); |
| 132 | + expect(createClient).toHaveBeenCalledWith({ channelAccessToken: "line-work-token" }); |
| 133 | + } else { |
| 134 | + expect(getGroupSummary).not.toHaveBeenCalled(); |
| 135 | + } |
| 136 | + expect(context.processMessage).not.toHaveBeenCalled(); |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + it("keeps an honest thin snapshot when the group summary request fails", async () => { |
| 141 | + getGroupSummary.mockRejectedValue(new Error("LINE unavailable")); |
| 142 | + await handleLineWebhookEvents([joinEvent(sources[0])], createContext()); |
| 143 | + |
| 144 | + const params = reportJoin.mock.calls[0]?.[0]; |
| 145 | + if (!params) { |
| 146 | + throw new Error("Expected a LINE join introduction"); |
| 147 | + } |
| 148 | + await expect(params.resolveRoomContext({ messageLimit: 100 })).resolves.toEqual({ |
| 149 | + historyUnavailable: true, |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + it.each([ |
| 154 | + { name: "a direct user chat", event: joinEvent({ type: "user", userId }) }, |
| 155 | + { |
| 156 | + name: "a human member join", |
| 157 | + event: { |
| 158 | + ...joinEvent(sources[0]), |
| 159 | + type: "memberJoined", |
| 160 | + joined: { members: [{ type: "user", userId }] }, |
| 161 | + } satisfies webhook.MemberJoinedEvent, |
| 162 | + }, |
| 163 | + ])("does not introduce for $name", async ({ event }) => { |
| 164 | + const context = createContext(); |
| 165 | + await handleLineWebhookEvents([event], context); |
| 166 | + |
| 167 | + expect(reportJoin).not.toHaveBeenCalled(); |
| 168 | + expect(getGroupSummary).not.toHaveBeenCalled(); |
| 169 | + expect(context.processMessage).not.toHaveBeenCalled(); |
| 170 | + }); |
| 171 | + |
| 172 | + it.each([ |
| 173 | + { name: "disabled group policy", config: { groupPolicy: "disabled" as const } }, |
| 174 | + { name: "a disabled group", config: { groups: { [groupId]: { enabled: false } } } }, |
| 175 | + { name: "disabled wildcard groups", config: { groups: { "*": { enabled: false } } } }, |
| 176 | + ])("reports denied admission for $name without reading metadata", async ({ config }) => { |
| 177 | + await handleLineWebhookEvents([joinEvent(sources[0])], createContext(config)); |
| 178 | + |
| 179 | + expect(reportJoin).toHaveBeenCalledWith( |
| 180 | + expect.objectContaining({ conversationId: groupId, roomAllowed: false }), |
| 181 | + ); |
| 182 | + expect(getGroupSummary).not.toHaveBeenCalled(); |
| 183 | + }); |
| 184 | +}); |
0 commit comments