X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/c781f79f3c8cb9a7577ee1574d196988da2719b3..9301f77a5709fe7daea3a71f8dd072c6cd7df4fe:/whatsapp_test.go?ds=sidebyside diff --git a/whatsapp_test.go b/whatsapp_test.go index 05ea123..d200a91 100644 --- a/whatsapp_test.go +++ b/whatsapp_test.go @@ -2,7 +2,6 @@ package main import ( "errors" - "log/slog" "os" "path/filepath" "strings" @@ -91,7 +90,7 @@ func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) { 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()) + require.Equal(t, "<4>first connect failed, retrying: network unavailable\n<6>connected again after="+daemonLogDuration(outage)+"\n", test.logs.String()) }) } @@ -150,20 +149,20 @@ func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) { stop func(test *WhatsAppRetryTest) }{ { - name: "unlink", + name: "stops retrying after unlink", stop: func(test *WhatsAppRetryTest) { test.whatsapp.Command("unlink", func(_ string) { }) }, }, { - name: "logout", + name: "stops retrying after logout", stop: func(test *WhatsAppRetryTest) { test.fake.Emit(&events.LoggedOut{}) }, }, { - name: "permanent disconnect", + name: "stops retrying after a permanent disconnect", stop: func(test *WhatsAppRetryTest) { test.fake.Emit(&events.StreamReplaced{}) }, @@ -329,12 +328,12 @@ func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) { session func(t *testing.T, dir string) }{ { - name: "no session file", + name: "stays unlinked without a session file", session: func(_ *testing.T, _ string) { }, }, { - name: "a session of a pairing that did not end", + name: "stays unlinked after incomplete pairing", session: func(t *testing.T, dir string) { whatsappTestSession(t, dir, false) }, @@ -357,7 +356,7 @@ func TestWhatsAppRefusesASessionThatOthersCanRead(t *testing.T) { whatsappTestSession(t, dir, true) require.NoError(t, os.Chmod(filepath.Join(dir, whatsappDirName, whatsappSessionFileName), 0o644)) - _, err := openWhatsApp(t.Context(), dir, time.Now, whatsappDiscardLog(), whatsappNewClient) + _, err := openWhatsApp(t.Context(), dir, time.Now, discardLog(), whatsappNewClient) require.EqualError(t, err, "whatsapp/session.db holds the WhatsApp session, so its mode must be 0600") } @@ -466,21 +465,21 @@ func TestWhatsAppPairingEnds(t *testing.T) { answer string }{ { - name: "the code expires", + name: "ends pairing when the code expires", end: func(items chan whatsmeow.QRChannelItem) { items <- whatsmeow.QRChannelTimeout }, answer: whatsappPairExpired, }, { - name: "the library closes the pairing", + name: "ends pairing when the library closes it", end: func(items chan whatsmeow.QRChannelItem) { close(items) }, answer: whatsappPairExpired, }, { - name: "the pairing fails", + name: "ends pairing when the library reports failure", end: func(items chan whatsmeow.QRChannelItem) { items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventError, @@ -549,11 +548,11 @@ func TestWhatsAppUnlink(t *testing.T) { answer string }{ { - name: "with a logout", + name: "logs out before removing the session", answer: "WhatsApp is unlinked.", }, { - name: "with no logout", + name: "removes an offline session without logout", logoutErr: errors.New("not connected"), 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.", }, @@ -602,7 +601,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.Equal(t, "<3>logged out by the 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)) @@ -677,27 +676,27 @@ func TestWhatsAppPhone(t *testing.T) { valid bool }{ { - name: "a number with a plus and dashes", + name: "accepts a number with a plus and dashes", phone: "+1-555-000-1111", valid: true, }, { - name: "a number with no country code", + name: "refuses a number without a country code", phone: "0115550001111", valid: false, }, { - name: "a short number", + name: "refuses a number shorter than the minimum", phone: "123456", valid: false, }, { - name: "a long number", + name: "refuses a number longer than the maximum", phone: "1234567890123456", valid: false, }, { - name: "a number with letters", + name: "refuses a number with letters", phone: "1555ABC1111", valid: false, }, @@ -719,25 +718,25 @@ func TestWhatsAppCommandArgs(t *testing.T) { found bool }{ { - name: "a command with its arguments", + name: "reads and trims the command arguments", line: " /whatsapp pair +1 555 ", args: "pair +1 555", found: true, }, { - name: "the command alone", + name: "accepts the command without arguments", line: "/whatsapp", args: "", found: true, }, { - name: "a word that starts with the command", + name: "leaves a joined command word as text", line: "/whatsappstatus", args: "", found: false, }, { - name: "a message that names the command", + name: "leaves a mention of the command as text", line: "what does /whatsapp status do?", args: "", found: false, @@ -758,8 +757,3 @@ func TestSubagentHasNoWhatsAppTools(t *testing.T) { require.Equal(t, `error: unknown tool "whatsapp_find"`, box.run(t.Context(), "whatsapp_find", `{"name":"ana"}`)) require.Equal(t, `error: unknown tool "whatsapp_send"`, box.run(t.Context(), "whatsapp_send", `{"id":"a","chat":"15550002222@s.whatsapp.net","text":"hi"}`)) } - -// whatsappDiscardLog is a logger for a WhatsApp whose log a test does not read. -func whatsappDiscardLog() *slog.Logger { - return slog.New(slog.DiscardHandler) -}