]> git.giacomolli.uk - onechat/blobdiff - whatsapp_test.go
Show styled replies only on terminal output
[onechat] / whatsapp_test.go
index 7a00bbeb3be005ab68a4a345216d57f27e3407df..96ec2df0031cb169310fb99324046ce8e9698bf4 100644 (file)
@@ -48,7 +48,7 @@ func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) {
                synctest.Wait()
 
                require.Equal(t, "WhatsApp is linked and disconnected since "+time.Now().Format(whatsappTimeLayout)+".", test.whatsapp.Status())
-               require.Equal(t, "first connect failed, retrying: network unavailable\n", test.logs.String())
+               require.Equal(t, "<4>first connect failed, retrying: network unavailable\n", test.logs.String())
        })
 }
 
@@ -91,7 +91,7 @@ func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) {
                time.Sleep(10 * time.Minute)
                synctest.Wait()
                require.Len(t, test.fake.Connections(), 11)
-               require.Equal(t, "first connect failed, retrying: network unavailable\nconnected again after "+durationText(outage)+"\n", test.logs.String())
+               require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after="+durationText(outage)+"\n", test.logs.String())
        })
 }
 
@@ -139,7 +139,7 @@ func TestWhatsAppLinkedPairRequestKeepsFirstConnectionRetries(t *testing.T) {
                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())
+               require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after=2s\n", test.logs.String())
        })
 }
 
@@ -312,9 +312,9 @@ func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) {
        require.True(t, fake.Emit(&events.Connected{}))
 
        require.Equal(t, []string{
-               "connected",
-               "disconnected, retrying",
-               "connected again after 1m25s",
+               "<6>connected",
+               "<4>disconnected, retrying",
+               "<6>connected again after=1m25s",
        }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n"))
        require.NotContains(t, logs.String(), whatsappTestNumber)
        require.NotContains(t, logs.String(), whatsappTestLID)
@@ -388,7 +388,7 @@ func TestWhatsAppCommandsAnswerOnlyTheClientThatAsked(t *testing.T) {
 
 func TestWhatsAppPairsWithACode(t *testing.T) {
        dir := t.TempDir()
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
+       whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
        server := newModelFake(t)
        socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp)
        asking := terminalStartTestClient(t, socket)
@@ -426,6 +426,7 @@ func TestWhatsAppPairsWithACode(t *testing.T) {
        require.Equal(t, "[connected to OneChat]\n", other.Output())
        require.Zero(t, server.Calls())
        require.Equal(t, []string{"WhatsApp is linked."}, whatsappTestLogTexts(t, memory))
+       require.Equal(t, "<6>linked\n", logs.String())
 
        info, err := os.Stat(filepath.Join(dir, whatsappDirName))
        require.NoError(t, err)
@@ -560,7 +561,7 @@ func TestWhatsAppUnlink(t *testing.T) {
                        dir := t.TempDir()
                        whatsappTestSession(t, dir, true)
 
-                       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
+                       whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
                        fake.logoutErr = tc.logoutErr
                        socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
                        client := terminalStartTestClient(t, socket)
@@ -570,6 +571,7 @@ func TestWhatsAppUnlink(t *testing.T) {
                        client.Type("/whatsapp unlink")
                        client.WaitFor(t, tc.answer)
                        require.Equal(t, 1, fake.Logouts())
+                       require.Equal(t, "<6>unlinked\n", logs.String())
                        require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
                        require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
                        require.Equal(t, []string{"WhatsApp is unlinked."}, whatsappTestLogTexts(t, memory))
@@ -584,7 +586,7 @@ func TestWhatsAppLogoutFromThePhone(t *testing.T) {
        dir := t.TempDir()
        whatsappTestSession(t, dir, true)
 
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
+       whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
        telegram := newTelegramFake(t)
        bot, _ := telegramStartTestBot(t, telegram)
        socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp)
@@ -597,6 +599,7 @@ func TestWhatsAppLogoutFromThePhone(t *testing.T) {
        notice := "WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again."
        client.WaitFor(t, notice)
        telegramWaitForSent(t, telegram, notice)
+       require.Equal(t, "<3>logged out by the phone, run /whatsapp pair PHONE\n", logs.String())
        require.NoFileExists(t, filepath.Join(dir, whatsappDirName, whatsappSessionFileName))
        require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status())
        require.Equal(t, []string{"WhatsApp was unlinked from the phone."}, whatsappTestLogTexts(t, memory))
@@ -637,7 +640,7 @@ func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) {
        dir := t.TempDir()
        whatsappTestSession(t, dir, true)
 
-       whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock())
+       whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock())
        socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
        client := terminalStartTestClient(t, socket)
        client.WaitFor(t, "[connected to OneChat]")
@@ -645,6 +648,7 @@ func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) {
 
        require.True(t, fake.Emit(&events.StreamReplaced{}))
        client.WaitFor(t, "WhatsApp is disconnected and does not connect again by itself: stream replaced. Restart the daemon to try again.")
+       require.Equal(t, "<3>disconnected for good, restart to retry: stream replaced\n", logs.String())
 }
 
 func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
@@ -660,7 +664,7 @@ func TestWhatsAppLogsNoOwnNumber(t *testing.T) {
        require.Eventually(t, func() bool {
                return logs.String() != ""
        }, 10*time.Second, time.Millisecond)
-       require.Equal(t, "first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
+       require.Equal(t, "<4>first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String())
 }
 
 func TestWhatsAppPhone(t *testing.T) {