@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.
Files changed (191) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +20 -0
  3. package/README.md +1401 -2
  4. package/build.config.json +28 -0
  5. package/dist/airich/airich.d.ts +180 -0
  6. package/dist/airich/airich.js +548 -0
  7. package/dist/airich/context.d.ts +24 -0
  8. package/dist/airich/context.js +48 -0
  9. package/dist/airich/encode.d.ts +3 -0
  10. package/dist/airich/encode.js +16 -0
  11. package/dist/airich/index.d.ts +20 -0
  12. package/dist/airich/index.js +11 -0
  13. package/dist/airich/inline.d.ts +45 -0
  14. package/dist/airich/inline.js +152 -0
  15. package/dist/airich/nodes.d.ts +30 -0
  16. package/dist/airich/nodes.js +121 -0
  17. package/dist/airich/primitives.d.ts +144 -0
  18. package/dist/airich/primitives.js +230 -0
  19. package/dist/airich/protocol.d.ts +35 -0
  20. package/dist/airich/protocol.js +65 -0
  21. package/dist/airich/table.d.ts +22 -0
  22. package/dist/airich/table.js +35 -0
  23. package/dist/airich/tokenizer.d.ts +13 -0
  24. package/dist/airich/tokenizer.js +717 -0
  25. package/dist/airich/transport.d.ts +15 -0
  26. package/dist/airich/transport.js +56 -0
  27. package/dist/branding.d.ts +5 -0
  28. package/dist/branding.js +9 -0
  29. package/dist/btn/builders.d.ts +246 -0
  30. package/dist/btn/builders.js +710 -0
  31. package/dist/btn/media-slot.d.ts +28 -0
  32. package/dist/btn/media-slot.js +323 -0
  33. package/dist/btn/media-source.d.ts +36 -0
  34. package/dist/btn/media-source.js +116 -0
  35. package/dist/btn/sharp-loader.d.ts +5 -0
  36. package/dist/btn/sharp-loader.js +18 -0
  37. package/dist/btn/thumbnail.d.ts +21 -0
  38. package/dist/btn/thumbnail.js +124 -0
  39. package/dist/btn/validators.d.ts +40 -0
  40. package/dist/btn/validators.js +135 -0
  41. package/dist/btn.d.ts +1 -0
  42. package/dist/btn.js +1 -0
  43. package/dist/core/async-send.d.ts +20 -0
  44. package/dist/core/async-send.js +1 -0
  45. package/dist/core/base-builder.d.ts +20 -0
  46. package/dist/core/base-builder.js +67 -0
  47. package/dist/core/errors.d.ts +30 -0
  48. package/dist/core/errors.js +61 -0
  49. package/dist/download.d.ts +24 -0
  50. package/dist/download.js +123 -0
  51. package/dist/index.d.ts +38 -0
  52. package/dist/index.js +22 -0
  53. package/dist/integration/augment.d.ts +97 -0
  54. package/dist/integration/augment.js +1 -0
  55. package/dist/integration/auto.d.ts +8 -0
  56. package/dist/integration/auto.js +79 -0
  57. package/dist/integration/index.d.ts +4 -0
  58. package/dist/integration/index.js +2 -0
  59. package/dist/integration/methods.d.ts +25 -0
  60. package/dist/integration/methods.js +24 -0
  61. package/dist/interactive.d.ts +24 -0
  62. package/dist/interactive.js +121 -0
  63. package/dist/media-gate.d.ts +99 -0
  64. package/dist/media-gate.js +322 -0
  65. package/dist/message/album.d.ts +33 -0
  66. package/dist/message/album.js +107 -0
  67. package/dist/message/contact.d.ts +29 -0
  68. package/dist/message/contact.js +123 -0
  69. package/dist/message/forward.d.ts +23 -0
  70. package/dist/message/forward.js +77 -0
  71. package/dist/message/index.d.ts +26 -0
  72. package/dist/message/index.js +13 -0
  73. package/dist/message/link-preview.d.ts +28 -0
  74. package/dist/message/link-preview.js +98 -0
  75. package/dist/message/location.d.ts +24 -0
  76. package/dist/message/location.js +94 -0
  77. package/dist/message/media.d.ts +90 -0
  78. package/dist/message/media.js +123 -0
  79. package/dist/message/poll.d.ts +24 -0
  80. package/dist/message/poll.js +98 -0
  81. package/dist/message/reaction.d.ts +20 -0
  82. package/dist/message/reaction.js +71 -0
  83. package/dist/message/sticker.d.ts +29 -0
  84. package/dist/message/sticker.js +44 -0
  85. package/dist/message/target.d.ts +18 -0
  86. package/dist/message/target.js +24 -0
  87. package/dist/message/text.d.ts +17 -0
  88. package/dist/message/text.js +29 -0
  89. package/dist/message/vcard.d.ts +9 -0
  90. package/dist/message/vcard.js +23 -0
  91. package/dist/message/webp.d.ts +6 -0
  92. package/dist/message/webp.js +57 -0
  93. package/dist/payloads.d.ts +180 -0
  94. package/dist/payloads.js +1 -0
  95. package/dist/serialize.d.ts +41 -0
  96. package/dist/serialize.js +93 -0
  97. package/dist/status.d.ts +3 -0
  98. package/dist/status.js +21 -0
  99. package/dist/zapo-client.d.ts +44 -0
  100. package/dist/zapo-client.js +1 -0
  101. package/examples/airich.ts +59 -0
  102. package/examples/buttons.ts +48 -0
  103. package/examples/client-methods.ts +58 -0
  104. package/examples/hybrid.ts +133 -0
  105. package/examples/serialize.ts +61 -0
  106. package/package.json +271 -4
  107. package/scripts/check-no-comments.mjs +101 -0
  108. package/scripts/compare-golden.mjs +461 -0
  109. package/scripts/lib-config.mjs +9 -0
  110. package/scripts/smoke-auto.mjs +75 -0
  111. package/scripts/test-external-consumer.mjs +316 -0
  112. package/scripts/verify-package.mjs +78 -0
  113. package/src/airich/airich.ts +970 -0
  114. package/src/airich/context.ts +96 -0
  115. package/src/airich/encode.ts +25 -0
  116. package/src/airich/index.ts +98 -0
  117. package/src/airich/inline.ts +272 -0
  118. package/src/airich/nodes.ts +174 -0
  119. package/src/airich/primitives.ts +474 -0
  120. package/src/airich/protocol.ts +133 -0
  121. package/src/airich/table.ts +70 -0
  122. package/src/airich/tokenizer.ts +799 -0
  123. package/src/airich/transport.ts +106 -0
  124. package/src/branding.ts +23 -0
  125. package/src/btn/builders.ts +1073 -0
  126. package/src/btn/media-slot.ts +486 -0
  127. package/src/btn/media-source.ts +189 -0
  128. package/src/btn/sharp-loader.ts +23 -0
  129. package/src/btn/thumbnail.ts +182 -0
  130. package/src/btn/validators.ts +237 -0
  131. package/src/btn.ts +8 -0
  132. package/src/core/async-send.ts +28 -0
  133. package/src/core/base-builder.ts +104 -0
  134. package/src/core/errors.ts +82 -0
  135. package/src/download.ts +212 -0
  136. package/src/index.ts +229 -0
  137. package/src/integration/augment.ts +262 -0
  138. package/src/integration/auto.ts +103 -0
  139. package/src/integration/index.ts +8 -0
  140. package/src/integration/methods.ts +45 -0
  141. package/src/interactive.ts +188 -0
  142. package/src/media-gate.ts +499 -0
  143. package/src/message/album.ts +187 -0
  144. package/src/message/contact.ts +170 -0
  145. package/src/message/forward.ts +123 -0
  146. package/src/message/index.ts +83 -0
  147. package/src/message/link-preview.ts +148 -0
  148. package/src/message/location.ts +150 -0
  149. package/src/message/media.ts +323 -0
  150. package/src/message/poll.ts +158 -0
  151. package/src/message/reaction.ts +133 -0
  152. package/src/message/sticker.ts +100 -0
  153. package/src/message/target.ts +38 -0
  154. package/src/message/text.ts +61 -0
  155. package/src/message/vcard.ts +36 -0
  156. package/src/message/webp.ts +78 -0
  157. package/src/payloads.ts +204 -0
  158. package/src/serialize.ts +180 -0
  159. package/src/status.ts +29 -0
  160. package/src/zapo-client.ts +84 -0
  161. package/test/airich-inline.test.ts +200 -0
  162. package/test/airich-primitives.test.ts +226 -0
  163. package/test/airich-tokenizer.test.ts +145 -0
  164. package/test/airich.golden.test.ts +406 -0
  165. package/test/airich.test.ts +1006 -0
  166. package/test/btn-button.test.ts +416 -0
  167. package/test/btn-header.test.ts +646 -0
  168. package/test/btn-legacy.test.ts +342 -0
  169. package/test/btn-lifecycle.test.ts +295 -0
  170. package/test/btn-list.test.ts +261 -0
  171. package/test/btn-media-source.test.ts +150 -0
  172. package/test/btn-thumbnail.test.ts +160 -0
  173. package/test/builder-compat.test.ts +264 -0
  174. package/test/helpers.ts +27 -0
  175. package/test/hybrid-airich.test.ts +345 -0
  176. package/test/hybrid-client.test.ts +239 -0
  177. package/test/hybrid-errors.test.ts +557 -0
  178. package/test/hybrid-interactive.test.ts +455 -0
  179. package/test/hybrid.golden.test.ts +349 -0
  180. package/test/hybrid.test.ts +479 -0
  181. package/test/integration-auto.test.ts +159 -0
  182. package/test/integration.fakeserver.test.ts +440 -0
  183. package/test/media-gate.test.ts +215 -0
  184. package/test/message-album.test.ts +145 -0
  185. package/test/message-builders.test.ts +401 -0
  186. package/test/message-media.test.ts +303 -0
  187. package/test/message.fakeserver.test.ts +284 -0
  188. package/test/message.golden.test.ts +254 -0
  189. package/test/serializer.test.ts +448 -0
  190. package/tsconfig.json +26 -0
  191. package/tsconfig.test.json +10 -0
@@ -0,0 +1,264 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { button, legacyButton, listMessage } from "@kyyinfinite/zapo-message";
5
+ import { ValidationError, DuplicateIdError } from "@kyyinfinite/zapo-message";
6
+ import type { NativeSection } from "@kyyinfinite/zapo-message";
7
+ import { asClient, first } from "./helpers.js";
8
+
9
+ const makeClient = () => {
10
+ const uploads: { source?: unknown; options?: Record<string, unknown> }[] = [];
11
+ const sent: { to?: unknown; content?: unknown; options?: unknown }[] = [];
12
+ return {
13
+ message: {
14
+ send: async (to: unknown, content: unknown, options: unknown) => {
15
+ sent.push({ to, content, options });
16
+ return { id: `MSG-${sent.length}`, attempts: 1, ack: {} };
17
+ },
18
+ upload: async (source: unknown, options: Record<string, unknown>) => {
19
+ uploads.push({ source, options });
20
+ return {
21
+ url: "https://mmg.whatsapp.net/uploaded",
22
+ directPath: "/v/t62/uploaded",
23
+ fileSha256: new Uint8Array([1, 2, 3]),
24
+ fileEncSha256: new Uint8Array([4, 5, 6]),
25
+ mediaKey: new Uint8Array([7, 8, 9]),
26
+ mediaKeyTimestamp: 1730000000,
27
+ fileLength: 1234,
28
+ mimetype: options?.mimetype,
29
+ };
30
+ },
31
+ },
32
+ __uploads: uploads,
33
+ __sent: sent,
34
+ };
35
+ };
36
+
37
+ const clientOf = (mock: ReturnType<typeof makeClient>) => asClient(mock);
38
+ const SECTIONS: NativeSection[] = [
39
+ {
40
+ title: "Utama",
41
+ rows: [{ id: "row_a", title: "Baris A", description: "desc" }],
42
+ },
43
+ ];
44
+
45
+ test("builder: quick_reply + build shape + send default via defaultTo/defaultQuote", async () => {
46
+ const mock = makeClient();
47
+ const client = clientOf(mock);
48
+ const m = {
49
+ key: { remoteJid: "6281@s.whatsapp.net", isGroup: false },
50
+ message: { conversation: "tes" },
51
+ };
52
+
53
+ const msg = button(client)
54
+ .defaultTo("6281@s.whatsapp.net")
55
+ .defaultQuote(m)
56
+ .title("Avelyna")
57
+ .body("Pilih menu")
58
+ .footer("Avelyna Bot")
59
+ .quick_reply("Tools", "menu tools");
60
+
61
+ await msg.send();
62
+
63
+ const call = mock.__sent[0] as {
64
+ to?: unknown;
65
+ options?: unknown;
66
+ content?: unknown;
67
+ };
68
+ assert.equal(call.to, "6281@s.whatsapp.net");
69
+ assert.equal((call.options as { quote?: unknown }).quote, m);
70
+ const content = (
71
+ call.content as { interactiveMessage: Record<string, unknown> }
72
+ ).interactiveMessage;
73
+ assert.equal((content.header as Record<string, unknown>).title, "Avelyna");
74
+ assert.equal((content.body as Record<string, unknown>).text, "Pilih menu");
75
+ assert.equal((content.footer as Record<string, unknown>).text, "Avelyna Bot");
76
+ const flow = content.nativeFlowMessage as Record<string, unknown>;
77
+ assert.equal(first(flow.buttons as { name: string }[]).name, "quick_reply");
78
+ assert.deepEqual(
79
+ JSON.parse(
80
+ first(flow.buttons as { buttonParamsJson: string }[]).buttonParamsJson,
81
+ ),
82
+ { display_text: "Tools", id: "menu tools" },
83
+ );
84
+ assert.equal(flow.messageVersion, 1);
85
+ });
86
+
87
+ test("builder: send(jid, { quote }) menimpa default", async () => {
88
+ const mock = makeClient();
89
+ const client = clientOf(mock);
90
+ const builder = button(client)
91
+ .defaultTo("628DEFAULT@s.whatsapp.net")
92
+ .defaultQuote({ key: "DEFAULT" });
93
+ builder.quick_reply("Halo", "x1");
94
+ await builder.send("628OTHER@s.whatsapp.net", { quote: { key: "CUSTOM" } });
95
+ const call = mock.__sent[0] as { to?: unknown; options?: unknown };
96
+ assert.equal(call.to, "628OTHER@s.whatsapp.net");
97
+ assert.deepEqual((call.options as { quote?: unknown }).quote, {
98
+ key: "CUSTOM",
99
+ });
100
+ });
101
+
102
+ test("builder: validasi native flow gagal dengan error kelas lama", async () => {
103
+ const client = clientOf(makeClient());
104
+
105
+ await assert.rejects(() => button(client).body("x").build(), ValidationError);
106
+ assert.throws(() => button(client).quick_reply("", "a"), ValidationError);
107
+ assert.throws(() => button(client).quick_reply("ok", ""), ValidationError);
108
+ const b = button(client).quick_reply("satu", "sama");
109
+ assert.throws(() => b.quick_reply("dua", "sama"), DuplicateIdError);
110
+ assert.throws(
111
+ () => button(client).cta_url("klik", "ftp://x"),
112
+ ValidationError,
113
+ );
114
+ assert.throws(
115
+ () => button(client).cta_url("klik", "bukan-url"),
116
+ ValidationError,
117
+ );
118
+ assert.throws(() => button(client).cta_copy("copy", ""), ValidationError);
119
+ assert.throws(
120
+ () => button(client).single_select("pilih", []),
121
+ ValidationError,
122
+ );
123
+ assert.throws(
124
+ () =>
125
+ button(client).single_select("pilih", [
126
+ { title: "A", rows: [] },
127
+ ] as NativeSection[]),
128
+ ValidationError,
129
+ );
130
+ assert.throws(
131
+ () =>
132
+ button(client).single_select("pilih", [
133
+ { title: "A", rows: [{ id: "r1", title: "x" }] },
134
+ { title: "B", rows: [{ id: "r1", title: "y" }] },
135
+ ]),
136
+ DuplicateIdError,
137
+ );
138
+ assert.throws(
139
+ () => button(client).document(Buffer.from("x"), {}),
140
+ ValidationError,
141
+ );
142
+ });
143
+
144
+ test("builder: legacy button — unik, maks 3, build shape", async () => {
145
+ const client = clientOf(makeClient());
146
+ const content = await legacyButton(client)
147
+ .body("tes legacy")
148
+ .footer("f")
149
+ .button("btn1", "Tombol 1")
150
+ .button("btn2", "Tombol 2")
151
+ .build();
152
+
153
+ assert.equal(content.buttonsMessage.buttons.length, 2);
154
+ assert.equal(content.buttonsMessage.buttons[0]?.buttonId, "btn1");
155
+ assert.equal(content.buttonsMessage.buttons[0]?.type, 1);
156
+
157
+ const dup = legacyButton(client).button("dup", "a");
158
+ assert.throws(() => dup.button("dup", "b"), /udah dipake/);
159
+ const max = legacyButton(client)
160
+ .button("1", "a")
161
+ .button("2", "b")
162
+ .button("3", "c");
163
+ assert.throws(() => max.button("4", "d"), /maksimal/);
164
+ });
165
+
166
+ test("builder: list message klasik — build() selalu klasik, fallback di send()", async () => {
167
+ const mock = makeClient();
168
+ const client = clientOf(mock);
169
+
170
+ const privateList = listMessage(client)
171
+ .title("Menu")
172
+ .body("daftar")
173
+ .section("Utama", [{ id: "menu tools", title: "Tools" }]);
174
+ const content = await privateList.build();
175
+ const firstRow = (
176
+ content.listMessage.sections[0] as NonNullable<
177
+ (typeof content.listMessage.sections)[0]
178
+ >
179
+ ).rows as { rowId?: string }[];
180
+ assert.equal(firstRow[0]?.rowId, "menu tools");
181
+ assert.equal(content.listMessage.listType, 1);
182
+
183
+ await privateList.send("6281@s.whatsapp.net");
184
+ assert.ok((mock.__sent[0]?.content as Record<string, unknown>).listMessage);
185
+
186
+ await listMessage(client)
187
+ .fallback("native")
188
+ .title("M")
189
+ .section("S", [{ id: "r", title: "t" }])
190
+ .send("6281@s.whatsapp.net");
191
+ assert.ok(
192
+ (mock.__sent[1]?.content as Record<string, unknown>).interactiveMessage,
193
+ );
194
+
195
+ await listMessage(client)
196
+ .fallback("never")
197
+ .title("M")
198
+ .section("S", [{ id: "r", title: "t" }])
199
+ .send("1203@g.us");
200
+ assert.ok((mock.__sent[2]?.content as Record<string, unknown>).listMessage);
201
+
202
+ await assert.rejects(
203
+ async () => listMessage(client).build(),
204
+ /minimal satu section/,
205
+ );
206
+ });
207
+
208
+ test("builder: header media image via upload + lifecycle", async () => {
209
+ const mock = makeClient();
210
+ const client = clientOf(mock);
211
+ const png = Buffer.from([0x89, 0x50, 0x4e, 0x47, 13, 10, 26, 10]);
212
+ const content = await button(client)
213
+ .body("header gambar")
214
+ .image(png, { mimetype: "image/png" })
215
+ .quick_reply("ok", "ok1")
216
+ .build();
217
+
218
+ assert.equal(mock.__uploads.length, 1);
219
+ assert.equal(
220
+ (mock.__uploads[0] as { options?: Record<string, unknown> }).options?.type,
221
+ "image",
222
+ );
223
+ const header = (
224
+ content.interactiveMessage as unknown as Record<string, unknown>
225
+ ).header as Record<string, unknown>;
226
+ assert.equal(header.hasMediaAttachment, true);
227
+ const image = header.imageMessage as Record<string, unknown>;
228
+ assert.equal(image.directPath, "/v/t62/uploaded");
229
+ assert.equal(image.mimetype, "image/png");
230
+ assert.ok(image.fileSha256);
231
+ assert.ok(image.mediaKeyTimestamp);
232
+ });
233
+
234
+ test("builder: cta_call validasi nomor E.164 sesuai sumber", async () => {
235
+ const client = clientOf(makeClient());
236
+ const content = await button(client)
237
+ .body("hubungi")
238
+ .cta_call("Telepon", "+628123456789")
239
+ .send_location("Kirim lokasi")
240
+ .build();
241
+
242
+ const flow = (
243
+ content.interactiveMessage as unknown as Record<string, unknown>
244
+ ).nativeFlowMessage as Record<string, unknown>;
245
+ const names = flow.buttons as { name: string }[];
246
+ assert.deepEqual(
247
+ names.map((b) => b.name),
248
+ ["cta_call", "send_location"],
249
+ );
250
+ const btn = flow.buttons as { name: string; buttonParamsJson: string }[];
251
+ assert.equal(
252
+ JSON.parse(btn[0]?.buttonParamsJson ?? "{}").phone_number,
253
+ "+628123456789",
254
+ );
255
+
256
+ assert.throws(
257
+ () => button(client).cta_call("x", "abc"),
258
+ /nomor telepon tidak valid/,
259
+ );
260
+ assert.throws(
261
+ () => button(client).cta_call("x", "12"),
262
+ /nomor telepon tidak valid/,
263
+ );
264
+ });
@@ -0,0 +1,27 @@
1
+ import type {
2
+ ZapoClient,
3
+ ZapoDownloadCapableClient,
4
+ } from "@kyyinfinite/zapo-message";
5
+
6
+ export const asClient = (value: unknown): ZapoClient => value as ZapoClient;
7
+
8
+ export const asDownloadClient = (value: unknown): ZapoDownloadCapableClient =>
9
+ value as ZapoDownloadCapableClient;
10
+
11
+ export const L = (value: unknown): Record<string, unknown> =>
12
+ value as Record<string, unknown>;
13
+ export const A = (value: unknown): unknown[] => value as unknown[];
14
+
15
+ export const first = <T>(list: readonly T[]): T => list[0] as T;
16
+
17
+ export interface UploadCall {
18
+ source: unknown;
19
+ options: Record<string, unknown>;
20
+ }
21
+
22
+ export interface SendCall {
23
+ jid?: unknown;
24
+ to?: unknown;
25
+ content?: unknown;
26
+ options?: unknown;
27
+ }
@@ -0,0 +1,345 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import {
5
+ airich,
6
+ decodeUnifiedResponse,
7
+ AIRich,
8
+ type AIRichConfig,
9
+ type AIRichConstructorOptions,
10
+ } from "@kyyinfinite/zapo-message";
11
+ import { applyAIRichConfig } from "@kyyinfinite/zapo-message/airich/airich.js";
12
+ import { asClient } from "./helpers.js";
13
+
14
+ interface CapturedSend {
15
+ to: unknown;
16
+ content: unknown;
17
+ options: unknown;
18
+ }
19
+
20
+ const captureClient = () => {
21
+ const sends: CapturedSend[] = [];
22
+ const client = asClient({
23
+ message: {
24
+ send: async (to: unknown, content: unknown, options: unknown) => {
25
+ sends.push({ to, content, options });
26
+ return { id: `ID-${sends.length}`, attempts: 1, ack: {} };
27
+ },
28
+ },
29
+ });
30
+ return { client, sends };
31
+ };
32
+
33
+ const asRecord = (value: unknown): Record<string, unknown> =>
34
+ value as Record<string, unknown>;
35
+
36
+ const richOfDirect = (content: unknown): Record<string, unknown> =>
37
+ asRecord(
38
+ asRecord(asRecord(asRecord(content).botForwardedMessage).message)
39
+ .richResponseMessage,
40
+ );
41
+
42
+ const richOfEdit = (content: unknown): Record<string, unknown> =>
43
+ asRecord(
44
+ asRecord(
45
+ asRecord(
46
+ asRecord(
47
+ asRecord(
48
+ asRecord(asRecord(asRecord(content).botForwardedMessage).message)
49
+ .protocolMessage,
50
+ ).editedMessage,
51
+ ).botForwardedMessage,
52
+ ).message,
53
+ ).richResponseMessage,
54
+ );
55
+
56
+ const decodedOf = (rich: Record<string, unknown>): Record<string, unknown> => {
57
+ const data = asRecord(rich.unifiedResponse).data as Uint8Array;
58
+ const decoded = asRecord(decodeUnifiedResponse(data as string | Uint8Array));
59
+ return {
60
+ ...decoded,
61
+ response_id: "MASKED",
62
+ };
63
+ };
64
+
65
+ const comparableEdit = (content: unknown): Record<string, unknown> => {
66
+ const rich = richOfEdit(content);
67
+ return {
68
+ key: asRecord(
69
+ asRecord(asRecord(asRecord(content).botForwardedMessage).message)
70
+ .protocolMessage,
71
+ ).key,
72
+ type: asRecord(
73
+ asRecord(asRecord(asRecord(content).botForwardedMessage).message)
74
+ .protocolMessage,
75
+ ).type,
76
+ rich: {
77
+ submessages: rich.submessages,
78
+ contextInfo: plain(rich.contextInfo),
79
+ unified: decodedOf(rich),
80
+ },
81
+ };
82
+ };
83
+
84
+ const plain = (value: unknown): unknown =>
85
+ JSON.parse(
86
+ JSON.stringify(value, (_key, raw) => {
87
+ if (raw instanceof Uint8Array) return { $bytes: raw.byteLength };
88
+ return raw;
89
+ }),
90
+ );
91
+
92
+ const allPrimitivesConfig = (): AIRichConfig => ({
93
+ sections: [
94
+ { type: "text", text: "Welcome" },
95
+ { type: "code", language: "javascript", code: "console.log('Hello')" },
96
+ {
97
+ type: "table",
98
+ rows: [
99
+ ["Item", "Qty"],
100
+ ["Kopi", "2"],
101
+ ],
102
+ },
103
+ { type: "metadata", text: "meta info" },
104
+ { type: "tip", text: "Helpful information" },
105
+ {
106
+ type: "suggest",
107
+ items: ["ulangi pesanan", "lihat menu"],
108
+ },
109
+ {
110
+ type: "source",
111
+ sources: [{ favicon: "https://ex.com/f.ico", url: "https://ex.com" }],
112
+ },
113
+ { type: "image", url: "https://ex.com/pic.jpg", width: 512, height: 512 },
114
+ {
115
+ type: "video",
116
+ url: "https://ex.com/clip.mp4",
117
+ mimeType: "video/mp4",
118
+ duration: 12,
119
+ },
120
+ {
121
+ type: "footerAction",
122
+ items: [{ text: "footer", cta_text: "Buka", url: "https://ex.com" }],
123
+ },
124
+ {
125
+ type: "widget",
126
+ items: { title: "Kalender", sections: [] },
127
+ },
128
+ {
129
+ type: "product",
130
+ items: [{ title: "Kopi", price: "15000", url: "https://ex.com/kopi" }],
131
+ },
132
+ {
133
+ type: "post",
134
+ items: [{ title: "Berita", username: "dunia" }],
135
+ },
136
+ {
137
+ type: "reels",
138
+ items: [{ videoUrl: "https://ex.com/r.mp4", title: "Reel" }],
139
+ },
140
+ { type: "html", html: "<b>halo</b>", trustedSources: ["https://ex.com"] },
141
+ ],
142
+ footer: "Powered by Avelyna",
143
+ });
144
+
145
+ const allPrimitivesChain = (builder: AIRich): AIRich =>
146
+ builder
147
+ .text("Welcome")
148
+ .code("javascript", "console.log('Hello')")
149
+ .table([
150
+ ["Item", "Qty"],
151
+ ["Kopi", "2"],
152
+ ])
153
+ .metadata("meta info")
154
+ .tip("Helpful information")
155
+ .suggest(["ulangi pesanan", "lihat menu"])
156
+ .source([{ favicon: "https://ex.com/f.ico", url: "https://ex.com" }])
157
+ .image("https://ex.com/pic.jpg", { width: 512, height: 512 })
158
+ .video("https://ex.com/clip.mp4", { mime_type: "video/mp4", duration: 12 })
159
+ .footerAction([{ text: "footer", cta_text: "Buka", url: "https://ex.com" }])
160
+ .widget({ title: "Kalender", sections: [] })
161
+ .product([{ title: "Kopi", price: "15000", url: "https://ex.com/kopi" }])
162
+ .post([{ title: "Berita", username: "dunia" }])
163
+ .reels([{ videoUrl: "https://ex.com/r.mp4", title: "Reel" }])
164
+ .html("<b>halo</b>", { trusted_sources: ["https://ex.com"] })
165
+ .footer("Powered by Avelyna");
166
+
167
+ test("hybrid airich: async dan chain dua-langkah identik untuk seluruh primitive", async () => {
168
+ const asyncCap = captureClient();
169
+ const chainCap = captureClient();
170
+
171
+ await airich(asyncCap.client, "6281@s.whatsapp.net", allPrimitivesConfig());
172
+ await allPrimitivesChain(airich(chainCap.client)).send("6281@s.whatsapp.net");
173
+
174
+ assert.equal(asyncCap.sends.length, 2, "async: placeholder + edit");
175
+ assert.equal(chainCap.sends.length, 2, "chain: placeholder + edit");
176
+ assert.equal(asyncCap.sends[0]?.to, chainCap.sends[0]?.to);
177
+ assert.deepEqual(
178
+ plain(asyncCap.sends[0]?.options),
179
+ plain(chainCap.sends[0]?.options),
180
+ );
181
+ assert.deepEqual(
182
+ comparableEdit(asyncCap.sends[1]?.content),
183
+ comparableEdit(chainCap.sends[1]?.content),
184
+ );
185
+
186
+ const rich = richOfEdit(asyncCap.sends[1]?.content);
187
+ const decoded = decodedOf(rich);
188
+ const sections = decoded.sections as Record<string, unknown>[];
189
+ assert.equal(sections.length, 16, "15 primitive + footer");
190
+ const typenames = sections.map(
191
+ (section) =>
192
+ (asRecord(section.view_model).primitive as Record<string, unknown>)
193
+ ?.__typename,
194
+ );
195
+ assert.equal(typenames[0], "GenAIMarkdownTextUXPrimitive");
196
+ assert.equal(typenames[1], "GenAICodeUXPrimitive");
197
+ assert.equal(typenames[2], "GenATableUXPrimitive");
198
+ assert.equal(typenames[15], "GenAIMetadataTextPrimitive");
199
+ });
200
+
201
+ test("hybrid airich: config minimal (text tunggal) dua-langkah identik", async () => {
202
+ const asyncCap = captureClient();
203
+ const chainCap = captureClient();
204
+ await airich(asyncCap.client, "6281@s.whatsapp.net", {
205
+ sections: [{ type: "text", text: "Hello from AIRich" }],
206
+ });
207
+ await airich(chainCap.client)
208
+ .text("Hello from AIRich")
209
+ .send("6281@s.whatsapp.net");
210
+ assert.deepEqual(
211
+ comparableEdit(asyncCap.sends[1]?.content),
212
+ comparableEdit(chainCap.sends[1]?.content),
213
+ );
214
+ });
215
+
216
+ test("hybrid airich: twoStep false kirim langsung identik", async () => {
217
+ const asyncCap = captureClient();
218
+ const chainCap = captureClient();
219
+ const config: AIRichConfig = {
220
+ sections: [{ type: "text", text: "Langsung" }],
221
+ };
222
+ await airich(asyncCap.client, "6281@s.whatsapp.net", config, {
223
+ twoStep: false,
224
+ });
225
+ await airich(chainCap.client)
226
+ .text("Langsung")
227
+ .send("6281@s.whatsapp.net", { twoStep: false });
228
+ assert.equal(asyncCap.sends.length, 1);
229
+ assert.equal(chainCap.sends.length, 1);
230
+ assert.deepEqual(
231
+ decodedOf(richOfDirect(asyncCap.sends[0]?.content)),
232
+ decodedOf(richOfDirect(chainCap.sends[0]?.content)),
233
+ );
234
+ const submessages = richOfDirect(asyncCap.sends[0]?.content)
235
+ .submessages as unknown[];
236
+ assert.equal(submessages.length, 1, "submessages default aktif");
237
+ });
238
+
239
+ test("hybrid airich: submessages false identik", async () => {
240
+ const asyncCap = captureClient();
241
+ const chainCap = captureClient();
242
+ await airich(
243
+ asyncCap.client,
244
+ "6281@s.whatsapp.net",
245
+ {
246
+ sections: [
247
+ { type: "text", text: "A" },
248
+ { type: "code", language: "js", code: "1" },
249
+ ],
250
+ },
251
+ { twoStep: false, submessages: false },
252
+ );
253
+ await airich(chainCap.client)
254
+ .text("A")
255
+ .code("js", "1")
256
+ .send("6281@s.whatsapp.net", { twoStep: false, submessages: false });
257
+ assert.deepEqual(
258
+ decodedOf(richOfDirect(asyncCap.sends[0]?.content)),
259
+ decodedOf(richOfDirect(chainCap.sends[0]?.content)),
260
+ );
261
+ assert.deepEqual(
262
+ richOfDirect(asyncCap.sends[0]?.content).submessages,
263
+ richOfDirect(chainCap.sends[0]?.content).submessages,
264
+ );
265
+ assert.equal(
266
+ (richOfDirect(asyncCap.sends[0]?.content).submessages as unknown[]).length,
267
+ 0,
268
+ );
269
+ });
270
+
271
+ test("hybrid airich: context dan quote lewat config identik", async () => {
272
+ const incoming = {
273
+ key: { remoteJid: "6281@s.whatsapp.net", id: "MSG-7", fromMe: false },
274
+ message: { conversation: "halo" },
275
+ };
276
+ const asyncCap = captureClient();
277
+ const chainCap = captureClient();
278
+ await airich(
279
+ asyncCap.client,
280
+ "6281@s.whatsapp.net",
281
+ {
282
+ sections: [{ type: "text", text: "dengan konteks" }],
283
+ quote: incoming,
284
+ },
285
+ { twoStep: false },
286
+ );
287
+ await airich(chainCap.client)
288
+ .text("dengan konteks")
289
+ .quote(incoming)
290
+ .send("6281@s.whatsapp.net", { twoStep: false });
291
+ const asyncRich = richOfDirect(asyncCap.sends[0]?.content);
292
+ const chainRich = richOfDirect(chainCap.sends[0]?.content);
293
+ const asyncContext = plain(asyncRich.contextInfo) as Record<string, unknown>;
294
+ const chainContext = plain(chainRich.contextInfo) as Record<string, unknown>;
295
+ assert.deepEqual(asyncContext, chainContext);
296
+ assert.equal(
297
+ (asyncContext.quotedMessage as Record<string, unknown>).conversation,
298
+ "halo",
299
+ );
300
+ });
301
+
302
+ test("hybrid airich: raw section via config identik dengan chain", async () => {
303
+ const rawSection = {
304
+ view_model: {
305
+ primitive: { text: "mentah", __typename: "GenAIMetadataTextPrimitive" },
306
+ __typename: "GenAISingleLayoutViewModel",
307
+ },
308
+ };
309
+ const asyncCap = captureClient();
310
+ const chainCap = captureClient();
311
+ await airich(asyncCap.client, "6281@s.whatsapp.net", {
312
+ sections: [{ type: "raw", section: rawSection }],
313
+ });
314
+ await airich(chainCap.client).raw(rawSection).send("6281@s.whatsapp.net");
315
+ assert.deepEqual(
316
+ comparableEdit(asyncCap.sends[1]?.content),
317
+ comparableEdit(chainCap.sends[1]?.content),
318
+ );
319
+ });
320
+
321
+ test("hybrid airich: opsi id per section diterapkan", () => {
322
+ const client = asClient({ message: { send: async () => ({}) } });
323
+ const builder = airich(client);
324
+ applyAIRichConfig(builder, {
325
+ sections: [
326
+ { type: "text", text: "satu", id: "node-satu" },
327
+ { type: "tip", text: "dua", id: "node-dua" },
328
+ ],
329
+ });
330
+ assert.deepEqual(builder.ids(), ["node-satu", "node-dua"]);
331
+ assert.equal(builder.has("node-satu"), true);
332
+ });
333
+
334
+ test("hybrid airich: airich() builder dan airich(options) tetap chain", () => {
335
+ const cap = captureClient();
336
+ const bare = airich(cap.client);
337
+ assert.ok(bare instanceof AIRich);
338
+ const withOptions = airich(cap.client, { botJid: "628999@s.whatsapp.net" });
339
+ assert.ok(withOptions instanceof AIRich);
340
+ const jidOnly = airich(
341
+ cap.client,
342
+ "6281@s.whatsapp.net" as unknown as AIRichConstructorOptions,
343
+ );
344
+ assert.ok(jidOnly instanceof AIRich, "jid tanpa config tetap builder lama");
345
+ });