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)
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)
33 require.True(t, fake.Emit(&events.Disconnected{}))
34 clock.now = clock.now.Add(time.Minute)
35 require.True(t, fake.Emit(&events.Disconnected{}))
36 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())
39 func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
40 synctest.Test(t, func(t *testing.T) {
41 test := whatsappRetryTest(t)
42 test.fake.connectErr = errors.New("network unavailable")
46 require.Equal(t, "WhatsApp is linked and disconnected since "+time.Now().Format(whatsappTimeLayout)+".", test.whatsapp.Status())
47 require.Equal(t, "first connect failed, retrying: network unavailable\n", test.logs.String())
51 func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) {
52 synctest.Test(t, func(t *testing.T) {
53 test := whatsappRetryTest(t)
55 test.fake.connectErrors = append(test.fake.connectErrors, errors.New("network unavailable"))
60 require.Len(t, test.fake.Connections(), 1)
61 require.True(t, test.fake.Emit(&events.Disconnected{}))
62 require.True(t, test.fake.Emit(&events.Disconnected{}))
64 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}
65 var outage time.Duration
66 for index, delay := range delays {
67 time.Sleep(delay - time.Nanosecond)
69 require.Len(t, test.fake.Connections(), index+1)
70 time.Sleep(time.Nanosecond)
72 require.Len(t, test.fake.Connections(), index+2)
76 times := test.fake.ConnectionTimes()
77 for index, delay := range delays {
78 require.Equal(t, delay, times[index+1].Sub(times[index]))
81 require.True(t, test.fake.Emit(&events.Connected{}))
82 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
84 time.Sleep(10 * time.Minute)
86 require.Len(t, test.fake.Connections(), 11)
87 require.Equal(t, "first connect failed, retrying: network unavailable\nconnected again after "+durationText(outage)+"\n", test.logs.String())
91 func TestWhatsAppFirstConnectionRetriesStopWhenContextEnds(t *testing.T) {
92 synctest.Test(t, func(t *testing.T) {
93 test := whatsappRetryTest(t)
94 test.fake.connectErr = errors.New("network unavailable")
97 time.Sleep(2 * time.Second)
99 require.Len(t, test.fake.Connections(), 2)
102 require.NoError(t, <-test.done)
103 time.Sleep(10 * time.Minute)
105 require.Len(t, test.fake.Connections(), 2)
109 func TestWhatsAppLinkedPairRequestKeepsFirstConnectionRetries(t *testing.T) {
110 synctest.Test(t, func(t *testing.T) {
111 test := whatsappRetryTest(t)
112 test.fake.connectErr = errors.New("network unavailable")
116 answers := make(chan string, 1)
117 test.whatsapp.Command("pair 15550001111", func(text string) {
120 require.Equal(t, "WhatsApp is linked already. Run /whatsapp unlink first.", <-answers)
122 test.fake.mutex.Lock()
123 test.fake.connectErr = nil
124 test.fake.mutex.Unlock()
125 time.Sleep(whatsappRetryDelay)
127 require.Len(t, test.fake.Connections(), 2)
128 require.True(t, test.fake.Emit(&events.Connected{}))
129 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
130 require.Contains(t, test.whatsapp.Status(), "linked and connected since")
131 time.Sleep(whatsappRetryLimit)
133 require.Len(t, test.fake.Connections(), 2)
134 require.Equal(t, "first connect failed, retrying: network unavailable\nconnected again after 2s\n", test.logs.String())
138 func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
139 testCases := []struct {
141 stop func(test *WhatsAppRetryTest)
145 stop: func(test *WhatsAppRetryTest) {
146 test.whatsapp.Command("unlink", func(text string) {})
151 stop: func(test *WhatsAppRetryTest) {
152 test.fake.Emit(&events.LoggedOut{})
156 name: "permanent disconnect",
157 stop: func(test *WhatsAppRetryTest) {
158 test.fake.Emit(&events.StreamReplaced{})
163 for _, tc := range testCases {
164 t.Run(tc.name, func(t *testing.T) {
165 synctest.Test(t, func(t *testing.T) {
166 test := whatsappRetryTest(t)
167 test.fake.connectErr = errors.New("network unavailable")
172 time.Sleep(10 * time.Minute)
174 require.Len(t, test.fake.Connections(), 1)
180 func TestWhatsAppPairsAfterFirstConnectionRetriesStop(t *testing.T) {
181 synctest.Test(t, func(t *testing.T) {
182 test := whatsappRetryTest(t)
183 test.fake.connectErr = errors.New("network unavailable")
186 test.whatsapp.Command("unlink", func(text string) {})
188 require.False(t, test.whatsapp.isLinked())
190 test.fake.mutex.Lock()
191 test.fake.connectErr = nil
192 test.fake.mutex.Unlock()
193 test.whatsapp.Command("pair 15550001111", func(text string) {})
194 items := <-test.fake.pairings
195 items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
197 require.True(t, test.fake.Emit(&events.PairSuccess{
198 ID: types.NewADJID(whatsappTestNumber, 0, 1),
199 LID: types.NewADJID(whatsappTestLID, 0, 1),
201 items <- whatsmeow.QRChannelSuccess
203 require.True(t, test.whatsapp.isLinked())
204 time.Sleep(10 * time.Minute)
206 require.Len(t, test.fake.Connections(), 2)
207 require.Equal(t, []string{whatsappTestNumber}, test.fake.Phones())
211 func TestWhatsAppInboxFailureStopsFirstConnectionRetries(t *testing.T) {
212 synctest.Test(t, func(t *testing.T) {
213 test := whatsappRetryTest(t)
214 test.fake.connectErr = errors.New("network unavailable")
217 failure := errors.New("state write failed")
218 test.whatsapp.inbox.failures <- failure
219 require.ErrorIs(t, <-test.done, failure)
220 time.Sleep(10 * time.Minute)
222 require.Len(t, test.fake.Connections(), 1)
226 func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) {
227 synctest.Test(t, func(t *testing.T) {
228 test := whatsappRetryTest(t)
229 test.fake.connectErr = errors.New("network unavailable")
232 since := time.Now().Format(whatsappTimeLayout)
233 time.Sleep(whatsappNoticeDelay)
235 require.Equal(t, 1, test.events.Count("notice"))
236 require.Equal(t, "WhatsApp is disconnected since "+since+". OneChat tries again.", test.events.Last("notice"))
238 time.Sleep(whatsappNoticeDelay)
240 require.Equal(t, 1, test.events.Count("notice"))
241 test.fake.mutex.Lock()
242 test.fake.connectErr = nil
243 test.fake.mutex.Unlock()
244 time.Sleep(whatsappRetryLimit)
246 require.True(t, test.fake.Emit(&events.Connected{}))
247 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
249 require.Equal(t, 2, test.events.Count("notice"))
250 require.Equal(t, "WhatsApp is connected again.", test.events.Last("notice"))
254 func TestWhatsAppStartingRetriesDoNotDuplicateLibraryRetries(t *testing.T) {
256 whatsappTestSession(t, dir, true)
257 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
258 client, ok := whatsappNewClient(whatsapp.device, whatsapp.handle).(*whatsmeow.Client)
260 require.False(t, client.InitialAutoReconnect)
261 require.True(t, client.EnableAutoReconnect)
263 device := whatsapp.container.NewDevice()
264 client, ok = whatsappNewClient(device, whatsapp.handle).(*whatsmeow.Client)
266 require.True(t, client.InitialAutoReconnect)
269 func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
271 whatsappTestSession(t, dir, true)
272 clock := newTestClock()
273 whatsapp, fake, logs := whatsappStartTest(t, dir, clock)
274 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
277 require.True(t, fake.Emit(&events.Connected{}))
278 clock.now = clock.now.Add(time.Minute)
279 require.True(t, fake.Emit(&events.Disconnected{}))
280 clock.now = clock.now.Add(time.Minute)
281 require.True(t, fake.Emit(&events.Disconnected{}))
282 clock.now = clock.now.Add(25 * time.Second)
283 require.True(t, fake.Emit(&events.Connected{}))
285 require.Equal(t, []string{
287 "disconnected, retrying",
288 "connected again after 1m25s",
289 }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
290 require.NotContains(t, logs.String(), whatsappTestNumber)
291 require.NotContains(t, logs.String(), whatsappTestLID)
294 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
295 testCases := []struct {
297 session func(t *testing.T, dir string)
300 name: "no session file",
301 session: func(t *testing.T, dir string) {
305 name: "a session of a pairing that did not end",
306 session: func(t *testing.T, dir string) {
307 whatsappTestSession(t, dir, false)
312 for _, tc := range testCases {
313 t.Run(tc.name, func(t *testing.T) {
316 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
317 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
322 func TestWhatsAppRefusesASessionThatOthersCanRead(t *testing.T) {
324 whatsappTestSession(t, dir, true)
325 require.NoError(t, os.Chmod(filepath.Join(dir, whatsappDirName, whatsappSessionFileName), 0o644))
327 _, err := openWhatsApp(t.Context(), dir, time.Now, whatsappDiscardLog(), whatsappNewClient)
328 require.EqualError(t, err, "whatsapp/session.db holds the WhatsApp session, so its mode must be 0600")
331 func TestWhatsAppCommandsAnswerOnlyTheClientThatAsked(t *testing.T) {
332 server := newModelFake(t)
333 telegram := newTelegramFake(t)
334 bot, _ := telegramStartTestBot(t, telegram)
335 socket := daemonStartTestWithBot(t, server, bot)
336 asking := terminalStartTestClient(t, socket)
337 other := terminalStartTestClient(t, socket)
338 asking.WaitFor(t, "[connected to OneChat]")
339 other.WaitFor(t, "[connected to OneChat]")
341 asking.Type("/whatsapp status")
342 asking.WaitFor(t, "WhatsApp is not linked.")
343 asking.Type(" /whatsapp unknown ")
344 asking.WaitFor(t, whatsappUsage)
345 asking.Type("/whatsapp pair 0123")
346 asking.WaitFor(t, "PHONE must be the number of your WhatsApp account")
348 telegram.Add(TelegramUpdate{
350 Message: telegramOwnerMessage("/whatsapp status"),
353 telegramWaitForSent(t, telegram, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.")
354 require.Len(t, telegram.Sent(), 1)
355 require.Equal(t, "[connected to OneChat]\n", other.Output())
356 require.Zero(t, server.Calls())
359 func TestWhatsAppPairsWithACode(t *testing.T) {
361 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
362 server := newModelFake(t)
363 socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
364 asking := terminalStartTestClient(t, socket)
365 other := terminalStartTestClient(t, socket)
366 asking.WaitFor(t, "[connected to OneChat]")
367 other.WaitFor(t, "[connected to OneChat]")
369 asking.Type("/whatsapp pair +1 555-000-1111")
370 items := <-fake.pairings
371 items <- whatsmeow.QRChannelItem{
372 Event: whatsmeow.QRChannelEventCode,
375 asking.WaitFor(t, whatsappPairSteps+"\n"+whatsappTestCode+"\n")
376 require.True(t, fake.Emit(&events.PairSuccess{
377 ID: types.NewADJID(whatsappTestNumber, 0, 1),
378 LID: types.NewADJID(whatsappTestLID, 0, 1),
380 items <- whatsmeow.QRChannelSuccess
381 asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
383 // The library connects again after the link with the context of the
384 // connection, so the end of the pairing must not end it.
385 connections := fake.Connections()
386 require.Len(t, connections, 1)
387 require.Never(t, func() bool {
388 return connections[0].Err() != nil
389 }, 200*time.Millisecond, time.Millisecond)
391 require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
392 require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
393 require.Equal(t, "[connected to OneChat]\n", other.Output())
394 require.Zero(t, server.Calls())
395 require.Equal(t, []string{"WhatsApp is linked."}, whatsappTestLogTexts(t, memory))
397 info, err := os.Stat(filepath.Join(dir, whatsappDirName))
398 require.NoError(t, err)
399 require.Equal(t, os.FileMode(0o700), info.Mode().Perm())
401 info, err = os.Stat(filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
402 require.NoError(t, err)
403 require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
406 func TestWhatsAppPairingCodeIsATelegramMessageAlone(t *testing.T) {
407 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
408 telegram := newTelegramFake(t)
409 bot, _ := telegramStartTestBot(t, telegram)
410 daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
412 telegram.Add(TelegramUpdate{
414 Message: telegramOwnerMessage("/whatsapp pair 15550001111"),
417 items := <-fake.pairings
418 items <- whatsmeow.QRChannelItem{
419 Event: whatsmeow.QRChannelEventCode,
422 telegramWaitForSent(t, telegram, whatsappPairSteps, whatsappTestCode)
425 func TestWhatsAppPairingEnds(t *testing.T) {
426 testCases := []struct {
428 end func(items chan whatsmeow.QRChannelItem)
432 name: "the code expires",
433 end: func(items chan whatsmeow.QRChannelItem) {
434 items <- whatsmeow.QRChannelTimeout
436 answer: whatsappPairExpired,
439 name: "the library closes the pairing",
440 end: func(items chan whatsmeow.QRChannelItem) {
443 answer: whatsappPairExpired,
446 name: "the pairing fails",
447 end: func(items chan whatsmeow.QRChannelItem) {
448 items <- whatsmeow.QRChannelItem{
449 Event: whatsmeow.QRChannelEventError,
450 Error: errors.New("bad handshake"),
453 answer: "The pairing failed: bad handshake. Run /whatsapp pair PHONE again.",
457 for _, tc := range testCases {
458 t.Run(tc.name, func(t *testing.T) {
459 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
460 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
461 client := terminalStartTestClient(t, socket)
462 client.Type("/whatsapp pair 15550001111")
464 items := <-fake.pairings
465 items <- whatsmeow.QRChannelItem{
466 Event: whatsmeow.QRChannelEventCode,
469 client.WaitFor(t, whatsappTestCode)
471 client.WaitFor(t, tc.answer)
472 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
477 func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) {
478 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
479 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
480 client := terminalStartTestClient(t, socket)
482 client.Type("/whatsapp pair 15550001111")
484 client.Type("/whatsapp pair 15550002222")
485 items := <-fake.pairings
486 items <- whatsmeow.QRChannelItem{
487 Event: whatsmeow.QRChannelEventCode,
490 client.WaitFor(t, whatsappTestCode)
491 require.Equal(t, []string{"15550002222"}, fake.Phones())
492 require.NotContains(t, client.Output(), "did not start the pairing")
495 func TestWhatsAppPairWhenLinked(t *testing.T) {
497 whatsappTestSession(t, dir, true)
498 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
499 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
500 client := terminalStartTestClient(t, socket)
502 client.Type("/whatsapp pair 15550001111")
503 client.WaitFor(t, "WhatsApp is linked already. Run /whatsapp unlink first.")
506 func TestWhatsAppUnlink(t *testing.T) {
507 testCases := []struct {
513 name: "with a logout",
514 answer: "WhatsApp is unlinked.",
517 name: "with no logout",
518 logoutErr: errors.New("not connected"),
519 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.",
523 for _, tc := range testCases {
524 t.Run(tc.name, func(t *testing.T) {
526 whatsappTestSession(t, dir, true)
527 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
528 fake.logoutErr = tc.logoutErr
529 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
530 client := terminalStartTestClient(t, socket)
533 client.Type("/whatsapp unlink")
534 client.WaitFor(t, tc.answer)
535 require.Equal(t, 1, fake.Logouts())
536 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
537 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
538 require.Equal(t, []string{"WhatsApp is unlinked."}, whatsappTestLogTexts(t, memory))
540 client.Type("/whatsapp unlink")
541 client.WaitFor(t, "WhatsApp is not linked.\n")
546 func TestWhatsAppLogoutFromThePhone(t *testing.T) {
548 whatsappTestSession(t, dir, true)
549 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
550 telegram := newTelegramFake(t)
551 bot, _ := telegramStartTestBot(t, telegram)
552 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
553 client := terminalStartTestClient(t, socket)
554 client.WaitFor(t, "[connected to OneChat]")
557 require.True(t, fake.Emit(&events.LoggedOut{}))
558 notice := "WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again."
559 client.WaitFor(t, notice)
560 telegramWaitForSent(t, telegram, notice)
561 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
562 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
563 require.Equal(t, []string{"WhatsApp was unlinked from the phone."}, whatsappTestLogTexts(t, memory))
566 func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
568 whatsappTestSession(t, dir, true)
569 clock := newTestClock()
570 whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
571 whatsapp.noticeDelay = 300 * time.Millisecond
572 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
573 client := terminalStartTestClient(t, socket)
574 client.WaitFor(t, "[connected to OneChat]")
577 require.True(t, fake.Emit(&events.Connected{}))
578 require.True(t, fake.Emit(&events.Disconnected{}))
579 require.True(t, fake.Emit(&events.Connected{}))
580 time.Sleep(400 * time.Millisecond)
581 require.Equal(t, "[connected to OneChat]\n", client.Output())
583 clock.now = clock.now.Add(time.Hour)
584 require.True(t, fake.Emit(&events.Disconnected{}))
585 require.True(t, fake.Emit(&events.Disconnected{}))
586 client.WaitFor(t, "WhatsApp is disconnected since 2026-01-01 13:00. OneChat tries again.\n")
587 require.True(t, fake.Emit(&events.Disconnected{}))
588 time.Sleep(400 * time.Millisecond)
590 require.True(t, fake.Emit(&events.Connected{}))
591 client.WaitFor(t, "WhatsApp is connected again.\n")
592 require.Equal(t, 1, strings.Count(client.Output(), "WhatsApp is disconnected since"))
595 func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) {
597 whatsappTestSession(t, dir, true)
598 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
599 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
600 client := terminalStartTestClient(t, socket)
601 client.WaitFor(t, "[connected to OneChat]")
604 require.True(t, fake.Emit(&events.StreamReplaced{}))
605 client.WaitFor(t, "WhatsApp is disconnected and does not connect again by itself: stream replaced. Restart the daemon to try again.")
608 func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
610 whatsappTestSession(t, dir, true)
611 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
612 fake.connectErr = errors.New("no route for " + whatsappTestNumber + "@s.whatsapp.net and " + whatsappTestLID + "@lid")
613 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
616 require.Eventually(t, func() bool {
617 return logs.String() != ""
618 }, 10*time.Second, time.Millisecond)
619 require.Equal(t, "first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
622 func TestWhatsAppPhone(t *testing.T) {
623 testCases := []struct {
629 name: "a number with a plus and dashes",
630 phone: "+1-555-000-1111",
634 name: "a number with no country code",
635 phone: "0115550001111",
639 name: "a short number",
644 name: "a long number",
645 phone: "1234567890123456",
649 name: "a number with letters",
650 phone: "1555ABC1111",
655 for _, tc := range testCases {
656 t.Run(tc.name, func(t *testing.T) {
657 _, valid := whatsappPhone(tc.phone)
658 require.Equal(t, tc.valid, valid)
663 func TestWhatsAppCommandArgs(t *testing.T) {
664 testCases := []struct {
671 name: "a command with its arguments",
672 line: " /whatsapp pair +1 555 ",
677 name: "the command alone",
683 name: "a word that starts with the command",
684 line: "/whatsappstatus",
689 name: "a message that names the command",
690 line: "what does /whatsapp status do?",
696 for _, tc := range testCases {
697 t.Run(tc.name, func(t *testing.T) {
698 args, found := whatsappCommandArgs(tc.line)
699 require.Equal(t, tc.args, args)
700 require.Equal(t, tc.found, found)
705 func TestSubagentHasNoWhatsAppTools(t *testing.T) {
707 require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
708 require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))
711 // WhatsAppRetryTest runs WhatsApp without a model or network client call. All
712 // timers and channels belong to its synctest bubble, so backoff uses fake time.
713 type WhatsAppRetryTest struct {
718 events *TurnEventRecorder
719 cancel context.CancelFunc
723 func (r *WhatsAppRetryTest) start(t *testing.T) {
726 ctx, cancel := context.WithCancel(t.Context())
728 r.done = make(chan error, 1)
730 r.done <- r.whatsapp.Run(ctx, r.loop, r.events.Add)
739 func whatsappRetryTest(t *testing.T) *WhatsAppRetryTest {
743 whatsappTestSession(t, dir, true)
744 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
745 whatsapp.clock = time.Now
746 agent, err := openAgent(t.TempDir())
747 require.NoError(t, err)
748 memory, err := openMemory(t.Context(), agent.dir, time.Now, newFakeSummarizer(), discardLog())
749 require.NoError(t, err)
751 require.NoError(t, memory.Close())
754 events := &TurnEventRecorder{}
755 client := newModelClient("https://example.com", ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl"))
756 loop, err := newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, time.Now), client, time.Now, discardLog(), events.Add)
757 require.NoError(t, err)
759 return &WhatsAppRetryTest{