|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { Client } from '../../src/client/client.js' |
| 3 | +import { MemoryAuthStore } from '../../src/auth/adapters/memory.js' |
| 4 | +import { attachInboundPipeline } from '../../src/events/pipeline.js' |
| 5 | +import { makeInboundSocket } from '../_helpers/mock-socket-events.js' |
| 6 | + |
| 7 | +const SELF = '628SELF@s.whatsapp.net' |
| 8 | +const SENDER = '628111@s.whatsapp.net' |
| 9 | + |
| 10 | +/** Drives the real decode pipeline so the command sees exactly what a live message produces. */ |
| 11 | +const bot = () => { |
| 12 | + const client = new Client({ |
| 13 | + auth: new MemoryAuthStore(), |
| 14 | + qrTerminal: false, |
| 15 | + autoConnect: false, |
| 16 | + commandPrefix: '!', |
| 17 | + }) |
| 18 | + const socket = makeInboundSocket({ user: { id: SELF } }) |
| 19 | + ;(client as unknown as { _socket: unknown })._socket = socket |
| 20 | + attachInboundPipeline(client, socket as unknown as Parameters<typeof attachInboundPipeline>[1], { |
| 21 | + selfJid: SELF, |
| 22 | + }) |
| 23 | + return { client, socket } |
| 24 | +} |
| 25 | + |
| 26 | +const deliver = async (socket: ReturnType<typeof bot>['socket'], message: unknown) => { |
| 27 | + socket.triggerMessagesUpsert({ |
| 28 | + messages: [{ key: { remoteJid: SENDER, id: 'M1', fromMe: false }, message, messageTimestamp: 1700 }], |
| 29 | + type: 'notify', |
| 30 | + }) |
| 31 | + await new Promise((r) => setTimeout(r, 20)) |
| 32 | +} |
| 33 | + |
| 34 | +describe('a command may arrive as a media caption', () => { |
| 35 | + it('runs when the prefix is the caption of an image', async () => { |
| 36 | + const { client, socket } = bot() |
| 37 | + const run = vi.fn() |
| 38 | + client.command('sticker', run) |
| 39 | + await deliver(socket, { imageMessage: { mimetype: 'image/jpeg', caption: '!sticker' } }) |
| 40 | + expect(run).toHaveBeenCalledTimes(1) |
| 41 | + }) |
| 42 | + |
| 43 | + it('runs for a video caption too, and passes its arguments', async () => { |
| 44 | + const { client, socket } = bot() |
| 45 | + const run = vi.fn() |
| 46 | + client.command('swgc', run) |
| 47 | + await deliver(socket, { videoMessage: { mimetype: 'video/mp4', caption: '!swgc promo hari ini' } }) |
| 48 | + expect(run).toHaveBeenCalledTimes(1) |
| 49 | + expect((run.mock.calls[0]![0] as { args: string[] }).args).toEqual(['promo', 'hari', 'ini']) |
| 50 | + }) |
| 51 | + |
| 52 | + it('still runs for a plain text message', async () => { |
| 53 | + const { client, socket } = bot() |
| 54 | + const run = vi.fn() |
| 55 | + client.command('ping', run) |
| 56 | + await deliver(socket, { conversation: '!ping' }) |
| 57 | + expect(run).toHaveBeenCalledTimes(1) |
| 58 | + }) |
| 59 | + |
| 60 | + it('runs a command exactly once, not once per event it appears on', async () => { |
| 61 | + const { client, socket } = bot() |
| 62 | + const run = vi.fn() |
| 63 | + client.command('ping', run) |
| 64 | + await deliver(socket, { conversation: '!ping' }) |
| 65 | + expect(run).toHaveBeenCalledTimes(1) |
| 66 | + }) |
| 67 | + |
| 68 | + it('leaves a captioned image alone when the caption is not a command', async () => { |
| 69 | + const { client, socket } = bot() |
| 70 | + const run = vi.fn() |
| 71 | + client.command('sticker', run) |
| 72 | + await deliver(socket, { imageMessage: { mimetype: 'image/jpeg', caption: 'foto liburan' } }) |
| 73 | + expect(run).not.toHaveBeenCalled() |
| 74 | + }) |
| 75 | +}) |
0 commit comments