X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/0da50dc187c8b4aa23ac1ddd0418c0da779389d9..b1be0ee8530df5db8973bfe355f2f6d3149383a0:/whatsapp_inbox_test.go?ds=inline diff --git a/whatsapp_inbox_test.go b/whatsapp_inbox_test.go index cea65fe..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,6 +137,27 @@ func TestWhatsAppGroupTurns(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 { @@ -235,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) {