]> git.giacomolli.uk - onechat/blobdiff - whatsapp_test.go
Check operation context on returned errors
[onechat] / whatsapp_test.go
index c14571b61211412693e064d5bc12befa065367fe..dca137b8a55bf6d2dd2d87ea2f8cd206087e3170 100644 (file)
@@ -106,6 +106,35 @@ func TestWhatsAppFirstConnectionRetriesStopWhenContextEnds(t *testing.T) {
        })
 }
 
        })
 }
 
+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, "first connect failed, retrying: network unavailable\nconnected again after 2s\n", test.logs.String())
+       })
+}
+
 func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
        testCases := []struct {
                name string
 func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
        testCases := []struct {
                name string
@@ -114,7 +143,8 @@ func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
                {
                        name: "unlink",
                        stop: func(test *WhatsAppRetryTest) {
                {
                        name: "unlink",
                        stop: func(test *WhatsAppRetryTest) {
-                               test.whatsapp.Command("unlink", func(text string) {})
+                               test.whatsapp.Command("unlink", func(_ string) {
+                               })
                        },
                },
                {
                        },
                },
                {
@@ -123,12 +153,6 @@ func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) {
                                test.fake.Emit(&events.LoggedOut{})
                        },
                },
                                test.fake.Emit(&events.LoggedOut{})
                        },
                },
-               {
-                       name: "pair command",
-                       stop: func(test *WhatsAppRetryTest) {
-                               test.whatsapp.Command("pair 15550001111", func(text string) {})
-                       },
-               },
                {
                        name: "permanent disconnect",
                        stop: func(test *WhatsAppRetryTest) {
                {
                        name: "permanent disconnect",
                        stop: func(test *WhatsAppRetryTest) {
@@ -160,14 +184,16 @@ func TestWhatsAppPairsAfterFirstConnectionRetriesStop(t *testing.T) {
                test.fake.connectErr = errors.New("network unavailable")
                test.start(t)
                synctest.Wait()
                test.fake.connectErr = errors.New("network unavailable")
                test.start(t)
                synctest.Wait()
-               test.whatsapp.Command("unlink", func(text string) {})
+               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()
                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(text string) {})
+               test.whatsapp.Command("pair 15550001111", func(_ string) {
+               })
                items := <-test.fake.pairings
                items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
                synctest.Wait()
                items := <-test.fake.pairings
                items <- whatsmeow.QRChannelItem{Event: whatsmeow.QRChannelEventCode}
                synctest.Wait()
@@ -275,7 +301,7 @@ func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) {
        }{
                {
                        name: "no session file",
        }{
                {
                        name: "no session file",
-                       session: func(t *testing.T, dir string) {
+                       session: func(_ *testing.T, _ string) {
                        },
                },
                {
                        },
                },
                {