|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { Client, parseMexUsers } from '../../src/client/client.js' |
| 3 | +import { MemoryAuthStore } from '../../src/auth/adapters/memory.js' |
| 4 | + |
| 5 | +const LID = '115362955808779@lid' |
| 6 | +const LID2 = '234256710246613@lid' |
| 7 | + |
| 8 | +/** Shaped from a real `w:mex` reply captured on the wire. */ |
| 9 | +const mexReply = (users: unknown[]) => ({ |
| 10 | + tag: 'iq', |
| 11 | + attrs: { from: '@s.whatsapp.net', type: 'result' }, |
| 12 | + content: [ |
| 13 | + { tag: 'result', attrs: {}, content: Buffer.from(JSON.stringify({ data: { xwa2_fetch_wa_users: users } })) }, |
| 14 | + ], |
| 15 | +}) |
| 16 | + |
| 17 | +const namedUser = (jid: string, username: string) => ({ |
| 18 | + __typename: 'XWA2User', |
| 19 | + id: null, |
| 20 | + jid, |
| 21 | + username_info: { __typename: 'XWA2Username', pin: null, state: null, timestamp: null, username }, |
| 22 | +}) |
| 23 | + |
| 24 | +const emptyUser = (jid: string) => ({ |
| 25 | + __typename: 'XWA2User', |
| 26 | + id: null, |
| 27 | + jid, |
| 28 | + username_info: { __typename: 'XWA2ResponseStatus', status: 'EMPTY' }, |
| 29 | +}) |
| 30 | + |
| 31 | +const connected = (query: unknown) => { |
| 32 | + const c = new Client({ auth: new MemoryAuthStore(), qrTerminal: false, autoConnect: false }) |
| 33 | + ;(c as unknown as { _socket: unknown })._socket = { query, generateMessageTag: () => 'TAG1' } |
| 34 | + return c |
| 35 | +} |
| 36 | + |
| 37 | +describe('parseMexUsers', () => { |
| 38 | + it('reads the usernames out of a real reply', () => { |
| 39 | + expect(parseMexUsers(mexReply([namedUser(LID, 'hacelo'), namedUser(LID2, 'hanntylor')]))).toEqual([ |
| 40 | + { jid: LID, username: 'hacelo' }, |
| 41 | + { jid: LID2, username: 'hanntylor' }, |
| 42 | + ]) |
| 43 | + }) |
| 44 | + |
| 45 | + it('skips users WhatsApp reports as having no username', () => { |
| 46 | + expect(parseMexUsers(mexReply([emptyUser(LID), namedUser(LID2, 'hanntylor')]))).toEqual([ |
| 47 | + { jid: LID2, username: 'hanntylor' }, |
| 48 | + ]) |
| 49 | + }) |
| 50 | + |
| 51 | + it('survives a malformed or unexpected payload', () => { |
| 52 | + expect(parseMexUsers(undefined)).toEqual([]) |
| 53 | + expect(parseMexUsers({ content: 'not-an-array' })).toEqual([]) |
| 54 | + expect(parseMexUsers({ content: [{ tag: 'other', content: Buffer.from('{}') }] })).toEqual([]) |
| 55 | + expect(parseMexUsers({ content: [{ tag: 'result', content: Buffer.from('not json') }] })).toEqual([]) |
| 56 | + expect(parseMexUsers({ content: [{ tag: 'result', content: Buffer.from('{"data":{}}') }] })).toEqual([]) |
| 57 | + }) |
| 58 | + |
| 59 | + it('ignores an entry whose username is empty', () => { |
| 60 | + expect(parseMexUsers(mexReply([namedUser(LID, '')]))).toEqual([]) |
| 61 | + }) |
| 62 | +}) |
| 63 | + |
| 64 | +describe('Client.usernames', () => { |
| 65 | + it('resolves many jids in a single round trip', async () => { |
| 66 | + const query = vi.fn(async () => mexReply([namedUser(LID, 'hacelo'), namedUser(LID2, 'hanntylor')])) |
| 67 | + const map = await connected(query).usernames([LID, LID2]) |
| 68 | + expect(query).toHaveBeenCalledTimes(1) |
| 69 | + expect(map.get(LID)).toBe('hacelo') |
| 70 | + expect(map.get(LID2)).toBe('hanntylor') |
| 71 | + }) |
| 72 | + |
| 73 | + it('sends the GraphQL document the official client uses', async () => { |
| 74 | + const query = vi.fn(async () => mexReply([])) |
| 75 | + await connected(query).usernames([LID]) |
| 76 | + const iq = query.mock.calls[0]![0] as { |
| 77 | + attrs: { xmlns: string } |
| 78 | + content: Array<{ attrs: { query_id: string }; content: Buffer }> |
| 79 | + } |
| 80 | + expect(iq.attrs.xmlns).toBe('w:mex') |
| 81 | + expect(iq.content[0]!.attrs.query_id).toBe('29829202653362039') |
| 82 | + const body = JSON.parse(iq.content[0]!.content.toString()) |
| 83 | + expect(body.variables.include_username).toBe(true) |
| 84 | + expect(body.variables.input.query_input).toEqual([{ jid: LID }]) |
| 85 | + }) |
| 86 | + |
| 87 | + it('omits jids that have no username rather than mapping them to null', async () => { |
| 88 | + const query = vi.fn(async () => mexReply([emptyUser(LID)])) |
| 89 | + const map = await connected(query).usernames([LID]) |
| 90 | + expect(map.has(LID)).toBe(false) |
| 91 | + expect(map.size).toBe(0) |
| 92 | + }) |
| 93 | + |
| 94 | + it('skips the round trip for an empty input', async () => { |
| 95 | + const query = vi.fn(async () => mexReply([])) |
| 96 | + expect((await connected(query).usernames([])).size).toBe(0) |
| 97 | + expect(query).not.toHaveBeenCalled() |
| 98 | + }) |
| 99 | + |
| 100 | + it('returns an empty map when the query throws', async () => { |
| 101 | + const query = vi.fn(async () => { |
| 102 | + throw new Error('offline') |
| 103 | + }) |
| 104 | + expect((await connected(query).usernames([LID])).size).toBe(0) |
| 105 | + }) |
| 106 | + |
| 107 | + it('returns an empty map when no socket is attached', async () => { |
| 108 | + const c = new Client({ auth: new MemoryAuthStore(), qrTerminal: false, autoConnect: false }) |
| 109 | + expect((await c.usernames([LID])).size).toBe(0) |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
| 113 | +describe('Client.getUsername', () => { |
| 114 | + it('returns the single username', async () => { |
| 115 | + const query = vi.fn(async () => mexReply([namedUser(LID, 'hacelo')])) |
| 116 | + await expect(connected(query).getUsername(LID)).resolves.toBe('hacelo') |
| 117 | + }) |
| 118 | + |
| 119 | + it('returns null when the account has no username', async () => { |
| 120 | + const query = vi.fn(async () => mexReply([emptyUser(LID)])) |
| 121 | + await expect(connected(query).getUsername(LID)).resolves.toBeNull() |
| 122 | + }) |
| 123 | +}) |
0 commit comments