@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,261 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { listMessage } from "@kyyinfinite/zapo-message";
|
|
4
|
+
import { ValidationError, DuplicateIdError } from "@kyyinfinite/zapo-message";
|
|
5
|
+
import { asClient, first, L } from "./helpers.js";
|
|
6
|
+
|
|
7
|
+
const PRIVATE_JID = "628123456789@s.whatsapp.net";
|
|
8
|
+
const GROUP_JID = "1203630123456789@g.us";
|
|
9
|
+
|
|
10
|
+
const makeSendClient = () => {
|
|
11
|
+
const sends: { jid?: unknown; content?: unknown; options?: unknown }[] = [];
|
|
12
|
+
return {
|
|
13
|
+
sends,
|
|
14
|
+
client: asClient({
|
|
15
|
+
message: {
|
|
16
|
+
async send(jid: unknown, content: unknown, options: unknown) {
|
|
17
|
+
sends.push({ jid, content, options });
|
|
18
|
+
return { id: "FAKE", attempts: 1, ack: {} };
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
}),
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const sendOnlyClient = () =>
|
|
26
|
+
asClient({
|
|
27
|
+
message: {
|
|
28
|
+
async send() {
|
|
29
|
+
return { id: "X" };
|
|
30
|
+
},
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const contentOf = (
|
|
35
|
+
sends: { content?: unknown }[],
|
|
36
|
+
index: number,
|
|
37
|
+
): Record<string, unknown> =>
|
|
38
|
+
(sends[index] as { content?: unknown }).content as Record<string, unknown>;
|
|
39
|
+
|
|
40
|
+
test("list: build payload dasar — listType 1, rowId dari id", () => {
|
|
41
|
+
const result = listMessage(sendOnlyClient())
|
|
42
|
+
.title("Judul List")
|
|
43
|
+
.body("Deskripsi List")
|
|
44
|
+
.buttonText("Pilih")
|
|
45
|
+
.footer("Footer")
|
|
46
|
+
.section("Utama", [
|
|
47
|
+
{ id: "row1", title: "Baris Satu", description: "Deskripsi satu" },
|
|
48
|
+
])
|
|
49
|
+
.build();
|
|
50
|
+
|
|
51
|
+
const msg = result.listMessage;
|
|
52
|
+
const firstRow = (msg.sections[0] as NonNullable<(typeof msg.sections)[0]>)
|
|
53
|
+
.rows as { rowId?: string; title?: string }[];
|
|
54
|
+
assert.equal(msg.title, "Judul List");
|
|
55
|
+
assert.equal(msg.description, "Deskripsi List");
|
|
56
|
+
assert.equal(msg.buttonText, "Pilih");
|
|
57
|
+
assert.equal(msg.listType, 1);
|
|
58
|
+
assert.equal(msg.footerText, "Footer");
|
|
59
|
+
assert.equal(firstRow[0]?.rowId, "row1");
|
|
60
|
+
assert.equal(firstRow[0]?.title, "Baris Satu");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("list: fallback default auto — chat pribadi tetap listMessage", async () => {
|
|
64
|
+
const { client, sends } = makeSendClient();
|
|
65
|
+
await listMessage(client)
|
|
66
|
+
.title("L")
|
|
67
|
+
.section("S", [{ id: "r1", title: "R1" }])
|
|
68
|
+
.send(PRIVATE_JID);
|
|
69
|
+
assert.ok(contentOf(sends, 0).listMessage);
|
|
70
|
+
assert.equal(contentOf(sends, 0).interactiveMessage, undefined);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("list: fallback auto — grup dikonversi ke native flow otomatis", async () => {
|
|
74
|
+
const { client, sends } = makeSendClient();
|
|
75
|
+
await listMessage(client)
|
|
76
|
+
.title("L")
|
|
77
|
+
.body("D")
|
|
78
|
+
.footer("F")
|
|
79
|
+
.section("S", [{ id: "r1", title: "R1" }])
|
|
80
|
+
.send(GROUP_JID);
|
|
81
|
+
|
|
82
|
+
const content = contentOf(sends, 0);
|
|
83
|
+
assert.ok(content.interactiveMessage);
|
|
84
|
+
assert.equal(content.listMessage, undefined);
|
|
85
|
+
|
|
86
|
+
const flow = L(content.interactiveMessage).nativeFlowMessage as Record<
|
|
87
|
+
string,
|
|
88
|
+
unknown
|
|
89
|
+
>;
|
|
90
|
+
assert.equal(flow.messageVersion, 1);
|
|
91
|
+
assert.equal(first(flow.buttons as { name: string }[]).name, "single_select");
|
|
92
|
+
|
|
93
|
+
const params = JSON.parse(
|
|
94
|
+
first(flow.buttons as { buttonParamsJson: string }[]).buttonParamsJson,
|
|
95
|
+
);
|
|
96
|
+
assert.equal(params.title, "Buka");
|
|
97
|
+
assert.equal(params.sections[0].rows[0].id, "r1");
|
|
98
|
+
assert.equal(params.sections[0].rows[0].title, "R1");
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("list: toNativeFlow — identitas rowId === id asli, identik untuk router", () => {
|
|
102
|
+
const builder = listMessage(sendOnlyClient())
|
|
103
|
+
.title("Judul")
|
|
104
|
+
.body("Deskripsi")
|
|
105
|
+
.buttonText("Buka Daftar")
|
|
106
|
+
.footer("Footer")
|
|
107
|
+
.section("Utama", [
|
|
108
|
+
{ id: "row1", title: "Baris Satu", description: "Deskripsi satu" },
|
|
109
|
+
])
|
|
110
|
+
.section("Lainnya", [{ id: "row2", title: "Baris Dua" }]);
|
|
111
|
+
|
|
112
|
+
const result = builder.toNativeFlow();
|
|
113
|
+
const flow = L(result.interactiveMessage).nativeFlowMessage as Record<
|
|
114
|
+
string,
|
|
115
|
+
unknown
|
|
116
|
+
>;
|
|
117
|
+
const params = JSON.parse(
|
|
118
|
+
first(flow.buttons as { buttonParamsJson: string }[]).buttonParamsJson,
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
assert.equal(first(flow.buttons as { name: string }[]).name, "single_select");
|
|
122
|
+
assert.equal(params.title, "Buka Daftar");
|
|
123
|
+
assert.deepEqual(
|
|
124
|
+
params.sections.flatMap((s: { rows: { id: string }[] }) =>
|
|
125
|
+
s.rows.map((r) => r.id),
|
|
126
|
+
),
|
|
127
|
+
["row1", "row2"],
|
|
128
|
+
);
|
|
129
|
+
assert.deepEqual(
|
|
130
|
+
params.sections.map((s: { title?: string }) => s.title),
|
|
131
|
+
["Utama", "Lainnya"],
|
|
132
|
+
);
|
|
133
|
+
assert.equal(params.sections[0].rows[0].description, "Deskripsi satu");
|
|
134
|
+
assert.equal(result.interactiveMessage.body.text, "Deskripsi");
|
|
135
|
+
assert.equal(
|
|
136
|
+
(result.interactiveMessage.footer as { text: string }).text,
|
|
137
|
+
"Footer",
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("list: fallback native — selalu native flow walau chat pribadi", async () => {
|
|
142
|
+
const { client, sends } = makeSendClient();
|
|
143
|
+
await listMessage(client)
|
|
144
|
+
.fallback("native")
|
|
145
|
+
.section("S", [{ id: "r1", title: "R1" }])
|
|
146
|
+
.send(PRIVATE_JID);
|
|
147
|
+
assert.ok(contentOf(sends, 0).interactiveMessage);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
test("list: fallback never — selalu listMessage walau di grup", async () => {
|
|
151
|
+
const { client, sends } = makeSendClient();
|
|
152
|
+
await listMessage(client)
|
|
153
|
+
.fallback("never")
|
|
154
|
+
.section("S", [{ id: "r1", title: "R1" }])
|
|
155
|
+
.send(GROUP_JID);
|
|
156
|
+
assert.ok(contentOf(sends, 0).listMessage);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test("list: inGroup(true) override keputusan auto", async () => {
|
|
160
|
+
const { client, sends } = makeSendClient();
|
|
161
|
+
await listMessage(client)
|
|
162
|
+
.inGroup(true)
|
|
163
|
+
.section("S", [{ id: "r1", title: "R1" }])
|
|
164
|
+
.send(PRIVATE_JID);
|
|
165
|
+
assert.ok(contentOf(sends, 0).interactiveMessage);
|
|
166
|
+
|
|
167
|
+
await listMessage(client)
|
|
168
|
+
.inGroup(false)
|
|
169
|
+
.section("S", [{ id: "r2", title: "R2" }])
|
|
170
|
+
.send(GROUP_JID);
|
|
171
|
+
assert.ok(contentOf(sends, 1).listMessage);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
test("list: fallback mode tidak valid ditolak", () => {
|
|
175
|
+
assert.throws(
|
|
176
|
+
() => listMessage(sendOnlyClient()).fallback("selalu" as "auto"),
|
|
177
|
+
/fallback harus/,
|
|
178
|
+
);
|
|
179
|
+
assert.throws(
|
|
180
|
+
() => listMessage(sendOnlyClient()).fallback(1 as unknown as "auto"),
|
|
181
|
+
/fallback harus/,
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
test("list: inGroup non-boolean ditolak", () => {
|
|
186
|
+
assert.throws(
|
|
187
|
+
() => listMessage(sendOnlyClient()).inGroup("ya" as unknown as boolean),
|
|
188
|
+
/butuh boolean/,
|
|
189
|
+
);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
test("list: send dengan options (quote) diteruskan ke transport", async () => {
|
|
193
|
+
const { client, sends } = makeSendClient();
|
|
194
|
+
const quote = { key: { id: "Q1" } };
|
|
195
|
+
await listMessage(client)
|
|
196
|
+
.section("S", [{ id: "r1", title: "R1" }])
|
|
197
|
+
.send(PRIVATE_JID, { quote });
|
|
198
|
+
assert.deepEqual((sends[0] as { options?: unknown }).options, { quote });
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("list: section tanpa rows ditolak; row tanpa id ditolak", () => {
|
|
202
|
+
const builder = listMessage(sendOnlyClient());
|
|
203
|
+
assert.throws(() => builder.section("S", []), /minimal satu row/);
|
|
204
|
+
assert.throws(
|
|
205
|
+
() =>
|
|
206
|
+
builder.section("S", [{ title: "tanpa id" }] as unknown as {
|
|
207
|
+
id: string;
|
|
208
|
+
title: string;
|
|
209
|
+
}[]),
|
|
210
|
+
/id/,
|
|
211
|
+
);
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
test("list: rowId duplikat antar section → DuplicateIdError", () => {
|
|
215
|
+
const builder = listMessage(sendOnlyClient());
|
|
216
|
+
builder.section("S1", [{ id: "dup", title: "A" }]);
|
|
217
|
+
assert.throws(
|
|
218
|
+
() => builder.section("S2", [{ id: "dup", title: "B" }]),
|
|
219
|
+
(error: Error) => error instanceof DuplicateIdError,
|
|
220
|
+
);
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
test("list: build tanpa section → ValidationError", () => {
|
|
224
|
+
assert.throws(
|
|
225
|
+
() => listMessage(sendOnlyClient()).build(),
|
|
226
|
+
/minimal satu section/,
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
test("list: repeated build tidak mengubah state internal", () => {
|
|
231
|
+
const builder = listMessage(sendOnlyClient())
|
|
232
|
+
.title("T")
|
|
233
|
+
.body("B")
|
|
234
|
+
.buttonText("BT")
|
|
235
|
+
.footer("F")
|
|
236
|
+
.section("S1", [{ id: "r1", title: "R1", description: "D1" }]);
|
|
237
|
+
|
|
238
|
+
const first = builder.build().listMessage;
|
|
239
|
+
const second = builder.build().listMessage;
|
|
240
|
+
assert.deepEqual(first, second);
|
|
241
|
+
|
|
242
|
+
builder.build();
|
|
243
|
+
const after = builder.build().listMessage;
|
|
244
|
+
assert.equal(after.title, "T");
|
|
245
|
+
assert.equal(after.sections.length, 1);
|
|
246
|
+
const afterFirst = after.sections[0] as NonNullable<
|
|
247
|
+
(typeof after.sections)[0]
|
|
248
|
+
>;
|
|
249
|
+
assert.equal((afterFirst.rows ?? []).length, 1);
|
|
250
|
+
assert.equal((afterFirst.rows ?? [])[0]?.rowId, "r1");
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
test("list: section title opsional (tanpa judul tetap valid)", () => {
|
|
254
|
+
const builder = listMessage(sendOnlyClient()).section(undefined, [
|
|
255
|
+
{ id: "r1", title: "R1" },
|
|
256
|
+
]);
|
|
257
|
+
const msg = builder.build().listMessage;
|
|
258
|
+
const firstSection = msg.sections[0] as NonNullable<(typeof msg.sections)[0]>;
|
|
259
|
+
assert.equal(firstSection.title, undefined);
|
|
260
|
+
assert.equal((firstSection.rows as { rowId?: string }[])[0]?.rowId, "r1");
|
|
261
|
+
});
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtemp, writeFile, rm } from "node:fs/promises";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { Readable } from "node:stream";
|
|
7
|
+
import sharp from "sharp";
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
normalizeMediaSource,
|
|
11
|
+
readBoundedBytes,
|
|
12
|
+
readPathBounded,
|
|
13
|
+
bufferFromBytes,
|
|
14
|
+
isReadableStreamSource,
|
|
15
|
+
} from "@kyyinfinite/zapo-message/btn/media-source.js";
|
|
16
|
+
import { ValidationError } from "@kyyinfinite/zapo-message/core/errors.js";
|
|
17
|
+
|
|
18
|
+
const makePng = async () =>
|
|
19
|
+
sharp({ create: { width: 64, height: 48, channels: 3, background: "green" } })
|
|
20
|
+
.png()
|
|
21
|
+
.toBuffer();
|
|
22
|
+
|
|
23
|
+
test("media-source: Buffer → bytes", async () => {
|
|
24
|
+
const png = await makePng();
|
|
25
|
+
const source = await normalizeMediaSource(png);
|
|
26
|
+
assert.equal(source.kind, "bytes");
|
|
27
|
+
assert.ok(
|
|
28
|
+
Buffer.isBuffer(source.kind === "bytes" ? source.bytes : undefined),
|
|
29
|
+
);
|
|
30
|
+
assert.equal(
|
|
31
|
+
source.kind === "bytes" ? source.byteSize : undefined,
|
|
32
|
+
png.byteLength,
|
|
33
|
+
);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
test("media-source: Uint8Array → bytes tanpa salin data", async () => {
|
|
37
|
+
const png = await makePng();
|
|
38
|
+
const view = new Uint8Array(png.buffer, png.byteOffset, png.byteLength);
|
|
39
|
+
const source = await normalizeMediaSource(view);
|
|
40
|
+
assert.equal(source.kind, "bytes");
|
|
41
|
+
assert.equal(
|
|
42
|
+
source.kind === "bytes" ? source.bytes.byteLength : undefined,
|
|
43
|
+
png.byteLength,
|
|
44
|
+
);
|
|
45
|
+
assert.equal(
|
|
46
|
+
source.kind === "bytes" ? source.bytes.toString("base64") : undefined,
|
|
47
|
+
Buffer.from(png).toString("base64"),
|
|
48
|
+
);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("media-source: path file → path + ukuran", async () => {
|
|
52
|
+
const dir = await mkdtemp(join(tmpdir(), "zapo-message-btn-"));
|
|
53
|
+
try {
|
|
54
|
+
const file = join(dir, "gambar.png");
|
|
55
|
+
await writeFile(file, await makePng());
|
|
56
|
+
const source = await normalizeMediaSource(file);
|
|
57
|
+
assert.equal(source.kind, "path");
|
|
58
|
+
assert.equal(source.kind === "path" ? source.path : undefined, file);
|
|
59
|
+
assert.ok((source.kind === "path" ? source.byteSize : 0) > 0);
|
|
60
|
+
} finally {
|
|
61
|
+
await rm(dir, { recursive: true, force: true });
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test("media-source: path tidak ada → ValidationError jelas", async () => {
|
|
66
|
+
await assert.rejects(
|
|
67
|
+
() => normalizeMediaSource("/pasti/tidak/ada/file.png"),
|
|
68
|
+
(error: Error) =>
|
|
69
|
+
error instanceof ValidationError && /tidak ditemukan/.test(error.message),
|
|
70
|
+
);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test("media-source: Readable → stream", async () => {
|
|
74
|
+
const png = await makePng();
|
|
75
|
+
const source = await normalizeMediaSource(Readable.from([png]));
|
|
76
|
+
assert.equal(source.kind, "stream");
|
|
77
|
+
assert.ok(
|
|
78
|
+
isReadableStreamSource(
|
|
79
|
+
source.kind === "stream" ? source.stream : undefined,
|
|
80
|
+
),
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
test("media-source: sumber kosong → ValidationError dengan daftar input didukung", async () => {
|
|
85
|
+
for (const bad of [undefined, null, ""]) {
|
|
86
|
+
await assert.rejects(
|
|
87
|
+
() => normalizeMediaSource(bad),
|
|
88
|
+
(error: Error) =>
|
|
89
|
+
error instanceof ValidationError &&
|
|
90
|
+
/Buffer.*Uint8Array.*path.*Readable|sumber.*kosong/.test(error.message),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test("media-source: sumber tidak valid (number/object) → ValidationError", async () => {
|
|
96
|
+
for (const bad of [123, { image: "x" }, ["array"], true]) {
|
|
97
|
+
await assert.rejects(
|
|
98
|
+
() => normalizeMediaSource(bad),
|
|
99
|
+
(error: Error) =>
|
|
100
|
+
error instanceof ValidationError && /tidak valid/.test(error.message),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
test("media-source: readBoundedBytes mengumpulkan stream penuh", async () => {
|
|
106
|
+
const png = await makePng();
|
|
107
|
+
const bytes = await readBoundedBytes(
|
|
108
|
+
Readable.from([png]),
|
|
109
|
+
1024 * 1024,
|
|
110
|
+
"tes",
|
|
111
|
+
);
|
|
112
|
+
assert.equal(bytes.byteLength, png.byteLength);
|
|
113
|
+
assert.equal(bytes.toString("base64"), png.toString("base64"));
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("media-source: readBoundedBytes menolak stream melebihi batas", async () => {
|
|
117
|
+
const big = Buffer.alloc(100, 7);
|
|
118
|
+
await assert.rejects(
|
|
119
|
+
() => readBoundedBytes(Readable.from([big]), 50, "tes"),
|
|
120
|
+
(error: Error) => /melebihi batas ukuran/.test(error.message),
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
test("media-source: readPathBounded membaca file kecil", async (t) => {
|
|
125
|
+
const dir = await mkdtemp(join(tmpdir(), "zapo-message-btn-"));
|
|
126
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
127
|
+
const file = join(dir, "kecil.bin");
|
|
128
|
+
await writeFile(file, Buffer.from("abc"));
|
|
129
|
+
const bytes = await readPathBounded(file, 100, "tes");
|
|
130
|
+
assert.equal(bytes.toString(), "abc");
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("media-source: readPathBounded menolak file besar", async (t) => {
|
|
134
|
+
const dir = await mkdtemp(join(tmpdir(), "zapo-message-btn-"));
|
|
135
|
+
t.after(() => rm(dir, { recursive: true, force: true }));
|
|
136
|
+
const file = join(dir, "besar.bin");
|
|
137
|
+
await writeFile(file, Buffer.alloc(200, 1));
|
|
138
|
+
await assert.rejects(
|
|
139
|
+
() => readPathBounded(file, 100, "tes"),
|
|
140
|
+
/melebihi batas ukuran/,
|
|
141
|
+
);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
test("media-source: bufferFromBytes zero-copy view", () => {
|
|
145
|
+
const array = new Uint8Array([1, 2, 3, 4]);
|
|
146
|
+
const view = array.subarray(1, 3);
|
|
147
|
+
const buffer = bufferFromBytes(view);
|
|
148
|
+
assert.equal(buffer.byteLength, 2);
|
|
149
|
+
assert.deepEqual([...buffer], [2, 3]);
|
|
150
|
+
});
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import test from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import sharp from "sharp";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ensureJpegThumbnail,
|
|
7
|
+
validateJpegThumbnail,
|
|
8
|
+
generateImageThumbnail,
|
|
9
|
+
probeImageDimensions,
|
|
10
|
+
isJpegBytes,
|
|
11
|
+
asThumbnailBytes,
|
|
12
|
+
THUMBNAIL_MAX_DIMENSION,
|
|
13
|
+
} from "@kyyinfinite/zapo-message/btn/thumbnail.js";
|
|
14
|
+
import { bufferFromBytes } from "@kyyinfinite/zapo-message/btn/media-source.js";
|
|
15
|
+
import {
|
|
16
|
+
ValidationError,
|
|
17
|
+
MediaError,
|
|
18
|
+
} from "@kyyinfinite/zapo-message/core/errors.js";
|
|
19
|
+
|
|
20
|
+
const makeJpeg = async ({
|
|
21
|
+
width = 800,
|
|
22
|
+
height = 600,
|
|
23
|
+
quality = 90,
|
|
24
|
+
}: { width?: number; height?: number; quality?: number } = {}) =>
|
|
25
|
+
sharp({ create: { width, height, channels: 3, background: "#2040ff" } })
|
|
26
|
+
.jpeg({ quality })
|
|
27
|
+
.toBuffer();
|
|
28
|
+
|
|
29
|
+
const makePng = async ({
|
|
30
|
+
width = 800,
|
|
31
|
+
height = 600,
|
|
32
|
+
}: { width?: number; height?: number } = {}) =>
|
|
33
|
+
sharp({ create: { width, height, channels: 3, background: "#2040ff" } })
|
|
34
|
+
.png()
|
|
35
|
+
.toBuffer();
|
|
36
|
+
|
|
37
|
+
test("thumbnail: isJpegBytes mengenali magic JPEG", async () => {
|
|
38
|
+
const jpeg = await makeJpeg();
|
|
39
|
+
assert.equal(isJpegBytes(jpeg), true);
|
|
40
|
+
const png = await makePng({ width: 10, height: 10 });
|
|
41
|
+
assert.equal(isJpegBytes(png), false);
|
|
42
|
+
assert.equal(isJpegBytes(Buffer.from("xx")), false);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("thumbnail: asThumbnailBytes menolak bukan bytes dan bytes kosong", () => {
|
|
46
|
+
assert.throws(() => asThumbnailBytes("bukan bytes"), ValidationError);
|
|
47
|
+
assert.throws(() => asThumbnailBytes(Buffer.alloc(0)), /thumbnail kosong/);
|
|
48
|
+
assert.throws(() => asThumbnailBytes(null), ValidationError);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("thumbnail: ensureJpegThumbnail loloskan JPEG apa adanya", async () => {
|
|
52
|
+
const jpeg = await makeJpeg();
|
|
53
|
+
const result = await ensureJpegThumbnail(jpeg);
|
|
54
|
+
assert.ok(Buffer.isBuffer(result));
|
|
55
|
+
assert.equal(result.byteLength, jpeg.byteLength);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test("thumbnail: ensureJpegThumbnail konversi PNG → JPEG valid", async () => {
|
|
59
|
+
const png = await makePng({ width: 100, height: 80 });
|
|
60
|
+
const result = await ensureJpegThumbnail(png);
|
|
61
|
+
assert.equal(isJpegBytes(result), true);
|
|
62
|
+
const metadata = await sharp(result).metadata();
|
|
63
|
+
assert.equal(metadata.format, "jpeg");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("thumbnail: ensureJpegThumbnail gagal jelas untuk bytes acak", async () => {
|
|
67
|
+
const junk = Buffer.from("ini bukan gambar sama sekali");
|
|
68
|
+
await assert.rejects(
|
|
69
|
+
() => ensureJpegThumbnail(junk),
|
|
70
|
+
(error: Error) =>
|
|
71
|
+
error instanceof ValidationError &&
|
|
72
|
+
/bukan JPEG valid/.test(error.message),
|
|
73
|
+
);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
test("thumbnail: validateJpegThumbnail menerima JPEG dan menolak non-JPEG", async () => {
|
|
77
|
+
const jpeg = await makeJpeg();
|
|
78
|
+
const result = validateJpegThumbnail(jpeg, { label: ".headerThumbnail()" });
|
|
79
|
+
assert.equal(isJpegBytes(result), true);
|
|
80
|
+
|
|
81
|
+
const png = await makePng({ width: 20, height: 20 });
|
|
82
|
+
await assert.rejects(
|
|
83
|
+
async () => validateJpegThumbnail(png),
|
|
84
|
+
(error: Error) =>
|
|
85
|
+
error instanceof ValidationError &&
|
|
86
|
+
/harus JPEG valid/.test(error.message),
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
test("thumbnail: generateImageThumbnail JPEG, fit inside, aspect ratio dipertahankan", async () => {
|
|
91
|
+
const jpeg = await makeJpeg({ width: 800, height: 400 });
|
|
92
|
+
const thumb = await generateImageThumbnail(jpeg);
|
|
93
|
+
assert.equal(isJpegBytes(thumb), true);
|
|
94
|
+
const metadata = await sharp(thumb).metadata();
|
|
95
|
+
assert.equal(metadata.format, "jpeg");
|
|
96
|
+
assert.ok((metadata.width ?? 0) <= THUMBNAIL_MAX_DIMENSION);
|
|
97
|
+
assert.ok((metadata.height ?? 0) <= THUMBNAIL_MAX_DIMENSION);
|
|
98
|
+
assert.equal((metadata.width as number) / (metadata.height as number), 2);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test("thumbnail: generateImageThumbnail tidak memperbesar gambar kecil", async () => {
|
|
102
|
+
const small = await makeJpeg({ width: 60, height: 40 });
|
|
103
|
+
const thumb = await generateImageThumbnail(small);
|
|
104
|
+
const metadata = await sharp(thumb).metadata();
|
|
105
|
+
assert.equal(metadata.width, 60);
|
|
106
|
+
assert.equal(metadata.height, 40);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("thumbnail: generateImageThumbnail memproses PNG dan webp", async () => {
|
|
110
|
+
const png = await makePng({ width: 500, height: 500 });
|
|
111
|
+
const fromPng = await generateImageThumbnail(png);
|
|
112
|
+
assert.equal(isJpegBytes(fromPng), true);
|
|
113
|
+
|
|
114
|
+
const webp = await sharp({
|
|
115
|
+
create: { width: 500, height: 500, channels: 3, background: "red" },
|
|
116
|
+
})
|
|
117
|
+
.webp()
|
|
118
|
+
.toBuffer();
|
|
119
|
+
const fromWebp = await generateImageThumbnail(webp);
|
|
120
|
+
assert.equal(isJpegBytes(fromWebp), true);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test("thumbnail: generateImageThumbnail output di bawah batas ukuran", async () => {
|
|
124
|
+
const jpeg = await makeJpeg({ width: 2000, height: 2000 });
|
|
125
|
+
const thumb = await generateImageThumbnail(jpeg);
|
|
126
|
+
assert.ok(thumb.byteLength > 0);
|
|
127
|
+
assert.ok(thumb.byteLength <= 100 * 1024);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("thumbnail: generateImageThumbnail error jelas untuk bytes bukan gambar", async () => {
|
|
131
|
+
const junk = Buffer.from("teks biasa, bukan gambar");
|
|
132
|
+
await assert.rejects(
|
|
133
|
+
() => generateImageThumbnail(junk),
|
|
134
|
+
(error: Error & { cause?: unknown }) =>
|
|
135
|
+
error instanceof MediaError &&
|
|
136
|
+
/generasi thumbnail otomatis.*gagal/.test(error.message) &&
|
|
137
|
+
error.cause !== undefined,
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
test("thumbnail: generateImageThumbnail menolak bytes kosong", async () => {
|
|
142
|
+
await assert.rejects(
|
|
143
|
+
() => generateImageThumbnail(Buffer.alloc(0)),
|
|
144
|
+
/thumbnail kosong/,
|
|
145
|
+
);
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
test("thumbnail: probeImageDimensions ukuran akurat, undefined untuk junk", async () => {
|
|
149
|
+
const jpeg = await makeJpeg({ width: 320, height: 240 });
|
|
150
|
+
const dims = await probeImageDimensions(jpeg);
|
|
151
|
+
assert.deepEqual(dims, { width: 320, height: 240 });
|
|
152
|
+
assert.equal(await probeImageDimensions(Buffer.from("junk")), undefined);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("thumbnail: bufferFromBytes pada Uint8Array view", async () => {
|
|
156
|
+
const jpeg = await makeJpeg({ width: 50, height: 50 });
|
|
157
|
+
const view = new Uint8Array(jpeg.buffer, jpeg.byteOffset, jpeg.byteLength);
|
|
158
|
+
const restored = bufferFromBytes(view);
|
|
159
|
+
assert.equal(isJpegBytes(restored), true);
|
|
160
|
+
});
|