13 "github.com/stretchr/testify/require"
15 "go.mau.fi/whatsmeow/types"
16 "go.mau.fi/whatsmeow/types/events"
19 func TestWhatsAppStatusFollowsTheConnection(t *testing.T) {
21 whatsappTestSession(t, dir, true)
23 clock := newTestClock()
24 whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
25 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
28 require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
30 require.True(t, fake.Emit(&events.Connected{}))
31 require.Equal(t, "WhatsApp is linked and connected since 2026-01-01 12:00.", whatsapp.Status())
33 clock.now = clock.now.Add(20 * time.Minute)
35 require.True(t, fake.Emit(&events.Disconnected{}))
37 clock.now = clock.now.Add(time.Minute)
39 require.True(t, fake.Emit(&events.Disconnected{}))
40 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())
43 func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
44 synctest.Test(t, func(t *testing.T) {
45 test := whatsappRetryTest(t)
46 test.fake.connectErr = errors.New("network unavailable")
50 require.Equal(t, "WhatsApp is linked and disconnected since "+time.Now().Format(whatsappTimeLayout)+".", test.whatsapp.Status())
51 require.Equal(t, "<4>first connect failed, retrying: network unavailable\n", test.logs.String())
55 func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) {
56 synctest.Test(t, func(t *testing.T) {
57 test := whatsappRetryTest(t)
59 test.fake.connectErrors = append(test.fake.connectErrors, errors.New("network unavailable"))
64 require.Len(t, test.fake.Connections(), 1)
65 require.True(t, test.fake.Emit(&events.Disconnected{}))
66 require.True(t, test.fake.Emit(&events.Disconnected{}))
68 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}
70 var outage time.Duration
72 for index, delay := range delays {
73 time.Sleep(delay - time.Nanosecond)
75 require.Len(t, test.fake.Connections(), index+1)
76 time.Sleep(time.Nanosecond)
78 require.Len(t, test.fake.Connections(), index+2)
83 times := test.fake.ConnectionTimes()
84 for index, delay := range delays {
85 require.Equal(t, delay, times[index+1].Sub(times[index]))
88 require.True(t, test.fake.Emit(&events.Connected{}))
89 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
91 time.Sleep(10 * time.Minute)
93 require.Len(t, test.fake.Connections(), 11)
94 require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after="+durationText(outage)+"\n", test.logs.String())
98 func TestWhatsAppFirstConnectionRetriesStopWhenContextEnds(t *testing.T) {
99 synctest.Test(t, func(t *testing.T) {
100 test := whatsappRetryTest(t)
101 test.fake.connectErr = errors.New("network unavailable")
104 time.Sleep(2 * time.Second)
106 require.Len(t, test.fake.Connections(), 2)
109 require.NoError(t, <-test.done)
110 time.Sleep(10 * time.Minute)
112 require.Len(t, test.fake.Connections(), 2)
116 func TestWhatsAppLinkedPairRequestKeepsFirstConnectionRetries(t *testing.T) {
117 synctest.Test(t, func(t *testing.T) {
118 test := whatsappRetryTest(t)
119 test.fake.connectErr = errors.New("network unavailable")
123 answers := make(chan string, 1)
124 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)
153 stop: func(test *WhatsAppRetryTest) {
154 test.whatsapp.Command("unlink", func(_ string) {
160 stop: func(test *WhatsAppRetryTest) {
161 test.fake.Emit(&events.LoggedOut{})
165 name: "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) {
198 require.False(t, test.whatsapp.isLinked())
200 test.fake.mutex.Lock()
202 test.fake.connectErr = nil
203 test.fake.mutex.Unlock()
204 test.whatsapp.Command("pair 15550001111", func(_ string) {
207 items := <-test.fake.pairings
208 items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
211 require.True(t, test.fake.Emit(&events.PairSuccess{
212 ID: types.NewADJID(whatsappTestNumber, 0, 1),
213 LID: types.NewADJID(whatsappTestLID, 0, 1),
216 items <- whatsmeow.QRChannelSuccess
219 require.True(t, test.whatsapp.isLinked())
220 time.Sleep(10 * time.Minute)
222 require.Len(t, test.fake.Connections(), 2)
223 require.Equal(t, []string{whatsappTestNumber}, test.fake.Phones())
227 func TestWhatsAppInboxFailureStopsFirstConnectionRetries(t *testing.T) {
228 synctest.Test(t, func(t *testing.T) {
229 test := whatsappRetryTest(t)
230 test.fake.connectErr = errors.New("network unavailable")
234 failure := errors.New("state write failed")
235 test.whatsapp.inbox.failures <- failure
237 require.ErrorIs(t, <-test.done, failure)
238 time.Sleep(10 * time.Minute)
240 require.Len(t, test.fake.Connections(), 1)
244 func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) {
245 synctest.Test(t, func(t *testing.T) {
246 test := whatsappRetryTest(t)
247 test.fake.connectErr = errors.New("network unavailable")
251 since := time.Now().Format(whatsappTimeLayout)
253 time.Sleep(whatsappNoticeDelay)
255 require.Equal(t, 1, test.events.Count("notice"))
256 require.Equal(t, "WhatsApp is disconnected since "+since+". OneChat tries again.", test.events.Last("notice"))
258 time.Sleep(whatsappNoticeDelay)
260 require.Equal(t, 1, test.events.Count("notice"))
261 test.fake.mutex.Lock()
263 test.fake.connectErr = nil
264 test.fake.mutex.Unlock()
265 time.Sleep(whatsappRetryLimit)
267 require.True(t, test.fake.Emit(&events.Connected{}))
268 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
270 require.Equal(t, 2, test.events.Count("notice"))
271 require.Equal(t, "WhatsApp is connected again.", test.events.Last("notice"))
275 func TestWhatsAppStartingRetriesDoNotDuplicateLibraryRetries(t *testing.T) {
277 whatsappTestSession(t, dir, true)
279 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
280 client, ok := whatsappNewClient(whatsapp.device, whatsapp.handle).(*whatsmeow.Client)
282 require.False(t, client.InitialAutoReconnect)
283 require.True(t, client.EnableAutoReconnect)
285 device := whatsapp.container.NewDevice()
286 client, ok = whatsappNewClient(device, whatsapp.handle).(*whatsmeow.Client)
288 require.True(t, client.InitialAutoReconnect)
291 func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
293 whatsappTestSession(t, dir, true)
295 clock := newTestClock()
296 whatsapp, fake, logs := whatsappStartTest(t, dir, clock)
297 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
300 require.True(t, fake.Emit(&events.Connected{}))
302 clock.now = clock.now.Add(time.Minute)
304 require.True(t, fake.Emit(&events.Disconnected{}))
306 clock.now = clock.now.Add(time.Minute)
308 require.True(t, fake.Emit(&events.Disconnected{}))
310 clock.now = clock.now.Add(25 * time.Second)
312 require.True(t, fake.Emit(&events.Connected{}))
314 require.Equal(t, []string{
316 "<4>disconnected, retrying",
317 "<6>connected again after=1m25s",
318 }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
319 require.NotContains(t, logs.String(), whatsappTestNumber)
320 require.NotContains(t, logs.String(), whatsappTestLID)
323 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
324 testCases := []struct {
326 session func(t *testing.T, dir string)
329 name: "no session file",
330 session: func(_ *testing.T, _ string) {
334 name: "a session of a pairing that did not end",
335 session: func(t *testing.T, dir string) {
336 whatsappTestSession(t, dir, false)
341 for _, tc := range testCases {
342 t.Run(tc.name, func(t *testing.T) {
346 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
347 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
352 func TestWhatsAppRefusesASessionThatOthersCanRead(t *testing.T) {
354 whatsappTestSession(t, dir, true)
355 require.NoError(t, os.Chmod(filepath.Join(dir, whatsappDirName, whatsappSessionFileName), 0o644))
357 _, err := openWhatsApp(t.Context(), dir, time.Now, whatsappDiscardLog(), whatsappNewClient)
358 require.EqualError(t, err, "whatsapp/session.db holds the WhatsApp session, so its mode must be 0600")
361 func TestWhatsAppCommandsAnswerOnlyTheClientThatAsked(t *testing.T) {
362 server := newModelFake(t)
363 telegram := newTelegramFake(t)
364 bot, _ := telegramStartTestBot(t, telegram)
365 socket := daemonStartTestWithBot(t, server, bot)
366 asking := terminalStartTestClient(t, socket)
367 other := terminalStartTestClient(t, socket)
368 asking.WaitFor(t, "[connected to OneChat]")
369 other.WaitFor(t, "[connected to OneChat]")
371 asking.Type("/whatsapp status")
372 asking.WaitFor(t, "WhatsApp is not linked.")
373 asking.Type(" /whatsapp unknown ")
374 asking.WaitFor(t, whatsappUsage)
375 asking.Type("/whatsapp pair 0123")
376 asking.WaitFor(t, "PHONE must be the number of your WhatsApp account")
378 telegram.Add(TelegramUpdate{
380 Message: telegramOwnerMessage("/whatsapp status"),
383 telegramWaitForSent(t, telegram, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.")
384 require.Len(t, telegram.Sent(), 1)
385 require.Equal(t, "[connected to OneChat]\n", other.Output())
386 require.Zero(t, server.Calls())
389 func TestWhatsAppPairsWithACode(t *testing.T) {
391 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
392 server := newModelFake(t)
393 socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
394 asking := terminalStartTestClient(t, socket)
395 other := terminalStartTestClient(t, socket)
396 asking.WaitFor(t, "[connected to OneChat]")
397 other.WaitFor(t, "[connected to OneChat]")
399 asking.Type("/whatsapp pair +1 555-000-1111")
401 items := <-fake.pairings
402 items <- whatsmeow.QRChannelItem{
403 Event: whatsmeow.QRChannelEventCode,
406 asking.WaitFor(t, whatsappPairSteps+"\n"+whatsappTestCode+"\n")
407 require.True(t, fake.Emit(&events.PairSuccess{
408 ID: types.NewADJID(whatsappTestNumber, 0, 1),
409 LID: types.NewADJID(whatsappTestLID, 0, 1),
412 items <- whatsmeow.QRChannelSuccess
414 asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
416 // The library connects again after the link with the context of the
417 // connection, so the end of the pairing must not end it.
418 connections := fake.Connections()
419 require.Len(t, connections, 1)
420 require.Never(t, func() bool {
421 return connections[0].Err() != nil
422 }, 200*time.Millisecond, time.Millisecond)
424 require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
425 require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
426 require.Equal(t, "[connected to OneChat]\n", other.Output())
427 require.Zero(t, server.Calls())
428 require.Equal(t, []string{"WhatsApp is linked."}, whatsappTestLogTexts(t, memory))
429 require.Equal(t, "<6>linked\n", logs.String())
431 info, err := os.Stat(filepath.Join(dir, whatsappDirName))
432 require.NoError(t, err)
433 require.Equal(t, os.FileMode(0o700), info.Mode().Perm())
435 info, err = os.Stat(filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
436 require.NoError(t, err)
437 require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
440 func TestWhatsAppPairingCodeIsATelegramMessageAlone(t *testing.T) {
441 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
442 telegram := newTelegramFake(t)
443 bot, _ := telegramStartTestBot(t, telegram)
444 daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
446 telegram.Add(TelegramUpdate{
448 Message: telegramOwnerMessage("/whatsapp pair 15550001111"),
451 items := <-fake.pairings
452 items <- whatsmeow.QRChannelItem{
453 Event: whatsmeow.QRChannelEventCode,
456 telegramWaitForSent(t, telegram, whatsappPairSteps, whatsappTestCode)
459 func TestWhatsAppPairingEnds(t *testing.T) {
460 testCases := []struct {
462 end func(items chan whatsmeow.QRChannelItem)
466 name: "the code expires",
467 end: func(items chan whatsmeow.QRChannelItem) {
468 items <- whatsmeow.QRChannelTimeout
470 answer: whatsappPairExpired,
473 name: "the library closes the pairing",
474 end: func(items chan whatsmeow.QRChannelItem) {
477 answer: whatsappPairExpired,
480 name: "the pairing fails",
481 end: func(items chan whatsmeow.QRChannelItem) {
482 items <- whatsmeow.QRChannelItem{
483 Event: whatsmeow.QRChannelEventError,
484 Error: errors.New("bad handshake"),
487 answer: "The pairing failed: bad handshake. Run /whatsapp pair PHONE again.",
491 for _, tc := range testCases {
492 t.Run(tc.name, func(t *testing.T) {
493 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
494 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
495 client := terminalStartTestClient(t, socket)
496 client.Type("/whatsapp pair 15550001111")
498 items := <-fake.pairings
499 items <- whatsmeow.QRChannelItem{
500 Event: whatsmeow.QRChannelEventCode,
503 client.WaitFor(t, whatsappTestCode)
505 client.WaitFor(t, tc.answer)
506 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
511 func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) {
512 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
513 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
514 client := terminalStartTestClient(t, socket)
516 client.Type("/whatsapp pair 15550001111")
518 client.Type("/whatsapp pair 15550002222")
520 items := <-fake.pairings
521 items <- whatsmeow.QRChannelItem{
522 Event: whatsmeow.QRChannelEventCode,
525 client.WaitFor(t, whatsappTestCode)
526 require.Equal(t, []string{"15550002222"}, fake.Phones())
527 require.NotContains(t, client.Output(), "did not start the pairing")
530 func TestWhatsAppPairWhenLinked(t *testing.T) {
532 whatsappTestSession(t, dir, true)
534 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
535 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
536 client := terminalStartTestClient(t, socket)
538 client.Type("/whatsapp pair 15550001111")
539 client.WaitFor(t, "WhatsApp is linked already. Run /whatsapp unlink first.")
542 func TestWhatsAppUnlink(t *testing.T) {
543 testCases := []struct {
549 name: "with a logout",
550 answer: "WhatsApp is unlinked.",
553 name: "with no logout",
554 logoutErr: errors.New("not connected"),
555 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.",
559 for _, tc := range testCases {
560 t.Run(tc.name, func(t *testing.T) {
562 whatsappTestSession(t, dir, true)
564 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
565 fake.logoutErr = tc.logoutErr
566 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
567 client := terminalStartTestClient(t, socket)
571 client.Type("/whatsapp unlink")
572 client.WaitFor(t, tc.answer)
573 require.Equal(t, 1, fake.Logouts())
574 require.Equal(t, "<6>unlinked\n", logs.String())
575 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
576 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
577 require.Equal(t, []string{"WhatsApp is unlinked."}, whatsappTestLogTexts(t, memory))
579 client.Type("/whatsapp unlink")
580 client.WaitFor(t, "WhatsApp is not linked.\n")
585 func TestWhatsAppLogoutFromThePhone(t *testing.T) {
587 whatsappTestSession(t, dir, true)
589 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
590 telegram := newTelegramFake(t)
591 bot, _ := telegramStartTestBot(t, telegram)
592 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
593 client := terminalStartTestClient(t, socket)
594 client.WaitFor(t, "[connected to OneChat]")
597 require.True(t, fake.Emit(&events.LoggedOut{}))
599 notice := "WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again."
600 client.WaitFor(t, notice)
601 telegramWaitForSent(t, telegram, notice)
602 require.Equal(t, "<3>logged out by the phone, run /whatsapp pair PHONE\n", logs.String())
603 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
604 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
605 require.Equal(t, []string{"WhatsApp was unlinked from the phone."}, whatsappTestLogTexts(t, memory))
608 func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
610 whatsappTestSession(t, dir, true)
612 clock := newTestClock()
613 whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
614 whatsapp.noticeDelay = 300 * time.Millisecond
615 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
616 client := terminalStartTestClient(t, socket)
617 client.WaitFor(t, "[connected to OneChat]")
620 require.True(t, fake.Emit(&events.Connected{}))
621 require.True(t, fake.Emit(&events.Disconnected{}))
622 require.True(t, fake.Emit(&events.Connected{}))
623 time.Sleep(400 * time.Millisecond)
624 require.Equal(t, "[connected to OneChat]\n", client.Output())
626 clock.now = clock.now.Add(time.Hour)
628 require.True(t, fake.Emit(&events.Disconnected{}))
629 require.True(t, fake.Emit(&events.Disconnected{}))
630 client.WaitFor(t, "WhatsApp is disconnected since 2026-01-01 13:00. OneChat tries again.\n")
631 require.True(t, fake.Emit(&events.Disconnected{}))
632 time.Sleep(400 * time.Millisecond)
634 require.True(t, fake.Emit(&events.Connected{}))
635 client.WaitFor(t, "WhatsApp is connected again.\n")
636 require.Equal(t, 1, strings.Count(client.Output(), "WhatsApp is disconnected since"))
639 func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) {
641 whatsappTestSession(t, dir, true)
643 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
644 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
645 client := terminalStartTestClient(t, socket)
646 client.WaitFor(t, "[connected to OneChat]")
649 require.True(t, fake.Emit(&events.StreamReplaced{}))
650 client.WaitFor(t, "WhatsApp is disconnected and does not connect again by itself: stream replaced. Restart the daemon to try again.")
651 require.Equal(t, "<3>disconnected for good, restart to retry: stream replaced\n", logs.String())
654 func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
656 whatsappTestSession(t, dir, true)
658 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
659 fake.connectErr = errors.New("no route for " + whatsappTestNumber + "@s.whatsapp.net and " + whatsappTestLID + "@lid")
661 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
664 require.Eventually(t, func() bool {
665 return logs.String() != ""
666 }, 10*time.Second, time.Millisecond)
667 require.Equal(t, "<4>first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
670 func TestWhatsAppPhone(t *testing.T) {
671 testCases := []struct {
677 name: "a number with a plus and dashes",
678 phone: "+1-555-000-1111",
682 name: "a number with no country code",
683 phone: "0115550001111",
687 name: "a short number",
692 name: "a long number",
693 phone: "1234567890123456",
697 name: "a number with letters",
698 phone: "1555ABC1111",
703 for _, tc := range testCases {
704 t.Run(tc.name, func(t *testing.T) {
705 _, valid := whatsappPhone(tc.phone)
706 require.Equal(t, tc.valid, valid)
711 func TestWhatsAppCommandArgs(t *testing.T) {
712 testCases := []struct {
719 name: "a command with its arguments",
720 line: " /whatsapp pair +1 555 ",
725 name: "the command alone",
731 name: "a word that starts with the command",
732 line: "/whatsappstatus",
737 name: "a message that names the command",
738 line: "what does /whatsapp status do?",
744 for _, tc := range testCases {
745 t.Run(tc.name, func(t *testing.T) {
746 args, found := whatsappCommandArgs(tc.line)
747 require.Equal(t, tc.args, args)
748 require.Equal(t, tc.found, found)
753 func TestSubagentHasNoWhatsAppTools(t *testing.T) {
755 require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
756 require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))
759 // WhatsAppRetryTest runs WhatsApp without a model or network client call. All
760 // timers and channels belong to its synctest bubble, so backoff uses fake time.
761 type WhatsAppRetryTest struct {
766 events *TurnEventRecorder
767 cancel context.CancelFunc
771 func (r *WhatsAppRetryTest) start(t *testing.T) {
774 ctx, cancel := context.WithCancel(t.Context())
776 r.done = make(chan error, 1)
778 r.done <- r.whatsapp.Run(ctx, r.loop, r.events.Add)
787 func whatsappRetryTest(t *testing.T) *WhatsAppRetryTest {
791 whatsappTestSession(t, dir, true)
793 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
794 whatsapp.clock = time.Now
795 agent, err := openAgent(t.TempDir())
796 require.NoError(t, err)
798 memory, err := openMemory(t.Context(), agent.dir, time.Now, newFakeSummarizer(), discardLog())
799 require.NoError(t, err)
801 require.NoError(t, memory.Close())
804 events := &TurnEventRecorder{}
805 client := newModelClient("https://example.com", ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl"))
806 loop, err := newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, time.Now), client, time.Now, discardLog(), events.Add)
807 require.NoError(t, err)
809 return &WhatsAppRetryTest{