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 TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
110 testCases := []struct {
112 stop func(test *WhatsAppRetryTest)
116 stop: func(test *WhatsAppRetryTest) {
117 test.whatsapp.Command("unlink", func(text string) {})
122 stop: func(test *WhatsAppRetryTest) {
123 test.fake.Emit(&events.LoggedOut{})
127 name: "pair command",
128 stop: func(test *WhatsAppRetryTest) {
129 test.whatsapp.Command("pair 15550001111", func(text string) {})
133 name: "permanent disconnect",
134 stop: func(test *WhatsAppRetryTest) {
135 test.fake.Emit(&events.StreamReplaced{})
140 for _, tc := range testCases {
141 t.Run(tc.name, func(t *testing.T) {
142 synctest.Test(t, func(t *testing.T) {
143 test := whatsappRetryTest(t)
144 test.fake.connectErr = errors.New("network unavailable")
149 time.Sleep(10 * time.Minute)
151 require.Len(t, test.fake.Connections(), 1)
157 func TestWhatsAppPairsAfterFirstConnectionRetriesStop(t *testing.T) {
158 synctest.Test(t, func(t *testing.T) {
159 test := whatsappRetryTest(t)
160 test.fake.connectErr = errors.New("network unavailable")
163 test.whatsapp.Command("unlink", func(text string) {})
165 require.False(t, test.whatsapp.isLinked())
167 test.fake.mutex.Lock()
168 test.fake.connectErr = nil
169 test.fake.mutex.Unlock()
170 test.whatsapp.Command("pair 15550001111", func(text string) {})
171 items := <-test.fake.pairings
172 items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
174 require.True(t, test.fake.Emit(&events.PairSuccess{
175 ID: types.NewADJID(whatsappTestNumber, 0, 1),
176 LID: types.NewADJID(whatsappTestLID, 0, 1),
178 items <- whatsmeow.QRChannelSuccess
180 require.True(t, test.whatsapp.isLinked())
181 time.Sleep(10 * time.Minute)
183 require.Len(t, test.fake.Connections(), 2)
184 require.Equal(t, []string{whatsappTestNumber}, test.fake.Phones())
188 func TestWhatsAppInboxFailureStopsFirstConnectionRetries(t *testing.T) {
189 synctest.Test(t, func(t *testing.T) {
190 test := whatsappRetryTest(t)
191 test.fake.connectErr = errors.New("network unavailable")
194 failure := errors.New("state write failed")
195 test.whatsapp.inbox.failures <- failure
196 require.ErrorIs(t, <-test.done, failure)
197 time.Sleep(10 * time.Minute)
199 require.Len(t, test.fake.Connections(), 1)
203 func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) {
204 synctest.Test(t, func(t *testing.T) {
205 test := whatsappRetryTest(t)
206 test.fake.connectErr = errors.New("network unavailable")
209 since := time.Now().Format(whatsappTimeLayout)
210 time.Sleep(whatsappNoticeDelay)
212 require.Equal(t, 1, test.events.Count("notice"))
213 require.Equal(t, "WhatsApp is disconnected since "+since+". OneChat tries again.", test.events.Last("notice"))
215 time.Sleep(whatsappNoticeDelay)
217 require.Equal(t, 1, test.events.Count("notice"))
218 test.fake.mutex.Lock()
219 test.fake.connectErr = nil
220 test.fake.mutex.Unlock()
221 time.Sleep(whatsappRetryLimit)
223 require.True(t, test.fake.Emit(&events.Connected{}))
224 require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
226 require.Equal(t, 2, test.events.Count("notice"))
227 require.Equal(t, "WhatsApp is connected again.", test.events.Last("notice"))
231 func TestWhatsAppStartingRetriesDoNotDuplicateLibraryRetries(t *testing.T) {
233 whatsappTestSession(t, dir, true)
234 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
235 client, ok := whatsappNewClient(whatsapp.device, whatsapp.handle).(*whatsmeow.Client)
237 require.False(t, client.InitialAutoReconnect)
238 require.True(t, client.EnableAutoReconnect)
240 device := whatsapp.container.NewDevice()
241 client, ok = whatsappNewClient(device, whatsapp.handle).(*whatsmeow.Client)
243 require.True(t, client.InitialAutoReconnect)
246 func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
248 whatsappTestSession(t, dir, true)
249 clock := newTestClock()
250 whatsapp, fake, logs := whatsappStartTest(t, dir, clock)
251 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
254 require.True(t, fake.Emit(&events.Connected{}))
255 clock.now = clock.now.Add(time.Minute)
256 require.True(t, fake.Emit(&events.Disconnected{}))
257 clock.now = clock.now.Add(time.Minute)
258 require.True(t, fake.Emit(&events.Disconnected{}))
259 clock.now = clock.now.Add(25 * time.Second)
260 require.True(t, fake.Emit(&events.Connected{}))
262 require.Equal(t, []string{
264 "disconnected, retrying",
265 "connected again after 1m25s",
266 }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
267 require.NotContains(t, logs.String(), whatsappTestNumber)
268 require.NotContains(t, logs.String(), whatsappTestLID)
271 func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
272 testCases := []struct {
274 session func(t *testing.T, dir string)
277 name: "no session file",
278 session: func(t *testing.T, dir string) {
282 name: "a session of a pairing that did not end",
283 session: func(t *testing.T, dir string) {
284 whatsappTestSession(t, dir, false)
289 for _, tc := range testCases {
290 t.Run(tc.name, func(t *testing.T) {
293 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
294 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
299 func TestWhatsAppRefusesASessionThatOthersCanRead(t *testing.T) {
301 whatsappTestSession(t, dir, true)
302 require.NoError(t, os.Chmod(filepath.Join(dir, whatsappDirName, whatsappSessionFileName), 0o644))
304 _, err := openWhatsApp(t.Context(), dir, time.Now, whatsappDiscardLog(), whatsappNewClient)
305 require.EqualError(t, err, "whatsapp/session.db holds the WhatsApp session, so its mode must be 0600")
308 func TestWhatsAppCommandsAnswerOnlyTheClientThatAsked(t *testing.T) {
309 server := newModelFake(t)
310 telegram := newTelegramFake(t)
311 bot, _ := telegramStartTestBot(t, telegram)
312 socket := daemonStartTestWithBot(t, server, bot)
313 asking := terminalStartTestClient(t, socket)
314 other := terminalStartTestClient(t, socket)
315 asking.WaitFor(t, "[connected to OneChat]")
316 other.WaitFor(t, "[connected to OneChat]")
318 asking.Type("/whatsapp status")
319 asking.WaitFor(t, "WhatsApp is not linked.")
320 asking.Type(" /whatsapp unknown ")
321 asking.WaitFor(t, whatsappUsage)
322 asking.Type("/whatsapp pair 0123")
323 asking.WaitFor(t, "PHONE must be the number of your WhatsApp account")
325 telegram.Add(TelegramUpdate{
327 Message: telegramOwnerMessage("/whatsapp status"),
330 telegramWaitForSent(t, telegram, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.")
331 require.Len(t, telegram.Sent(), 1)
332 require.Equal(t, "[connected to OneChat]\n", other.Output())
333 require.Zero(t, server.Calls())
336 func TestWhatsAppPairsWithACode(t *testing.T) {
338 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
339 server := newModelFake(t)
340 socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
341 asking := terminalStartTestClient(t, socket)
342 other := terminalStartTestClient(t, socket)
343 asking.WaitFor(t, "[connected to OneChat]")
344 other.WaitFor(t, "[connected to OneChat]")
346 asking.Type("/whatsapp pair +1 555-000-1111")
347 items := <-fake.pairings
348 items <- whatsmeow.QRChannelItem{
349 Event: whatsmeow.QRChannelEventCode,
352 asking.WaitFor(t, whatsappPairSteps+"\n"+whatsappTestCode+"\n")
353 require.True(t, fake.Emit(&events.PairSuccess{
354 ID: types.NewADJID(whatsappTestNumber, 0, 1),
355 LID: types.NewADJID(whatsappTestLID, 0, 1),
357 items <- whatsmeow.QRChannelSuccess
358 asking.WaitFor(t, "WhatsApp is linked to +15550001111.")
360 // The library connects again after the link with the context of the
361 // connection, so the end of the pairing must not end it.
362 connections := fake.Connections()
363 require.Len(t, connections, 1)
364 require.Never(t, func() bool {
365 return connections[0].Err() != nil
366 }, 200*time.Millisecond, time.Millisecond)
368 require.Equal(t, []string{whatsappTestNumber}, fake.Phones())
369 require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status())
370 require.Equal(t, "[connected to OneChat]\n", other.Output())
371 require.Zero(t, server.Calls())
372 require.Equal(t, []string{"WhatsApp is linked."}, whatsappTestLogTexts(t, memory))
374 info, err := os.Stat(filepath.Join(dir, whatsappDirName))
375 require.NoError(t, err)
376 require.Equal(t, os.FileMode(0o700), info.Mode().Perm())
378 info, err = os.Stat(filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
379 require.NoError(t, err)
380 require.Equal(t, os.FileMode(0o600), info.Mode().Perm())
383 func TestWhatsAppPairingCodeIsATelegramMessageAlone(t *testing.T) {
384 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
385 telegram := newTelegramFake(t)
386 bot, _ := telegramStartTestBot(t, telegram)
387 daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
389 telegram.Add(TelegramUpdate{
391 Message: telegramOwnerMessage("/whatsapp pair 15550001111"),
394 items := <-fake.pairings
395 items <- whatsmeow.QRChannelItem{
396 Event: whatsmeow.QRChannelEventCode,
399 telegramWaitForSent(t, telegram, whatsappPairSteps, whatsappTestCode)
402 func TestWhatsAppPairingEnds(t *testing.T) {
403 testCases := []struct {
405 end func(items chan whatsmeow.QRChannelItem)
409 name: "the code expires",
410 end: func(items chan whatsmeow.QRChannelItem) {
411 items <- whatsmeow.QRChannelTimeout
413 answer: whatsappPairExpired,
416 name: "the library closes the pairing",
417 end: func(items chan whatsmeow.QRChannelItem) {
420 answer: whatsappPairExpired,
423 name: "the pairing fails",
424 end: func(items chan whatsmeow.QRChannelItem) {
425 items <- whatsmeow.QRChannelItem{
426 Event: whatsmeow.QRChannelEventError,
427 Error: errors.New("bad handshake"),
430 answer: "The pairing failed: bad handshake. Run /whatsapp pair PHONE again.",
434 for _, tc := range testCases {
435 t.Run(tc.name, func(t *testing.T) {
436 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
437 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
438 client := terminalStartTestClient(t, socket)
439 client.Type("/whatsapp pair 15550001111")
441 items := <-fake.pairings
442 items <- whatsmeow.QRChannelItem{
443 Event: whatsmeow.QRChannelEventCode,
446 client.WaitFor(t, whatsappTestCode)
448 client.WaitFor(t, tc.answer)
449 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
454 func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) {
455 whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock())
456 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
457 client := terminalStartTestClient(t, socket)
459 client.Type("/whatsapp pair 15550001111")
461 client.Type("/whatsapp pair 15550002222")
462 items := <-fake.pairings
463 items <- whatsmeow.QRChannelItem{
464 Event: whatsmeow.QRChannelEventCode,
467 client.WaitFor(t, whatsappTestCode)
468 require.Equal(t, []string{"15550002222"}, fake.Phones())
469 require.NotContains(t, client.Output(), "did not start the pairing")
472 func TestWhatsAppPairWhenLinked(t *testing.T) {
474 whatsappTestSession(t, dir, true)
475 whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
476 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
477 client := terminalStartTestClient(t, socket)
479 client.Type("/whatsapp pair 15550001111")
480 client.WaitFor(t, "WhatsApp is linked already. Run /whatsapp unlink first.")
483 func TestWhatsAppUnlink(t *testing.T) {
484 testCases := []struct {
490 name: "with a logout",
491 answer: "WhatsApp is unlinked.",
494 name: "with no logout",
495 logoutErr: errors.New("not connected"),
496 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.",
500 for _, tc := range testCases {
501 t.Run(tc.name, func(t *testing.T) {
503 whatsappTestSession(t, dir, true)
504 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
505 fake.logoutErr = tc.logoutErr
506 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
507 client := terminalStartTestClient(t, socket)
510 client.Type("/whatsapp unlink")
511 client.WaitFor(t, tc.answer)
512 require.Equal(t, 1, fake.Logouts())
513 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
514 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
515 require.Equal(t, []string{"WhatsApp is unlinked."}, whatsappTestLogTexts(t, memory))
517 client.Type("/whatsapp unlink")
518 client.WaitFor(t, "WhatsApp is not linked.\n")
523 func TestWhatsAppLogoutFromThePhone(t *testing.T) {
525 whatsappTestSession(t, dir, true)
526 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
527 telegram := newTelegramFake(t)
528 bot, _ := telegramStartTestBot(t, telegram)
529 socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
530 client := terminalStartTestClient(t, socket)
531 client.WaitFor(t, "[connected to OneChat]")
534 require.True(t, fake.Emit(&events.LoggedOut{}))
535 notice := "WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again."
536 client.WaitFor(t, notice)
537 telegramWaitForSent(t, telegram, notice)
538 require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
539 require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
540 require.Equal(t, []string{"WhatsApp was unlinked from the phone."}, whatsappTestLogTexts(t, memory))
543 func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
545 whatsappTestSession(t, dir, true)
546 clock := newTestClock()
547 whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
548 whatsapp.noticeDelay = 300 * time.Millisecond
549 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
550 client := terminalStartTestClient(t, socket)
551 client.WaitFor(t, "[connected to OneChat]")
554 require.True(t, fake.Emit(&events.Connected{}))
555 require.True(t, fake.Emit(&events.Disconnected{}))
556 require.True(t, fake.Emit(&events.Connected{}))
557 time.Sleep(400 * time.Millisecond)
558 require.Equal(t, "[connected to OneChat]\n", client.Output())
560 clock.now = clock.now.Add(time.Hour)
561 require.True(t, fake.Emit(&events.Disconnected{}))
562 require.True(t, fake.Emit(&events.Disconnected{}))
563 client.WaitFor(t, "WhatsApp is disconnected since 2026-01-01 13:00. OneChat tries again.\n")
564 require.True(t, fake.Emit(&events.Disconnected{}))
565 time.Sleep(400 * time.Millisecond)
567 require.True(t, fake.Emit(&events.Connected{}))
568 client.WaitFor(t, "WhatsApp is connected again.\n")
569 require.Equal(t, 1, strings.Count(client.Output(), "WhatsApp is disconnected since"))
572 func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) {
574 whatsappTestSession(t, dir, true)
575 whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
576 socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
577 client := terminalStartTestClient(t, socket)
578 client.WaitFor(t, "[connected to OneChat]")
581 require.True(t, fake.Emit(&events.StreamReplaced{}))
582 client.WaitFor(t, "WhatsApp is disconnected and does not connect again by itself: stream replaced. Restart the daemon to try again.")
585 func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
587 whatsappTestSession(t, dir, true)
588 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
589 fake.connectErr = errors.New("no route for " + whatsappTestNumber + "@s.whatsapp.net and " + whatsappTestLID + "@lid")
590 daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
593 require.Eventually(t, func() bool {
594 return logs.String() != ""
595 }, 10*time.Second, time.Millisecond)
596 require.Equal(t, "first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
599 func TestWhatsAppPhone(t *testing.T) {
600 testCases := []struct {
606 name: "a number with a plus and dashes",
607 phone: "+1-555-000-1111",
611 name: "a number with no country code",
612 phone: "0115550001111",
616 name: "a short number",
621 name: "a long number",
622 phone: "1234567890123456",
626 name: "a number with letters",
627 phone: "1555ABC1111",
632 for _, tc := range testCases {
633 t.Run(tc.name, func(t *testing.T) {
634 _, valid := whatsappPhone(tc.phone)
635 require.Equal(t, tc.valid, valid)
640 func TestWhatsAppCommandArgs(t *testing.T) {
641 testCases := []struct {
648 name: "a command with its arguments",
649 line: " /whatsapp pair +1 555 ",
654 name: "the command alone",
660 name: "a word that starts with the command",
661 line: "/whatsappstatus",
666 name: "a message that names the command",
667 line: "what does /whatsapp status do?",
673 for _, tc := range testCases {
674 t.Run(tc.name, func(t *testing.T) {
675 args, found := whatsappCommandArgs(tc.line)
676 require.Equal(t, tc.args, args)
677 require.Equal(t, tc.found, found)
682 func TestSubagentHasNoWhatsAppTools(t *testing.T) {
684 require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`))
685 require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`))
688 // WhatsAppRetryTest runs WhatsApp without a model or network client call. All
689 // timers and channels belong to its synctest bubble, so backoff uses fake time.
690 type WhatsAppRetryTest struct {
695 events *TurnEventRecorder
696 cancel context.CancelFunc
700 func (r *WhatsAppRetryTest) start(t *testing.T) {
703 ctx, cancel := context.WithCancel(t.Context())
705 r.done = make(chan error, 1)
707 r.done <- r.whatsapp.Run(ctx, r.loop, r.events.Add)
716 func whatsappRetryTest(t *testing.T) *WhatsAppRetryTest {
720 whatsappTestSession(t, dir, true)
721 whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
722 whatsapp.clock = time.Now
723 agent, err := openAgent(t.TempDir())
724 require.NoError(t, err)
725 memory, err := openMemory(t.Context(), agent.dir, time.Now, newFakeSummarizer(), discardLog())
726 require.NoError(t, err)
728 require.NoError(t, memory.Close())
731 events := &TurnEventRecorder{}
732 client := newModelClient("https://example.com", ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl"))
733 loop, err := newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, time.Now), client, time.Now, discardLog(), events.Add)
734 require.NoError(t, err)
736 return &WhatsAppRetryTest{