|
| 1 | +import { describe, expect, it, vi, beforeEach } from 'vitest' |
| 2 | +import { createMockSocket } from '../_helpers/mock-socket.js' |
| 3 | + |
| 4 | +const { makeWASocketMock, initAuthCredsMock } = vi.hoisted(() => ({ |
| 5 | + makeWASocketMock: vi.fn(), |
| 6 | + initAuthCredsMock: vi.fn(() => ({ fake: 'creds' })), |
| 7 | +})) |
| 8 | + |
| 9 | +vi.mock('baileys', () => ({ |
| 10 | + default: makeWASocketMock, |
| 11 | + makeWASocket: makeWASocketMock, |
| 12 | + initAuthCreds: initAuthCredsMock, |
| 13 | + DisconnectReason: { |
| 14 | + loggedOut: 401, forbidden: 403, connectionLost: 408, multideviceMismatch: 411, |
| 15 | + connectionClosed: 428, connectionReplaced: 440, badSession: 500, |
| 16 | + unavailableService: 503, restartRequired: 515, timedOut: 408, |
| 17 | + }, |
| 18 | + makeCacheableSignalKeyStore: vi.fn((k: unknown) => k), |
| 19 | + BufferJSON: { replacer: (_k: string, v: unknown) => v, reviver: (_k: string, v: unknown) => v }, |
| 20 | +})) |
| 21 | + |
| 22 | +vi.mock('../../src/connection/qr-terminal.js', async () => { |
| 23 | + const actual = await vi.importActual<typeof import('../../src/connection/qr-terminal.js')>( |
| 24 | + '../../src/connection/qr-terminal.js', |
| 25 | + ) |
| 26 | + return { ...actual, printQrToTerminal: vi.fn(async () => undefined) } |
| 27 | +}) |
| 28 | + |
| 29 | +import { Client } from '../../src/client/client.js' |
| 30 | +import { MemoryAuthStore } from '../../src/auth/adapters/memory.js' |
| 31 | +import type { ClientOptions } from '../../src/client/types.js' |
| 32 | + |
| 33 | +beforeEach(() => { |
| 34 | + makeWASocketMock.mockReset() |
| 35 | + initAuthCredsMock.mockClear() |
| 36 | +}) |
| 37 | + |
| 38 | +const CALLER = '628111@s.whatsapp.net' |
| 39 | + |
| 40 | +function mkSocket() { |
| 41 | + const sock = createMockSocket({ user: { id: 'me@s.whatsapp.net' } }) |
| 42 | + const rejectCall = vi.fn(async () => undefined) |
| 43 | + Object.assign(sock, { rejectCall }) |
| 44 | + return Object.assign(sock, { rejectCall }) |
| 45 | +} |
| 46 | + |
| 47 | +async function boot(opts: Partial<ClientOptions> = {}) { |
| 48 | + const sock = mkSocket() |
| 49 | + makeWASocketMock.mockReturnValue(sock) |
| 50 | + const c = new Client({ |
| 51 | + auth: new MemoryAuthStore(), qrTerminal: false, autoConnect: false, statusLog: false, ...opts, |
| 52 | + }) |
| 53 | + const p = c.connect() |
| 54 | + sock.triggerConnectionUpdate({ connection: 'open' }) |
| 55 | + await p |
| 56 | + return { c, sock } |
| 57 | +} |
| 58 | + |
| 59 | +const ringCall = (sock: ReturnType<typeof mkSocket>, from = CALLER) => |
| 60 | + sock.ev.emit('call', [ |
| 61 | + { id: 'CALL1', from, status: 'offer', date: new Date(), isGroup: false, isVideo: false }, |
| 62 | + ]) |
| 63 | + |
| 64 | +const settle = () => new Promise((r) => setTimeout(r, 20)) |
| 65 | + |
| 66 | +describe('autoRejectCall config', () => { |
| 67 | + it('rejects an incoming call when autoRejectCall: true', async () => { |
| 68 | + const { sock } = await boot({ autoRejectCall: true }) |
| 69 | + ringCall(sock) |
| 70 | + await settle() |
| 71 | + expect(sock.rejectCall).toHaveBeenCalledWith('CALL1', CALLER) |
| 72 | + }) |
| 73 | + |
| 74 | + it('does NOT reject by default (opt-in)', async () => { |
| 75 | + const { sock } = await boot() |
| 76 | + ringCall(sock) |
| 77 | + await settle() |
| 78 | + expect(sock.rejectCall).not.toHaveBeenCalled() |
| 79 | + }) |
| 80 | + |
| 81 | + it('autoRejectCall: false does not reject', async () => { |
| 82 | + const { sock } = await boot({ autoRejectCall: false }) |
| 83 | + ringCall(sock) |
| 84 | + await settle() |
| 85 | + expect(sock.rejectCall).not.toHaveBeenCalled() |
| 86 | + }) |
| 87 | + |
| 88 | + it('respects the allow list end-to-end', async () => { |
| 89 | + const { sock } = await boot({ autoRejectCall: { enabled: true, allow: [CALLER] } }) |
| 90 | + ringCall(sock) |
| 91 | + await settle() |
| 92 | + expect(sock.rejectCall).not.toHaveBeenCalled() |
| 93 | + |
| 94 | + ringCall(sock, '628999@s.whatsapp.net') |
| 95 | + await settle() |
| 96 | + expect(sock.rejectCall).toHaveBeenCalledOnce() |
| 97 | + }) |
| 98 | + |
| 99 | + it('runs onReject with the call payload', async () => { |
| 100 | + const onReject = vi.fn() |
| 101 | + const { sock } = await boot({ autoRejectCall: { enabled: true, onReject } }) |
| 102 | + ringCall(sock) |
| 103 | + await settle() |
| 104 | + expect(onReject).toHaveBeenCalledOnce() |
| 105 | + expect(onReject.mock.calls[0]?.[0]).toMatchObject({ callId: 'CALL1', from: CALLER }) |
| 106 | + }) |
| 107 | + |
| 108 | + it('rejects exactly once per call (no double-attach)', async () => { |
| 109 | + const { sock } = await boot({ autoRejectCall: true }) |
| 110 | + ringCall(sock) |
| 111 | + await settle() |
| 112 | + expect(sock.rejectCall).toHaveBeenCalledTimes(1) |
| 113 | + }) |
| 114 | +}) |
| 115 | + |
| 116 | +describe('client.rejectCall()', () => { |
| 117 | + it('accepts the call payload from the event', async () => { |
| 118 | + const { c, sock } = await boot() |
| 119 | + const seen = new Promise<void>((resolve) => { |
| 120 | + c.on('call-incoming', async (call) => { |
| 121 | + await c.rejectCall(call) |
| 122 | + resolve() |
| 123 | + }) |
| 124 | + }) |
| 125 | + ringCall(sock) |
| 126 | + await seen |
| 127 | + expect(sock.rejectCall).toHaveBeenCalledWith('CALL1', CALLER) |
| 128 | + }) |
| 129 | + |
| 130 | + it('accepts (callId, from)', async () => { |
| 131 | + const { c, sock } = await boot() |
| 132 | + await c.rejectCall('CALL9', '628222@s.whatsapp.net') |
| 133 | + expect(sock.rejectCall).toHaveBeenCalledWith('CALL9', '628222@s.whatsapp.net') |
| 134 | + }) |
| 135 | + |
| 136 | + it('throws UNSUPPORTED_ON_CLOUD on the cloud provider', async () => { |
| 137 | + const c = new Client({ |
| 138 | + provider: 'cloud', |
| 139 | + cloud: { accessToken: 'tok', phoneNumberId: '555' }, |
| 140 | + autoConnect: false, statusLog: false, |
| 141 | + }) |
| 142 | + await expect(c.rejectCall('C', 'x@s.whatsapp.net')).rejects.toMatchObject({ |
| 143 | + code: 'UNSUPPORTED_ON_CLOUD', |
| 144 | + }) |
| 145 | + }) |
| 146 | +}) |
0 commit comments