@kyyinfinite/zapo-message 1.0.0 → 1.1.0
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/CHANGELOG.md +46 -0
- package/README.md +1220 -328
- package/dist/airich/airich.js +13 -8
- package/dist/btn/builders.js +8 -2
- package/dist/btn/media-slot.js +12 -3
- package/dist/btn/media-source.d.ts +1 -3
- package/dist/btn/thumbnail.js +18 -0
- package/dist/core/base-builder.d.ts +1 -0
- package/dist/core/base-builder.js +5 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/dist/message/album.js +3 -2
- package/dist/message/forward.js +3 -2
- package/dist/message/index.d.ts +2 -0
- package/dist/message/index.js +1 -0
- package/dist/message/operations.d.ts +22 -0
- package/dist/message/operations.js +176 -0
- package/dist/message/reaction.js +3 -2
- package/package.json +111 -107
- package/src/airich/airich.ts +16 -7
- package/src/btn/builders.ts +12 -2
- package/src/btn/media-slot.ts +11 -3
- package/src/btn/thumbnail.ts +22 -0
- package/src/core/base-builder.ts +8 -0
- package/src/index.ts +13 -0
- package/src/message/album.ts +3 -2
- package/src/message/forward.ts +3 -2
- package/src/message/index.ts +13 -0
- package/src/message/operations.ts +325 -0
- package/src/message/reaction.ts +3 -2
- package/test/message-operations.test.ts +298 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
WaMessageKey,
|
|
3
|
+
WaMessageRef,
|
|
4
|
+
WaMessageTargetInput,
|
|
5
|
+
WaSendEditKey,
|
|
6
|
+
WaSendPollParent,
|
|
7
|
+
WaSendEventParent,
|
|
8
|
+
WaSendEventResponseType,
|
|
9
|
+
} from "zapo-js";
|
|
10
|
+
import { MessageError } from "../core/errors.js";
|
|
11
|
+
import { targetChatJidOf, targetIdOf, targetKeyOf } from "../message/target.js";
|
|
12
|
+
import type { ZapoClient } from "../zapo-client.js";
|
|
13
|
+
|
|
14
|
+
export type MessageOperationTargetInput = WaMessageTargetInput;
|
|
15
|
+
|
|
16
|
+
const asTargetRecord = (
|
|
17
|
+
target: unknown,
|
|
18
|
+
): { key?: unknown; rawNode?: unknown } | undefined => {
|
|
19
|
+
if (!target || typeof target !== "object") return undefined;
|
|
20
|
+
const ref = target as { key?: unknown; rawNode?: unknown };
|
|
21
|
+
if (ref.rawNode !== undefined) return ref;
|
|
22
|
+
if (ref.key && typeof ref.key === "object") return ref;
|
|
23
|
+
return { key: ref };
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const resolveTargetKey = (
|
|
27
|
+
target: unknown,
|
|
28
|
+
): { remoteJid: string; id: string; fromMe: boolean; participant?: string } => {
|
|
29
|
+
const record = asTargetRecord(target);
|
|
30
|
+
const key = record?.key ?? target;
|
|
31
|
+
const keyRecord = (key ?? {}) as {
|
|
32
|
+
remoteJid?: unknown;
|
|
33
|
+
id?: unknown;
|
|
34
|
+
fromMe?: unknown;
|
|
35
|
+
participant?: unknown;
|
|
36
|
+
};
|
|
37
|
+
const remoteJid = keyRecord.remoteJid ?? targetChatJidOf(target);
|
|
38
|
+
const id = keyRecord.id ?? targetIdOf(target);
|
|
39
|
+
if (typeof remoteJid !== "string" || !remoteJid) {
|
|
40
|
+
throw new MessageError(
|
|
41
|
+
"target tidak punya remoteJid — berikan message key atau event pesan masuk",
|
|
42
|
+
"INVALID_TARGET",
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
if (typeof id !== "string" || !id) {
|
|
46
|
+
throw new MessageError(
|
|
47
|
+
"target tidak punya id — berikan message key atau event pesan masuk",
|
|
48
|
+
"INVALID_TARGET",
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
const resolved: {
|
|
52
|
+
remoteJid: string;
|
|
53
|
+
id: string;
|
|
54
|
+
fromMe: boolean;
|
|
55
|
+
participant?: string;
|
|
56
|
+
} = {
|
|
57
|
+
remoteJid,
|
|
58
|
+
id,
|
|
59
|
+
fromMe: keyRecord.fromMe === true,
|
|
60
|
+
};
|
|
61
|
+
if (typeof keyRecord.participant === "string" && keyRecord.participant) {
|
|
62
|
+
resolved.participant = keyRecord.participant;
|
|
63
|
+
}
|
|
64
|
+
return resolved;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const assertClient = (client: ZapoClient | undefined): ZapoClient => {
|
|
68
|
+
if (!client) throw new MessageError("client belum diset", "NO_CLIENT");
|
|
69
|
+
return client;
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
export const resolveMessageTargetKey = resolveTargetKey;
|
|
73
|
+
|
|
74
|
+
export const editMessage = async (
|
|
75
|
+
client: ZapoClient | undefined,
|
|
76
|
+
target: unknown,
|
|
77
|
+
content: unknown,
|
|
78
|
+
): Promise<unknown> =>
|
|
79
|
+
assertClient(client).message.send(
|
|
80
|
+
resolveTargetKey(target).remoteJid,
|
|
81
|
+
content as never,
|
|
82
|
+
{ editKey: resolveTargetKey(target) as WaSendEditKey } as never,
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
export const revokeMessage = async (
|
|
86
|
+
client: ZapoClient | undefined,
|
|
87
|
+
target: unknown,
|
|
88
|
+
): Promise<unknown> => {
|
|
89
|
+
const key = resolveTargetKey(target);
|
|
90
|
+
return assertClient(client).message.send(key.remoteJid, {
|
|
91
|
+
type: "revoke",
|
|
92
|
+
target: { key },
|
|
93
|
+
} as never);
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
export const pinMessage = async (
|
|
97
|
+
client: ZapoClient | undefined,
|
|
98
|
+
target: unknown,
|
|
99
|
+
durationSecs?: number,
|
|
100
|
+
): Promise<unknown> => {
|
|
101
|
+
const key = resolveTargetKey(target);
|
|
102
|
+
const payload: Record<string, unknown> = {
|
|
103
|
+
type: "pin",
|
|
104
|
+
target: { key },
|
|
105
|
+
};
|
|
106
|
+
if (durationSecs !== undefined) {
|
|
107
|
+
if (!Number.isFinite(durationSecs) || durationSecs < 0) {
|
|
108
|
+
throw new MessageError(
|
|
109
|
+
"pin durationSecs harus number >= 0",
|
|
110
|
+
"VALIDATION",
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
payload.durationSecs = durationSecs;
|
|
114
|
+
}
|
|
115
|
+
return assertClient(client).message.send(
|
|
116
|
+
key.remoteJid,
|
|
117
|
+
payload as never,
|
|
118
|
+
);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export const unpinMessage = async (
|
|
122
|
+
client: ZapoClient | undefined,
|
|
123
|
+
target: unknown,
|
|
124
|
+
): Promise<unknown> => {
|
|
125
|
+
const key = resolveTargetKey(target);
|
|
126
|
+
return assertClient(client).message.send(key.remoteJid, {
|
|
127
|
+
type: "unpin",
|
|
128
|
+
target: { key },
|
|
129
|
+
} as never);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export const keepMessage = async (
|
|
133
|
+
client: ZapoClient | undefined,
|
|
134
|
+
target: unknown,
|
|
135
|
+
): Promise<unknown> => {
|
|
136
|
+
const key = resolveTargetKey(target);
|
|
137
|
+
return assertClient(client).message.send(key.remoteJid, {
|
|
138
|
+
type: "keep",
|
|
139
|
+
target: { key },
|
|
140
|
+
} as never);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
export const unkeepMessage = async (
|
|
144
|
+
client: ZapoClient | undefined,
|
|
145
|
+
target: unknown,
|
|
146
|
+
): Promise<unknown> => {
|
|
147
|
+
const key = resolveTargetKey(target);
|
|
148
|
+
return assertClient(client).message.send(key.remoteJid, {
|
|
149
|
+
type: "unkeep",
|
|
150
|
+
target: { key },
|
|
151
|
+
} as never);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
export interface PollVoteInput {
|
|
155
|
+
poll: WaSendPollParent;
|
|
156
|
+
selectedOptionNames: readonly string[];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export const votePoll = async (
|
|
160
|
+
client: ZapoClient | undefined,
|
|
161
|
+
input: PollVoteInput,
|
|
162
|
+
): Promise<unknown> => {
|
|
163
|
+
if (!input || typeof input !== "object") {
|
|
164
|
+
throw new MessageError(
|
|
165
|
+
"poll vote butuh { poll, selectedOptionNames }",
|
|
166
|
+
"VALIDATION",
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
if (!input.poll || typeof input.poll !== "object") {
|
|
170
|
+
throw new MessageError(
|
|
171
|
+
"poll vote butuh poll parent (id, fromMe, authorJid, messageSecret)",
|
|
172
|
+
"VALIDATION",
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
if (
|
|
176
|
+
typeof input.poll.id !== "string" ||
|
|
177
|
+
!input.poll.id ||
|
|
178
|
+
typeof input.poll.authorJid !== "string" ||
|
|
179
|
+
!input.poll.authorJid ||
|
|
180
|
+
!(input.poll.messageSecret instanceof Uint8Array)
|
|
181
|
+
) {
|
|
182
|
+
throw new MessageError(
|
|
183
|
+
"poll vote butuh poll parent (id, fromMe, authorJid, messageSecret)",
|
|
184
|
+
"VALIDATION",
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
if (
|
|
188
|
+
!Array.isArray(input.selectedOptionNames) ||
|
|
189
|
+
!input.selectedOptionNames.length ||
|
|
190
|
+
!input.selectedOptionNames.every(
|
|
191
|
+
(name) => typeof name === "string" && !!name,
|
|
192
|
+
)
|
|
193
|
+
) {
|
|
194
|
+
throw new MessageError(
|
|
195
|
+
"poll vote selectedOptionNames butuh array string non-kosong",
|
|
196
|
+
"VALIDATION",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const remoteJid = targetChatJidOf(input.poll as never) ?? undefined;
|
|
200
|
+
if (!remoteJid) {
|
|
201
|
+
throw new MessageError(
|
|
202
|
+
"poll vote butuh remoteJid pada poll parent",
|
|
203
|
+
"INVALID_TARGET",
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return assertClient(client).message.send(remoteJid, {
|
|
207
|
+
type: "poll-vote",
|
|
208
|
+
poll: input.poll,
|
|
209
|
+
selectedOptionNames: [...input.selectedOptionNames],
|
|
210
|
+
} as never);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
export const sendEvent = async (
|
|
214
|
+
client: ZapoClient | undefined,
|
|
215
|
+
jid: string | undefined,
|
|
216
|
+
input: unknown,
|
|
217
|
+
): Promise<unknown> => {
|
|
218
|
+
const config = (input ?? {}) as {
|
|
219
|
+
name?: unknown;
|
|
220
|
+
startTime?: unknown;
|
|
221
|
+
endTime?: unknown;
|
|
222
|
+
description?: unknown;
|
|
223
|
+
location?: unknown;
|
|
224
|
+
joinLink?: unknown;
|
|
225
|
+
extraGuestsAllowed?: unknown;
|
|
226
|
+
isScheduleCall?: unknown;
|
|
227
|
+
isCanceled?: unknown;
|
|
228
|
+
hasReminder?: unknown;
|
|
229
|
+
reminderOffsetSec?: unknown;
|
|
230
|
+
};
|
|
231
|
+
if (typeof jid !== "string" || !jid) {
|
|
232
|
+
throw new MessageError(
|
|
233
|
+
"event butuh jid tujuan — panggil send(jid) atau .defaultTo(jid)",
|
|
234
|
+
"NO_TARGET",
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
if (typeof config.name !== "string" || !config.name.trim()) {
|
|
238
|
+
throw new MessageError(
|
|
239
|
+
"event.name wajib diisi (string non-kosong)",
|
|
240
|
+
"VALIDATION",
|
|
241
|
+
);
|
|
242
|
+
}
|
|
243
|
+
if (
|
|
244
|
+
typeof config.startTime !== "number" ||
|
|
245
|
+
!Number.isFinite(config.startTime)
|
|
246
|
+
) {
|
|
247
|
+
throw new MessageError(
|
|
248
|
+
"event.startTime harus unix seconds (number)",
|
|
249
|
+
"VALIDATION",
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
const payload: Record<string, unknown> = {
|
|
253
|
+
type: "event",
|
|
254
|
+
name: config.name,
|
|
255
|
+
startTime: config.startTime,
|
|
256
|
+
};
|
|
257
|
+
if (config.endTime !== undefined) {
|
|
258
|
+
if (
|
|
259
|
+
typeof config.endTime !== "number" ||
|
|
260
|
+
!Number.isFinite(config.endTime)
|
|
261
|
+
) {
|
|
262
|
+
throw new MessageError(
|
|
263
|
+
"event.endTime harus unix seconds (number)",
|
|
264
|
+
"VALIDATION",
|
|
265
|
+
);
|
|
266
|
+
}
|
|
267
|
+
payload.endTime = config.endTime;
|
|
268
|
+
}
|
|
269
|
+
if (config.description !== undefined) payload.description = config.description;
|
|
270
|
+
if (config.location !== undefined) payload.location = config.location;
|
|
271
|
+
if (config.joinLink !== undefined) payload.joinLink = config.joinLink;
|
|
272
|
+
if (config.extraGuestsAllowed !== undefined)
|
|
273
|
+
payload.extraGuestsAllowed = config.extraGuestsAllowed;
|
|
274
|
+
if (config.isScheduleCall !== undefined)
|
|
275
|
+
payload.isScheduleCall = config.isScheduleCall;
|
|
276
|
+
if (config.isCanceled !== undefined) payload.isCanceled = config.isCanceled;
|
|
277
|
+
if (config.hasReminder !== undefined) payload.hasReminder = config.hasReminder;
|
|
278
|
+
if (config.reminderOffsetSec !== undefined)
|
|
279
|
+
payload.reminderOffsetSec = config.reminderOffsetSec;
|
|
280
|
+
return assertClient(client).message.send(jid, payload as never);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
export const respondEvent = async (
|
|
284
|
+
client: ZapoClient | undefined,
|
|
285
|
+
event: unknown,
|
|
286
|
+
response: WaSendEventResponseType,
|
|
287
|
+
): Promise<unknown> => {
|
|
288
|
+
if (response !== "going" && response !== "not_going" && response !== "maybe") {
|
|
289
|
+
throw new MessageError(
|
|
290
|
+
'event response harus "going" | "not_going" | "maybe"',
|
|
291
|
+
"VALIDATION",
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
const parent = (event ?? {}) as {
|
|
295
|
+
id?: unknown;
|
|
296
|
+
fromMe?: unknown;
|
|
297
|
+
participant?: unknown;
|
|
298
|
+
authorJid?: unknown;
|
|
299
|
+
messageSecret?: unknown;
|
|
300
|
+
};
|
|
301
|
+
if (
|
|
302
|
+
typeof parent.id !== "string" ||
|
|
303
|
+
!parent.id ||
|
|
304
|
+
typeof parent.authorJid !== "string" ||
|
|
305
|
+
!parent.authorJid ||
|
|
306
|
+
!(parent.messageSecret instanceof Uint8Array)
|
|
307
|
+
) {
|
|
308
|
+
throw new MessageError(
|
|
309
|
+
"event response butuh parent event (id, fromMe, authorJid, messageSecret)",
|
|
310
|
+
"VALIDATION",
|
|
311
|
+
);
|
|
312
|
+
}
|
|
313
|
+
const remoteJid = targetChatJidOf(event as never) ?? undefined;
|
|
314
|
+
if (!remoteJid) {
|
|
315
|
+
throw new MessageError(
|
|
316
|
+
"event response butuh remoteJid pada parent event",
|
|
317
|
+
"INVALID_TARGET",
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
return assertClient(client).message.send(remoteJid, {
|
|
321
|
+
type: "event-response",
|
|
322
|
+
event: parent,
|
|
323
|
+
response,
|
|
324
|
+
} as never);
|
|
325
|
+
};
|
package/src/message/reaction.ts
CHANGED
|
@@ -73,9 +73,10 @@ export class ReactionBuilder extends BaseBuilder<ZapoReactionContent> {
|
|
|
73
73
|
);
|
|
74
74
|
}
|
|
75
75
|
const content = this.build();
|
|
76
|
+
const merged = this._mergedOptions(options);
|
|
76
77
|
const send = client.message.send.bind(client.message);
|
|
77
|
-
return Object.keys(
|
|
78
|
-
? send(target, content,
|
|
78
|
+
return Object.keys(merged).length
|
|
79
|
+
? send(target, content, merged)
|
|
79
80
|
: send(target, content);
|
|
80
81
|
}
|
|
81
82
|
}
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import sharp from "sharp";
|
|
4
|
+
import {
|
|
5
|
+
listMessage,
|
|
6
|
+
reaction,
|
|
7
|
+
forward,
|
|
8
|
+
album,
|
|
9
|
+
airich,
|
|
10
|
+
editMessage,
|
|
11
|
+
revokeMessage,
|
|
12
|
+
pinMessage,
|
|
13
|
+
unpinMessage,
|
|
14
|
+
keepMessage,
|
|
15
|
+
unkeepMessage,
|
|
16
|
+
votePoll,
|
|
17
|
+
sendEvent,
|
|
18
|
+
respondEvent,
|
|
19
|
+
MessageError,
|
|
20
|
+
type ZapoClient,
|
|
21
|
+
} from "@kyyinfinite/zapo-message";
|
|
22
|
+
import { asClient } from "./helpers.js";
|
|
23
|
+
|
|
24
|
+
const INCOMING = {
|
|
25
|
+
key: { remoteJid: "6281@s.whatsapp.net", id: "MSG-9", fromMe: false },
|
|
26
|
+
message: { conversation: "asli" },
|
|
27
|
+
rawNode: {},
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const capture = () => {
|
|
31
|
+
const sends: { to: unknown; content: unknown; options: unknown }[] = [];
|
|
32
|
+
const client = asClient({
|
|
33
|
+
message: {
|
|
34
|
+
upload: async () => ({
|
|
35
|
+
url: "https://mmg.whatsapp.net/v/t62.7118-24/op",
|
|
36
|
+
directPath: "/v/t62.7118-24/op",
|
|
37
|
+
mediaKey: new Uint8Array(32).fill(1),
|
|
38
|
+
fileSha256: new Uint8Array(32).fill(2),
|
|
39
|
+
fileEncSha256: new Uint8Array(32).fill(3),
|
|
40
|
+
fileLength: 10,
|
|
41
|
+
mediaKeyTimestamp: 1730000000,
|
|
42
|
+
}),
|
|
43
|
+
send: async (to: unknown, content: unknown, options: unknown) => {
|
|
44
|
+
sends.push({ to, content, options });
|
|
45
|
+
return { id: `OP-${sends.length}`, attempts: 1, ack: {} };
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
return { client, sends };
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
test("quote parity: defaultQuote dipakai seluruh builder custom send", async () => {
|
|
53
|
+
const listCap = capture();
|
|
54
|
+
await listMessage(listCap.client)
|
|
55
|
+
.defaultQuote(INCOMING)
|
|
56
|
+
.title("M")
|
|
57
|
+
.body("b")
|
|
58
|
+
.buttonText("Buka")
|
|
59
|
+
.section("S", [{ id: "r", title: "t" }])
|
|
60
|
+
.send("6281@s.whatsapp.net");
|
|
61
|
+
assert.equal(
|
|
62
|
+
(listCap.sends[0]?.options as { quote?: unknown })?.quote,
|
|
63
|
+
INCOMING,
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
const reactCap = capture();
|
|
67
|
+
await reaction(reactCap.client)
|
|
68
|
+
.defaultQuote(INCOMING)
|
|
69
|
+
.target({ key: { remoteJid: "6281@s.whatsapp.net", id: "MSG-8" } })
|
|
70
|
+
.emoji("⭐")
|
|
71
|
+
.send("6281@s.whatsapp.net");
|
|
72
|
+
assert.equal(
|
|
73
|
+
(reactCap.sends[0]?.options as { quote?: unknown })?.quote,
|
|
74
|
+
INCOMING,
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const fwdCap = capture();
|
|
78
|
+
await forward(fwdCap.client)
|
|
79
|
+
.defaultQuote(INCOMING)
|
|
80
|
+
.message({
|
|
81
|
+
message: { conversation: "asli" },
|
|
82
|
+
key: { remoteJid: "6281@s.whatsapp.net", id: "MSG-7", fromMe: false },
|
|
83
|
+
rawNode: {},
|
|
84
|
+
})
|
|
85
|
+
.send("6281@s.whatsapp.net");
|
|
86
|
+
assert.equal(
|
|
87
|
+
(fwdCap.sends[0]?.options as { quote?: unknown })?.quote,
|
|
88
|
+
INCOMING,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const albumCap = capture();
|
|
92
|
+
const png = await sharp({
|
|
93
|
+
create: { width: 100, height: 100, channels: 3, background: "red" },
|
|
94
|
+
})
|
|
95
|
+
.png()
|
|
96
|
+
.toBuffer();
|
|
97
|
+
const albumBuilder = album(albumCap.client).defaultQuote(INCOMING);
|
|
98
|
+
await albumBuilder.image(png).send("6281@s.whatsapp.net");
|
|
99
|
+
assert.equal(
|
|
100
|
+
(albumCap.sends[0]?.options as { quote?: unknown })?.quote,
|
|
101
|
+
INCOMING,
|
|
102
|
+
);
|
|
103
|
+
const childQuote = (
|
|
104
|
+
(albumCap.sends[1]?.options as { quote?: { id?: string } })?.quote ?? {}
|
|
105
|
+
).id;
|
|
106
|
+
assert.equal(childQuote, "OP-1");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("quote parity: quote eksplisit menimpa defaultQuote", async () => {
|
|
110
|
+
const cap = capture();
|
|
111
|
+
const explicit = { key: { id: "EXPLICIT" } };
|
|
112
|
+
await listMessage(cap.client)
|
|
113
|
+
.defaultQuote(INCOMING)
|
|
114
|
+
.title("M")
|
|
115
|
+
.body("b")
|
|
116
|
+
.buttonText("Buka")
|
|
117
|
+
.section("S", [{ id: "r", title: "t" }])
|
|
118
|
+
.send("6281@s.whatsapp.net", { quote: explicit });
|
|
119
|
+
assert.equal((cap.sends[0]?.options as { quote?: unknown })?.quote, explicit);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("target parity: airich defaultTo dipakai send() tanpa argumen", async () => {
|
|
123
|
+
const cap = capture();
|
|
124
|
+
await airich(cap.client)
|
|
125
|
+
.defaultTo("6281@s.whatsapp.net")
|
|
126
|
+
.text("halo")
|
|
127
|
+
.send();
|
|
128
|
+
assert.equal(cap.sends[0]?.to, "6281@s.whatsapp.net");
|
|
129
|
+
assert.equal(cap.sends[1]?.to, "6281@s.whatsapp.net");
|
|
130
|
+
assert.equal(cap.sends.length, 2);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("target parity: airich tanpa target melempar NO_TARGET", async () => {
|
|
134
|
+
const cap = capture();
|
|
135
|
+
await assert.rejects(
|
|
136
|
+
() => airich(cap.client).text("halo").send(),
|
|
137
|
+
(error: MessageError) =>
|
|
138
|
+
error instanceof MessageError && error.code === "NO_TARGET",
|
|
139
|
+
);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test("operations: editMessage mengirim editKey native", async () => {
|
|
143
|
+
const cap = capture();
|
|
144
|
+
await editMessage(cap.client, INCOMING, { conversation: "baru" });
|
|
145
|
+
const call = cap.sends[0] as {
|
|
146
|
+
to?: string;
|
|
147
|
+
content?: { conversation?: string };
|
|
148
|
+
options?: { editKey?: { id?: string; remoteJid?: string } };
|
|
149
|
+
};
|
|
150
|
+
assert.equal(call.to, "6281@s.whatsapp.net");
|
|
151
|
+
assert.equal(call.content?.conversation, "baru");
|
|
152
|
+
assert.equal(call.options?.editKey?.id, "MSG-9");
|
|
153
|
+
assert.equal(call.options?.editKey?.remoteJid, "6281@s.whatsapp.net");
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
test("operations: revoke/pin/unpin/keep/unkeep payload terketik", async () => {
|
|
157
|
+
const cap = capture();
|
|
158
|
+
await revokeMessage(cap.client, INCOMING);
|
|
159
|
+
await pinMessage(cap.client, INCOMING, 604800);
|
|
160
|
+
await unpinMessage(cap.client, INCOMING);
|
|
161
|
+
await keepMessage(cap.client, INCOMING);
|
|
162
|
+
await unkeepMessage(cap.client, INCOMING);
|
|
163
|
+
const types = cap.sends.map(
|
|
164
|
+
(s) => (s.content as { type?: string }).type ?? "?",
|
|
165
|
+
);
|
|
166
|
+
assert.deepEqual(types, [
|
|
167
|
+
"revoke",
|
|
168
|
+
"pin",
|
|
169
|
+
"unpin",
|
|
170
|
+
"keep",
|
|
171
|
+
"unkeep",
|
|
172
|
+
]);
|
|
173
|
+
const pin = cap.sends[1]?.content as {
|
|
174
|
+
target?: { key?: { id?: string } };
|
|
175
|
+
durationSecs?: number;
|
|
176
|
+
};
|
|
177
|
+
assert.equal(pin.target?.key?.id, "MSG-9");
|
|
178
|
+
assert.equal(pin.durationSecs, 604800);
|
|
179
|
+
assert.equal(cap.sends.every((s) => s.to === "6281@s.whatsapp.net"), true);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test("operations: votePoll validasi parent dan mengirim poll-vote", async () => {
|
|
183
|
+
const cap = capture();
|
|
184
|
+
const pollParent = {
|
|
185
|
+
id: "POLL-1",
|
|
186
|
+
fromMe: false,
|
|
187
|
+
authorJid: "6281@s.whatsapp.net",
|
|
188
|
+
messageSecret: new Uint8Array(32).fill(7),
|
|
189
|
+
remoteJid: "6281@s.whatsapp.net",
|
|
190
|
+
};
|
|
191
|
+
await votePoll(cap.client, {
|
|
192
|
+
poll: pollParent,
|
|
193
|
+
selectedOptionNames: ["A"],
|
|
194
|
+
});
|
|
195
|
+
const call = cap.sends[0] as {
|
|
196
|
+
to?: string;
|
|
197
|
+
content?: {
|
|
198
|
+
type?: string;
|
|
199
|
+
selectedOptionNames?: string[];
|
|
200
|
+
poll?: { id?: string };
|
|
201
|
+
};
|
|
202
|
+
};
|
|
203
|
+
assert.equal(call.to, "6281@s.whatsapp.net");
|
|
204
|
+
assert.equal(call.content?.type, "poll-vote");
|
|
205
|
+
assert.deepEqual(call.content?.selectedOptionNames, ["A"]);
|
|
206
|
+
assert.equal(call.content?.poll?.id, "POLL-1");
|
|
207
|
+
|
|
208
|
+
await assert.rejects(
|
|
209
|
+
() =>
|
|
210
|
+
votePoll(cap.client, {
|
|
211
|
+
poll: { ...pollParent, messageSecret: undefined } as never,
|
|
212
|
+
selectedOptionNames: ["A"],
|
|
213
|
+
}),
|
|
214
|
+
(error: MessageError) =>
|
|
215
|
+
error instanceof MessageError && error.code === "VALIDATION",
|
|
216
|
+
);
|
|
217
|
+
await assert.rejects(
|
|
218
|
+
() =>
|
|
219
|
+
votePoll(cap.client, {
|
|
220
|
+
poll: pollParent,
|
|
221
|
+
selectedOptionNames: [],
|
|
222
|
+
}),
|
|
223
|
+
(error: MessageError) =>
|
|
224
|
+
error instanceof MessageError && error.code === "VALIDATION",
|
|
225
|
+
);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("operations: sendEvent dan respondEvent payload native", async () => {
|
|
229
|
+
const cap = capture();
|
|
230
|
+
await sendEvent(cap.client, "6281@s.whatsapp.net", {
|
|
231
|
+
name: "Rapat",
|
|
232
|
+
startTime: 1730000000,
|
|
233
|
+
});
|
|
234
|
+
const event = cap.sends[0] as {
|
|
235
|
+
to?: string;
|
|
236
|
+
content?: {
|
|
237
|
+
type?: string;
|
|
238
|
+
name?: string;
|
|
239
|
+
startTime?: number;
|
|
240
|
+
};
|
|
241
|
+
};
|
|
242
|
+
assert.equal(event.to, "6281@s.whatsapp.net");
|
|
243
|
+
assert.equal(event.content?.type, "event");
|
|
244
|
+
assert.equal(event.content?.name, "Rapat");
|
|
245
|
+
assert.equal(event.content?.startTime, 1730000000);
|
|
246
|
+
|
|
247
|
+
const eventParent = {
|
|
248
|
+
id: "EVT-1",
|
|
249
|
+
fromMe: false,
|
|
250
|
+
authorJid: "6281@s.whatsapp.net",
|
|
251
|
+
messageSecret: new Uint8Array(32).fill(9),
|
|
252
|
+
remoteJid: "6281@s.whatsapp.net",
|
|
253
|
+
};
|
|
254
|
+
await respondEvent(cap.client, eventParent, "going");
|
|
255
|
+
const response = cap.sends[1] as {
|
|
256
|
+
to?: string;
|
|
257
|
+
content?: {
|
|
258
|
+
type?: string;
|
|
259
|
+
response?: string;
|
|
260
|
+
event?: { id?: string };
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
assert.equal(response.to, "6281@s.whatsapp.net");
|
|
264
|
+
assert.equal(response.content?.type, "event-response");
|
|
265
|
+
assert.equal(response.content?.response, "going");
|
|
266
|
+
assert.equal(response.content?.event?.id, "EVT-1");
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
test("operations: validasi menolak input tidak valid sebelum kirim", async () => {
|
|
270
|
+
const cap = capture();
|
|
271
|
+
await assert.rejects(
|
|
272
|
+
() =>
|
|
273
|
+
sendEvent(cap.client, "6281@s.whatsapp.net", { startTime: 1730000000 }),
|
|
274
|
+
(error: MessageError) =>
|
|
275
|
+
error instanceof MessageError && error.code === "VALIDATION",
|
|
276
|
+
);
|
|
277
|
+
await assert.rejects(
|
|
278
|
+
() => respondEvent(cap.client, { id: "x" } as never, "wat" as never),
|
|
279
|
+
(error: MessageError) =>
|
|
280
|
+
error instanceof MessageError && error.code === "VALIDATION",
|
|
281
|
+
);
|
|
282
|
+
await assert.rejects(
|
|
283
|
+
() => revokeMessage(cap.client, { key: { remoteJid: "x" } }),
|
|
284
|
+
(error: MessageError) =>
|
|
285
|
+
error instanceof MessageError && error.code === "INVALID_TARGET",
|
|
286
|
+
);
|
|
287
|
+
await assert.rejects(
|
|
288
|
+
() => pinMessage(cap.client, INCOMING, -5),
|
|
289
|
+
(error: MessageError) =>
|
|
290
|
+
error instanceof MessageError && error.code === "VALIDATION",
|
|
291
|
+
);
|
|
292
|
+
await assert.rejects(
|
|
293
|
+
() => revokeMessage(undefined, INCOMING),
|
|
294
|
+
(error: MessageError) =>
|
|
295
|
+
error instanceof MessageError && error.code === "NO_CLIENT",
|
|
296
|
+
);
|
|
297
|
+
assert.equal(cap.sends.length, 0);
|
|
298
|
+
});
|