@kyyinfinite/baileys 2.0.0 → 2.1.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.
@@ -0,0 +1,183 @@
1
+ import assert from 'node:assert/strict'
2
+ import { createKyyinfinite, attachKyyinfinite } from '../lib/Kyyinfinite/Simple/index.js'
3
+ import { TextBuilder } from '../lib/Kyyinfinite/Simple/TextBuilder.js'
4
+ import { MediaBuilder } from '../lib/Kyyinfinite/Simple/MediaBuilder.js'
5
+ import { MessageBuilder } from '../lib/Kyyinfinite/Simple/MessageBuilder.js'
6
+ import { KyyMessage } from '../lib/Kyyinfinite/Simple/KyyMessage.js'
7
+ import { parseMessage } from '../lib/Kyyinfinite/Simple/parseMessage.js'
8
+ import { parseJid, makeJid, isGroupJid, isPrivateJid, isLidJid } from '../lib/Kyyinfinite/Simple/jid.js'
9
+ import { extractMentions, applyAutoMentions, resolveMediaContent } from '../lib/Kyyinfinite/Simple/messageUtils.js'
10
+
11
+ const sent = []
12
+ const sock = {
13
+ text: 'NATIVE_TEXT_MUST_SURVIVE',
14
+ sendMessage: async (jid, content, options = {}) => {
15
+ const result = { jid, content, options }
16
+ sent.push(result)
17
+ return result
18
+ }
19
+ }
20
+
21
+ function test(name, fn) {
22
+ try {
23
+ fn()
24
+ console.log(`PASS ${name}`)
25
+ } catch (error) {
26
+ console.error(`FAIL ${name}`)
27
+ throw error
28
+ }
29
+ }
30
+
31
+ async function testAsync(name, fn) {
32
+ try {
33
+ await fn()
34
+ console.log(`PASS ${name}`)
35
+ } catch (error) {
36
+ console.error(`FAIL ${name}`)
37
+ throw error
38
+ }
39
+ }
40
+
41
+ test('JID parse + classify', () => {
42
+ assert.deepEqual(parseJid('12345:7@s.whatsapp.net'), {
43
+ user: '12345', device: '7', server: 's.whatsapp.net', jid: '12345:7@s.whatsapp.net'
44
+ })
45
+ assert.equal(makeJid(12345), '12345@s.whatsapp.net')
46
+ assert.equal(isPrivateJid('12345@s.whatsapp.net'), true)
47
+ assert.equal(isGroupJid('123-456@g.us'), true)
48
+ assert.equal(isLidJid('12345@lid'), true)
49
+ })
50
+
51
+ test('Mention extraction + dedupe', () => {
52
+ assert.deepEqual(extractMentions('Hi @628123456789 @628123456789!'), ['628123456789@s.whatsapp.net'])
53
+ assert.deepEqual(applyAutoMentions({ text: 'Hi @628123456789' }).mentions, ['628123456789@s.whatsapp.net'])
54
+ assert.equal(applyAutoMentions({ text: 'Hi @628123456789' }, false).mentions, undefined)
55
+ })
56
+
57
+ test('Media resolver', () => {
58
+ assert.deepEqual(resolveMediaContent({ image: 'https://example.com/a.jpg', caption: 'x' }), {
59
+ image: { url: 'https://example.com/a.jpg' }, caption: 'x'
60
+ })
61
+ })
62
+
63
+ test('Simple API contract exposes final public methods', () => {
64
+ const api = createKyyinfinite(sock)
65
+ const methods = [
66
+ 'text', 'media', 'send', 'quoted', 'mentions', 'resolveMedia', 'resolveMediaContent',
67
+ 'wrapMessage', 'parseMessage', 'jid', 'parseJid', 'jidUser', 'jidServer', 'jidDevice',
68
+ 'isGroup', 'isPrivate', 'isBroadcast', 'isStatus', 'isNewsletter', 'isLid', 'makeJid',
69
+ 'reply', 'reactTo', 'editMessage', 'deleteMessage', 'forwardMessage', 'downloadMessage',
70
+ 'image', 'video', 'audio', 'voice', 'document', 'sticker', 'ptv', 'gif', 'message',
71
+ 'poll', 'event', 'album', 'contacts', 'location', 'buttonReply', 'listReply', 'react',
72
+ 'groupInvite', 'product', 'requestPhoneNumber', 'sharePhoneNumber', 'pin', 'forward',
73
+ 'delete', 'disappearing', 'limitSharing', 'button', 'buttonV2', 'carousel', 'ai', 'foad', 'foad2'
74
+ ]
75
+ for (const name of methods) assert.equal(typeof api[name], 'function', `missing ${name}()`)
76
+ for (const name of ['Toolkit', 'Button', 'ButtonV2', 'Carousel', 'AIRich', 'Foad', 'Foad2']) {
77
+ assert.ok(api.raw[name], `missing raw.${name}`)
78
+ }
79
+ })
80
+
81
+ test('createKyyinfinite is non-mutating', () => {
82
+ const local = { sendMessage: sock.sendMessage }
83
+ const api = createKyyinfinite(local)
84
+ assert.equal(local.text, undefined)
85
+ assert.equal(typeof api.text, 'function')
86
+ assert.equal(typeof api.send, 'function')
87
+ })
88
+
89
+ test('attachKyyinfinite preserves native collisions', () => {
90
+ const local = { text: 'native', sendMessage: sock.sendMessage }
91
+ const result = attachKyyinfinite(local)
92
+ assert.equal(result, local)
93
+ assert.equal(local.text, 'native')
94
+ assert.equal(typeof local.image, 'function')
95
+ })
96
+
97
+ await testAsync('Simple text send + auto mentions', async () => {
98
+ sent.length = 0
99
+ const api = createKyyinfinite(sock)
100
+ const result = await api.text('Hello @628123456789').send('628000@g.us')
101
+ assert.equal(result.jid, '628000@g.us')
102
+ assert.deepEqual(result.content.mentions, ['628123456789@s.whatsapp.net'])
103
+ assert.equal(result.content.text, 'Hello @628123456789')
104
+ })
105
+
106
+ await testAsync('Unified send', async () => {
107
+ sent.length = 0
108
+ const api = createKyyinfinite(sock)
109
+ const result = await api.send('123@s.whatsapp.net', 'Hello')
110
+ assert.deepEqual(result.content, { text: 'Hello' })
111
+
112
+ const media = await api.send('123@s.whatsapp.net', {
113
+ image: 'https://example.com/a.jpg',
114
+ caption: 'Photo @628123456789'
115
+ })
116
+ assert.deepEqual(media.content.image, { url: 'https://example.com/a.jpg' })
117
+ assert.deepEqual(media.content.mentions, ['628123456789@s.whatsapp.net'])
118
+ })
119
+
120
+ await testAsync('quoted + message shortcuts', async () => {
121
+ sent.length = 0
122
+ const api = createKyyinfinite(sock)
123
+ const message = { key: { id: 'ABC', remoteJid: '123@g.us', fromMe: false }, message: { conversation: 'old' } }
124
+ await api.quoted(message, 'reply')
125
+ assert.equal(sent[0].options.quoted, message)
126
+
127
+ const wrapped = new KyyMessage(sock, message)
128
+ await wrapped.reply('reply 2')
129
+ await wrapped.react('ok')
130
+ assert.equal(sent[1].options.quoted, message)
131
+ assert.deepEqual(sent[2].content.react, { text: 'ok', key: message.key })
132
+ })
133
+
134
+ test('Builder state isolation', () => {
135
+ const api = createKyyinfinite(sock)
136
+ const a = api.text('A').footer('one')
137
+ const b = api.text('B').footer('two')
138
+ assert.equal(a.build().text, 'A')
139
+ assert.equal(a.build().footer, 'one')
140
+ assert.equal(b.build().text, 'B')
141
+ assert.equal(b.build().footer, 'two')
142
+ })
143
+
144
+ test('Universal media builder type switching', () => {
145
+ const api = createKyyinfinite(sock)
146
+ assert.deepEqual(api.media('x').asDocument().build(), { document: { url: 'x' } })
147
+ assert.deepEqual(api.media('x').asImage().build(), { image: { url: 'x' } })
148
+ })
149
+
150
+ test('parseMessage command + group metadata', () => {
151
+ const parsed = parseMessage({
152
+ key: { remoteJid: '123-456@g.us', participant: '628111@s.whatsapp.net', fromMe: false, id: 'M1' },
153
+ message: { conversation: '.ping one two' }
154
+ }, { prefix: '.' })
155
+ assert.equal(parsed.text, '.ping one two')
156
+ assert.equal(parsed.command, 'ping')
157
+ assert.deepEqual(parsed.args, ['one', 'two'])
158
+ assert.equal(parsed.isGroup, true)
159
+ assert.equal(parsed.sender, '628111@s.whatsapp.net')
160
+ })
161
+
162
+ await testAsync('Fluent builder quoted() + send options', async () => {
163
+ sent.length = 0
164
+ const api = createKyyinfinite(sock)
165
+ const quoted = { key: { id: 'Q1' }, message: { conversation: 'quoted' } }
166
+ const result = await api.text('reply').quoted(quoted).send('123@s.whatsapp.net', { ephemeralExpiration: 3600 })
167
+ assert.equal(result.options.quoted, quoted)
168
+ assert.equal(result.options.ephemeralExpiration, 3600)
169
+ })
170
+
171
+ await testAsync('Builder send forwards native sendMessage', async () => {
172
+ sent.length = 0
173
+ const api = createKyyinfinite(sock)
174
+ const result = await new TextBuilder(sock, 'builder').send('123@s.whatsapp.net', { quoted: 'q' })
175
+ assert.equal(result.options.quoted, 'q')
176
+ const media = await new MediaBuilder(sock, 'image', 'x.jpg').caption('x').send('123@s.whatsapp.net')
177
+ assert.deepEqual(media.content.image, { url: 'x.jpg' })
178
+ const message = await new MessageBuilder(sock, { text: 'm' }).send('123@s.whatsapp.net')
179
+ assert.equal(message.content.text, 'm')
180
+ void api
181
+ })
182
+
183
+ console.log(`\n2.1.0 Simple API test suite: ${'PASS'}`)