12 "github.com/stretchr/testify/require"
14 "go.mau.fi/whatsmeow/types"
15 "go.mau.fi/whatsmeow/types/events"
18 func TestWhatsAppStatusFollowsTheConnection(t *testing.T) {
20 whatsappTestSession(t, dir, true)
22 clock := newTestClock()
23 whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
24 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
27 require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
29 require.True(t, fake.Emit(&events.Connected{}))
30 require.Equal(t, "WhatsApp is linked and connected since 2026-01-01 12:00.", whatsapp.Status())
32 clock.now = clock.now.Add(20 * time.Minute)
34 require.True(t, fake.Emit(&events.Disconnected{}))
36 clock.now = clock.now.Add(time.Minute)
38 require.True(t, fake.Emit(&events.Disconnected{}))
39 require.Equal(t, "WhatsApp is linked and disconnected since 2026-01-01 12:20. The last connection started at 2026-01-01 12:00.", whatsapp.Status())
42 func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
43 synctest.Test(t, func(t *testing.T) {
44 test := whatsappRetryTest(t)
45 test.fake.connectErr = errors.New("network unavailable")
49 require.Equal(t, "WhatsApp is linked and disconnected since "+time.Now().Format(whatsappTimeLayout)+".", test.whatsapp.Status())
50 require.Equal(t, "<4>first connect failed, retrying: network unavailable\n", test.logs.String())
54 func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) {
55 synctest.Test(t, func(t *testing.T) {
56 test := whatsappRetryTest(t)
58 test.fake.connectErrors = append(test.fake.connectErrors, errors.New("network unavailable"))
63 require.Len(t, test.fake.Connections(), 1)
64 require.True(t, test.fake.Emit(&events.Disconnected{}))
65 require.True(t, test.fake.Emit(&events.Disconnected{}))
67 delays := []time.Duration{2 * time.Second, 4 * time.Second, 8 * time.Second, 16 * time.Second, 32 * time.Second, 64 * time.Second, 128 * time.Second, 256 * time.Second, 5 * time.Minute, 5 * time.Minute}
69 var outage time.Duration
71 for index, delay := range delays {
72 time.Sleep(delay - time.Nanosecond)
74 require.Len(t, test.fake.Connections(), index+1)
75 time.Sleep(time.Nanosecond)
77 require.Len(t, test.fake.Connections(), index+2)
82 times := test.fake.ConnectionTimes()
83 for index, delay := range delays {
84 require.Equal(t, delay, times[index+1].Sub(times[index]))
87 require.True(t, test.fake.Emit(&events.Connected{}))
88 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
90 time.Sleep(10 * time.Minute)
92 require.Len(t, test.fake.Connections(), 11)
93 require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after="+daemonLogDuration(outage)+"\n", test.logs.String())
97 func TestWhatsAppFirstConnectionRetriesStopWhenContextEnds(t *testing.T) {
98 synctest.Test(t, func(t *testing.T) {
99 test := whatsappRetryTest(t)
100 test.fake.connectErr = errors.New("network unavailable")
103 time.Sleep(2 * time.Second)
105 require.Len(t, test.fake.Connections(), 2)
108 require.NoError(t, <-test.done)
109 time.Sleep(10 * time.Minute)
111 require.Len(t, test.fake.Connections(), 2)
115 func TestWhatsAppLinkedPairRequestKeepsFirstConnectionRetries(t *testing.T) {
116 synctest.Test(t, func(t *testing.T) {
117 test := whatsappRetryTest(t)
118 test.fake.connectErr = errors.New("network unavailable")
122 answers := make(chan string, 1)
123 test.whatsapp.Command("pair 15550001111", func(text string) {
127 require.Equal(t, "WhatsApp is linked already. Run /whatsapp unlink first.", <-answers)
129 test.fake.mutex.Lock()
131 test.fake.connectErr = nil
132 test.fake.mutex.Unlock()
133 time.Sleep(whatsappRetryDelay)
135 require.Len(t, test.fake.Connections(), 2)
136 require.True(t, test.fake.Emit(&events.Connected{}))
137 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
138 require.Contains(t, test.whatsapp.Status(), "linked and connected since")
139 time.Sleep(whatsappRetryLimit)
141 require.Len(t, test.fake.Connections(), 2)
142 require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after=2s\n", test.logs.String())
146 func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
147 testCases := []struct {
149 stop func(test *WhatsAppRetryTest)
152 name: "stops retrying after unlink",
153 stop: func(test *WhatsAppRetryTest) {
154 test.whatsapp.Command("unlink", func(_ string) {
159 name: "stops retrying after logout",
160 stop: func(test *WhatsAppRetryTest) {
161 test.fake.Emit(&events.LoggedOut{})
165 name: "stops retrying after a permanent disconnect",
166 stop: func(test *WhatsAppRetryTest) {
167 test.fake.Emit(&events.StreamReplaced{})
172 for _, tc := range testCases {
173 t.Run(tc.name, func(t *testing.T) {
174 synctest.Test(t, func(t *testing.T) {
175 test := whatsappRetryTest(t)
176 test.fake.connectErr = errors.New("network unavailable")
181 time.Sleep(10 * time.Minute)
183 require.Len(t, test.fake.Connections(), 1)
189 func TestWhatsAppPairsAfterFirstConnectionRetriesStop(t *testing.T) {
190 synctest.Test(t, func(t *testing.T) {
191 test := whatsappRetryTest(t)
192 test.fake.connectErr = errors.New("network unavailable")
195 test.whatsapp.Command("unlink", func(_ string) {
199 require.False(t, test.whatsapp.isLinked())
201 test.fake.mutex.Lock()
203 test.fake.connectErr = nil
204 test.fake.mutex.Unlock()
205 test.whatsapp.Command("pair 15550001111", func(_ string) {
208 items := <-test.fake.pairings
209 items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
212 require.True(t, test.fake.Emit(&events.PairSuccess{
213 ID: types.NewADJID(whatsappTestNumber, 0, 1),
214 LID: types.NewADJID(whatsappTestLID, 0, 1),
217 items <- whatsmeow.QRChannelSuccess
220 require.True(t, test.whatsapp.isLinked())
221 time.Sleep(10 * time.Minute)
223 require.Len(t, test.fake.Connections(), 2)
224 require.Equal(t, []string{whatsappTestNumber}, test.fake.Phones())
228 func TestWhatsAppInboxFailureStopsFirstConnectionRetries(t *testing.T) {
229 synctest.Test(t, func(t *testing.T) {
230 test := whatsappRetryTest(t)
231 test.fake.connectErr = errors.New("network unavailable")
235 failure := errors.New("state write failed")
236 test.whatsapp.inbox.failures <- failure
238 require.ErrorIs(t, <-test.done, failure)
239 time.Sleep(10 * time.Minute)
241 require.Len(t, test.fake.Connections(), 1)
245 func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) {
246 synctest.Test(t, func(t *testing.T) {
247 test := whatsappRetryTest(t)
248 test.fake.connectErr = errors.New("network unavailable")
252 since := time.Now().Format(whatsappTimeLayout)
254 time.Sleep(whatsappOutageNoticeDelay)
256 require.Equal(t, 1, test.events.Count("notice"))
257 require.Equal(t, "WhatsApp is disconnected since "+since+". OneChat tries again.", test.events.Last("notice"))
259 time.Sleep(whatsappOutageNoticeDelay)
261 require.Equal(t, 1, test.events.Count("notice"))
262 test.fake.mutex.Lock()
264 test.fake.connectErr = nil
265 test.fake.mutex.Unlock()
266 time.Sleep(whatsappRetryLimit)
268 require.True(t, test.fake.Emit(&events.Connected{}))
269 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
271 require.Equal(t, 2, test.events.Count("notice"))
272 require.Equal(t, "WhatsApp is connected again.", test.events.Last("notice"))
276 func TestWhatsAppStartingRetriesDoNotDuplicateLibraryRetries(t *testing.T) {
278 whatsappTestSession(t, dir, true)
280 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
281 client, ok := whatsappNewClient(whatsapp.device, whatsapp.handle).(*whatsmeow.Client)
283 require.False(t, client.InitialAutoReconnect)
284 require.True(t, client.EnableAutoReconnect)
286 device := whatsapp.container.NewDevice()
287 client, ok = whatsappNewClient(device, whatsapp.handle).(*whatsmeow.Client)
289 require.True(t, client.InitialAutoReconnect)
292 func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
294 whatsappTestSession(t, dir, true)
296 clock := newTestClock()
297 whatsapp, fake, logs := whatsappStartTest(t, dir, clock)
298 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
301 require.True(t, fake.Emit(&events.Connected{}))
303 clock.now = clock.now.Add(time.Minute)
305 require.True(t, fake.Emit(&events.Disconnected{}))
307 clock.now = clock.now.Add(time.Minute)
309 require.True(t, fake.Emit(&events.Disconnected{}))
311 clock.now = clock.now.Add(25 * time.Second)
313 require.True(t, fake.Emit(&events.Connected{}))
315 require.Equal(t, []string{
317 "<4>disconnected, retrying",
318 "<6>connected again after=1m25s",
319 }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
321 require.NotContains(t, logs.String(), whatsappTestNumber)
322 require.NotContains(t, logs.String(), whatsappTestLID)
325 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
326 testCases := []struct {
328 session func(t *testing.T, dir string)
331 name: "stays unlinked without a session file",
332 session: func(_ *testing.T, _ string) {
336 name: "stays unlinked after incomplete pairing",
337 session: func(t *testing.T, dir string) {
338 whatsappTestSession(t, dir, false)
343 for _, tc := range testCases {
344 t.Run(tc.name, func(t *testing.T) {
348 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
349 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
354 func TestWhatsAppRefusesASessionThatOthersCanRead(t *testing.T) {
356 whatsappTestSession(t, dir, true)
357 require.NoError(t, os.Chmod(filepath.Join(dir, whatsappDirName, whatsappSessionFileName), 0o644))
359 _, err := openWhatsApp(t.Context(), dir, time.Now, discardLog(), whatsappNewClient)
360 require.EqualError(t, err, "whatsapp/session.db holds the WhatsApp session, so its mode must be 0600")
363 func TestWhatsAppCommandsAnswerOnlyTheClientThatAsked(t *testing.T) {
364 server := newModelFake(t)
365 telegram := newTelegramFake(t)
366 bot, _ := telegramStartTestBot(t, telegram)
367 socket := daemonStartTestWithBot(t, server, bot)
368 asking := terminalStartTestClient(t, socket)
369 other := terminalStartTestClient(t, socket)
370 asking.WaitFor(t, "[connected to OneChat]")
371 other.WaitFor(t, "[connected to OneChat]")
373 asking.Type("/whatsapp status")
374 asking.WaitFor(t, "WhatsApp is not linked.")
375 asking.Type(" /whatsapp unknown ")
376 asking.WaitFor(t, whatsappUsage)
377 asking.Type("/whatsapp pair 0123")
378 asking.WaitFor(t, "PHONE must be the number of your WhatsApp account")
380 telegram.Add(TelegramUpdate{
382 Message: telegramOwnerMessage("/whatsapp status"),
385 telegramWaitForSent(t, telegram, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.")
386 require.Len(t, telegram.Sent(), 1)
387 require.Equal(t, "[connected to OneChat]\n", other.Output())
388 require.Zero(t, server.Calls())
391 func TestWhatsAppPairsWithACode(t *testing.T) {
393 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
394 server := newModelFake(t)
395 socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
396 asking := terminalStartTestClient(t, socket)
397 other := terminalStartTestClient(t, socket)
398 asking.WaitFor(t, "[connected to OneChat]")
399 other.WaitFor(t, "[connected to OneChat]")
401 asking.Type("/whatsapp pair +1 555-000-1111")
403 items := <-fake.pairings
404 items <- whatsmeow.QRChannelItem{
405 Event: whatsmeow.QRChannelEventCode,
408 asking.WaitFor(t, whatsappPairSteps+"\n"+whatsappTestCode+"\n")
409 require.True(t, fake.Emit(&events.PairSuccess{
410 ID: types.NewADJID(whatsappTestNumber, 0, 1),
411 LID: types.NewADJID(whatsappTestLID, 0, 1),
414 items <- whatsmeow.QRChannelSuccess
416 asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
418 // The library connects again after the link with the context of the
419 // connection, so the end of the pairing must not end it.
420 connections := fake.Connections()
421 require.Len(t, connections, 1)
422 require.Never(t, func() bool {
423 return connections[0].Err() != nil
424 }, 200*time.Millisecond, time.Millisecond)
426 require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
427 require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
428 require.Equal(t, "[connected to OneChat]\n", other.Output())
429 require.Zero(t, server.Calls())
430 require.Equal(t, []string{"WhatsApp is linked."}, whatsappTestLogTexts(t, memory))
431 require.Equal(t, "<6>linked\n", logs.String())
433 info, err := os.Stat(filepath.Join(dir, whatsappDirName))
434 require.NoError(t, err)
435 require.Equal(t, os.FileMode(0o700), info.Mode().Perm())
437 info, err = os.Stat(filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
438 require.NoError(t, err)
439 require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
442 func TestWhatsAppPairingCodeIsATelegramMessageAlone(t *testing.T) {
443 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
444 telegram := newTelegramFake(t)
445 bot, _ := telegramStartTestBot(t, telegram)
446 daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
448 telegram.Add(TelegramUpdate{
450 Message: telegramOwnerMessage("/whatsapp pair 15550001111"),
453 items := <-fake.pairings
454 items <- whatsmeow.QRChannelItem{
455 Event: whatsmeow.QRChannelEventCode,
458 telegramWaitForSent(t, telegram, whatsappPairSteps, whatsappTestCode)
461 func TestWhatsAppPairingEnds(t *testing.T) {
462 testCases := []struct {
464 end func(items chan whatsmeow.QRChannelItem)
468 name: "ends pairing when the code expires",
469 end: func(items chan whatsmeow.QRChannelItem) {
470 items <- whatsmeow.QRChannelTimeout
472 answer: whatsappPairExpired,
475 name: "ends pairing when the library closes it",
476 end: func(items chan whatsmeow.QRChannelItem) {
479 answer: whatsappPairExpired,
482 name: "ends pairing when the library reports failure",
483 end: func(items chan whatsmeow.QRChannelItem) {
484 items <- whatsmeow.QRChannelItem{
485 Event: whatsmeow.QRChannelEventError,
486 Error: errors.New("bad handshake"),
489 answer: "The pairing failed: bad handshake. Run /whatsapp pair PHONE again.",
493 for _, tc := range testCases {
494 t.Run(tc.name, func(t *testing.T) {
495 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
496 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
497 client := terminalStartTestClient(t, socket)
498 client.Type("/whatsapp pair 15550001111")
500 items := <-fake.pairings
501 items <- whatsmeow.QRChannelItem{
502 Event: whatsmeow.QRChannelEventCode,
505 client.WaitFor(t, whatsappTestCode)
507 client.WaitFor(t, tc.answer)
508 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
513 func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) {
514 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
515 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
516 client := terminalStartTestClient(t, socket)
518 client.Type("/whatsapp pair 15550001111")
520 client.Type("/whatsapp pair 15550002222")
522 items := <-fake.pairings
523 items <- whatsmeow.QRChannelItem{
524 Event: whatsmeow.QRChannelEventCode,
527 client.WaitFor(t, whatsappTestCode)
528 require.Equal(t, []string{"15550002222"}, fake.Phones())
529 require.NotContains(t, client.Output(), "did not start the pairing")
532 func TestWhatsAppPairWhenLinked(t *testing.T) {
534 whatsappTestSession(t, dir, true)
536 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
537 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
538 client := terminalStartTestClient(t, socket)
540 client.Type("/whatsapp pair 15550001111")
541 client.WaitFor(t, "WhatsApp is linked already. Run /whatsapp unlink first.")
544 func TestWhatsAppUnlink(t *testing.T) {
545 testCases := []struct {
551 name: "logs out before removing the session",
552 answer: "WhatsApp is unlinked.",
555 name: "removes an offline session without logout",
556 logoutErr: errors.New("not connected"),
557 answer: "WhatsApp is unlinked here, but the phone did not get the logout. In WhatsApp on your phone, open Linked devices and remove the device.",
561 for _, tc := range testCases {
562 t.Run(tc.name, func(t *testing.T) {
564 whatsappTestSession(t, dir, true)
566 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
567 fake.logoutErr = tc.logoutErr
568 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
569 client := terminalStartTestClient(t, socket)
573 client.Type("/whatsapp unlink")
574 client.WaitFor(t, tc.answer)
575 require.Equal(t, 1, fake.Logouts())
576 require.Equal(t, "<6>unlinked\n", logs.String())
577 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
578 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
579 require.Equal(t, []string{"WhatsApp is unlinked."}, whatsappTestLogTexts(t, memory))
581 client.Type("/whatsapp unlink")
582 client.WaitFor(t, "WhatsApp is not linked.\n")
587 func TestWhatsAppLogoutFromThePhone(t *testing.T) {
589 whatsappTestSession(t, dir, true)
591 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
592 telegram := newTelegramFake(t)
593 bot, _ := telegramStartTestBot(t, telegram)
594 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
595 client := terminalStartTestClient(t, socket)
596 client.WaitFor(t, "[connected to OneChat]")
599 require.True(t, fake.Emit(&events.LoggedOut{}))
601 notice := "WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again."
602 client.WaitFor(t, notice)
603 telegramWaitForSent(t, telegram, notice)
604 require.Equal(t, "<3>logged out by the phone, run /whatsapp pair PHONE\n", logs.String())
605 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
606 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
607 require.Equal(t, []string{"WhatsApp was unlinked from the phone."}, whatsappTestLogTexts(t, memory))
610 func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
611 synctest.Test(t, func(t *testing.T) {
612 test := whatsappRetryTest(t)
616 require.True(t, test.fake.Emit(&events.Connected{}))
617 require.True(t, test.fake.Emit(&events.Disconnected{}))
618 require.True(t, test.fake.Emit(&events.Connected{}))
619 time.Sleep(whatsappOutageNoticeDelay + time.Second)
621 require.Zero(t, test.events.Count("notice"))
623 require.True(t, test.fake.Emit(&events.Disconnected{}))
624 require.True(t, test.fake.Emit(&events.Disconnected{}))
625 time.Sleep(whatsappOutageNoticeDelay)
627 require.Equal(t, 1, test.events.Count("notice"))
628 require.Contains(t, test.events.Last("notice"), "OneChat tries again.")
629 require.True(t, test.fake.Emit(&events.Disconnected{}))
630 time.Sleep(whatsappOutageNoticeDelay)
632 require.Equal(t, 1, test.events.Count("notice"))
634 require.True(t, test.fake.Emit(&events.Connected{}))
635 require.Equal(t, 2, test.events.Count("notice"))
636 require.Equal(t, "WhatsApp is connected again.", test.events.Last("notice"))
640 func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) {
642 whatsappTestSession(t, dir, true)
644 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
645 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
646 client := terminalStartTestClient(t, socket)
647 client.WaitFor(t, "[connected to OneChat]")
650 require.True(t, fake.Emit(&events.StreamReplaced{}))
651 client.WaitFor(t, "WhatsApp is disconnected and does not connect again by itself: stream replaced. Restart the daemon to try again.")
652 require.Equal(t, "<3>disconnected for good, restart to retry: stream replaced\n", logs.String())
655 func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
657 whatsappTestSession(t, dir, true)
659 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
660 fake.connectErr = errors.New("no route for " + whatsappTestNumber + "@s.whatsapp.net and " + whatsappTestLID + "@lid")
662 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
665 require.Eventually(t, func() bool {
666 return logs.String() != ""
667 }, 10*time.Second, time.Millisecond)
669 require.Equal(t, "<4>first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
672 func TestWhatsAppPhone(t *testing.T) {
673 testCases := []struct {
679 name: "accepts a number with a plus and dashes",
680 phone: "+1-555-000-1111",
684 name: "refuses a number without a country code",
685 phone: "0115550001111",
689 name: "refuses a number shorter than the minimum",
694 name: "refuses a number longer than the maximum",
695 phone: "1234567890123456",
699 name: "refuses a number with letters",
700 phone: "1555ABC1111",
705 for _, tc := range testCases {
706 t.Run(tc.name, func(t *testing.T) {
707 _, valid := whatsappPhone(tc.phone)
708 require.Equal(t, tc.valid, valid)
713 func TestWhatsAppCommandArgs(t *testing.T) {
714 testCases := []struct {
721 name: "reads and trims the command arguments",
722 line: " /whatsapp pair +1 555 ",
727 name: "accepts the command without arguments",
733 name: "leaves a joined command word as text",
734 line: "/whatsappstatus",
739 name: "leaves a mention of the command as text",
740 line: "what does /whatsapp status do?",
746 for _, tc := range testCases {
747 t.Run(tc.name, func(t *testing.T) {
748 args, found := whatsappCommandArgs(tc.line)
749 require.Equal(t, tc.args, args)
750 require.Equal(t, tc.found, found)
755 func TestSubagentHasNoWhatsAppTools(t *testing.T) {
757 require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
758 require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))