@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,1006 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
AIRich,
|
|
6
|
+
airich,
|
|
7
|
+
messageKit,
|
|
8
|
+
decodeUnifiedResponse,
|
|
9
|
+
ZWSP,
|
|
10
|
+
} from "@kyyinfinite/zapo-message";
|
|
11
|
+
import type { UnifiedResponseSection } from "@kyyinfinite/zapo-message";
|
|
12
|
+
import { ProtocolSendError } from "@kyyinfinite/zapo-message";
|
|
13
|
+
import { asClient, L, A } from "./helpers.js";
|
|
14
|
+
|
|
15
|
+
const makeClient = ({
|
|
16
|
+
id = "FAKEID",
|
|
17
|
+
failStage = null,
|
|
18
|
+
}: { id?: string; failStage?: number | null } = {}) => {
|
|
19
|
+
const sends: { jid?: unknown; content?: unknown; options?: unknown }[] = [];
|
|
20
|
+
const client = {
|
|
21
|
+
message: {
|
|
22
|
+
send: async (jid: unknown, content: unknown, options: unknown) => {
|
|
23
|
+
sends.push({ jid, content, options });
|
|
24
|
+
if (failStage !== null && sends.length > failStage)
|
|
25
|
+
throw new Error("transport down");
|
|
26
|
+
return { id: `${id}_${sends.length}`, attempts: 1, ack: {} };
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
return { client: asClient(client), sends };
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const bytesToUtf8 = (data: Uint8Array): string =>
|
|
34
|
+
Buffer.from(data).toString("utf8");
|
|
35
|
+
|
|
36
|
+
const richOf = (payload: unknown): Record<string, unknown> => {
|
|
37
|
+
const message = L(L(payload).botForwardedMessage).message;
|
|
38
|
+
const direct = L(message).richResponseMessage;
|
|
39
|
+
if (direct) return L(direct);
|
|
40
|
+
const edited = L(
|
|
41
|
+
L(L(message).protocolMessage).editedMessage,
|
|
42
|
+
).botForwardedMessage;
|
|
43
|
+
return L(L(edited).message).richResponseMessage as Record<string, unknown>;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const decodeSections = (
|
|
47
|
+
payload: unknown,
|
|
48
|
+
): { response_id: string; sections: unknown[] } =>
|
|
49
|
+
decodeUnifiedResponse(
|
|
50
|
+
L(L(richOf(payload)).unifiedResponse).data as unknown as Uint8Array,
|
|
51
|
+
) as { response_id: string; sections: unknown[] };
|
|
52
|
+
|
|
53
|
+
const sectionAt = (payload: unknown, index: number): Record<string, unknown> =>
|
|
54
|
+
L(A(L(decodeSections(payload)).sections)[index]);
|
|
55
|
+
|
|
56
|
+
const viewModelOf = (
|
|
57
|
+
payload: unknown,
|
|
58
|
+
index: number,
|
|
59
|
+
): Record<string, unknown> => L(sectionAt(payload, index).view_model);
|
|
60
|
+
|
|
61
|
+
const primitiveOf = (
|
|
62
|
+
payload: unknown,
|
|
63
|
+
index: number,
|
|
64
|
+
): Record<string, unknown> => L(viewModelOf(payload, index).primitive);
|
|
65
|
+
|
|
66
|
+
test("unit (airich): struktur build() mengikuti botForwardedMessage.message.richResponseMessage", () => {
|
|
67
|
+
const { client } = makeClient();
|
|
68
|
+
const rich = airich(client).text("halo").build();
|
|
69
|
+
|
|
70
|
+
const message = L(rich.botForwardedMessage).message;
|
|
71
|
+
assert.ok(message, "botForwardedMessage.message harus ada");
|
|
72
|
+
const rr = L(message).richResponseMessage;
|
|
73
|
+
assert.equal(L(rr).messageType, 1);
|
|
74
|
+
assert.ok(Array.isArray(L(rr).submessages));
|
|
75
|
+
assert.equal(A(L(rr).submessages).length, 1);
|
|
76
|
+
assert.deepEqual(L(rr).submessages, [
|
|
77
|
+
{ messageType: 2, messageText: "halo" },
|
|
78
|
+
]);
|
|
79
|
+
assert.ok(L(L(rr).unifiedResponse).data instanceof Uint8Array);
|
|
80
|
+
|
|
81
|
+
const decoded = decodeUnifiedResponse(
|
|
82
|
+
L(L(rr).unifiedResponse).data as unknown as Uint8Array,
|
|
83
|
+
) as { response_id: string; sections: unknown[] };
|
|
84
|
+
assert.match(decoded.response_id, /^[0-9a-f-]{36}$/);
|
|
85
|
+
assert.equal(A(decoded.sections).length, 1);
|
|
86
|
+
const primitive = L(L(L(A(decoded.sections)[0]).view_model).primitive);
|
|
87
|
+
assert.equal(primitive.text, "halo");
|
|
88
|
+
assert.equal(primitive.__typename, "GenAIMarkdownTextUXPrimitive");
|
|
89
|
+
assert.equal(
|
|
90
|
+
L(L(A(decoded.sections)[0]).view_model).__typename,
|
|
91
|
+
"GenAISingleLayoutViewModel",
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("unit (airich): unified response encode/decode roundtrip tanpa perubahan isi", () => {
|
|
96
|
+
const { client } = makeClient();
|
|
97
|
+
const rich = airich(client)
|
|
98
|
+
.text("unicode ☕ café — [éntitas](https://ü.com/path)")
|
|
99
|
+
.code("javascript", 'const x = "☕"')
|
|
100
|
+
.table([
|
|
101
|
+
["H1", "H2"],
|
|
102
|
+
["a", "b"],
|
|
103
|
+
])
|
|
104
|
+
.suggest(["satu", "dua"])
|
|
105
|
+
.build();
|
|
106
|
+
|
|
107
|
+
const data = L(L(richOf(rich)).unifiedResponse).data as Uint8Array;
|
|
108
|
+
const decoded = decodeUnifiedResponse(data);
|
|
109
|
+
const reencoded = Buffer.from(
|
|
110
|
+
JSON.stringify(decoded).replace(
|
|
111
|
+
/[\u007f-\uffff]/g,
|
|
112
|
+
(c) => "\\u" + c.charCodeAt(0).toString(16).padStart(4, "0"),
|
|
113
|
+
),
|
|
114
|
+
"utf8",
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
assert.equal(
|
|
118
|
+
Buffer.compare(Buffer.from(data), reencoded),
|
|
119
|
+
0,
|
|
120
|
+
"encode(decode(bytes)) harus identik byte-per-byte",
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("unit (airich): unicode dan escaping konsisten dengan format botf", () => {
|
|
125
|
+
const { client } = makeClient();
|
|
126
|
+
const rich = airich(client).text("ZWSP: emoji ✨").build();
|
|
127
|
+
|
|
128
|
+
const rr = richOf(rich);
|
|
129
|
+
const decoded = decodeSections(rich);
|
|
130
|
+
assert.equal(
|
|
131
|
+
L(L(A(decoded.sections)[0]).view_model).primitive !== undefined
|
|
132
|
+
? L(primitiveOf(rich, 0)).text
|
|
133
|
+
: undefined,
|
|
134
|
+
"ZWSP: emoji ✨",
|
|
135
|
+
);
|
|
136
|
+
const raw = bytesToUtf8(L(L(rr).unifiedResponse).data as Uint8Array);
|
|
137
|
+
assert.ok(
|
|
138
|
+
raw.includes("\\u200b"),
|
|
139
|
+
"ZWSP harus ter-escape \\u200b seperti payload botf",
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test("unit (airich): placeholder memakai ZWSP + messageSecret dinamis + tanpa metadata palsu", () => {
|
|
144
|
+
const { client } = makeClient();
|
|
145
|
+
const builder = airich(client).text("isi");
|
|
146
|
+
const p1 = builder.buildPlaceholder();
|
|
147
|
+
const p2 = builder.buildPlaceholder();
|
|
148
|
+
|
|
149
|
+
assert.deepEqual(p1.messageContextInfo.deviceListMetadataVersion, 2);
|
|
150
|
+
assert.ok(p1.messageContextInfo.messageSecret instanceof Uint8Array);
|
|
151
|
+
assert.equal(p1.messageContextInfo.messageSecret.length, 32);
|
|
152
|
+
assert.notEqual(
|
|
153
|
+
Buffer.compare(
|
|
154
|
+
Buffer.from(p1.messageContextInfo.messageSecret),
|
|
155
|
+
Buffer.from(p2.messageContextInfo.messageSecret),
|
|
156
|
+
),
|
|
157
|
+
0,
|
|
158
|
+
"messageSecret antar-panggilan TIDAK boleh sama (dinamis)",
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
assert.equal(
|
|
162
|
+
"botMetadata" in p1.messageContextInfo,
|
|
163
|
+
false,
|
|
164
|
+
"dilarang botMetadata palsu",
|
|
165
|
+
);
|
|
166
|
+
assert.equal(
|
|
167
|
+
"deviceListMetadata" in p1.messageContextInfo,
|
|
168
|
+
false,
|
|
169
|
+
"dilarang deviceListMetadata palsu",
|
|
170
|
+
);
|
|
171
|
+
assert.equal(
|
|
172
|
+
"verificationMetadata" in p1.messageContextInfo,
|
|
173
|
+
false,
|
|
174
|
+
"dilarang verificationMetadata palsu",
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
const rr = richOf(p1);
|
|
178
|
+
const raw = bytesToUtf8(L(L(rr).unifiedResponse).data as Uint8Array);
|
|
179
|
+
assert.ok(
|
|
180
|
+
raw.includes("\\u200b"),
|
|
181
|
+
"placeholder unified response berisi ZWSP",
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
assert.deepEqual(L(rr).submessages, [{ messageType: 2, messageText: ZWSP }]);
|
|
185
|
+
const contextInfo = L(L(rr).contextInfo);
|
|
186
|
+
assert.equal(contextInfo.forwardingScore, 1);
|
|
187
|
+
assert.equal(contextInfo.isForwarded, true);
|
|
188
|
+
assert.equal(contextInfo.forwardOrigin, 4);
|
|
189
|
+
assert.equal(
|
|
190
|
+
"forwardedAiBotMessageInfo" in contextInfo,
|
|
191
|
+
false,
|
|
192
|
+
"botJid tidak boleh muncul tanpa nilai valid",
|
|
193
|
+
);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
test("unit (airich): forwardedAiBotMessageInfo hanya muncul bila botJid eksplisit", () => {
|
|
197
|
+
const { client } = makeClient();
|
|
198
|
+
const withBot = new AIRich(client, { botJid: "999@bot" });
|
|
199
|
+
const rr = richOf(withBot.text("x").build());
|
|
200
|
+
assert.deepEqual(L(rr).contextInfo, {
|
|
201
|
+
forwardingScore: 1,
|
|
202
|
+
isForwarded: true,
|
|
203
|
+
forwardOrigin: 4,
|
|
204
|
+
forwardedAiBotMessageInfo: { botJid: "999@bot" },
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const withoutBot = airich(client);
|
|
208
|
+
const rr2 = richOf(withoutBot.text("x").build());
|
|
209
|
+
assert.equal("forwardedAiBotMessageInfo" in L(L(rr2).contextInfo), false);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
test("unit (airich): protocol edit type 14 menunjuk key placeholder", () => {
|
|
213
|
+
const { client } = makeClient();
|
|
214
|
+
const builder = airich(client).text("isi edit");
|
|
215
|
+
const edit = builder.buildEdit("628123@s.whatsapp.net", "PLACEHOLDER_ID", {
|
|
216
|
+
submessages: false,
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const protoMessage = L(L(edit.botForwardedMessage).message).protocolMessage;
|
|
220
|
+
assert.deepEqual(L(protoMessage).key, {
|
|
221
|
+
remoteJid: "628123@s.whatsapp.net",
|
|
222
|
+
fromMe: true,
|
|
223
|
+
id: "PLACEHOLDER_ID",
|
|
224
|
+
});
|
|
225
|
+
assert.equal(L(protoMessage).type, 14);
|
|
226
|
+
|
|
227
|
+
const editedRich = richOf(edit);
|
|
228
|
+
assert.deepEqual(L(editedRich).submessages, []);
|
|
229
|
+
const decoded = decodeSections(edit);
|
|
230
|
+
assert.equal(L(primitiveOf(edit, 0)).text, "isi edit");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
test("unit (airich): validasi ID pesan untuk edit", async () => {
|
|
234
|
+
const { client } = makeClient();
|
|
235
|
+
const builder = airich(client).text("x");
|
|
236
|
+
|
|
237
|
+
assert.throws(() => builder.buildEdit("jid", ""), /ID pesan/);
|
|
238
|
+
assert.throws(() => builder.buildEdit("jid", null), /ID pesan/);
|
|
239
|
+
|
|
240
|
+
await assert.rejects(builder.edit("jid", ""), /ID pesan/);
|
|
241
|
+
await assert.rejects(builder.edit("jid", undefined), /ID pesan/);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("unit (airich): submessage text/code/table/suggestion", () => {
|
|
245
|
+
const { client } = makeClient();
|
|
246
|
+
const rich = airich(client)
|
|
247
|
+
.text("teks")
|
|
248
|
+
.code("javascript", "const a = 1")
|
|
249
|
+
.table([
|
|
250
|
+
["H", "K"],
|
|
251
|
+
["1", "2"],
|
|
252
|
+
])
|
|
253
|
+
.suggest(["lanjut", "ulang"])
|
|
254
|
+
.build({ submessages: true });
|
|
255
|
+
|
|
256
|
+
const subs = A(L(richOf(rich)).submessages);
|
|
257
|
+
assert.equal(subs.length, 3);
|
|
258
|
+
assert.deepEqual(subs[0], { messageType: 2, messageText: "teks" });
|
|
259
|
+
assert.equal(L(subs[1]).messageType, 5);
|
|
260
|
+
assert.equal(L(L(subs[1]).codeMetadata).codeLanguage, "javascript");
|
|
261
|
+
assert.deepEqual(A(L(L(subs[1]).codeMetadata).codeBlocks)[0], {
|
|
262
|
+
codeContent: "const",
|
|
263
|
+
highlightType: 1,
|
|
264
|
+
});
|
|
265
|
+
assert.equal(L(subs[2]).messageType, 4);
|
|
266
|
+
assert.deepEqual(L(L(subs[2]).tableMetadata).rows, [
|
|
267
|
+
{ items: ["H", "K"], isHeading: true },
|
|
268
|
+
{ items: ["1", "2"] },
|
|
269
|
+
]);
|
|
270
|
+
assert.equal(subs[3], undefined, "suggest tidak menghasilkan submessage");
|
|
271
|
+
|
|
272
|
+
assert.equal(
|
|
273
|
+
L(primitiveOf(rich, 0)).__typename,
|
|
274
|
+
"GenAIMarkdownTextUXPrimitive",
|
|
275
|
+
);
|
|
276
|
+
assert.equal(L(primitiveOf(rich, 1)).__typename, "GenAICodeUXPrimitive");
|
|
277
|
+
assert.equal(L(primitiveOf(rich, 1)).language, "javascript");
|
|
278
|
+
assert.deepEqual(A(L(primitiveOf(rich, 1)).code_blocks)[0], {
|
|
279
|
+
content: "const",
|
|
280
|
+
type: "KEYWORD",
|
|
281
|
+
});
|
|
282
|
+
assert.equal(L(primitiveOf(rich, 2)).__typename, "GenATableUXPrimitive");
|
|
283
|
+
assert.deepEqual(
|
|
284
|
+
A(L(primitiveOf(rich, 2)).rows).map((r) => L(r).is_header),
|
|
285
|
+
[true, false],
|
|
286
|
+
);
|
|
287
|
+
assert.deepEqual(L(A(L(primitiveOf(rich, 2)).rows)[0]).cells, ["H", "K"]);
|
|
288
|
+
assert.deepEqual(L(A(L(primitiveOf(rich, 2)).rows)[0]).markdown_cells, [
|
|
289
|
+
{ text: "H" },
|
|
290
|
+
{ text: "K" },
|
|
291
|
+
]);
|
|
292
|
+
assert.equal(A(L(viewModelOf(rich, 3)).primitives).length, 2);
|
|
293
|
+
assert.equal(
|
|
294
|
+
L(viewModelOf(rich, 3)).__typename,
|
|
295
|
+
"GenAIHScrollLayoutViewModel",
|
|
296
|
+
);
|
|
297
|
+
assert.equal(
|
|
298
|
+
L(A(L(viewModelOf(rich, 3)).primitives)[0]).prompt_type,
|
|
299
|
+
"SUGGESTED_PROMPT",
|
|
300
|
+
);
|
|
301
|
+
assert.equal(L(sectionAt(rich, 3)).__typename, "GenAIUnifiedResponseSection");
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
test("unit (airich): suggestion layout Single, HScroll, dan ActionRow", () => {
|
|
305
|
+
const { client } = makeClient();
|
|
306
|
+
|
|
307
|
+
assert.equal(
|
|
308
|
+
L(viewModelOf(airich(client).suggest(["satu"]).build(), 0)).__typename,
|
|
309
|
+
"GenAISingleLayoutViewModel",
|
|
310
|
+
);
|
|
311
|
+
assert.equal(
|
|
312
|
+
L(primitiveOf(airich(client).suggest(["satu"]).build(), 0)).prompt_text,
|
|
313
|
+
"satu",
|
|
314
|
+
);
|
|
315
|
+
|
|
316
|
+
assert.equal(
|
|
317
|
+
L(viewModelOf(airich(client).suggest(["a", "b"]).build(), 0)).__typename,
|
|
318
|
+
"GenAIHScrollLayoutViewModel",
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
assert.equal(
|
|
322
|
+
L(
|
|
323
|
+
viewModelOf(
|
|
324
|
+
airich(client).suggest(["a", "b"], { scroll: false }).build(),
|
|
325
|
+
0,
|
|
326
|
+
),
|
|
327
|
+
).__typename,
|
|
328
|
+
"GenAIActionRowLayoutViewModel",
|
|
329
|
+
);
|
|
330
|
+
|
|
331
|
+
assert.equal(
|
|
332
|
+
L(
|
|
333
|
+
viewModelOf(
|
|
334
|
+
airich(client).suggest(["a", "b"], { layout: "ActionRow" }).build(),
|
|
335
|
+
0,
|
|
336
|
+
),
|
|
337
|
+
).__typename,
|
|
338
|
+
"GenAIActionRowLayoutViewModel",
|
|
339
|
+
);
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
test("unit (airich): inline entities hyperlink, citation, latex", () => {
|
|
343
|
+
const { client } = makeClient();
|
|
344
|
+
const payload = airich(client)
|
|
345
|
+
.text(
|
|
346
|
+
"[web](https://a.com) ref [](https://b.com) rumus [E|10]<https://c.com/img.png>",
|
|
347
|
+
)
|
|
348
|
+
.build();
|
|
349
|
+
const primitive = primitiveOf(payload, 0);
|
|
350
|
+
|
|
351
|
+
assert.equal(
|
|
352
|
+
primitive.text,
|
|
353
|
+
"{{ZAPOK_HYPERLINK_0}}https://a.com{{/ZAPOK_HYPERLINK_0}} ref {{ZAPOK_CITATION_0}}https://b.com{{/ZAPOK_CITATION_0}} rumus {{ZAPOK_LATEX_0}}E{{/ZAPOK_LATEX_0}}",
|
|
354
|
+
);
|
|
355
|
+
const entities = A(primitive.inline_entities);
|
|
356
|
+
assert.equal(entities.length, 3);
|
|
357
|
+
|
|
358
|
+
const [link, cite, latex] = entities.map((e) => L(L(e).metadata));
|
|
359
|
+
assert.equal(L(link).__typename, "GenAIInlineLinkItem");
|
|
360
|
+
assert.deepEqual(link, {
|
|
361
|
+
display_name: "web",
|
|
362
|
+
is_trusted: true,
|
|
363
|
+
url: "https://a.com",
|
|
364
|
+
__typename: "GenAIInlineLinkItem",
|
|
365
|
+
});
|
|
366
|
+
assert.equal(L(cite).__typename, "GenAISearchCitationItem");
|
|
367
|
+
assert.equal(L(cite).reference_id, 1);
|
|
368
|
+
assert.equal(L(latex).__typename, "GenAILatexItem");
|
|
369
|
+
assert.equal(L(latex).latex_expression, "E");
|
|
370
|
+
assert.equal(L(L(latex).latex_image).url, "https://c.com/img.png");
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("unit (airich): context() allowlist — field internal dan identitas tidak bisa ditimpa", () => {
|
|
374
|
+
const { client } = makeClient();
|
|
375
|
+
const builder = airich(client);
|
|
376
|
+
|
|
377
|
+
assert.throws(
|
|
378
|
+
() =>
|
|
379
|
+
builder.context({ forwardingScore: 99 } as unknown as Record<
|
|
380
|
+
string,
|
|
381
|
+
unknown
|
|
382
|
+
>),
|
|
383
|
+
/dikendalikan builder/,
|
|
384
|
+
);
|
|
385
|
+
assert.throws(
|
|
386
|
+
() =>
|
|
387
|
+
builder.context({ isForwarded: false } as unknown as Record<
|
|
388
|
+
string,
|
|
389
|
+
unknown
|
|
390
|
+
>),
|
|
391
|
+
/dikendalikan builder/,
|
|
392
|
+
);
|
|
393
|
+
assert.throws(
|
|
394
|
+
() =>
|
|
395
|
+
builder.context({ forwardOrigin: 0 } as unknown as Record<
|
|
396
|
+
string,
|
|
397
|
+
unknown
|
|
398
|
+
>),
|
|
399
|
+
/dikendalikan builder/,
|
|
400
|
+
);
|
|
401
|
+
assert.throws(
|
|
402
|
+
() =>
|
|
403
|
+
builder.context({
|
|
404
|
+
forwardedAiBotMessageInfo: { botJid: "0@bot" },
|
|
405
|
+
} as unknown as Record<string, unknown>),
|
|
406
|
+
/dikendalikan builder/,
|
|
407
|
+
);
|
|
408
|
+
assert.throws(
|
|
409
|
+
() =>
|
|
410
|
+
builder.context({ stanzaId: "x", participant: "y" } as unknown as Record<
|
|
411
|
+
string,
|
|
412
|
+
unknown
|
|
413
|
+
>),
|
|
414
|
+
/dikendalikan builder/,
|
|
415
|
+
);
|
|
416
|
+
assert.throws(
|
|
417
|
+
() =>
|
|
418
|
+
builder.context({ randomField: true } as unknown as Record<
|
|
419
|
+
string,
|
|
420
|
+
unknown
|
|
421
|
+
>),
|
|
422
|
+
/tidak di-allowlist/,
|
|
423
|
+
);
|
|
424
|
+
|
|
425
|
+
builder.context({
|
|
426
|
+
mentionedJid: ["6281@s.whatsapp.net"],
|
|
427
|
+
groupMentions: [],
|
|
428
|
+
statusAttributions: [],
|
|
429
|
+
expiration: 7776000,
|
|
430
|
+
ephemeralSettingTimestamp: 1773527159,
|
|
431
|
+
disappearingMode: {
|
|
432
|
+
initiator: 1,
|
|
433
|
+
trigger: 2,
|
|
434
|
+
initiatedByMe: true,
|
|
435
|
+
} as never,
|
|
436
|
+
});
|
|
437
|
+
|
|
438
|
+
const contextInfo = L(L(richOf(builder.buildPlaceholder())).contextInfo);
|
|
439
|
+
assert.deepEqual(contextInfo.mentionedJid, ["6281@s.whatsapp.net"]);
|
|
440
|
+
assert.equal(contextInfo.expiration, 7776000);
|
|
441
|
+
assert.deepEqual(contextInfo.disappearingMode, {
|
|
442
|
+
initiator: 1,
|
|
443
|
+
trigger: 2,
|
|
444
|
+
initiatedByMe: true,
|
|
445
|
+
});
|
|
446
|
+
assert.equal(contextInfo.forwardingScore, 1);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
test("unit (airich): quote masuk contextInfo placeholder dan direct send, tidak di build() default", () => {
|
|
450
|
+
const { client } = makeClient();
|
|
451
|
+
const quotedMessage = {
|
|
452
|
+
key: { id: "QID", remoteJid: "6281@s.whatsapp.net" },
|
|
453
|
+
message: { conversation: "kutip" },
|
|
454
|
+
};
|
|
455
|
+
const builder = airich(client).text("x").quote(quotedMessage);
|
|
456
|
+
|
|
457
|
+
const ph = L(L(richOf(builder.buildPlaceholder())).contextInfo);
|
|
458
|
+
assert.equal(ph.stanzaId, "QID");
|
|
459
|
+
assert.equal(ph.participant, "6281@s.whatsapp.net");
|
|
460
|
+
assert.equal(ph.quotedType, 0);
|
|
461
|
+
assert.deepEqual(ph.quotedMessage, { conversation: "kutip" });
|
|
462
|
+
|
|
463
|
+
const defaultBuild = L(
|
|
464
|
+
L(richOf(builder.build({ submessages: false }))).contextInfo,
|
|
465
|
+
);
|
|
466
|
+
assert.equal(
|
|
467
|
+
"stanzaId" in defaultBuild,
|
|
468
|
+
false,
|
|
469
|
+
"build() default tidak menyertakan quote (perilaku lama dipertahankan)",
|
|
470
|
+
);
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
test("unit (airich): id stabil, duplikat ditolak", () => {
|
|
474
|
+
const { client } = makeClient();
|
|
475
|
+
const builder = airich(client)
|
|
476
|
+
.text("a", { id: "satu" })
|
|
477
|
+
.text("b", { id: "dua" });
|
|
478
|
+
|
|
479
|
+
assert.deepEqual(builder.ids(), ["satu", "dua"]);
|
|
480
|
+
assert.equal(builder.has("satu"), true);
|
|
481
|
+
assert.equal(builder.has("gak-ada"), false);
|
|
482
|
+
assert.equal(builder.size, 2);
|
|
483
|
+
|
|
484
|
+
assert.throws(
|
|
485
|
+
() => builder.text("c", { id: "satu" }),
|
|
486
|
+
(e: Error & { code?: string }) => e.code === "DUPLICATE_ID",
|
|
487
|
+
);
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
test("unit (airich): remove by id dan index", () => {
|
|
491
|
+
const { client } = makeClient();
|
|
492
|
+
const builder = airich(client)
|
|
493
|
+
.text("a", { id: "a" })
|
|
494
|
+
.text("b", { id: "b" })
|
|
495
|
+
.text("c", { id: "c" });
|
|
496
|
+
|
|
497
|
+
builder.remove("b");
|
|
498
|
+
assert.deepEqual(builder.ids(), ["a", "c"]);
|
|
499
|
+
|
|
500
|
+
builder.remove(0);
|
|
501
|
+
assert.deepEqual(builder.ids(), ["c"]);
|
|
502
|
+
|
|
503
|
+
assert.throws(
|
|
504
|
+
() => builder.remove("b"),
|
|
505
|
+
(e: Error & { code?: string }) => e.code === "ITEM_NOT_FOUND",
|
|
506
|
+
);
|
|
507
|
+
assert.throws(
|
|
508
|
+
() => builder.remove(99),
|
|
509
|
+
(e: Error & { code?: string }) => e.code === "ITEM_NOT_FOUND",
|
|
510
|
+
);
|
|
511
|
+
assert.throws(
|
|
512
|
+
() => builder.remove(-1),
|
|
513
|
+
(e: Error & { code?: string }) => e.code === "ITEM_NOT_FOUND",
|
|
514
|
+
);
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
test("unit (airich): replace mempertahankan id lama saat id baru tidak diberi", () => {
|
|
518
|
+
const { client } = makeClient();
|
|
519
|
+
const builder = airich(client)
|
|
520
|
+
.text("a", { id: "a" })
|
|
521
|
+
.text("b", { id: "b" })
|
|
522
|
+
.text("c", { id: "c" });
|
|
523
|
+
|
|
524
|
+
builder.replace("b", {
|
|
525
|
+
view_model: {
|
|
526
|
+
primitive: { text: "B2", __typename: "GenAIMarkdownTextUXPrimitive" },
|
|
527
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
528
|
+
},
|
|
529
|
+
} as UnifiedResponseSection);
|
|
530
|
+
assert.deepEqual(builder.ids(), ["a", "b", "c"]);
|
|
531
|
+
|
|
532
|
+
assert.equal(L(primitiveOf(builder.build(), 1)).text, "B2");
|
|
533
|
+
|
|
534
|
+
builder.replace(
|
|
535
|
+
0,
|
|
536
|
+
{
|
|
537
|
+
view_model: {
|
|
538
|
+
primitive: { text: "A2", __typename: "GenAIMetadataTextPrimitive" },
|
|
539
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
540
|
+
},
|
|
541
|
+
} as UnifiedResponseSection,
|
|
542
|
+
undefined,
|
|
543
|
+
{ id: "a2" },
|
|
544
|
+
);
|
|
545
|
+
assert.deepEqual(builder.ids(), ["a2", "b", "c"]);
|
|
546
|
+
assert.throws(
|
|
547
|
+
() =>
|
|
548
|
+
builder.replace(
|
|
549
|
+
"b",
|
|
550
|
+
{ view_model: {} } as unknown as UnifiedResponseSection,
|
|
551
|
+
undefined,
|
|
552
|
+
{ id: "c" },
|
|
553
|
+
),
|
|
554
|
+
(e: Error & { code?: string }) => e.code === "DUPLICATE_ID",
|
|
555
|
+
);
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
test("unit (airich): insert, insertAt, urutan section dipertahankan", () => {
|
|
559
|
+
const { client } = makeClient();
|
|
560
|
+
const builder = airich(client).text("a", { id: "a" }).text("c", { id: "c" });
|
|
561
|
+
|
|
562
|
+
builder.insert(
|
|
563
|
+
"a",
|
|
564
|
+
{
|
|
565
|
+
view_model: {
|
|
566
|
+
primitive: { text: "b", __typename: "GenAIMetadataTextPrimitive" },
|
|
567
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
568
|
+
},
|
|
569
|
+
} as UnifiedResponseSection,
|
|
570
|
+
undefined,
|
|
571
|
+
{ id: "b" },
|
|
572
|
+
);
|
|
573
|
+
builder.insert(
|
|
574
|
+
"c",
|
|
575
|
+
{
|
|
576
|
+
view_model: {
|
|
577
|
+
primitive: {
|
|
578
|
+
text: "sebelum-c",
|
|
579
|
+
__typename: "GenAIMetadataTextPrimitive",
|
|
580
|
+
},
|
|
581
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
582
|
+
},
|
|
583
|
+
} as UnifiedResponseSection,
|
|
584
|
+
undefined,
|
|
585
|
+
{ id: "b2", after: false },
|
|
586
|
+
);
|
|
587
|
+
builder.insertAt(
|
|
588
|
+
0,
|
|
589
|
+
{
|
|
590
|
+
view_model: {
|
|
591
|
+
primitive: { text: "depan", __typename: "GenAIMetadataTextPrimitive" },
|
|
592
|
+
__typename: "GenAISingleLayoutViewModel",
|
|
593
|
+
},
|
|
594
|
+
} as UnifiedResponseSection,
|
|
595
|
+
undefined,
|
|
596
|
+
{ id: "depan" },
|
|
597
|
+
);
|
|
598
|
+
|
|
599
|
+
const payload = builder.build();
|
|
600
|
+
assert.deepEqual(
|
|
601
|
+
A(L(decodeSections(payload)).sections).map(
|
|
602
|
+
(s) => L(L(L(s).view_model).primitive).text,
|
|
603
|
+
),
|
|
604
|
+
["depan", "a", "b", "sebelum-c", "c"],
|
|
605
|
+
);
|
|
606
|
+
assert.throws(
|
|
607
|
+
() =>
|
|
608
|
+
builder.insert("zzz", {
|
|
609
|
+
view_model: {},
|
|
610
|
+
} as unknown as UnifiedResponseSection),
|
|
611
|
+
(e: Error & { code?: string }) => e.code === "ITEM_NOT_FOUND",
|
|
612
|
+
);
|
|
613
|
+
assert.throws(
|
|
614
|
+
() =>
|
|
615
|
+
builder.insertAt(42, {
|
|
616
|
+
view_model: {},
|
|
617
|
+
} as unknown as UnifiedResponseSection),
|
|
618
|
+
(e: Error & { code?: string }) => e.code === "INVALID_TARGET",
|
|
619
|
+
);
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
test("unit (airich): loadPayload dari payload AIRich lalu rebuild", () => {
|
|
623
|
+
const { client } = makeClient();
|
|
624
|
+
const source = airich(client)
|
|
625
|
+
.text("a", { id: "a" })
|
|
626
|
+
.code("python", "x = 1")
|
|
627
|
+
.table([["H"], ["v"]])
|
|
628
|
+
.build({ submessages: true });
|
|
629
|
+
|
|
630
|
+
const builder2 = new AIRich(client);
|
|
631
|
+
builder2.loadPayload(source);
|
|
632
|
+
|
|
633
|
+
const rebuilt = builder2.build();
|
|
634
|
+
const decodedSource = decodeSections(source);
|
|
635
|
+
const decodedRebuilt = decodeSections(rebuilt);
|
|
636
|
+
|
|
637
|
+
assert.deepEqual(
|
|
638
|
+
decodedRebuilt.response_id,
|
|
639
|
+
decodedSource.response_id,
|
|
640
|
+
"response_id muatan asli dipertahankan",
|
|
641
|
+
);
|
|
642
|
+
assert.deepEqual(
|
|
643
|
+
decodedRebuilt.sections,
|
|
644
|
+
decodedSource.sections,
|
|
645
|
+
"section harus identik setelah load+rebuild",
|
|
646
|
+
);
|
|
647
|
+
assert.equal(A(L(richOf(rebuilt)).submessages).length, 3);
|
|
648
|
+
|
|
649
|
+
assert.equal("a" in {}, false);
|
|
650
|
+
assert.deepEqual(builder2.ids(), [], "node hasil load tanpa id (netral)");
|
|
651
|
+
|
|
652
|
+
builder2.text("tambahan", { id: "tambahan" });
|
|
653
|
+
assert.equal(A(L(decodeSections(builder2.build())).sections).length, 4);
|
|
654
|
+
|
|
655
|
+
assert.throws(() => builder2.loadPayload({}), /richResponseMessage/);
|
|
656
|
+
});
|
|
657
|
+
|
|
658
|
+
test("unit (airich): clear mengosongkan node dan regenerasi response_id", () => {
|
|
659
|
+
const { client } = makeClient();
|
|
660
|
+
const builder = airich(client).text("a").text("b");
|
|
661
|
+
const before = decodeSections(builder.build());
|
|
662
|
+
|
|
663
|
+
builder.clear();
|
|
664
|
+
assert.equal(builder.size, 0);
|
|
665
|
+
assert.deepEqual(builder.ids(), []);
|
|
666
|
+
|
|
667
|
+
builder.text("c");
|
|
668
|
+
const after = decodeSections(builder.build());
|
|
669
|
+
assert.notEqual(after.response_id, before.response_id);
|
|
670
|
+
assert.equal(A(after.sections).length, 1);
|
|
671
|
+
assert.equal(L(primitiveOf(builder.build(), 0)).text, "c");
|
|
672
|
+
});
|
|
673
|
+
|
|
674
|
+
test("unit (airich): input tidak valid ditolak", () => {
|
|
675
|
+
const { client } = makeClient();
|
|
676
|
+
const builder = airich(client);
|
|
677
|
+
|
|
678
|
+
assert.throws(() => builder.text(123 as unknown as string), /harus string/);
|
|
679
|
+
assert.throws(
|
|
680
|
+
() => builder.code("js", 123 as unknown as string),
|
|
681
|
+
/harus string/,
|
|
682
|
+
);
|
|
683
|
+
assert.throws(
|
|
684
|
+
() => builder.table("bukan array" as unknown as string[][]),
|
|
685
|
+
/2 dimensi/,
|
|
686
|
+
);
|
|
687
|
+
assert.throws(
|
|
688
|
+
() => builder.table([[1, 2]] as unknown as string[][]),
|
|
689
|
+
/2 dimensi/,
|
|
690
|
+
);
|
|
691
|
+
assert.throws(() => builder.table([]), /2 dimensi/);
|
|
692
|
+
assert.throws(
|
|
693
|
+
() => builder.suggest(123 as unknown as string),
|
|
694
|
+
/suggest harus string/,
|
|
695
|
+
);
|
|
696
|
+
assert.throws(() => builder.suggest([]), /suggest harus string/);
|
|
697
|
+
assert.throws(() => builder.footer(123 as unknown as string), /harus string/);
|
|
698
|
+
assert.throws(
|
|
699
|
+
() => builder.raw("bukan object" as unknown as never),
|
|
700
|
+
/section harus object/,
|
|
701
|
+
);
|
|
702
|
+
assert.throws(() => builder.build(), /minimal satu konten/);
|
|
703
|
+
assert.throws(
|
|
704
|
+
() => new AIRich(client, { botJid: 123 as unknown as string }),
|
|
705
|
+
/botJid harus string/,
|
|
706
|
+
);
|
|
707
|
+
assert.throws(() => airich(asClient({})), /client harus WaClient/);
|
|
708
|
+
});
|
|
709
|
+
|
|
710
|
+
test("unit (airich): direct send (twoStep false) lewat client.message.send", async () => {
|
|
711
|
+
const { client, sends } = makeClient();
|
|
712
|
+
const builder = airich(client).text("langsung");
|
|
713
|
+
|
|
714
|
+
const result = await builder.send("6281@s.whatsapp.net", { twoStep: false });
|
|
715
|
+
|
|
716
|
+
assert.equal(sends.length, 1);
|
|
717
|
+
assert.equal((sends[0] as { jid?: unknown }).jid, "6281@s.whatsapp.net");
|
|
718
|
+
const content = L(sends[0]?.content);
|
|
719
|
+
assert.equal(
|
|
720
|
+
L(L(content.botForwardedMessage).message).protocolMessage,
|
|
721
|
+
undefined,
|
|
722
|
+
);
|
|
723
|
+
assert.ok(L(L(content.botForwardedMessage).message).richResponseMessage);
|
|
724
|
+
const firstOptions = sends[0] as { options?: unknown } | undefined;
|
|
725
|
+
assert.equal(L(L(firstOptions?.options).additionalAttributes).type, "text");
|
|
726
|
+
assert.equal((result as { id?: string }).id, "FAKEID_1");
|
|
727
|
+
assert.equal(builder.lastId, "FAKEID_1");
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
test("unit (airich): two-step send — placeholder dulu, edit belakangan", async () => {
|
|
731
|
+
const { client, sends } = makeClient();
|
|
732
|
+
const builder = airich(client).text("dua tahap");
|
|
733
|
+
|
|
734
|
+
const result = await builder.send("6281@s.whatsapp.net");
|
|
735
|
+
|
|
736
|
+
assert.equal(sends.length, 2, "placeholder + edit");
|
|
737
|
+
assert.equal(
|
|
738
|
+
L(L(L(sends[0]?.content).botForwardedMessage).message).protocolMessage,
|
|
739
|
+
undefined,
|
|
740
|
+
"pesan pertama placeholder",
|
|
741
|
+
);
|
|
742
|
+
assert.ok(
|
|
743
|
+
L(sends[0]?.content).messageContextInfo,
|
|
744
|
+
"placeholder bawa messageContextInfo",
|
|
745
|
+
);
|
|
746
|
+
|
|
747
|
+
const edit = L(
|
|
748
|
+
L(L(L(sends[1]?.content).botForwardedMessage).message).protocolMessage,
|
|
749
|
+
);
|
|
750
|
+
assert.equal(edit.type, 14);
|
|
751
|
+
assert.equal(
|
|
752
|
+
L(edit.key).id,
|
|
753
|
+
"FAKEID_1",
|
|
754
|
+
"edit menunjuk id hasil placeholder",
|
|
755
|
+
);
|
|
756
|
+
assert.equal(L(edit.key).fromMe, true);
|
|
757
|
+
assert.equal(L(edit.key).remoteJid, "6281@s.whatsapp.net");
|
|
758
|
+
|
|
759
|
+
assert.equal(
|
|
760
|
+
(result as { id?: string }).id,
|
|
761
|
+
"FAKEID_1",
|
|
762
|
+
"send() mengembalikan hasil placeholder (kompatibel)",
|
|
763
|
+
);
|
|
764
|
+
assert.equal(builder.lastId, "FAKEID_1");
|
|
765
|
+
assert.equal(
|
|
766
|
+
builder.pendingEdit("6281@s.whatsapp.net"),
|
|
767
|
+
null,
|
|
768
|
+
"tidak ada edit pending setelah sukses",
|
|
769
|
+
);
|
|
770
|
+
});
|
|
771
|
+
|
|
772
|
+
test("unit (airich): gagal placeholder — tidak lanjut ke edit, error bertahap", async () => {
|
|
773
|
+
const { client, sends } = makeClient({ failStage: 0 });
|
|
774
|
+
const builder = airich(client).text("x");
|
|
775
|
+
|
|
776
|
+
await assert.rejects(
|
|
777
|
+
builder.send("6281@s.whatsapp.net"),
|
|
778
|
+
(error: Error & { stage?: string }) => {
|
|
779
|
+
assert.ok(error instanceof ProtocolSendError);
|
|
780
|
+
assert.equal(error.stage, "placeholder");
|
|
781
|
+
assert.equal(sends.length, 1, "edit tidak boleh dikirim");
|
|
782
|
+
return true;
|
|
783
|
+
},
|
|
784
|
+
);
|
|
785
|
+
assert.equal(builder.lastId, null);
|
|
786
|
+
});
|
|
787
|
+
|
|
788
|
+
test("unit (airich): ID placeholder tidak valid — error validate_id, tidak lanjut edit", async () => {
|
|
789
|
+
const brokenClient = asClient({
|
|
790
|
+
message: { send: async () => ({ attempts: 1 }) },
|
|
791
|
+
});
|
|
792
|
+
const builder = new AIRich(brokenClient).text("x");
|
|
793
|
+
|
|
794
|
+
await assert.rejects(
|
|
795
|
+
builder.send("6281@s.whatsapp.net"),
|
|
796
|
+
(error: Error & { stage?: string }) => {
|
|
797
|
+
assert.ok(error instanceof ProtocolSendError);
|
|
798
|
+
assert.equal(error.stage, "validate_id");
|
|
799
|
+
return true;
|
|
800
|
+
},
|
|
801
|
+
);
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
test("unit (airich): gagal edit — TIDAK kirim placeholder kedua, ID tersimpan untuk retry", async () => {
|
|
805
|
+
const { client, sends } = makeClient({ failStage: 1 });
|
|
806
|
+
const builder = airich(client).text("x");
|
|
807
|
+
|
|
808
|
+
await assert.rejects(
|
|
809
|
+
builder.send("6281@s.whatsapp.net"),
|
|
810
|
+
(
|
|
811
|
+
error: Error & {
|
|
812
|
+
stage?: string;
|
|
813
|
+
placeholderId?: string;
|
|
814
|
+
cause?: unknown;
|
|
815
|
+
},
|
|
816
|
+
) => {
|
|
817
|
+
assert.ok(error instanceof ProtocolSendError);
|
|
818
|
+
assert.equal(error.stage, "edit");
|
|
819
|
+
assert.equal(error.placeholderId, "FAKEID_1");
|
|
820
|
+
assert.ok(error.cause, "penyebab asli dibawa");
|
|
821
|
+
return true;
|
|
822
|
+
},
|
|
823
|
+
);
|
|
824
|
+
|
|
825
|
+
assert.equal(
|
|
826
|
+
sends.length,
|
|
827
|
+
2,
|
|
828
|
+
"persis satu placeholder + satu percobaan edit",
|
|
829
|
+
);
|
|
830
|
+
assert.equal(builder.lastId, "FAKEID_1", "ID placeholder tersimpan");
|
|
831
|
+
assert.equal(
|
|
832
|
+
builder.pendingEdit("6281@s.whatsapp.net"),
|
|
833
|
+
"FAKEID_1",
|
|
834
|
+
"status edit pending tersedia",
|
|
835
|
+
);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
test("unit (airich): retry edit setelah gagal memakai edit(jid)", async () => {
|
|
839
|
+
let attempt = 0;
|
|
840
|
+
const sends: unknown[] = [];
|
|
841
|
+
const client = asClient({
|
|
842
|
+
message: {
|
|
843
|
+
send: async (_jid: unknown, content: unknown) => {
|
|
844
|
+
sends.push(content);
|
|
845
|
+
attempt++;
|
|
846
|
+
if (attempt === 2) throw new Error("sementara gagal");
|
|
847
|
+
return { id: `MSG_${attempt}` };
|
|
848
|
+
},
|
|
849
|
+
},
|
|
850
|
+
});
|
|
851
|
+
|
|
852
|
+
const builder = airich(client).text("x");
|
|
853
|
+
await assert.rejects(
|
|
854
|
+
builder.send("6281@s.whatsapp.net"),
|
|
855
|
+
(e: Error & { stage?: string }) => e.stage === "edit",
|
|
856
|
+
);
|
|
857
|
+
|
|
858
|
+
const retryResult = await builder.edit("6281@s.whatsapp.net");
|
|
859
|
+
assert.equal(
|
|
860
|
+
(retryResult as { id?: string }).id,
|
|
861
|
+
"MSG_3",
|
|
862
|
+
"edit ulang memakai id placeholder tersimpan",
|
|
863
|
+
);
|
|
864
|
+
assert.equal(sends.length, 3);
|
|
865
|
+
const protocolMessage = L(
|
|
866
|
+
L(L(sends[2]).botForwardedMessage).message,
|
|
867
|
+
).protocolMessage;
|
|
868
|
+
assert.equal(
|
|
869
|
+
L(protocolMessage).key !== undefined
|
|
870
|
+
? L(L(protocolMessage).key).id
|
|
871
|
+
: undefined,
|
|
872
|
+
"MSG_1",
|
|
873
|
+
);
|
|
874
|
+
assert.equal(
|
|
875
|
+
builder.pendingEdit("6281@s.whatsapp.net"),
|
|
876
|
+
null,
|
|
877
|
+
"pending dibersihkan setelah retry sukses",
|
|
878
|
+
);
|
|
879
|
+
});
|
|
880
|
+
|
|
881
|
+
test("unit (airich): send options diteruskan ke transport (kecuali twoStep/submessages)", async () => {
|
|
882
|
+
const { client, sends } = makeClient();
|
|
883
|
+
const builder = airich(client).text("x");
|
|
884
|
+
|
|
885
|
+
await builder.send("6281@s.whatsapp.net", {
|
|
886
|
+
twoStep: false,
|
|
887
|
+
additionalAttributes: { ephemerality: "ephemeral" },
|
|
888
|
+
});
|
|
889
|
+
assert.deepEqual(L(L(sends[0]?.options).additionalAttributes), {
|
|
890
|
+
type: "text",
|
|
891
|
+
ephemerality: "ephemeral",
|
|
892
|
+
});
|
|
893
|
+
});
|
|
894
|
+
|
|
895
|
+
test("unit (airich): state tidak bocor antar pengiriman", async () => {
|
|
896
|
+
const { client, sends } = makeClient();
|
|
897
|
+
const builder = airich(client);
|
|
898
|
+
|
|
899
|
+
await builder
|
|
900
|
+
.text("kirim satu")
|
|
901
|
+
.send("6281@s.whatsapp.net", { twoStep: false });
|
|
902
|
+
|
|
903
|
+
await builder.text("kirim dua").send("6281@s.whatsapp.net");
|
|
904
|
+
|
|
905
|
+
const directPayload = sends[0]?.content;
|
|
906
|
+
const placeholderPayload = sends[1]?.content;
|
|
907
|
+
const editPayload = sends[2]?.content;
|
|
908
|
+
|
|
909
|
+
const directDecoded = decodeSections(directPayload);
|
|
910
|
+
const editDecoded = decodeSections(editPayload);
|
|
911
|
+
|
|
912
|
+
assert.equal(
|
|
913
|
+
directDecoded.response_id,
|
|
914
|
+
editDecoded.response_id,
|
|
915
|
+
"response_id per-instance dipertahankan (perilaku saat ini, kecuali clear())",
|
|
916
|
+
);
|
|
917
|
+
|
|
918
|
+
const otherBuilder = airich(client).text("builder lain");
|
|
919
|
+
const otherDecoded = decodeSections(
|
|
920
|
+
otherBuilder.build({ submessages: false }),
|
|
921
|
+
);
|
|
922
|
+
assert.notEqual(
|
|
923
|
+
otherDecoded.response_id,
|
|
924
|
+
directDecoded.response_id,
|
|
925
|
+
"response_id beda antar instance",
|
|
926
|
+
);
|
|
927
|
+
|
|
928
|
+
assert.ok(
|
|
929
|
+
L(L(richOf(placeholderPayload)).unifiedResponse).data instanceof Uint8Array,
|
|
930
|
+
);
|
|
931
|
+
assert.ok(
|
|
932
|
+
L(L(richOf(directPayload)).unifiedResponse).data instanceof Uint8Array,
|
|
933
|
+
);
|
|
934
|
+
|
|
935
|
+
const directPlaceholder = await (async () => {
|
|
936
|
+
const other = airich(client);
|
|
937
|
+
const before = other.buildPlaceholder();
|
|
938
|
+
const after = other.buildPlaceholder();
|
|
939
|
+
assert.notEqual(
|
|
940
|
+
Buffer.compare(
|
|
941
|
+
Buffer.from(before.messageContextInfo.messageSecret),
|
|
942
|
+
Buffer.from(after.messageContextInfo.messageSecret),
|
|
943
|
+
),
|
|
944
|
+
0,
|
|
945
|
+
"messageSecret placeholder selalu baru per panggilan",
|
|
946
|
+
);
|
|
947
|
+
return before;
|
|
948
|
+
})();
|
|
949
|
+
|
|
950
|
+
assert.notEqual(
|
|
951
|
+
Buffer.compare(
|
|
952
|
+
Buffer.from(
|
|
953
|
+
(
|
|
954
|
+
placeholderPayload as {
|
|
955
|
+
messageContextInfo: { messageSecret: Uint8Array };
|
|
956
|
+
}
|
|
957
|
+
).messageContextInfo.messageSecret,
|
|
958
|
+
),
|
|
959
|
+
Buffer.from(directPlaceholder.messageContextInfo.messageSecret),
|
|
960
|
+
),
|
|
961
|
+
0,
|
|
962
|
+
"messageSecret beda antar pengiriman (tidak ada state statis)",
|
|
963
|
+
);
|
|
964
|
+
});
|
|
965
|
+
|
|
966
|
+
test("unit (airich): messageKit menyediakan airich(client, options)", () => {
|
|
967
|
+
const { client } = makeClient();
|
|
968
|
+
const builder = messageKit(client).airich();
|
|
969
|
+
assert.ok(builder instanceof AIRich);
|
|
970
|
+
assert.equal(builder.size, 0);
|
|
971
|
+
});
|
|
972
|
+
|
|
973
|
+
test("unit (airich): table padding kolom dan header", () => {
|
|
974
|
+
const { client } = makeClient();
|
|
975
|
+
const payload = airich(client)
|
|
976
|
+
.table([["A", "B", "C"], ["satu"]])
|
|
977
|
+
.build();
|
|
978
|
+
const rows = A(L(primitiveOf(payload, 0)).rows);
|
|
979
|
+
|
|
980
|
+
assert.equal(rows.length, 2);
|
|
981
|
+
assert.deepEqual(L(rows[0]).cells, ["A", "B", "C"]);
|
|
982
|
+
assert.deepEqual(L(rows[1]).cells, ["satu", "", ""]);
|
|
983
|
+
assert.equal(L(rows[0]).is_header, true);
|
|
984
|
+
assert.equal(L(rows[1]).is_header, false);
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
test("unit (airich): footer jadi section metadata di akhir", () => {
|
|
988
|
+
const { client } = makeClient();
|
|
989
|
+
const payload = airich(client).text("isi").footer("catatan kaki").build();
|
|
990
|
+
const sections = A(L(decodeSections(payload)).sections);
|
|
991
|
+
assert.equal(sections.length, 2);
|
|
992
|
+
assert.equal(
|
|
993
|
+
L(L(L(sections[1]).view_model).primitive).__typename,
|
|
994
|
+
"GenAIMetadataTextPrimitive",
|
|
995
|
+
);
|
|
996
|
+
assert.equal(L(L(L(sections[1]).view_model).primitive).text, "catatan kaki");
|
|
997
|
+
});
|
|
998
|
+
|
|
999
|
+
test("unit (airich): build({ submessages: false }) mengosongkan submessages", () => {
|
|
1000
|
+
const { client } = makeClient();
|
|
1001
|
+
const rich = airich(client)
|
|
1002
|
+
.text("x")
|
|
1003
|
+
.code("js", "y")
|
|
1004
|
+
.build({ submessages: false });
|
|
1005
|
+
assert.deepEqual(L(richOf(rich)).submessages, []);
|
|
1006
|
+
});
|