@kodelyth/zalouser 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.
- package/README.md +120 -0
- package/api.ts +9 -0
- package/channel-plugin-api.ts +3 -0
- package/contract-api.ts +2 -0
- package/dist/accounts-DOefD_if.js +66 -0
- package/dist/accounts.runtime-KT101uuu.js +2 -0
- package/dist/api-DSWT4Dh_.js +133 -0
- package/dist/api.js +7 -0
- package/dist/channel-pby_3Sur.js +602 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-0aJ2O7Y8.js +25 -0
- package/dist/channel.setup-CqyWwqcQ.js +9 -0
- package/dist/contract-api.js +3 -0
- package/dist/doctor-contract-B9EvrW0j.js +128 -0
- package/dist/doctor-contract-api.js +2 -0
- package/dist/index.js +27 -0
- package/dist/monitor-CVtrUqyW.js +708 -0
- package/dist/runtime-api.js +19 -0
- package/dist/secret-contract-api.js +5 -0
- package/dist/security-audit-D_rftvs-.js +34 -0
- package/dist/send-uRjUB8mG.js +542 -0
- package/dist/session-route-CalHiv1d.js +92 -0
- package/dist/setup-entry.js +11 -0
- package/dist/setup-plugin-api.js +2 -0
- package/dist/setup-surface-Cfj4GQlB.js +360 -0
- package/dist/shared-DjK0e2FC.js +160 -0
- package/dist/test-api.js +5 -0
- package/dist/zalo-js-B80cRyDF.js +1285 -0
- package/doctor-contract-api.ts +1 -0
- package/index.ts +34 -0
- package/klaw.plugin.json +3 -286
- package/package.json +4 -4
- package/runtime-api.ts +62 -0
- package/secret-contract-api.ts +4 -0
- package/setup-entry.ts +9 -0
- package/setup-plugin-api.ts +2 -0
- package/src/accounts.runtime.ts +1 -0
- package/src/accounts.test-mocks.ts +14 -0
- package/src/accounts.test.ts +298 -0
- package/src/accounts.ts +136 -0
- package/src/channel-api.ts +16 -0
- package/src/channel.adapters.ts +432 -0
- package/src/channel.directory.test.ts +59 -0
- package/src/channel.runtime.ts +12 -0
- package/src/channel.sendpayload.test.ts +311 -0
- package/src/channel.setup.test.ts +30 -0
- package/src/channel.setup.ts +12 -0
- package/src/channel.test.ts +424 -0
- package/src/channel.ts +221 -0
- package/src/config-schema.ts +33 -0
- package/src/directory.ts +54 -0
- package/src/doctor-contract.ts +156 -0
- package/src/doctor.test.ts +87 -0
- package/src/doctor.ts +37 -0
- package/src/group-policy.test.ts +61 -0
- package/src/group-policy.ts +83 -0
- package/src/message-sid.test.ts +66 -0
- package/src/message-sid.ts +80 -0
- package/src/monitor.account-scope.test.ts +122 -0
- package/src/monitor.group-gating.test.ts +967 -0
- package/src/monitor.send-mocks.ts +20 -0
- package/src/monitor.ts +1057 -0
- package/src/probe.test.ts +60 -0
- package/src/probe.ts +35 -0
- package/src/qr-temp-file.ts +19 -0
- package/src/reaction.test.ts +19 -0
- package/src/reaction.ts +32 -0
- package/src/runtime.ts +9 -0
- package/src/security-audit.test.ts +83 -0
- package/src/security-audit.ts +71 -0
- package/src/send-receipt.ts +31 -0
- package/src/send.test.ts +424 -0
- package/src/send.ts +280 -0
- package/src/session-route.ts +121 -0
- package/src/setup-core.ts +36 -0
- package/src/setup-surface.test.ts +367 -0
- package/src/setup-surface.ts +481 -0
- package/src/setup-test-helpers.ts +42 -0
- package/src/shared.ts +92 -0
- package/src/status-issues.test.ts +31 -0
- package/src/status-issues.ts +55 -0
- package/src/test-helpers.ts +26 -0
- package/src/text-styles.test.ts +203 -0
- package/src/text-styles.ts +540 -0
- package/src/tool.test.ts +212 -0
- package/src/tool.ts +200 -0
- package/src/types.ts +127 -0
- package/src/zalo-js.credentials.test.ts +465 -0
- package/src/zalo-js.test-mocks.ts +89 -0
- package/src/zalo-js.ts +1889 -0
- package/src/zca-client.test.ts +27 -0
- package/src/zca-client.ts +259 -0
- package/src/zca-constants.ts +55 -0
- package/src/zca-js-exports.d.ts +22 -0
- package/test-api.ts +21 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/doctor-contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-entry.js +0 -7
- package/setup-plugin-api.js +0 -7
- package/test-api.js +0 -7
package/src/send.test.ts
ADDED
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import { createZalouserSendReceipt } from "./send-receipt.js";
|
|
3
|
+
import {
|
|
4
|
+
sendDeliveredZalouser,
|
|
5
|
+
sendImageZalouser,
|
|
6
|
+
sendLinkZalouser,
|
|
7
|
+
sendMessageZalouser,
|
|
8
|
+
sendReactionZalouser,
|
|
9
|
+
sendSeenZalouser,
|
|
10
|
+
sendTypingZalouser,
|
|
11
|
+
} from "./send.js";
|
|
12
|
+
import { parseZalouserTextStyles } from "./text-styles.js";
|
|
13
|
+
import {
|
|
14
|
+
sendZaloDeliveredEvent,
|
|
15
|
+
sendZaloLink,
|
|
16
|
+
sendZaloReaction,
|
|
17
|
+
sendZaloSeenEvent,
|
|
18
|
+
sendZaloTextMessage,
|
|
19
|
+
sendZaloTypingEvent,
|
|
20
|
+
} from "./zalo-js.js";
|
|
21
|
+
import { TextStyle } from "./zca-constants.js";
|
|
22
|
+
|
|
23
|
+
vi.mock("./zalo-js.js", () => ({
|
|
24
|
+
sendZaloTextMessage: vi.fn(),
|
|
25
|
+
sendZaloLink: vi.fn(),
|
|
26
|
+
sendZaloTypingEvent: vi.fn(),
|
|
27
|
+
sendZaloReaction: vi.fn(),
|
|
28
|
+
sendZaloDeliveredEvent: vi.fn(),
|
|
29
|
+
sendZaloSeenEvent: vi.fn(),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
const mockSendText = vi.mocked(sendZaloTextMessage);
|
|
33
|
+
const mockSendLink = vi.mocked(sendZaloLink);
|
|
34
|
+
const mockSendTyping = vi.mocked(sendZaloTypingEvent);
|
|
35
|
+
const mockSendReaction = vi.mocked(sendZaloReaction);
|
|
36
|
+
const mockSendDelivered = vi.mocked(sendZaloDeliveredEvent);
|
|
37
|
+
const mockSendSeen = vi.mocked(sendZaloSeenEvent);
|
|
38
|
+
|
|
39
|
+
function sendResult(
|
|
40
|
+
messageId: string,
|
|
41
|
+
threadId = "thread",
|
|
42
|
+
): {
|
|
43
|
+
ok: true;
|
|
44
|
+
messageId: string;
|
|
45
|
+
receipt: ReturnType<typeof createZalouserSendReceipt>;
|
|
46
|
+
} {
|
|
47
|
+
return {
|
|
48
|
+
ok: true,
|
|
49
|
+
messageId,
|
|
50
|
+
receipt: createZalouserSendReceipt({ messageId, threadId, kind: "text" }),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function sendFailure(error: string, threadId = "thread") {
|
|
55
|
+
return {
|
|
56
|
+
ok: false,
|
|
57
|
+
error,
|
|
58
|
+
receipt: createZalouserSendReceipt({ threadId, kind: "unknown" }),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
|
63
|
+
if (typeof value !== "object" || value === null) {
|
|
64
|
+
throw new Error(`${label} was not an object`);
|
|
65
|
+
}
|
|
66
|
+
return value as Record<string, unknown>;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function expectRecordFields(record: Record<string, unknown>, fields: Record<string, unknown>) {
|
|
70
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
71
|
+
expect(record[key]).toEqual(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function expectResultFields(result: unknown, fields: Record<string, unknown>) {
|
|
76
|
+
expectRecordFields(requireRecord(result, "send result"), fields);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function requireSendTextCall(callIndex: number): unknown[] {
|
|
80
|
+
const call = (mockSendText.mock.calls as unknown[][])[callIndex];
|
|
81
|
+
if (!call) {
|
|
82
|
+
throw new Error(`expected send text call ${callIndex + 1}`);
|
|
83
|
+
}
|
|
84
|
+
return call;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function requireSendTextOptions(callIndex: number): Record<string, unknown> {
|
|
88
|
+
return requireRecord(
|
|
89
|
+
requireSendTextCall(callIndex)[2],
|
|
90
|
+
`send text call ${callIndex + 1} options`,
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function expectSendTextOptions(callIndex: number, fields: Record<string, unknown>) {
|
|
95
|
+
expectRecordFields(requireSendTextOptions(callIndex), fields);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
describe("zalouser send helpers", () => {
|
|
99
|
+
beforeEach(() => {
|
|
100
|
+
mockSendText.mockReset();
|
|
101
|
+
mockSendLink.mockReset();
|
|
102
|
+
mockSendTyping.mockReset();
|
|
103
|
+
mockSendReaction.mockReset();
|
|
104
|
+
mockSendDelivered.mockReset();
|
|
105
|
+
mockSendSeen.mockReset();
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("keeps plain text literal by default", async () => {
|
|
109
|
+
mockSendText.mockResolvedValueOnce(sendResult("mid-1", "thread-1"));
|
|
110
|
+
|
|
111
|
+
const result = await sendMessageZalouser("thread-1", "**hello**", {
|
|
112
|
+
profile: "default",
|
|
113
|
+
isGroup: true,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-1");
|
|
117
|
+
expect(requireSendTextCall(0)[1]).toBe("**hello**");
|
|
118
|
+
expectSendTextOptions(0, { profile: "default", isGroup: true });
|
|
119
|
+
expectResultFields(result, { ok: true, messageId: "mid-1" });
|
|
120
|
+
expect(result.receipt.primaryPlatformMessageId).toBe("mid-1");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("formats markdown text when markdown mode is enabled", async () => {
|
|
124
|
+
mockSendText.mockResolvedValueOnce(sendResult("mid-1b", "thread-1"));
|
|
125
|
+
|
|
126
|
+
await sendMessageZalouser("thread-1", "**hello**", {
|
|
127
|
+
profile: "default",
|
|
128
|
+
isGroup: true,
|
|
129
|
+
textMode: "markdown",
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-1");
|
|
133
|
+
expect(requireSendTextCall(0)[1]).toBe("hello");
|
|
134
|
+
expectSendTextOptions(0, {
|
|
135
|
+
profile: "default",
|
|
136
|
+
isGroup: true,
|
|
137
|
+
textMode: "markdown",
|
|
138
|
+
textStyles: [{ start: 0, len: 5, st: TextStyle.Bold }],
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("formats image captions in markdown mode", async () => {
|
|
143
|
+
mockSendText.mockResolvedValueOnce(sendResult("mid-2", "thread-2"));
|
|
144
|
+
|
|
145
|
+
await sendImageZalouser("thread-2", "https://example.com/a.png", {
|
|
146
|
+
profile: "p2",
|
|
147
|
+
caption: "_cap_",
|
|
148
|
+
isGroup: false,
|
|
149
|
+
textMode: "markdown",
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-2");
|
|
153
|
+
expect(requireSendTextCall(0)[1]).toBe("cap");
|
|
154
|
+
expectSendTextOptions(0, {
|
|
155
|
+
profile: "p2",
|
|
156
|
+
caption: undefined,
|
|
157
|
+
isGroup: false,
|
|
158
|
+
mediaUrl: "https://example.com/a.png",
|
|
159
|
+
textMode: "markdown",
|
|
160
|
+
textStyles: [{ start: 0, len: 3, st: TextStyle.Italic }],
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("does not keep the raw markdown caption as a media fallback after formatting", async () => {
|
|
165
|
+
mockSendText.mockResolvedValueOnce(sendResult("mid-2b", "thread-2"));
|
|
166
|
+
|
|
167
|
+
await sendImageZalouser("thread-2", "https://example.com/a.png", {
|
|
168
|
+
profile: "p2",
|
|
169
|
+
caption: "```\n```",
|
|
170
|
+
isGroup: false,
|
|
171
|
+
textMode: "markdown",
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-2");
|
|
175
|
+
expect(requireSendTextCall(0)[1]).toBe("");
|
|
176
|
+
expectSendTextOptions(0, {
|
|
177
|
+
profile: "p2",
|
|
178
|
+
caption: undefined,
|
|
179
|
+
isGroup: false,
|
|
180
|
+
mediaUrl: "https://example.com/a.png",
|
|
181
|
+
textMode: "markdown",
|
|
182
|
+
textStyles: undefined,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
it("rechunks normalized markdown text before sending to avoid transport truncation", async () => {
|
|
187
|
+
const text = "\t".repeat(500) + "a".repeat(1500);
|
|
188
|
+
const formatted = parseZalouserTextStyles(text);
|
|
189
|
+
mockSendText
|
|
190
|
+
.mockResolvedValueOnce(sendResult("mid-2c-1", "thread-2c"))
|
|
191
|
+
.mockResolvedValueOnce(sendResult("mid-2c-2", "thread-2c"));
|
|
192
|
+
|
|
193
|
+
const result = await sendMessageZalouser("thread-2c", text, {
|
|
194
|
+
profile: "p2c",
|
|
195
|
+
isGroup: false,
|
|
196
|
+
textMode: "markdown",
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
expect(formatted.text.length).toBeGreaterThan(2000);
|
|
200
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
201
|
+
expect(mockSendText.mock.calls.map((call) => call[1]).join("")).toBe(formatted.text);
|
|
202
|
+
expect(
|
|
203
|
+
mockSendText.mock.calls
|
|
204
|
+
.map((call, index) => ({ index, length: call[1].length }))
|
|
205
|
+
.filter((call) => call.length > 2000),
|
|
206
|
+
).toStrictEqual([]);
|
|
207
|
+
expectResultFields(result, { ok: true, messageId: "mid-2c-2" });
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it("preserves text styles when splitting long formatted markdown", async () => {
|
|
211
|
+
const text = `**${"a".repeat(2501)}**`;
|
|
212
|
+
mockSendText
|
|
213
|
+
.mockResolvedValueOnce(sendResult("mid-2d-1", "thread-2d"))
|
|
214
|
+
.mockResolvedValueOnce(sendResult("mid-2d-2", "thread-2d"));
|
|
215
|
+
|
|
216
|
+
const result = await sendMessageZalouser("thread-2d", text, {
|
|
217
|
+
profile: "p2d",
|
|
218
|
+
isGroup: false,
|
|
219
|
+
textMode: "markdown",
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-2d");
|
|
223
|
+
expect(requireSendTextCall(0)[1]).toBe("a".repeat(2000));
|
|
224
|
+
expectSendTextOptions(0, {
|
|
225
|
+
profile: "p2d",
|
|
226
|
+
isGroup: false,
|
|
227
|
+
textMode: "markdown",
|
|
228
|
+
textStyles: [{ start: 0, len: 2000, st: TextStyle.Bold }],
|
|
229
|
+
});
|
|
230
|
+
expect(requireSendTextCall(1)[0]).toBe("thread-2d");
|
|
231
|
+
expect(requireSendTextCall(1)[1]).toBe("a".repeat(501));
|
|
232
|
+
expectSendTextOptions(1, {
|
|
233
|
+
profile: "p2d",
|
|
234
|
+
isGroup: false,
|
|
235
|
+
textMode: "markdown",
|
|
236
|
+
textStyles: [{ start: 0, len: 501, st: TextStyle.Bold }],
|
|
237
|
+
});
|
|
238
|
+
expectResultFields(result, { ok: true, messageId: "mid-2d-2" });
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
it("preserves formatted text and styles when newline chunk mode splits after parsing", async () => {
|
|
242
|
+
const text = `**${"a".repeat(1995)}**\n\nsecond paragraph`;
|
|
243
|
+
const formatted = parseZalouserTextStyles(text);
|
|
244
|
+
mockSendText
|
|
245
|
+
.mockResolvedValueOnce(sendResult("mid-2d-3", "thread-2d-2"))
|
|
246
|
+
.mockResolvedValueOnce(sendResult("mid-2d-4", "thread-2d-2"));
|
|
247
|
+
|
|
248
|
+
const result = await sendMessageZalouser("thread-2d-2", text, {
|
|
249
|
+
profile: "p2d-2",
|
|
250
|
+
isGroup: false,
|
|
251
|
+
textMode: "markdown",
|
|
252
|
+
textChunkMode: "newline",
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
256
|
+
expect(mockSendText.mock.calls.map((call) => call[1]).join("")).toBe(formatted.text);
|
|
257
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-2d-2");
|
|
258
|
+
expect(requireSendTextCall(0)[1]).toBe(`${"a".repeat(1995)}\n\n`);
|
|
259
|
+
expectSendTextOptions(0, {
|
|
260
|
+
profile: "p2d-2",
|
|
261
|
+
isGroup: false,
|
|
262
|
+
textMode: "markdown",
|
|
263
|
+
textChunkMode: "newline",
|
|
264
|
+
textStyles: [{ start: 0, len: 1995, st: TextStyle.Bold }],
|
|
265
|
+
});
|
|
266
|
+
expect(requireSendTextCall(1)[0]).toBe("thread-2d-2");
|
|
267
|
+
expect(requireSendTextCall(1)[1]).toBe("second paragraph");
|
|
268
|
+
expectSendTextOptions(1, {
|
|
269
|
+
profile: "p2d-2",
|
|
270
|
+
isGroup: false,
|
|
271
|
+
textMode: "markdown",
|
|
272
|
+
textChunkMode: "newline",
|
|
273
|
+
textStyles: undefined,
|
|
274
|
+
});
|
|
275
|
+
expectResultFields(result, { ok: true, messageId: "mid-2d-4" });
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("respects an explicit text chunk limit when splitting formatted markdown", async () => {
|
|
279
|
+
const text = `**${"a".repeat(1501)}**`;
|
|
280
|
+
mockSendText
|
|
281
|
+
.mockResolvedValueOnce(sendResult("mid-2d-5", "thread-2d-3"))
|
|
282
|
+
.mockResolvedValueOnce(sendResult("mid-2d-6", "thread-2d-3"));
|
|
283
|
+
|
|
284
|
+
const result = await sendMessageZalouser("thread-2d-3", text, {
|
|
285
|
+
profile: "p2d-3",
|
|
286
|
+
isGroup: false,
|
|
287
|
+
textMode: "markdown",
|
|
288
|
+
textChunkLimit: 1200,
|
|
289
|
+
} as never);
|
|
290
|
+
|
|
291
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
292
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-2d-3");
|
|
293
|
+
expect(requireSendTextCall(0)[1]).toBe("a".repeat(1200));
|
|
294
|
+
expectSendTextOptions(0, {
|
|
295
|
+
profile: "p2d-3",
|
|
296
|
+
isGroup: false,
|
|
297
|
+
textMode: "markdown",
|
|
298
|
+
textChunkLimit: 1200,
|
|
299
|
+
textStyles: [{ start: 0, len: 1200, st: TextStyle.Bold }],
|
|
300
|
+
});
|
|
301
|
+
expect(requireSendTextCall(1)[0]).toBe("thread-2d-3");
|
|
302
|
+
expect(requireSendTextCall(1)[1]).toBe("a".repeat(301));
|
|
303
|
+
expectSendTextOptions(1, {
|
|
304
|
+
profile: "p2d-3",
|
|
305
|
+
isGroup: false,
|
|
306
|
+
textMode: "markdown",
|
|
307
|
+
textChunkLimit: 1200,
|
|
308
|
+
textStyles: [{ start: 0, len: 301, st: TextStyle.Bold }],
|
|
309
|
+
});
|
|
310
|
+
expectResultFields(result, { ok: true, messageId: "mid-2d-6" });
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("sends overflow markdown captions as follow-up text after the media message", async () => {
|
|
314
|
+
const caption = "\t".repeat(500) + "a".repeat(1500);
|
|
315
|
+
const formatted = parseZalouserTextStyles(caption);
|
|
316
|
+
mockSendText
|
|
317
|
+
.mockResolvedValueOnce(sendResult("mid-2e-1", "thread-2e"))
|
|
318
|
+
.mockResolvedValueOnce(sendResult("mid-2e-2", "thread-2e"));
|
|
319
|
+
|
|
320
|
+
const result = await sendImageZalouser("thread-2e", "https://example.com/long.png", {
|
|
321
|
+
profile: "p2e",
|
|
322
|
+
caption,
|
|
323
|
+
isGroup: false,
|
|
324
|
+
textMode: "markdown",
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
expect(mockSendText).toHaveBeenCalledTimes(2);
|
|
328
|
+
expect(mockSendText.mock.calls.map((call) => call[1]).join("")).toBe(formatted.text);
|
|
329
|
+
expect(requireSendTextCall(0)[0]).toBe("thread-2e");
|
|
330
|
+
expect(typeof requireSendTextCall(0)[1]).toBe("string");
|
|
331
|
+
expectSendTextOptions(0, {
|
|
332
|
+
profile: "p2e",
|
|
333
|
+
caption: undefined,
|
|
334
|
+
isGroup: false,
|
|
335
|
+
mediaUrl: "https://example.com/long.png",
|
|
336
|
+
textMode: "markdown",
|
|
337
|
+
});
|
|
338
|
+
expect(requireSendTextCall(1)[0]).toBe("thread-2e");
|
|
339
|
+
expect(typeof requireSendTextCall(1)[1]).toBe("string");
|
|
340
|
+
expect(requireSendTextOptions(1).mediaUrl).toBeUndefined();
|
|
341
|
+
expectResultFields(result, { ok: true, messageId: "mid-2e-2" });
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("delegates link helper to JS transport", async () => {
|
|
345
|
+
mockSendLink.mockResolvedValueOnce(sendFailure("boom", "thread-3"));
|
|
346
|
+
|
|
347
|
+
const result = await sendLinkZalouser("thread-3", "https://klaw.kodelyth.com", {
|
|
348
|
+
profile: "p3",
|
|
349
|
+
isGroup: true,
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
expect(mockSendLink).toHaveBeenCalledWith("thread-3", "https://klaw.kodelyth.com", {
|
|
353
|
+
profile: "p3",
|
|
354
|
+
isGroup: true,
|
|
355
|
+
});
|
|
356
|
+
expectResultFields(result, { ok: false, error: "boom" });
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("delegates typing helper to JS transport", async () => {
|
|
360
|
+
await sendTypingZalouser("thread-4", { profile: "p4", isGroup: true });
|
|
361
|
+
|
|
362
|
+
expect(mockSendTyping).toHaveBeenCalledWith("thread-4", {
|
|
363
|
+
profile: "p4",
|
|
364
|
+
isGroup: true,
|
|
365
|
+
});
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
it("delegates reaction helper to JS transport", async () => {
|
|
369
|
+
mockSendReaction.mockResolvedValueOnce({ ok: true });
|
|
370
|
+
|
|
371
|
+
const result = await sendReactionZalouser({
|
|
372
|
+
threadId: "thread-5",
|
|
373
|
+
profile: "p5",
|
|
374
|
+
isGroup: true,
|
|
375
|
+
msgId: "100",
|
|
376
|
+
cliMsgId: "200",
|
|
377
|
+
emoji: "👍",
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
expect(mockSendReaction).toHaveBeenCalledWith({
|
|
381
|
+
profile: "p5",
|
|
382
|
+
threadId: "thread-5",
|
|
383
|
+
isGroup: true,
|
|
384
|
+
msgId: "100",
|
|
385
|
+
cliMsgId: "200",
|
|
386
|
+
emoji: "👍",
|
|
387
|
+
remove: undefined,
|
|
388
|
+
});
|
|
389
|
+
expectResultFields(result, { ok: true, error: undefined });
|
|
390
|
+
expect(result.receipt.platformMessageIds).toStrictEqual([]);
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
it("delegates delivered+seen helpers to JS transport", async () => {
|
|
394
|
+
mockSendDelivered.mockResolvedValueOnce();
|
|
395
|
+
mockSendSeen.mockResolvedValueOnce();
|
|
396
|
+
|
|
397
|
+
const message = {
|
|
398
|
+
msgId: "100",
|
|
399
|
+
cliMsgId: "200",
|
|
400
|
+
uidFrom: "1",
|
|
401
|
+
idTo: "2",
|
|
402
|
+
msgType: "webchat",
|
|
403
|
+
st: 1,
|
|
404
|
+
at: 0,
|
|
405
|
+
cmd: 0,
|
|
406
|
+
ts: "123",
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
await sendDeliveredZalouser({ profile: "p6", isGroup: true, message, isSeen: false });
|
|
410
|
+
await sendSeenZalouser({ profile: "p6", isGroup: true, message });
|
|
411
|
+
|
|
412
|
+
expect(mockSendDelivered).toHaveBeenCalledWith({
|
|
413
|
+
profile: "p6",
|
|
414
|
+
isGroup: true,
|
|
415
|
+
message,
|
|
416
|
+
isSeen: false,
|
|
417
|
+
});
|
|
418
|
+
expect(mockSendSeen).toHaveBeenCalledWith({
|
|
419
|
+
profile: "p6",
|
|
420
|
+
isGroup: true,
|
|
421
|
+
message,
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
});
|
package/src/send.ts
ADDED
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { createZalouserSendReceipt } from "./send-receipt.js";
|
|
2
|
+
import { parseZalouserTextStyles } from "./text-styles.js";
|
|
3
|
+
import type { ZaloEventMessage, ZaloSendOptions, ZaloSendResult } from "./types.js";
|
|
4
|
+
import {
|
|
5
|
+
sendZaloDeliveredEvent,
|
|
6
|
+
sendZaloLink,
|
|
7
|
+
sendZaloReaction,
|
|
8
|
+
sendZaloSeenEvent,
|
|
9
|
+
sendZaloTextMessage,
|
|
10
|
+
sendZaloTypingEvent,
|
|
11
|
+
} from "./zalo-js.js";
|
|
12
|
+
import { TextStyle } from "./zca-constants.js";
|
|
13
|
+
|
|
14
|
+
type ZalouserSendOptions = ZaloSendOptions;
|
|
15
|
+
type ZalouserSendResult = ZaloSendResult;
|
|
16
|
+
|
|
17
|
+
const ZALO_TEXT_LIMIT = 2000;
|
|
18
|
+
const DEFAULT_TEXT_CHUNK_MODE = "length";
|
|
19
|
+
|
|
20
|
+
type StyledTextChunk = {
|
|
21
|
+
text: string;
|
|
22
|
+
styles?: ZaloSendOptions["textStyles"];
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type TextChunkMode = NonNullable<ZaloSendOptions["textChunkMode"]>;
|
|
26
|
+
|
|
27
|
+
export async function sendMessageZalouser(
|
|
28
|
+
threadId: string,
|
|
29
|
+
text: string,
|
|
30
|
+
options: ZalouserSendOptions = {},
|
|
31
|
+
): Promise<ZalouserSendResult> {
|
|
32
|
+
const prepared =
|
|
33
|
+
options.textMode === "markdown"
|
|
34
|
+
? parseZalouserTextStyles(text)
|
|
35
|
+
: { text, styles: options.textStyles };
|
|
36
|
+
const textChunkLimit = options.textChunkLimit ?? ZALO_TEXT_LIMIT;
|
|
37
|
+
const chunks = splitStyledText(
|
|
38
|
+
prepared.text,
|
|
39
|
+
(prepared.styles?.length ?? 0) > 0 ? prepared.styles : undefined,
|
|
40
|
+
textChunkLimit,
|
|
41
|
+
options.textChunkMode,
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
let lastResult: ZalouserSendResult | null = null;
|
|
45
|
+
for (const [index, chunk] of chunks.entries()) {
|
|
46
|
+
const chunkOptions =
|
|
47
|
+
index === 0
|
|
48
|
+
? { ...options, textStyles: chunk.styles }
|
|
49
|
+
: {
|
|
50
|
+
...options,
|
|
51
|
+
caption: undefined,
|
|
52
|
+
mediaLocalRoots: undefined,
|
|
53
|
+
mediaUrl: undefined,
|
|
54
|
+
textStyles: chunk.styles,
|
|
55
|
+
};
|
|
56
|
+
const result = await sendZaloTextMessage(threadId, chunk.text, chunkOptions);
|
|
57
|
+
if (!result.ok) {
|
|
58
|
+
return result;
|
|
59
|
+
}
|
|
60
|
+
lastResult = result;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
lastResult ?? {
|
|
65
|
+
ok: false,
|
|
66
|
+
error: "No message content provided",
|
|
67
|
+
receipt: createZalouserSendReceipt({ threadId, kind: "text" }),
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export async function sendImageZalouser(
|
|
73
|
+
threadId: string,
|
|
74
|
+
imageUrl: string,
|
|
75
|
+
options: ZalouserSendOptions = {},
|
|
76
|
+
): Promise<ZalouserSendResult> {
|
|
77
|
+
return await sendMessageZalouser(threadId, options.caption ?? "", {
|
|
78
|
+
...options,
|
|
79
|
+
caption: undefined,
|
|
80
|
+
mediaUrl: imageUrl,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function sendLinkZalouser(
|
|
85
|
+
threadId: string,
|
|
86
|
+
url: string,
|
|
87
|
+
options: ZalouserSendOptions = {},
|
|
88
|
+
): Promise<ZalouserSendResult> {
|
|
89
|
+
return await sendZaloLink(threadId, url, options);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export async function sendTypingZalouser(
|
|
93
|
+
threadId: string,
|
|
94
|
+
options: Pick<ZalouserSendOptions, "profile" | "isGroup"> = {},
|
|
95
|
+
): Promise<void> {
|
|
96
|
+
await sendZaloTypingEvent(threadId, options);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function sendReactionZalouser(params: {
|
|
100
|
+
threadId: string;
|
|
101
|
+
msgId: string;
|
|
102
|
+
cliMsgId: string;
|
|
103
|
+
emoji: string;
|
|
104
|
+
remove?: boolean;
|
|
105
|
+
profile?: string;
|
|
106
|
+
isGroup?: boolean;
|
|
107
|
+
}): Promise<ZalouserSendResult> {
|
|
108
|
+
const result = await sendZaloReaction({
|
|
109
|
+
profile: params.profile,
|
|
110
|
+
threadId: params.threadId,
|
|
111
|
+
isGroup: params.isGroup,
|
|
112
|
+
msgId: params.msgId,
|
|
113
|
+
cliMsgId: params.cliMsgId,
|
|
114
|
+
emoji: params.emoji,
|
|
115
|
+
remove: params.remove,
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
ok: result.ok,
|
|
119
|
+
error: result.error,
|
|
120
|
+
receipt: createZalouserSendReceipt({ threadId: params.threadId, kind: "unknown" }),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function sendDeliveredZalouser(params: {
|
|
125
|
+
profile?: string;
|
|
126
|
+
isGroup?: boolean;
|
|
127
|
+
message: ZaloEventMessage;
|
|
128
|
+
isSeen?: boolean;
|
|
129
|
+
}): Promise<void> {
|
|
130
|
+
await sendZaloDeliveredEvent(params);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export async function sendSeenZalouser(params: {
|
|
134
|
+
profile?: string;
|
|
135
|
+
isGroup?: boolean;
|
|
136
|
+
message: ZaloEventMessage;
|
|
137
|
+
}): Promise<void> {
|
|
138
|
+
await sendZaloSeenEvent(params);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function splitStyledText(
|
|
142
|
+
text: string,
|
|
143
|
+
styles: ZaloSendOptions["textStyles"],
|
|
144
|
+
limit: number,
|
|
145
|
+
mode: ZaloSendOptions["textChunkMode"],
|
|
146
|
+
): StyledTextChunk[] {
|
|
147
|
+
if (text.length === 0) {
|
|
148
|
+
return [{ text, styles: undefined }];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const chunks: StyledTextChunk[] = [];
|
|
152
|
+
for (const range of splitTextRanges(text, limit, mode ?? DEFAULT_TEXT_CHUNK_MODE)) {
|
|
153
|
+
const { start, end } = range;
|
|
154
|
+
chunks.push({
|
|
155
|
+
text: text.slice(start, end),
|
|
156
|
+
styles: sliceTextStyles(styles, start, end),
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
return chunks;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function sliceTextStyles(
|
|
163
|
+
styles: ZaloSendOptions["textStyles"],
|
|
164
|
+
start: number,
|
|
165
|
+
end: number,
|
|
166
|
+
): ZaloSendOptions["textStyles"] {
|
|
167
|
+
if (!styles || styles.length === 0) {
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const chunkStyles = styles
|
|
172
|
+
.map((style) => {
|
|
173
|
+
const overlapStart = Math.max(style.start, start);
|
|
174
|
+
const overlapEnd = Math.min(style.start + style.len, end);
|
|
175
|
+
if (overlapEnd <= overlapStart) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (style.st === TextStyle.Indent) {
|
|
180
|
+
return {
|
|
181
|
+
start: overlapStart - start,
|
|
182
|
+
len: overlapEnd - overlapStart,
|
|
183
|
+
st: style.st,
|
|
184
|
+
indentSize: style.indentSize,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
start: overlapStart - start,
|
|
190
|
+
len: overlapEnd - overlapStart,
|
|
191
|
+
st: style.st,
|
|
192
|
+
};
|
|
193
|
+
})
|
|
194
|
+
.filter((style): style is NonNullable<typeof style> => style !== null);
|
|
195
|
+
|
|
196
|
+
return chunkStyles.length > 0 ? chunkStyles : undefined;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function splitTextRanges(
|
|
200
|
+
text: string,
|
|
201
|
+
limit: number,
|
|
202
|
+
mode: TextChunkMode,
|
|
203
|
+
): Array<{ start: number; end: number }> {
|
|
204
|
+
if (mode === "newline") {
|
|
205
|
+
return splitTextRangesByPreferredBreaks(text, limit);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const ranges: Array<{ start: number; end: number }> = [];
|
|
209
|
+
for (let start = 0; start < text.length; start += limit) {
|
|
210
|
+
ranges.push({
|
|
211
|
+
start,
|
|
212
|
+
end: Math.min(text.length, start + limit),
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return ranges;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function splitTextRangesByPreferredBreaks(
|
|
219
|
+
text: string,
|
|
220
|
+
limit: number,
|
|
221
|
+
): Array<{ start: number; end: number }> {
|
|
222
|
+
const ranges: Array<{ start: number; end: number }> = [];
|
|
223
|
+
let start = 0;
|
|
224
|
+
|
|
225
|
+
while (start < text.length) {
|
|
226
|
+
const maxEnd = Math.min(text.length, start + limit);
|
|
227
|
+
let end = maxEnd;
|
|
228
|
+
if (maxEnd < text.length) {
|
|
229
|
+
end =
|
|
230
|
+
findParagraphBreak(text, start, maxEnd) ??
|
|
231
|
+
findLastBreak(text, "\n", start, maxEnd) ??
|
|
232
|
+
findLastWhitespaceBreak(text, start, maxEnd) ??
|
|
233
|
+
maxEnd;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (end <= start) {
|
|
237
|
+
end = maxEnd;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
ranges.push({ start, end });
|
|
241
|
+
start = end;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
return ranges;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
function findParagraphBreak(text: string, start: number, end: number): number | undefined {
|
|
248
|
+
const slice = text.slice(start, end);
|
|
249
|
+
const matches = slice.matchAll(/\n[\t ]*\n+/g);
|
|
250
|
+
let lastMatch: RegExpMatchArray | undefined;
|
|
251
|
+
for (const match of matches) {
|
|
252
|
+
lastMatch = match;
|
|
253
|
+
}
|
|
254
|
+
if (!lastMatch || lastMatch.index === undefined) {
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
return start + lastMatch.index + lastMatch[0].length;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function findLastBreak(
|
|
261
|
+
text: string,
|
|
262
|
+
marker: string,
|
|
263
|
+
start: number,
|
|
264
|
+
end: number,
|
|
265
|
+
): number | undefined {
|
|
266
|
+
const index = text.lastIndexOf(marker, end - 1);
|
|
267
|
+
if (index < start) {
|
|
268
|
+
return undefined;
|
|
269
|
+
}
|
|
270
|
+
return index + marker.length;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function findLastWhitespaceBreak(text: string, start: number, end: number): number | undefined {
|
|
274
|
+
for (let index = end - 1; index > start; index -= 1) {
|
|
275
|
+
if (/\s/.test(text[index])) {
|
|
276
|
+
return index + 1;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return undefined;
|
|
280
|
+
}
|