X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/56d959f2d573eef1be5c04d0bf1659fa30d36e7c..7c84ed87ef71088b880a3765b2653e1ded57bf14:/whatsapp_test.go?ds=sidebyside diff --git a/whatsapp_test.go b/whatsapp_test.go index 38a7b8d..db04dc7 100644 --- a/whatsapp_test.go +++ b/whatsapp_test.go @@ -1,18 +1,17 @@ package main import ( + "context" "errors" - "net/http" "os" "path/filepath" "strings" "testing" + "testing/synctest" "time" "github.com/stretchr/testify/require" "go.mau.fi/whatsmeow" - "go.mau.fi/whatsmeow/proto/waE2E" - "go.mau.fi/whatsmeow/proto/waWeb" "go.mau.fi/whatsmeow/types" "go.mau.fi/whatsmeow/types/events" ) @@ -38,14 +37,258 @@ func TestWhatsAppStatusFollowsTheConnection(t *testing.T) { } func TestWhatsAppStatusOfAFirstConnectionThatFailed(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() + + 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()) + }) +} + +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, "first connect failed, retrying: network unavailable\nconnected 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, "first connect failed, retrying: network unavailable\nconnected 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(text 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(text 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) {}) + 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(whatsappNoticeDelay) + 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(whatsappNoticeDelay) + 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, fake, _ := whatsappStartTest(t, dir, newTestClock()) + 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) { + 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{})) - require.Equal(t, "WhatsApp is linked and disconnected since 2026-01-01 12:00.", whatsapp.Status()) + 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{ + "connected", + "disconnected, retrying", + "connected again after 1m25s", + }, strings.Split(strings.TrimSuffix(logs.String(), "\n"), "\n")) + require.NotContains(t, logs.String(), whatsappTestNumber) + require.NotContains(t, logs.String(), whatsappTestLID) } func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) { @@ -137,6 +380,14 @@ func TestWhatsAppPairsWithACode(t *testing.T) { items <- whatsmeow.QRChannelSuccess asking.WaitFor(t, "WhatsApp is linked to +15550001111.") + // The library connects again after the link with the context of the + // connection, so the end of the pairing must not end it. + connections := fake.Connections() + require.Len(t, connections, 1) + require.Never(t, func() bool { + return connections[0].Err() != nil + }, 200*time.Millisecond, time.Millisecond) + require.Equal(t, []string{whatsappTestNumber}, fake.Phones()) require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status()) require.Equal(t, "[connected to OneChat]\n", other.Output()) @@ -317,7 +568,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { whatsappTestSession(t, dir, true) clock := newTestClock() whatsapp, fake, _ := whatsappStartTest(t, dir, clock) - whatsapp.noticeDelay = 50 * time.Millisecond + whatsapp.noticeDelay = 300 * time.Millisecond socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") @@ -326,7 +577,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { require.True(t, fake.Emit(&events.Connected{})) require.True(t, fake.Emit(&events.Disconnected{})) require.True(t, fake.Emit(&events.Connected{})) - time.Sleep(100 * time.Millisecond) + time.Sleep(400 * time.Millisecond) require.Equal(t, "[connected to OneChat]\n", client.Output()) clock.now = clock.now.Add(time.Hour) @@ -334,7 +585,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { require.True(t, fake.Emit(&events.Disconnected{})) client.WaitFor(t, "WhatsApp is disconnected since 2026-01-01 13:00. OneChat tries again.\n") require.True(t, fake.Emit(&events.Disconnected{})) - time.Sleep(100 * time.Millisecond) + time.Sleep(400 * time.Millisecond) require.True(t, fake.Emit(&events.Connected{})) client.WaitFor(t, "WhatsApp is connected again.\n") @@ -365,7 +616,7 @@ func TestWhatsAppLogsNoOwnNumber(t *testing.T) { require.Eventually(t, func() bool { return logs.String() != "" }, 10*time.Second, time.Millisecond) - require.Equal(t, "connect: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String()) + require.Equal(t, "first connect failed, retrying: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String()) } func TestWhatsAppPhone(t *testing.T) { @@ -451,222 +702,65 @@ func TestWhatsAppCommandArgs(t *testing.T) { } } -// whatsappTestLogTexts returns the texts of the messages in the log of memory, -// after the log holds at least one message. -func whatsappTestLogTexts(t *testing.T, memory *Memory) []string { - t.Helper() - - require.Eventually(t, func() bool { - return memory.Len() > 0 - }, 10*time.Second, time.Millisecond) - - var texts []string - for id := range memory.Len() { - message, err := memory.Message(id) - require.NoError(t, err) - - texts = append(texts, message.Text) - } - - return texts +func TestSubagentHasNoWhatsAppTools(t *testing.T) { + box := &Toolbox{} + 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"}`)) } -func TestWhatsAppPrivateBatchStartsATurnWithAnEmptyReply(t *testing.T) { - whatsapp, fake := whatsappStartTestLinked(t) - require.NoError(t, whatsapp.device.Contacts.PutContactName(t.Context(), whatsappTestAna, "Ana", "Ana Souza")) - - server := newModelFake(t) - server.reply = func(w http.ResponseWriter, call ModelFakeCall) { - modelWriteEvents(w, modelCompletedEvent([]any{modelMessageItem("")}, 100, 0, 5)) - } - - telegram := newTelegramFake(t) - bot, _ := telegramStartTestBot(t, telegram) - socket, memory := daemonStartTestWithWhatsApp(t, server, bot, whatsapp) - client := terminalStartTestClient(t, socket) - client.WaitFor(t, "[connected to OneChat]") - <-fake.connects - - at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) - require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "Are you at home?", at))) - require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m2", "Yes", at))) - - text := "[WhatsApp] Ana Souza, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n12:00 Ana Souza: Are you at home?\n12:00 You: Yes\n" + whatsappBatchEmptyReply - client.WaitFor(t, text+"\n[working]\n[done]\n") - require.Equal(t, 1, server.Calls()) - require.Contains(t, string(server.Call(0).raw), "Are you at home?") - require.Equal(t, []string{text}, whatsappTestLogTexts(t, memory)) - require.Empty(t, telegram.Sent()) - require.Eventually(t, func() bool { - return len(whatsapp.batches.Closed()) == 0 - }, 10*time.Second, time.Millisecond) +// WhatsAppRetryTest runs WhatsApp without a model or network client call. All +// timers and channels belong to its synctest bubble, so backoff uses fake time. +type WhatsAppRetryTest struct { + whatsapp *WhatsApp + fake *WhatsAppFake + logs *SyncBuffer + loop *TurnLoop + events *TurnEventRecorder + cancel context.CancelFunc + done chan error } -func TestWhatsAppGroupTurns(t *testing.T) { - testCases := []struct { - name string - message func(event *events.Message) - line string - turn bool - }{ - { - name: "plain", - message: func(event *events.Message) { - }, - line: "12:00 Bruno Lima: Who brings the cake?", - }, - { - name: "mention", - message: func(event *events.Message) { - event.Message = &waE2E.Message{ - ExtendedTextMessage: &waE2E.ExtendedTextMessage{ - Text: new("@" + whatsappTestLID + " can you bring the cake? Call " + whatsappTestNumber + "."), - ContextInfo: &waE2E.ContextInfo{ - MentionedJID: []string{whatsappTestLID + "@lid"}, - }, - }, - } - }, - line: "12:00 Bruno Lima (mentions you): @you can you bring the cake? Call OWN.", - turn: true, - }, - { - name: "reply", - message: func(event *events.Message) { - event.Message = &waE2E.Message{ - ExtendedTextMessage: &waE2E.ExtendedTextMessage{ - Text: new("Who brings the cake?"), - ContextInfo: &waE2E.ContextInfo{ - Participant: new(whatsappTestOwn.String()), - }, - }, - } - }, - line: "12:00 Bruno Lima (replies to you): Who brings the cake?", - turn: true, - }, - } - - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - whatsapp, fake := whatsappStartTestLinked(t) - fake.groups = []*types.GroupInfo{ - { - JID: whatsappTestFamily, - GroupName: types.GroupName{ - Name: "Family", - }, - }, - } - - server := newModelFake(t) - socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp) - client := terminalStartTestClient(t, socket) - client.WaitFor(t, "[connected to OneChat]") - <-fake.connects - - event := whatsappTestMessage(whatsappTestFamily, whatsappTestBruno, "m1", "Who brings the cake?", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)) - event.Info.PushName = "Bruno Lima" - tc.message(event) - require.True(t, fake.Emit(event)) - - text := "[WhatsApp] Family, group 120363000000000001@g.us, Thursday 2026-01-01\n" + tc.line - if tc.turn { - text += "\n" + whatsappBatchEmptyReply - client.WaitFor(t, "[done]") - require.Equal(t, 1, server.Calls()) - } else { - client.WaitFor(t, text) - time.Sleep(100 * time.Millisecond) - require.Zero(t, server.Calls()) - } - - require.Equal(t, text, whatsappTestLogTexts(t, memory)[0]) - }) - } -} +func (r *WhatsAppRetryTest) start(t *testing.T) { + t.Helper() -func TestWhatsAppTakesOnlyText(t *testing.T) { - at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) - testCases := []struct { - name string - change func(event *events.Message) - }{ - { - name: "an edit", - change: func(event *events.Message) { - event.IsEdit = true - }, - }, - { - name: "old history", - change: func(event *events.Message) { - event.SourceWebMsg = &waWeb.WebMessageInfo{} - }, - }, - { - name: "a view-once message", - change: func(event *events.Message) { - event.IsViewOnce = true - }, - }, - { - name: "a disappearing message", - change: func(event *events.Message) { - event.IsEphemeral = true - }, - }, - { - name: "the chat of the user with itself", - change: func(event *events.Message) { - event.Info.Chat = whatsappTestOwn - }, - }, - { - name: "a status broadcast", - change: func(event *events.Message) { - event.Info.Chat = types.StatusBroadcastJID - }, - }, - { - name: "a photo with a caption", - change: func(event *events.Message) { - event.Message = &waE2E.Message{ - ImageMessage: &waE2E.ImageMessage{ - Caption: new("look"), - }, - } - }, - }, - { - name: "a text of spaces", - change: func(event *events.Message) { - event.Message.Conversation = new(" ") - }, - }, - } + ctx, cancel := context.WithCancel(t.Context()) + r.cancel = cancel + r.done = make(chan error, 1) + go func() { + r.done <- r.whatsapp.Run(ctx, r.loop, r.events.Add) + }() - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - whatsapp, _ := whatsappStartTestLinked(t) - event := whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "hi", at) - tc.change(event) - require.True(t, whatsapp.handle(event)) - require.NoError(t, whatsapp.batches.CloseAll()) - require.Empty(t, whatsapp.batches.Closed()) - }) - } + t.Cleanup(func() { + cancel() + synctest.Wait() + }) } -// whatsappStartTestLinked opens a WhatsApp with a linked device in a temporary -// directory, with a quiet time of 50 ms for its batches. -func whatsappStartTestLinked(t *testing.T) (*WhatsApp, *WhatsAppFake) { +func whatsappRetryTest(t *testing.T) *WhatsAppRetryTest { t.Helper() dir := t.TempDir() whatsappTestSession(t, dir, true) - whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) - whatsapp.batches.quiet = 50 * time.Millisecond + 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() { + require.NoError(t, memory.Close()) + }) + + events := &TurnEventRecorder{} + client := newModelClient("https://example.com", ModelAPIKey(modelTestKey), agent.CacheKey(), filepath.Join(agent.dir, "usage.jsonl")) + loop, err := newTurnLoop(agent, memory, newTestSubagents(t), whatsapp, newTestSchedules(t, time.Now), client, time.Now, discardLog(), events.Add) + require.NoError(t, err) - return whatsapp, fake + return &WhatsAppRetryTest{ + whatsapp: whatsapp, + fake: fake, + logs: logs, + loop: loop, + events: events, + } }