@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.
Files changed (191) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +20 -0
  3. package/README.md +532 -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 +38 -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,200 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { parseInline } from "@kyyinfinite/zapo-message/airich/inline.js";
5
+ import type { InlineEntity } from "@kyyinfinite/zapo-message";
6
+
7
+ const metaOf = (entity: InlineEntity | undefined): Record<string, unknown> =>
8
+ entity?.metadata as Record<string, unknown>;
9
+
10
+ test("inline: hyperlink dasar", () => {
11
+ const { text, entities } = parseInline("lihat [web](https://example.com) ya");
12
+ assert.equal(
13
+ text,
14
+ "lihat {{ZAPOK_HYPERLINK_0}}https://example.com{{/ZAPOK_HYPERLINK_0}} ya",
15
+ );
16
+ assert.deepEqual(entities, [
17
+ {
18
+ key: "ZAPOK_HYPERLINK_0",
19
+ metadata: {
20
+ display_name: "web",
21
+ is_trusted: true,
22
+ url: "https://example.com",
23
+ __typename: "GenAIInlineLinkItem",
24
+ },
25
+ },
26
+ ]);
27
+ });
28
+
29
+ test("inline: citation berurutan dengan reference_id bertambah", () => {
30
+ const { text, entities } = parseInline(
31
+ "a [](https://a.com) b [](https://b.com)",
32
+ );
33
+ assert.equal(entities[0]?.key, "ZAPOK_CITATION_0");
34
+ assert.equal(metaOf(entities[0]).reference_id, 1);
35
+ assert.equal(entities[1]?.key, "ZAPOK_CITATION_1");
36
+ assert.equal(metaOf(entities[1]).reference_id, 2);
37
+ assert.deepEqual(metaOf(entities[1]).sources, []);
38
+ assert.equal(metaOf(entities[1]).__typename, "GenAISearchCitationItem");
39
+ });
40
+
41
+ test("inline: latex dengan ukuran kustom", () => {
42
+ const { entities } = parseInline(
43
+ "rumus [E=mc^2|120|60|90|12]<https://x.com/f.png> akhir",
44
+ );
45
+ assert.equal(entities.length, 1);
46
+ const meta = entities[0]!.metadata;
47
+ assert.equal(meta.__typename, "GenAILatexItem");
48
+ assert.equal(meta.latex_expression, "E=mc^2");
49
+ assert.deepEqual(meta.latex_image, {
50
+ url: "https://x.com/f.png",
51
+ width: 120,
52
+ height: 60,
53
+ });
54
+ assert.equal(meta.font_height, 90);
55
+ assert.equal(meta.padding, 12);
56
+ });
57
+
58
+ test("inline: latex default ukuran", () => {
59
+ const { entities } = parseInline("[x]<https://y.com/img.png>");
60
+ assert.equal(
61
+ (metaOf(entities[0]).latex_image as Record<string, unknown>).width,
62
+ 100,
63
+ );
64
+ assert.equal(
65
+ (metaOf(entities[0]).latex_image as Record<string, unknown>).height,
66
+ 100,
67
+ );
68
+ assert.equal(metaOf(entities[0]).font_height, 83.333333333333);
69
+ assert.equal(metaOf(entities[0]).padding, 15);
70
+ });
71
+
72
+ test("inline: nested brackets", () => {
73
+ const { text, entities } = parseInline(
74
+ "nested [a [b](https://1.com)](https://2.com) done",
75
+ );
76
+ assert.equal(entities.length, 2);
77
+ assert.equal(metaOf(entities[0]).display_name, "b");
78
+ assert.equal(metaOf(entities[1]).display_name, "a [b](https://1.com)");
79
+ assert.equal(
80
+ text,
81
+ "nested [a {{ZAPOK_HYPERLINK_0}}https://1.com{{/ZAPOK_HYPERLINK_0}}{{ZAPOK_HYPERLINK_1}}https://2.com{{/ZAPOK_HYPERLINK_1}} done",
82
+ );
83
+ });
84
+
85
+ test("inline: nested parentheses pada URL", () => {
86
+ const { text, entities } = parseInline(
87
+ "x [wiki](https://en.wikipedia.org/wiki/Foo_(bar)) y",
88
+ );
89
+ assert.equal(entities.length, 1);
90
+ assert.equal(
91
+ metaOf(entities[0]).url,
92
+ "https://en.wikipedia.org/wiki/Foo_(bar)",
93
+ );
94
+ assert.ok(
95
+ text.includes(
96
+ "{{ZAPOK_HYPERLINK_0}}https://en.wikipedia.org/wiki/Foo_(bar){{/ZAPOK_HYPERLINK_0}}",
97
+ ),
98
+ );
99
+ });
100
+
101
+ test("inline: escape characters", () => {
102
+ const noOpen = parseInline("\\[bukan](https://x.com) [iya](https://y.com)");
103
+ assert.equal(noOpen.entities.length, 1);
104
+ assert.equal(metaOf(noOpen.entities[0]).display_name, "iya");
105
+
106
+ const noClose = parseInline("a\\](https://x.com) b");
107
+ assert.equal(noClose.entities.length, 0);
108
+ assert.equal(noClose.text, "a\\](https://x.com) b");
109
+ });
110
+
111
+ test("inline: malformed link tidak merusak link berikutnya", () => {
112
+ const { text, entities } = parseInline("[a](x(b [b](https://ok.com) tail");
113
+ assert.equal(entities.length, 1);
114
+ assert.equal(metaOf(entities[0]).display_name, "b");
115
+ assert.ok(text.startsWith("[a](x(b "), "bagian malformed tetap literal");
116
+ assert.ok(
117
+ text.includes("{{ZAPOK_HYPERLINK_0}}https://ok.com{{/ZAPOK_HYPERLINK_0}}"),
118
+ );
119
+ });
120
+
121
+ test("inline: URL validation menolak skema berbahaya dan relatif", () => {
122
+ const js = parseInline("[x](javascript:alert(1)) [ok](https://ok.com)");
123
+ assert.equal(js.entities.length, 1);
124
+ assert.equal(metaOf(js.entities[0]).url, "https://ok.com");
125
+ assert.ok(
126
+ js.text.includes("[x](javascript:alert(1))"),
127
+ "link berbahaya tetap literal",
128
+ );
129
+
130
+ const data = parseInline("[x](data:text/html,halo)");
131
+ assert.equal(data.entities.length, 0);
132
+
133
+ const rel = parseInline("[x](path/relatif)");
134
+ assert.equal(rel.entities.length, 0);
135
+ });
136
+
137
+ test("inline: URL scheme-less dengan dot tetap diterima", () => {
138
+ const { entities } = parseInline("[x](www.example.com/produk?a=1)");
139
+ assert.equal(entities.length, 1);
140
+ assert.equal(metaOf(entities[0]).url, "www.example.com/produk?a=1");
141
+ });
142
+
143
+ test("inline: empty url ditolak", () => {
144
+ const { entities, text } = parseInline("[kosong]()");
145
+ assert.equal(entities.length, 0);
146
+ assert.equal(text, "[kosong]()");
147
+ });
148
+
149
+ test("inline: untrusted url lewat prefix !", () => {
150
+ const { entities } = parseInline("[un](!https://t.com)");
151
+ assert.equal(metaOf(entities[0]).is_trusted, false);
152
+ assert.equal(metaOf(entities[0]).url, "https://t.com");
153
+ });
154
+
155
+ test("inline: key entitas stabil dan unik per tipe", () => {
156
+ const { entities } = parseInline(
157
+ "[a](https://a.com) [](https://b.com) [c](https://c.com) [d|1]<https://d.com>",
158
+ );
159
+ const keys = entities.map((e) => e.key);
160
+ assert.deepEqual(keys, [
161
+ "ZAPOK_HYPERLINK_0",
162
+ "ZAPOK_CITATION_0",
163
+ "ZAPOK_HYPERLINK_1",
164
+ "ZAPOK_LATEX_0",
165
+ ]);
166
+ assert.equal(new Set(keys).size, keys.length);
167
+
168
+ const again = parseInline(
169
+ "[a](https://a.com) [](https://b.com) [c](https://c.com) [d|1]<https://d.com>",
170
+ );
171
+ assert.deepEqual(
172
+ again.entities.map((e) => e.key),
173
+ keys,
174
+ "keys stabil untuk input sama",
175
+ );
176
+ });
177
+
178
+ test("inline: opsi disable per tipe mempertahankan literal", () => {
179
+ const noHyper = parseInline("[a](https://a.com)", { hyperlink: false });
180
+ assert.equal(noHyper.entities.length, 0);
181
+ assert.equal(noHyper.text, "[a](https://a.com)");
182
+
183
+ const noCite = parseInline("[](https://a.com)", { citation: false });
184
+ assert.equal(noCite.entities.length, 0);
185
+
186
+ const noLatex = parseInline("[x]<https://a.com>", { latex: false });
187
+ assert.equal(noLatex.entities.length, 0);
188
+ });
189
+
190
+ test("inline: unicode dipertahankan", () => {
191
+ const { text, entities } = parseInline("[café ☕](https://cafe.com) ✨");
192
+ assert.equal(metaOf(entities[0]).display_name, "café ☕");
193
+ assert.ok(text.includes("✨"));
194
+ });
195
+
196
+ test("inline: whitespace di display text dan url di-trim", () => {
197
+ const { entities } = parseInline("[ buka ]( https://x.com )");
198
+ assert.equal(metaOf(entities[0]).display_name, "buka");
199
+ assert.equal(metaOf(entities[0]).url, "https://x.com");
200
+ });
@@ -0,0 +1,226 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { airich, EXPERIMENTAL_PRIMITIVES } from "@kyyinfinite/zapo-message";
5
+ import { asClient } from "./helpers.js";
6
+
7
+ const makeClient = () => {
8
+ const sends: { jid?: unknown; content?: unknown; options?: unknown }[] = [];
9
+ return {
10
+ sends,
11
+ client: asClient({
12
+ message: {
13
+ send: async (jid: unknown, content: unknown, options: unknown) => {
14
+ sends.push({ jid, content, options });
15
+ return { id: `EXP_${sends.length}`, attempts: 1, ack: {} };
16
+ },
17
+ },
18
+ }),
19
+ };
20
+ };
21
+
22
+ interface PrimitiveSection {
23
+ view_model: {
24
+ __typename: string;
25
+ primitive?: Record<string, unknown>;
26
+ primitives?: Record<string, unknown>[];
27
+ };
28
+ }
29
+
30
+ const decode = (payload: unknown): { sections: PrimitiveSection[] } => {
31
+ const rich = (
32
+ (payload as unknown as Record<string, unknown>)
33
+ .botForwardedMessage as Record<string, unknown>
34
+ ).message as Record<string, unknown>;
35
+ const rr = rich.richResponseMessage as Record<string, unknown>;
36
+ return JSON.parse(
37
+ Buffer.from((rr.unifiedResponse as { data: Uint8Array }).data).toString(
38
+ "utf8",
39
+ ),
40
+ );
41
+ };
42
+
43
+ const firstPrimitive = (buildFn: () => unknown): Record<string, unknown> => {
44
+ const decoded = decode(buildFn());
45
+ return decoded.sections[0]?.view_model.primitive as Record<string, unknown>;
46
+ };
47
+
48
+ test("primitive (experimental): source single dan multi", () => {
49
+ const { client } = makeClient();
50
+ const rich = airich(client)
51
+ .source([
52
+ {
53
+ url: "https://a.com",
54
+ title: "A",
55
+ subtitle: "sub A",
56
+ icon: "https://a.com/i.png",
57
+ },
58
+ ])
59
+ .source(
60
+ ["https://b.com", ["https://c.com/i.png", "https://c.com", "C", "sub C"]],
61
+ { layout: "HScroll" },
62
+ )
63
+ .build({ submessages: false });
64
+
65
+ const sections = decode(rich).sections;
66
+ const single = sections[0]?.view_model.primitive as Record<string, unknown>;
67
+ assert.equal(single.__typename, EXPERIMENTAL_PRIMITIVES.source);
68
+ const singleSources = single.sources as Record<string, unknown>[];
69
+ assert.equal(singleSources[0]?.source_type, "THIRD_PARTY");
70
+ assert.equal(singleSources[0]?.source_url, "https://a.com");
71
+ assert.equal(singleSources[0]?.source_display_name, "A");
72
+ assert.equal(
73
+ (singleSources[0]?.favicon as Record<string, unknown>).width,
74
+ 16,
75
+ );
76
+
77
+ const multi = sections[1]?.view_model;
78
+ assert.equal(multi?.__typename, "GenAIHScrollLayoutViewModel");
79
+ const multiSources = (multi?.primitive as Record<string, unknown>)
80
+ .sources as Record<string, unknown>[];
81
+ assert.deepEqual(
82
+ multiSources.map((s) => s.source_url),
83
+ ["https://b.com", "https://c.com"],
84
+ );
85
+ assert.equal(multiSources[1]?.source_display_name, "C");
86
+
87
+ assert.throws(() => airich(client).source([]), /array/);
88
+ assert.throws(
89
+ () => airich(client).source([42] as unknown as string[]),
90
+ /harus object/,
91
+ );
92
+ assert.throws(() => airich(client).source([{}]), /url/);
93
+ });
94
+
95
+ test("primitive (experimental): image dan video", () => {
96
+ const { client } = makeClient();
97
+ const image = firstPrimitive(() =>
98
+ airich(client)
99
+ .image("https://x.com/a.png", { width: 512, height: 512 })
100
+ .build({ submessages: false }),
101
+ );
102
+ assert.equal(image.__typename, EXPERIMENTAL_PRIMITIVES.image);
103
+ assert.equal(image.imagine_type, "IMAGE");
104
+ assert.deepEqual(image.media, {
105
+ url: "https://x.com/a.png",
106
+ mime_type: "image/png",
107
+ width: 512,
108
+ height: 512,
109
+ });
110
+ assert.equal((image.status as Record<string, unknown>).status, "READY");
111
+
112
+ const video = firstPrimitive(() =>
113
+ airich(client)
114
+ .video("https://x.com/v.mp4", { duration: 12, file_length: 1024 })
115
+ .build({ submessages: false }),
116
+ );
117
+ assert.equal(video.__typename, EXPERIMENTAL_PRIMITIVES.video);
118
+ assert.equal(video.imagine_type, "ANIMATE");
119
+ assert.equal((video.media as Record<string, unknown>).duration, 12);
120
+ assert.equal(
121
+ "thumbnail" in video,
122
+ false,
123
+ "thumbnail tidak dipalsukan (unverified)",
124
+ );
125
+
126
+ assert.throws(() => airich(client).image(123 as unknown as string), /string/);
127
+ assert.throws(
128
+ () => airich(client).video(null as unknown as string),
129
+ /string/,
130
+ );
131
+ });
132
+
133
+ test("primitive (experimental): footerAction, widget, product, post, reels, html", () => {
134
+ const { client } = makeClient();
135
+ const decoded = decode(
136
+ airich(client)
137
+ .footerAction({ text: "Buka", url: "https://x.com" })
138
+ .widget({
139
+ title: "Kalender",
140
+ sections: [],
141
+ actions: [{ label: "Lihat" }],
142
+ })
143
+ .product({ title: "Produk", price: "Rp10", url: "https://x.com/p" })
144
+ .post([{ title: "Post", username: "user1", url: "https://x.com/post" }])
145
+ .reels([{ url: "https://x.com/r.mp4", username: "kreator" }])
146
+ .html("<b>halo</b>", { trusted_sources: ["https://x.com"] })
147
+ .build({ submessages: false }),
148
+ ).sections;
149
+ const section = (i: number): PrimitiveSection =>
150
+ decoded[i] as PrimitiveSection;
151
+ const prim = (i: number): Record<string, unknown> =>
152
+ section(i).view_model.primitive as Record<string, unknown>;
153
+
154
+ assert.equal(prim(0).cta_type, "OPEN_URL");
155
+ assert.equal(prim(0).cta_url, "https://x.com");
156
+ assert.equal(prim(1).__typename, EXPERIMENTAL_PRIMITIVES.widget);
157
+ assert.equal(
158
+ (
159
+ (prim(1).body as Record<string, unknown>).ctas as Record<
160
+ string,
161
+ unknown
162
+ >[]
163
+ )[0]?.label,
164
+ "Lihat",
165
+ );
166
+ assert.equal(prim(2).__typename, EXPERIMENTAL_PRIMITIVES.product);
167
+ assert.equal(
168
+ (section(3).view_model.primitives as Record<string, unknown>[])[0]
169
+ ?.is_carousel,
170
+ false,
171
+ );
172
+ assert.equal(section(3).view_model.__typename, "GenAIHScrollLayoutViewModel");
173
+ assert.equal(
174
+ (section(4).view_model.primitives as Record<string, unknown>[])[0]
175
+ ?.__typename,
176
+ EXPERIMENTAL_PRIMITIVES.reels,
177
+ );
178
+ assert.equal(prim(5).__typename, EXPERIMENTAL_PRIMITIVES.html);
179
+ assert.deepEqual(prim(5).trusted_sources, ["https://x.com"]);
180
+ });
181
+
182
+ test("primitive (experimental): validasi input", () => {
183
+ const { client } = makeClient();
184
+ assert.throws(() => airich(client).footerAction([]), /minimal satu/);
185
+ assert.throws(
186
+ () => airich(client).footerAction("string" as unknown as never),
187
+ /object/,
188
+ );
189
+ assert.throws(
190
+ () => airich(client).widget(null as unknown as never),
191
+ /object/,
192
+ );
193
+ assert.throws(() => airich(client).product([]), /minimal satu/);
194
+ assert.throws(() => airich(client).post([]), /minimal satu/);
195
+ assert.throws(() => airich(client).reels([]), /minimal satu/);
196
+ assert.throws(() => airich(client).reels([1] as unknown as never), /object/);
197
+ assert.throws(() => airich(client).html(123 as unknown as string), /string/);
198
+ assert.throws(
199
+ () =>
200
+ airich(client).html("<b>x</b>", {
201
+ trusted_sources: "bukan array" as unknown as string[],
202
+ }),
203
+ /array/,
204
+ );
205
+ });
206
+
207
+ test("primitive (experimental): tanpa submessage dan tanpa identitas palsu", () => {
208
+ const { client, sends } = makeClient();
209
+ const builder = airich(client).source(["https://a.com"]);
210
+ assert.equal(builder.size, 1);
211
+
212
+ const payload = builder.build();
213
+ const rr = (
214
+ (payload as unknown as Record<string, unknown>)
215
+ .botForwardedMessage as Record<string, unknown>
216
+ ).message as Record<string, unknown>;
217
+ const rrRich = rr.richResponseMessage as Record<string, unknown>;
218
+ assert.deepEqual(
219
+ rrRich.submessages,
220
+ [],
221
+ "primitive experimental tidak mengarang submessage",
222
+ );
223
+ const contextInfo = rrRich.contextInfo as Record<string, unknown>;
224
+ assert.equal(contextInfo.forwardingScore, 1);
225
+ assert.equal("forwardedAiBotMessageInfo" in contextInfo, false);
226
+ });
@@ -0,0 +1,145 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { tokenizeCode } from "@kyyinfinite/zapo-message/airich/tokenizer.js";
5
+
6
+ const types = (code: string, lang?: string): string[] =>
7
+ tokenizeCode(code, lang).unified_codeBlock.map((t) => t.type);
8
+ const contents = (code: string, lang?: string): string[] =>
9
+ tokenizeCode(code, lang).unified_codeBlock.map((t) => t.content);
10
+
11
+ test("tokenizer: output schema codeBlock + unified_codeBlock dipertahankan", () => {
12
+ const result = tokenizeCode("const x = 1", "javascript");
13
+ assert.deepEqual(Object.keys(result), ["codeBlock", "unified_codeBlock"]);
14
+ assert.deepEqual(
15
+ Object.keys(result.codeBlock[0] as unknown as Record<string, unknown>),
16
+ ["codeContent", "highlightType"],
17
+ );
18
+ assert.deepEqual(
19
+ Object.keys(
20
+ result.unified_codeBlock[0] as unknown as Record<string, unknown>,
21
+ ),
22
+ ["content", "type"],
23
+ );
24
+ assert.equal(result.unified_codeBlock[0]?.type, "KEYWORD");
25
+ assert.equal(
26
+ (result.codeBlock[0] as { highlightType: number }).highlightType,
27
+ 1,
28
+ );
29
+ });
30
+
31
+ test("tokenizer: case sensitivity nama bahasa", () => {
32
+ assert.deepEqual(
33
+ types("const x", "JavaScript"),
34
+ types("const x", "javascript"),
35
+ );
36
+ assert.deepEqual(types("def x", "PYTHON"), types("def x", "python"));
37
+ });
38
+
39
+ test("tokenizer: alias bahasa umum", () => {
40
+ assert.deepEqual(types("const x", "js"), ["KEYWORD", "DEFAULT"]);
41
+ assert.deepEqual(types("def x", "py"), types("def x", "python"));
42
+ assert.deepEqual(types("#!/bin/sh\nif true", "sh").includes("KEYWORD"), true);
43
+ assert.deepEqual(types("int x", "c++"), types("int x", "cpp"));
44
+ });
45
+
46
+ test("tokenizer: plaintext fallback", () => {
47
+ for (const lang of ["txt", "text", "plaintext", "TEXT"]) {
48
+ const result = tokenizeCode('const x = "a"', lang);
49
+ assert.deepEqual(result.unified_codeBlock, [
50
+ { content: 'const x = "a"', type: "DEFAULT" },
51
+ ]);
52
+ }
53
+ });
54
+
55
+ test("tokenizer: bahasa tidak didukung fallback generik tanpa crash", () => {
56
+ const result = tokenizeCode("SELECT * FROM users WHERE x = 1", "sql");
57
+ assert.ok(result.unified_codeBlock.length > 0);
58
+ assert.ok(
59
+ result.unified_codeBlock.every((t) =>
60
+ ["DEFAULT", "NUMBER"].includes(t.type),
61
+ ),
62
+ );
63
+ });
64
+
65
+ test("tokenizer: keyword matching javascript", () => {
66
+ assert.deepEqual(types("if (a) { return b }", "javascript"), [
67
+ "KEYWORD",
68
+ "DEFAULT",
69
+ "KEYWORD",
70
+ "DEFAULT",
71
+ ]);
72
+ assert.ok(contents("if (a) { return b }", "javascript").includes("return"));
73
+ });
74
+
75
+ test("tokenizer: string dengan escape", () => {
76
+ const result = contents('x = "a\\"b"', "javascript");
77
+ assert.deepEqual(result, ["x = ", '"a\\"b"']);
78
+ assert.deepEqual(types('x = "a\\"b"', "javascript")[1], "STR");
79
+ });
80
+
81
+ test("tokenizer: template literal utuh sebagai STR", () => {
82
+ const result = contents("x = `a ${y} b`", "javascript");
83
+ assert.deepEqual(result, ["x = ", "`a ${y} b`"]);
84
+ });
85
+
86
+ test("tokenizer: line comment // dan # (python/bash)", () => {
87
+ assert.deepEqual(types("// komentar", "javascript"), ["COMMENT"]);
88
+ assert.deepEqual(types("# komentar", "python"), ["COMMENT"]);
89
+ assert.deepEqual(types("# komentar", "bash"), ["COMMENT"]);
90
+ });
91
+
92
+ test("tokenizer: block comment /* */ untuk bahasa c-family dan css", () => {
93
+ assert.deepEqual(types("/* catatan */", "css"), ["COMMENT"]);
94
+ assert.deepEqual(types("/* catatan */", "cpp"), ["COMMENT"]);
95
+ assert.deepEqual(types("/* tak selesai", "c"), ["COMMENT"]);
96
+ });
97
+
98
+ test("tokenizer: html comment <!-- -->", () => {
99
+ assert.deepEqual(types("<!-- catatan -->", "html"), ["COMMENT"]);
100
+ });
101
+
102
+ test("tokenizer: python triple-quote multiline string", () => {
103
+ const result = types('x = """baris\nlain"""', "python");
104
+ assert.deepEqual(result, ["DEFAULT", "STR"]);
105
+ });
106
+
107
+ test("tokenizer: numeric token termasuk hex, biner, eksponen", () => {
108
+ const result = contents("0xFF 0b101 1e5 1.5 7", "javascript");
109
+ assert.deepEqual(result, [
110
+ "0xFF",
111
+ " ",
112
+ "0b101",
113
+ " ",
114
+ "1e5",
115
+ " ",
116
+ "1.5",
117
+ " ",
118
+ "7",
119
+ ]);
120
+ assert.deepEqual(types("0xFF", "javascript"), ["NUMBER"]);
121
+ });
122
+
123
+ test("tokenizer: css property diikuti titik dua jadi KEYWORD", () => {
124
+ assert.deepEqual(types("color: red;", "css"), ["KEYWORD", "DEFAULT"]);
125
+ assert.deepEqual(contents("color: red;", "css"), ["color", ": red;"]);
126
+ });
127
+
128
+ test("tokenizer: html tag case-insensitive", () => {
129
+ assert.deepEqual(types("<DIV>x</DIV>", "html"), [
130
+ "DEFAULT",
131
+ "KEYWORD",
132
+ "DEFAULT",
133
+ "KEYWORD",
134
+ "DEFAULT",
135
+ ]);
136
+ });
137
+
138
+ test("tokenizer: method detection untuk identifier diikuti (", () => {
139
+ assert.deepEqual(types("foo()", "javascript"), ["METHOD", "DEFAULT"]);
140
+ assert.deepEqual(types("bar", "javascript"), ["DEFAULT"]);
141
+ });
142
+
143
+ test("tokenizer: kode kosong", () => {
144
+ assert.deepEqual(tokenizeCode("", "javascript").unified_codeBlock, []);
145
+ });