package main import ( "errors" "net/http" "os" "path/filepath" "strings" "testing" "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" ) 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) <-fake.connects require.Equal(t, "WhatsApp is linked and not connected yet.", whatsapp.Status()) require.True(t, fake.Emit(&events.Connected{})) 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()) } func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) <-fake.connects require.True(t, fake.Emit(&events.Disconnected{})) require.Equal(t, "WhatsApp is linked and disconnected since 2026-01-01 12:00.", whatsapp.Status()) } func TestWhatsAppIsNotLinkedWithoutALinkedDevice(t *testing.T) { testCases := []struct { name string session func(t *testing.T, dir string) }{ { name: "no session file", session: func(t *testing.T, dir string) { }, }, { name: "a session of a pairing that did not end", session: func(t *testing.T, dir string) { whatsappTestSession(t, dir, false) }, }, } for _, tc := range testCases { 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()) }) } } func TestWhatsAppRefusesASessionThatOthersCanRead(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) require.NoError(t, os.Chmod(filepath.Join(dir, whatsappDirName, whatsappSessionFileName), 0o644)) _, err := openWhatsApp(t.Context(), dir, time.Now, whatsappDiscardLog(), whatsappNewClient) require.EqualError(t, err, "whatsapp/session.db holds the WhatsApp session, so its mode must be 0600") } func TestWhatsAppCommandsAnswerOnlyTheClientThatAsked(t *testing.T) { server := newModelFake(t) telegram := newTelegramFake(t) bot, _ := telegramStartTestBot(t, telegram) socket := daemonStartTestWithBot(t, server, bot) asking := terminalStartTestClient(t, socket) other := terminalStartTestClient(t, socket) asking.WaitFor(t, "[connected to OneChat]") other.WaitFor(t, "[connected to OneChat]") asking.Type("/whatsapp status") asking.WaitFor(t, "WhatsApp is not linked.") asking.Type(" /whatsapp unknown ") asking.WaitFor(t, whatsappUsage) asking.Type("/whatsapp pair 0123") asking.WaitFor(t, "PHONE must be the number of your WhatsApp account") telegram.Add(TelegramUpdate{ UpdateID: 1, Message: telegramOwnerMessage("/whatsapp status"), }) telegramWaitForSent(t, telegram, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.") require.Len(t, telegram.Sent(), 1) require.Equal(t, "[connected to OneChat]\n", other.Output()) require.Zero(t, server.Calls()) } func TestWhatsAppPairsWithACode(t *testing.T) { dir := t.TempDir() whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) server := newModelFake(t) socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp) asking := terminalStartTestClient(t, socket) other := terminalStartTestClient(t, socket) asking.WaitFor(t, "[connected to OneChat]") other.WaitFor(t, "[connected to OneChat]") asking.Type("/whatsapp pair +1 555-000-1111") items := <-fake.pairings items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventCode, } asking.WaitFor(t, whatsappPairSteps+"\n"+whatsappTestCode+"\n") require.True(t, fake.Emit(&events.PairSuccess{ ID: types.NewADJID(whatsappTestNumber, 0, 1), LID: types.NewADJID(whatsappTestLID, 0, 1), })) items <- whatsmeow.QRChannelSuccess asking.WaitFor(t, "WhatsApp is linked to +15550001111.") 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()) require.Zero(t, server.Calls()) require.Equal(t, []string{"WhatsApp is linked."}, whatsappTestLogTexts(t, memory)) info, err := os.Stat(filepath.Join(dir, whatsappDirName)) require.NoError(t, err) require.Equal(t, os.FileMode(0o700), info.Mode().Perm()) info, err = os.Stat(filepath.Join(dir, whatsappDirName, whatsappSessionFileName)) require.NoError(t, err) require.Equal(t, os.FileMode(0o600), info.Mode().Perm()) } func TestWhatsAppPairingCodeIsATelegramMessageAlone(t *testing.T) { whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock()) telegram := newTelegramFake(t) bot, _ := telegramStartTestBot(t, telegram) daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp) telegram.Add(TelegramUpdate{ UpdateID: 1, Message: telegramOwnerMessage("/whatsapp pair 15550001111"), }) items := <-fake.pairings items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventCode, } telegramWaitForSent(t, telegram, whatsappPairSteps, whatsappTestCode) } func TestWhatsAppPairingEnds(t *testing.T) { testCases := []struct { name string end func(items chan whatsmeow.QRChannelItem) answer string }{ { name: "the code expires", end: func(items chan whatsmeow.QRChannelItem) { items <- whatsmeow.QRChannelTimeout }, answer: whatsappPairExpired, }, { name: "the library closes the pairing", end: func(items chan whatsmeow.QRChannelItem) { close(items) }, answer: whatsappPairExpired, }, { name: "the pairing fails", end: func(items chan whatsmeow.QRChannelItem) { items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventError, Error: errors.New("bad handshake"), } }, answer: "The pairing failed: bad handshake. Run /whatsapp pair PHONE again.", }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock()) socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) client.Type("/whatsapp pair 15550001111") items := <-fake.pairings items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventCode, } client.WaitFor(t, whatsappTestCode) tc.end(items) client.WaitFor(t, tc.answer) require.Equal(t, "WhatsApp is not linked. Run /whatsapp pair PHONE to link it.", whatsapp.Status()) }) } } func TestWhatsAppNewPairingEndsTheOneBefore(t *testing.T) { whatsapp, fake, _ := whatsappStartTest(t, t.TempDir(), newTestClock()) socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) client.Type("/whatsapp pair 15550001111") <-fake.pairings client.Type("/whatsapp pair 15550002222") items := <-fake.pairings items <- whatsmeow.QRChannelItem{ Event: whatsmeow.QRChannelEventCode, } client.WaitFor(t, whatsappTestCode) require.Equal(t, []string{"15550002222"}, fake.Phones()) require.NotContains(t, client.Output(), "did not start the pairing") } 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) client.Type("/whatsapp pair 15550001111") client.WaitFor(t, "WhatsApp is linked already. Run /whatsapp unlink first.") } func TestWhatsAppUnlink(t *testing.T) { testCases := []struct { name string logoutErr error answer string }{ { name: "with a logout", answer: "WhatsApp is unlinked.", }, { name: "with no 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.", }, } for _, tc := range testCases { 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") client.WaitFor(t, tc.answer) require.Equal(t, 1, fake.Logouts()) 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)) client.Type("/whatsapp unlink") client.WaitFor(t, "WhatsApp is not linked.\n") }) } } 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) socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), bot, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") <-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) 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)) } func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) clock := newTestClock() whatsapp, fake, _ := whatsappStartTest(t, dir, clock) whatsapp.noticeDelay = 50 * time.Millisecond socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") <-fake.connects 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) 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") require.True(t, fake.Emit(&events.Disconnected{})) time.Sleep(100 * time.Millisecond) require.True(t, fake.Emit(&events.Connected{})) client.WaitFor(t, "WhatsApp is connected again.\n") require.Equal(t, 1, strings.Count(client.Output(), "WhatsApp is disconnected since")) } 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) client.WaitFor(t, "[connected to OneChat]") <-fake.connects 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.") } 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 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()) } func TestWhatsAppPhone(t *testing.T) { testCases := []struct { name string phone string valid bool }{ { name: "a number with a plus and dashes", phone: "+1-555-000-1111", valid: true, }, { name: "a number with no country code", phone: "0115550001111", valid: false, }, { name: "a short number", phone: "123456", valid: false, }, { name: "a long number", phone: "1234567890123456", valid: false, }, { name: "a number with letters", phone: "1555ABC1111", valid: false, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { _, valid := whatsappPhone(tc.phone) require.Equal(t, tc.valid, valid) }) } } func TestWhatsAppCommandArgs(t *testing.T) { testCases := []struct { name string line string args string found bool }{ { name: "a command with its arguments", line: " /whatsapp pair +1 555 ", args: "pair +1 555", found: true, }, { name: "the command alone", line: "/whatsapp", args: "", found: true, }, { name: "a word that starts with the command", line: "/whatsappstatus", args: "", found: false, }, { name: "a message that names the command", line: "what does /whatsapp status do?", args: "", found: false, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { args, found := whatsappCommandArgs(tc.line) require.Equal(t, tc.args, args) require.Equal(t, tc.found, found) }) } } // 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 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]") whatsappTestConnect(t, fake) 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) } 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]") whatsappTestConnect(t, fake) 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 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(" ") }, }, } 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()) }) } } // 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) { t.Helper() dir := t.TempDir() whatsappTestSession(t, dir, true) whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) whatsapp.batches.quiet = 50 * time.Millisecond return whatsapp, fake } func TestWhatsAppCatchupGivesOneTurn(t *testing.T) { whatsapp, fake := whatsappStartTestLinked(t) server := newModelFake(t) socket, memory := daemonStartTestWithWhatsApp(t, server, nil, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") <-fake.connects require.True(t, fake.Emit(&events.Connected{})) at := time.Date(2026, 1, 1, 8, 0, 0, 0, time.UTC) for index, event := range []*events.Message{ whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m1", "Good morning", at), whatsappTestMessage(whatsappTestBruno, whatsappTestBruno, "m2", "Call me", at.Add(time.Hour)), whatsappTestMessage(whatsappTestAna, whatsappTestAna, "m3", "Are you there?", at.Add(2*time.Hour)), } { event.Info.PushName = []string{"Ana", "Bruno", "Ana"}[index] require.True(t, fake.Emit(event)) } time.Sleep(100 * time.Millisecond) require.Zero(t, memory.Len()) require.True(t, fake.Emit(&events.OfflineSyncCompleted{})) client.WaitFor(t, "[done]") require.Equal(t, []string{ "[WhatsApp] Ana, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n08:00 Ana: Good morning\n" + whatsappBatchEmptyReply, "[WhatsApp] Bruno, private chat 15550003333@s.whatsapp.net, Thursday 2026-01-01\n09:00 Bruno: Call me\n" + whatsappBatchEmptyReply, "[WhatsApp] Ana, private chat 15550002222@s.whatsapp.net, Thursday 2026-01-01\n10:00 Ana: Are you there?\n" + whatsappBatchEmptyReply, }, whatsappTestLogTexts(t, memory)[:3]) time.Sleep(100 * time.Millisecond) require.Equal(t, 1, server.Calls()) for _, text := range []string{"Good morning", "Call me", "Are you there?"} { require.Contains(t, string(server.Call(0).raw), text) } } func TestWhatsAppCatchupEndsAfterItsLimit(t *testing.T) { whatsapp, fake := whatsappStartTestLinked(t) whatsapp.catchupLimit = 100 * time.Millisecond socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") <-fake.connects require.True(t, fake.Emit(&events.Connected{})) require.True(t, fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m1", "On my way.", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)))) client.WaitFor(t, "12:00 You: On my way.") require.Len(t, whatsappTestLogTexts(t, memory), 1) } func TestWhatsAppBatchesOfTheStateFileWaitForTheCatchup(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) before, _, _ := whatsappStartTest(t, dir, newTestClock()) _, err := before.batches.Add(whatsappTestChat, WhatsAppMessage{ ID: "m1", Sender: "Ana Souza", Time: time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC), Text: "Did you see my message?", }, time.Now().Add(-time.Hour)) require.NoError(t, err) whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) client := terminalStartTestClient(t, socket) client.WaitFor(t, "[connected to OneChat]") <-fake.connects time.Sleep(100 * time.Millisecond) require.Zero(t, memory.Len()) require.True(t, fake.Emit(&events.Connected{})) require.True(t, fake.Emit(&events.OfflineSyncCompleted{})) client.WaitFor(t, "12:00 Ana Souza: Did you see my message?") client.WaitFor(t, "[done]") } // whatsappTestConnect waits for the connection of the daemon and gives it the // events of a connection with an empty catch-up. func whatsappTestConnect(t *testing.T, fake *WhatsAppFake) { t.Helper() <-fake.connects require.True(t, fake.Emit(&events.Connected{})) require.True(t, fake.Emit(&events.OfflineSyncCompleted{})) }