+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+
+ require.Equal(t, "WhatsApp is linked and disconnected since "+time.Now().Format(whatsappTimeLayout)+".", test.whatsapp.Status())
+ require.Equal(t, "<4>first connect failed, retrying: network unavailable\n", test.logs.String())
+ })
+}
+
+func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ for range 10 {
+ test.fake.connectErrors = append(test.fake.connectErrors, errors.New("network unavailable"))
+ }
+
+ test.start(t)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 1)
+ require.True(t, test.fake.Emit(&events.Disconnected{}))
+ require.True(t, test.fake.Emit(&events.Disconnected{}))
+
+ 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}
+
+ var outage time.Duration
+
+ for index, delay := range delays {
+ time.Sleep(delay - time.Nanosecond)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), index+1)
+ time.Sleep(time.Nanosecond)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), index+2)
+
+ outage += delay
+ }
+
+ times := test.fake.ConnectionTimes()
+ for index, delay := range delays {
+ require.Equal(t, delay, times[index+1].Sub(times[index]))
+ }
+
+ require.True(t, test.fake.Emit(&events.Connected{}))
+ require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
+ synctest.Wait()
+ time.Sleep(10 * time.Minute)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 11)
+ require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after="+durationText(outage)+"\n", test.logs.String())
+ })
+}
+
+func TestWhatsAppFirstConnectionRetriesStopWhenContextEnds(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+ time.Sleep(2 * time.Second)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 2)
+
+ test.cancel()
+ require.NoError(t, <-test.done)
+ time.Sleep(10 * time.Minute)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 2)
+ })
+}
+
+func TestWhatsAppLinkedPairRequestKeepsFirstConnectionRetries(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+
+ answers := make(chan string, 1)
+ test.whatsapp.Command("pair 15550001111", func(text string) {
+ answers <- text
+ })
+ require.Equal(t, "WhatsApp is linked already. Run /whatsapp unlink first.", <-answers)
+ synctest.Wait()
+ test.fake.mutex.Lock()
+
+ test.fake.connectErr = nil
+ test.fake.mutex.Unlock()
+ time.Sleep(whatsappRetryDelay)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 2)
+ require.True(t, test.fake.Emit(&events.Connected{}))
+ require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
+ require.Contains(t, test.whatsapp.Status(), "linked and connected since")
+ time.Sleep(whatsappRetryLimit)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 2)
+ require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after=2s\n", test.logs.String())
+ })
+}
+
+func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
+ testCases := []struct {
+ name string
+ stop func(test *WhatsAppRetryTest)
+ }{
+ {
+ name: "unlink",
+ stop: func(test *WhatsAppRetryTest) {
+ test.whatsapp.Command("unlink", func(_ string) {
+ })
+ },
+ },
+ {
+ name: "logout",
+ stop: func(test *WhatsAppRetryTest) {
+ test.fake.Emit(&events.LoggedOut{})
+ },
+ },
+ {
+ name: "permanent disconnect",
+ stop: func(test *WhatsAppRetryTest) {
+ test.fake.Emit(&events.StreamReplaced{})
+ },
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+ tc.stop(test)
+ synctest.Wait()
+ time.Sleep(10 * time.Minute)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 1)
+ })
+ })
+ }
+}
+
+func TestWhatsAppPairsAfterFirstConnectionRetriesStop(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+ test.whatsapp.Command("unlink", func(_ string) {
+ })
+ synctest.Wait()
+ require.False(t, test.whatsapp.isLinked())
+
+ test.fake.mutex.Lock()
+
+ test.fake.connectErr = nil
+ test.fake.mutex.Unlock()
+ test.whatsapp.Command("pair 15550001111", func(_ string) {
+ })
+
+ items := <-test.fake.pairings
+ items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
+
+ synctest.Wait()
+ require.True(t, test.fake.Emit(&events.PairSuccess{
+ ID: types.NewADJID(whatsappTestNumber, 0, 1),
+ LID: types.NewADJID(whatsappTestLID, 0, 1),
+ }))
+
+ items <- whatsmeow.QRChannelSuccess
+
+ synctest.Wait()
+ require.True(t, test.whatsapp.isLinked())
+ time.Sleep(10 * time.Minute)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 2)
+ require.Equal(t, []string{whatsappTestNumber}, test.fake.Phones())
+ })
+}
+
+func TestWhatsAppInboxFailureStopsFirstConnectionRetries(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+
+ failure := errors.New("state write failed")
+ test.whatsapp.inbox.failures <- failure
+
+ require.ErrorIs(t, <-test.done, failure)
+ time.Sleep(10 * time.Minute)
+ synctest.Wait()
+ require.Len(t, test.fake.Connections(), 1)
+ })
+}
+
+func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) {
+ synctest.Test(t, func(t *testing.T) {
+ test := whatsappRetryTest(t)
+ test.fake.connectErr = errors.New("network unavailable")
+ test.start(t)
+ synctest.Wait()
+
+ since := time.Now().Format(whatsappTimeLayout)
+
+ time.Sleep(whatsappOutageNoticeDelay)
+ synctest.Wait()
+ require.Equal(t, 1, test.events.Count("notice"))
+ require.Equal(t, "WhatsApp is disconnected since "+since+". OneChat tries again.", test.events.Last("notice"))
+
+ time.Sleep(whatsappOutageNoticeDelay)
+ synctest.Wait()
+ require.Equal(t, 1, test.events.Count("notice"))
+ test.fake.mutex.Lock()
+
+ test.fake.connectErr = nil
+ test.fake.mutex.Unlock()
+ time.Sleep(whatsappRetryLimit)
+ synctest.Wait()
+ require.True(t, test.fake.Emit(&events.Connected{}))
+ require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{}))
+ synctest.Wait()
+ require.Equal(t, 2, test.events.Count("notice"))
+ require.Equal(t, "WhatsApp is connected again.", test.events.Last("notice"))
+ })
+}
+
+func TestWhatsAppStartingRetriesDoNotDuplicateLibraryRetries(t *testing.T) {
+ dir := t.TempDir()
+ whatsappTestSession(t, dir, true)
+
+ whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock())
+ client, ok := whatsappNewClient(whatsapp.device, whatsapp.handle).(*whatsmeow.Client)
+ require.True(t, ok)
+ require.False(t, client.InitialAutoReconnect)
+ require.True(t, client.EnableAutoReconnect)
+
+ device := whatsapp.container.NewDevice()
+ client, ok = whatsappNewClient(device, whatsapp.handle).(*whatsmeow.Client)
+ require.True(t, ok)
+ require.True(t, client.InitialAutoReconnect)
+}
+
+func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {