@kyyinfinite/zapo-message 0.0.0-stage → 1.0.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 +159 -0
- package/LICENSE +20 -0
- package/README.md +532 -2
- package/build.config.json +28 -0
- package/dist/airich/airich.d.ts +180 -0
- package/dist/airich/airich.js +548 -0
- package/dist/airich/context.d.ts +24 -0
- package/dist/airich/context.js +48 -0
- package/dist/airich/encode.d.ts +3 -0
- package/dist/airich/encode.js +16 -0
- package/dist/airich/index.d.ts +20 -0
- package/dist/airich/index.js +11 -0
- package/dist/airich/inline.d.ts +45 -0
- package/dist/airich/inline.js +152 -0
- package/dist/airich/nodes.d.ts +30 -0
- package/dist/airich/nodes.js +121 -0
- package/dist/airich/primitives.d.ts +144 -0
- package/dist/airich/primitives.js +230 -0
- package/dist/airich/protocol.d.ts +35 -0
- package/dist/airich/protocol.js +65 -0
- package/dist/airich/table.d.ts +22 -0
- package/dist/airich/table.js +35 -0
- package/dist/airich/tokenizer.d.ts +13 -0
- package/dist/airich/tokenizer.js +717 -0
- package/dist/airich/transport.d.ts +15 -0
- package/dist/airich/transport.js +56 -0
- package/dist/branding.d.ts +5 -0
- package/dist/branding.js +9 -0
- package/dist/btn/builders.d.ts +246 -0
- package/dist/btn/builders.js +710 -0
- package/dist/btn/media-slot.d.ts +28 -0
- package/dist/btn/media-slot.js +323 -0
- package/dist/btn/media-source.d.ts +38 -0
- package/dist/btn/media-source.js +116 -0
- package/dist/btn/sharp-loader.d.ts +5 -0
- package/dist/btn/sharp-loader.js +18 -0
- package/dist/btn/thumbnail.d.ts +21 -0
- package/dist/btn/thumbnail.js +124 -0
- package/dist/btn/validators.d.ts +40 -0
- package/dist/btn/validators.js +135 -0
- package/dist/btn.d.ts +1 -0
- package/dist/btn.js +1 -0
- package/dist/core/async-send.d.ts +20 -0
- package/dist/core/async-send.js +1 -0
- package/dist/core/base-builder.d.ts +20 -0
- package/dist/core/base-builder.js +67 -0
- package/dist/core/errors.d.ts +30 -0
- package/dist/core/errors.js +61 -0
- package/dist/download.d.ts +24 -0
- package/dist/download.js +123 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +22 -0
- package/dist/integration/augment.d.ts +97 -0
- package/dist/integration/augment.js +1 -0
- package/dist/integration/auto.d.ts +8 -0
- package/dist/integration/auto.js +79 -0
- package/dist/integration/index.d.ts +4 -0
- package/dist/integration/index.js +2 -0
- package/dist/integration/methods.d.ts +25 -0
- package/dist/integration/methods.js +24 -0
- package/dist/interactive.d.ts +24 -0
- package/dist/interactive.js +121 -0
- package/dist/media-gate.d.ts +99 -0
- package/dist/media-gate.js +322 -0
- package/dist/message/album.d.ts +33 -0
- package/dist/message/album.js +107 -0
- package/dist/message/contact.d.ts +29 -0
- package/dist/message/contact.js +123 -0
- package/dist/message/forward.d.ts +23 -0
- package/dist/message/forward.js +77 -0
- package/dist/message/index.d.ts +26 -0
- package/dist/message/index.js +13 -0
- package/dist/message/link-preview.d.ts +28 -0
- package/dist/message/link-preview.js +98 -0
- package/dist/message/location.d.ts +24 -0
- package/dist/message/location.js +94 -0
- package/dist/message/media.d.ts +90 -0
- package/dist/message/media.js +123 -0
- package/dist/message/poll.d.ts +24 -0
- package/dist/message/poll.js +98 -0
- package/dist/message/reaction.d.ts +20 -0
- package/dist/message/reaction.js +71 -0
- package/dist/message/sticker.d.ts +29 -0
- package/dist/message/sticker.js +44 -0
- package/dist/message/target.d.ts +18 -0
- package/dist/message/target.js +24 -0
- package/dist/message/text.d.ts +17 -0
- package/dist/message/text.js +29 -0
- package/dist/message/vcard.d.ts +9 -0
- package/dist/message/vcard.js +23 -0
- package/dist/message/webp.d.ts +6 -0
- package/dist/message/webp.js +57 -0
- package/dist/payloads.d.ts +180 -0
- package/dist/payloads.js +1 -0
- package/dist/serialize.d.ts +41 -0
- package/dist/serialize.js +93 -0
- package/dist/status.d.ts +3 -0
- package/dist/status.js +21 -0
- package/dist/zapo-client.d.ts +44 -0
- package/dist/zapo-client.js +1 -0
- package/examples/airich.ts +59 -0
- package/examples/buttons.ts +48 -0
- package/examples/client-methods.ts +58 -0
- package/examples/hybrid.ts +133 -0
- package/examples/serialize.ts +61 -0
- package/package.json +271 -4
- package/scripts/check-no-comments.mjs +101 -0
- package/scripts/compare-golden.mjs +461 -0
- package/scripts/lib-config.mjs +9 -0
- package/scripts/smoke-auto.mjs +75 -0
- package/scripts/test-external-consumer.mjs +316 -0
- package/scripts/verify-package.mjs +78 -0
- package/src/airich/airich.ts +970 -0
- package/src/airich/context.ts +96 -0
- package/src/airich/encode.ts +25 -0
- package/src/airich/index.ts +98 -0
- package/src/airich/inline.ts +272 -0
- package/src/airich/nodes.ts +174 -0
- package/src/airich/primitives.ts +474 -0
- package/src/airich/protocol.ts +133 -0
- package/src/airich/table.ts +70 -0
- package/src/airich/tokenizer.ts +799 -0
- package/src/airich/transport.ts +106 -0
- package/src/branding.ts +23 -0
- package/src/btn/builders.ts +1073 -0
- package/src/btn/media-slot.ts +486 -0
- package/src/btn/media-source.ts +189 -0
- package/src/btn/sharp-loader.ts +23 -0
- package/src/btn/thumbnail.ts +182 -0
- package/src/btn/validators.ts +237 -0
- package/src/btn.ts +8 -0
- package/src/core/async-send.ts +28 -0
- package/src/core/base-builder.ts +104 -0
- package/src/core/errors.ts +82 -0
- package/src/download.ts +212 -0
- package/src/index.ts +229 -0
- package/src/integration/augment.ts +262 -0
- package/src/integration/auto.ts +103 -0
- package/src/integration/index.ts +8 -0
- package/src/integration/methods.ts +45 -0
- package/src/interactive.ts +188 -0
- package/src/media-gate.ts +499 -0
- package/src/message/album.ts +187 -0
- package/src/message/contact.ts +170 -0
- package/src/message/forward.ts +123 -0
- package/src/message/index.ts +83 -0
- package/src/message/link-preview.ts +148 -0
- package/src/message/location.ts +150 -0
- package/src/message/media.ts +323 -0
- package/src/message/poll.ts +158 -0
- package/src/message/reaction.ts +133 -0
- package/src/message/sticker.ts +100 -0
- package/src/message/target.ts +38 -0
- package/src/message/text.ts +61 -0
- package/src/message/vcard.ts +36 -0
- package/src/message/webp.ts +78 -0
- package/src/payloads.ts +204 -0
- package/src/serialize.ts +180 -0
- package/src/status.ts +29 -0
- package/src/zapo-client.ts +84 -0
- package/test/airich-inline.test.ts +200 -0
- package/test/airich-primitives.test.ts +226 -0
- package/test/airich-tokenizer.test.ts +145 -0
- package/test/airich.golden.test.ts +406 -0
- package/test/airich.test.ts +1006 -0
- package/test/btn-button.test.ts +416 -0
- package/test/btn-header.test.ts +646 -0
- package/test/btn-legacy.test.ts +342 -0
- package/test/btn-lifecycle.test.ts +295 -0
- package/test/btn-list.test.ts +261 -0
- package/test/btn-media-source.test.ts +150 -0
- package/test/btn-thumbnail.test.ts +160 -0
- package/test/builder-compat.test.ts +264 -0
- package/test/helpers.ts +27 -0
- package/test/hybrid-airich.test.ts +345 -0
- package/test/hybrid-client.test.ts +239 -0
- package/test/hybrid-errors.test.ts +557 -0
- package/test/hybrid-interactive.test.ts +455 -0
- package/test/hybrid.golden.test.ts +349 -0
- package/test/hybrid.test.ts +479 -0
- package/test/integration-auto.test.ts +159 -0
- package/test/integration.fakeserver.test.ts +440 -0
- package/test/media-gate.test.ts +215 -0
- package/test/message-album.test.ts +145 -0
- package/test/message-builders.test.ts +401 -0
- package/test/message-media.test.ts +303 -0
- package/test/message.fakeserver.test.ts +284 -0
- package/test/message.golden.test.ts +254 -0
- package/test/serializer.test.ts +448 -0
- package/tsconfig.json +26 -0
- package/tsconfig.test.json +10 -0
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import sharp from "sharp";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
text,
|
|
7
|
+
linkPreview,
|
|
8
|
+
reaction,
|
|
9
|
+
forward,
|
|
10
|
+
contact,
|
|
11
|
+
poll,
|
|
12
|
+
media,
|
|
13
|
+
image,
|
|
14
|
+
video,
|
|
15
|
+
audio,
|
|
16
|
+
document,
|
|
17
|
+
sticker,
|
|
18
|
+
album,
|
|
19
|
+
location,
|
|
20
|
+
type ZapoClient,
|
|
21
|
+
} from "@kyyinfinite/zapo-message";
|
|
22
|
+
import { asClient } from "./helpers.js";
|
|
23
|
+
|
|
24
|
+
const GOLDEN_UPLOAD = {
|
|
25
|
+
url: "https://mmg.whatsapp.net/v/t62.7118-24/golden",
|
|
26
|
+
directPath: "/v/t62.7118-24/golden",
|
|
27
|
+
mediaKey: new Uint8Array(32).fill(1),
|
|
28
|
+
fileSha256: new Uint8Array(32).fill(2),
|
|
29
|
+
fileEncSha256: new Uint8Array(32).fill(3),
|
|
30
|
+
fileLength: 1000,
|
|
31
|
+
mediaKeyTimestamp: 1730000000,
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
interface CapturedSend {
|
|
35
|
+
to: unknown;
|
|
36
|
+
content: unknown;
|
|
37
|
+
options: unknown;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const captureClient = () => {
|
|
41
|
+
const sends: CapturedSend[] = [];
|
|
42
|
+
const uploads: { source: unknown; options: unknown }[] = [];
|
|
43
|
+
const client = asClient({
|
|
44
|
+
message: {
|
|
45
|
+
upload: async (source: unknown, options: unknown) => {
|
|
46
|
+
uploads.push({ source, options });
|
|
47
|
+
return GOLDEN_UPLOAD;
|
|
48
|
+
},
|
|
49
|
+
send: async (to: unknown, content: unknown, options: unknown) => {
|
|
50
|
+
sends.push({ to, content, options });
|
|
51
|
+
return { id: `ID-${sends.length}`, attempts: 1, ack: {} };
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
return { client, sends, uploads };
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const PNG_BYTES = await sharp({
|
|
59
|
+
create: { width: 120, height: 90, channels: 3, background: "#3060ff" },
|
|
60
|
+
})
|
|
61
|
+
.png()
|
|
62
|
+
.toBuffer();
|
|
63
|
+
|
|
64
|
+
const VIDEO_BYTES = Buffer.from("videofixturebytes");
|
|
65
|
+
const AUDIO_BYTES = Buffer.from("audiofixturebytes");
|
|
66
|
+
const DOC_BYTES = Buffer.from("documentfixturebytes");
|
|
67
|
+
|
|
68
|
+
const WEBP_BYTES = Buffer.concat([
|
|
69
|
+
Buffer.from([0x52, 0x49, 0x46, 0x46, 0x24, 0x00, 0x00, 0x00]),
|
|
70
|
+
Buffer.from("WEBPVP8 "),
|
|
71
|
+
Buffer.alloc(24, 1),
|
|
72
|
+
]);
|
|
73
|
+
|
|
74
|
+
const INCOMING = {
|
|
75
|
+
key: { remoteJid: "628111@s.whatsapp.net", id: "MSG-1", fromMe: false },
|
|
76
|
+
message: { conversation: "halo" },
|
|
77
|
+
rawNode: {},
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const plain = (value: unknown): unknown =>
|
|
81
|
+
JSON.parse(
|
|
82
|
+
JSON.stringify(value, (_key, raw) => {
|
|
83
|
+
if (raw instanceof Uint8Array) return { $bytes: raw.byteLength };
|
|
84
|
+
if (
|
|
85
|
+
raw &&
|
|
86
|
+
typeof raw === "object" &&
|
|
87
|
+
!Array.isArray(raw) &&
|
|
88
|
+
(raw as { type?: unknown }).type === "Buffer" &&
|
|
89
|
+
Array.isArray((raw as { data?: unknown }).data)
|
|
90
|
+
) {
|
|
91
|
+
return { $bytes: (raw as { data: number[] }).data.length };
|
|
92
|
+
}
|
|
93
|
+
return raw;
|
|
94
|
+
}),
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
const assertSameSend = (
|
|
98
|
+
a: CapturedSend | undefined,
|
|
99
|
+
b: CapturedSend | undefined,
|
|
100
|
+
label: string,
|
|
101
|
+
): void => {
|
|
102
|
+
assert.ok(a, `async send tercatat: ${label}`);
|
|
103
|
+
assert.ok(b, `chain send tercatat: ${label}`);
|
|
104
|
+
assert.equal(a.to, b.to, `${label}: tujuan sama`);
|
|
105
|
+
assert.deepEqual(
|
|
106
|
+
plain(a.content),
|
|
107
|
+
plain(b.content),
|
|
108
|
+
`${label}: payload sama`,
|
|
109
|
+
);
|
|
110
|
+
assert.deepEqual(a.options, b.options, `${label}: opsi sama`);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
test("hybrid text: async dan chain menghasilkan payload identik", async () => {
|
|
114
|
+
const asyncCap = captureClient();
|
|
115
|
+
const chainCap = captureClient();
|
|
116
|
+
await text(asyncCap.client, "6281@s.whatsapp.net", { body: "Halo Dunia" });
|
|
117
|
+
await text(chainCap.client)
|
|
118
|
+
.to("6281@s.whatsapp.net")
|
|
119
|
+
.body("Halo Dunia")
|
|
120
|
+
.send();
|
|
121
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "text");
|
|
122
|
+
assert.deepEqual(plain(asyncCap.sends[0]?.content), {
|
|
123
|
+
conversation: "Halo Dunia",
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
test("hybrid text: opsi send mengalir identik", async () => {
|
|
128
|
+
const asyncCap = captureClient();
|
|
129
|
+
const chainCap = captureClient();
|
|
130
|
+
const options = { quote: INCOMING, mentions: ["6281@s.whatsapp.net"] };
|
|
131
|
+
await text(
|
|
132
|
+
asyncCap.client,
|
|
133
|
+
"6281@s.whatsapp.net",
|
|
134
|
+
{ body: "balasan" },
|
|
135
|
+
options,
|
|
136
|
+
);
|
|
137
|
+
await text(chainCap.client)
|
|
138
|
+
.body("balasan")
|
|
139
|
+
.send("6281@s.whatsapp.net", options);
|
|
140
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "text+options");
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("hybrid linkPreview: async dan chain identik", async () => {
|
|
144
|
+
const config = {
|
|
145
|
+
body: "Baca https://example.com/docs ya",
|
|
146
|
+
title: "Dokumentasi",
|
|
147
|
+
description: "Semua yang perlu diketahui",
|
|
148
|
+
kind: "image" as const,
|
|
149
|
+
};
|
|
150
|
+
const asyncCap = captureClient();
|
|
151
|
+
const chainCap = captureClient();
|
|
152
|
+
await linkPreview(asyncCap.client, "6281@s.whatsapp.net", config);
|
|
153
|
+
await linkPreview(chainCap.client)
|
|
154
|
+
.body(config.body)
|
|
155
|
+
.title(config.title)
|
|
156
|
+
.description(config.description)
|
|
157
|
+
.kind(config.kind)
|
|
158
|
+
.send("6281@s.whatsapp.net");
|
|
159
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "linkPreview");
|
|
160
|
+
const payload = plain(asyncCap.sends[0]?.content) as Record<string, unknown>;
|
|
161
|
+
const extended = payload.extendedTextMessage as Record<string, unknown>;
|
|
162
|
+
assert.equal(extended.matchedText, "https://example.com/docs");
|
|
163
|
+
assert.equal(extended.title, "Dokumentasi");
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
test("hybrid contact: async dan chain identik", async () => {
|
|
167
|
+
const config = {
|
|
168
|
+
name: "Rani",
|
|
169
|
+
phone: "+6281298765432",
|
|
170
|
+
organization: "PT Contoh",
|
|
171
|
+
email: "rani@contoh.id",
|
|
172
|
+
};
|
|
173
|
+
const asyncCap = captureClient();
|
|
174
|
+
const chainCap = captureClient();
|
|
175
|
+
await contact(asyncCap.client, "6281@s.whatsapp.net", config);
|
|
176
|
+
await contact(chainCap.client)
|
|
177
|
+
.name(config.name)
|
|
178
|
+
.phone(config.phone)
|
|
179
|
+
.organization(config.organization)
|
|
180
|
+
.email(config.email)
|
|
181
|
+
.send("6281@s.whatsapp.net");
|
|
182
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "contact");
|
|
183
|
+
const payload = plain(asyncCap.sends[0]?.content) as {
|
|
184
|
+
contactMessage: { displayName: string; vcard: string };
|
|
185
|
+
};
|
|
186
|
+
assert.equal(payload.contactMessage.displayName, "Rani");
|
|
187
|
+
assert.match(payload.contactMessage.vcard, /FN:Rani/);
|
|
188
|
+
assert.match(payload.contactMessage.vcard, /ORG:PT Contoh/);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test("hybrid poll: async dan chain identik (v3 dan v6)", async () => {
|
|
192
|
+
const asyncCap = captureClient();
|
|
193
|
+
const chainCap = captureClient();
|
|
194
|
+
await poll(asyncCap.client, "6281@s.whatsapp.net", {
|
|
195
|
+
question: "Makan apa?",
|
|
196
|
+
options: ["Nasi goreng", "Mie ayam"],
|
|
197
|
+
});
|
|
198
|
+
await poll(chainCap.client)
|
|
199
|
+
.question("Makan apa?")
|
|
200
|
+
.options(["Nasi goreng", "Mie ayam"])
|
|
201
|
+
.send("6281@s.whatsapp.net");
|
|
202
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "poll v3");
|
|
203
|
+
assert.deepEqual(plain(asyncCap.sends[0]?.content), {
|
|
204
|
+
type: "poll",
|
|
205
|
+
name: "Makan apa?",
|
|
206
|
+
options: ["Nasi goreng", "Mie ayam"],
|
|
207
|
+
selectableCount: 1,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
const asyncCap2 = captureClient();
|
|
211
|
+
const chainCap2 = captureClient();
|
|
212
|
+
await poll(asyncCap2.client, "6281@s.whatsapp.net", {
|
|
213
|
+
question: "Pilih dua",
|
|
214
|
+
options: ["A", "B", "C"],
|
|
215
|
+
selectableCount: 2,
|
|
216
|
+
allowAddOption: true,
|
|
217
|
+
hideParticipantName: true,
|
|
218
|
+
});
|
|
219
|
+
await poll(chainCap2.client)
|
|
220
|
+
.question("Pilih dua")
|
|
221
|
+
.options(["A", "B", "C"])
|
|
222
|
+
.selectableCount(2)
|
|
223
|
+
.allowAddOption()
|
|
224
|
+
.hideParticipantName()
|
|
225
|
+
.send("6281@s.whatsapp.net");
|
|
226
|
+
assertSameSend(asyncCap2.sends[0], chainCap2.sends[0], "poll v6");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
test("hybrid reaction: config-first async dan chain identik", async () => {
|
|
230
|
+
const asyncCap = captureClient();
|
|
231
|
+
const chainCap = captureClient();
|
|
232
|
+
await reaction(asyncCap.client, { target: INCOMING, emoji: "🔥" });
|
|
233
|
+
await reaction(chainCap.client).target(INCOMING).emoji("🔥").send();
|
|
234
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "reaction");
|
|
235
|
+
assert.equal(asyncCap.sends[0]?.to, "628111@s.whatsapp.net");
|
|
236
|
+
assert.deepEqual(plain(asyncCap.sends[0]?.content), {
|
|
237
|
+
type: "reaction",
|
|
238
|
+
target: INCOMING,
|
|
239
|
+
emoji: "🔥",
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
test("hybrid reaction: jid eksplisit async dan chain identik", async () => {
|
|
244
|
+
const asyncCap = captureClient();
|
|
245
|
+
const chainCap = captureClient();
|
|
246
|
+
await reaction(asyncCap.client, "6281@s.whatsapp.net", {
|
|
247
|
+
target: INCOMING,
|
|
248
|
+
emoji: "",
|
|
249
|
+
});
|
|
250
|
+
await reaction(chainCap.client)
|
|
251
|
+
.target(INCOMING)
|
|
252
|
+
.emoji("")
|
|
253
|
+
.send("6281@s.whatsapp.net");
|
|
254
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "reaction+jid");
|
|
255
|
+
assert.equal(asyncCap.sends[0]?.to, "6281@s.whatsapp.net");
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("hybrid forward: async dan chain identik (skor diteruskan)", async () => {
|
|
259
|
+
const forwarded = {
|
|
260
|
+
key: { remoteJid: "628111@s.whatsapp.net", id: "MSG-9", fromMe: false },
|
|
261
|
+
message: {
|
|
262
|
+
extendedTextMessage: {
|
|
263
|
+
text: "pesan asal",
|
|
264
|
+
contextInfo: { forwardingScore: 3, isForwarded: true },
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
rawNode: {},
|
|
268
|
+
};
|
|
269
|
+
const asyncCap = captureClient();
|
|
270
|
+
const chainCap = captureClient();
|
|
271
|
+
await forward(asyncCap.client, "6282@s.whatsapp.net", {
|
|
272
|
+
message: forwarded,
|
|
273
|
+
});
|
|
274
|
+
await forward(chainCap.client).message(forwarded).send("6282@s.whatsapp.net");
|
|
275
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "forward");
|
|
276
|
+
assert.deepEqual(
|
|
277
|
+
(asyncCap.sends[0]?.options as Record<string, unknown>).forward,
|
|
278
|
+
{ score: 3 },
|
|
279
|
+
);
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
test("hybrid media: seluruh jenis async dan chain identik", async () => {
|
|
283
|
+
const cases = [
|
|
284
|
+
{
|
|
285
|
+
label: "image",
|
|
286
|
+
config: { type: "image" as const, source: PNG_BYTES, caption: "Foto" },
|
|
287
|
+
chain: (client: ZapoClient, to: string) =>
|
|
288
|
+
media(client).image(PNG_BYTES, { caption: "Foto" }).send(to),
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
label: "video",
|
|
292
|
+
config: {
|
|
293
|
+
type: "video" as const,
|
|
294
|
+
source: VIDEO_BYTES,
|
|
295
|
+
mimetype: "video/mp4",
|
|
296
|
+
seconds: 30,
|
|
297
|
+
},
|
|
298
|
+
chain: (client: ZapoClient, to: string) =>
|
|
299
|
+
media(client)
|
|
300
|
+
.video(VIDEO_BYTES, { mimetype: "video/mp4", seconds: 30 })
|
|
301
|
+
.send(to),
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
label: "audio",
|
|
305
|
+
config: {
|
|
306
|
+
type: "audio" as const,
|
|
307
|
+
source: AUDIO_BYTES,
|
|
308
|
+
ptt: true,
|
|
309
|
+
mimetype: "audio/ogg; codecs=opus",
|
|
310
|
+
},
|
|
311
|
+
chain: (client: ZapoClient, to: string) =>
|
|
312
|
+
media(client)
|
|
313
|
+
.audio(AUDIO_BYTES, {
|
|
314
|
+
ptt: true,
|
|
315
|
+
mimetype: "audio/ogg; codecs=opus",
|
|
316
|
+
})
|
|
317
|
+
.send(to),
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
label: "document",
|
|
321
|
+
config: {
|
|
322
|
+
type: "document" as const,
|
|
323
|
+
source: DOC_BYTES,
|
|
324
|
+
fileName: "laporan.pdf",
|
|
325
|
+
title: "Laporan",
|
|
326
|
+
},
|
|
327
|
+
chain: (client: ZapoClient, to: string) =>
|
|
328
|
+
media(client)
|
|
329
|
+
.document(DOC_BYTES, {
|
|
330
|
+
fileName: "laporan.pdf",
|
|
331
|
+
title: "Laporan",
|
|
332
|
+
})
|
|
333
|
+
.send(to),
|
|
334
|
+
},
|
|
335
|
+
];
|
|
336
|
+
|
|
337
|
+
for (const { label, config, chain } of cases) {
|
|
338
|
+
const asyncCap = captureClient();
|
|
339
|
+
const chainCap = captureClient();
|
|
340
|
+
await media(asyncCap.client, "6281@s.whatsapp.net", config);
|
|
341
|
+
await chain(chainCap.client, "6281@s.whatsapp.net");
|
|
342
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], `media ${label}`);
|
|
343
|
+
assert.equal(asyncCap.uploads.length, 1, `media ${label}: satu upload`);
|
|
344
|
+
const payload = plain(asyncCap.sends[0]?.content) as Record<
|
|
345
|
+
string,
|
|
346
|
+
Record<string, unknown>
|
|
347
|
+
>;
|
|
348
|
+
const field = `${config.type}Message`;
|
|
349
|
+
assert.ok(payload[field], `media ${label}: field ${field}`);
|
|
350
|
+
assert.equal(payload[field]?.url, GOLDEN_UPLOAD.url);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
test("hybrid specialized: image/video/audio/document facade satu pipeline", async () => {
|
|
355
|
+
const asyncCap = captureClient();
|
|
356
|
+
const chainCap = captureClient();
|
|
357
|
+
await image(asyncCap.client, "6281@s.whatsapp.net", {
|
|
358
|
+
source: PNG_BYTES,
|
|
359
|
+
caption: "Fasad",
|
|
360
|
+
});
|
|
361
|
+
await image(chainCap.client)
|
|
362
|
+
.image(PNG_BYTES, { caption: "Fasad" })
|
|
363
|
+
.send("6281@s.whatsapp.net");
|
|
364
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "image facade");
|
|
365
|
+
|
|
366
|
+
const videoCap = captureClient();
|
|
367
|
+
await video(videoCap.client, "6281@s.whatsapp.net", {
|
|
368
|
+
source: VIDEO_BYTES,
|
|
369
|
+
mimetype: "video/mp4",
|
|
370
|
+
});
|
|
371
|
+
assert.ok(videoCap.sends[0]);
|
|
372
|
+
assert.ok(
|
|
373
|
+
(plain(videoCap.sends[0]?.content) as Record<string, unknown>).videoMessage,
|
|
374
|
+
);
|
|
375
|
+
|
|
376
|
+
const audioCap = captureClient();
|
|
377
|
+
await audio(audioCap.client, "6281@s.whatsapp.net", {
|
|
378
|
+
source: AUDIO_BYTES,
|
|
379
|
+
ptt: true,
|
|
380
|
+
mimetype: "audio/ogg",
|
|
381
|
+
});
|
|
382
|
+
assert.ok(
|
|
383
|
+
(plain(audioCap.sends[0]?.content) as Record<string, unknown>).audioMessage,
|
|
384
|
+
);
|
|
385
|
+
|
|
386
|
+
const documentCap = captureClient();
|
|
387
|
+
await document(documentCap.client, "6281@s.whatsapp.net", {
|
|
388
|
+
source: DOC_BYTES,
|
|
389
|
+
fileName: "nota.txt",
|
|
390
|
+
});
|
|
391
|
+
assert.ok(
|
|
392
|
+
(plain(documentCap.sends[0]?.content) as Record<string, unknown>)
|
|
393
|
+
.documentMessage,
|
|
394
|
+
);
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
test("hybrid sticker: async dan chain identik", async () => {
|
|
398
|
+
const asyncCap = captureClient();
|
|
399
|
+
const chainCap = captureClient();
|
|
400
|
+
await sticker(asyncCap.client, "6281@s.whatsapp.net", {
|
|
401
|
+
source: WEBP_BYTES,
|
|
402
|
+
});
|
|
403
|
+
await sticker(chainCap.client).source(WEBP_BYTES).send("6281@s.whatsapp.net");
|
|
404
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "sticker");
|
|
405
|
+
const payload = plain(asyncCap.sends[0]?.content) as {
|
|
406
|
+
stickerMessage: Record<string, unknown>;
|
|
407
|
+
};
|
|
408
|
+
assert.equal(payload.stickerMessage.mimetype, "image/webp");
|
|
409
|
+
assert.equal(payload.stickerMessage.isAnimated, false);
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
test("hybrid album: async dan chain identik (album + anak mengutip id)", async () => {
|
|
413
|
+
const asyncCap = captureClient();
|
|
414
|
+
const chainCap = captureClient();
|
|
415
|
+
const items = [
|
|
416
|
+
{ type: "image" as const, source: PNG_BYTES },
|
|
417
|
+
{ type: "image" as const, source: PNG_BYTES },
|
|
418
|
+
{
|
|
419
|
+
type: "video" as const,
|
|
420
|
+
source: VIDEO_BYTES,
|
|
421
|
+
options: { mimetype: "video/mp4" },
|
|
422
|
+
},
|
|
423
|
+
];
|
|
424
|
+
await album(asyncCap.client, "6281@s.whatsapp.net", { media: items });
|
|
425
|
+
await album(chainCap.client)
|
|
426
|
+
.image(PNG_BYTES)
|
|
427
|
+
.image(PNG_BYTES)
|
|
428
|
+
.video(VIDEO_BYTES, { mimetype: "video/mp4" })
|
|
429
|
+
.send("6281@s.whatsapp.net");
|
|
430
|
+
|
|
431
|
+
assert.equal(asyncCap.sends.length, 4, "album async: 1 album + 3 anak");
|
|
432
|
+
assert.equal(chainCap.sends.length, 4, "album chain: 1 album + 3 anak");
|
|
433
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "album header");
|
|
434
|
+
for (let i = 1; i < 4; i += 1) {
|
|
435
|
+
assertSameSend(asyncCap.sends[i], chainCap.sends[i], `album anak ${i}`);
|
|
436
|
+
const quote = (asyncCap.sends[i]?.options as Record<string, unknown>)
|
|
437
|
+
.quote as Record<string, unknown>;
|
|
438
|
+
assert.equal(quote.id, "ID-1");
|
|
439
|
+
assert.equal(quote.remoteJid, "6281@s.whatsapp.net");
|
|
440
|
+
assert.equal(quote.fromMe, true);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
test("hybrid location: async dan chain identik", async () => {
|
|
445
|
+
const asyncCap = captureClient();
|
|
446
|
+
const chainCap = captureClient();
|
|
447
|
+
await location(asyncCap.client, "6281@s.whatsapp.net", {
|
|
448
|
+
latitude: -6.2,
|
|
449
|
+
longitude: 106.8,
|
|
450
|
+
name: "Jakarta",
|
|
451
|
+
address: "Indonesia",
|
|
452
|
+
});
|
|
453
|
+
await location(chainCap.client)
|
|
454
|
+
.to("6281@s.whatsapp.net")
|
|
455
|
+
.coordinates(-6.2, 106.8)
|
|
456
|
+
.name("Jakarta")
|
|
457
|
+
.address("Indonesia")
|
|
458
|
+
.send();
|
|
459
|
+
assertSameSend(asyncCap.sends[0], chainCap.sends[0], "location");
|
|
460
|
+
assert.deepEqual(plain(asyncCap.sends[0]?.content), {
|
|
461
|
+
locationMessage: {
|
|
462
|
+
degreesLatitude: -6.2,
|
|
463
|
+
degreesLongitude: 106.8,
|
|
464
|
+
name: "Jakarta",
|
|
465
|
+
address: "Indonesia",
|
|
466
|
+
},
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
test("hybrid location: golden payload tanpa nama/alamat", async () => {
|
|
471
|
+
const cap = captureClient();
|
|
472
|
+
await location(cap.client, "6281@s.whatsapp.net", {
|
|
473
|
+
latitude: 0,
|
|
474
|
+
longitude: 0,
|
|
475
|
+
});
|
|
476
|
+
assert.deepEqual(plain(cap.sends[0]?.content), {
|
|
477
|
+
locationMessage: { degreesLatitude: 0, degreesLongitude: 0 },
|
|
478
|
+
});
|
|
479
|
+
});
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { createStore, WaClient } from "zapo-js";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
autoInstallStatus,
|
|
7
|
+
installOnPrototype,
|
|
8
|
+
installZapoMessage,
|
|
9
|
+
ZAPO_MESSAGE_METHOD_NAMES,
|
|
10
|
+
zapoMessageMethods,
|
|
11
|
+
text,
|
|
12
|
+
isClient,
|
|
13
|
+
} from "@kyyinfinite/zapo-message";
|
|
14
|
+
import { asClient } from "./helpers.js";
|
|
15
|
+
|
|
16
|
+
const makeWaClient = () =>
|
|
17
|
+
new WaClient({ store: createStore({}), sessionId: `auto-test` });
|
|
18
|
+
|
|
19
|
+
test("auto: import side-effect memasang semua method di WaClient", () => {
|
|
20
|
+
const client = makeWaClient();
|
|
21
|
+
for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
|
|
22
|
+
assert.equal(
|
|
23
|
+
typeof (client as unknown as Record<string, unknown>)[name],
|
|
24
|
+
"function",
|
|
25
|
+
`client.${name} harus terpasang`,
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
assert.equal(autoInstallStatus().esm, true);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("auto: method terpasang di prototype, tidak enumerasi", () => {
|
|
32
|
+
const client = makeWaClient();
|
|
33
|
+
const own = Object.getOwnPropertyNames(client);
|
|
34
|
+
for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
|
|
35
|
+
assert.equal(own.includes(name), false, `${name} bukan properti instance`);
|
|
36
|
+
}
|
|
37
|
+
const descriptor = Object.getOwnPropertyDescriptor(
|
|
38
|
+
Object.getPrototypeOf(client),
|
|
39
|
+
"text",
|
|
40
|
+
);
|
|
41
|
+
assert.ok(descriptor);
|
|
42
|
+
assert.equal(descriptor?.enumerable, false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("auto: beberapa client sekaligus mendapat method", () => {
|
|
46
|
+
const a = makeWaClient();
|
|
47
|
+
const b = makeWaClient();
|
|
48
|
+
const payloadA = a.text().body("A").build();
|
|
49
|
+
const payloadB = b.text().body("B").build();
|
|
50
|
+
assert.deepEqual(payloadA, { conversation: "A" });
|
|
51
|
+
assert.deepEqual(payloadB, { conversation: "B" });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
test("auto: method bawaan ZapoJS tidak tertimpa", () => {
|
|
55
|
+
const client = makeWaClient();
|
|
56
|
+
assert.equal(typeof client.message, "object");
|
|
57
|
+
assert.equal(typeof client.message.send, "function");
|
|
58
|
+
assert.equal(typeof client.connect, "function");
|
|
59
|
+
assert.equal(typeof client.disconnect, "function");
|
|
60
|
+
assert.equal(typeof client.on, "function");
|
|
61
|
+
|
|
62
|
+
const withNative = {
|
|
63
|
+
text() {
|
|
64
|
+
return "asli";
|
|
65
|
+
},
|
|
66
|
+
message: { send: async () => ({}) },
|
|
67
|
+
};
|
|
68
|
+
const fake = Object.create(withNative);
|
|
69
|
+
installOnPrototype(withNative);
|
|
70
|
+
assert.equal(fake.text(), "asli", "method native tidak tertimpa");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("auto: installOnPrototype idempoten", () => {
|
|
74
|
+
const base = { message: { send: async () => ({}) } };
|
|
75
|
+
assert.equal(installOnPrototype(base), true);
|
|
76
|
+
const first = (base as unknown as Record<string, unknown>).text;
|
|
77
|
+
assert.equal(installOnPrototype(base), false, "pemasangan kedua ditolak");
|
|
78
|
+
assert.equal(
|
|
79
|
+
(base as unknown as Record<string, unknown>).text,
|
|
80
|
+
first,
|
|
81
|
+
"fungsi tidak diganti",
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("auto: prototype tidak extensible ditolak tanpa error", () => {
|
|
86
|
+
const frozen = Object.preventExtensions({
|
|
87
|
+
message: { send: async () => ({}) },
|
|
88
|
+
});
|
|
89
|
+
assert.equal(installOnPrototype(frozen), false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test("auto: installZapoMessage untuk instance manual", () => {
|
|
93
|
+
const duck = asClient({ message: { send: async () => ({}) } });
|
|
94
|
+
const installed = installZapoMessage(duck);
|
|
95
|
+
assert.equal(installed, duck);
|
|
96
|
+
for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
|
|
97
|
+
assert.equal(
|
|
98
|
+
typeof (duck as unknown as Record<string, unknown>)[name],
|
|
99
|
+
"function",
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
assert.equal(installZapoMessage(duck), duck, "instalasi ulang aman");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
interface DuckClientWithText {
|
|
106
|
+
message: { send: () => Promise<unknown> };
|
|
107
|
+
text(): { body(v: string): { build(): unknown } };
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
test("auto: installZapoMessage menolak non-client", () => {
|
|
111
|
+
assert.throws(() => installZapoMessage({} as never), /WaClient/);
|
|
112
|
+
assert.throws(() => installZapoMessage({ message: {} } as never), /WaClient/);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
test("auto: properti instance sendiri tidak tertimpa", () => {
|
|
116
|
+
const duck = asClient({ message: { send: async () => ({}) } });
|
|
117
|
+
const typed = duck as unknown as Record<string, () => string>;
|
|
118
|
+
typed.text = () => "punya user";
|
|
119
|
+
installZapoMessage(duck);
|
|
120
|
+
assert.equal(typed.text(), "punya user");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("auto: method bawaan client tetap berfungsi setelah patch", async () => {
|
|
124
|
+
const client = makeWaClient();
|
|
125
|
+
const events: unknown[] = [];
|
|
126
|
+
client.on("connection", (event: unknown) => events.push(event));
|
|
127
|
+
client.emit("connection", { reason: "replaced" } as never);
|
|
128
|
+
assert.ok(events.length >= 1, "EventEmitter tetap berfungsi");
|
|
129
|
+
await client.disconnect().catch(() => {});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test("auto: factory manual dan method client menghasilkan builder sama", () => {
|
|
133
|
+
const duck = asClient({ message: { send: async () => ({}) } });
|
|
134
|
+
const viaFactory = text(duck).body("x").build();
|
|
135
|
+
installZapoMessage(duck);
|
|
136
|
+
const viaMethod = (duck as unknown as DuckClientWithText)
|
|
137
|
+
.text()
|
|
138
|
+
.body("x")
|
|
139
|
+
.build();
|
|
140
|
+
assert.deepEqual(viaFactory, viaMethod);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("auto: isClient tetap mengenali duck client", () => {
|
|
144
|
+
assert.equal(
|
|
145
|
+
isClient(asClient({ message: { send: async () => ({}) } })),
|
|
146
|
+
true,
|
|
147
|
+
);
|
|
148
|
+
assert.equal(isClient(null), false);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("auto: registry method selaras dengan nama yang dipasang", () => {
|
|
152
|
+
assert.deepEqual(
|
|
153
|
+
[...ZAPO_MESSAGE_METHOD_NAMES].sort(),
|
|
154
|
+
Object.keys(zapoMessageMethods).sort(),
|
|
155
|
+
);
|
|
156
|
+
for (const name of ZAPO_MESSAGE_METHOD_NAMES) {
|
|
157
|
+
assert.equal(typeof zapoMessageMethods[name], "function");
|
|
158
|
+
}
|
|
159
|
+
});
|