X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/786c2b0551517d4befaa35be2c7743f65bd5f1c4..016d46008fc9726d2617e4e3ea098113f34c7636:/whatsapp_test.go diff --git a/whatsapp_test.go b/whatsapp_test.go index db04dc7..7a00bbe 100644 --- a/whatsapp_test.go +++ b/whatsapp_test.go @@ -19,6 +19,7 @@ import ( func TestWhatsAppStatusFollowsTheConnection(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + clock := newTestClock() whatsapp, fake, _ := whatsappStartTest(t, dir, clock) daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) @@ -30,8 +31,11 @@ func TestWhatsAppStatusFollowsTheConnection(t *testing.T) { require.Equal(t, "WhatsApp is linked and connected since 2026-01-01 12:00.", whatsapp.Status()) clock.now = clock.now.Add(20 * time.Minute) + require.True(t, fake.Emit(&events.Disconnected{})) + clock.now = clock.now.Add(time.Minute) + require.True(t, fake.Emit(&events.Disconnected{})) 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()) } @@ -62,7 +66,9 @@ func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) { 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() @@ -70,6 +76,7 @@ func TestWhatsAppRetriesTheFirstConnectionWithCappedBackoff(t *testing.T) { time.Sleep(time.Nanosecond) synctest.Wait() require.Len(t, test.fake.Connections(), index+2) + outage += delay } @@ -120,6 +127,7 @@ func TestWhatsAppLinkedPairRequestKeepsFirstConnectionRetries(t *testing.T) { 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) @@ -143,7 +151,8 @@ func TestWhatsAppFirstConnectionRetriesStopForLinkChanges(t *testing.T) { { name: "unlink", stop: func(test *WhatsAppRetryTest) { - test.whatsapp.Command("unlink", func(text string) {}) + test.whatsapp.Command("unlink", func(_ string) { + }) }, }, { @@ -183,22 +192,29 @@ func TestWhatsAppPairsAfterFirstConnectionRetriesStop(t *testing.T) { 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() - 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() 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) @@ -214,8 +230,10 @@ func TestWhatsAppInboxFailureStopsFirstConnectionRetries(t *testing.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() @@ -229,7 +247,9 @@ func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) { test.fake.connectErr = errors.New("network unavailable") test.start(t) synctest.Wait() + since := time.Now().Format(whatsappTimeLayout) + time.Sleep(whatsappNoticeDelay) synctest.Wait() require.Equal(t, 1, test.events.Count("notice")) @@ -239,6 +259,7 @@ func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) { 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) @@ -254,6 +275,7 @@ func TestWhatsAppLongFirstConnectionOutageGivesOneNotice(t *testing.T) { 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) @@ -269,17 +291,24 @@ func TestWhatsAppStartingRetriesDoNotDuplicateLibraryRetries(t *testing.T) { func TestWhatsAppLogsEachChangeOfTheConnectionOnce(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + clock := newTestClock() whatsapp, fake, logs := whatsappStartTest(t, dir, clock) daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) <-fake.connects require.True(t, fake.Emit(&events.Connected{})) + clock.now = clock.now.Add(time.Minute) + require.True(t, fake.Emit(&events.Disconnected{})) + clock.now = clock.now.Add(time.Minute) + require.True(t, fake.Emit(&events.Disconnected{})) + clock.now = clock.now.Add(25 * time.Second) + require.True(t, fake.Emit(&events.Connected{})) require.Equal(t, []string{ @@ -298,7 +327,7 @@ func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) { }{ { name: "no session file", - session: func(t *testing.T, dir string) { + session: func(_ *testing.T, _ string) { }, }, { @@ -313,6 +342,7 @@ func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) { t.Run(tc.name, func(t *testing.T) { dir := t.TempDir() tc.session(t, dir) + whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock()) require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status()) }) @@ -367,6 +397,7 @@ func TestWhatsAppPairsWithACode(t *testing.T) { other.WaitFor(t, "[connected to OneChat]") asking.Type("/whatsapp pair +1 555-000-1111") + items := <-fake.pairings items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventCode, @@ -377,7 +408,9 @@ func TestWhatsAppPairsWithACode(t *testing.T) { ID: types.NewADJID(whatsappTestNumber, 0, 1), LID: types.NewADJID(whatsappTestLID, 0, 1), })) + items <- whatsmeow.QRChannelSuccess + asking.WaitFor(t, "WhatsApp is linked to +15550001111.") // The library connects again after the link with the context of the @@ -482,6 +515,7 @@ func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) { client.Type("/whatsapp pair 15550001111") <-fake.pairings client.Type("/whatsapp pair 15550002222") + items := <-fake.pairings items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventCode, @@ -495,6 +529,7 @@ func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) { func TestWhatsAppPairWhenLinked(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + whatsapp, _, _ := whatsappStartTest(t, dir, newTestClock()) socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) @@ -524,10 +559,12 @@ func TestWhatsAppUnlink(t *testing.T) { t.Run(tc.name, func(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) fake.logoutErr = tc.logoutErr socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) + <-fake.connects client.Type("/whatsapp unlink") @@ -546,6 +583,7 @@ func TestWhatsAppUnlink(t *testing.T) { func TestWhatsAppLogoutFromThePhone(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) telegram := newTelegramFake(t) bot, _ := telegramStartTestBot(t, telegram) @@ -555,6 +593,7 @@ func TestWhatsAppLogoutFromThePhone(t *testing.T) { <-fake.connects require.True(t, fake.Emit(&events.LoggedOut{})) + notice := "WhatsApp was unlinked from the phone. Run /whatsapp pair PHONE to link it again." client.WaitFor(t, notice) telegramWaitForSent(t, telegram, notice) @@ -566,6 +605,7 @@ func TestWhatsAppLogoutFromThePhone(t *testing.T) { func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + clock := newTestClock() whatsapp, fake, _ := whatsappStartTest(t, dir, clock) whatsapp.noticeDelay = 300 * time.Millisecond @@ -581,6 +621,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { require.Equal(t, "[connected to OneChat]\n", client.Output()) clock.now = clock.now.Add(time.Hour) + require.True(t, fake.Emit(&events.Disconnected{})) require.True(t, fake.Emit(&events.Disconnected{})) client.WaitFor(t, "WhatsApp is disconnected since 2026-01-01 13:00. OneChat tries again.\n") @@ -595,6 +636,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) @@ -608,8 +650,10 @@ func TestWhatsAppDisconnectionWithNoRetryGivesANoticeAtOnce(t *testing.T) { func TestWhatsAppLogsNoOwnNumber(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) + whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock()) fake.connectErr = errors.New("no route for " + whatsappTestNumber + "@s.whatsapp.net and " + whatsappTestLID + "@lid") + daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) <-fake.connects @@ -741,10 +785,12 @@ func whatsappRetryTest(t *testing.T) *WhatsAppRetryTest { dir := t.TempDir() whatsappTestSession(t, dir, true) + whatsapp, fake, logs := whatsappStartTest(t, dir, newTestClock()) whatsapp.clock = time.Now agent, err := openAgent(t.TempDir()) require.NoError(t, err) + memory, err := openMemory(t.Context(), agent.dir, time.Now, newFakeSummarizer(), discardLog()) require.NoError(t, err) t.Cleanup(func() {