@kyyinfinite/zapo-message 0.0.0-stage → 1.0.1
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 +1401 -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 +36 -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,440 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import type { BinaryNode } from "zapo-js";
|
|
4
|
+
|
|
5
|
+
import { FakeWaServer } from "@zapo-js/fake-server";
|
|
6
|
+
import type { WaIncomingMessageEvent } from "zapo-js";
|
|
7
|
+
import { createStore, WaClient } from "zapo-js";
|
|
8
|
+
import {
|
|
9
|
+
airich,
|
|
10
|
+
decodeUnifiedResponse,
|
|
11
|
+
ZWSP,
|
|
12
|
+
button,
|
|
13
|
+
} from "@kyyinfinite/zapo-message";
|
|
14
|
+
import { L, A } from "./helpers.js";
|
|
15
|
+
|
|
16
|
+
const BOT_PN = "5511999999999";
|
|
17
|
+
const BOT_DEVICE_JID = `${BOT_PN}:1@s.whatsapp.net`;
|
|
18
|
+
const OWNER_JID = "5511988888888@s.whatsapp.net";
|
|
19
|
+
|
|
20
|
+
interface DecryptedMessage {
|
|
21
|
+
message: Record<string, unknown>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const plain = (value: unknown): unknown => JSON.parse(JSON.stringify(value));
|
|
25
|
+
|
|
26
|
+
type StartedFakeServer = Awaited<ReturnType<typeof FakeWaServer.start>>;
|
|
27
|
+
|
|
28
|
+
const createPairedClient = async (server: StartedFakeServer) => {
|
|
29
|
+
const store = createStore({});
|
|
30
|
+
|
|
31
|
+
const client = new WaClient({
|
|
32
|
+
store,
|
|
33
|
+
sessionId: "@kyyinfinite/zapo-message-integration",
|
|
34
|
+
chatSocketUrls: [server.url],
|
|
35
|
+
testHooks: { noiseRootCa: server.noiseRootCa },
|
|
36
|
+
proxy: {
|
|
37
|
+
mediaUpload: server.mediaProxyAgent,
|
|
38
|
+
mediaDownload: server.mediaProxyAgent,
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const pairingMaterial = new Promise<{
|
|
43
|
+
identityPublicKey: Uint8Array;
|
|
44
|
+
advSecretKey: Uint8Array;
|
|
45
|
+
}>((resolve) => {
|
|
46
|
+
void client.once("auth_qr", ({ qr }: { qr: string }) => {
|
|
47
|
+
const [, , identityPubB64, advSecretB64] = qr.split(",");
|
|
48
|
+
resolve({
|
|
49
|
+
identityPublicKey: new Uint8Array(
|
|
50
|
+
Buffer.from(identityPubB64 ?? "", "base64"),
|
|
51
|
+
),
|
|
52
|
+
advSecretKey: new Uint8Array(Buffer.from(advSecretB64 ?? "", "base64")),
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
await client.connect();
|
|
58
|
+
const pipeline = await server.waitForAuthenticatedPipeline();
|
|
59
|
+
const nextPipelineWaiter = server.waitForNextAuthenticatedPipeline(30_000);
|
|
60
|
+
await server.runPairing(
|
|
61
|
+
pipeline,
|
|
62
|
+
{ deviceJid: BOT_DEVICE_JID },
|
|
63
|
+
() => pairingMaterial,
|
|
64
|
+
);
|
|
65
|
+
const registeredPipeline = await nextPipelineWaiter;
|
|
66
|
+
|
|
67
|
+
return { client, registeredPipeline };
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
test(
|
|
71
|
+
"integration (fake-server): AIRich two-step, edit, dan direct send lewat transport Zapo nyata",
|
|
72
|
+
{ timeout: 120_000 },
|
|
73
|
+
async () => {
|
|
74
|
+
const server = await FakeWaServer.start();
|
|
75
|
+
let client: WaClient | null = null;
|
|
76
|
+
|
|
77
|
+
try {
|
|
78
|
+
const { client: waClient, registeredPipeline } =
|
|
79
|
+
await createPairedClient(server);
|
|
80
|
+
client = waClient;
|
|
81
|
+
|
|
82
|
+
void client.on("message", async (event: WaIncomingMessageEvent) => {
|
|
83
|
+
if (event.message?.conversation === "ping") {
|
|
84
|
+
await client!.message.send(event.key.remoteJid, "pong");
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
const peer = await server.createFakePeer(
|
|
89
|
+
{ jid: OWNER_JID, displayName: "Owner" },
|
|
90
|
+
registeredPipeline,
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
const session = server.sessionFor(registeredPipeline);
|
|
94
|
+
const captured: { node: BinaryNode; decrypted?: unknown }[] = [];
|
|
95
|
+
const stopCapture = session.onCapturedStanza((node: BinaryNode) => {
|
|
96
|
+
if (node.tag === "message") captured.push({ node });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
const decryptCaptured = async (): Promise<DecryptedMessage[]> => {
|
|
100
|
+
const received: DecryptedMessage[] = [];
|
|
101
|
+
for (const entry of captured) {
|
|
102
|
+
if (entry.decrypted !== undefined) continue;
|
|
103
|
+
const result = await peer.decryptStanza(entry.node).catch(() => null);
|
|
104
|
+
entry.decrypted = result;
|
|
105
|
+
if (result) received.push(result as unknown as DecryptedMessage);
|
|
106
|
+
}
|
|
107
|
+
return received;
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
await peer.sendConversation("ping");
|
|
111
|
+
const deadlinePong = Date.now() + 15_000;
|
|
112
|
+
let pong: DecryptedMessage | null = null;
|
|
113
|
+
while (Date.now() < deadlinePong && !pong) {
|
|
114
|
+
const found = await decryptCaptured();
|
|
115
|
+
pong =
|
|
116
|
+
found.find(
|
|
117
|
+
(r) =>
|
|
118
|
+
L(r.message).conversation === "pong" ||
|
|
119
|
+
L(L(r.message).extendedTextMessage).text === "pong",
|
|
120
|
+
) ?? null;
|
|
121
|
+
if (!pong) await new Promise((r) => setTimeout(r, 100));
|
|
122
|
+
}
|
|
123
|
+
assert.ok(
|
|
124
|
+
pong,
|
|
125
|
+
"sesi peer aktif: balasan ping->pong sampai dan terdekripsi",
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
const builder = airich(client);
|
|
129
|
+
builder.text("jawaban rich text", { id: "utama" });
|
|
130
|
+
|
|
131
|
+
const sent = (await builder.send(OWNER_JID)) as { id?: string };
|
|
132
|
+
assert.ok(sent?.id, "send() mengembalikan hasil placeholder");
|
|
133
|
+
assert.equal(sent.id, builder.lastId, "lastId terisi dari placeholder");
|
|
134
|
+
|
|
135
|
+
const deadlineTwoStep = Date.now() + 15_000;
|
|
136
|
+
let placeholder: DecryptedMessage | null = null;
|
|
137
|
+
let edit: DecryptedMessage | null = null;
|
|
138
|
+
while (Date.now() < deadlineTwoStep && !(placeholder && edit)) {
|
|
139
|
+
const found = await decryptCaptured();
|
|
140
|
+
if (!placeholder) {
|
|
141
|
+
placeholder =
|
|
142
|
+
found.find(
|
|
143
|
+
(r) =>
|
|
144
|
+
L(L(L(r.message).botForwardedMessage).message)
|
|
145
|
+
.richResponseMessage,
|
|
146
|
+
) ?? null;
|
|
147
|
+
}
|
|
148
|
+
if (!edit) {
|
|
149
|
+
edit =
|
|
150
|
+
found.find(
|
|
151
|
+
(r) =>
|
|
152
|
+
L(L(L(r.message).botForwardedMessage).message).protocolMessage,
|
|
153
|
+
) ?? null;
|
|
154
|
+
}
|
|
155
|
+
if (!(placeholder && edit))
|
|
156
|
+
await new Promise((r) => setTimeout(r, 100));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
assert.ok(placeholder, "placeholder sampai ke peer dan terdekripsi");
|
|
160
|
+
assert.ok(edit, "edit sampai ke peer dan terdekripsi");
|
|
161
|
+
const placeholderMsg = placeholder as DecryptedMessage;
|
|
162
|
+
const editMsg = edit as DecryptedMessage;
|
|
163
|
+
|
|
164
|
+
const placeholderMessageInfo = L(
|
|
165
|
+
placeholderMsg.message,
|
|
166
|
+
).messageContextInfo;
|
|
167
|
+
assert.ok(
|
|
168
|
+
placeholderMessageInfo,
|
|
169
|
+
"placeholder membawa messageContextInfo",
|
|
170
|
+
);
|
|
171
|
+
assert.equal(L(placeholderMessageInfo).deviceListMetadataVersion, 2);
|
|
172
|
+
assert.equal(
|
|
173
|
+
(L(placeholderMessageInfo).messageSecret as Uint8Array)?.length,
|
|
174
|
+
32,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const placeholderRich = L(
|
|
178
|
+
L(L(placeholderMsg.message).botForwardedMessage).message,
|
|
179
|
+
).richResponseMessage;
|
|
180
|
+
assert.equal(L(placeholderRich).messageType, 1);
|
|
181
|
+
assert.deepEqual(plain(L(placeholderRich).submessages), [
|
|
182
|
+
{ messageType: 2, messageText: ZWSP },
|
|
183
|
+
]);
|
|
184
|
+
|
|
185
|
+
const placeholderUnified = decodeUnifiedResponse(
|
|
186
|
+
L(L(placeholderRich).unifiedResponse).data as unknown as Uint8Array,
|
|
187
|
+
) as {
|
|
188
|
+
response_id: string;
|
|
189
|
+
sections: { view_model: { primitive: Record<string, unknown> } }[];
|
|
190
|
+
};
|
|
191
|
+
assert.match(placeholderUnified.response_id, /^[0-9a-f-]{36}$/);
|
|
192
|
+
assert.equal(
|
|
193
|
+
L(L(L(A(placeholderUnified.sections)[0]).view_model).primitive).text,
|
|
194
|
+
ZWSP,
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const editProto = L(
|
|
198
|
+
L(L(editMsg.message).botForwardedMessage).message,
|
|
199
|
+
).protocolMessage;
|
|
200
|
+
assert.equal(L(editProto).type, 14);
|
|
201
|
+
assert.equal(L(L(editProto).key).fromMe, true);
|
|
202
|
+
assert.equal(L(L(editProto).key).remoteJid, OWNER_JID);
|
|
203
|
+
assert.equal(
|
|
204
|
+
L(L(editProto).key).id,
|
|
205
|
+
sent.id,
|
|
206
|
+
"edit menunjuk id placeholder yang terkirim",
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
const editRich = L(
|
|
210
|
+
L(L(L(editProto).editedMessage).botForwardedMessage).message,
|
|
211
|
+
).richResponseMessage;
|
|
212
|
+
const editUnified = decodeUnifiedResponse(
|
|
213
|
+
L(L(editRich).unifiedResponse).data as unknown as Uint8Array,
|
|
214
|
+
) as {
|
|
215
|
+
response_id: string;
|
|
216
|
+
sections: { view_model: { primitive: Record<string, unknown> } }[];
|
|
217
|
+
};
|
|
218
|
+
assert.equal(
|
|
219
|
+
L(L(L(A(editUnified.sections)[0]).view_model).primitive).text,
|
|
220
|
+
"jawaban rich text",
|
|
221
|
+
);
|
|
222
|
+
assert.equal(
|
|
223
|
+
editUnified.response_id,
|
|
224
|
+
placeholderUnified.response_id,
|
|
225
|
+
"edit memakai response_id yang sama dengan placeholder (per-instance)",
|
|
226
|
+
);
|
|
227
|
+
|
|
228
|
+
assert.equal(builder.lastId, L(L(editProto).key).id);
|
|
229
|
+
assert.equal(builder.pendingEdit(OWNER_JID), null);
|
|
230
|
+
|
|
231
|
+
const directBuilder = airich(client);
|
|
232
|
+
directBuilder
|
|
233
|
+
.text("ini judul")
|
|
234
|
+
.table([
|
|
235
|
+
["Nama", "Nilai"],
|
|
236
|
+
["zapo-message", "100"],
|
|
237
|
+
])
|
|
238
|
+
.suggest(["lanjutkan", "ulangi"]);
|
|
239
|
+
|
|
240
|
+
const directSent = (await directBuilder.send(OWNER_JID, {
|
|
241
|
+
twoStep: false,
|
|
242
|
+
})) as { id?: string };
|
|
243
|
+
assert.ok(directSent?.id, "direct send mengembalikan id pesan");
|
|
244
|
+
|
|
245
|
+
const deadlineDirect = Date.now() + 15_000;
|
|
246
|
+
let direct: DecryptedMessage | null = null;
|
|
247
|
+
while (Date.now() < deadlineDirect && !direct) {
|
|
248
|
+
const found = await decryptCaptured();
|
|
249
|
+
direct =
|
|
250
|
+
found.find((r) => {
|
|
251
|
+
const message = L(L(L(r.message).botForwardedMessage).message);
|
|
252
|
+
return (
|
|
253
|
+
L(message).richResponseMessage && !L(message).protocolMessage
|
|
254
|
+
);
|
|
255
|
+
}) ?? null;
|
|
256
|
+
if (!direct) await new Promise((r) => setTimeout(r, 100));
|
|
257
|
+
}
|
|
258
|
+
assert.ok(direct, "direct send sampai ke peer dan terdekripsi");
|
|
259
|
+
const directMsg = direct as DecryptedMessage;
|
|
260
|
+
|
|
261
|
+
const directRich = L(
|
|
262
|
+
L(L(directMsg.message).botForwardedMessage).message,
|
|
263
|
+
).richResponseMessage;
|
|
264
|
+
assert.equal(L(directRich).messageType, 1);
|
|
265
|
+
assert.equal(
|
|
266
|
+
A(L(directRich).submessages).length,
|
|
267
|
+
2,
|
|
268
|
+
"text + table submessage",
|
|
269
|
+
);
|
|
270
|
+
assert.equal(L(A(L(directRich).submessages)[0]).messageType, 2);
|
|
271
|
+
assert.equal(L(A(L(directRich).submessages)[1]).messageType, 4);
|
|
272
|
+
const directRows = A(
|
|
273
|
+
L(L(L(A(L(directRich).submessages)[1]).tableMetadata)).rows,
|
|
274
|
+
);
|
|
275
|
+
assert.deepEqual(plain(directRows[0]), {
|
|
276
|
+
items: ["Nama", "Nilai"],
|
|
277
|
+
isHeading: true,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
const directContextInfo = L(L(directRich).contextInfo);
|
|
281
|
+
assert.equal(directContextInfo.forwardingScore, 1);
|
|
282
|
+
assert.equal(directContextInfo.isForwarded, true);
|
|
283
|
+
assert.equal(directContextInfo.forwardOrigin, 4);
|
|
284
|
+
assert.equal(
|
|
285
|
+
directContextInfo.forwardedAiBotMessageInfo ?? null,
|
|
286
|
+
null,
|
|
287
|
+
"tanpa botJid palsu",
|
|
288
|
+
);
|
|
289
|
+
|
|
290
|
+
const directUnified = decodeUnifiedResponse(
|
|
291
|
+
L(L(directRich).unifiedResponse).data as unknown as Uint8Array,
|
|
292
|
+
) as {
|
|
293
|
+
response_id: string;
|
|
294
|
+
sections: { view_model: { primitives?: Record<string, unknown>[] } }[];
|
|
295
|
+
};
|
|
296
|
+
assert.equal(
|
|
297
|
+
A(directUnified.sections).length,
|
|
298
|
+
3,
|
|
299
|
+
"text + table + suggestion",
|
|
300
|
+
);
|
|
301
|
+
assert.equal(
|
|
302
|
+
A(L(L(A(directUnified.sections)[2]).view_model).primitives).length,
|
|
303
|
+
2,
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
const secondBuilder = airich(client);
|
|
307
|
+
secondBuilder.text("payload kedua");
|
|
308
|
+
await secondBuilder.send(OWNER_JID, { twoStep: false });
|
|
309
|
+
|
|
310
|
+
const deadlineSecond = Date.now() + 15_000;
|
|
311
|
+
let second: DecryptedMessage | null = null;
|
|
312
|
+
while (Date.now() < deadlineSecond && !second) {
|
|
313
|
+
const found = await decryptCaptured();
|
|
314
|
+
const richMessages = found.filter(
|
|
315
|
+
(r) =>
|
|
316
|
+
L(L(L(r.message).botForwardedMessage).message).richResponseMessage,
|
|
317
|
+
);
|
|
318
|
+
second =
|
|
319
|
+
richMessages.find((r) => {
|
|
320
|
+
const rich = L(
|
|
321
|
+
L(L(r.message).botForwardedMessage).message,
|
|
322
|
+
).richResponseMessage;
|
|
323
|
+
const decoded = decodeUnifiedResponse(
|
|
324
|
+
L(L(rich).unifiedResponse).data as unknown as Uint8Array,
|
|
325
|
+
) as {
|
|
326
|
+
sections: {
|
|
327
|
+
view_model: { primitive?: Record<string, unknown> };
|
|
328
|
+
}[];
|
|
329
|
+
};
|
|
330
|
+
return (
|
|
331
|
+
L(L(L(A(decoded.sections)[0]).view_model).primitive).text ===
|
|
332
|
+
"payload kedua"
|
|
333
|
+
);
|
|
334
|
+
}) ?? null;
|
|
335
|
+
if (!second) await new Promise((r) => setTimeout(r, 100));
|
|
336
|
+
}
|
|
337
|
+
assert.ok(second, "pengiriman kedua sampai ke peer");
|
|
338
|
+
const secondMsg = second as DecryptedMessage;
|
|
339
|
+
|
|
340
|
+
const secondRich = L(
|
|
341
|
+
L(L(secondMsg.message).botForwardedMessage).message,
|
|
342
|
+
).richResponseMessage;
|
|
343
|
+
const secondUnified = decodeUnifiedResponse(
|
|
344
|
+
L(L(secondRich).unifiedResponse).data as unknown as Uint8Array,
|
|
345
|
+
) as { response_id: string };
|
|
346
|
+
assert.notEqual(
|
|
347
|
+
secondUnified.response_id,
|
|
348
|
+
directUnified.response_id,
|
|
349
|
+
"state tidak bocor antar pengiriman (instance baru)",
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
stopCapture();
|
|
353
|
+
await client.disconnect();
|
|
354
|
+
} finally {
|
|
355
|
+
await client?.disconnect().catch(() => {});
|
|
356
|
+
await server.stop().catch(() => {});
|
|
357
|
+
}
|
|
358
|
+
},
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
test(
|
|
362
|
+
"integration (fake-server): tombol native flow terkirim dan terdekripsi sebagai interactiveMessage",
|
|
363
|
+
{ timeout: 120_000 },
|
|
364
|
+
async () => {
|
|
365
|
+
const server = await FakeWaServer.start();
|
|
366
|
+
let client: WaClient | null = null;
|
|
367
|
+
|
|
368
|
+
try {
|
|
369
|
+
const { client: waClient, registeredPipeline } =
|
|
370
|
+
await createPairedClient(server);
|
|
371
|
+
client = waClient;
|
|
372
|
+
|
|
373
|
+
const peer = await server.createFakePeer(
|
|
374
|
+
{ jid: OWNER_JID, displayName: "Owner" },
|
|
375
|
+
registeredPipeline,
|
|
376
|
+
);
|
|
377
|
+
await peer.sendConversation("ping");
|
|
378
|
+
|
|
379
|
+
const session = server.sessionFor(registeredPipeline);
|
|
380
|
+
const captured: { node: BinaryNode; decrypted?: unknown }[] = [];
|
|
381
|
+
const stopCapture = session.onCapturedStanza((node: BinaryNode) => {
|
|
382
|
+
if (node.tag === "message") captured.push({ node });
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
await button(client)
|
|
386
|
+
.body("pilih salah satu")
|
|
387
|
+
.quick_reply("Oke", "menu ok")
|
|
388
|
+
.cta_url("Buka", "https://example.com")
|
|
389
|
+
.send(OWNER_JID);
|
|
390
|
+
|
|
391
|
+
const deadline = Date.now() + 15_000;
|
|
392
|
+
let interactive: DecryptedMessage | null = null;
|
|
393
|
+
while (Date.now() < deadline && !interactive) {
|
|
394
|
+
for (const entry of captured) {
|
|
395
|
+
if (entry.decrypted !== undefined) continue;
|
|
396
|
+
const result = await peer.decryptStanza(entry.node).catch(() => null);
|
|
397
|
+
entry.decrypted = result;
|
|
398
|
+
if (
|
|
399
|
+
result &&
|
|
400
|
+
L((result as unknown as DecryptedMessage).message)
|
|
401
|
+
.interactiveMessage
|
|
402
|
+
) {
|
|
403
|
+
interactive = result as unknown as DecryptedMessage;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
if (!interactive) await new Promise((r) => setTimeout(r, 100));
|
|
407
|
+
}
|
|
408
|
+
stopCapture();
|
|
409
|
+
assert.ok(
|
|
410
|
+
interactive,
|
|
411
|
+
"interactiveMessage sampai ke peer dan terdekripsi",
|
|
412
|
+
);
|
|
413
|
+
const interactiveMsg = interactive as DecryptedMessage;
|
|
414
|
+
|
|
415
|
+
const interactiveMessage = L(interactiveMsg.message).interactiveMessage;
|
|
416
|
+
const flow = L(interactiveMessage).nativeFlowMessage;
|
|
417
|
+
assert.equal(L(flow).messageVersion, 1);
|
|
418
|
+
const buttons = A(L(flow).buttons) as {
|
|
419
|
+
name: string;
|
|
420
|
+
buttonParamsJson: string;
|
|
421
|
+
}[];
|
|
422
|
+
assert.equal(buttons.length, 2);
|
|
423
|
+
assert.equal(buttons[0]?.name, "quick_reply");
|
|
424
|
+
assert.deepEqual(JSON.parse(buttons[0]?.buttonParamsJson ?? "{}"), {
|
|
425
|
+
display_text: "Oke",
|
|
426
|
+
id: "menu ok",
|
|
427
|
+
});
|
|
428
|
+
assert.equal(buttons[1]?.name, "cta_url");
|
|
429
|
+
assert.deepEqual(JSON.parse(buttons[1]?.buttonParamsJson ?? "{}"), {
|
|
430
|
+
display_text: "Buka",
|
|
431
|
+
url: "https://example.com",
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
await client.disconnect();
|
|
435
|
+
} finally {
|
|
436
|
+
await client?.disconnect().catch(() => {});
|
|
437
|
+
await server.stop().catch(() => {});
|
|
438
|
+
}
|
|
439
|
+
},
|
|
440
|
+
);
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { Readable } from "node:stream";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
createMediaQueue,
|
|
7
|
+
limitStreamBytes,
|
|
8
|
+
configureSharedMediaQueue,
|
|
9
|
+
getSharedMediaQueue,
|
|
10
|
+
resetSharedMediaQueue,
|
|
11
|
+
QueueOverflowError,
|
|
12
|
+
MediaAdmissionPausedError,
|
|
13
|
+
} from "@kyyinfinite/zapo-message/media-gate.js";
|
|
14
|
+
|
|
15
|
+
test.afterEach(() => resetSharedMediaQueue());
|
|
16
|
+
|
|
17
|
+
test("media queue: concurrency terbatas (2)", async () => {
|
|
18
|
+
const queue = createMediaQueue({
|
|
19
|
+
concurrency: 2,
|
|
20
|
+
capacity: 10,
|
|
21
|
+
timeoutMs: 5000,
|
|
22
|
+
});
|
|
23
|
+
let peak = 0;
|
|
24
|
+
let active = 0;
|
|
25
|
+
|
|
26
|
+
const jobs: Promise<void>[] = [];
|
|
27
|
+
for (let i = 0; i < 6; i++) {
|
|
28
|
+
jobs.push(
|
|
29
|
+
queue.run(
|
|
30
|
+
async () => {
|
|
31
|
+
active++;
|
|
32
|
+
peak = Math.max(peak, active);
|
|
33
|
+
await new Promise((r) => setTimeout(r, 30));
|
|
34
|
+
active--;
|
|
35
|
+
},
|
|
36
|
+
{ label: `job${i}` },
|
|
37
|
+
),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
await Promise.all(jobs);
|
|
41
|
+
|
|
42
|
+
assert.equal(peak, 2);
|
|
43
|
+
assert.equal(queue.snapshot().processed, 6);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
test("media queue: job timeout melempar error + signal di-abort", async () => {
|
|
47
|
+
const queue = createMediaQueue({
|
|
48
|
+
concurrency: 1,
|
|
49
|
+
capacity: 10,
|
|
50
|
+
timeoutMs: 50,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
let sawAbort = false;
|
|
54
|
+
await assert.rejects(
|
|
55
|
+
() =>
|
|
56
|
+
queue.run(
|
|
57
|
+
async ({ signal }) => {
|
|
58
|
+
await new Promise<void>((resolve) => {
|
|
59
|
+
signal.addEventListener(
|
|
60
|
+
"abort",
|
|
61
|
+
() => {
|
|
62
|
+
sawAbort = true;
|
|
63
|
+
resolve();
|
|
64
|
+
},
|
|
65
|
+
{ once: true },
|
|
66
|
+
);
|
|
67
|
+
});
|
|
68
|
+
throw new Error("unreachable");
|
|
69
|
+
},
|
|
70
|
+
{ label: "lambat", timeoutMs: 30 },
|
|
71
|
+
),
|
|
72
|
+
/melebihi/,
|
|
73
|
+
"job timeout harus gagal jelas",
|
|
74
|
+
);
|
|
75
|
+
assert.equal(sawAbort, true);
|
|
76
|
+
assert.equal(queue.snapshot().timeouts, 1);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("media queue: ukuran buffer melebihi maxBytes ditolak (memory guard)", async () => {
|
|
80
|
+
const queue = createMediaQueue({ concurrency: 1, maxBytes: 1024 });
|
|
81
|
+
|
|
82
|
+
await assert.rejects(
|
|
83
|
+
() =>
|
|
84
|
+
queue.run(
|
|
85
|
+
async ({ noteBytes }) => {
|
|
86
|
+
noteBytes(2048);
|
|
87
|
+
return Buffer.alloc(2048, 7);
|
|
88
|
+
},
|
|
89
|
+
{ label: "gede" },
|
|
90
|
+
),
|
|
91
|
+
/batas ukuran/,
|
|
92
|
+
"buffer besar harus ditolak",
|
|
93
|
+
);
|
|
94
|
+
assert.equal(queue.snapshot().oversized, 1);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test("media queue: buffer dalam batas lolos dan jadi Buffer", async () => {
|
|
98
|
+
const queue = createMediaQueue({ concurrency: 1, maxBytes: 1024 * 1024 });
|
|
99
|
+
const result = await queue.run(
|
|
100
|
+
async ({ noteBytes }) => {
|
|
101
|
+
const buffer = Buffer.alloc(1000, 3);
|
|
102
|
+
noteBytes(buffer.byteLength);
|
|
103
|
+
return buffer;
|
|
104
|
+
},
|
|
105
|
+
{ label: "pas" },
|
|
106
|
+
);
|
|
107
|
+
assert.ok(Buffer.isBuffer(result));
|
|
108
|
+
assert.equal(result.length, 1000);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test("media queue: external signal membatalkan hasil job", async () => {
|
|
112
|
+
const queue = createMediaQueue({ concurrency: 1 });
|
|
113
|
+
const controller = new AbortController();
|
|
114
|
+
|
|
115
|
+
const promise = queue.run(
|
|
116
|
+
async ({ signal }) => {
|
|
117
|
+
await new Promise((r) => setTimeout(r, 20));
|
|
118
|
+
if (signal.aborted) throw new Error("dibatalkan");
|
|
119
|
+
return "ok";
|
|
120
|
+
},
|
|
121
|
+
{ label: "cancel", signal: controller.signal },
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
controller.abort();
|
|
125
|
+
await assert.rejects(promise, /dibatalkan/);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
test("media queue: admission pause (governor hook)", async () => {
|
|
129
|
+
const queue = createMediaQueue({ concurrency: 1 });
|
|
130
|
+
queue.setAdmissionPaused(true);
|
|
131
|
+
await assert.rejects(
|
|
132
|
+
() => queue.run(async () => 1),
|
|
133
|
+
MediaAdmissionPausedError,
|
|
134
|
+
);
|
|
135
|
+
queue.setAdmissionPaused(false);
|
|
136
|
+
assert.equal(await queue.run(async () => "ok"), "ok");
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test("media queue: kapasitas pending terbatas", async () => {
|
|
140
|
+
const queue = createMediaQueue({
|
|
141
|
+
concurrency: 1,
|
|
142
|
+
capacity: 2,
|
|
143
|
+
timeoutMs: 5000,
|
|
144
|
+
});
|
|
145
|
+
const release: (() => void)[] = [];
|
|
146
|
+
const running = queue.run(
|
|
147
|
+
() => new Promise((resolve) => release.push(resolve as () => void)),
|
|
148
|
+
{ label: "block" },
|
|
149
|
+
);
|
|
150
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
151
|
+
void queue.run(async () => 1);
|
|
152
|
+
void queue.run(async () => 2);
|
|
153
|
+
|
|
154
|
+
await assert.rejects(
|
|
155
|
+
() => queue.run(async () => 3, { label: "ke-4" }),
|
|
156
|
+
QueueOverflowError,
|
|
157
|
+
);
|
|
158
|
+
release[0]?.();
|
|
159
|
+
await running;
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
test("media queue: shutdown menolak pending + nunggu yang jalan", async () => {
|
|
163
|
+
const queue = createMediaQueue({ concurrency: 1, timeoutMs: 5000 });
|
|
164
|
+
const release: (() => void)[] = [];
|
|
165
|
+
const running = queue.run(
|
|
166
|
+
() => new Promise((resolve) => release.push(resolve as () => void)),
|
|
167
|
+
{ label: "block" },
|
|
168
|
+
);
|
|
169
|
+
await new Promise((r) => setTimeout(r, 5));
|
|
170
|
+
const pending = queue.run(async () => 1).catch((e) => e);
|
|
171
|
+
|
|
172
|
+
const result = await queue.shutdown({ timeoutMs: 2000 });
|
|
173
|
+
assert.ok(result.rejectedPending >= 1);
|
|
174
|
+
assert.ok((await pending) instanceof Error);
|
|
175
|
+
|
|
176
|
+
release[0]?.();
|
|
177
|
+
await running;
|
|
178
|
+
assert.equal(queue.snapshot().closed, true);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
test("limitStreamBytes: stream melebihi batas di-destroy dengan error", async () => {
|
|
182
|
+
const source = Readable.from([Buffer.alloc(100, 1), Buffer.alloc(2000, 2)]);
|
|
183
|
+
const limited = limitStreamBytes(source, 1024);
|
|
184
|
+
|
|
185
|
+
const chunks: Buffer[] = [];
|
|
186
|
+
await assert.rejects(
|
|
187
|
+
(async () => {
|
|
188
|
+
for await (const chunk of limited) chunks.push(chunk as Buffer);
|
|
189
|
+
})(),
|
|
190
|
+
/batas ukuran/,
|
|
191
|
+
);
|
|
192
|
+
const total = chunks.reduce((sum, c) => sum + c.length, 0);
|
|
193
|
+
assert.ok(total < 1024 + 4096, "stream dibatasi");
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("limitStreamBytes: stream kecil lolos utuh", async () => {
|
|
197
|
+
const payload = Buffer.alloc(500, 9);
|
|
198
|
+
const source = Readable.from([payload]);
|
|
199
|
+
const limited = limitStreamBytes(source, 1024);
|
|
200
|
+
|
|
201
|
+
const chunks: Buffer[] = [];
|
|
202
|
+
for await (const chunk of limited) chunks.push(chunk as Buffer);
|
|
203
|
+
assert.equal(Buffer.concat(chunks).length, 500);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
test("media gate: singleton konsisten (download + governor lihat queue sama)", () => {
|
|
207
|
+
resetSharedMediaQueue();
|
|
208
|
+
const first = getSharedMediaQueue();
|
|
209
|
+
const second = getSharedMediaQueue();
|
|
210
|
+
assert.equal(first, second);
|
|
211
|
+
|
|
212
|
+
const configured = configureSharedMediaQueue({ concurrency: 3 });
|
|
213
|
+
assert.equal(getSharedMediaQueue(), configured);
|
|
214
|
+
assert.equal(configured.snapshot().concurrency, 3);
|
|
215
|
+
});
|