]> git.giacomolli.uk - onechat/commitdiff
Make two WhatsApp tests independent of the load
authorAnderson A. Giacomolli <agiacomolli@gmail.com>
Sun, 8 Nov 2026 15:25:24 +0000 (12:25 -0300)
committerAnderson A. Giacomolli <agiacomolli@gmail.com>
Sun, 8 Nov 2026 15:25:24 +0000 (12:25 -0300)
The test of a private batch gave its two messages with 50 ms of real
quiet time between them, so on a loaded machine the first message closed
its batch alone and the test failed. The messages now come during a
catch-up, which holds the batch open until its end, so the test no
longer depends on the speed of the machine.

The test of a long disconnection waits 300 ms before the notice and 400
ms for a notice that must not come, so that a slow machine does not give
a notice between two events of the test.

Issue: 6f69ca1

whatsapp_test.go

index 7c893cc85ecbdbdb9bcb32c4b1f65a859266dad1..437a8debec492becf5fdd9130328745f3a2bde62 100644 (file)
@@ -318,7 +318,7 @@ func TestWhatsAppLongDisconnectionGivesOneNotice(t *testing.T) {
        whatsappTestSession(t, dir, true)
        clock := newTestClock()
        whatsapp, fake, _ := whatsappStartTest(t, dir, clock)
        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]")
        socket, _ := daemonStartTestWithWhatsApp(t, newModelFake(t), nil, whatsapp)
        client := terminalStartTestClient(t, socket)
        client.WaitFor(t, "[connected to OneChat]")
@@ -327,7 +327,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{}))
        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)
        require.Equal(t, "[connected to OneChat]\n", client.Output())
 
        clock.now = clock.now.Add(time.Hour)
@@ -335,7 +335,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{}))
        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")
 
        require.True(t, fake.Emit(&events.Connected{}))
        client.WaitFor(t, "WhatsApp is connected again.\n")
@@ -486,11 +486,15 @@ func TestWhatsAppPrivateBatchStartsATurnWithAnEmptyReply(t *testing.T) {
        socket, memory := daemonStartTestWithWhatsApp(t, server, bot, whatsapp)
        client := terminalStartTestClient(t, socket)
        client.WaitFor(t, "[connected to OneChat]")
        socket, memory := daemonStartTestWithWhatsApp(t, server, bot, whatsapp)
        client := terminalStartTestClient(t, socket)
        client.WaitFor(t, "[connected to OneChat]")
-       whatsappTestConnect(t, fake)
+       <-fake.connects
 
 
+       // The catch-up holds the batch open until its end, so the two messages
+       // form one batch also on a slow machine.
+       require.True(t, fake.Emit(&events.Connected{}))
        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)))
        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)))
+       require.True(t, fake.Emit(&events.OfflineSyncCompleted{}))
 
        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")
 
        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")