X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..b1be0ee8530df5db8973bfe355f2f6d3149383a0:/whatsapp_inbox_test.go?ds=sidebyside diff --git a/whatsapp_inbox_test.go b/whatsapp_inbox_test.go index c3937df..1ce9a72 100644 --- a/whatsapp_inbox_test.go +++ b/whatsapp_inbox_test.go @@ -3,6 +3,7 @@ package main import ( "net/http" "testing" + "testing/synctest" "time" "github.com/stretchr/testify/require" @@ -115,6 +116,9 @@ func TestWhatsAppGroupTurns(t *testing.T) { event.Info.PushName = "Bruno Lima" tc.message(event) require.True(t, fake.Emit(event)) + require.NoError(t, whatsapp.state.CloseAll()) + + whatsapp.inbox.wake <- struct{}{} text := "[WhatsApp] Family, group 120363000000000001@g.us, Thursday 2026-01-01\n" + tc.line if tc.turn { @@ -133,7 +137,28 @@ func TestWhatsAppGroupTurns(t *testing.T) { } } -func TestWhatsAppTakesOnlyText(t *testing.T) { +func TestWhatsAppRunClosesABatchAfterQuietTime(t *testing.T) { + synctest.Test(t, func(t *testing.T) { + test := whatsappRetryTest(t) + test.start(t) + synctest.Wait() + require.True(t, test.fake.Emit(&events.Connected{})) + require.True(t, test.fake.Emit(&events.OfflineSyncCompleted{})) + synctest.Wait() + require.True(t, test.fake.Emit(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m1", "On my way.", time.Now()))) + synctest.Wait() + time.Sleep(whatsappBatchIdleTimeout - time.Nanosecond) + synctest.Wait() + require.Zero(t, test.loop.memory.Len()) + + time.Sleep(time.Nanosecond) + synctest.Wait() + require.Equal(t, 1, test.loop.memory.Len()) + require.Contains(t, test.events.Last("work"), "You: On my way.") + }) +} + +func TestWhatsAppSkipsExcludedMessages(t *testing.T) { at := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC) testCases := []struct { name string @@ -175,16 +200,6 @@ func TestWhatsAppTakesOnlyText(t *testing.T) { 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) { @@ -245,17 +260,20 @@ func TestWhatsAppCatchupGivesOneTurn(t *testing.T) { } func TestWhatsAppCatchupEndsAfterItsLimit(t *testing.T) { - whatsapp, fake := whatsappStartTestLinked(t) - whatsapp.inbox.catchupLimit = 100 * time.Millisecond - socket, memory := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp) - client := terminalStartTestClient(t, socket) - client.WaitFor(t, "[connected to OneChat]") - <-fake.connects + synctest.Test(t, func(t *testing.T) { + test := whatsappRetryTest(t) + test.whatsapp.client = test.fake + require.True(t, test.whatsapp.handle(&events.Connected{})) + require.True(t, test.whatsapp.handle(whatsappTestMessage(whatsappTestAna, whatsappTestOwn, "m1", "On my way.", time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)))) + require.NoError(t, test.whatsapp.inbox.deliver(t.Context(), test.loop, test.whatsapp.link(), false)) + require.Zero(t, test.loop.memory.Len()) - 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) + time.Sleep(whatsappCatchupTimeout) + synctest.Wait() + require.NoError(t, test.whatsapp.inbox.deliver(t.Context(), test.loop, test.whatsapp.link(), false)) + require.Equal(t, 1, test.loop.memory.Len()) + require.Contains(t, test.events.Last("work"), "12:00 You: On my way.") + }) } func TestWhatsAppStateOfTheStateFileWaitForTheCatchup(t *testing.T) {