@actagent/synology-chat 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.
@@ -0,0 +1,177 @@
1
+ // Synology Chat plugin module implements channel mocks behavior.
2
+ import type { IncomingMessage, ServerResponse } from "node:http";
3
+ import type { Mock } from "vitest";
4
+ import { vi } from "vitest";
5
+
6
+ type RegisteredRoute = {
7
+ path: string;
8
+ accountId: string;
9
+ handler: (req: IncomingMessage, res: ServerResponse) => Promise<void>;
10
+ };
11
+
12
+ export const registerPluginHttpRouteMock: Mock<(params: RegisteredRoute) => () => void> = vi.fn(
13
+ () => vi.fn(),
14
+ );
15
+
16
+ export const dispatchReplyWithBufferedBlockDispatcher: Mock<
17
+ () => Promise<{ counts: Record<string, number> }>
18
+ > = vi.fn().mockResolvedValue({ counts: {} });
19
+ export const finalizeInboundContextMock: Mock<
20
+ (ctx: Record<string, unknown>) => Record<string, unknown>
21
+ > = vi.fn((ctx) => ctx);
22
+ export const buildChannelInboundEventContextMock: Mock<
23
+ (params: {
24
+ channel: string;
25
+ accountId?: string;
26
+ timestamp?: number;
27
+ from: string;
28
+ sender: { id: string; name?: string };
29
+ conversation: { kind: string; label?: string };
30
+ route: {
31
+ accountId?: string;
32
+ routeSessionKey: string;
33
+ dispatchSessionKey?: string;
34
+ };
35
+ reply: { to: string; originatingTo: string };
36
+ message: {
37
+ rawBody: string;
38
+ bodyForAgent?: string;
39
+ commandBody?: string;
40
+ };
41
+ extra?: Record<string, unknown>;
42
+ }) => Record<string, unknown>
43
+ > = vi.fn((params) =>
44
+ finalizeInboundContextMock({
45
+ Body: params.message.rawBody,
46
+ BodyForAgent: params.message.bodyForAgent ?? params.message.rawBody,
47
+ RawBody: params.message.rawBody,
48
+ CommandBody: params.message.commandBody ?? params.message.rawBody,
49
+ From: params.from,
50
+ To: params.reply.to,
51
+ SessionKey: params.route.dispatchSessionKey ?? params.route.routeSessionKey,
52
+ AccountId: params.route.accountId ?? params.accountId,
53
+ OriginatingChannel: params.channel,
54
+ OriginatingTo: params.reply.originatingTo,
55
+ ChatType: params.conversation.kind,
56
+ SenderName: params.sender.name,
57
+ SenderId: params.sender.id,
58
+ Provider: params.channel,
59
+ Surface: params.channel,
60
+ ConversationLabel: params.conversation.label,
61
+ Timestamp: params.timestamp,
62
+ ...params.extra,
63
+ }),
64
+ );
65
+ export const resolveAgentRouteMock: Mock<
66
+ (params: { accountId?: string }) => { agentId: string; sessionKey: string; accountId: string }
67
+ > = vi.fn((params) => {
68
+ const accountId = params.accountId?.trim() || "default";
69
+ return {
70
+ agentId: `agent-${accountId}`,
71
+ sessionKey: `agent:agent-${accountId}:main`,
72
+ accountId,
73
+ };
74
+ });
75
+ let mockRuntimeConfig: unknown = {};
76
+
77
+ export function setSynologyRuntimeConfigForTest(cfg: unknown): void {
78
+ mockRuntimeConfig = cfg;
79
+ }
80
+
81
+ async function readRequestBodyWithLimitForTest(req: IncomingMessage): Promise<string> {
82
+ return await new Promise<string>((resolve, reject) => {
83
+ const chunks: Buffer[] = [];
84
+ req.on("data", (chunk) => {
85
+ chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk));
86
+ });
87
+ req.on("end", () => resolve(Buffer.concat(chunks).toString("utf8")));
88
+ req.on("error", reject);
89
+ });
90
+ }
91
+
92
+ vi.mock("actagent/plugin-sdk/setup", async () => {
93
+ const actual = await vi.importActual<object>("actagent/plugin-sdk/setup");
94
+ return {
95
+ ...actual,
96
+ DEFAULT_ACCOUNT_ID: "default",
97
+ };
98
+ });
99
+
100
+ vi.mock("actagent/plugin-sdk/channel-config-schema", async () => {
101
+ const actual = await vi.importActual<object>("actagent/plugin-sdk/channel-config-schema");
102
+ return {
103
+ ...actual,
104
+ buildChannelConfigSchema: vi.fn((schema: unknown) => ({ schema })),
105
+ };
106
+ });
107
+
108
+ vi.mock("actagent/plugin-sdk/webhook-ingress", async () => {
109
+ const actual = await vi.importActual<object>("actagent/plugin-sdk/webhook-ingress");
110
+ return {
111
+ ...actual,
112
+ registerPluginHttpRoute: registerPluginHttpRouteMock,
113
+ readRequestBodyWithLimit: vi.fn(readRequestBodyWithLimitForTest),
114
+ isRequestBodyLimitError: vi.fn(() => false),
115
+ requestBodyErrorToText: vi.fn(() => "Request body too large"),
116
+ createFixedWindowRateLimiter: vi.fn(() => ({
117
+ isRateLimited: vi.fn(() => false),
118
+ size: vi.fn(() => 0),
119
+ clear: vi.fn(),
120
+ })),
121
+ };
122
+ });
123
+
124
+ vi.mock("./client.js", () => ({
125
+ sendMessage: vi.fn().mockResolvedValue(true),
126
+ sendFileUrl: vi.fn().mockResolvedValue(true),
127
+ resolveLegacyWebhookNameToChatUserId: vi.fn().mockResolvedValue(undefined),
128
+ }));
129
+
130
+ vi.mock("./runtime.js", () => ({
131
+ getSynologyRuntime: vi.fn(() => ({
132
+ config: { current: vi.fn(() => mockRuntimeConfig) },
133
+ channel: {
134
+ routing: {
135
+ resolveAgentRoute: resolveAgentRouteMock,
136
+ },
137
+ reply: {
138
+ finalizeInboundContext: finalizeInboundContextMock,
139
+ dispatchReplyWithBufferedBlockDispatcher,
140
+ },
141
+ session: {
142
+ resolveStorePath: vi.fn(() => "/tmp/actagent/synology-chat-sessions.json"),
143
+ recordInboundSession: vi.fn(async () => undefined),
144
+ },
145
+ inbound: {
146
+ run: vi.fn(async (params) => {
147
+ const input = await params.adapter.ingest(params.raw);
148
+ if (!input) {
149
+ return { admission: { kind: "drop", reason: "ingest-null" }, dispatched: false };
150
+ }
151
+ const resolved = await params.adapter.resolveTurn(input, {
152
+ kind: "message",
153
+ canStartAgentTurn: true,
154
+ });
155
+ const dispatchResult = await resolved.dispatchReplyWithBufferedBlockDispatcher({
156
+ ctx: resolved.ctxPayload,
157
+ cfg: mockRuntimeConfig,
158
+ dispatcherOptions: {
159
+ ...resolved.dispatcherOptions,
160
+ deliver: resolved.delivery.deliver,
161
+ onError: resolved.delivery.onError,
162
+ },
163
+ });
164
+ return {
165
+ admission: { kind: "dispatch" },
166
+ dispatched: true,
167
+ dispatchResult,
168
+ ctxPayload: resolved.ctxPayload,
169
+ routeSessionKey: resolved.routeSessionKey,
170
+ };
171
+ }),
172
+ buildContext: buildChannelInboundEventContextMock,
173
+ },
174
+ },
175
+ })),
176
+ setSynologyRuntime: vi.fn(),
177
+ }));