X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/0018014ab36f3de61a4b8a5515009dab803d6c38..8d9547f6e7ffa1e95ad25d9f8f2e7cfd23d992d0:/whatsapp_test.go diff --git a/whatsapp_test.go b/whatsapp_test.go index e0018e5..5c38d59 100644 --- a/whatsapp_test.go +++ b/whatsapp_test.go @@ -37,12 +37,38 @@ func TestWhatsAppStatusFollowsTheConnection(t *testing.T) { func TestWhatsAppStatusOfAFirstConnectionThatFailed(t *testing.T) { dir := t.TempDir() whatsappTestSession(t, dir, true) - whatsapp, fake, _ := whatsappStartTest(t, dir, newTestClock()) + whatsapp, fake, logs := 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()) + require.Equal(t, "new WhatsApp messages wait, because the first connection failed (the next try is automatic)\n", logs.String()) +} + +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{})) + 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{ + "WhatsApp is connected, and new messages come in", + "new WhatsApp messages wait, because the connection is lost (the next try is automatic)", + "WhatsApp is 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) { @@ -134,6 +160,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()) @@ -314,7 +348,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]") @@ -323,7 +357,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) @@ -331,7 +365,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") @@ -362,7 +396,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, "WhatsApp messages do not come, because the connection failed, and it is tried again only at a restart of the daemon: no route for OWN@s.whatsapp.net and OWN@lid\n", logs.String()) } func TestWhatsAppPhone(t *testing.T) { @@ -448,22 +482,8 @@ 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"}`)) }